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

1.10    ! deraadt     1: /*     $OpenBSD: ypcat.c,v 1.9 2003/06/02 04:00:16 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:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     17:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     18:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     19:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     20:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     21:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     22:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     23:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     24:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     25:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     26:  * SUCH DAMAGE.
                     27:  */
                     28:
                     29: #ifndef LINT
1.10    ! deraadt    30: static char rcsid[] = "$OpenBSD: ypcat.c,v 1.9 2003/06/02 04:00:16 deraadt Exp $";
1.1       deraadt    31: #endif
                     32:
                     33: #include <sys/param.h>
                     34: #include <sys/types.h>
                     35: #include <sys/socket.h>
1.6       deraadt    36: #include <unistd.h>
                     37: #include <string.h>
1.1       deraadt    38: #include <stdio.h>
                     39: #include <ctype.h>
                     40:
                     41: #include <rpc/rpc.h>
                     42: #include <rpc/xdr.h>
1.2       deraadt    43: #include <rpcsvc/yp.h>
1.1       deraadt    44: #include <rpcsvc/ypclnt.h>
1.10    ! deraadt    45:
        !            46: void   usage(void);
        !            47: int    printit(u_long, char *, int, char *, int, void *);
1.1       deraadt    48:
                     49: struct ypalias {
                     50:        char *alias, *name;
                     51: } ypaliases[] = {
                     52:        { "passwd", "passwd.byname" },
1.7       deraadt    53:        { "master.passwd", "master.passwd.byname" },
1.1       deraadt    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:
1.6       deraadt    65: void
1.8       deraadt    66: usage(void)
1.1       deraadt    67: {
                     68:        fprintf(stderr, "Usage:\n");
                     69:        fprintf(stderr, "\typcat [-k] [-d domainname] [-t] mapname\n");
                     70:        fprintf(stderr, "\typcat -x\n");
                     71:        exit(1);
                     72: }
                     73:
1.6       deraadt    74: int
1.8       deraadt    75: printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen,
                     76:     void *indata)
1.1       deraadt    77: {
1.5       deraadt    78:        if (instatus != YP_TRUE)
1.1       deraadt    79:                return instatus;
1.5       deraadt    80:        if (key)
1.1       deraadt    81:                printf("%*.*s ", inkeylen, inkeylen, inkey);
                     82:        printf("%*.*s\n", invallen, invallen, inval);
                     83:        return 0;
                     84: }
                     85:
                     86: int
1.8       deraadt    87: main(int argc, char *argv[])
1.1       deraadt    88: {
1.8       deraadt    89:        char *domain = NULL, *inmap;
1.1       deraadt    90:        struct ypall_callback ypcb;
                     91:        extern char *optarg;
                     92:        extern int optind;
1.8       deraadt    93:        int notrans, c, r, i;
1.1       deraadt    94:
                     95:        notrans = key = 0;
1.5       deraadt    96:        while ((c=getopt(argc, argv, "xd:kt")) != -1)
                     97:                switch (c) {
1.1       deraadt    98:                case 'x':
1.5       deraadt    99:                        for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.1       deraadt   100:                                printf("Use \"%s\" for \"%s\"\n",
1.5       deraadt   101:                                    ypaliases[i].alias, ypaliases[i].name);
1.1       deraadt   102:                        exit(0);
                    103:                case 'd':
1.2       deraadt   104:                        domain = optarg;
1.1       deraadt   105:                        break;
                    106:                case 't':
                    107:                        notrans++;
                    108:                        break;
                    109:                case 'k':
                    110:                        key++;
                    111:                        break;
                    112:                default:
                    113:                        usage();
                    114:                }
                    115:
1.5       deraadt   116:        if (optind + 1 != argc )
1.1       deraadt   117:                usage();
1.4       deraadt   118:
1.5       deraadt   119:        if (!domain)
                    120:                yp_get_default_domain(&domain);
1.1       deraadt   121:
                    122:        inmap = argv[optind];
1.3       deraadt   123:        if (!notrans) {
1.5       deraadt   124:                for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    125:                        if (strcmp(inmap, ypaliases[i].alias) == 0)
1.3       deraadt   126:                                inmap = ypaliases[i].name;
                    127:        }
1.1       deraadt   128:        ypcb.foreach = printit;
                    129:        ypcb.data = NULL;
                    130:
1.2       deraadt   131:        r = yp_all(domain, inmap, &ypcb);
1.5       deraadt   132:        switch (r) {
1.1       deraadt   133:        case 0:
                    134:                break;
                    135:        case YPERR_YPBIND:
                    136:                fprintf(stderr, "ypcat: not running ypbind\n");
                    137:                exit(1);
                    138:        default:
                    139:                fprintf(stderr, "No such map %s. Reason: %s\n",
1.5       deraadt   140:                    inmap, yperr_string(r));
1.1       deraadt   141:                exit(1);
                    142:        }
                    143:        exit(0);
                    144: }