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.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * This is a user command which looks up the value of a key in a map
41 * ypmatch [-d domain] [-t] [-k] key [key ...] mname
44 * where: the -d switch can be used to specify a domain other than the
45 * default domain. mname may be either a mapname, or a nickname which
46 * will be translated into a mapname according this translation. The
47 * -k switch prints keys as well as values. The -x switch may be used
48 * to dump the translation table.
53 #include <rpcsvc/yp_prot.h>
54 #include <rpcsvc/ypclnt.h>
59 static void get_command_line_args();
60 static void getdomain();
61 static bool match_list();
62 static bool match_one();
63 static void print_one();
64 extern void maketable();
65 extern int getmapname();
66 extern int yp_match_rsvdport();
68 static int translate
= TRUE
;
69 static int dodump
= FALSE
;
70 static int printkeys
= FALSE
;
71 static char *domain
= NULL
;
72 static char default_domain_name
[YPMAXDOMAIN
];
73 static char *map
= NULL
;
74 static char nm
[YPMAXMAP
+1];
75 static char **keys
= NULL
;
77 static char err_usage
[] =
79 ypmatch [-d domain] [-t] [-k] key [key ...] mname\n\
82 mname may be either a mapname or a nickname for a map\n\
83 -t inhibits map nickname translation\n\
84 -k prints keys as well as values.\n\
85 -x dumps the map nickname translation table.\n";
86 static char err_bad_args
[] =
87 "ypmatch: %s argument is bad.\n";
88 static char err_cant_get_kname
[] =
89 "ypmatch: can't get %s back from system call.\n";
90 static char err_null_kname
[] =
91 "ypmatch: the %s hasn't been set on this machine.\n";
92 static char err_bad_mapname
[] = "mapname";
93 static char err_bad_domainname
[] = "domainname";
96 * This is the main line for the ypmatch process.
102 get_command_line_args(argc
, argv
);
113 if (translate
&& (strchr(map
, '.') == NULL
) &&
114 (getmapname(map
, nm
))) {
124 * This does the command line argument processing.
127 get_command_line_args(argc
, argv
)
134 (void) fprintf(stderr
, err_usage
);
139 while (--argc
> 0 && (*argv
)[0] == '-') {
141 switch ((*argv
)[1]) {
162 if ((int)strlen(domain
) > YPMAXDOMAIN
) {
163 (void) fprintf(stderr
, err_bad_args
,
169 (void) fprintf(stderr
, err_usage
);
176 (void) fprintf(stderr
, err_usage
);
185 (void) fprintf(stderr
, err_usage
);
193 if ((int)strlen(map
) > YPMAXMAP
) {
194 (void) fprintf(stderr
, err_bad_args
, err_bad_mapname
);
201 * This gets the local default domainname, and makes sure that it's set
202 * to something reasonable. domain is set here.
207 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
208 domain
= default_domain_name
;
210 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_domainname
);
214 if ((int)strlen(domain
) == 0) {
215 (void) fprintf(stderr
, err_null_kname
, err_bad_domainname
);
221 * This traverses the list of argument keys.
233 error
= match_one(keys
[n
], &val
, &len
);
236 print_one(keys
[n
], val
, len
);
249 * This fires off a "match" request to any old yp server, using the vanilla
250 * yp client interface. To cover the case in which trailing NULLs are included
251 * in the keys, this retrys the match request including the NULL if the key
255 match_one(key
, val
, len
)
265 err
= yp_match_rsvdport(domain
, map
, key
, (int)strlen(key
), val
, len
);
268 if (err
== YPERR_KEY
) {
269 err
= yp_match_rsvdport(domain
, map
, key
,
270 ((int)strlen(key
) + 1),
275 (void) fprintf(stderr
,
276 "Can't match key %s in map %s. Reason: %s.\n", key
, map
,
285 * This prints the value, (and optionally, the key) after first checking that
286 * the last char in the value isn't a NULL. If the last char is a NULL, the
287 * \n\0 sequence which the yp client layer has given to us is shuffled back
291 print_one(key
, val
, len
)
297 (void) printf("%s: ", key
);
300 (void) printf("%.*s\n", len
, val
);