Home | History | Annotate | Line # | Download | only in common
yplib_host.c revision 1.5
      1  1.5      wiz /*	$NetBSD: yplib_host.c,v 1.5 2002/07/06 21:39:25 wiz Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) theos.com>
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.1  thorpej  *    must display the following acknowledgement:
     17  1.1  thorpej  *	This product includes software developed by Theo de Raadt.
     18  1.1  thorpej  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  thorpej  *    derived from this software without specific prior written permission.
     20  1.1  thorpej  *
     21  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  1.1  thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  1.1  thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  1.1  thorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  thorpej  * SUCH DAMAGE.
     32  1.1  thorpej  */
     33  1.3    lukem 
     34  1.3    lukem #include <sys/cdefs.h>
     35  1.3    lukem #ifndef lint
     36  1.5      wiz __RCSID("$NetBSD: yplib_host.c,v 1.5 2002/07/06 21:39:25 wiz Exp $");
     37  1.3    lukem #endif
     38  1.1  thorpej 
     39  1.1  thorpej #include <sys/param.h>
     40  1.1  thorpej #include <sys/types.h>
     41  1.1  thorpej #include <sys/socket.h>
     42  1.1  thorpej #include <sys/file.h>
     43  1.1  thorpej #include <sys/uio.h>
     44  1.1  thorpej 
     45  1.1  thorpej #include <netinet/in.h>
     46  1.1  thorpej #include <arpa/inet.h>
     47  1.1  thorpej 
     48  1.2  thorpej #include <ctype.h>
     49  1.2  thorpej #include <err.h>
     50  1.1  thorpej #include <errno.h>
     51  1.1  thorpej #include <stdio.h>
     52  1.1  thorpej #include <stdlib.h>
     53  1.1  thorpej #include <string.h>
     54  1.1  thorpej #include <netdb.h>
     55  1.1  thorpej #include <unistd.h>
     56  1.1  thorpej 
     57  1.1  thorpej #include <rpc/rpc.h>
     58  1.1  thorpej #include <rpc/xdr.h>
     59  1.1  thorpej #include <rpcsvc/yp_prot.h>
     60  1.1  thorpej #include <rpcsvc/ypclnt.h>
     61  1.2  thorpej 
     62  1.2  thorpej #include "yplib_host.h"
     63  1.1  thorpej 
     64  1.1  thorpej struct timeval _yplib_host_timeout = { 10, 0 };
     65  1.1  thorpej 
     66  1.1  thorpej CLIENT *
     67  1.5      wiz yp_bind_host(char *server, u_int program, u_int version, u_short port,
     68  1.5      wiz 	     int usetcp)
     69  1.1  thorpej {
     70  1.1  thorpej 	struct sockaddr_in rsrv_sin;
     71  1.1  thorpej 	int rsrv_sock;
     72  1.1  thorpej 	struct hostent *h;
     73  1.1  thorpej 	static CLIENT *client;
     74  1.1  thorpej 
     75  1.1  thorpej 	memset(&rsrv_sin, 0, sizeof rsrv_sin);
     76  1.1  thorpej 	rsrv_sin.sin_len = sizeof rsrv_sin;
     77  1.1  thorpej 	rsrv_sin.sin_family = AF_INET;
     78  1.1  thorpej 	rsrv_sock = RPC_ANYSOCK;
     79  1.1  thorpej 	if (port != 0) {
     80  1.1  thorpej 		rsrv_sin.sin_port = htons(port);
     81  1.1  thorpej 	}
     82  1.1  thorpej 
     83  1.1  thorpej 	if (isdigit(*server)) {
     84  1.1  thorpej 		if (inet_aton(server,&rsrv_sin.sin_addr) == 0) {
     85  1.1  thorpej 			errx(1, "invalid IP address `%s'", server);
     86  1.1  thorpej 		}
     87  1.1  thorpej 	} else {
     88  1.1  thorpej 		h = gethostbyname(server);
     89  1.1  thorpej 		if(h == NULL) {
     90  1.1  thorpej 			errx(1, "unknown host `%s'", server);
     91  1.1  thorpej 		}
     92  1.1  thorpej 		memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr_list[0],
     93  1.1  thorpej 		    h->h_length);
     94  1.1  thorpej 	}
     95  1.1  thorpej 
     96  1.1  thorpej 	if (usetcp)
     97  1.1  thorpej 		client = clnttcp_create(&rsrv_sin, program, version,
     98  1.1  thorpej 		    &rsrv_sock, 0, 0);
     99  1.1  thorpej 	else
    100  1.1  thorpej 		client = clntudp_create(&rsrv_sin, program, version,
    101  1.1  thorpej 		    _yplib_host_timeout, &rsrv_sock);
    102  1.1  thorpej 
    103  1.1  thorpej 	if (client == NULL)
    104  1.1  thorpej 		errx(1, "%s: no contact with host `%s'",
    105  1.1  thorpej 		    usetcp ? "clnttcp_create" : "clntudp_create", server);
    106  1.1  thorpej 
    107  1.1  thorpej 	return(client);
    108  1.1  thorpej }
    109  1.1  thorpej 
    110  1.1  thorpej CLIENT *
    111  1.5      wiz yp_bind_local(u_int program, u_int version)
    112  1.1  thorpej {
    113  1.1  thorpej 	struct sockaddr_in rsrv_sin;
    114  1.1  thorpej 	int rsrv_sock;
    115  1.1  thorpej 	static CLIENT *client;
    116  1.1  thorpej 
    117  1.1  thorpej 	memset(&rsrv_sin, 0, sizeof rsrv_sin);
    118  1.1  thorpej 	rsrv_sin.sin_len = sizeof rsrv_sin;
    119  1.1  thorpej 	rsrv_sin.sin_family = AF_INET;
    120  1.1  thorpej 	rsrv_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    121  1.1  thorpej 	rsrv_sock = RPC_ANYSOCK;
    122  1.1  thorpej 
    123  1.1  thorpej 	client = clntudp_create(&rsrv_sin, program, version,
    124  1.1  thorpej 	    _yplib_host_timeout, &rsrv_sock);
    125  1.1  thorpej 	if (client == NULL)
    126  1.1  thorpej 		errx(1, "clntudp_create: no contact with localhost");
    127  1.1  thorpej 
    128  1.1  thorpej 	return(client);
    129  1.1  thorpej }
    130  1.1  thorpej 
    131  1.1  thorpej int
    132  1.5      wiz yp_match_host(CLIENT *client, char *indomain, char *inmap, const char *inkey,
    133  1.5      wiz 	      int inkeylen, char **outval, int *outvallen)
    134  1.1  thorpej {
    135  1.1  thorpej 	struct ypresp_val yprv;
    136  1.1  thorpej 	struct ypreq_key yprk;
    137  1.1  thorpej 	int r;
    138  1.1  thorpej 
    139  1.1  thorpej 	*outval = NULL;
    140  1.1  thorpej 	*outvallen = 0;
    141  1.1  thorpej 
    142  1.1  thorpej 	yprk.domain = indomain;
    143  1.1  thorpej 	yprk.map = inmap;
    144  1.1  thorpej 	yprk.keydat.dptr = (char *)inkey;
    145  1.1  thorpej 	yprk.keydat.dsize = inkeylen;
    146  1.1  thorpej 
    147  1.1  thorpej 	memset(&yprv, 0, sizeof yprv);
    148  1.1  thorpej 
    149  1.1  thorpej 	r = clnt_call(client, YPPROC_MATCH, xdr_ypreq_key, &yprk,
    150  1.1  thorpej 	    xdr_ypresp_val, &yprv, _yplib_host_timeout);
    151  1.1  thorpej 	if(r != RPC_SUCCESS)
    152  1.1  thorpej 		clnt_perror(client, "yp_match_host: clnt_call");
    153  1.1  thorpej 
    154  1.1  thorpej 	if(!(r=ypprot_err(yprv.status)) ) {
    155  1.1  thorpej 		*outvallen = yprv.valdat.dsize;
    156  1.1  thorpej 		*outval = (char *)malloc(*outvallen+1);
    157  1.1  thorpej 		memcpy(*outval, yprv.valdat.dptr, *outvallen);
    158  1.1  thorpej 		(*outval)[*outvallen] = '\0';
    159  1.1  thorpej 	}
    160  1.1  thorpej 	xdr_free(xdr_ypresp_val, (char *)&yprv);
    161  1.1  thorpej 	return r;
    162  1.1  thorpej }
    163  1.1  thorpej 
    164  1.1  thorpej int
    165  1.5      wiz yp_first_host(CLIENT *client, char *indomain, char *inmap, char **outkey,
    166  1.5      wiz 	      int *outkeylen, char **outval, int *outvallen)
    167  1.1  thorpej {
    168  1.1  thorpej 	struct ypresp_key_val yprkv;
    169  1.1  thorpej 	struct ypreq_nokey yprnk;
    170  1.1  thorpej 	int r;
    171  1.1  thorpej 
    172  1.1  thorpej 	*outkey = *outval = NULL;
    173  1.1  thorpej 	*outkeylen = *outvallen = 0;
    174  1.1  thorpej 
    175  1.1  thorpej 	yprnk.domain = indomain;
    176  1.1  thorpej 	yprnk.map = inmap;
    177  1.1  thorpej 	memset(&yprkv, 0, sizeof yprkv);
    178  1.1  thorpej 
    179  1.1  thorpej 	r = clnt_call(client, YPPROC_FIRST, xdr_ypreq_nokey, &yprnk,
    180  1.1  thorpej 	    xdr_ypresp_key_val, &yprkv, _yplib_host_timeout);
    181  1.1  thorpej 	if (r != RPC_SUCCESS)
    182  1.1  thorpej 		clnt_perror(client, "yp_first_host: clnt_call");
    183  1.1  thorpej 
    184  1.1  thorpej 	if(!(r=ypprot_err(yprkv.status)) ) {
    185  1.1  thorpej 		*outkeylen = yprkv.keydat.dsize;
    186  1.1  thorpej 		*outkey = (char *)malloc(*outkeylen+1);
    187  1.1  thorpej 		memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
    188  1.1  thorpej 		(*outkey)[*outkeylen] = '\0';
    189  1.1  thorpej 		*outvallen = yprkv.valdat.dsize;
    190  1.1  thorpej 		*outval = (char *)malloc(*outvallen+1);
    191  1.1  thorpej 		memcpy(*outval, yprkv.valdat.dptr, *outvallen);
    192  1.1  thorpej 		(*outval)[*outvallen] = '\0';
    193  1.1  thorpej 	}
    194  1.1  thorpej 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
    195  1.1  thorpej 	return r;
    196  1.1  thorpej }
    197  1.1  thorpej 
    198  1.1  thorpej int
    199  1.5      wiz yp_next_host(CLIENT *client, char *indomain, char *inmap, char *inkey,
    200  1.5      wiz 	     int inkeylen, char **outkey, int *outkeylen, char **outval,
    201  1.5      wiz 	     int *outvallen)
    202  1.1  thorpej {
    203  1.1  thorpej 	struct ypresp_key_val yprkv;
    204  1.1  thorpej 	struct ypreq_key yprk;
    205  1.1  thorpej 	int r;
    206  1.1  thorpej 
    207  1.1  thorpej 	*outkey = *outval = NULL;
    208  1.1  thorpej 	*outkeylen = *outvallen = 0;
    209  1.1  thorpej 
    210  1.1  thorpej 	yprk.domain = indomain;
    211  1.1  thorpej 	yprk.map = inmap;
    212  1.1  thorpej 	yprk.keydat.dptr = inkey;
    213  1.1  thorpej 	yprk.keydat.dsize = inkeylen;
    214  1.1  thorpej 	memset(&yprkv, 0, sizeof yprkv);
    215  1.1  thorpej 
    216  1.1  thorpej 	r = clnt_call(client, YPPROC_NEXT, xdr_ypreq_key, &yprk,
    217  1.1  thorpej 	    xdr_ypresp_key_val, &yprkv, _yplib_host_timeout);
    218  1.1  thorpej 	if (r != RPC_SUCCESS)
    219  1.1  thorpej 		clnt_perror(client, "yp_next_host: clnt_call");
    220  1.1  thorpej 
    221  1.1  thorpej 	if(!(r=ypprot_err(yprkv.status)) ) {
    222  1.1  thorpej 		*outkeylen = yprkv.keydat.dsize;
    223  1.1  thorpej 		*outkey = (char *)malloc(*outkeylen+1);
    224  1.1  thorpej 		memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
    225  1.1  thorpej 		(*outkey)[*outkeylen] = '\0';
    226  1.1  thorpej 		*outvallen = yprkv.valdat.dsize;
    227  1.1  thorpej 		*outval = (char *)malloc(*outvallen+1);
    228  1.1  thorpej 		memcpy(*outval, yprkv.valdat.dptr, *outvallen);
    229  1.1  thorpej 		(*outval)[*outvallen] = '\0';
    230  1.1  thorpej 	}
    231  1.1  thorpej 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
    232  1.1  thorpej 	return r;
    233  1.1  thorpej }
    234  1.1  thorpej 
    235  1.1  thorpej int
    236  1.5      wiz yp_all_host(CLIENT *client, char *indomain, char *inmap,
    237  1.5      wiz 	    struct ypall_callback *incallback)
    238  1.1  thorpej {
    239  1.1  thorpej 	struct ypreq_nokey yprnk;
    240  1.1  thorpej 	int status;
    241  1.1  thorpej 
    242  1.1  thorpej 	yprnk.domain = indomain;
    243  1.1  thorpej 	yprnk.map = inmap;
    244  1.1  thorpej 
    245  1.1  thorpej 	status = clnt_call(client, YPPROC_ALL, xdr_ypreq_nokey, &yprnk,
    246  1.1  thorpej 	    xdr_ypall, (char *)incallback, _yplib_host_timeout);
    247  1.1  thorpej 
    248  1.1  thorpej 	if (status != RPC_SUCCESS)
    249  1.1  thorpej 		return YPERR_RPC;
    250  1.1  thorpej 
    251  1.1  thorpej 	return 0;
    252  1.1  thorpej }
    253  1.1  thorpej 
    254  1.1  thorpej int
    255  1.5      wiz yp_order_host(CLIENT *client, char *indomain, char *inmap, int *outorder)
    256  1.1  thorpej {
    257  1.1  thorpej 	struct ypresp_order ypro;
    258  1.1  thorpej 	struct ypreq_nokey yprnk;
    259  1.1  thorpej 	int r;
    260  1.1  thorpej 
    261  1.1  thorpej 	yprnk.domain = indomain;
    262  1.1  thorpej 	yprnk.map = inmap;
    263  1.1  thorpej 
    264  1.1  thorpej 	memset(&ypro, 0, sizeof ypro);
    265  1.1  thorpej 
    266  1.1  thorpej 	r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
    267  1.1  thorpej 	    xdr_ypresp_order, &ypro, _yplib_host_timeout);
    268  1.4      mrg 	if (r != RPC_SUCCESS)
    269  1.1  thorpej 		clnt_perror(client, "yp_order_host: clnt_call");
    270  1.1  thorpej 
    271  1.1  thorpej 	*outorder = ypro.ordernum;
    272  1.1  thorpej 	xdr_free(xdr_ypresp_order, (char *)&ypro);
    273  1.1  thorpej 	return ypprot_err(ypro.status);
    274  1.1  thorpej }
    275  1.1  thorpej 
    276  1.1  thorpej int
    277  1.5      wiz yp_master_host(CLIENT *client, char *indomain, char *inmap, char **outname)
    278  1.1  thorpej {
    279  1.1  thorpej 	struct ypresp_master yprm;
    280  1.1  thorpej 	struct ypreq_nokey yprnk;
    281  1.1  thorpej 	int r;
    282  1.1  thorpej 
    283  1.1  thorpej 	yprnk.domain = indomain;
    284  1.1  thorpej 	yprnk.map = inmap;
    285  1.1  thorpej 
    286  1.1  thorpej 	memset(&yprm, 0, sizeof yprm);
    287  1.1  thorpej 
    288  1.1  thorpej 	r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey, &yprnk,
    289  1.1  thorpej 	    xdr_ypresp_master, &yprm, _yplib_host_timeout);
    290  1.1  thorpej 	if (r != RPC_SUCCESS)
    291  1.1  thorpej 		clnt_perror(client, "yp_master: clnt_call");
    292  1.1  thorpej 
    293  1.4      mrg 	if (!(r = ypprot_err(yprm.status))) {
    294  1.1  thorpej 		*outname = (char *)strdup(yprm.master);
    295  1.1  thorpej 	}
    296  1.1  thorpej 	xdr_free(xdr_ypresp_master, (char *)&yprm);
    297  1.1  thorpej 	return r;
    298  1.1  thorpej }
    299  1.1  thorpej 
    300  1.1  thorpej int
    301  1.5      wiz yp_maplist_host(CLIENT *client, char *indomain, struct ypmaplist **outmaplist)
    302  1.1  thorpej {
    303  1.1  thorpej 	struct ypresp_maplist ypml;
    304  1.1  thorpej 	int r;
    305  1.1  thorpej 
    306  1.1  thorpej 	memset(&ypml, 0, sizeof ypml);
    307  1.1  thorpej 
    308  1.1  thorpej 	r = clnt_call(client, YPPROC_MAPLIST, xdr_ypdomain_wrap_string,
    309  1.1  thorpej 	    indomain, xdr_ypresp_maplist, &ypml, _yplib_host_timeout);
    310  1.1  thorpej 	if (r != RPC_SUCCESS)
    311  1.1  thorpej 		clnt_perror(client, "yp_maplist: clnt_call");
    312  1.1  thorpej 
    313  1.1  thorpej 	*outmaplist = ypml.list;
    314  1.1  thorpej 	/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
    315  1.1  thorpej 	return ypprot_err(ypml.status);
    316  1.1  thorpej }
    317