Annotation of src/usr.bin/ypmatch/ypmatch.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD$ */
! 2: /* $NetBSD: ypmatch.c,v 1.6 1996/04/30 22:58:31 jtc Exp $ */
1.1 deraadt 3:
4: /*
1.3 ! deraadt 5: * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
1.1 deraadt 6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by Theo de Raadt.
19: * 4. The name of the author may not be used to endorse or promote
20: * products derived from this software without specific prior written
21: * permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef LINT
1.3 ! deraadt 37: static char rcsid[] = "$OpenBSD$";
1.1 deraadt 38: #endif
39:
40: #include <sys/param.h>
41: #include <sys/types.h>
42: #include <sys/socket.h>
43: #include <stdio.h>
44: #include <string.h>
45: #include <ctype.h>
46:
47: #include <rpc/rpc.h>
48: #include <rpc/xdr.h>
49: #include <rpcsvc/yp_prot.h>
50: #include <rpcsvc/ypclnt.h>
51:
52: struct ypalias {
53: char *alias, *name;
54: } ypaliases[] = {
55: { "passwd", "passwd.byname" },
56: { "group", "group.byname" },
57: { "networks", "networks.byaddr" },
58: { "hosts", "hosts.byname" },
59: { "protocols", "protocols.bynumber" },
60: { "services", "services.byname" },
61: { "aliases", "mail.aliases" },
62: { "ethers", "ethers.byname" },
63: };
64:
65: usage()
66: {
67: fprintf(stderr, "Usage:\n");
68: fprintf(stderr, "\typmatch [-d domain] [-t] [-k] key [key ...] mname\n");
69: fprintf(stderr, "\typmatch -x\n");
70: fprintf(stderr, "where\n");
71: fprintf(stderr, "\tmname may be either a mapname or a nickname for a map\n");
72: fprintf(stderr, "\t-t inhibits map nickname translation\n");
73: fprintf(stderr, "\t-k prints keys as well as values.\n");
74: fprintf(stderr, "\t-x dumps the map nickname translation table.\n");
75: exit(1);
76: }
77:
78: int
79: main(argc, argv)
80: char **argv;
81: {
82: char *domainname;
83: char *inkey, *inmap, *outbuf;
84: extern char *optarg;
85: extern int optind;
86: int outbuflen, key, notrans;
87: int c, r, i;
88:
89: notrans = key = 0;
90: yp_get_default_domain(&domainname);
91:
92: while( (c=getopt(argc, argv, "xd:kt")) != -1)
93: switch(c) {
94: case 'x':
95: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
96: printf("Use \"%s\" for \"%s\"\n",
97: ypaliases[i].alias,
98: ypaliases[i].name);
99: exit(0);
100: case 'd':
101: domainname = optarg;
102: break;
103: case 't':
104: notrans++;
105: break;
106: case 'k':
107: key++;
108: break;
109: default:
110: usage();
111: }
112:
113: if( (argc-optind) < 2 )
114: usage();
115:
116: inmap = argv[argc-1];
1.3 ! deraadt 117: if (!notrans) {
! 118: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
! 119: if( strcmp(inmap, ypaliases[i].alias) == 0)
! 120: inmap = ypaliases[i].name;
! 121: }
1.1 deraadt 122: for(; optind < argc-1; optind++) {
123: inkey = argv[optind];
124:
125: r = yp_match(domainname, inmap, inkey,
126: strlen(inkey), &outbuf, &outbuflen);
127: switch(r) {
128: case 0:
129: if(key)
130: printf("%s ", inkey);
131: printf("%*.*s\n", outbuflen, outbuflen, outbuf);
132: break;
133: case YPERR_YPBIND:
134: fprintf(stderr, "yp_match: not running ypbind\n");
135: exit(1);
136: default:
137: fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
138: inkey, inmap, yperr_string(r));
1.2 deraadt 139: exit(1);
1.1 deraadt 140: }
141: }
142: exit(0);
143: }