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