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

1.7     ! deraadt     1: /*     $OpenBSD: ypcat.c,v 1.6 1997/07/21 19:21:14 deraadt Exp $ */
1.3       deraadt     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.7     ! deraadt    36: static char rcsid[] = "$OpenBSD: ypcat.c,v 1.6 1997/07/21 19:21:14 deraadt Exp $";
1.1       deraadt    37: #endif
                     38:
                     39: #include <sys/param.h>
                     40: #include <sys/types.h>
                     41: #include <sys/socket.h>
1.6       deraadt    42: #include <unistd.h>
                     43: #include <string.h>
1.1       deraadt    44: #include <stdio.h>
                     45: #include <ctype.h>
                     46:
                     47: #include <rpc/rpc.h>
                     48: #include <rpc/xdr.h>
1.2       deraadt    49: #include <rpcsvc/yp.h>
1.1       deraadt    50: #include <rpcsvc/ypclnt.h>
                     51:
                     52: struct ypalias {
                     53:        char *alias, *name;
                     54: } ypaliases[] = {
                     55:        { "passwd", "passwd.byname" },
1.7     ! deraadt    56:        { "master.passwd", "master.passwd.byname" },
1.1       deraadt    57:        { "group", "group.byname" },
                     58:        { "networks", "networks.byaddr" },
                     59:        { "hosts", "hosts.byaddr" },
                     60:        { "protocols", "protocols.bynumber" },
                     61:        { "services", "services.byname" },
                     62:        { "aliases", "mail.aliases" },
                     63:        { "ethers", "ethers.byname" },
                     64: };
                     65:
                     66: int key;
                     67:
1.6       deraadt    68: void
1.1       deraadt    69: usage()
                     70: {
                     71:        fprintf(stderr, "Usage:\n");
                     72:        fprintf(stderr, "\typcat [-k] [-d domainname] [-t] mapname\n");
                     73:        fprintf(stderr, "\typcat -x\n");
                     74:        exit(1);
                     75: }
                     76:
1.6       deraadt    77: int
1.1       deraadt    78: printit(instatus, inkey, inkeylen, inval, invallen, indata)
                     79: int instatus;
                     80: char *inkey;
                     81: int inkeylen;
                     82: char *inval;
                     83: int invallen;
                     84: char *indata;
                     85: {
1.5       deraadt    86:        if (instatus != YP_TRUE)
1.1       deraadt    87:                return instatus;
1.5       deraadt    88:        if (key)
1.1       deraadt    89:                printf("%*.*s ", inkeylen, inkeylen, inkey);
                     90:        printf("%*.*s\n", invallen, invallen, inval);
                     91:        return 0;
                     92: }
                     93:
                     94: int
                     95: main(argc, argv)
                     96: char **argv;
                     97: {
1.4       deraadt    98:        char *domain = NULL;
1.1       deraadt    99:        struct ypall_callback ypcb;
                    100:        char *inmap;
                    101:        extern char *optarg;
                    102:        extern int optind;
                    103:        int notrans;
                    104:        int c, r, i;
                    105:
                    106:        notrans = key = 0;
1.5       deraadt   107:        while ((c=getopt(argc, argv, "xd:kt")) != -1)
                    108:                switch (c) {
1.1       deraadt   109:                case 'x':
1.5       deraadt   110:                        for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.1       deraadt   111:                                printf("Use \"%s\" for \"%s\"\n",
1.5       deraadt   112:                                    ypaliases[i].alias, ypaliases[i].name);
1.1       deraadt   113:                        exit(0);
                    114:                case 'd':
1.2       deraadt   115:                        domain = optarg;
1.1       deraadt   116:                        break;
                    117:                case 't':
                    118:                        notrans++;
                    119:                        break;
                    120:                case 'k':
                    121:                        key++;
                    122:                        break;
                    123:                default:
                    124:                        usage();
                    125:                }
                    126:
1.5       deraadt   127:        if (optind + 1 != argc )
1.1       deraadt   128:                usage();
1.4       deraadt   129:
1.5       deraadt   130:        if (!domain)
                    131:                yp_get_default_domain(&domain);
1.1       deraadt   132:
                    133:        inmap = argv[optind];
1.3       deraadt   134:        if (!notrans) {
1.5       deraadt   135:                for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    136:                        if (strcmp(inmap, ypaliases[i].alias) == 0)
1.3       deraadt   137:                                inmap = ypaliases[i].name;
                    138:        }
1.1       deraadt   139:        ypcb.foreach = printit;
                    140:        ypcb.data = NULL;
                    141:
1.2       deraadt   142:        r = yp_all(domain, inmap, &ypcb);
1.5       deraadt   143:        switch (r) {
1.1       deraadt   144:        case 0:
                    145:                break;
                    146:        case YPERR_YPBIND:
                    147:                fprintf(stderr, "ypcat: not running ypbind\n");
                    148:                exit(1);
                    149:        default:
                    150:                fprintf(stderr, "No such map %s. Reason: %s\n",
1.5       deraadt   151:                    inmap, yperr_string(r));
1.1       deraadt   152:                exit(1);
                    153:        }
                    154:        exit(0);
                    155: }