📄 mount_9fs.c
字号:
exit(0); } (void) setsid(); (void) close(STDIN_FILENO); (void) close(STDOUT_FILENO); (void) close(STDERR_FILENO); (void) chdir("/"); } openlog("mount_nfs:", LOG_PID, LOG_DAEMON); nfssvc_flag = NFSSVC_MNTD; ncd.ncd_dirp = name; while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) { if (errno != ENEEDAUTH) { syslog(LOG_ERR, "nfssvc err %m"); continue; } nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;#ifdef NFSKERB /* * Set up as ncd_authuid for the kerberos call. * Must set ruid to ncd_authuid and reset the * ticket name iff ncd_authuid is not the same * as last time, so that the right ticket file * is found. * Get the Kerberos credential structure so that * we have the session key and get a ticket for * this uid. * For more info see the IETF Draft "Authentication * in ONC RPC". */ if (ncd.ncd_authuid != last_ruid) { char buf[512]; (void)sprintf(buf, "%s%d", TKT_ROOT, ncd.ncd_authuid); krb_set_tkt_string(buf); last_ruid = ncd.ncd_authuid; } setreuid(ncd.ncd_authuid, 0); kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr); if (kret == RET_NOTKT) { kret = get_ad_tkt(NFS_KERBSRV, inst, realm, DEFAULT_TKT_LIFE); if (kret == KSUCCESS) kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr); } if (kret == KSUCCESS) kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst, realm, 0); /* * Fill in the AKN_FULLNAME authenticator and verifier. * Along with the Kerberos ticket, we need to build * the timestamp verifier and encrypt it in CBC mode. */ if (kret == KSUCCESS && ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED) && gettimeofday(&ktv, (struct timezone *)0) == 0) { ncd.ncd_authtype = RPCAUTH_KERB4; ncd.ncd_authstr = (u_char *)&ktick; ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) + 3 * NFSX_UNSIGNED; ncd.ncd_verfstr = (u_char *)&kverf; ncd.ncd_verflen = sizeof (kverf); memmove(ncd.ncd_key, kcr.session, sizeof (kcr.session)); kin.t1 = htonl(ktv.tv_sec); kin.t2 = htonl(ktv.tv_usec); kin.w1 = htonl(NFS_KERBTTL); kin.w2 = htonl(NFS_KERBTTL - 1); bzero((caddr_t)kivec, sizeof (kivec)); /* * Encrypt kin in CBC mode using the session * key in kcr. */ XXX /* * Finally, fill the timestamp verifier into the * authenticator and verifier. */ ktick.kind = htonl(RPCAKN_FULLNAME); kverf.kind = htonl(RPCAKN_FULLNAME); NFS_KERBW1(ktick.kt) = kout.w1; ktick.kt.length = htonl(ktick.kt.length); kverf.verf.t1 = kout.t1; kverf.verf.t2 = kout.t2; kverf.verf.w2 = kout.w2; nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH; } setreuid(0, 0);#endif /* NFSKERB */ } } exit(0);}/* * Return RPC_SUCCESS if server responds. */enum clnt_statpingnfsserver(addr, version, sotype) struct sockaddr_in *addr; int version; int sotype;{ struct sockaddr_in sin; int tport; CLIENT *clp; int so = RPC_ANYSOCK; enum clnt_stat stat; struct timeval pertry, try; sin = *addr; if ((tport = port_no ? port_no : pmap_getport(&sin, RPCPROG_NFS, version, nfsproto)) == 0) { return rpc_createerr.cf_stat; } sin.sin_port = htons(tport); pertry.tv_sec = 10; pertry.tv_usec = 0; if (sotype == SOCK_STREAM) clp = clnttcp_create(&sin, RPCPROG_NFS, version, &so, 0, 0); else clp = clntudp_create(&sin, RPCPROG_NFS, version, pertry, &so); if (clp == NULL) return rpc_createerr.cf_stat; try.tv_sec = 10; try.tv_usec = 0; stat = clnt_call(clp, NFSPROC_NULL, xdr_void, NULL, xdr_void, NULL, try); clnt_destroy(clp); return stat;}int load_9uid(struct u9fs_args * nfsargsp){ FILE * fd; char line[80], * cp; int nusers, siz, n; struct p9user * p9p, * p9alloc; if( (fd = fopen("/etc/9uid.conf", "r")) == 0 ) errx(1, "fopen"); siz = 128; if( (p9alloc = malloc(siz*sizeof(struct p9user))) == 0 ) errx(1, "malloc"); nusers = 0; p9p = p9alloc; while(1) { if( nusers < siz ) { if ( fgets(line, 80, fd) == 0 ) break; cp = line; if ( strsep(&cp, " \t") == 0 ) errx(1, "bad format in 9uid.conf"); p9p->p9_uid = atoi(cp); strncpy(p9p->p9_name, line, U9FS_NAMELEN); nusers++; p9p++; } else { if( (p9p = realloc(p9alloc, 2*siz*sizeof(struct p9user))) == 0 ) errx(1, "realloc"); p9alloc = p9p; p9p = p9alloc + siz; siz <<= 1; } } nfsargsp->nusers = nusers; nfsargsp->users = p9alloc; return 0;}intpasstokey(char *key, char *p){ u_char buf[U9FS_NAMELEN], *t; int i, n; n = strlen(p); if(n >= U9FS_NAMELEN) n = U9FS_NAMELEN-1; memset(buf, ' ', 8); t = buf; strncpy((char*)t, p, n); t[n] = '\0'; memset(key, 0, U9AUTH_DESKEYLEN); for(;;){ for(i = 0; i < U9AUTH_DESKEYLEN; i++) key[i] = (t[i] >> i) + (t[i+1] << (8 - (i+1))); if(n <= 8) return 1; n -= 8; t += 8; if(n < 8){ t -= 8 - n; n = 8; } encrypt9(key, t, 8); } return 1; /* not reached */}void load_9key(struct u9fs_args * nfsargsp){ char * p; p = getpass("Plan 9 Password: "); passtokey(nfsargsp->key, p);}void gethostaddr(char * hostp, struct sockaddr_in * saddr){ struct hostent *hp; /* * Handle an internet host address and reverse resolve it if * doing Kerberos. */ if (isdigit(*hostp)) { if ((saddr->sin_addr.s_addr = inet_addr(hostp)) == -1) { warnx("bad net address %s", hostp); } } else if ((hp = gethostbyname(hostp)) != NULL) memmove(&saddr->sin_addr, hp->h_addr, MIN(hp->h_length, sizeof(saddr->sin_addr))); else { warnx("can't get net id for host"); }}intgetnfsargs(spec, nfsargsp) char *spec; struct u9fs_args *nfsargsp;{ register CLIENT *clp; struct hostent *hp; static struct sockaddr_in saddr;#ifdef ISO static struct sockaddr_iso isoaddr; struct iso_addr *isop; int isoflag = 0;#endif struct timeval pertry, try; enum clnt_stat clnt_stat; int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt; char *hostp, *delimp;#ifdef NFSKERB char *cp;#endif u_short tport; static struct nfhret nfhret; static char nam[MNAMELEN + 1]; strncpy(nam, spec, MNAMELEN); nam[MNAMELEN] = '\0'; if ((delimp = strchr(spec, '@')) != NULL) { hostp = delimp + 1; } else if ((delimp = strchr(spec, ':')) != NULL) { hostp = spec; spec = delimp + 1; } else { warnx("no <host>:<dirpath> or <dirpath>@<host> spec"); return (0); } *delimp = '\0'; /* * DUMB!! Until the mount protocol works on iso transport, we must * supply both an iso and an inet address for the host. */#ifdef ISO if (!strncmp(hostp, "iso=", 4)) { u_short isoport; hostp += 4; isoflag++; if ((delimp = strchr(hostp, '+')) == NULL) { warnx("no iso+inet address"); return (0); } *delimp = '\0'; if ((isop = iso_addr(hostp)) == NULL) { warnx("bad ISO address"); return (0); } memset(&isoaddr, 0, sizeof (isoaddr)); memmove(&isoaddr.siso_addr, isop, sizeof (struct iso_addr)); isoaddr.siso_len = sizeof (isoaddr); isoaddr.siso_family = AF_ISO; isoaddr.siso_tlen = 2; isoport = htons(NFS_PORT); memmove(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen); hostp = delimp + 1; }#endif /* ISO */ gethostaddr(hostp, & saddr);#ifdef NFSKERB if ((nfsargsp->flags & NFSMNT_KERB)) { if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr, sizeof (u_long), AF_INET)) == (struct hostent *)0) { warnx("can't reverse resolve net address"); return (0); } memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length, sizeof(saddr.sin_addr))); strncpy(inst, hp->h_name, INST_SZ); inst[INST_SZ - 1] = '\0'; if (cp = strchr(inst, '.')) *cp = '\0'; }#endif /* NFSKERB */ orgcnt = retrycnt;tryagain: if (mountmode == ANY || mountmode == V3) { nfsvers = 3; mntvers = 3; nfsargsp->flags |= NFSMNT_NFSV3; } else { nfsvers = 2; mntvers = 1; nfsargsp->flags &= ~NFSMNT_NFSV3; } tport = port_no ? port_no : U9FS_PORT;#if 0 nfhret.stat = EACCES; /* Mark not yet successful */ while (retrycnt > 0) { saddr.sin_family = AF_INET; saddr.sin_port = htons(PMAPPORT); if ((tport = port_no ? port_no : pmap_getport(&saddr, RPCPROG_NFS, nfsvers, nfsproto)) == 0) { if ((opflags & ISBGRND) == 0) clnt_pcreateerror("NFS Portmap"); } else { /* * First ping the nfs server to see if it supports * the version of the protocol we want to use. */ clnt_stat = pingnfsserver(&saddr, nfsvers, nfsargsp->sotype); if (clnt_stat == RPC_PROGVERSMISMATCH) { if (mountmode == ANY) { mountmode = V2; goto tryagain; } else { errx(1, "can't contact NFS server"); } } saddr.sin_port = 0; pertry.tv_sec = 10; pertry.tv_usec = 0; if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM) clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers, &so, 0, 0); else clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers, pertry, &so); if (clp == NULL) { if ((opflags & ISBGRND) == 0) clnt_pcreateerror("Cannot MNT RPC"); } else { clp->cl_auth = authunix_create_default(); try.tv_sec = 10; try.tv_usec = 0; if (nfsargsp->flags & NFSMNT_KERB) nfhret.auth = RPCAUTH_KERB4; else nfhret.auth = RPCAUTH_UNIX; nfhret.vers = mntvers; clnt_stat = clnt_call(clp, RPCMNT_MOUNT, xdr_dir, spec, xdr_fh, &nfhret, try); if (clnt_stat != RPC_SUCCESS) { if (clnt_stat == RPC_PROGVERSMISMATCH) { if (mountmode == ANY) { mountmode = V2; goto tryagain; } else { errx(1, "%s", clnt_sperror(clp, "MNT RPC")); } } if ((opflags & ISBGRND) == 0) warnx("%s", clnt_sperror(clp, "bad MNT RPC")); } else { auth_destroy(clp->cl_auth); clnt_destroy(clp); retrycnt = 0; } } } if (--retrycnt > 0) { if (opflags & BGRND) { opflags &= ~BGRND; if ((i = fork())) { if (i == -1) err(1, "nqnfs 2"); exit(0); } (void) setsid(); (void) close(STDIN_FILENO); (void) close(STDOUT_FILENO); (void) close(STDERR_FILENO); (void) chdir("/"); opflags |= ISBGRND; } sleep(60); } } if (nfhret.stat) { if (opflags & ISBGRND) exit(1); warnx("can't access %s: %s", spec, strerror(nfhret.stat)); return (0); }#endif saddr.sin_family = AF_INET; saddr.sin_port = htons(tport);#ifdef ISO if (isoflag) { nfsargsp->addr = (struct sockaddr *) &isoaddr; nfsargsp->addrlen = sizeof (isoaddr); } else#endif /* ISO */ { nfsargsp->addr = (struct sockaddr *) &saddr; nfsargsp->addrlen = sizeof (saddr); }#if 0 nfsargsp->fh = nfhret.nfh;#endif nfsargsp->fhsize = nfhret.fhsize; nfsargsp->hostname = nam; load_9key(nfsargsp); if( load_9uid(nfsargsp) ) errx(1, "can't load 9uid.conf"); return (1);}/* * xdr routines for mount rpc's */intxdr_dir(xdrsp, dirp) XDR *xdrsp; char *dirp;{ return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));}intxdr_fh(xdrsp, np) XDR *xdrsp; register struct nfhret *np;{ register int i; long auth, authcnt, authfnd = 0; if (!xdr_u_long(xdrsp, &np->stat)) return (0); if (np->stat) return (1); switch (np->vers) { case 1: np->fhsize = NFSX_V2FH; return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); case 3: if (!xdr_long(xdrsp, &np->fhsize)) return (0); if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) return (0); if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) return (0); if (!xdr_long(xdrsp, &authcnt)) return (0); for (i = 0; i < authcnt; i++) { if (!xdr_long(xdrsp, &auth)) return (0); if (auth == np->auth) authfnd++; } /* * Some servers, such as DEC's OSF/1 return a nil authenticator * list to indicate RPCAUTH_UNIX. */ if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) np->stat = EAUTH; return (1); }; return (0);}voidusage(){ (void)fprintf(stderr, "%s\n%s\n%s\n%s\n","usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]"," [-L leaseterm] [-R retrycnt] [-a maxreadahead]"," [-g maxgroups] [-m realm] [-o options] [-r readsize]"," [-t timeout] [-w writesize] [-x retrans] rhost:path node"); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -