xref: /netbsd-src/usr.sbin/ypserv/yptest/yptest.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: yptest.c,v 1.8 2004/10/22 18:41:09 peter Exp $	 */
2 
3 /*
4  * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Mats O Jansson
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: yptest.c,v 1.8 2004/10/22 18:41:09 peter Exp $");
37 #endif
38 
39 #include <sys/types.h>
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <rpc/rpc.h>
47 #include <rpc/xdr.h>
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
50 
51 int	main(int, char *[]);
52 static	int yptest_foreach(int, char *, int, char *, int, char *);
53 
54 int
55 main(int argc, char **argv)
56 {
57 	char *Domain, *Value, *Key2;
58 	const char *Map = "passwd.byname";
59 	const char *Key = "root";
60 	int KeyLen, ValLen, Status, Order;
61 	struct ypall_callback Callback;
62 	struct ypmaplist *ypml, *y;
63 
64 	if (argc != 1) {
65 		fprintf(stderr, "usage: %s\n", getprogname());
66 		exit(1);
67 	}
68 
69 	Status = yp_get_default_domain(&Domain);
70 	if (Status != 0) {
71 		printf("Can't get YP domain name: %s\n", yperr_string(Status));
72 		exit(1);
73 	}
74 
75 	printf("Test 1: yp_match\n");
76 	KeyLen = strlen(Key);
77 	Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen);
78 	if (Status == 0)
79 		printf("%*.*s\n", ValLen, ValLen, Value);
80 	else
81 		printf("yp error: %s\n", yperr_string(Status));
82 
83 	printf("\nTest 2: yp_first\n");
84 	Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen);
85 	if (Status == 0)
86 		printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen,
87 		    Value);
88 	else
89 		printf("yp error: %s\n", yperr_string(Status));
90 
91 	printf("\nTest 3: yp_next\n");
92 	while (Status == 0) {
93 		Status = yp_next(Domain, Map, Key2, KeyLen, &Key2,
94 		    &KeyLen, &Value, &ValLen);
95 		if (Status == 0)
96 			printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2,
97 			    ValLen, ValLen, Value);
98 		else
99 			printf("yp error: %s\n", yperr_string(Status));
100 	}
101 
102 	printf("\nTest 4: yp_master\n");
103 	Status = yp_master(Domain, Map, &Key2);
104 	if (Status == 0)
105 		printf("%s\n", Key2);
106 	else
107 		printf("yp error: %s\n", yperr_string(Status));
108 
109 	printf("\nTest 5: yp_order\n");
110 	Status = yp_order(Domain, Map, &Order);
111 	if (Status == 0)
112 		printf("%d\n", Order);
113 	else
114 		printf("yp error: %s\n", yperr_string(Status));
115 
116 	printf("\nTest 6: yp_maplist\n");
117 	ypml = NULL;
118 	switch (yp_maplist(Domain, &ypml)) {
119 	case 0:
120 		for (y = ypml; y;) {
121 			ypml = y;
122 			printf("%s\n", ypml->ypml_name);
123 			y = ypml->ypml_next;
124 		}
125 		break;
126 	default:
127 		printf("yp error: %s\n", yperr_string(Status));
128 		break;
129 	}
130 
131 	printf("\nTest 7: yp_all\n");
132 	Callback.foreach = yptest_foreach;
133 	Status = yp_all(Domain, Map, &Callback);
134 	if (Status != 0)
135 		printf("yp error: %s\n", yperr_string(Status));
136 
137 	exit(0);
138 }
139 
140 static int
141 yptest_foreach(int status, char *key, int keylen, char *val, int vallen,
142 	       char *data)
143 {
144 
145 	if (status == YP_NOMORE)
146 		return 0;
147 
148 	/* key avslutas med NUL */
149 	/* val avslutas med NUL */
150 	key[keylen] = '\0';
151 	val[vallen] = '\0';
152 	printf("%s %s\n", key, val);
153 	return 0;
154 }
155