]>
git.saurik.com Git - apple/network_cmds.git/blob - ypwhich.tproj/ypwhich.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by Theo de Raadt.
39 * 4. The name of the author may not be used to endorse or promote
40 * products derived from this software without specific prior written
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 static char rcsid
[] = "$Id: ypwhich.c,v 1.1 1999/05/02 03:59:06 wsanchez Exp $";
60 #include <sys/param.h>
61 #include <sys/types.h>
62 #include <sys/socket.h>
68 #include <rpcsvc/yp.h>
69 //#include <rpcsvc/yp_prot.h>
70 #include <rpcsvc/ypclnt.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
77 extern bool_t
xdr_domainname();
82 { "passwd", "passwd.byname" },
83 { "group", "group.byname" },
84 { "networks", "networks.byaddr" },
85 { "hosts", "hosts.byaddr" },
86 { "protocols", "protocols.bynumber" },
87 { "services", "services.byname" },
88 { "aliases", "mail.aliases" },
89 { "ethers", "ethers.byname" },
91 static int n_aliases
= 8;
96 fprintf(stderr
, "Usage:\n");
97 fprintf(stderr
, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
98 fprintf(stderr
, "\typwhich -x\n");
104 * Like yp_bind except can query a specific host
107 bind_host(char *dom
, struct sockaddr_in
*sin
)
109 struct hostent
*hent
= NULL
;
110 struct ypbind_resp ypbr
;
119 client
= clntudp_create(sin
, YPBINDPROG
, YPBINDVERS
, tv
, &sock
);
122 fprintf(stderr
, "can't clntudp_create: %s\n",
123 yperr_string(YPERR_YPBIND
));
129 r
= clnt_call(client
, YPBINDPROC_DOMAIN
,
130 xdr_domainname
, &dom
, xdr_ypbind_resp
, &ypbr
, tv
);
132 if (r
!= RPC_SUCCESS
)
134 fprintf(stderr
, "can't clnt_call: %s\n", yperr_string(YPERR_YPBIND
));
135 clnt_destroy(client
);
140 if (ypbr
.ypbind_status
!= YPBIND_SUCC_VAL
)
142 fprintf(stderr
, "can't yp_bind: Reason: %s\n",
143 yperr_string(ypbr
.ypbind_status
));
144 clnt_destroy(client
);
149 clnt_destroy(client
);
151 memmove(&addr
, ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
, 4);
153 hent
= gethostbyaddr((char *)&(addr
.s_addr
), sizeof(u_long
), AF_INET
);
154 if (hent
!= NULL
) printf("%s\n", hent
->h_name
);
155 else printf("%s\n", inet_ntoa(addr
));
160 main(int argc
, char *argv
[])
162 char *domainname
, *master
, *map
;
166 struct hostent
*hent
;
167 struct sockaddr_in sin
;
168 int notrans
, mode
, getmap
;
171 yp_get_default_domain(&domainname
);
174 getmap
= notrans
= mode
= 0;
175 while ((c
= getopt(argc
, argv
, "xd:mt")) != -1)
180 for (i
= 0; i
< n_aliases
; i
++)
182 printf("Use \"%s\" for \"%s\"\n",
210 bzero(&sin
, sizeof sin
);
211 sin
.sin_family
= AF_INET
;
212 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
214 if (bind_host(domainname
, &sin
)) exit(1);
218 bzero(&sin
, sizeof sin
);
219 sin
.sin_family
= AF_INET
;
220 sin
.sin_addr
.s_addr
= inet_addr(argv
[optind
]);
221 if (sin
.sin_addr
.s_addr
== -1)
223 hent
= gethostbyname(argv
[optind
]);
226 fprintf(stderr
, "ypwhich: host %s unknown\n",
231 bcopy((char *)hent
->h_addr_list
[0],
232 (char *)&sin
.sin_addr
, sizeof sin
.sin_addr
);
235 if (bind_host(domainname
, &sin
)) exit(1);
245 if (argc
-optind
> 1) usage();
250 for (i
= 0; (!notrans
) && (i
< n_aliases
); i
++)
252 if (!strcmp(map
, ypaliases
[i
].alias
)) map
= ypaliases
[i
].name
;
255 r
= yp_master(domainname
, map
, &master
);
259 printf("%s\n", master
);
263 fprintf(stderr
, "ypwhich: not running ypbind\n");
266 fprintf(stderr
, "Can't find master for map %s. Reason: %s\n",
267 map
, yperr_string(r
));
274 r
= yp_maplist(domainname
, &ypml
);
280 r
= yp_master(domainname
, ypml
->map
, &master
);
284 printf("%s %s\n", ypml
->map
, master
);
289 "YP: can't find the master of %s: Reason: %s\n",
290 ypml
->map
, yperr_string(r
));
301 fprintf(stderr
, "ypwhich: not running ypbind\n");
305 fprintf(stderr
, "Can't get map list for domain %s. Reason: %s\n",
306 domainname
, yperr_string(r
));