]> git.saurik.com Git - apple/libinfo.git/blob - nis.subproj/yp_bind.c
Libinfo-222.3.6.tar.gz
[apple/libinfo.git] / nis.subproj / yp_bind.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.1 (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
12 * this file.
13 *
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
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1996 Theo de Raadt <deraadt@theos.com>
26 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Theo de Raadt.
40 * 4. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
42 *
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
53 * SUCH DAMAGE.
54 */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char *rcsid = "$OpenBSD: yp_bind.c,v 1.9 1997/04/29 21:25:20 deraadt Exp $";
58 #endif /* LIBC_SCCS and not lint */
59
60 #include <sys/param.h>
61 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <sys/file.h>
64 #include <sys/uio.h>
65 #include <errno.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <rpc/rpc.h>
71 #include <rpc/xdr.h>
72 #include <rpcsvc/yp.h>
73 #include <rpcsvc/ypclnt.h>
74 #include "ypinternal.h"
75
76 struct dom_binding *_ypbindlist = NULL;
77 char _yp_domain[MAXHOSTNAMELEN] = { '\0' };
78
79 int _yplib_timeout = 10;
80
81 int
82 _yp_dobind(dom, ypdb)
83 const char *dom;
84 struct dom_binding **ypdb;
85 {
86 static int pid = -1;
87 char path[MAXPATHLEN];
88 struct dom_binding *ysd, *ysd2;
89 struct ypbind_resp ypbr;
90 struct timeval tv;
91 struct sockaddr_in clnt_sin;
92 struct ypbind_binding *bn;
93 int clnt_sock, fd, gpid;
94 CLIENT *client;
95 int new = 0, r;
96 int count = 0;
97 u_short port;
98
99 /*
100 * test if YP is running or not
101 */
102 if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1)
103 return YPERR_YPBIND;
104 if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
105 (void)close(fd);
106 return YPERR_YPBIND;
107 }
108 (void)close(fd);
109
110 gpid = getpid();
111 if (!(pid == -1 || pid == gpid)) {
112 ysd = _ypbindlist;
113 while (ysd) {
114 if (ysd->dom_client)
115 clnt_destroy(ysd->dom_client);
116 ysd2 = ysd->dom_pnext;
117 free(ysd);
118 ysd = ysd2;
119 }
120 _ypbindlist = NULL;
121 }
122 pid = gpid;
123
124 if (ypdb != NULL)
125 *ypdb = NULL;
126
127 if (dom == NULL || strlen(dom) == 0)
128 return YPERR_BADARGS;
129
130 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
131 if (strcmp(dom, ysd->dom_domain) == 0)
132 break;
133 if (ysd == NULL) {
134 if ((ysd = malloc(sizeof *ysd)) == NULL)
135 return YPERR_YPERR;
136 (void)memset(ysd, 0, sizeof *ysd);
137 ysd->dom_socket = -1;
138 ysd->dom_vers = 0;
139 new = 1;
140 }
141 again:
142 if (ysd->dom_vers == 0) {
143 (void) snprintf(path, sizeof(path), "%s/%s.%d",
144 BINDINGDIR, dom, 2);
145 if ((fd = open(path, O_RDONLY)) == -1) {
146 /*
147 * no binding file, YP is dead, or not yet fully
148 * alive.
149 */
150 goto trynet;
151 }
152 if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
153 errno == EWOULDBLOCK) {
154 struct iovec iov[2];
155 u_short ypb_port;
156
157 /*
158 * we fetch the ypbind port number, but do
159 * nothing with it.
160 */
161 iov[0].iov_base = (caddr_t) &ypb_port;
162 iov[0].iov_len = sizeof ypb_port;
163 iov[1].iov_base = (caddr_t) &ypbr;
164 iov[1].iov_len = sizeof ypbr;
165
166 r = readv(fd, iov, 2);
167 if (r != iov[0].iov_len + iov[1].iov_len) {
168 (void)close(fd);
169 ysd->dom_vers = -1;
170 goto again;
171 }
172 (void)close(fd);
173 goto gotdata;
174 } else {
175 /* no lock on binding file, YP is dead. */
176 (void)close(fd);
177 if (new)
178 free(ysd);
179 return YPERR_YPBIND;
180 }
181 }
182 trynet:
183 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
184 (void)memset(&clnt_sin, 0, sizeof clnt_sin);
185 clnt_sin.sin_len = sizeof(struct sockaddr_in);
186 clnt_sin.sin_family = AF_INET;
187 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
188
189 clnt_sock = RPC_ANYSOCK;
190 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
191 &clnt_sock, 0, 0);
192 if (client == NULL) {
193 clnt_pcreateerror("clnttcp_create");
194 if (new)
195 free(ysd);
196 return YPERR_YPBIND;
197 }
198 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED ||
199 ntohs(clnt_sin.sin_port) == 20) {
200 /*
201 * YP was not running, but someone has registered
202 * ypbind with portmap -- this simply means YP is
203 * not running.
204 */
205 clnt_destroy(client);
206 if (new)
207 free(ysd);
208 return YPERR_YPBIND;
209 }
210 tv.tv_sec = _yplib_timeout;
211 tv.tv_usec = 0;
212 r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_domainname,
213 &dom, xdr_ypbind_resp, &ypbr, tv);
214 if (r != RPC_SUCCESS) {
215 if (new == 0 || count)
216 fprintf(stderr,
217 "YP server for domain %s not responding, still trying\n",
218 dom);
219 count++;
220 clnt_destroy(client);
221 ysd->dom_vers = -1;
222 goto again;
223 }
224 clnt_destroy(client);
225 gotdata:
226 bn = &ypbr.ypbind_resp_u.ypbind_bindinfo;
227 memcpy(&port, &bn->ypbind_binding_port, sizeof port);
228 if (ntohs(port) >= IPPORT_RESERVED ||
229 ntohs(port) == 20) {
230 /*
231 * This is bogus -- the ypbind wants me to
232 * communicate to an insecure ypserv. We are
233 * within rights to syslog this as an attack,
234 * but for now we'll simply ignore it; real YP
235 * is obviously not running.
236 */
237 if (new)
238 free(ysd);
239 return YPERR_YPBIND;
240 }
241 (void)memset(&ysd->dom_server_addr, 0,
242 sizeof ysd->dom_server_addr);
243 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
244 ysd->dom_server_addr.sin_family = AF_INET;
245 memcpy(&ysd->dom_server_addr.sin_port,
246 &bn->ypbind_binding_port,
247 sizeof(ysd->dom_server_addr.sin_port));
248 memcpy(&ysd->dom_server_addr.sin_addr.s_addr,
249 &bn->ypbind_binding_addr,
250 sizeof(ysd->dom_server_addr.sin_addr.s_addr));
251 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
252 ysd->dom_vers = YPVERS;
253 (void)strncpy(ysd->dom_domain, dom, sizeof ysd->dom_domain-1);
254 ysd->dom_domain[sizeof ysd->dom_domain-1] = '\0';
255 }
256 tv.tv_sec = _yplib_timeout / 2;
257 tv.tv_usec = 0;
258 if (ysd->dom_client)
259 clnt_destroy(ysd->dom_client);
260 ysd->dom_socket = RPC_ANYSOCK;
261 ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
262 YPPROG, YPVERS, tv, &ysd->dom_socket);
263 if (ysd->dom_client == NULL) {
264 clnt_pcreateerror("clntudp_create");
265 ysd->dom_vers = -1;
266 goto again;
267 }
268 if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
269 perror("fcntl: F_SETFD");
270
271 if (new) {
272 ysd->dom_pnext = _ypbindlist;
273 _ypbindlist = ysd;
274 }
275 if (ypdb != NULL)
276 *ypdb = ysd;
277 return 0;
278 }
279
280 void
281 _yp_unbind(ypb)
282 struct dom_binding *ypb;
283 {
284 if (ypb->dom_client) clnt_destroy(ypb->dom_client);
285 ypb->dom_client = NULL;
286 ypb->dom_socket = -1;
287 }
288
289 int
290 yp_bind(dom)
291 const char *dom;
292 {
293 return _yp_dobind(dom, NULL);
294 }
295
296 void
297 yp_unbind(dom)
298 const char *dom;
299 {
300 struct dom_binding *ypb, *ypbp;
301
302 ypbp = NULL;
303 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
304 if (strcmp(dom, ypb->dom_domain) == 0) {
305 if (ypb->dom_client) clnt_destroy(ypb->dom_client);
306 if (ypbp)
307 ypbp->dom_pnext = ypb->dom_pnext;
308 else
309 _ypbindlist = ypb->dom_pnext;
310 free(ypb);
311 return;
312 }
313 ypbp = ypb;
314 }
315 }