xref: /openbsd/lib/libc/yp/yp_bind.c (revision 903e91b4)
1 /*	$OpenBSD: yp_bind.c,v 1.33 2024/01/22 16:18:06 deraadt Exp $ */
2 /*
3  * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <paths.h>
39 
40 #include <rpc/rpc.h>
41 #include <rpc/xdr.h>
42 #include <rpcsvc/yp.h>
43 #include <rpcsvc/ypclnt.h>
44 #include "ypinternal.h"
45 
46 char _yp_domain[HOST_NAME_MAX+1];
47 int _yplib_timeout = 10;
48 
49 extern CLIENT *
50 clntudp_bufcreate_simple(struct sockaddr_in *raddr, u_long program, u_long version,
51     struct timeval wait, int *sockp, u_int sendsz, u_int recvsz);
52 
53 int
_yp_dobind(const char * dom,struct dom_binding ** ypdb)54 _yp_dobind(const char *dom, struct dom_binding **ypdb)
55 {
56 	struct dom_binding *ypbinding;
57 	struct timeval tv;
58 	int connected = 1;
59 	int s;
60 
61 	if (dom == NULL || strlen(dom) == 0)
62 		return YPERR_BADARGS;
63 
64 	ypbinding = calloc(1, sizeof (*ypbinding));
65 	if (ypbinding == NULL)
66 		return YPERR_RESRC;
67 
68 again:
69 	s = ypconnect(SOCK_DGRAM);
70 	if (s == -1) {
71 		free(ypbinding);
72 		return YPERR_YPBIND;	/* YP not running */
73 	}
74 	ypbinding->dom_socket = s;
75 	ypbinding->dom_server_addr.sin_port = -1; /* don't consult portmap */
76 
77 	tv.tv_sec = _yplib_timeout / 2;
78 	tv.tv_usec = 0;
79 	ypbinding->dom_client = clntudp_bufcreate_simple(&ypbinding->dom_server_addr,
80 	    YPPROG, YPVERS, tv, &ypbinding->dom_socket, UDPMSGSIZE, UDPMSGSIZE);
81 	if (ypbinding->dom_client == NULL) {
82 		close(ypbinding->dom_socket);
83 		ypbinding->dom_socket = -1;
84 		clnt_pcreateerror("clntudp_create");
85 		goto again;
86 	}
87 	clnt_control(ypbinding->dom_client, CLSET_CONNECTED, &connected);
88 	*ypdb = ypbinding;
89 	return 0;
90 }
91 
92 void
_yp_unbind(struct dom_binding * ypb)93 _yp_unbind(struct dom_binding *ypb)
94 {
95 	close(ypb->dom_socket);
96 	if (ypb->dom_client)
97 		clnt_destroy(ypb->dom_client);
98 	free(ypb);
99 }
100 
101 /*
102  * Check if YP is running.  But do not leave it active, because we
103  * may not return from libc with a fd active.
104  */
105 int
yp_bind(const char * dom)106 yp_bind(const char *dom)
107 {
108 	struct dom_binding *ysd;
109 	int r;
110 
111 	r = _yp_dobind(dom, &ysd);
112 	if (r == 0)
113 		_yp_unbind(ysd);
114 	return r;
115 }
116 DEF_WEAK(yp_bind);
117 
118 void
yp_unbind(const char * dom)119 yp_unbind(const char *dom)
120 {
121 	/* do nothing */
122 }
123