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