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

1.8     ! deraadt     1: /*     $OpenBSD: ypcat.c,v 1.7 2002/01/02 20:07: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:  * 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.8     ! deraadt    36: static char rcsid[] = "$OpenBSD: ypcat.c,v 1.7 2002/01/02 20:07:09 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.8     ! deraadt    69: usage(void)
1.1       deraadt    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.8     ! deraadt    78: printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen,
        !            79:     void *indata)
1.1       deraadt    80: {
1.5       deraadt    81:        if (instatus != YP_TRUE)
1.1       deraadt    82:                return instatus;
1.5       deraadt    83:        if (key)
1.1       deraadt    84:                printf("%*.*s ", inkeylen, inkeylen, inkey);
                     85:        printf("%*.*s\n", invallen, invallen, inval);
                     86:        return 0;
                     87: }
                     88:
                     89: int
1.8     ! deraadt    90: main(int argc, char *argv[])
1.1       deraadt    91: {
1.8     ! deraadt    92:        char *domain = NULL, *inmap;
1.1       deraadt    93:        struct ypall_callback ypcb;
                     94:        extern char *optarg;
                     95:        extern int optind;
1.8     ! deraadt    96:        int notrans, c, r, i;
1.1       deraadt    97:
                     98:        notrans = key = 0;
1.5       deraadt    99:        while ((c=getopt(argc, argv, "xd:kt")) != -1)
                    100:                switch (c) {
1.1       deraadt   101:                case 'x':
1.5       deraadt   102:                        for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.1       deraadt   103:                                printf("Use \"%s\" for \"%s\"\n",
1.5       deraadt   104:                                    ypaliases[i].alias, ypaliases[i].name);
1.1       deraadt   105:                        exit(0);
                    106:                case 'd':
1.2       deraadt   107:                        domain = optarg;
1.1       deraadt   108:                        break;
                    109:                case 't':
                    110:                        notrans++;
                    111:                        break;
                    112:                case 'k':
                    113:                        key++;
                    114:                        break;
                    115:                default:
                    116:                        usage();
                    117:                }
                    118:
1.5       deraadt   119:        if (optind + 1 != argc )
1.1       deraadt   120:                usage();
1.4       deraadt   121:
1.5       deraadt   122:        if (!domain)
                    123:                yp_get_default_domain(&domain);
1.1       deraadt   124:
                    125:        inmap = argv[optind];
1.3       deraadt   126:        if (!notrans) {
1.5       deraadt   127:                for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    128:                        if (strcmp(inmap, ypaliases[i].alias) == 0)
1.3       deraadt   129:                                inmap = ypaliases[i].name;
                    130:        }
1.1       deraadt   131:        ypcb.foreach = printit;
                    132:        ypcb.data = NULL;
                    133:
1.2       deraadt   134:        r = yp_all(domain, inmap, &ypcb);
1.5       deraadt   135:        switch (r) {
1.1       deraadt   136:        case 0:
                    137:                break;
                    138:        case YPERR_YPBIND:
                    139:                fprintf(stderr, "ypcat: not running ypbind\n");
                    140:                exit(1);
                    141:        default:
                    142:                fprintf(stderr, "No such map %s. Reason: %s\n",
1.5       deraadt   143:                    inmap, yperr_string(r));
1.1       deraadt   144:                exit(1);
                    145:        }
                    146:        exit(0);
                    147: }