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