xref: /netbsd-src/usr.sbin/ypserv/yptest/yptest.c (revision f7a3540ecf73535f4217ff5a1ae640ad21e02832)
1 /*	$NetBSD: yptest.c,v 1.10 2017/05/04 16:26:10 sevan 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: yptest.c,v 1.10 2017/05/04 16:26:10 sevan Exp $");
32 #endif
33 
34 #include <sys/types.h>
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <rpc/rpc.h>
42 #include <rpc/xdr.h>
43 #include <rpcsvc/yp_prot.h>
44 #include <rpcsvc/ypclnt.h>
45 
46 static	int yptest_foreach(int, char *, int, char *, int, char *);
47 
48 int
main(int argc,char ** argv)49 main(int argc, char **argv)
50 {
51 	char *Domain, *Value, *Key2;
52 	const char *Map = "passwd.byname";
53 	const char *Key = "root";
54 	int KeyLen, ValLen, Status, Order;
55 	struct ypall_callback Callback;
56 	struct ypmaplist *ypml, *y;
57 
58 	if (argc != 1) {
59 		fprintf(stderr, "usage: %s\n", getprogname());
60 		exit(1);
61 	}
62 
63 	Status = yp_get_default_domain(&Domain);
64 	if (Status != 0) {
65 		printf("Can't get YP domain name: %s\n", yperr_string(Status));
66 		exit(1);
67 	}
68 
69 	printf("Test 1: yp_match\n");
70 	KeyLen = strlen(Key);
71 	Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen);
72 	if (Status == 0)
73 		printf("%*.*s\n", ValLen, ValLen, Value);
74 	else
75 		printf("yp error: %s\n", yperr_string(Status));
76 
77 	printf("\nTest 2: yp_first\n");
78 	Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen);
79 	if (Status == 0)
80 		printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen,
81 		    Value);
82 	else
83 		printf("yp error: %s\n", yperr_string(Status));
84 
85 	printf("\nTest 3: yp_next\n");
86 	while (Status == 0) {
87 		Status = yp_next(Domain, Map, Key2, KeyLen, &Key2,
88 		    &KeyLen, &Value, &ValLen);
89 		if (Status == 0)
90 			printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2,
91 			    ValLen, ValLen, Value);
92 		else
93 			printf("yp error: %s\n", yperr_string(Status));
94 	}
95 
96 	printf("\nTest 4: yp_master\n");
97 	Status = yp_master(Domain, Map, &Key2);
98 	if (Status == 0)
99 		printf("%s\n", Key2);
100 	else
101 		printf("yp error: %s\n", yperr_string(Status));
102 
103 	printf("\nTest 5: yp_order\n");
104 	Status = yp_order(Domain, Map, &Order);
105 	if (Status == 0)
106 		printf("%d\n", Order);
107 	else
108 		printf("yp error: %s\n", yperr_string(Status));
109 
110 	printf("\nTest 6: yp_maplist\n");
111 	ypml = NULL;
112 	switch (yp_maplist(Domain, &ypml)) {
113 	case 0:
114 		for (y = ypml; y;) {
115 			ypml = y;
116 			printf("%s\n", ypml->ypml_name);
117 			y = ypml->ypml_next;
118 		}
119 		break;
120 	default:
121 		printf("yp error: %s\n", yperr_string(Status));
122 		break;
123 	}
124 
125 	printf("\nTest 7: yp_all\n");
126 	Callback.foreach = yptest_foreach;
127 	Status = yp_all(Domain, Map, &Callback);
128 	if (Status != 0)
129 		printf("yp error: %s\n", yperr_string(Status));
130 
131 	exit(0);
132 }
133 
134 static int
yptest_foreach(int status,char * key,int keylen,char * val,int vallen,char * data)135 yptest_foreach(int status, char *key, int keylen, char *val, int vallen,
136 	       char *data)
137 {
138 
139 	if (status == YP_NOMORE)
140 		return 0;
141 
142 	/* key avslutas med NUL */
143 	/* val avslutas med NUL */
144 	key[keylen] = '\0';
145 	val[vallen] = '\0';
146 	printf("%s %s\n", key, val);
147 	return 0;
148 }
149