Home | History | Annotate | Line # | Download | only in yp
xdryp.c revision 1.9
      1 /*	$NetBSD: xdryp.c,v 1.9 1995/07/14 21:04:17 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      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 #ifndef LINT
     35 static char *rcsid = "$NetBSD: xdryp.c,v 1.9 1995/07/14 21:04:17 christos Exp $";
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 #include <sys/socket.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <ctype.h>
     45 #include <rpc/rpc.h>
     46 #include <rpc/xdr.h>
     47 #include <rpcsvc/yp_prot.h>
     48 #include <rpcsvc/ypclnt.h>
     49 
     50 extern int (*ypresp_allfn) __P((u_long, char *, int, char *, int, void *));
     51 extern void *ypresp_data;
     52 
     53 bool_t
     54 xdr_domainname(xdrs, objp)
     55 XDR *xdrs;
     56 char *objp;
     57 {
     58 	return xdr_string(xdrs, &objp, YPMAXDOMAIN);
     59 }
     60 
     61 bool_t
     62 xdr_peername(xdrs, objp)
     63 XDR *xdrs;
     64 char *objp;
     65 {
     66 	return xdr_string(xdrs, &objp, YPMAXPEER);
     67 }
     68 
     69 bool_t
     70 xdr_datum(xdrs, objp)
     71 XDR *xdrs;
     72 datum *objp;
     73 {
     74 	return xdr_bytes(xdrs, (char **)&objp->dptr,
     75 			 (u_int *)&objp->dsize, YPMAXRECORD);
     76 }
     77 
     78 bool_t
     79 xdr_mapname(xdrs, objp)
     80 XDR *xdrs;
     81 char *objp;
     82 {
     83 	return xdr_string(xdrs, &objp, YPMAXMAP);
     84 }
     85 
     86 bool_t
     87 xdr_ypreq_key(xdrs, objp)
     88 XDR *xdrs;
     89 struct ypreq_key *objp;
     90 {
     91 	if (!xdr_domainname(xdrs, (char *)objp->domain)) {
     92 		return FALSE;
     93 	}
     94 	if (!xdr_mapname(xdrs, (char *)objp->map)) {
     95 		return FALSE;
     96 	}
     97 	return xdr_datum(xdrs, &objp->keydat);
     98 }
     99 
    100 bool_t
    101 xdr_ypreq_nokey(xdrs, objp)
    102 XDR *xdrs;
    103 struct ypreq_nokey *objp;
    104 {
    105 	if (!xdr_domainname(xdrs, (char *)objp->domain)) {
    106 		return FALSE;
    107 	}
    108 	return xdr_mapname(xdrs, (char *)objp->map);
    109 }
    110 
    111 bool_t
    112 xdr_yp_inaddr(xdrs, objp)
    113 XDR *xdrs;
    114 struct in_addr *objp;
    115 {
    116 	return xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr);
    117 }
    118 
    119 bool_t
    120 xdr_ypbind_binding(xdrs, objp)
    121 XDR *xdrs;
    122 struct ypbind_binding *objp;
    123 {
    124 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr)) {
    125 		return FALSE;
    126 	}
    127 	return xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
    128 	    sizeof objp->ypbind_binding_port);
    129 }
    130 
    131 bool_t
    132 xdr_ypbind_resptype(xdrs, objp)
    133 XDR *xdrs;
    134 enum ypbind_resptype *objp;
    135 {
    136 	return xdr_enum(xdrs, (enum_t *)objp);
    137 }
    138 
    139 bool_t
    140 xdr_ypstat(xdrs, objp)
    141 XDR *xdrs;
    142 enum ypbind_resptype *objp;
    143 {
    144 	return xdr_enum(xdrs, (enum_t *)objp);
    145 }
    146 
    147 bool_t
    148 xdr_ypbind_resp(xdrs, objp)
    149 XDR *xdrs;
    150 struct ypbind_resp *objp;
    151 {
    152 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status)) {
    153 		return FALSE;
    154 	}
    155 
    156 	switch (objp->ypbind_status) {
    157 	case YPBIND_FAIL_VAL:
    158 		return xdr_u_int(xdrs,
    159 				 (u_int *)&objp->ypbind_respbody.ypbind_error);
    160 	case YPBIND_SUCC_VAL:
    161 		return xdr_ypbind_binding(xdrs,
    162 				 &objp->ypbind_respbody.ypbind_bindinfo);
    163 	default:
    164 		return FALSE;
    165 	}
    166 	/* NOTREACHED */
    167 }
    168 
    169 bool_t
    170 xdr_ypresp_val(xdrs, objp)
    171 XDR *xdrs;
    172 struct ypresp_val *objp;
    173 {
    174 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) {
    175 		return FALSE;
    176 	}
    177 	return xdr_datum(xdrs, &objp->valdat);
    178 }
    179 
    180 bool_t
    181 xdr_ypbind_setdom(xdrs, objp)
    182 XDR *xdrs;
    183 struct ypbind_setdom *objp;
    184 {
    185 	if (!xdr_domainname(xdrs, objp->ypsetdom_domain)) {
    186 		return FALSE;
    187 	}
    188 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding)) {
    189 		return FALSE;
    190 	}
    191 	return xdr_u_short(xdrs, &objp->ypsetdom_vers);
    192 }
    193 
    194 bool_t
    195 xdr_ypresp_key_val(xdrs, objp)
    196 XDR *xdrs;
    197 struct ypresp_key_val *objp;
    198 {
    199 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) {
    200 		return FALSE;
    201 	}
    202 	if (!xdr_datum(xdrs, &objp->valdat)) {
    203 		return FALSE;
    204 	}
    205 	return xdr_datum(xdrs, &objp->keydat);
    206 }
    207 
    208 bool_t
    209 xdr_ypresp_all(xdrs, objp)
    210 XDR *xdrs;
    211 struct ypresp_all *objp;
    212 {
    213 	if (!xdr_bool(xdrs, &objp->more)) {
    214 		return FALSE;
    215 	}
    216 	switch (objp->more) {
    217 	case TRUE:
    218 		return xdr_ypresp_key_val(xdrs, &objp->ypresp_all_u.val);
    219 	case FALSE:
    220 		return (TRUE);
    221 	default:
    222 		return FALSE;
    223 	}
    224 	/* NOTREACHED */
    225 }
    226 
    227 bool_t
    228 xdr_ypresp_all_seq(xdrs, objp)
    229 XDR *xdrs;
    230 u_long *objp;
    231 {
    232 	struct ypresp_all out;
    233 	u_long status;
    234 	char *key, *val;
    235 	int size;
    236 	int r;
    237 
    238 	memset(&out, 0, sizeof out);
    239 	while(1) {
    240 		if( !xdr_ypresp_all(xdrs, &out)) {
    241 			xdr_free(xdr_ypresp_all, (char *)&out);
    242 			*objp = YP_YPERR;
    243 			return FALSE;
    244 		}
    245 		if(out.more == 0) {
    246 			xdr_free(xdr_ypresp_all, (char *)&out);
    247 			return FALSE;
    248 		}
    249 		status = out.ypresp_all_u.val.status;
    250 		switch(status) {
    251 		case YP_TRUE:
    252 			size = out.ypresp_all_u.val.keydat.dsize;
    253 			if ((key = malloc(size + 1)) != NULL) {
    254 				(void)memcpy(key,
    255 					     out.ypresp_all_u.val.keydat.dptr,
    256 					     size);
    257 				key[size] = '\0';
    258 			}
    259 			size = out.ypresp_all_u.val.valdat.dsize;
    260 			if ((val = malloc(size + 1)) != NULL) {
    261 				(void)memcpy(val,
    262 					     out.ypresp_all_u.val.valdat.dptr,
    263 					     size);
    264 				val[size] = '\0';
    265 			}
    266 			else {
    267 				free(key);
    268 				key = NULL;
    269 			}
    270 			xdr_free(xdr_ypresp_all, (char *)&out);
    271 
    272 			if (key == NULL || val == NULL)
    273 				return FALSE;
    274 
    275 			r = (*ypresp_allfn)(status,
    276 				key, out.ypresp_all_u.val.keydat.dsize,
    277 				val, out.ypresp_all_u.val.valdat.dsize,
    278 				ypresp_data);
    279 			*objp = status;
    280 			free(key);
    281 			free(val);
    282 			if(r)
    283 				return TRUE;
    284 			break;
    285 		case YP_NOMORE:
    286 			xdr_free(xdr_ypresp_all, (char *)&out);
    287 			return TRUE;
    288 		default:
    289 			xdr_free(xdr_ypresp_all, (char *)&out);
    290 			*objp = status;
    291 			return TRUE;
    292 		}
    293 	}
    294 }
    295 
    296 bool_t
    297 xdr_ypresp_master(xdrs, objp)
    298 XDR *xdrs;
    299 struct ypresp_master *objp;
    300 {
    301 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) {
    302 		return FALSE;
    303 	}
    304 	return xdr_string(xdrs, &objp->master, YPMAXPEER);
    305 }
    306 
    307 bool_t
    308 xdr_ypmaplist_str(xdrs, objp)
    309 XDR *xdrs;
    310 char *objp;
    311 {
    312 	return xdr_string(xdrs, &objp, YPMAXMAP+1);
    313 }
    314 
    315 bool_t
    316 xdr_ypmaplist(xdrs, objp)
    317 XDR *xdrs;
    318 struct ypmaplist *objp;
    319 {
    320 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name)) {
    321 		return FALSE;
    322 	}
    323 	return xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
    324 	    sizeof(struct ypmaplist), xdr_ypmaplist);
    325 }
    326 
    327 bool_t
    328 xdr_ypresp_maplist(xdrs, objp)
    329 XDR *xdrs;
    330 struct ypresp_maplist *objp;
    331 {
    332 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) {
    333 		return FALSE;
    334 	}
    335 	return xdr_pointer(xdrs, (caddr_t *)&objp->list,
    336 	    sizeof(struct ypmaplist), xdr_ypmaplist);
    337 }
    338 
    339 bool_t
    340 xdr_ypresp_order(xdrs, objp)
    341 XDR *xdrs;
    342 struct ypresp_order *objp;
    343 {
    344 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) {
    345 		return FALSE;
    346 	}
    347 	return xdr_u_long(xdrs, &objp->ordernum);
    348 }
    349