[BACK]Return to ypmatch.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / ypmatch

Annotation of src/usr.bin/ypmatch/ypmatch.c, Revision 1.19

1.19    ! wiz         1: /*     $NetBSD: ypmatch.c,v 1.18 2009/06/20 19:27:26 christos Exp $    */
1.5       cgd         2:
1.2       deraadt     3: /*
1.4       deraadt     4:  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
1.2       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:
1.10      thorpej    29: #include <sys/cdefs.h>
                     30: #ifndef lint
1.19    ! wiz        31: __RCSID("$NetBSD: ypmatch.c,v 1.18 2009/06/20 19:27:26 christos Exp $");
1.2       deraadt    32: #endif
                     33:
1.1       deraadt    34: #include <sys/param.h>
                     35: #include <sys/types.h>
                     36: #include <sys/socket.h>
1.10      thorpej    37: #include <ctype.h>
                     38: #include <err.h>
1.1       deraadt    39: #include <stdio.h>
1.12      matt       40: #include <stdlib.h>
1.5       cgd        41: #include <string.h>
1.10      thorpej    42: #include <unistd.h>
1.1       deraadt    43:
                     44: #include <rpc/rpc.h>
                     45: #include <rpc/xdr.h>
                     46: #include <rpcsvc/yp_prot.h>
                     47: #include <rpcsvc/ypclnt.h>
                     48:
1.18      christos   49: #include "ypalias_init.h"
1.1       deraadt    50:
1.18      christos   51: static void    usage(void) __attribute__((__noreturn__));
1.10      thorpej    52:
1.1       deraadt    53: int
1.18      christos   54: main(int argc, char *argv[])
1.1       deraadt    55: {
                     56:        char *domainname;
1.17      lukem      57:        char *inkey, *outbuf;
                     58:        const char *inmap;
1.14      christos   59:        int outbuflen, key, null, notrans;
1.17      lukem      60:        int c, r, len;
                     61:        size_t i;
1.8       jtc        62:        int rval;
1.18      christos   63:        const struct ypalias *ypaliases;
1.1       deraadt    64:
1.18      christos   65:        setprogname(*argv);
1.9       jtc        66:        domainname = NULL;
1.14      christos   67:        notrans = key = null = 0;
1.18      christos   68:        ypaliases = ypalias_init();
1.14      christos   69:        while ((c = getopt(argc, argv, "xd:ktz")) != -1) {
1.10      thorpej    70:                switch (c) {
1.1       deraadt    71:                case 'x':
1.18      christos   72:                        for(i = 0; ypaliases[i].alias; i++)
1.1       deraadt    73:                                printf("Use \"%s\" for \"%s\"\n",
                     74:                                        ypaliases[i].alias,
                     75:                                        ypaliases[i].name);
1.18      christos   76:                        return 0;
1.10      thorpej    77:
1.1       deraadt    78:                case 'd':
                     79:                        domainname = optarg;
                     80:                        break;
1.10      thorpej    81:
1.1       deraadt    82:                case 't':
                     83:                        notrans++;
                     84:                        break;
1.10      thorpej    85:
1.1       deraadt    86:                case 'k':
                     87:                        key++;
                     88:                        break;
1.10      thorpej    89:
1.14      christos   90:                case 'z':
                     91:                        null++;
                     92:                        break;
                     93:
1.1       deraadt    94:                default:
                     95:                        usage();
                     96:                }
1.10      thorpej    97:        }
                     98:
                     99:        argc -= optind;
                    100:        argv += optind;
1.1       deraadt   101:
1.10      thorpej   102:        if (argc < 2)
1.1       deraadt   103:                usage();
1.9       jtc       104:
1.10      thorpej   105:        if (domainname == NULL)
1.9       jtc       106:                yp_get_default_domain(&domainname);
1.1       deraadt   107:
1.10      thorpej   108:        inmap = argv[argc - 1];
                    109:        if (notrans == 0) {
1.18      christos  110:                for (i = 0; ypaliases[i].alias; i++)
1.10      thorpej   111:                        if (strcmp(inmap, ypaliases[i].alias) == 0)
1.6       jtc       112:                                inmap = ypaliases[i].name;
                    113:        }
1.8       jtc       114:
                    115:        rval = 0;
1.17      lukem     116:        for(c = 0; c < (argc - 1); c++) {
                    117:                inkey = argv[c];
1.1       deraadt   118:
1.14      christos  119:                len = strlen(inkey);
                    120:                if (null)
                    121:                        len++;
                    122:                r = yp_match(domainname, inmap, inkey, len,
1.10      thorpej   123:                    &outbuf, &outbuflen);
                    124:                switch (r) {
1.1       deraadt   125:                case 0:
1.10      thorpej   126:                        if (key)
1.7       jtc       127:                                printf("%s: ", inkey);
1.14      christos  128:                        fwrite(outbuf, outbuflen, 1, stdout);
                    129:                        putc('\n', stdout);
1.1       deraadt   130:                        break;
1.10      thorpej   131:
1.1       deraadt   132:                case YPERR_YPBIND:
1.10      thorpej   133:                        errx(1, "not running ypbind");
                    134:
1.1       deraadt   135:                default:
1.10      thorpej   136:                        warnx("can't match key %s in map %s.  Reason: %s",
                    137:                            inkey, inmap, yperr_string(r));
1.8       jtc       138:                        rval = 1;
1.1       deraadt   139:                        break;
                    140:                }
                    141:        }
1.8       jtc       142:
1.18      christos  143:        return rval;
1.10      thorpej   144: }
                    145:
1.18      christos  146: static void
                    147: usage(void)
1.10      thorpej   148: {
                    149:
1.19    ! wiz       150:        (void)fprintf(stderr, "Usage: %s [-ktz] [-d domain] key ... "
1.13      cgd       151:            "mapname\n", getprogname());
1.18      christos  152:        (void)fprintf(stderr, "       %s -x\n", getprogname());
1.10      thorpej   153:        exit(1);
1.1       deraadt   154: }

CVSweb <webmaster@jp.NetBSD.org>