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