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

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