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