xref: /netbsd/usr.sbin/ypserv/yptest/yptest.c (revision c4a72b64)
1 /*	$NetBSD: yptest.c,v 1.6 2002/07/06 00:47:55 wiz 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.6 2002/07/06 00:47:55 wiz 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 	char *Map = "passwd.byname";
59 	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 	if (yp_get_default_domain(&Domain))
70 		errx(1, "can't get YP domain name");
71 
72 	printf("Test 1: yp_match\n");
73 	KeyLen = strlen(Key);
74 	Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen);
75 	printf("%*.*s\n", ValLen, ValLen, Value);
76 
77 	printf("\nTest 2: yp_first\n");
78 	Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen);
79 	printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen, Value);
80 
81 	printf("\nTest 3: yp_next\n");
82 	while (Status == 0) {
83 		Status = yp_next(Domain, Map, Key2, KeyLen, &Key2,
84 		    &KeyLen, &Value, &ValLen);
85 		if (Status == 0)
86 			printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2,
87 			    ValLen, ValLen, Value);
88 	}
89 
90 	printf("\nTest 4: yp_master\n");
91 	Status = yp_master(Domain, Map, &Key2);
92 	printf("%s\n", Key2);
93 
94 	printf("\nTest 5: yp_order\n");
95 	Status = yp_order(Domain, Map, &Order);
96 	printf("%d\n", Order);
97 
98 	printf("\nTest 6: yp_maplist\n");
99 	ypml = NULL;
100 	switch (yp_maplist(Domain, &ypml)) {
101 	case 0:
102 		for (y = ypml; y;) {
103 			ypml = y;
104 			printf("%s\n", ypml->ypml_name);
105 			y = ypml->ypml_next;
106 		}
107 	}
108 
109 	printf("\nTest 7: yp_all\n");
110 	Callback.foreach = yptest_foreach;
111 	Status = yp_all(Domain, Map, &Callback);
112 	exit(0);
113 }
114 
115 static int
116 yptest_foreach(int status, char *key, int keylen, char *val, int vallen,
117 	       char *data)
118 {
119 
120 	if (status == YP_NOMORE)
121 		return 0;
122 
123 	/* key avslutas med NUL */
124 	/* val avslutas med NUL */
125 	key[keylen] = '\0';
126 	val[vallen] = '\0';
127 	printf("%s %s\n", key, val);
128 	return 0;
129 }
130