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

Annotation of src/usr.bin/ypcat/ypcat.c, Revision 1.4

1.3       deraadt     1: /*     $OpenBSD$ */
                      2:
1.1       deraadt     3: /*
1.3       deraadt     4:  * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
1.1       deraadt     5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by Theo de Raadt.
                     18:  * 4. The name of the author may not be used to endorse or promote
                     19:  *    products derived from this software without specific prior written
                     20:  *    permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     23:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     24:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     26:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef LINT
1.3       deraadt    36: static char rcsid[] = "$OpenBSD$";
1.1       deraadt    37: #endif
                     38:
                     39: #include <sys/param.h>
                     40: #include <sys/types.h>
                     41: #include <sys/socket.h>
                     42: #include <stdio.h>
                     43: #include <ctype.h>
                     44:
                     45: #include <rpc/rpc.h>
                     46: #include <rpc/xdr.h>
1.2       deraadt    47: #include <rpcsvc/yp.h>
1.1       deraadt    48: #include <rpcsvc/ypclnt.h>
                     49:
                     50: struct ypalias {
                     51:        char *alias, *name;
                     52: } ypaliases[] = {
                     53:        { "passwd", "passwd.byname" },
                     54:        { "group", "group.byname" },
                     55:        { "networks", "networks.byaddr" },
                     56:        { "hosts", "hosts.byaddr" },
                     57:        { "protocols", "protocols.bynumber" },
                     58:        { "services", "services.byname" },
                     59:        { "aliases", "mail.aliases" },
                     60:        { "ethers", "ethers.byname" },
                     61: };
                     62:
                     63: int key;
                     64:
                     65: usage()
                     66: {
                     67:        fprintf(stderr, "Usage:\n");
                     68:        fprintf(stderr, "\typcat [-k] [-d domainname] [-t] mapname\n");
                     69:        fprintf(stderr, "\typcat -x\n");
                     70:        exit(1);
                     71: }
                     72:
                     73: printit(instatus, inkey, inkeylen, inval, invallen, indata)
                     74: int instatus;
                     75: char *inkey;
                     76: int inkeylen;
                     77: char *inval;
                     78: int invallen;
                     79: char *indata;
                     80: {
                     81:        if(instatus != YP_TRUE)
                     82:                return instatus;
                     83:        if(key)
                     84:                printf("%*.*s ", inkeylen, inkeylen, inkey);
                     85:        printf("%*.*s\n", invallen, invallen, inval);
                     86:        return 0;
                     87: }
                     88:
                     89: int
                     90: main(argc, argv)
                     91: char **argv;
                     92: {
1.4     ! deraadt    93:        char *domain = NULL;
1.1       deraadt    94:        struct ypall_callback ypcb;
                     95:        char *inmap;
                     96:        extern char *optarg;
                     97:        extern int optind;
                     98:        int notrans;
                     99:        int c, r, i;
                    100:
                    101:        notrans = key = 0;
                    102:        while( (c=getopt(argc, argv, "xd:kt")) != -1)
                    103:                switch(c) {
                    104:                case 'x':
                    105:                        for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    106:                                printf("Use \"%s\" for \"%s\"\n",
                    107:                                        ypaliases[i].alias,
                    108:                                        ypaliases[i].name);
                    109:                        exit(0);
                    110:                case 'd':
1.2       deraadt   111:                        domain = optarg;
1.1       deraadt   112:                        break;
                    113:                case 't':
                    114:                        notrans++;
                    115:                        break;
                    116:                case 'k':
                    117:                        key++;
                    118:                        break;
                    119:                default:
                    120:                        usage();
                    121:                }
                    122:
                    123:        if(optind + 1 != argc )
                    124:                usage();
1.4     ! deraadt   125:
        !           126:        if (!domainname) {
        !           127:                yp_get_default_domain(&domainname);
        !           128:        }
1.1       deraadt   129:
                    130:        inmap = argv[optind];
1.3       deraadt   131:        if (!notrans) {
                    132:                for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    133:                        if( strcmp(inmap, ypaliases[i].alias) == 0)
                    134:                                inmap = ypaliases[i].name;
                    135:        }
1.1       deraadt   136:        ypcb.foreach = printit;
                    137:        ypcb.data = NULL;
                    138:
1.2       deraadt   139:        r = yp_all(domain, inmap, &ypcb);
1.1       deraadt   140:        switch(r) {
                    141:        case 0:
                    142:                break;
                    143:        case YPERR_YPBIND:
                    144:                fprintf(stderr, "ypcat: not running ypbind\n");
                    145:                exit(1);
                    146:        default:
                    147:                fprintf(stderr, "No such map %s. Reason: %s\n",
                    148:                        inmap, yperr_string(r));
                    149:                exit(1);
                    150:        }
                    151:        exit(0);
                    152: }