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