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

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