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
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]
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
36 #pragma ident "%Z%%M% %I% %E% SMI"
39 * This is a user command which dumps each entry in a yp data base. It gets
40 * the stuff using the normal ypclnt package; the user doesn't get to choose
41 * which server gives them the input. Usage is:
42 * ypcat [-k] [-d domain] [-t] map
44 * where the -k switch will dump keys followed by a single blank space
45 * before the value, and the -d switch can be used to specify a domain other
46 * than the default domain. -t switch inhibits nickname translation of map
47 * names. -x is to dump the nickname translation table from file /var/yp/
57 #include <rpcsvc/ypclnt.h>
58 #include <rpcsvc/yp_prot.h>
63 static int translate
= TRUE
;
64 static int dodump
= FALSE
;
65 static int dumpkeys
= FALSE
;
66 static char *domain
= NULL
;
67 static char default_domain_name
[YPMAXDOMAIN
];
68 static char nm
[YPMAXMAP
+1];
69 static char *map
= NULL
;
70 static char nullstring
[] = "";
71 static char err_usage
[] =
73 ypcat [-k] [-d domainname] [-t] mapname\n\
76 mapname may be either a mapname or a nickname for a map.\n\
77 -t inhibits map nickname translation.\n\
78 -k prints keys as well as values.\n\
79 -x dumps the map nickname translation table.\n";
80 static char err_bad_args
[] =
81 "ypcat: %s argument is bad.\n";
82 static char err_cant_get_kname
[] =
83 "ypcat: can't get %s back from system call.\n";
84 static char err_null_kname
[] =
85 "ypcat: the %s hasn't been set on this machine.\n";
86 static char err_bad_mapname
[] = "mapname";
87 static char err_bad_domainname
[] = "domainname";
88 static char err_first_failed
[] =
89 "ypcat: can't get first record from yp. Reason: %s.\n";
90 static char err_next_failed
[] =
91 "ypcat: can't get next record from yp. Reason: %s.\n";
93 static void get_command_line_args();
94 static int callback();
95 static void one_by_one_all();
96 extern void maketable();
97 extern int getmapname();
98 static void getdomain();
101 * This is the mainline for the ypcat process. It pulls whatever arguments
102 * have been passed from the command line, and uses defaults for the rest.
106 main(int argc
, char ** argv
)
110 struct ypall_callback cbinfo
;
112 get_command_line_args(argc
, argv
);
123 if (translate
&& (strchr(map
, '.') == NULL
) &&
124 (getmapname(map
, nm
))) {
128 cbinfo
.foreach
= callback
;
129 cbinfo
.data
= (char *)&fail
;
130 err
= __yp_all_rsvdport(domain
, map
, &cbinfo
);
132 if (err
== YPERR_VERS
) {
133 one_by_one_all(domain
, map
);
136 fprintf(stderr
, "%s\n", yperr_string(err
));
143 * This does the command line argument processing.
146 get_command_line_args(argc
, argv
)
154 while (--argc
> 0 && (*argv
)[0] == '-') {
156 switch ((*argv
)[1]) {
177 if ((int)strlen(domain
) > YPMAXDOMAIN
) {
178 (void) fprintf(stderr
, err_bad_args
,
184 (void) fprintf(stderr
, err_usage
);
191 (void) fprintf(stderr
, err_usage
);
200 (void) fprintf(stderr
, err_usage
);
203 if ((int)strlen(map
) > YPMAXMAP
) {
204 (void) fprintf(stderr
, err_bad_args
, err_bad_mapname
);
211 * This dumps out the value, optionally the key, and perhaps an error message.
214 callback(status
, key
, kl
, val
, vl
, fail
)
224 if (status
== YP_TRUE
) {
227 (void) printf("%.*s ", kl
, key
);
229 (void) printf("%.*s\n", vl
, val
);
233 e
= ypprot_err(status
);
235 if (e
!= YPERR_NOMORE
) {
236 (void) fprintf(stderr
, "%s\n", yperr_string(e
));
245 * This cats the map out by using the old one-by-one enumeration interface.
246 * As such, it is prey to the old-style problems of rebinding to different
247 * servers during the enumeration.
250 one_by_one_all(domain
, map
)
267 if (err
= yp_first(domain
, map
, &outkey
, &outkeylen
, &val
, &vallen
)) {
269 if (err
== YPERR_NOMORE
) {
272 (void) fprintf(stderr
, err_first_failed
,
281 (void) printf("%.*s ", outkeylen
, outkey
);
284 (void) printf("%.*s\n", vallen
, val
);
289 if (err
= yp_next(domain
, map
, key
, keylen
, &outkey
, &outkeylen
,
292 if (err
== YPERR_NOMORE
) {
295 (void) fprintf(stderr
, err_next_failed
,
306 * This gets the local default domainname, and makes sure that it's set
307 * to something reasonable. domain is set here.
312 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
313 domain
= default_domain_name
;
315 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_domainname
);
319 if ((int)strlen(domain
) == 0) {
320 (void) fprintf(stderr
, err_null_kname
, err_bad_domainname
);