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

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