1*2d62dde8Sderaadt /* $OpenBSD: yp_first.c,v 1.12 2022/08/02 16:59:29 deraadt Exp $ */
218128546Sderaadt /*
318128546Sderaadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
418128546Sderaadt * All rights reserved.
518128546Sderaadt *
618128546Sderaadt * Redistribution and use in source and binary forms, with or without
718128546Sderaadt * modification, are permitted provided that the following conditions
818128546Sderaadt * are met:
918128546Sderaadt * 1. Redistributions of source code must retain the above copyright
1018128546Sderaadt * notice, this list of conditions and the following disclaimer.
1118128546Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
1218128546Sderaadt * notice, this list of conditions and the following disclaimer in the
1318128546Sderaadt * documentation and/or other materials provided with the distribution.
1418128546Sderaadt *
1518128546Sderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1618128546Sderaadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1718128546Sderaadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1818128546Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1918128546Sderaadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2018128546Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2118128546Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2218128546Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2318128546Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2418128546Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2518128546Sderaadt * SUCH DAMAGE.
2618128546Sderaadt */
2718128546Sderaadt
2818128546Sderaadt #include <sys/types.h>
29aea60beeSderaadt #include <limits.h>
3018128546Sderaadt #include <stdlib.h>
3118128546Sderaadt #include <string.h>
3218128546Sderaadt #include <rpc/rpc.h>
3318128546Sderaadt #include <rpc/xdr.h>
3418128546Sderaadt #include <rpcsvc/yp.h>
3518128546Sderaadt #include <rpcsvc/ypclnt.h>
3618128546Sderaadt #include "ypinternal.h"
3718128546Sderaadt
3818128546Sderaadt int
yp_first(const char * indomain,const char * inmap,char ** outkey,int * outkeylen,char ** outval,int * outvallen)3911e5d692Sderaadt yp_first(const char *indomain, const char *inmap, char **outkey, int *outkeylen,
4011e5d692Sderaadt char **outval, int *outvallen)
4118128546Sderaadt {
4218128546Sderaadt struct ypresp_key_val yprkv;
4318128546Sderaadt struct ypreq_nokey yprnk;
44*2d62dde8Sderaadt struct dom_binding *ysd = NULL;
4518128546Sderaadt struct timeval tv;
4677124a50Sderaadt int tries = 0, r;
4718128546Sderaadt
48a69cfd83Sderaadt if (indomain == NULL || *indomain == '\0' ||
49a69cfd83Sderaadt strlen(indomain) > YPMAXDOMAIN || inmap == NULL ||
50a69cfd83Sderaadt *inmap == '\0' || strlen(inmap) > YPMAXMAP)
51a69cfd83Sderaadt return YPERR_BADARGS;
52a69cfd83Sderaadt
5318128546Sderaadt *outkey = *outval = NULL;
5418128546Sderaadt *outkeylen = *outvallen = 0;
5518128546Sderaadt
5618128546Sderaadt again:
5718128546Sderaadt if (_yp_dobind(indomain, &ysd) != 0)
5818128546Sderaadt return YPERR_DOMAIN;
5918128546Sderaadt
6018128546Sderaadt tv.tv_sec = _yplib_timeout;
6118128546Sderaadt tv.tv_usec = 0;
6218128546Sderaadt
6318128546Sderaadt yprnk.domain = (char *)indomain;
6418128546Sderaadt yprnk.map = (char *)inmap;
6518128546Sderaadt (void)memset(&yprkv, 0, sizeof yprkv);
6618128546Sderaadt
6718128546Sderaadt r = clnt_call(ysd->dom_client, YPPROC_FIRST,
6818128546Sderaadt xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
6918128546Sderaadt if (r != RPC_SUCCESS) {
7077124a50Sderaadt if (tries++)
7118128546Sderaadt clnt_perror(ysd->dom_client, "yp_first: clnt_call");
72*2d62dde8Sderaadt _yp_unbind(ysd);
7318128546Sderaadt goto again;
7418128546Sderaadt }
7518128546Sderaadt if (!(r = ypprot_err(yprkv.stat))) {
7618128546Sderaadt *outkeylen = yprkv.key.keydat_len;
7757fb332bSschwarze *outvallen = yprkv.val.valdat_len;
7857fb332bSschwarze if ((*outkey = malloc(*outkeylen + 1)) == NULL ||
7957fb332bSschwarze (*outval = malloc(*outvallen + 1)) == NULL) {
8057fb332bSschwarze free(*outkey);
81a69cfd83Sderaadt r = YPERR_RESRC;
8257fb332bSschwarze } else {
8318128546Sderaadt (void)memcpy(*outkey, yprkv.key.keydat_val, *outkeylen);
8418128546Sderaadt (*outkey)[*outkeylen] = '\0';
8518128546Sderaadt (void)memcpy(*outval, yprkv.val.valdat_val, *outvallen);
8618128546Sderaadt (*outval)[*outvallen] = '\0';
8718128546Sderaadt }
8818128546Sderaadt }
8918128546Sderaadt xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
9018128546Sderaadt _yp_unbind(ysd);
9118128546Sderaadt return r;
9218128546Sderaadt }
93ba364befSguenther DEF_WEAK(yp_first);
94