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
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
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/
51 #include <rpcsvc/ypclnt.h>
52 #include <rpcsvc/yp_prot.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
[] =
67 ypcat [-k] [-d domainname] [-t] mapname\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.
100 main(int argc
, char ** argv
)
104 struct ypall_callback cbinfo
;
106 get_command_line_args(argc
, argv
);
117 if (translate
&& (strchr(map
, '.') == NULL
) &&
118 (getmapname(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
);
130 fprintf(stderr
, "%s\n", yperr_string(err
));
137 * This does the command line argument processing.
140 get_command_line_args(argc
, argv
)
148 while (--argc
> 0 && (*argv
)[0] == '-') {
150 switch ((*argv
)[1]) {
171 if ((int)strlen(domain
) > YPMAXDOMAIN
) {
172 (void) fprintf(stderr
, err_bad_args
,
178 (void) fprintf(stderr
, err_usage
);
185 (void) fprintf(stderr
, err_usage
);
194 (void) fprintf(stderr
, err_usage
);
197 if ((int)strlen(map
) > YPMAXMAP
) {
198 (void) fprintf(stderr
, err_bad_args
, err_bad_mapname
);
205 * This dumps out the value, optionally the key, and perhaps an error message.
208 callback(status
, key
, kl
, val
, vl
, fail
)
218 if (status
== YP_TRUE
) {
221 (void) printf("%.*s ", kl
, key
);
223 (void) printf("%.*s\n", vl
, val
);
227 e
= ypprot_err(status
);
229 if (e
!= YPERR_NOMORE
) {
230 (void) fprintf(stderr
, "%s\n", yperr_string(e
));
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.
244 one_by_one_all(domain
, map
)
261 if (err
= yp_first(domain
, map
, &outkey
, &outkeylen
, &val
, &vallen
)) {
263 if (err
== YPERR_NOMORE
) {
266 (void) fprintf(stderr
, err_first_failed
,
275 (void) printf("%.*s ", outkeylen
, outkey
);
278 (void) printf("%.*s\n", vallen
, val
);
283 if (err
= yp_next(domain
, map
, key
, keylen
, &outkey
, &outkeylen
,
286 if (err
== YPERR_NOMORE
) {
289 (void) fprintf(stderr
, err_next_failed
,
300 * This gets the local default domainname, and makes sure that it's set
301 * to something reasonable. domain is set here.
306 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
307 domain
= default_domain_name
;
309 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_domainname
);
313 if ((int)strlen(domain
) == 0) {
314 (void) fprintf(stderr
, err_null_kname
, err_bad_domainname
);