xref: /openbsd-src/lib/libc/yp/yp_bind.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /*	$OpenBSD: yp_bind.c,v 1.32 2022/08/02 16:59:29 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 int
50 _yp_dobind(const char *dom, struct dom_binding **ypdb)
51 {
52 	struct dom_binding *ypbinding;
53 	struct timeval tv;
54 	int connected = 1;
55 	int s;
56 
57 	if (dom == NULL || strlen(dom) == 0)
58 		return YPERR_BADARGS;
59 
60 	ypbinding = calloc(1, sizeof (*ypbinding));
61 	if (ypbinding == NULL)
62 		return YPERR_RESRC;
63 
64 again:
65 	s = ypconnect(SOCK_DGRAM);
66 	if (s == -1) {
67 		free(ypbinding);
68 		return YPERR_YPBIND;	/* YP not running */
69 	}
70 	ypbinding->dom_socket = s;
71 	ypbinding->dom_server_addr.sin_port = -1; /* don't consult portmap */
72 
73 	tv.tv_sec = _yplib_timeout / 2;
74 	tv.tv_usec = 0;
75 	ypbinding->dom_client = clntudp_create(&ypbinding->dom_server_addr,
76 	    YPPROG, YPVERS, tv, &ypbinding->dom_socket);
77 	if (ypbinding->dom_client == NULL) {
78 		close(ypbinding->dom_socket);
79 		ypbinding->dom_socket = -1;
80 		clnt_pcreateerror("clntudp_create");
81 		goto again;
82 	}
83 	clnt_control(ypbinding->dom_client, CLSET_CONNECTED, &connected);
84 	*ypdb = ypbinding;
85 	return 0;
86 }
87 
88 void
89 _yp_unbind(struct dom_binding *ypb)
90 {
91 	close(ypb->dom_socket);
92 	if (ypb->dom_client)
93 		clnt_destroy(ypb->dom_client);
94 	free(ypb);
95 }
96 
97 /*
98  * Check if YP is running.  But do not leave it active, because we
99  * may not return from libc with a fd active.
100  */
101 int
102 yp_bind(const char *dom)
103 {
104 	struct dom_binding *ysd;
105 	int r;
106 
107 	r = _yp_dobind(dom, &ysd);
108 	if (r == 0)
109 		_yp_unbind(ysd);
110 	return r;
111 }
112 DEF_WEAK(yp_bind);
113 
114 void
115 yp_unbind(const char *dom)
116 {
117 	/* do nothing */
118 }
119