Merge commit 'a058d1cc571af5fbcfe7f1d719df1abbfdb722f3' into merges
[unleashed.git] / usr / src / cmd / ypcmd / ypcat.c
blobd2fb321ad1e6b4a6e5f8e7e2ad2c7861a6167c78
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2016 by Delphix. All rights reserved.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley
32 * under license from the Regents of the University of
33 * California.
37 * This is a user command which dumps each entry in a yp data base. It gets
38 * the stuff using the normal ypclnt package; the user doesn't get to choose
39 * which server gives them the input. Usage is:
40 * ypcat [-k] [-d domain] [-t] map
41 * ypcat -x
42 * where the -k switch will dump keys followed by a single blank space
43 * before the value, and the -d switch can be used to specify a domain other
44 * than the default domain. -t switch inhibits nickname translation of map
45 * names. -x is to dump the nickname translation table from file /var/yp/
46 * nicknames.
49 #include <stdio.h>
50 #include <rpc/rpc.h>
51 #include <rpcsvc/ypclnt.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <stdlib.h>
57 static int translate = TRUE;
58 static int dodump = FALSE;
59 static int dumpkeys = FALSE;
60 static char *domain = NULL;
61 static char default_domain_name[YPMAXDOMAIN];
62 static char nm[YPMAXMAP+1];
63 static char *map = NULL;
64 static char nullstring[] = "";
65 static char err_usage[] =
66 "Usage:\n\
67 ypcat [-k] [-d domainname] [-t] mapname\n\
68 ypcat -x\n\
69 where\n\
70 mapname may be either a mapname or a nickname for a map.\n\
71 -t inhibits map nickname translation.\n\
72 -k prints keys as well as values.\n\
73 -x dumps the map nickname translation table.\n";
74 static char err_bad_args[] =
75 "ypcat: %s argument is bad.\n";
76 static char err_cant_get_kname[] =
77 "ypcat: can't get %s back from system call.\n";
78 static char err_null_kname[] =
79 "ypcat: the %s hasn't been set on this machine.\n";
80 static char err_bad_mapname[] = "mapname";
81 static char err_bad_domainname[] = "domainname";
82 static char err_first_failed[] =
83 "ypcat: can't get first record from yp. Reason: %s.\n";
84 static char err_next_failed[] =
85 "ypcat: can't get next record from yp. Reason: %s.\n";
87 static void get_command_line_args();
88 static int callback();
89 static void one_by_one_all();
90 extern void maketable();
91 extern int getmapname();
92 static void getdomain();
95 * This is the mainline for the ypcat process. It pulls whatever arguments
96 * have been passed from the command line, and uses defaults for the rest.
99 int
100 main(int argc, char ** argv)
102 int err;
103 int fail = 0;
104 struct ypall_callback cbinfo;
106 get_command_line_args(argc, argv);
108 if (dodump) {
109 maketable(dodump);
110 exit(0);
113 if (!domain) {
114 getdomain();
117 if (translate && (strchr(map, '.') == NULL) &&
118 (getmapname(map, nm))) {
119 map = nm;
122 cbinfo.foreach = callback;
123 cbinfo.data = (char *)&fail;
124 err = __yp_all_rsvdport(domain, map, &cbinfo);
126 if (err == YPERR_VERS) {
127 one_by_one_all(domain, map);
128 } else if (err) {
129 fail = TRUE;
130 fprintf(stderr, "%s\n", yperr_string(err));
133 exit(fail);
137 * This does the command line argument processing.
139 static void
140 get_command_line_args(argc, argv)
141 int argc;
142 char **argv;
146 argv++;
148 while (--argc > 0 && (*argv)[0] == '-') {
150 switch ((*argv)[1]) {
152 case 't':
153 translate = FALSE;
154 break;
156 case 'k':
157 dumpkeys = TRUE;
158 break;
160 case 'x':
161 dodump = TRUE;
162 break;
164 case 'd':
166 if (argc > 1) {
167 argv++;
168 argc--;
169 domain = *argv;
171 if ((int)strlen(domain) > YPMAXDOMAIN) {
172 (void) fprintf(stderr, err_bad_args,
173 err_bad_domainname);
174 exit(1);
177 } else {
178 (void) fprintf(stderr, err_usage);
179 exit(1);
182 break;
184 default:
185 (void) fprintf(stderr, err_usage);
186 exit(1);
188 argv++;
191 if (!dodump) {
192 map = *argv;
193 if (argc < 1) {
194 (void) fprintf(stderr, err_usage);
195 exit(1);
197 if ((int)strlen(map) > YPMAXMAP) {
198 (void) fprintf(stderr, err_bad_args, err_bad_mapname);
199 exit(1);
205 * This dumps out the value, optionally the key, and perhaps an error message.
207 static int
208 callback(status, key, kl, val, vl, fail)
209 int status;
210 char *key;
211 int kl;
212 char *val;
213 int vl;
214 int *fail;
216 int e;
218 if (status == YP_TRUE) {
220 if (dumpkeys)
221 (void) printf("%.*s ", kl, key);
223 (void) printf("%.*s\n", vl, val);
224 return (FALSE);
225 } else {
227 e = ypprot_err(status);
229 if (e != YPERR_NOMORE) {
230 (void) fprintf(stderr, "%s\n", yperr_string(e));
231 *fail = TRUE;
234 return (TRUE);
239 * This cats the map out by using the old one-by-one enumeration interface.
240 * As such, it is prey to the old-style problems of rebinding to different
241 * servers during the enumeration.
243 static void
244 one_by_one_all(domain, map)
245 char *domain;
246 char *map;
248 char *key;
249 int keylen;
250 char *outkey;
251 int outkeylen;
252 char *val;
253 int vallen;
254 int err;
256 key = nullstring;
257 keylen = 0;
258 val = nullstring;
259 vallen = 0;
261 if (err = yp_first(domain, map, &outkey, &outkeylen, &val, &vallen)) {
263 if (err == YPERR_NOMORE) {
264 exit(0);
265 } else {
266 (void) fprintf(stderr, err_first_failed,
267 yperr_string(err));
268 exit(1);
272 for (;;) {
274 if (dumpkeys) {
275 (void) printf("%.*s ", outkeylen, outkey);
278 (void) printf("%.*s\n", vallen, val);
279 free(val);
280 key = outkey;
281 keylen = outkeylen;
283 if (err = yp_next(domain, map, key, keylen, &outkey, &outkeylen,
284 &val, &vallen)) {
286 if (err == YPERR_NOMORE) {
287 break;
288 } else {
289 (void) fprintf(stderr, err_next_failed,
290 yperr_string(err));
291 exit(1);
295 free(key);
300 * This gets the local default domainname, and makes sure that it's set
301 * to something reasonable. domain is set here.
303 static void
304 getdomain()
306 if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
307 domain = default_domain_name;
308 } else {
309 (void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
310 exit(1);
313 if ((int)strlen(domain) == 0) {
314 (void) fprintf(stderr, err_null_kname, err_bad_domainname);
315 exit(1);