ypwhich.c (6fdf637e231849d1ed733e3322f0684f81c7218d) | ypwhich.c (58458d066f88e28e2059f3ed77a069c4c384abd6) |
---|---|
1/* $OpenBSD: ypwhich.c,v 1.23 2015/02/08 23:40:35 deraadt Exp $ */ 2/* $NetBSD: ypwhich.c,v 1.6 1996/05/13 02:43:48 thorpej Exp $ */ 3 |
|
1/* | 4/* |
2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> | 5 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com> |
3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. | 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. |
13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. | |
16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) --- 5 unchanged lines hidden (view full) --- 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD$"); 32 33#include <sys/param.h> 34#include <sys/types.h> 35#include <sys/socket.h> 36 | 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) --- 5 unchanged lines hidden (view full) --- 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD$"); 32 33#include <sys/param.h> 34#include <sys/types.h> 35#include <sys/socket.h> 36 |
37#include <rpc/rpc.h> 38#include <rpc/xdr.h> 39#include <rpcsvc/yp_prot.h> 40#include <rpcsvc/ypclnt.h> 41 | |
42#include <netinet/in.h> | 37#include <netinet/in.h> |
43 | |
44#include <arpa/inet.h> 45 46#include <ctype.h> 47#include <err.h> 48#include <netdb.h> 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52#include <unistd.h> 53 | 38#include <arpa/inet.h> 39 40#include <ctype.h> 41#include <err.h> 42#include <netdb.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> 47 |
54#define ERR_USAGE 1 /* bad arguments - display 'usage' message */ 55#define ERR_NOSUCHHOST 2 /* no such host */ 56#define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */ 57#define ERR_NOYPBIND 4 /* ypbind not running */ 58#define ERR_NOMASTER 5 /* could not find master server */ | 48#include <rpc/rpc.h> 49#include <rpc/xdr.h> 50#include <rpcsvc/yp.h> 51#include <rpcsvc/ypclnt.h> |
59 | 52 |
60extern bool_t xdr_domainname(); | 53#include "yplib_host.h" |
61 62static const struct ypalias { 63 char *alias, *name; 64} ypaliases[] = { 65 { "passwd", "passwd.byname" }, 66 { "master.passwd", "master.passwd.byname" }, 67 { "shadow", "shadow.byname" }, 68 { "group", "group.byname" }, 69 { "networks", "networks.byaddr" }, 70 { "hosts", "hosts.byaddr" }, 71 { "protocols", "protocols.bynumber" }, 72 { "services", "services.byname" }, 73 { "aliases", "mail.aliases" }, 74 { "ethers", "ethers.byname" }, 75}; 76 77static void 78usage(void) 79{ | 54 55static const struct ypalias { 56 char *alias, *name; 57} ypaliases[] = { 58 { "passwd", "passwd.byname" }, 59 { "master.passwd", "master.passwd.byname" }, 60 { "shadow", "shadow.byname" }, 61 { "group", "group.byname" }, 62 { "networks", "networks.byaddr" }, 63 { "hosts", "hosts.byaddr" }, 64 { "protocols", "protocols.bynumber" }, 65 { "services", "services.byname" }, 66 { "aliases", "mail.aliases" }, 67 { "ethers", "ethers.byname" }, 68}; 69 70static void 71usage(void) 72{ |
80 fprintf(stderr, "%s\n%s\n", 81 "usage: ypwhich [-d domain] [[-t] -m [mname] | host]", 82 " ypwhich -x"); 83 exit(ERR_USAGE); | 73 fprintf(stderr, 74 "usage: ypwhich [-t] [-d domain] [[-h] host]\n" 75 " ypwhich [-t] [-d domain] [-h host] -m [mname]\n" 76 " ypwhich -x\n"); 77 exit(1); |
84} 85 86 87/* 88 * Like yp_bind except can query a specific host 89 */ 90static int | 78} 79 80 81/* 82 * Like yp_bind except can query a specific host 83 */ 84static int |
91bind_host(char *dom, struct sockaddr_in *lsin) | 85bind_host(char *dom, struct sockaddr_in *sin) |
92{ 93 struct hostent *hent = NULL; 94 struct ypbind_resp ypbr; | 86{ 87 struct hostent *hent = NULL; 88 struct ypbind_resp ypbr; |
89 struct in_addr ss_addr; |
|
95 struct timeval tv; 96 CLIENT *client; 97 int sock, r; | 90 struct timeval tv; 91 CLIENT *client; 92 int sock, r; |
98 struct in_addr ss_addr; | |
99 100 sock = RPC_ANYSOCK; 101 tv.tv_sec = 15; 102 tv.tv_usec = 0; | 93 94 sock = RPC_ANYSOCK; 95 tv.tv_sec = 15; 96 tv.tv_usec = 0; |
103 client = clntudp_create(lsin, YPBINDPROG, YPBINDVERS, tv, &sock); | 97 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 98 |
104 if (client == NULL) { | 99 if (client == NULL) { |
105 warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND)); | 100 warnx("host is not bound to a ypmaster"); |
106 return (YPERR_YPBIND); 107 } 108 109 tv.tv_sec = 5; 110 tv.tv_usec = 0; | 101 return (YPERR_YPBIND); 102 } 103 104 tv.tv_sec = 5; 105 tv.tv_usec = 0; |
106 |
|
111 r = clnt_call(client, YPBINDPROC_DOMAIN, 112 (xdrproc_t)xdr_domainname, &dom, 113 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv); 114 if (r != RPC_SUCCESS) { 115 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND)); 116 clnt_destroy(client); 117 return (YPERR_YPBIND); 118 } else { 119 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 120 warnx("can't yp_bind: reason: %s", | 107 r = clnt_call(client, YPBINDPROC_DOMAIN, 108 (xdrproc_t)xdr_domainname, &dom, 109 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv); 110 if (r != RPC_SUCCESS) { 111 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND)); 112 clnt_destroy(client); 113 return (YPERR_YPBIND); 114 } else { 115 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 116 warnx("can't yp_bind: reason: %s", |
121 ypbinderr_string(ypbr.ypbind_respbody.ypbind_error)); | 117 yperr_string(ypbr.ypbind_status)); |
122 clnt_destroy(client); 123 return (r); 124 } 125 } 126 clnt_destroy(client); 127 | 118 clnt_destroy(client); 119 return (r); 120 } 121 } 122 clnt_destroy(client); 123 |
128 ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr; 129 /*printf("%08x\n", ss_addr);*/ 130 hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET); 131 if (hent) | 124 memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 125 sizeof (ss_addr)); 126 127 hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr), 128 AF_INET); 129 if (hent != NULL) |
132 printf("%s\n", hent->h_name); 133 else 134 printf("%s\n", inet_ntoa(ss_addr)); | 130 printf("%s\n", hent->h_name); 131 else 132 printf("%s\n", inet_ntoa(ss_addr)); |
133 |
|
135 return (0); 136} 137 138int 139main(int argc, char *argv[]) 140{ | 134 return (0); 135} 136 137int 138main(int argc, char *argv[]) 139{ |
141 char *domnam = NULL, *master; 142 char *map = NULL; | 140 char *domain, *master, *map = NULL, *host = NULL; 141 int notrans = 0, mode = 0, c, r, i; |
143 struct ypmaplist *ypml, *y; | 142 struct ypmaplist *ypml, *y; |
143 struct sockaddr_in sin; |
|
144 struct hostent *hent; | 144 struct hostent *hent; |
145 struct sockaddr_in lsin; 146 int notrans, mode; 147 int c, r; 148 u_int i; | 145 CLIENT *client = NULL; |
149 | 146 |
150 notrans = mode = 0; 151 while ((c = getopt(argc, argv, "xd:mt")) != -1) | 147 yp_get_default_domain(&domain); 148 if (domain == NULL) 149 errx(1, "YP domain name not set"); 150 151 while ((c = getopt(argc, argv, "xd:h:mt")) != -1) |
152 switch (c) { 153 case 'x': 154 for (i = 0; i < nitems(ypaliases); i++) 155 printf("\"%s\" is an alias for \"%s\"\n", 156 ypaliases[i].alias, 157 ypaliases[i].name); 158 exit(0); | 152 switch (c) { 153 case 'x': 154 for (i = 0; i < nitems(ypaliases); i++) 155 printf("\"%s\" is an alias for \"%s\"\n", 156 ypaliases[i].alias, 157 ypaliases[i].name); 158 exit(0); |
159 case 'h': 160 host = optarg; 161 break; |
|
159 case 'd': | 162 case 'd': |
160 domnam = optarg; | 163 domain = optarg; |
161 break; 162 case 't': | 164 break; 165 case 't': |
163 notrans++; | 166 notrans = 1; |
164 break; 165 case 'm': | 167 break; 168 case 'm': |
166 mode++; | 169 mode = 1; |
167 break; 168 default: 169 usage(); 170 } | 170 break; 171 default: 172 usage(); 173 } |
174 argc -= optind; 175 argv += optind; |
|
171 | 176 |
172 if (domnam == NULL) 173 yp_get_default_domain(&domnam); 174 | |
175 if (mode == 0) { | 177 if (mode == 0) { |
176 switch (argc-optind) { | 178 switch (argc) { |
177 case 0: | 179 case 0: |
178 bzero(&lsin, sizeof lsin); 179 lsin.sin_family = AF_INET; 180 lsin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | 180 memset(&sin, 0, sizeof sin); 181 sin.sin_family = AF_INET; 182 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
181 | 183 |
182 if (bind_host(domnam, &lsin)) 183 exit(ERR_NOBINDING); | 184 if (bind_host(domain, &sin)) 185 exit(1); |
184 break; 185 case 1: | 186 break; 187 case 1: |
186 bzero(&lsin, sizeof lsin); 187 lsin.sin_family = AF_INET; 188 if ((lsin.sin_addr.s_addr = inet_addr(argv[optind])) == INADDR_NONE) { 189 hent = gethostbyname(argv[optind]); 190 if (!hent) 191 errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]); 192 bcopy((char *)hent->h_addr_list[0], 193 (char *)&lsin.sin_addr, sizeof lsin.sin_addr); | 188 bzero(&sin, sizeof sin); 189 sin.sin_family = AF_INET; 190 if (inet_aton(argv[0], &sin.sin_addr) == 0) { 191 hent = gethostbyname(argv[0]); 192 if (!hent) { 193 errx(1, "host %s unknown", 194 argv[0]); 195 } |
194 } | 196 } |
195 if (bind_host(domnam, &lsin)) 196 exit(ERR_NOBINDING); | 197 if (bind_host(domain, &sin)) 198 exit(1); |
197 break; 198 default: 199 usage(); 200 } 201 exit(0); 202 } 203 | 199 break; 200 default: 201 usage(); 202 } 203 exit(0); 204 } 205 |
204 if (argc-optind > 1) | 206 if (argc > 1) |
205 usage(); 206 | 207 usage(); 208 |
207 if (argv[optind]) { 208 map = argv[optind]; | 209 if (host != NULL) 210 client = yp_bind_host(host, YPPROG, YPVERS, 0, 1); 211 212 if (argv[0]) { 213 map = argv[0]; |
209 if (notrans == 0) { 210 for (i = 0; i < nitems(ypaliases); i++) 211 if (strcmp(map, ypaliases[i].alias) == 0) 212 map = ypaliases[i].name; 213 } | 214 if (notrans == 0) { 215 for (i = 0; i < nitems(ypaliases); i++) 216 if (strcmp(map, ypaliases[i].alias) == 0) 217 map = ypaliases[i].name; 218 } |
214 r = yp_master(domnam, map, &master); | 219 220 if (host != NULL) 221 r = yp_master_host(client, domain, map, &master); 222 else 223 r = yp_master(domain, map, &master); 224 |
215 switch (r) { 216 case 0: 217 printf("%s\n", master); 218 free(master); 219 break; 220 case YPERR_YPBIND: | 225 switch (r) { 226 case 0: 227 printf("%s\n", master); 228 free(master); 229 break; 230 case YPERR_YPBIND: |
221 errx(ERR_NOYPBIND, "not running ypbind"); | 231 errx(1, "not running ypbind"); |
222 default: | 232 default: |
223 errx(ERR_NOMASTER, "can't find master for map %s: reason: %s", 224 map, yperr_string(r)); | 233 errx(1, "can't find master for map %s: reason: %s", 234 map, yperr_string(r)); |
225 } 226 exit(0); 227 } 228 229 ypml = NULL; | 235 } 236 exit(0); 237 } 238 239 ypml = NULL; |
230 r = yp_maplist(domnam, &ypml); | 240 if (host != NULL) 241 r = yp_maplist_host(client, domain, &ypml); 242 else 243 r = yp_maplist(domain, &ypml); 244 245 r = 0; |
231 switch (r) { 232 case 0: | 246 switch (r) { 247 case 0: |
233 for (y = ypml; y;) { | 248 for (y = ypml; y; ) { |
234 ypml = y; | 249 ypml = y; |
235 r = yp_master(domnam, ypml->ypml_name, &master); | 250 if (host != NULL) { 251 r = yp_master_host(client, 252 domain, ypml->map, &master); 253 } else { 254 r = yp_master(domain, ypml->map, &master); 255 } |
236 switch (r) { 237 case 0: | 256 switch (r) { 257 case 0: |
238 printf("%s %s\n", ypml->ypml_name, master); | 258 printf("%s %s\n", ypml->map, master); |
239 free(master); 240 break; 241 default: 242 warnx("can't find the master of %s: reason: %s", | 259 free(master); 260 break; 261 default: 262 warnx("can't find the master of %s: reason: %s", |
243 ypml->ypml_name, yperr_string(r)); | 263 ypml->map, yperr_string(r)); |
244 break; 245 } | 264 break; 265 } |
246 y = ypml->ypml_next; | 266 y = ypml->next; |
247 free(ypml); 248 } 249 break; 250 case YPERR_YPBIND: | 267 free(ypml); 268 } 269 break; 270 case YPERR_YPBIND: |
251 errx(ERR_NOYPBIND, "not running ypbind"); | 271 errx(1, "not running ypbind"); |
252 default: | 272 default: |
253 errx(ERR_NOMASTER, "can't get map list for domain %s: reason: %s", 254 domnam, yperr_string(r)); | 273 errx(1, "can't get map list for domain %s: reason: %s", 274 domain, yperr_string(r)); |
255 } 256 exit(0); 257} | 275 } 276 exit(0); 277} |