1 /* $NetBSD: yptest.c,v 1.9 2009/10/20 00:51:15 snj 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.9 2009/10/20 00:51:15 snj 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 int main(int, char *[]); 47 static int yptest_foreach(int, char *, int, char *, int, char *); 48 49 int 50 main(int argc, char **argv) 51 { 52 char *Domain, *Value, *Key2; 53 const char *Map = "passwd.byname"; 54 const char *Key = "root"; 55 int KeyLen, ValLen, Status, Order; 56 struct ypall_callback Callback; 57 struct ypmaplist *ypml, *y; 58 59 if (argc != 1) { 60 fprintf(stderr, "usage: %s\n", getprogname()); 61 exit(1); 62 } 63 64 Status = yp_get_default_domain(&Domain); 65 if (Status != 0) { 66 printf("Can't get YP domain name: %s\n", yperr_string(Status)); 67 exit(1); 68 } 69 70 printf("Test 1: yp_match\n"); 71 KeyLen = strlen(Key); 72 Status = yp_match(Domain, Map, Key, KeyLen, &Value, &ValLen); 73 if (Status == 0) 74 printf("%*.*s\n", ValLen, ValLen, Value); 75 else 76 printf("yp error: %s\n", yperr_string(Status)); 77 78 printf("\nTest 2: yp_first\n"); 79 Status = yp_first(Domain, Map, &Key2, &KeyLen, &Value, &ValLen); 80 if (Status == 0) 81 printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, ValLen, ValLen, 82 Value); 83 else 84 printf("yp error: %s\n", yperr_string(Status)); 85 86 printf("\nTest 3: yp_next\n"); 87 while (Status == 0) { 88 Status = yp_next(Domain, Map, Key2, KeyLen, &Key2, 89 &KeyLen, &Value, &ValLen); 90 if (Status == 0) 91 printf("%*.*s %*.*s\n", KeyLen, KeyLen, Key2, 92 ValLen, ValLen, Value); 93 else 94 printf("yp error: %s\n", yperr_string(Status)); 95 } 96 97 printf("\nTest 4: yp_master\n"); 98 Status = yp_master(Domain, Map, &Key2); 99 if (Status == 0) 100 printf("%s\n", Key2); 101 else 102 printf("yp error: %s\n", yperr_string(Status)); 103 104 printf("\nTest 5: yp_order\n"); 105 Status = yp_order(Domain, Map, &Order); 106 if (Status == 0) 107 printf("%d\n", Order); 108 else 109 printf("yp error: %s\n", yperr_string(Status)); 110 111 printf("\nTest 6: yp_maplist\n"); 112 ypml = NULL; 113 switch (yp_maplist(Domain, &ypml)) { 114 case 0: 115 for (y = ypml; y;) { 116 ypml = y; 117 printf("%s\n", ypml->ypml_name); 118 y = ypml->ypml_next; 119 } 120 break; 121 default: 122 printf("yp error: %s\n", yperr_string(Status)); 123 break; 124 } 125 126 printf("\nTest 7: yp_all\n"); 127 Callback.foreach = yptest_foreach; 128 Status = yp_all(Domain, Map, &Callback); 129 if (Status != 0) 130 printf("yp error: %s\n", yperr_string(Status)); 131 132 exit(0); 133 } 134 135 static int 136 yptest_foreach(int status, char *key, int keylen, char *val, int vallen, 137 char *data) 138 { 139 140 if (status == YP_NOMORE) 141 return 0; 142 143 /* key avslutas med NUL */ 144 /* val avslutas med NUL */ 145 key[keylen] = '\0'; 146 val[vallen] = '\0'; 147 printf("%s %s\n", key, val); 148 return 0; 149 } 150