[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.12

1.12    ! millert     1: /*     $OpenBSD: showmount.c,v 1.11 2003/04/30 21:27:23 vincent 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.12    ! millert    46: static char rcsid[] = "$OpenBSD: showmount.c,v 1.11 2003/04/30 21:27:23 vincent 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
                    108: main(argc, argv)
                    109:        int argc;
                    110:        char **argv;
                    111: {
                    112:        struct exportslist *exp;
                    113:        struct grouplist *grp;
1.6       deraadt   114:        struct sockaddr_in clnt_sin;
                    115:        struct hostent *hp;
                    116:        struct timeval timeout;
                    117:        int rpcs = 0, mntvers = 1;
                    118:        enum clnt_stat estat;
                    119:        CLIENT *client;
1.2       deraadt   120:        char *host;
1.6       deraadt   121:        int ch, clnt_sock;
1.1       deraadt   122:
1.2       deraadt   123:        while ((ch = getopt(argc, argv, "ade3")) != -1)
1.1       deraadt   124:                switch((char)ch) {
                    125:                case 'a':
                    126:                        if (type == 0) {
                    127:                                type = ALL;
                    128:                                rpcs |= DODUMP;
                    129:                        } else
                    130:                                usage();
                    131:                        break;
                    132:                case 'd':
                    133:                        if (type == 0) {
                    134:                                type = DIRS;
                    135:                                rpcs |= DODUMP;
                    136:                        } else
                    137:                                usage();
                    138:                        break;
                    139:                case 'e':
                    140:                        rpcs |= DOEXPORTS;
                    141:                        break;
                    142:                case '3':
                    143:                        mntvers = 3;
                    144:                        break;
                    145:                case '?':
                    146:                default:
                    147:                        usage();
                    148:                }
                    149:        argc -= optind;
                    150:        argv += optind;
                    151:
                    152:        if (argc > 0)
                    153:                host = *argv;
                    154:        else
                    155:                host = "localhost";
                    156:
                    157:        if (rpcs == 0)
                    158:                rpcs = DODUMP;
                    159:
1.6       deraadt   160:        if ((hp = gethostbyname(host)) == NULL) {
                    161:                fprintf(stderr, "showmount: unknown host %s\n", host);
                    162:                exit(1);
                    163:        }
                    164:        bzero(&clnt_sin, sizeof clnt_sin);
                    165:        clnt_sin.sin_len = sizeof clnt_sin;
                    166:        clnt_sin.sin_family = AF_INET;
                    167:        bcopy(hp->h_addr, (char *)&clnt_sin.sin_addr, hp->h_length);
                    168:        clnt_sock = RPC_ANYSOCK;
                    169:        client = clnttcp_create(&clnt_sin, RPCPROG_MNT, mntvers,
                    170:            &clnt_sock, 0, 0);
                    171:        if (client == NULL) {
                    172:                clnt_pcreateerror("showmount: clnttcp_create");
                    173:                exit(1);
                    174:        }
                    175:        timeout.tv_sec = 30;
                    176:        timeout.tv_usec = 0;
                    177:
                    178:        if (rpcs & DODUMP) {
                    179:                estat = clnt_call(client, RPCMNT_DUMP, xdr_void, (char *)0,
                    180:                    xdr_mntdump, (char *)&mntdump, timeout);
                    181:                if (estat != RPC_SUCCESS) {
1.3       deraadt   182:                        fprintf(stderr, "showmount: Can't do Mountdump rpc: ");
1.1       deraadt   183:                        clnt_perrno(estat);
                    184:                        exit(1);
                    185:                }
1.6       deraadt   186:        }
                    187:        if (rpcs & DOEXPORTS) {
                    188:                estat = clnt_call(client, RPCMNT_EXPORT, xdr_void, (char *)0,
                    189:                    xdr_exports, (char *)&exports, timeout);
                    190:                if (estat != RPC_SUCCESS) {
1.3       deraadt   191:                        fprintf(stderr, "showmount: Can't do Exports rpc: ");
1.1       deraadt   192:                        clnt_perrno(estat);
                    193:                        exit(1);
                    194:                }
1.6       deraadt   195:        }
1.1       deraadt   196:
                    197:        /* Now just print out the results */
                    198:        if (rpcs & DODUMP) {
                    199:                switch (type) {
                    200:                case ALL:
                    201:                        printf("All mount points on %s:\n", host);
                    202:                        break;
                    203:                case DIRS:
                    204:                        printf("Directories on %s:\n", host);
                    205:                        break;
                    206:                default:
                    207:                        printf("Hosts on %s:\n", host);
                    208:                        break;
1.9       deraadt   209:                }
1.1       deraadt   210:                print_dump(mntdump);
                    211:        }
                    212:        if (rpcs & DOEXPORTS) {
1.5       deraadt   213:                char    vp[(RPCMNT_PATHLEN+1)*4];
                    214:                char    vn[(RPCMNT_NAMELEN+1)*4];
                    215:
1.1       deraadt   216:                printf("Exports list on %s:\n", host);
                    217:                exp = exports;
                    218:                while (exp) {
1.11      vincent   219:                        strnvis(vp, exp->ex_dirp, sizeof vp, VIS_CSTYLE);
1.5       deraadt   220:                        printf("%-35s", vp);
1.1       deraadt   221:                        grp = exp->ex_groups;
                    222:                        if (grp == NULL) {
                    223:                                printf("Everyone\n");
                    224:                        } else {
                    225:                                while (grp) {
1.11      vincent   226:                                        strnvis(vn, grp->gr_name, sizeof vn,
                    227:                                            VIS_CSTYLE);
1.5       deraadt   228:                                        printf("%s ", vn);
1.1       deraadt   229:                                        grp = grp->gr_next;
                    230:                                }
                    231:                                printf("\n");
                    232:                        }
                    233:                        exp = exp->ex_next;
                    234:                }
                    235:        }
                    236:
                    237:        exit(0);
                    238: }
                    239:
                    240: /*
                    241:  * Xdr routine for retrieving the mount dump list
                    242:  */
                    243: int
                    244: xdr_mntdump(xdrsp, mlp)
                    245:        XDR *xdrsp;
                    246:        struct mountlist **mlp;
                    247: {
1.7       deraadt   248:        struct mountlist *mp, **otp = NULL, *tp;
1.1       deraadt   249:        int bool, val, val2;
                    250:        char *strp;
                    251:
                    252:        *mlp = (struct mountlist *)0;
                    253:        if (!xdr_bool(xdrsp, &bool))
                    254:                return (0);
                    255:        while (bool) {
                    256:                mp = (struct mountlist *)malloc(sizeof(struct mountlist));
                    257:                if (mp == NULL)
                    258:                        return (0);
                    259:                mp->ml_left = mp->ml_right = (struct mountlist *)0;
                    260:                strp = mp->ml_host;
                    261:                if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
                    262:                        return (0);
                    263:                strp = mp->ml_dirp;
                    264:                if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
                    265:                        return (0);
                    266:
                    267:                /*
                    268:                 * Build a binary tree on sorted order of either host or dirp.
                    269:                 * Drop any duplications.
                    270:                 */
                    271:                if (*mlp == NULL) {
                    272:                        *mlp = mp;
                    273:                } else {
                    274:                        tp = *mlp;
                    275:                        while (tp) {
                    276:                                val = strcmp(mp->ml_host, tp->ml_host);
                    277:                                val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
                    278:                                switch (type) {
                    279:                                case ALL:
                    280:                                        if (val == 0) {
                    281:                                                if (val2 == 0) {
                    282:                                                        free((caddr_t)mp);
                    283:                                                        goto next;
                    284:                                                }
                    285:                                                val = val2;
                    286:                                        }
                    287:                                        break;
                    288:                                case DIRS:
                    289:                                        if (val2 == 0) {
                    290:                                                free((caddr_t)mp);
                    291:                                                goto next;
                    292:                                        }
                    293:                                        val = val2;
                    294:                                        break;
                    295:                                default:
                    296:                                        if (val == 0) {
                    297:                                                free((caddr_t)mp);
                    298:                                                goto next;
                    299:                                        }
                    300:                                        break;
1.7       deraadt   301:                                }
1.1       deraadt   302:                                if (val < 0) {
                    303:                                        otp = &tp->ml_left;
                    304:                                        tp = tp->ml_left;
                    305:                                } else {
                    306:                                        otp = &tp->ml_right;
                    307:                                        tp = tp->ml_right;
                    308:                                }
                    309:                        }
                    310:                        *otp = mp;
                    311:                }
                    312: next:
                    313:                if (!xdr_bool(xdrsp, &bool))
                    314:                        return (0);
                    315:        }
                    316:        return (1);
                    317: }
                    318:
                    319: /*
                    320:  * Xdr routine to retrieve exports list
                    321:  */
                    322: int
                    323: xdr_exports(xdrsp, exp)
                    324:        XDR *xdrsp;
                    325:        struct exportslist **exp;
                    326: {
                    327:        struct exportslist *ep;
                    328:        struct grouplist *gp;
                    329:        int bool, grpbool;
                    330:        char *strp;
                    331:
                    332:        *exp = (struct exportslist *)0;
                    333:        if (!xdr_bool(xdrsp, &bool))
                    334:                return (0);
                    335:        while (bool) {
                    336:                ep = (struct exportslist *)malloc(sizeof(struct exportslist));
                    337:                if (ep == NULL)
                    338:                        return (0);
                    339:                ep->ex_groups = (struct grouplist *)0;
                    340:                strp = ep->ex_dirp;
                    341:                if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
                    342:                        return (0);
                    343:                if (!xdr_bool(xdrsp, &grpbool))
                    344:                        return (0);
                    345:                while (grpbool) {
1.6       deraadt   346:                        gp = (struct grouplist *)malloc(
                    347:                            sizeof(struct grouplist));
1.1       deraadt   348:                        if (gp == NULL)
                    349:                                return (0);
                    350:                        strp = gp->gr_name;
                    351:                        if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
                    352:                                return (0);
                    353:                        gp->gr_next = ep->ex_groups;
                    354:                        ep->ex_groups = gp;
                    355:                        if (!xdr_bool(xdrsp, &grpbool))
                    356:                                return (0);
                    357:                }
                    358:                ep->ex_next = *exp;
                    359:                *exp = ep;
                    360:                if (!xdr_bool(xdrsp, &bool))
                    361:                        return (0);
                    362:        }
                    363:        return (1);
                    364: }
                    365:
                    366: void
                    367: usage()
                    368: {
                    369:
1.3       deraadt   370:        fprintf(stderr, "usage: showmount [-ade3] host\n");
1.1       deraadt   371:        exit(1);
                    372: }
                    373:
                    374: /*
                    375:  * Print the binary tree in inorder so that output is sorted.
                    376:  */
                    377: void
                    378: print_dump(mp)
                    379:        struct mountlist *mp;
                    380: {
1.5       deraadt   381:        char    vn[(RPCMNT_NAMELEN+1)*4];
                    382:        char    vp[(RPCMNT_PATHLEN+1)*4];
1.1       deraadt   383:
                    384:        if (mp == NULL)
                    385:                return;
                    386:        if (mp->ml_left)
                    387:                print_dump(mp->ml_left);
                    388:        switch (type) {
                    389:        case ALL:
1.5       deraadt   390:                strvis(vn, mp->ml_host, VIS_CSTYLE);
                    391:                strvis(vp, mp->ml_dirp, VIS_CSTYLE);
                    392:                printf("%s:%s\n", vn, vp);
1.1       deraadt   393:                break;
                    394:        case DIRS:
1.5       deraadt   395:                strvis(vp, mp->ml_dirp, VIS_CSTYLE);
                    396:                printf("%s\n", vp);
1.1       deraadt   397:                break;
                    398:        default:
1.5       deraadt   399:                strvis(vn, mp->ml_host, VIS_CSTYLE);
                    400:                printf("%s\n", vn);
1.1       deraadt   401:                break;
1.9       deraadt   402:        }
1.1       deraadt   403:        if (mp->ml_right)
                    404:                print_dump(mp->ml_right);
                    405: }