2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)mountd.c 8.15 (Berkeley) 5/1/95
38 * $FreeBSD: src/sbin/mountd/mountd.c,v 1.39.2.5 2002/09/13 15:57:43 joerg Exp $
39 * $DragonFly: src/sbin/mountd/mountd.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
49 #include <rpc/pmap_clnt.h>
50 #include <nfs/rpcv2.h>
51 #include <nfs/nfsproto.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <msdosfs/msdosfsmount.h>
55 #include <ntfs/ntfsmount.h>
56 #include <isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */
58 #include <arpa/inet.h>
71 #include "pathnames.h"
78 * Structures for keeping the mount list and export list
81 struct mountlist *ml_next;
82 char ml_host[RPCMNT_NAMELEN+1];
83 char ml_dirp[RPCMNT_PATHLEN+1];
87 struct dirlist *dp_left;
88 struct dirlist *dp_right;
90 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
91 char dp_dirp[1]; /* Actually malloc'd to size of dir */
95 #define DP_HOSTSET 0x2
99 struct exportlist *ex_next;
100 struct dirlist *ex_dirl;
101 struct dirlist *ex_defdir;
108 #define EX_LINKED 0x1
117 struct hostent *gt_hostent;
118 struct netmsk gt_net;
123 union grouptypes gr_ptr;
124 struct grouplist *gr_next;
130 #define GT_IGNORE 0x5
133 int ht_flag; /* Uses DP_xx bits */
134 struct grouplist *ht_grp;
135 struct hostlist *ht_next;
145 char *add_expdir __P((struct dirlist **, char *, int));
146 void add_dlist __P((struct dirlist **, struct dirlist *,
147 struct grouplist *, int));
148 void add_mlist __P((char *, char *));
149 int check_dirpath __P((char *));
150 int check_options __P((struct dirlist *));
151 int chk_host __P((struct dirlist *, u_int32_t, int *, int *));
152 void del_mlist __P((char *, char *));
153 struct dirlist *dirp_search __P((struct dirlist *, char *));
154 int do_mount __P((struct exportlist *, struct grouplist *, int,
155 struct ucred *, char *, int, struct statfs *));
156 int do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
157 int *, int *, struct ucred *));
158 struct exportlist *ex_search __P((fsid_t *));
159 struct exportlist *get_exp __P((void));
160 void free_dir __P((struct dirlist *));
161 void free_exp __P((struct exportlist *));
162 void free_grp __P((struct grouplist *));
163 void free_host __P((struct hostlist *));
164 void get_exportlist __P((void));
165 int get_host __P((char *, struct grouplist *, struct grouplist *));
166 int get_num __P((char *));
167 struct hostlist *get_ht __P((void));
168 int get_line __P((void));
169 void get_mountlist __P((void));
170 int get_net __P((char *, struct netmsk *, int));
171 void getexp_err __P((struct exportlist *, struct grouplist *));
172 struct grouplist *get_grp __P((void));
173 void hang_dirp __P((struct dirlist *, struct grouplist *,
174 struct exportlist *, int));
175 void mntsrv __P((struct svc_req *, SVCXPRT *));
176 void nextfield __P((char **, char **));
177 void out_of_mem __P((void));
178 void parsecred __P((char *, struct ucred *));
179 int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
180 int scan_tree __P((struct dirlist *, u_int32_t));
181 static void usage __P((void));
182 int xdr_dir __P((XDR *, char *));
183 int xdr_explist __P((XDR *, caddr_t));
184 int xdr_fhs __P((XDR *, caddr_t));
185 int xdr_mlist __P((XDR *, caddr_t));
187 struct exportlist *exphead;
188 struct mountlist *mlhead;
189 struct grouplist *grphead;
190 char exname[MAXPATHLEN];
191 struct ucred def_anon = {
198 int resvport_only = 1;
203 #define OP_MAPROOT 0x01
204 #define OP_MAPALL 0x02
208 #define OP_ALLDIRS 0x40
209 /* 0x80 is OP_HAVEMASK in FreeBSD 5+ */
210 #define OP_QUIET 0x100
214 void SYSLOG __P((int, const char *, ...));
215 #define syslog SYSLOG
221 * Mountd server for NFS mount protocol as described in:
222 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
223 * The optional arguments are the exports file name
224 * default: _PATH_EXPORTS
225 * and "-n" to allow nonroot mount.
232 SVCXPRT *udptransp, *tcptransp;
233 int c, error, mib[3];
236 error = getvfsbyname("nfs", &vfc);
237 if (error && vfsisloadable("nfs")) {
239 err(1, "vfsload(nfs)");
240 endvfsent(); /* flush cache */
241 error = getvfsbyname("nfs", &vfc);
244 errx(1, "NFS support is not available in the running kernel");
246 while ((c = getopt(argc, argv, "2dlnr")) != -1)
258 debug = debug ? 0 : 1;
268 grphead = (struct grouplist *)NULL;
269 exphead = (struct exportlist *)NULL;
270 mlhead = (struct mountlist *)NULL;
272 strncpy(exname, *argv, MAXPATHLEN-1);
273 exname[MAXPATHLEN-1] = '\0';
275 strcpy(exname, _PATH_EXPORTS);
276 openlog("mountd", LOG_PID, LOG_DAEMON);
278 warnx("getting export list");
281 warnx("getting mount list");
287 signal(SIGINT, SIG_IGN);
288 signal(SIGQUIT, SIG_IGN);
290 signal(SIGHUP, (void (*) __P((int))) get_exportlist);
291 { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
292 if (pidfile != NULL) {
293 fprintf(pidfile, "%d\n", getpid());
297 if (!resvport_only) {
299 mib[1] = vfc.vfc_typenum;
300 mib[2] = NFS_NFSPRIVPORT;
301 if (sysctl(mib, 3, NULL, NULL, &resvport_only,
302 sizeof(resvport_only)) != 0 && errno != ENOENT) {
303 syslog(LOG_ERR, "sysctl: %m");
307 if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
308 (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
309 syslog(LOG_ERR, "can't create socket");
312 pmap_unset(RPCPROG_MNT, 1);
313 pmap_unset(RPCPROG_MNT, 3);
315 if (!svc_register(udptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_UDP) ||
316 !svc_register(tcptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_TCP)) {
317 syslog(LOG_ERR, "can't register mount");
320 if (!svc_register(udptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_UDP) ||
321 !svc_register(tcptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_TCP)) {
322 syslog(LOG_ERR, "can't register mount");
326 syslog(LOG_ERR, "mountd died");
334 "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
339 * The mount rpc service
342 mntsrv(rqstp, transp)
343 struct svc_req *rqstp;
346 struct exportlist *ep;
352 struct in_addr saddrin;
355 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
356 int bad = 0, defset, hostset;
357 sigset_t sighup_mask;
359 sigemptyset(&sighup_mask);
360 sigaddset(&sighup_mask, SIGHUP);
361 saddr = transp->xp_raddr.sin_addr.s_addr;
362 saddrin = transp->xp_raddr.sin_addr;
363 sport = ntohs(transp->xp_raddr.sin_port);
364 hp = (struct hostent *)NULL;
365 switch (rqstp->rq_proc) {
367 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
368 syslog(LOG_ERR, "can't send reply");
371 if (sport >= IPPORT_RESERVED && resvport_only) {
373 "mount request from %s from unprivileged port",
375 svcerr_weakauth(transp);
378 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
379 syslog(LOG_NOTICE, "undecodable mount request from %s",
381 svcerr_decode(transp);
386 * Get the real pathname and make sure it is a directory
387 * or a regular file if the -r option was specified
390 if (realpath(rpcpath, dirpath) == NULL ||
391 stat(dirpath, &stb) < 0 ||
392 (!S_ISDIR(stb.st_mode) &&
393 (dir_only || !S_ISREG(stb.st_mode))) ||
394 statfs(dirpath, &fsb) < 0) {
395 chdir("/"); /* Just in case realpath doesn't */
397 "mount request from %s for non existent path %s",
398 inet_ntoa(saddrin), dirpath);
400 warnx("stat failed on %s", dirpath);
401 bad = ENOENT; /* We will send error reply later */
404 /* Check in the exports list */
405 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
406 ep = ex_search(&fsb.f_fsid);
407 hostset = defset = 0;
408 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
409 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
410 chk_host(dp, saddr, &defset, &hostset)) ||
411 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
412 scan_tree(ep->ex_dirl, saddr) == 0))) {
414 if (!svc_sendreply(transp, xdr_long,
416 syslog(LOG_ERR, "can't send reply");
417 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
420 if (hostset & DP_HOSTSET)
421 fhr.fhr_flag = hostset;
423 fhr.fhr_flag = defset;
424 fhr.fhr_vers = rqstp->rq_vers;
425 /* Get the file handle */
426 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
427 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
429 syslog(LOG_ERR, "can't get fh for %s", dirpath);
430 if (!svc_sendreply(transp, xdr_long,
432 syslog(LOG_ERR, "can't send reply");
433 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
436 if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
437 syslog(LOG_ERR, "can't send reply");
439 hp = gethostbyaddr((caddr_t)&saddr,
440 sizeof(saddr), AF_INET);
442 add_mlist(hp->h_name, dirpath);
444 add_mlist(inet_ntoa(saddrin),
447 warnx("mount successful");
450 "mount request succeeded from %s for %s",
451 inet_ntoa(saddrin), dirpath);
455 "mount request denied from %s for %s",
456 inet_ntoa(saddrin), dirpath);
459 if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
460 syslog(LOG_ERR, "can't send reply");
461 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
464 if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
465 syslog(LOG_ERR, "can't send reply");
468 "dump request succeeded from %s",
472 if (sport >= IPPORT_RESERVED && resvport_only) {
474 "umount request from %s from unprivileged port",
476 svcerr_weakauth(transp);
479 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
480 syslog(LOG_NOTICE, "undecodable umount request from %s",
482 svcerr_decode(transp);
485 if (realpath(rpcpath, dirpath) == NULL) {
486 syslog(LOG_NOTICE, "umount request from %s "
487 "for non existent path %s",
488 inet_ntoa(saddrin), dirpath);
490 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
491 syslog(LOG_ERR, "can't send reply");
492 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
494 del_mlist(hp->h_name, dirpath);
495 del_mlist(inet_ntoa(saddrin), dirpath);
498 "umount request succeeded from %s for %s",
499 inet_ntoa(saddrin), dirpath);
502 if (sport >= IPPORT_RESERVED && resvport_only) {
504 "umountall request from %s from unprivileged port",
506 svcerr_weakauth(transp);
509 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
510 syslog(LOG_ERR, "can't send reply");
511 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
513 del_mlist(hp->h_name, (char *)NULL);
514 del_mlist(inet_ntoa(saddrin), (char *)NULL);
517 "umountall request succeeded from %s",
521 if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
522 syslog(LOG_ERR, "can't send reply");
525 "export request succeeded from %s",
529 svcerr_noproc(transp);
535 * Xdr conversion for a dirpath string
542 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
546 * Xdr routine to generate file handle reply
553 register struct fhreturn *fhrp = (struct fhreturn *)cp;
554 u_long ok = 0, len, auth;
556 if (!xdr_long(xdrsp, &ok))
558 switch (fhrp->fhr_vers) {
560 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
563 if (!xdr_long(xdrsp, &len))
565 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
567 if (fhrp->fhr_flag & DP_KERB)
568 auth = RPCAUTH_KERB4;
572 if (!xdr_long(xdrsp, &len))
574 return (xdr_long(xdrsp, &auth));
584 struct mountlist *mlp;
591 if (!xdr_bool(xdrsp, &true))
593 strp = &mlp->ml_host[0];
594 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
596 strp = &mlp->ml_dirp[0];
597 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
601 if (!xdr_bool(xdrsp, &false))
607 * Xdr conversion for export list
610 xdr_explist(xdrsp, cp)
614 struct exportlist *ep;
617 sigset_t sighup_mask;
619 sigemptyset(&sighup_mask);
620 sigaddset(&sighup_mask, SIGHUP);
621 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
625 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
627 if (ep->ex_defdir && putdef == 0 &&
628 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
633 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
634 if (!xdr_bool(xdrsp, &false))
638 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
643 * Called from xdr_explist() to traverse the tree and export the
647 put_exlist(dp, xdrsp, adp, putdefp)
653 struct grouplist *grp;
661 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
663 if (!xdr_bool(xdrsp, &true))
666 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
668 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
672 if ((dp->dp_flag & DP_DEFSET) == 0 &&
673 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
677 if (grp->gr_type == GT_HOST) {
678 if (!xdr_bool(xdrsp, &true))
680 strp = grp->gr_ptr.gt_hostent->h_name;
681 if (!xdr_string(xdrsp, &strp,
684 } else if (grp->gr_type == GT_NET) {
685 if (!xdr_bool(xdrsp, &true))
687 strp = grp->gr_ptr.gt_net.nt_name;
688 if (!xdr_string(xdrsp, &strp,
693 if (gotalldir && hp == (struct hostlist *)NULL) {
699 if (!xdr_bool(xdrsp, &false))
701 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
707 #define LINESIZ 10240
712 * Get the export list
717 struct exportlist *ep, *ep2;
718 struct grouplist *grp, *tgrp;
719 struct exportlist **epp;
720 struct dirlist *dirhead;
721 struct statfs fsb, *fsp;
724 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
725 int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
731 * First, get rid of the old list
739 exphead = (struct exportlist *)NULL;
747 grphead = (struct grouplist *)NULL;
750 * And delete exports that are in the kernel for all local
752 * XXX: Should know how to handle all local exportable file systems
753 * instead of just "ufs".
755 num = getmntinfo(&fsp, MNT_NOWAIT);
756 for (i = 0; i < num; i++) {
761 struct msdosfs_args da;
765 if (!strcmp(fsp->f_fstypename, "mfs") ||
766 !strcmp(fsp->f_fstypename, "ufs") ||
767 !strcmp(fsp->f_fstypename, "msdos") ||
768 !strcmp(fsp->f_fstypename, "ntfs") ||
769 !strcmp(fsp->f_fstypename, "cd9660")) {
770 targs.ua.fspec = NULL;
771 targs.ua.export.ex_flags = MNT_DELEXPORT;
772 if (mount(fsp->f_fstypename, fsp->f_mntonname,
773 fsp->f_flags | MNT_UPDATE,
774 (caddr_t)&targs) < 0)
775 syslog(LOG_ERR, "can't delete exports for %s",
782 * Read in the exports file and build the list, calling
783 * mount() as we go along to push the export rules into the kernel.
785 if ((exp_file = fopen(exname, "r")) == NULL) {
786 syslog(LOG_ERR, "can't open %s", exname);
789 dirhead = (struct dirlist *)NULL;
792 warnx("got line %s", line);
794 nextfield(&cp, &endcp);
803 exflags = MNT_EXPORTED;
806 ep = (struct exportlist *)NULL;
809 * Create new exports list entry
812 tgrp = grp = get_grp();
814 if (len > RPCMNT_NAMELEN) {
815 getexp_err(ep, tgrp);
819 if (ep == (struct exportlist *)NULL) {
820 getexp_err(ep, tgrp);
824 warnx("doing opt %s", cp);
826 if (do_opt(&cp, &endcp, ep, grp, &has_host,
828 getexp_err(ep, tgrp);
831 } else if (*cp == '/') {
834 if (check_dirpath(cp) &&
835 statfs(cp, &fsb) >= 0) {
837 syslog(LOG_ERR, "dirs must be first");
838 getexp_err(ep, tgrp);
842 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
843 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
844 getexp_err(ep, tgrp);
849 * See if this directory is already
852 ep = ex_search(&fsb.f_fsid);
853 if (ep == (struct exportlist *)NULL) {
855 ep->ex_fs = fsb.f_fsid;
856 ep->ex_fsdir = (char *)
857 malloc(strlen(fsb.f_mntonname) + 1);
864 warnx("making new ep fs=0x%x,0x%x",
868 warnx("found ep fs=0x%x,0x%x",
874 * Add dirpath to export mount point.
876 dirp = add_expdir(&dirhead, cp, len);
879 getexp_err(ep, tgrp);
887 if (ep == (struct exportlist *)NULL) {
888 getexp_err(ep, tgrp);
893 * Get the host or netgroup.
896 netgrp = getnetgrent(&hst, &usr, &dom);
899 grp->gr_next = get_grp();
905 "null hostname in netgroup %s, skipping", cp);
906 grp->gr_type = GT_IGNORE;
907 } else if (get_host(hst, grp, tgrp)) {
909 "bad host %s in netgroup %s, skipping", hst, cp);
910 grp->gr_type = GT_IGNORE;
912 } else if (get_host(cp, grp, tgrp)) {
913 syslog(LOG_ERR, "bad host %s, skipping", cp);
914 grp->gr_type = GT_IGNORE;
917 } while (netgrp && getnetgrent(&hst, &usr, &dom));
922 nextfield(&cp, &endcp);
925 if (check_options(dirhead)) {
926 getexp_err(ep, tgrp);
930 grp->gr_type = GT_HOST;
932 warnx("adding a default entry");
933 /* add a default group and make the grp list NULL */
934 hpe = (struct hostent *)malloc(sizeof(struct hostent));
935 if (hpe == (struct hostent *)NULL)
937 hpe->h_name = strdup("Default");
938 hpe->h_addrtype = AF_INET;
939 hpe->h_length = sizeof (u_int32_t);
940 hpe->h_addr_list = (char **)NULL;
941 grp->gr_ptr.gt_hostent = hpe;
944 * Don't allow a network export coincide with a list of
945 * host(s) on the same line.
947 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
948 getexp_err(ep, tgrp);
952 * If an export list was specified on this line, make sure
953 * that we have at least one valid entry, otherwise skip it.
957 while (grp && grp->gr_type == GT_IGNORE)
960 getexp_err(ep, tgrp);
966 * Loop through hosts, pushing the exports into the kernel.
967 * After loop, tgrp points to the start of the list and
968 * grp points to the last entry in the list.
972 if (do_mount(ep, grp, exflags, &anon, dirp,
974 getexp_err(ep, tgrp);
977 } while (grp->gr_next && (grp = grp->gr_next));
980 * Success. Update the data structures.
983 hang_dirp(dirhead, tgrp, ep, opt_flags);
984 grp->gr_next = grphead;
987 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
991 dirhead = (struct dirlist *)NULL;
992 if ((ep->ex_flag & EX_LINKED) == 0) {
997 * Insert in the list in alphabetical order.
999 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1000 epp = &ep2->ex_next;
1006 ep->ex_flag |= EX_LINKED;
1011 dirhead = (struct dirlist *)NULL;
1018 * Allocate an export list element
1023 struct exportlist *ep;
1025 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
1026 if (ep == (struct exportlist *)NULL)
1028 memset(ep, 0, sizeof(struct exportlist));
1033 * Allocate a group list element
1038 struct grouplist *gp;
1040 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
1041 if (gp == (struct grouplist *)NULL)
1043 memset(gp, 0, sizeof(struct grouplist));
1048 * Clean up upon an error in get_exportlist().
1052 struct exportlist *ep;
1053 struct grouplist *grp;
1055 struct grouplist *tgrp;
1057 if (!(opt_flags & OP_QUIET))
1058 syslog(LOG_ERR, "bad exports list line %s", line);
1059 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1069 * Search the export list for a matching fs.
1075 struct exportlist *ep;
1079 if (ep->ex_fs.val[0] == fsid->val[0] &&
1080 ep->ex_fs.val[1] == fsid->val[1])
1088 * Add a directory path to the list.
1091 add_expdir(dpp, cp, len)
1092 struct dirlist **dpp;
1098 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1099 if (dp == (struct dirlist *)NULL)
1102 dp->dp_right = (struct dirlist *)NULL;
1104 dp->dp_hosts = (struct hostlist *)NULL;
1105 strcpy(dp->dp_dirp, cp);
1107 return (dp->dp_dirp);
1111 * Hang the dir list element off the dirpath binary tree as required
1112 * and update the entry for host.
1115 hang_dirp(dp, grp, ep, flags)
1117 struct grouplist *grp;
1118 struct exportlist *ep;
1121 struct hostlist *hp;
1122 struct dirlist *dp2;
1124 if (flags & OP_ALLDIRS) {
1129 if (grp == (struct grouplist *)NULL) {
1130 ep->ex_defdir->dp_flag |= DP_DEFSET;
1131 if (flags & OP_KERB)
1132 ep->ex_defdir->dp_flag |= DP_KERB;
1133 } else while (grp) {
1135 if (flags & OP_KERB)
1136 hp->ht_flag |= DP_KERB;
1138 hp->ht_next = ep->ex_defdir->dp_hosts;
1139 ep->ex_defdir->dp_hosts = hp;
1145 * Loop through the directories adding them to the tree.
1149 add_dlist(&ep->ex_dirl, dp, grp, flags);
1156 * Traverse the binary tree either updating a node that is already there
1157 * for the new directory or adding the new node.
1160 add_dlist(dpp, newdp, grp, flags)
1161 struct dirlist **dpp;
1162 struct dirlist *newdp;
1163 struct grouplist *grp;
1167 struct hostlist *hp;
1172 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1174 add_dlist(&dp->dp_left, newdp, grp, flags);
1176 } else if (cmp < 0) {
1177 add_dlist(&dp->dp_right, newdp, grp, flags);
1180 free((caddr_t)newdp);
1183 dp->dp_left = (struct dirlist *)NULL;
1189 * Hang all of the host(s) off of the directory point.
1193 if (flags & OP_KERB)
1194 hp->ht_flag |= DP_KERB;
1196 hp->ht_next = dp->dp_hosts;
1201 dp->dp_flag |= DP_DEFSET;
1202 if (flags & OP_KERB)
1203 dp->dp_flag |= DP_KERB;
1208 * Search for a dirpath on the export point.
1211 dirp_search(dp, dirpath)
1218 cmp = strcmp(dp->dp_dirp, dirpath);
1220 return (dirp_search(dp->dp_left, dirpath));
1222 return (dirp_search(dp->dp_right, dirpath));
1230 * Scan for a host match in a directory tree.
1233 chk_host(dp, saddr, defsetp, hostsetp)
1239 struct hostlist *hp;
1240 struct grouplist *grp;
1244 if (dp->dp_flag & DP_DEFSET)
1245 *defsetp = dp->dp_flag;
1249 switch (grp->gr_type) {
1251 addrp = (u_int32_t **)
1252 grp->gr_ptr.gt_hostent->h_addr_list;
1254 if (**addrp == saddr) {
1255 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1262 if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
1263 grp->gr_ptr.gt_net.nt_net) {
1264 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1276 * Scan tree for a host that matches the address.
1279 scan_tree(dp, saddr)
1283 int defset, hostset;
1286 if (scan_tree(dp->dp_left, saddr))
1288 if (chk_host(dp, saddr, &defset, &hostset))
1290 if (scan_tree(dp->dp_right, saddr))
1297 * Traverse the dirlist tree and free it up.
1305 free_dir(dp->dp_left);
1306 free_dir(dp->dp_right);
1307 free_host(dp->dp_hosts);
1313 * Parse the option string and update fields.
1314 * Option arguments may either be -<option>=<value> or
1318 do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1319 char **cpp, **endcpp;
1320 struct exportlist *ep;
1321 struct grouplist *grp;
1326 char *cpoptarg, *cpoptend;
1327 char *cp, *endcp, *cpopt, savedc, savedc2;
1328 int allflag, usedarg;
1336 while (cpopt && *cpopt) {
1339 if ((cpoptend = strchr(cpopt, ','))) {
1341 if ((cpoptarg = strchr(cpopt, '=')))
1344 if ((cpoptarg = strchr(cpopt, '=')))
1348 nextfield(&cp, &endcp);
1350 if (endcp > cp && *cp != '-') {
1358 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1359 *exflagsp |= MNT_EXRDONLY;
1360 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1361 !(allflag = strcmp(cpopt, "mapall")) ||
1362 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1364 parsecred(cpoptarg, cr);
1366 *exflagsp |= MNT_EXPORTANON;
1367 opt_flags |= OP_MAPALL;
1369 opt_flags |= OP_MAPROOT;
1370 } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1371 *exflagsp |= MNT_EXKERB;
1372 opt_flags |= OP_KERB;
1373 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1374 !strcmp(cpopt, "m"))) {
1375 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1376 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
1380 opt_flags |= OP_MASK;
1381 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1382 !strcmp(cpopt, "n"))) {
1383 if (grp->gr_type != GT_NULL) {
1384 syslog(LOG_ERR, "network/host conflict");
1386 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1387 syslog(LOG_ERR, "bad net: %s", cpoptarg);
1390 grp->gr_type = GT_NET;
1393 opt_flags |= OP_NET;
1394 } else if (!strcmp(cpopt, "alldirs")) {
1395 opt_flags |= OP_ALLDIRS;
1396 } else if (!strcmp(cpopt, "public")) {
1397 *exflagsp |= MNT_EXPUBLIC;
1398 } else if (!strcmp(cpopt, "webnfs")) {
1399 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
1400 opt_flags |= OP_MAPALL;
1401 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1402 ep->ex_indexfile = strdup(cpoptarg);
1403 } else if (!strcmp(cpopt, "quiet")) {
1404 opt_flags |= OP_QUIET;
1406 syslog(LOG_ERR, "bad opt %s", cpopt);
1425 * Translate a character string to the corresponding list of network
1426 * addresses for a hostname.
1429 get_host(cp, grp, tgrp)
1431 struct grouplist *grp;
1432 struct grouplist *tgrp;
1434 struct grouplist *checkgrp;
1435 struct hostent *hp, *nhp;
1436 char **addrp, **naddrp;
1437 struct hostent t_host;
1442 if (grp->gr_type != GT_NULL)
1444 if ((hp = gethostbyname(cp)) == NULL) {
1446 saddr = inet_addr(cp);
1448 syslog(LOG_ERR, "inet_addr failed for %s", cp);
1451 if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
1452 AF_INET)) == NULL) {
1455 hp->h_addrtype = AF_INET;
1456 hp->h_length = sizeof (u_int32_t);
1457 hp->h_addr_list = aptr;
1458 aptr[0] = (char *)&saddr;
1459 aptr[1] = (char *)NULL;
1462 syslog(LOG_ERR, "gethostbyname failed for %s", cp);
1467 * Sanity check: make sure we don't already have an entry
1468 * for this host in the grouplist.
1471 while (checkgrp != NULL) {
1472 if (checkgrp->gr_type == GT_HOST &&
1473 checkgrp->gr_ptr.gt_hostent != NULL &&
1474 (!strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)
1475 || *(u_int32_t *)checkgrp->gr_ptr.gt_hostent->h_addr ==
1476 *(u_int32_t *)hp->h_addr)) {
1477 grp->gr_type = GT_IGNORE;
1480 checkgrp = checkgrp->gr_next;
1483 grp->gr_type = GT_HOST;
1484 nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
1485 malloc(sizeof(struct hostent));
1486 if (nhp == (struct hostent *)NULL)
1488 memmove(nhp, hp, sizeof(struct hostent));
1489 i = strlen(hp->h_name)+1;
1490 nhp->h_name = (char *)malloc(i);
1491 if (nhp->h_name == (char *)NULL)
1493 memmove(nhp->h_name, hp->h_name, i);
1494 addrp = hp->h_addr_list;
1498 naddrp = nhp->h_addr_list = (char **)malloc(i*sizeof(char *));
1499 if (naddrp == (char **)NULL)
1501 addrp = hp->h_addr_list;
1503 *naddrp = (char *)malloc(hp->h_length);
1504 if (*naddrp == (char *)NULL)
1506 memmove(*naddrp, *addrp, hp->h_length);
1510 *naddrp = (char *)NULL;
1512 warnx("got host %s", hp->h_name);
1517 * Free up an exports list component
1521 struct exportlist *ep;
1524 if (ep->ex_defdir) {
1525 free_host(ep->ex_defdir->dp_hosts);
1526 free((caddr_t)ep->ex_defdir);
1530 if (ep->ex_indexfile)
1531 free(ep->ex_indexfile);
1532 free_dir(ep->ex_dirl);
1541 struct hostlist *hp;
1543 struct hostlist *hp2;
1555 struct hostlist *hp;
1557 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1558 if (hp == (struct hostlist *)NULL)
1560 hp->ht_next = (struct hostlist *)NULL;
1566 * Out of memory, fatal
1572 syslog(LOG_ERR, "out of memory");
1577 * Do the mount syscall with the update flag to push the export info into
1581 do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1582 struct exportlist *ep;
1583 struct grouplist *grp;
1585 struct ucred *anoncrp;
1595 struct sockaddr_in sin, imask;
1601 struct msdosfs_args da;
1603 struct ntfs_args na;
1608 args.ua.export.ex_flags = exflags;
1609 args.ua.export.ex_anon = *anoncrp;
1610 args.ua.export.ex_indexfile = ep->ex_indexfile;
1611 memset(&sin, 0, sizeof(sin));
1612 memset(&imask, 0, sizeof(imask));
1613 sin.sin_family = AF_INET;
1614 sin.sin_len = sizeof(sin);
1615 imask.sin_family = AF_INET;
1616 imask.sin_len = sizeof(sin);
1617 if (grp->gr_type == GT_HOST)
1618 addrp = (u_int32_t **)grp->gr_ptr.gt_hostent->h_addr_list;
1620 addrp = (u_int32_t **)NULL;
1623 switch (grp->gr_type) {
1626 sin.sin_addr.s_addr = **addrp;
1627 args.ua.export.ex_addrlen = sizeof(sin);
1629 args.ua.export.ex_addrlen = 0;
1630 args.ua.export.ex_addr = (struct sockaddr *)&sin;
1631 args.ua.export.ex_masklen = 0;
1634 if (grp->gr_ptr.gt_net.nt_mask)
1635 imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
1637 net = ntohl(grp->gr_ptr.gt_net.nt_net);
1639 imask.sin_addr.s_addr = inet_addr("255.0.0.0");
1640 else if (IN_CLASSB(net))
1641 imask.sin_addr.s_addr =
1642 inet_addr("255.255.0.0");
1644 imask.sin_addr.s_addr =
1645 inet_addr("255.255.255.0");
1646 grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
1648 sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
1649 args.ua.export.ex_addr = (struct sockaddr *)&sin;
1650 args.ua.export.ex_addrlen = sizeof (sin);
1651 args.ua.export.ex_mask = (struct sockaddr *)&imask;
1652 args.ua.export.ex_masklen = sizeof (imask);
1658 syslog(LOG_ERR, "bad grouptype");
1666 * Maybe I should just use the fsb->f_mntonname path instead
1667 * of looping back up the dirp to the mount point??
1668 * Also, needs to know how to export all types of local
1669 * exportable file systems and not just "ufs".
1671 while (mount(fsb->f_fstypename, dirp,
1672 fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
1676 cp = dirp + dirplen - 1;
1677 if (opt_flags & OP_QUIET)
1679 if (errno == EPERM) {
1681 "can't change attributes for %s", dirp);
1684 if (opt_flags & OP_ALLDIRS) {
1685 if (errno == EINVAL)
1687 "-alldirs requested but %s is not a filesystem mountpoint",
1691 "could not remount %s: %m",
1695 /* back up over the last component */
1696 while (*cp == '/' && cp > dirp)
1698 while (*(cp - 1) != '/' && cp > dirp)
1702 warnx("mnt unsucc");
1703 syslog(LOG_ERR, "can't export %s", dirp);
1708 /* Check that we're still on the same filesystem. */
1709 if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
1710 &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
1712 syslog(LOG_ERR, "can't export %s", dirp);
1718 if (*addrp == (u_int32_t *)NULL)
1729 * Translate a net address.
1732 get_net(cp, net, maskflg)
1739 struct in_addr inetaddr, inetaddr2;
1742 if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
1743 inetaddr = inet_makeaddr(netaddr, 0);
1745 * Due to arbitrary subnet masks, you don't know how many
1746 * bits to shift the address to make it into a network,
1747 * however you do know how to make a network address into
1748 * a host with host == 0 and then compare them.
1753 while ((np = getnetent())) {
1754 inetaddr2 = inet_makeaddr(np->n_net, 0);
1755 if (inetaddr2.s_addr == inetaddr.s_addr)
1760 } else if ((np = getnetbyname(cp)) != NULL) {
1761 inetaddr = inet_makeaddr(np->n_net, 0);
1766 net->nt_mask = inetaddr.s_addr;
1771 name = inet_ntoa(inetaddr);
1772 net->nt_name = (char *)malloc(strlen(name) + 1);
1773 if (net->nt_name == (char *)NULL)
1775 strcpy(net->nt_name, name);
1776 net->nt_net = inetaddr.s_addr;
1782 * Parse out the next white space separated field
1785 nextfield(cp, endcp)
1792 while (*p == ' ' || *p == '\t')
1794 if (*p == '\n' || *p == '\0')
1798 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
1805 * Get an exports file line. Skip over blank lines and handle line
1813 int totlen, cont_line;
1816 * Loop around ignoring blank lines and getting all continuation lines.
1821 if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
1827 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
1840 if (totlen >= LINESIZ) {
1841 syslog(LOG_ERR, "exports line too long");
1846 } while (totlen == 0 || cont_line);
1851 * Parse a description of a credential.
1854 parsecred(namelist, cr)
1863 int ngroups, groups[NGROUPS + 1];
1866 * Set up the unprivileged user.
1870 cr->cr_groups[0] = -2;
1873 * Get the user's password table entry.
1875 names = strsep(&namelist, " \t\n");
1876 name = strsep(&names, ":");
1877 if (isdigit(*name) || *name == '-')
1878 pw = getpwuid(atoi(name));
1880 pw = getpwnam(name);
1882 * Credentials specified as those of a user.
1884 if (names == NULL) {
1886 syslog(LOG_ERR, "unknown user: %s", name);
1889 cr->cr_uid = pw->pw_uid;
1890 ngroups = NGROUPS + 1;
1891 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
1892 syslog(LOG_ERR, "too many groups");
1894 * Convert from int's to gid_t's and compress out duplicate
1896 cr->cr_ngroups = ngroups - 1;
1897 cr->cr_groups[0] = groups[0];
1898 for (cnt = 2; cnt < ngroups; cnt++)
1899 cr->cr_groups[cnt - 1] = groups[cnt];
1903 * Explicit credential specified as a colon separated list:
1907 cr->cr_uid = pw->pw_uid;
1908 else if (isdigit(*name) || *name == '-')
1909 cr->cr_uid = atoi(name);
1911 syslog(LOG_ERR, "unknown user: %s", name);
1915 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
1916 name = strsep(&names, ":");
1917 if (isdigit(*name) || *name == '-') {
1918 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
1920 if ((gr = getgrnam(name)) == NULL) {
1921 syslog(LOG_ERR, "unknown group: %s", name);
1924 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
1927 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
1928 syslog(LOG_ERR, "too many groups");
1931 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
1933 * Routines that maintain the remote mounttab
1938 struct mountlist *mlp, **mlpp;
1939 char *host, *dirp, *cp;
1943 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
1944 if (errno == ENOENT)
1947 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
1952 while (fgets(str, STRSIZ, mlfile) != NULL) {
1954 host = strsep(&cp, " \t\n");
1955 dirp = strsep(&cp, " \t\n");
1956 if (host == NULL || dirp == NULL)
1958 mlp = (struct mountlist *)malloc(sizeof (*mlp));
1959 if (mlp == (struct mountlist *)NULL)
1961 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
1962 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
1963 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
1964 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
1965 mlp->ml_next = (struct mountlist *)NULL;
1967 mlpp = &mlp->ml_next;
1973 del_mlist(hostp, dirp)
1976 struct mountlist *mlp, **mlpp;
1977 struct mountlist *mlp2;
1984 if (!strcmp(mlp->ml_host, hostp) &&
1985 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
1988 *mlpp = mlp = mlp->ml_next;
1989 free((caddr_t)mlp2);
1991 mlpp = &mlp->ml_next;
1996 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
1997 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2002 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2010 add_mlist(hostp, dirp)
2013 struct mountlist *mlp, **mlpp;
2019 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2021 mlpp = &mlp->ml_next;
2024 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2025 if (mlp == (struct mountlist *)NULL)
2027 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2028 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2029 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2030 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2031 mlp->ml_next = (struct mountlist *)NULL;
2033 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2034 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2037 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2042 * Free up a group list.
2046 struct grouplist *grp;
2050 if (grp->gr_type == GT_HOST) {
2051 if (grp->gr_ptr.gt_hostent->h_name) {
2052 addrp = grp->gr_ptr.gt_hostent->h_addr_list;
2053 while (addrp && *addrp)
2055 free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
2056 free(grp->gr_ptr.gt_hostent->h_name);
2058 free((caddr_t)grp->gr_ptr.gt_hostent);
2059 } else if (grp->gr_type == GT_NET) {
2060 if (grp->gr_ptr.gt_net.nt_name)
2061 free(grp->gr_ptr.gt_net.nt_name);
2068 SYSLOG(int pri, const char *fmt, ...)
2073 vfprintf(stderr, fmt, ap);
2079 * Check options for consistency.
2086 if (dp == (struct dirlist *)NULL)
2088 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
2089 (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
2090 (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
2091 syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
2094 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2095 syslog(LOG_ERR, "-mask requires -network");
2098 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2099 syslog(LOG_ERR, "-alldirs has multiple directories");
2106 * Check an absolute directory path for any symbolic links. Return true
2107 * if no symbolic links are found.
2118 while (*cp && ret) {
2121 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2127 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2133 * Just translate an ascii string to an integer.
2139 register int res = 0;
2142 if (*cp < '0' || *cp > '9')
2144 res = res * 10 + (*cp++ - '0');