xref: /netbsd/usr.sbin/ypserv/yptest/yptest.c (revision bf9ec67e)
1 /*	$NetBSD: yptest.c,v 1.5 2001/02/19 23:22:52 cgd 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.5 2001/02/19 23:22:52 cgd 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 __P((int, char *[]));
52 static	int yptest_foreach __P((int, char *, int, char *, int, char *));
53 
54 int
55 main(argc, argv)
56 	int argc;
57 	char *argv[];
58 {
59 	char *Domain, *Value, *Key2;
60 	char *Map = "passwd.byname";
61 	char *Key = "root";
62 	int KeyLen, ValLen, Status, Order;
63 	struct ypall_callback Callback;
64 	struct ypmaplist *ypml, *y;
65 
66 	if (argc != 1) {
67 		fprintf(stderr, "usage: %s\n", getprogname());
68 		exit(1);
69 	}
70 
71 	if (yp_get_default_domain(&Domain))
72 		errx(1, "can't get YP domain name");
73 
74 	printf("Test 1: yp_match\n");
75 	KeyLen = strlen(Key);
76 	Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen);
77 	printf("%*.*s\n", ValLen, ValLen, Value);
78 
79 	printf("\nTest 2: yp_first\n");
80 	Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen);
81 	printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen, Value);
82 
83 	printf("\nTest 3: yp_next\n");
84 	while (Status == 0) {
85 		Status = yp_next(Domain, Map, Key2, KeyLen, &Key2,
86 		    &KeyLen, &Value, &ValLen);
87 		if (Status == 0)
88 			printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2,
89 			    ValLen, ValLen, Value);
90 	}
91 
92 	printf("\nTest 4: yp_master\n");
93 	Status = yp_master(Domain, Map, &Key2);
94 	printf("%s\n", Key2);
95 
96 	printf("\nTest 5: yp_order\n");
97 	Status = yp_order(Domain, Map, &Order);
98 	printf("%d\n", Order);
99 
100 	printf("\nTest 6: yp_maplist\n");
101 	ypml = NULL;
102 	switch (yp_maplist(Domain, &ypml)) {
103 	case 0:
104 		for (y = ypml; y;) {
105 			ypml = y;
106 			printf("%s\n", ypml->ypml_name);
107 			y = ypml->ypml_next;
108 		}
109 	}
110 
111 	printf("\nTest 7: yp_all\n");
112 	Callback.foreach = yptest_foreach;
113 	Status = yp_all(Domain, Map, &Callback);
114 	exit(0);
115 }
116 
117 static int
118 yptest_foreach(status, key, keylen, val, vallen, data)
119 	int status;
120 	char *key;
121 	int keylen;
122 	char *val;
123 	int vallen;
124 	char *data;
125 {
126 
127 	if (status == YP_NOMORE)
128 		return 0;
129 
130 	/* key avslutas med NUL */
131 	/* val avslutas med NUL */
132 	key[keylen] = '\0';
133 	val[vallen] = '\0';
134 	printf("%s %s\n", key, val);
135 	return 0;
136 }
137