]> git.saurik.com Git - apple/libinfo.git/blame - nis.subproj/yp_bind.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / nis.subproj / yp_bind.c
CommitLineData
03fb6eb0 1/*
f475de6c 2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
03fb6eb0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ad21edcc
A
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.
03fb6eb0
A
13 *
14 * The Original Code and all software distributed under the License are
ad21edcc 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
03fb6eb0
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ad21edcc
A
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.
03fb6eb0
A
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)
57static 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
f475de6c
A
60#include "libinfo_common.h"
61
03fb6eb0
A
62#include <sys/param.h>
63#include <sys/types.h>
64#include <sys/socket.h>
65#include <sys/file.h>
66#include <sys/uio.h>
67#include <errno.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <unistd.h>
72#include <rpc/rpc.h>
73#include <rpc/xdr.h>
74#include <rpcsvc/yp.h>
75#include <rpcsvc/ypclnt.h>
b3dd680f
A
76#include <notify.h>
77#include <pthread.h>
03fb6eb0
A
78#include "ypinternal.h"
79
b3dd680f
A
80extern int notify_register_plain(char *, int *);
81
03fb6eb0
A
82struct dom_binding *_ypbindlist = NULL;
83char _yp_domain[MAXHOSTNAMELEN] = { '\0' };
84
85int _yplib_timeout = 10;
86
f475de6c 87LIBINFO_EXPORT
03fb6eb0
A
88int
89_yp_dobind(dom, ypdb)
90 const char *dom;
91 struct dom_binding **ypdb;
92{
93 static int pid = -1;
03fb6eb0
A
94 struct dom_binding *ysd, *ysd2;
95 struct ypbind_resp ypbr;
96 struct timeval tv;
97 struct sockaddr_in clnt_sin;
98 struct ypbind_binding *bn;
99 int clnt_sock, fd, gpid;
100 CLIENT *client;
b3dd680f 101 int new = 0, r, proto;
03fb6eb0
A
102 int count = 0;
103 u_short port;
b3dd680f
A
104 int status, notify_token;
105 uint64_t abort;
106 char *notify_name;
03fb6eb0
A
107
108 /*
109 * test if YP is running or not
110 */
b3dd680f
A
111 if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1) return YPERR_YPBIND;
112
113 if (!((flock(fd, LOCK_EX | LOCK_NB) == -1) && (errno == EWOULDBLOCK)))
114 {
115 close(fd);
03fb6eb0
A
116 return YPERR_YPBIND;
117 }
b3dd680f
A
118
119 close(fd);
03fb6eb0
A
120
121 gpid = getpid();
b3dd680f
A
122 if (!((pid == -1) || (pid == gpid)))
123 {
03fb6eb0 124 ysd = _ypbindlist;
b3dd680f
A
125 while (ysd)
126 {
127 if (ysd->dom_client) clnt_destroy(ysd->dom_client);
03fb6eb0
A
128 ysd2 = ysd->dom_pnext;
129 free(ysd);
130 ysd = ysd2;
131 }
b3dd680f 132
03fb6eb0
A
133 _ypbindlist = NULL;
134 }
b3dd680f 135
03fb6eb0
A
136 pid = gpid;
137
b3dd680f 138 if (ypdb != NULL) *ypdb = NULL;
03fb6eb0 139
b3dd680f 140 if ((dom == NULL) || (strlen(dom) == 0)) return YPERR_BADARGS;
03fb6eb0
A
141
142 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
b3dd680f
A
143 {
144 if (strcmp(dom, ysd->dom_domain) == 0) break;
145 }
146
147 if (ysd == NULL)
148 {
149 ysd = calloc(1, sizeof(struct dom_binding));
150 if (ysd == NULL) return YPERR_YPERR;
03fb6eb0
A
151 ysd->dom_socket = -1;
152 ysd->dom_vers = 0;
153 new = 1;
154 }
03fb6eb0 155
b3dd680f
A
156 /*
157 * Get notification token
158 * we use a self-notification token to allow a caller
159 * to signal the thread doing this bind call to quit.
160 */
161 notify_name = NULL;
162 notify_token = -1;
163
164 asprintf(&notify_name, "self.thread.%lu", (unsigned long)pthread_self());
165 if (notify_name != NULL)
166 {
167 status = notify_register_plain(notify_name, &notify_token);
168 free(notify_name);
169 }
170
171again:
172 if (notify_token != -1)
173 {
174 abort = 0;
175 status = notify_get_state(notify_token, &abort);
176 if (abort == ThreadStateExitRequested)
177 {
178 if (new) free(ysd);
179 notify_cancel(notify_token);
03fb6eb0
A
180 return YPERR_YPBIND;
181 }
182 }
b3dd680f
A
183
184 proto = YP_BIND_UDP;
185 if (ysd->dom_vers == YP_BIND_TCP) proto = YP_BIND_TCP;
186
187 if ((ysd->dom_vers == 0) || (ysd->dom_vers == YP_BIND_UDP) || (ysd->dom_vers == YP_BIND_TCP))
188 {
189 memset(&clnt_sin, 0, sizeof clnt_sin);
03fb6eb0
A
190 clnt_sin.sin_len = sizeof(struct sockaddr_in);
191 clnt_sin.sin_family = AF_INET;
192 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
193
194 clnt_sock = RPC_ANYSOCK;
b3dd680f
A
195 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock, 0, 0);
196 if (client == NULL)
197 {
03fb6eb0 198 clnt_pcreateerror("clnttcp_create");
b3dd680f
A
199 if (new) free(ysd);
200 if (notify_token != -1) notify_cancel(notify_token);
03fb6eb0
A
201 return YPERR_YPBIND;
202 }
b3dd680f
A
203
204 if ((ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) || (ntohs(clnt_sin.sin_port) == 20))
205 {
03fb6eb0
A
206 /*
207 * YP was not running, but someone has registered
208 * ypbind with portmap -- this simply means YP is
209 * not running.
210 */
211 clnt_destroy(client);
b3dd680f
A
212 if (new) free(ysd);
213 if (notify_token != -1) notify_cancel(notify_token);
03fb6eb0
A
214 return YPERR_YPBIND;
215 }
b3dd680f 216
03fb6eb0
A
217 tv.tv_sec = _yplib_timeout;
218 tv.tv_usec = 0;
b3dd680f
A
219
220 r = clnt_call(client, YPBINDPROC_DOMAIN, (xdrproc_t)xdr_domainname, &dom, (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
221 if (r != RPC_SUCCESS)
222 {
223 if (new == 0 || count) fprintf(stderr, "YP server for domain %s not responding, still trying\n", dom);
03fb6eb0
A
224 count++;
225 clnt_destroy(client);
b3dd680f 226 ysd->dom_vers = proto;
03fb6eb0
A
227 goto again;
228 }
b3dd680f 229
03fb6eb0 230 clnt_destroy(client);
b3dd680f 231
03fb6eb0
A
232 bn = &ypbr.ypbind_resp_u.ypbind_bindinfo;
233 memcpy(&port, &bn->ypbind_binding_port, sizeof port);
b3dd680f
A
234 if ((ntohs(port) >= IPPORT_RESERVED) || (ntohs(port) == 20))
235 {
03fb6eb0
A
236 /*
237 * This is bogus -- the ypbind wants me to
238 * communicate to an insecure ypserv. We are
239 * within rights to syslog this as an attack,
240 * but for now we'll simply ignore it; real YP
241 * is obviously not running.
242 */
b3dd680f
A
243 if (new) free(ysd);
244 if (notify_token != -1) notify_cancel(notify_token);
03fb6eb0
A
245 return YPERR_YPBIND;
246 }
b3dd680f
A
247
248 memset(&ysd->dom_server_addr, 0, sizeof ysd->dom_server_addr);
03fb6eb0
A
249 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
250 ysd->dom_server_addr.sin_family = AF_INET;
b3dd680f
A
251 memcpy(&ysd->dom_server_addr.sin_port, &bn->ypbind_binding_port, sizeof(ysd->dom_server_addr.sin_port));
252 memcpy(&ysd->dom_server_addr.sin_addr.s_addr, &bn->ypbind_binding_addr, sizeof(ysd->dom_server_addr.sin_addr.s_addr));
03fb6eb0
A
253 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
254 ysd->dom_vers = YPVERS;
b3dd680f 255 strncpy(ysd->dom_domain, dom, sizeof ysd->dom_domain-1);
03fb6eb0
A
256 ysd->dom_domain[sizeof ysd->dom_domain-1] = '\0';
257 }
b3dd680f 258
03fb6eb0
A
259 tv.tv_sec = _yplib_timeout / 2;
260 tv.tv_usec = 0;
b3dd680f
A
261
262 if (ysd->dom_client) clnt_destroy(ysd->dom_client);
03fb6eb0 263 ysd->dom_socket = RPC_ANYSOCK;
b3dd680f
A
264
265 if (proto == YP_BIND_UDP) ysd->dom_client = clntudp_create(&ysd->dom_server_addr, YPPROG, YPVERS, tv, &ysd->dom_socket);
266 else ysd->dom_client = clnttcp_create(&ysd->dom_server_addr, YPPROG, YPVERS, &ysd->dom_socket, 0, 0);
267
268 if (ysd->dom_client == NULL)
269 {
270 if (proto == YP_BIND_UDP) clnt_pcreateerror("clntudp_create");
271 else clnt_pcreateerror("clnttcp_create");
272 ysd->dom_vers = proto;
03fb6eb0
A
273 goto again;
274 }
b3dd680f
A
275
276 if (notify_token != -1) notify_cancel(notify_token);
277
03fb6eb0
A
278 if (fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
279 perror("fcntl: F_SETFD");
280
b3dd680f
A
281 if (new != 0)
282 {
03fb6eb0
A
283 ysd->dom_pnext = _ypbindlist;
284 _ypbindlist = ysd;
285 }
b3dd680f
A
286
287 if (ypdb != NULL) *ypdb = ysd;
03fb6eb0
A
288 return 0;
289}
290
291void
292_yp_unbind(ypb)
293 struct dom_binding *ypb;
294{
295 if (ypb->dom_client) clnt_destroy(ypb->dom_client);
296 ypb->dom_client = NULL;
297 ypb->dom_socket = -1;
298}
299
f475de6c 300LIBINFO_EXPORT
03fb6eb0
A
301int
302yp_bind(dom)
303 const char *dom;
304{
305 return _yp_dobind(dom, NULL);
306}
307
f475de6c 308LIBINFO_EXPORT
03fb6eb0
A
309void
310yp_unbind(dom)
311 const char *dom;
312{
313 struct dom_binding *ypb, *ypbp;
314
315 ypbp = NULL;
316 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
317 if (strcmp(dom, ypb->dom_domain) == 0) {
318 if (ypb->dom_client) clnt_destroy(ypb->dom_client);
319 if (ypbp)
320 ypbp->dom_pnext = ypb->dom_pnext;
321 else
322 _ypbindlist = ypb->dom_pnext;
323 free(ypb);
324 return;
325 }
326 ypbp = ypb;
327 }
328}