[BACK]Return to showmount.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / showmount

Annotation of src/usr.bin/showmount/showmount.c, Revision 1.14

1.14    ! deraadt     1: /*     $OpenBSD: showmount.c,v 1.13 2003/06/10 22:20:51 deraadt Exp $  */
1.3       deraadt     2: /*     $NetBSD: showmount.c,v 1.7 1996/05/01 18:14:10 cgd Exp $        */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993, 1995
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Rick Macklem at The University of Guelph.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.12      millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1989, 1993, 1995\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
1.8       heko       40: #endif /* not lint */
1.1       deraadt    41:
                     42: #ifndef lint
                     43: #if 0
                     44: static char sccsid[] = "@(#)showmount.c        8.3 (Berkeley) 3/29/95";
                     45: #endif
1.14    ! deraadt    46: static char rcsid[] = "$OpenBSD: showmount.c,v 1.13 2003/06/10 22:20:51 deraadt Exp $";
1.8       heko       47: #endif /* not lint */
1.1       deraadt    48:
                     49: #include <sys/types.h>
                     50: #include <sys/file.h>
                     51: #include <sys/socket.h>
                     52: #include <sys/socketvar.h>
                     53:
                     54: #include <netdb.h>
                     55: #include <rpc/rpc.h>
                     56: #include <rpc/pmap_clnt.h>
                     57: #include <rpc/pmap_prot.h>
                     58: #include <nfs/rpcv2.h>
                     59:
                     60: #include <stdio.h>
                     61: #include <stdlib.h>
                     62: #include <string.h>
                     63: #include <unistd.h>
1.5       deraadt    64: #include <vis.h>
1.1       deraadt    65:
                     66: /* Constant defs */
                     67: #define        ALL     1
                     68: #define        DIRS    2
                     69:
                     70: #define        DODUMP          0x1
                     71: #define        DOEXPORTS       0x2
                     72:
                     73: struct mountlist {
                     74:        struct mountlist *ml_left;
                     75:        struct mountlist *ml_right;
                     76:        char    ml_host[RPCMNT_NAMELEN+1];
                     77:        char    ml_dirp[RPCMNT_PATHLEN+1];
                     78: };
                     79:
                     80: struct grouplist {
                     81:        struct grouplist *gr_next;
                     82:        char    gr_name[RPCMNT_NAMELEN+1];
                     83: };
                     84:
                     85: struct exportslist {
                     86:        struct exportslist *ex_next;
                     87:        struct grouplist *ex_groups;
                     88:        char    ex_dirp[RPCMNT_PATHLEN+1];
                     89: };
                     90:
                     91: static struct mountlist *mntdump;
                     92: static struct exportslist *exports;
                     93: static int type = 0;
                     94:
1.10      millert    95: void   print_dump(struct mountlist *);
                     96: void   usage(void);
                     97: int    xdr_mntdump(XDR *, struct mountlist **);
                     98: int    xdr_exports(XDR *, struct exportslist **);
1.1       deraadt    99:
                    100: /*
                    101:  * This command queries the NFS mount daemon for it's mount list and/or
                    102:  * it's exports list and prints them out.
                    103:  * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
                    104:  * and the "Network File System Protocol XXX.."
                    105:  * for detailed information on the protocol.
                    106:  */
                    107: int
1.13      deraadt   108: main(int argc, char *argv[])
1.1       deraadt   109: {
                    110:        struct exportslist *exp;
                    111:        struct grouplist *grp;
1.6       deraadt   112:        struct sockaddr_in clnt_sin;
                    113:        struct hostent *hp;
                    114:        struct timeval timeout;
                    115:        int rpcs = 0, mntvers = 1;
                    116:        enum clnt_stat estat;
                    117:        CLIENT *client;
1.2       deraadt   118:        char *host;
1.6       deraadt   119:        int ch, clnt_sock;
1.1       deraadt   120:
1.2       deraadt   121:        while ((ch = getopt(argc, argv, "ade3")) != -1)
1.14    ! deraadt   122:                switch (ch) {
1.1       deraadt   123:                case 'a':
                    124:                        if (type == 0) {
                    125:                                type = ALL;
                    126:                                rpcs |= DODUMP;
                    127:                        } else
                    128:                                usage();
                    129:                        break;
                    130:                case 'd':
                    131:                        if (type == 0) {
                    132:                                type = DIRS;
                    133:                                rpcs |= DODUMP;
                    134:                        } else
                    135:                                usage();
                    136:                        break;
                    137:                case 'e':
                    138:                        rpcs |= DOEXPORTS;
                    139:                        break;
                    140:                case '3':
                    141:                        mntvers = 3;
                    142:                        break;
                    143:                default:
                    144:                        usage();
                    145:                }
                    146:        argc -= optind;
                    147:        argv += optind;
                    148:
                    149:        if (argc > 0)
                    150:                host = *argv;
                    151:        else
                    152:                host = "localhost";
                    153:
                    154:        if (rpcs == 0)
                    155:                rpcs = DODUMP;
                    156:
1.6       deraadt   157:        if ((hp = gethostbyname(host)) == NULL) {
                    158:                fprintf(stderr, "showmount: unknown host %s\n", host);
                    159:                exit(1);
                    160:        }
                    161:        bzero(&clnt_sin, sizeof clnt_sin);
                    162:        clnt_sin.sin_len = sizeof clnt_sin;
                    163:        clnt_sin.sin_family = AF_INET;
                    164:        bcopy(hp->h_addr, (char *)&clnt_sin.sin_addr, hp->h_length);
                    165:        clnt_sock = RPC_ANYSOCK;
                    166:        client = clnttcp_create(&clnt_sin, RPCPROG_MNT, mntvers,
                    167:            &clnt_sock, 0, 0);
                    168:        if (client == NULL) {
                    169:                clnt_pcreateerror("showmount: clnttcp_create");
                    170:                exit(1);
                    171:        }
                    172:        timeout.tv_sec = 30;
                    173:        timeout.tv_usec = 0;
                    174:
                    175:        if (rpcs & DODUMP) {
                    176:                estat = clnt_call(client, RPCMNT_DUMP, xdr_void, (char *)0,
                    177:                    xdr_mntdump, (char *)&mntdump, timeout);
                    178:                if (estat != RPC_SUCCESS) {
1.3       deraadt   179:                        fprintf(stderr, "showmount: Can't do Mountdump rpc: ");
1.1       deraadt   180:                        clnt_perrno(estat);
                    181:                        exit(1);
                    182:                }
1.6       deraadt   183:        }
                    184:        if (rpcs & DOEXPORTS) {
                    185:                estat = clnt_call(client, RPCMNT_EXPORT, xdr_void, (char *)0,
                    186:                    xdr_exports, (char *)&exports, timeout);
                    187:                if (estat != RPC_SUCCESS) {
1.3       deraadt   188:                        fprintf(stderr, "showmount: Can't do Exports rpc: ");
1.1       deraadt   189:                        clnt_perrno(estat);
                    190:                        exit(1);
                    191:                }
1.6       deraadt   192:        }
1.1       deraadt   193:
                    194:        /* Now just print out the results */
                    195:        if (rpcs & DODUMP) {
                    196:                switch (type) {
                    197:                case ALL:
                    198:                        printf("All mount points on %s:\n", host);
                    199:                        break;
                    200:                case DIRS:
                    201:                        printf("Directories on %s:\n", host);
                    202:                        break;
                    203:                default:
                    204:                        printf("Hosts on %s:\n", host);
                    205:                        break;
1.9       deraadt   206:                }
1.1       deraadt   207:                print_dump(mntdump);
                    208:        }
                    209:        if (rpcs & DOEXPORTS) {
1.5       deraadt   210:                char    vp[(RPCMNT_PATHLEN+1)*4];
                    211:                char    vn[(RPCMNT_NAMELEN+1)*4];
                    212:
1.1       deraadt   213:                printf("Exports list on %s:\n", host);
                    214:                exp = exports;
                    215:                while (exp) {
1.11      vincent   216:                        strnvis(vp, exp->ex_dirp, sizeof vp, VIS_CSTYLE);
1.5       deraadt   217:                        printf("%-35s", vp);
1.1       deraadt   218:                        grp = exp->ex_groups;
                    219:                        if (grp == NULL) {
                    220:                                printf("Everyone\n");
                    221:                        } else {
                    222:                                while (grp) {
1.11      vincent   223:                                        strnvis(vn, grp->gr_name, sizeof vn,
                    224:                                            VIS_CSTYLE);
1.5       deraadt   225:                                        printf("%s ", vn);
1.1       deraadt   226:                                        grp = grp->gr_next;
                    227:                                }
                    228:                                printf("\n");
                    229:                        }
                    230:                        exp = exp->ex_next;
                    231:                }
                    232:        }
                    233:
                    234:        exit(0);
                    235: }
                    236:
                    237: /*
                    238:  * Xdr routine for retrieving the mount dump list
                    239:  */
                    240: int
1.13      deraadt   241: xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
1.1       deraadt   242: {
1.7       deraadt   243:        struct mountlist *mp, **otp = NULL, *tp;
1.1       deraadt   244:        int bool, val, val2;
                    245:        char *strp;
                    246:
                    247:        *mlp = (struct mountlist *)0;
                    248:        if (!xdr_bool(xdrsp, &bool))
                    249:                return (0);
                    250:        while (bool) {
                    251:                mp = (struct mountlist *)malloc(sizeof(struct mountlist));
                    252:                if (mp == NULL)
                    253:                        return (0);
                    254:                mp->ml_left = mp->ml_right = (struct mountlist *)0;
                    255:                strp = mp->ml_host;
                    256:                if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
                    257:                        return (0);
                    258:                strp = mp->ml_dirp;
                    259:                if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
                    260:                        return (0);
                    261:
                    262:                /*
                    263:                 * Build a binary tree on sorted order of either host or dirp.
                    264:                 * Drop any duplications.
                    265:                 */
                    266:                if (*mlp == NULL) {
                    267:                        *mlp = mp;
                    268:                } else {
                    269:                        tp = *mlp;
                    270:                        while (tp) {
                    271:                                val = strcmp(mp->ml_host, tp->ml_host);
                    272:                                val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
                    273:                                switch (type) {
                    274:                                case ALL:
                    275:                                        if (val == 0) {
                    276:                                                if (val2 == 0) {
                    277:                                                        free((caddr_t)mp);
                    278:                                                        goto next;
                    279:                                                }
                    280:                                                val = val2;
                    281:                                        }
                    282:                                        break;
                    283:                                case DIRS:
                    284:                                        if (val2 == 0) {
                    285:                                                free((caddr_t)mp);
                    286:                                                goto next;
                    287:                                        }
                    288:                                        val = val2;
                    289:                                        break;
                    290:                                default:
                    291:                                        if (val == 0) {
                    292:                                                free((caddr_t)mp);
                    293:                                                goto next;
                    294:                                        }
                    295:                                        break;
1.7       deraadt   296:                                }
1.1       deraadt   297:                                if (val < 0) {
                    298:                                        otp = &tp->ml_left;
                    299:                                        tp = tp->ml_left;
                    300:                                } else {
                    301:                                        otp = &tp->ml_right;
                    302:                                        tp = tp->ml_right;
                    303:                                }
                    304:                        }
                    305:                        *otp = mp;
                    306:                }
                    307: next:
                    308:                if (!xdr_bool(xdrsp, &bool))
                    309:                        return (0);
                    310:        }
                    311:        return (1);
                    312: }
                    313:
                    314: /*
                    315:  * Xdr routine to retrieve exports list
                    316:  */
                    317: int
1.13      deraadt   318: xdr_exports(XDR *xdrsp, struct exportslist **exp)
1.1       deraadt   319: {
                    320:        struct exportslist *ep;
                    321:        struct grouplist *gp;
                    322:        int bool, grpbool;
                    323:        char *strp;
                    324:
                    325:        *exp = (struct exportslist *)0;
                    326:        if (!xdr_bool(xdrsp, &bool))
                    327:                return (0);
                    328:        while (bool) {
                    329:                ep = (struct exportslist *)malloc(sizeof(struct exportslist));
                    330:                if (ep == NULL)
                    331:                        return (0);
                    332:                ep->ex_groups = (struct grouplist *)0;
                    333:                strp = ep->ex_dirp;
                    334:                if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
                    335:                        return (0);
                    336:                if (!xdr_bool(xdrsp, &grpbool))
                    337:                        return (0);
                    338:                while (grpbool) {
1.6       deraadt   339:                        gp = (struct grouplist *)malloc(
                    340:                            sizeof(struct grouplist));
1.1       deraadt   341:                        if (gp == NULL)
                    342:                                return (0);
                    343:                        strp = gp->gr_name;
                    344:                        if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
                    345:                                return (0);
                    346:                        gp->gr_next = ep->ex_groups;
                    347:                        ep->ex_groups = gp;
                    348:                        if (!xdr_bool(xdrsp, &grpbool))
                    349:                                return (0);
                    350:                }
                    351:                ep->ex_next = *exp;
                    352:                *exp = ep;
                    353:                if (!xdr_bool(xdrsp, &bool))
                    354:                        return (0);
                    355:        }
                    356:        return (1);
                    357: }
                    358:
                    359: void
1.13      deraadt   360: usage(void)
1.1       deraadt   361: {
                    362:
1.3       deraadt   363:        fprintf(stderr, "usage: showmount [-ade3] host\n");
1.1       deraadt   364:        exit(1);
                    365: }
                    366:
                    367: /*
                    368:  * Print the binary tree in inorder so that output is sorted.
                    369:  */
                    370: void
1.13      deraadt   371: print_dump(struct mountlist *mp)
1.1       deraadt   372: {
1.5       deraadt   373:        char    vn[(RPCMNT_NAMELEN+1)*4];
                    374:        char    vp[(RPCMNT_PATHLEN+1)*4];
1.1       deraadt   375:
                    376:        if (mp == NULL)
                    377:                return;
                    378:        if (mp->ml_left)
                    379:                print_dump(mp->ml_left);
                    380:        switch (type) {
                    381:        case ALL:
1.5       deraadt   382:                strvis(vn, mp->ml_host, VIS_CSTYLE);
                    383:                strvis(vp, mp->ml_dirp, VIS_CSTYLE);
                    384:                printf("%s:%s\n", vn, vp);
1.1       deraadt   385:                break;
                    386:        case DIRS:
1.5       deraadt   387:                strvis(vp, mp->ml_dirp, VIS_CSTYLE);
                    388:                printf("%s\n", vp);
1.1       deraadt   389:                break;
                    390:        default:
1.5       deraadt   391:                strvis(vn, mp->ml_host, VIS_CSTYLE);
                    392:                printf("%s\n", vn);
1.1       deraadt   393:                break;
1.9       deraadt   394:        }
1.1       deraadt   395:        if (mp->ml_right)
                    396:                print_dump(mp->ml_right);
                    397: }