1*903e91b4Sderaadt /* $OpenBSD: yp_bind.c,v 1.33 2024/01/22 16:18:06 deraadt Exp $ */
218128546Sderaadt /*
300466ad4Sderaadt * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
418128546Sderaadt * All rights reserved.
518128546Sderaadt *
618128546Sderaadt * Redistribution and use in source and binary forms, with or without
718128546Sderaadt * modification, are permitted provided that the following conditions
818128546Sderaadt * are met:
918128546Sderaadt * 1. Redistributions of source code must retain the above copyright
1018128546Sderaadt * notice, this list of conditions and the following disclaimer.
1118128546Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
1218128546Sderaadt * notice, this list of conditions and the following disclaimer in the
1318128546Sderaadt * documentation and/or other materials provided with the distribution.
1418128546Sderaadt *
1518128546Sderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1618128546Sderaadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1718128546Sderaadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1818128546Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1918128546Sderaadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2018128546Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2118128546Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2218128546Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2318128546Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2418128546Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2518128546Sderaadt * SUCH DAMAGE.
2618128546Sderaadt */
2718128546Sderaadt
2818128546Sderaadt #include <sys/types.h>
2918128546Sderaadt #include <sys/socket.h>
3018128546Sderaadt #include <sys/uio.h>
3118128546Sderaadt #include <errno.h>
3201a83688Smillert #include <fcntl.h>
3318128546Sderaadt #include <stdio.h>
3418128546Sderaadt #include <stdlib.h>
3518128546Sderaadt #include <string.h>
3618128546Sderaadt #include <unistd.h>
37aea60beeSderaadt #include <limits.h>
385e571521Sderaadt #include <paths.h>
395e571521Sderaadt
4018128546Sderaadt #include <rpc/rpc.h>
4118128546Sderaadt #include <rpc/xdr.h>
4218128546Sderaadt #include <rpcsvc/yp.h>
4318128546Sderaadt #include <rpcsvc/ypclnt.h>
4418128546Sderaadt #include "ypinternal.h"
4518128546Sderaadt
46e7e445a1Sderaadt char _yp_domain[HOST_NAME_MAX+1];
4718128546Sderaadt int _yplib_timeout = 10;
4818128546Sderaadt
49*903e91b4Sderaadt extern CLIENT *
50*903e91b4Sderaadt clntudp_bufcreate_simple(struct sockaddr_in *raddr, u_long program, u_long version,
51*903e91b4Sderaadt struct timeval wait, int *sockp, u_int sendsz, u_int recvsz);
52*903e91b4Sderaadt
5318128546Sderaadt int
_yp_dobind(const char * dom,struct dom_binding ** ypdb)5411e5d692Sderaadt _yp_dobind(const char *dom, struct dom_binding **ypdb)
5518128546Sderaadt {
562d62dde8Sderaadt struct dom_binding *ypbinding;
5718128546Sderaadt struct timeval tv;
58125e6122Sderaadt int connected = 1;
59125e6122Sderaadt int s;
6018128546Sderaadt
6118128546Sderaadt if (dom == NULL || strlen(dom) == 0)
6218128546Sderaadt return YPERR_BADARGS;
6318128546Sderaadt
642d62dde8Sderaadt ypbinding = calloc(1, sizeof (*ypbinding));
652d62dde8Sderaadt if (ypbinding == NULL)
662f7e579fSschwarze return YPERR_RESRC;
672d62dde8Sderaadt
682d62dde8Sderaadt again:
692d62dde8Sderaadt s = ypconnect(SOCK_DGRAM);
702d62dde8Sderaadt if (s == -1) {
712d62dde8Sderaadt free(ypbinding);
722d62dde8Sderaadt return YPERR_YPBIND; /* YP not running */
732f7e579fSschwarze }
74125e6122Sderaadt ypbinding->dom_socket = s;
75125e6122Sderaadt ypbinding->dom_server_addr.sin_port = -1; /* don't consult portmap */
76125e6122Sderaadt
77451511c7Sderaadt tv.tv_sec = _yplib_timeout / 2;
7818128546Sderaadt tv.tv_usec = 0;
79*903e91b4Sderaadt ypbinding->dom_client = clntudp_bufcreate_simple(&ypbinding->dom_server_addr,
80*903e91b4Sderaadt YPPROG, YPVERS, tv, &ypbinding->dom_socket, UDPMSGSIZE, UDPMSGSIZE);
81125e6122Sderaadt if (ypbinding->dom_client == NULL) {
82125e6122Sderaadt close(ypbinding->dom_socket);
832d62dde8Sderaadt ypbinding->dom_socket = -1;
8418128546Sderaadt clnt_pcreateerror("clntudp_create");
8518128546Sderaadt goto again;
8618128546Sderaadt }
87125e6122Sderaadt clnt_control(ypbinding->dom_client, CLSET_CONNECTED, &connected);
88125e6122Sderaadt *ypdb = ypbinding;
8918128546Sderaadt return 0;
9018128546Sderaadt }
9118128546Sderaadt
9218128546Sderaadt void
_yp_unbind(struct dom_binding * ypb)9311e5d692Sderaadt _yp_unbind(struct dom_binding *ypb)
9418128546Sderaadt {
95125e6122Sderaadt close(ypb->dom_socket);
962d62dde8Sderaadt if (ypb->dom_client)
9718128546Sderaadt clnt_destroy(ypb->dom_client);
982d62dde8Sderaadt free(ypb);
9918128546Sderaadt }
10018128546Sderaadt
1012d62dde8Sderaadt /*
1022d62dde8Sderaadt * Check if YP is running. But do not leave it active, because we
1032d62dde8Sderaadt * may not return from libc with a fd active.
1042d62dde8Sderaadt */
10518128546Sderaadt int
yp_bind(const char * dom)10611e5d692Sderaadt yp_bind(const char *dom)
10718128546Sderaadt {
1082d62dde8Sderaadt struct dom_binding *ysd;
1092d62dde8Sderaadt int r;
1102d62dde8Sderaadt
1112d62dde8Sderaadt r = _yp_dobind(dom, &ysd);
1122d62dde8Sderaadt if (r == 0)
1132d62dde8Sderaadt _yp_unbind(ysd);
1142d62dde8Sderaadt return r;
11518128546Sderaadt }
116ba364befSguenther DEF_WEAK(yp_bind);
11718128546Sderaadt
11818128546Sderaadt void
yp_unbind(const char * dom)11911e5d692Sderaadt yp_unbind(const char *dom)
12018128546Sderaadt {
1212d62dde8Sderaadt /* do nothing */
12218128546Sderaadt }
123