xref: /openbsd/lib/libc/yp/yp_first.c (revision 404b540a)
1 /*	$OpenBSD: yp_first.c,v 1.9 2009/06/07 03:33:36 schwarze Exp $ */
2 /*
3  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <rpc/rpc.h>
33 #include <rpc/xdr.h>
34 #include <rpcsvc/yp.h>
35 #include <rpcsvc/ypclnt.h>
36 #include "ypinternal.h"
37 
38 int
39 yp_first(const char *indomain, const char *inmap, char **outkey, int *outkeylen,
40     char **outval, int *outvallen)
41 {
42 	struct ypresp_key_val yprkv;
43 	struct ypreq_nokey yprnk;
44 	struct dom_binding *ysd;
45 	struct timeval  tv;
46 	int tries = 0, r;
47 
48 	if (indomain == NULL || *indomain == '\0' ||
49 	    strlen(indomain) > YPMAXDOMAIN || inmap == NULL ||
50 	    *inmap == '\0' || strlen(inmap) > YPMAXMAP)
51 		return YPERR_BADARGS;
52 
53 	*outkey = *outval = NULL;
54 	*outkeylen = *outvallen = 0;
55 
56 again:
57 	if (_yp_dobind(indomain, &ysd) != 0)
58 		return YPERR_DOMAIN;
59 
60 	tv.tv_sec = _yplib_timeout;
61 	tv.tv_usec = 0;
62 
63 	yprnk.domain = (char *)indomain;
64 	yprnk.map = (char *)inmap;
65 	(void)memset(&yprkv, 0, sizeof yprkv);
66 
67 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
68 	    xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
69 	if (r != RPC_SUCCESS) {
70 		if (tries++)
71 			clnt_perror(ysd->dom_client, "yp_first: clnt_call");
72 		ysd->dom_vers = -1;
73 		goto again;
74 	}
75 	if (!(r = ypprot_err(yprkv.stat))) {
76 		*outkeylen = yprkv.key.keydat_len;
77 		*outvallen = yprkv.val.valdat_len;
78 		if ((*outkey = malloc(*outkeylen + 1)) == NULL ||
79 		    (*outval = malloc(*outvallen + 1)) == NULL) {
80 			free(*outkey);
81 			r = YPERR_RESRC;
82 		} else {
83 			(void)memcpy(*outkey, yprkv.key.keydat_val, *outkeylen);
84 			(*outkey)[*outkeylen] = '\0';
85 			(void)memcpy(*outval, yprkv.val.valdat_val, *outvallen);
86 			(*outval)[*outvallen] = '\0';
87 		}
88 	}
89 	xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
90 	_yp_unbind(ysd);
91 	return r;
92 }
93