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