Home | History | Annotate | Line # | Download | only in libhack
yplib.c revision 1.3
      1  1.3  christos /*	$NetBSD: yplib.c,v 1.3 1999/09/19 19:51:11 christos Exp $	*/
      2  1.1       gwr 
      3  1.1       gwr /*
      4  1.1       gwr  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      5  1.1       gwr  * All rights reserved.
      6  1.1       gwr  *
      7  1.1       gwr  * Redistribution and use in source and binary forms, with or without
      8  1.1       gwr  * modification, are permitted provided that the following conditions
      9  1.1       gwr  * are met:
     10  1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     11  1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     12  1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       gwr  *    documentation and/or other materials provided with the distribution.
     15  1.1       gwr  * 3. All advertising materials mentioning features or use of this software
     16  1.1       gwr  *    must display the following acknowledgement:
     17  1.1       gwr  *	This product includes software developed by Theo de Raadt.
     18  1.1       gwr  * 4. The name of the author may not be used to endorse or promote products
     19  1.1       gwr  *    derived from this software without specific prior written permission.
     20  1.1       gwr  *
     21  1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  1.1       gwr  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  1.1       gwr  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  1.1       gwr  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1       gwr  * SUCH DAMAGE.
     32  1.1       gwr  */
     33  1.1       gwr 
     34  1.1       gwr /*
     35  1.1       gwr  * This file provides "stubs" for all the YP library functions.
     36  1.1       gwr  * It is not needed unless you pull in things that call YP, and
     37  1.1       gwr  * if you use all the get* files here then the YP stuff should
     38  1.1       gwr  * not get dragged in.  But if it does, one can use this.
     39  1.1       gwr  *
     40  1.1       gwr  * This was copied from:
     41  1.1       gwr  *      lib/libc/yp/yplib.c
     42  1.1       gwr  * (and then completely gutted! 8^)
     43  1.1       gwr  */
     44  1.3  christos #include <sys/cdefs.h>
     45  1.3  christos 
     46  1.3  christos #ifdef __weak_alias
     47  1.3  christos #define yp_all			_yp_all
     48  1.3  christos #define yp_bind			_yp_bind
     49  1.3  christos #define yp_first		_yp_first
     50  1.3  christos #define yp_get_default_domain	_yp_get_default_domain
     51  1.3  christos #define yp_maplist		_yp_maplist
     52  1.3  christos #define yp_master		_yp_master
     53  1.3  christos #define yp_match		_yp_match
     54  1.3  christos #define yp_next			_yp_next
     55  1.3  christos #define yp_order		_yp_order
     56  1.3  christos #define yp_unbind		_yp_unbind
     57  1.3  christos #define yperr_string		_yperr_string
     58  1.3  christos #define ypprot_err		_ypprot_err
     59  1.3  christos #endif
     60  1.1       gwr 
     61  1.1       gwr #include <sys/types.h>
     62  1.1       gwr 
     63  1.2  sommerfe #include <errno.h>
     64  1.2  sommerfe #include <stdio.h>
     65  1.2  sommerfe #include <stdlib.h>
     66  1.2  sommerfe #include <string.h>
     67  1.2  sommerfe #include <unistd.h>
     68  1.2  sommerfe 
     69  1.2  sommerfe #include <rpc/rpc.h>
     70  1.2  sommerfe #include <rpc/xdr.h>
     71  1.2  sommerfe #include <rpcsvc/yp_prot.h>
     72  1.2  sommerfe #include <rpcsvc/ypclnt.h>
     73  1.1       gwr 
     74  1.3  christos struct dom_binding *_ypbindlist;
     75  1.3  christos char _yp_domain[256];
     76  1.3  christos 
     77  1.3  christos #define YPLIB_TIMEOUT		10
     78  1.3  christos #define YPLIB_RPC_RETRIES	4
     79  1.3  christos 
     80  1.3  christos struct timeval _yplib_timeout = { YPLIB_TIMEOUT, 0 };
     81  1.3  christos struct timeval _yplib_rpc_timeout = { YPLIB_TIMEOUT / YPLIB_RPC_RETRIES,
     82  1.3  christos 	1000000 * (YPLIB_TIMEOUT % YPLIB_RPC_RETRIES) / YPLIB_RPC_RETRIES };
     83  1.3  christos int _yplib_nerrs = 5;
     84  1.3  christos 
     85  1.3  christos 
     86  1.3  christos #ifdef __weak_alias
     87  1.3  christos __weak_alias(yp_all,_yp_all);
     88  1.3  christos __weak_alias(yp_bind, _yp_bind);
     89  1.3  christos __weak_alias(yp_first,_yp_first);
     90  1.3  christos __weak_alias(yp_get_default_domain, _yp_get_default_domain);
     91  1.3  christos __weak_alias(yp_maplist,_yp_maplist);
     92  1.3  christos __weak_alias(yp_master,_yp_master);
     93  1.3  christos __weak_alias(yp_match,_yp_match);
     94  1.3  christos __weak_alias(yp_next,_yp_next);
     95  1.3  christos __weak_alias(yp_order,_yp_order);
     96  1.3  christos __weak_alias(yp_unbind, _yp_unbind);
     97  1.3  christos __weak_alias(yperr_string,_yperr_string);
     98  1.3  christos __weak_alias(ypprot_err,_ypprot_err);
     99  1.3  christos #endif
    100  1.3  christos 
    101  1.3  christos void __yp_unbind __P((struct dom_binding *));
    102  1.3  christos int _yp_invalid_domain __P((const char *));
    103  1.1       gwr 
    104  1.1       gwr int
    105  1.1       gwr _yp_dobind(dom, ypdb)
    106  1.1       gwr 	const char *dom;
    107  1.2  sommerfe 	struct dom_binding **ypdb;
    108  1.1       gwr {
    109  1.1       gwr 	return YPERR_YPBIND;
    110  1.1       gwr }
    111  1.1       gwr 
    112  1.3  christos void
    113  1.3  christos __yp_unbind(ypb)
    114  1.3  christos 	struct dom_binding *ypb;
    115  1.3  christos {
    116  1.3  christos }
    117  1.3  christos 
    118  1.1       gwr int
    119  1.1       gwr yp_bind(dom)
    120  1.1       gwr 	const char     *dom;
    121  1.1       gwr {
    122  1.1       gwr 	return _yp_dobind(dom, NULL);
    123  1.1       gwr }
    124  1.1       gwr 
    125  1.1       gwr void
    126  1.1       gwr yp_unbind(dom)
    127  1.1       gwr 	const char     *dom;
    128  1.1       gwr {
    129  1.1       gwr }
    130  1.1       gwr 
    131  1.1       gwr int
    132  1.3  christos yp_get_default_domain(domp)
    133  1.3  christos 	char **domp;
    134  1.3  christos {
    135  1.3  christos 	*domp = NULL;
    136  1.3  christos 	if (_yp_domain[0] == '\0')
    137  1.3  christos 		if (getdomainname(_yp_domain, sizeof(_yp_domain)))
    138  1.3  christos 			return YPERR_NODOM;
    139  1.3  christos 	*domp = _yp_domain;
    140  1.3  christos 	return 0;
    141  1.3  christos }
    142  1.3  christos 
    143  1.3  christos int
    144  1.3  christos _yp_check(dom)
    145  1.3  christos 	char          **dom;
    146  1.3  christos {
    147  1.3  christos 	char           *unused;
    148  1.3  christos 
    149  1.3  christos 	if (_yp_domain[0] == '\0')
    150  1.3  christos 		if (yp_get_default_domain(&unused))
    151  1.3  christos 			return 0;
    152  1.3  christos 
    153  1.3  christos 	if (dom)
    154  1.3  christos 		*dom = _yp_domain;
    155  1.3  christos 
    156  1.3  christos 	if (yp_bind(_yp_domain) == 0)
    157  1.3  christos 		return 1;
    158  1.3  christos 	return 0;
    159  1.3  christos }
    160  1.3  christos 
    161  1.3  christos int
    162  1.3  christos _yp_invalid_domain(dom)
    163  1.3  christos 	const char *dom;
    164  1.3  christos {
    165  1.3  christos 	if (dom == NULL || *dom == '\0')
    166  1.3  christos 		return 1;
    167  1.3  christos 
    168  1.3  christos 	if (strlen(dom) > YPMAXDOMAIN)
    169  1.3  christos 		return 1;
    170  1.3  christos 
    171  1.3  christos 	if (strchr(dom, '/') != NULL)
    172  1.3  christos 		return 1;
    173  1.3  christos 
    174  1.3  christos 	return 0;
    175  1.3  christos }
    176  1.3  christos 
    177  1.3  christos int
    178  1.1       gwr yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
    179  1.1       gwr 	const char     *indomain;
    180  1.1       gwr 	const char     *inmap;
    181  1.1       gwr 	const char     *inkey;
    182  1.1       gwr 	int             inkeylen;
    183  1.1       gwr 	char          **outval;
    184  1.1       gwr 	int            *outvallen;
    185  1.1       gwr {
    186  1.1       gwr 	*outval = NULL;
    187  1.1       gwr 	*outvallen = 0;
    188  1.1       gwr 
    189  1.1       gwr 	return YPERR_DOMAIN;
    190  1.1       gwr }
    191  1.1       gwr 
    192  1.1       gwr int
    193  1.1       gwr yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
    194  1.1       gwr 	const char     *indomain;
    195  1.1       gwr 	const char     *inmap;
    196  1.1       gwr 	char          **outkey;
    197  1.1       gwr 	int            *outkeylen;
    198  1.1       gwr 	char          **outval;
    199  1.1       gwr 	int            *outvallen;
    200  1.1       gwr {
    201  1.1       gwr 
    202  1.1       gwr 	*outkey = *outval = NULL;
    203  1.1       gwr 	*outkeylen = *outvallen = 0;
    204  1.1       gwr 
    205  1.1       gwr 	return YPERR_DOMAIN;
    206  1.1       gwr }
    207  1.1       gwr 
    208  1.1       gwr int
    209  1.1       gwr yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
    210  1.1       gwr 	const char     *indomain;
    211  1.1       gwr 	const char     *inmap;
    212  1.1       gwr 	const char     *inkey;
    213  1.1       gwr 	int             inkeylen;
    214  1.1       gwr 	char          **outkey;
    215  1.1       gwr 	int            *outkeylen;
    216  1.1       gwr 	char          **outval;
    217  1.1       gwr 	int            *outvallen;
    218  1.1       gwr {
    219  1.1       gwr 	*outkey = *outval = NULL;
    220  1.1       gwr 	*outkeylen = *outvallen = 0;
    221  1.1       gwr 
    222  1.1       gwr 	return YPERR_DOMAIN;
    223  1.1       gwr }
    224  1.1       gwr 
    225  1.1       gwr int
    226  1.1       gwr yp_all(indomain, inmap, incallback)
    227  1.1       gwr 	const char     *indomain;
    228  1.1       gwr 	const char     *inmap;
    229  1.2  sommerfe 	struct ypall_callback *incallback;
    230  1.1       gwr {
    231  1.1       gwr 	return YPERR_DOMAIN;
    232  1.1       gwr }
    233  1.1       gwr 
    234  1.1       gwr int
    235  1.1       gwr yp_order(indomain, inmap, outorder)
    236  1.1       gwr 	const char     *indomain;
    237  1.1       gwr 	const char     *inmap;
    238  1.1       gwr 	int            *outorder;
    239  1.1       gwr {
    240  1.1       gwr 	return YPERR_DOMAIN;
    241  1.1       gwr }
    242  1.1       gwr 
    243  1.1       gwr int
    244  1.1       gwr yp_master(indomain, inmap, outname)
    245  1.1       gwr 	const char     *indomain;
    246  1.1       gwr 	const char     *inmap;
    247  1.1       gwr 	char          **outname;
    248  1.1       gwr {
    249  1.1       gwr 	return YPERR_DOMAIN;
    250  1.1       gwr }
    251  1.1       gwr 
    252  1.1       gwr int
    253  1.1       gwr yp_maplist(indomain, outmaplist)
    254  1.1       gwr 	const char     *indomain;
    255  1.1       gwr 	struct ypmaplist **outmaplist;
    256  1.1       gwr {
    257  1.1       gwr 	return YPERR_DOMAIN;
    258  1.1       gwr }
    259  1.1       gwr 
    260  1.1       gwr char *
    261  1.1       gwr yperr_string(incode)
    262  1.1       gwr 	int             incode;
    263  1.1       gwr {
    264  1.1       gwr 	static char     err[80];
    265  1.1       gwr 
    266  1.1       gwr 	if (incode == 0)
    267  1.1       gwr 		return "Success";
    268  1.1       gwr 
    269  1.1       gwr 	sprintf(err, "YP FAKE error %d\n", incode);
    270  1.1       gwr 	return err;
    271  1.1       gwr }
    272  1.1       gwr 
    273  1.1       gwr int
    274  1.1       gwr ypprot_err(incode)
    275  1.1       gwr 	unsigned int    incode;
    276  1.1       gwr {
    277  1.1       gwr 	switch (incode) {
    278  1.1       gwr 	case YP_TRUE:	/* success */
    279  1.1       gwr 		return 0;
    280  1.1       gwr 	case YP_FALSE:	/* failure */
    281  1.1       gwr 		return YPERR_YPBIND;
    282  1.1       gwr 	}
    283  1.1       gwr 	return YPERR_YPERR;
    284  1.1       gwr }
    285  1.1       gwr 
    286