Npcfid* npc_auth (Npcfsys *fs, char *aname, u32 uid, AuthFun auth) { Npcfid *afid = NULL; Npfcall *tc = NULL, *rc = NULL; if (!(afid = npc_fid_alloc (fs))) goto done; if (!(tc = np_create_tauth (afid->fid, NULL, aname, uid))) { np_uerror (ENOMEM); npc_fid_free (afid); afid = NULL; goto done; } if (afid->fsys->rpc (afid->fsys, tc, &rc) < 0) { npc_fid_free (afid); afid = NULL; goto done; } if (auth && auth (afid, uid) < 0) { int saved_err = np_rerror (); (void)npc_clunk (afid); afid = NULL; np_uerror (saved_err); goto done; } done: if (tc) free(tc); if (rc) free(rc); return afid; }
static void _send_tauth (Nptrans *t) { Npfcall *tc, *rc = _alloc_rc (); tc = np_create_tauth (0, NULL, "/foo", 0); if (!tc) msg_exit ("oom"); ttrans_rpc (t, tc, rc); free (tc); free (rc); }
static void test_tauth (void) { Npfcall *fc, *fc2; if (!(fc = np_create_tauth (1, "abc", "xyz", 4))) msg_exit ("out of memory in %s", __FUNCTION__); fc2 = _rcv_buf (fc, P9_TAUTH, __FUNCTION__); assert (fc->u.tauth.afid == fc2->u.tauth.afid); assert (np_str9cmp (&fc->u.tauth.uname, &fc2->u.tauth.uname) == 0); assert (np_str9cmp (&fc->u.tauth.aname, &fc2->u.tauth.aname) == 0); assert (fc->u.tauth.n_uname == fc2->u.tauth.n_uname); free (fc); free (fc2); }
Npcfsys* npc_mount(int fd, char *aname, Npuser *user, int (*auth)(Npcfid *afid, Npuser *user, void *aux), void *aux) { Npcfsys *fs; Npfcall *tc, *rc; fs = npc_create_fsys(fd, 8216); if (!fs) return NULL; tc = np_create_tversion(8216, "9P2000.u"); if (npc_rpc(fs, tc, &rc) < 0) goto error; if (rc->version.len==8 && !memcmp(rc->version.str, "9P2000.u", 8)) { fs->dotu = 1; } else if (rc->version.len==6 && !memcmp(rc->version.str, "9P2000", 6)) { fs->dotu = 0; } else { np_werror("unsupported 9P version", EIO); goto error; } free(tc); free(rc); tc = rc = NULL; if (auth) { fs->afid = npc_fid_alloc(fs); if (!fs->afid) goto error; tc = np_create_tauth(fs->afid->fid, user?user->uname:NULL, aname, user?user->uid:-1, fs->dotu); if (npc_rpc(fs, tc, &rc) < 0) { npc_fid_free(fs->afid); fs->afid = NULL; } else if ((*auth)(fs->afid, user, aux) < 0) goto error; free(tc); free(rc); tc = rc = NULL; } fs->root = npc_fid_alloc(fs); if (!fs->root) goto error; tc = np_create_tattach(fs->root->fid, fs->afid?fs->afid->fid:NOFID, user->uname, aname, user->uid, fs->dotu); if (npc_rpc(fs, tc, &rc) < 0) goto error; free(tc); free(rc); return fs; error: free(tc); free(rc); npc_disconnect_fsys(fs); npc_decref_fsys(fs); return NULL; }