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