xref: /netbsd-src/lib/libc/yp/yp_master.c (revision 4be2f99d7b796075be09b940699672ca22c01984)
1*4be2f99dSchristos /*	$NetBSD: yp_master.c,v 1.16 2024/01/03 18:41:53 christos Exp $	 */
27808771dSjtc 
37808771dSjtc /*
47808771dSjtc  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
57808771dSjtc  * All rights reserved.
67808771dSjtc  *
77808771dSjtc  * Redistribution and use in source and binary forms, with or without
87808771dSjtc  * modification, are permitted provided that the following conditions
97808771dSjtc  * are met:
107808771dSjtc  * 1. Redistributions of source code must retain the above copyright
117808771dSjtc  *    notice, this list of conditions and the following disclaimer.
127808771dSjtc  * 2. Redistributions in binary form must reproduce the above copyright
137808771dSjtc  *    notice, this list of conditions and the following disclaimer in the
147808771dSjtc  *    documentation and/or other materials provided with the distribution.
157808771dSjtc  *
167808771dSjtc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
177808771dSjtc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
187808771dSjtc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197808771dSjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
207808771dSjtc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217808771dSjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227808771dSjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237808771dSjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247808771dSjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257808771dSjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267808771dSjtc  * SUCH DAMAGE.
277808771dSjtc  */
287808771dSjtc 
29e7cc5503Schristos #include <sys/cdefs.h>
307808771dSjtc #if defined(LIBC_SCCS) && !defined(lint)
31*4be2f99dSchristos __RCSID("$NetBSD: yp_master.c,v 1.16 2024/01/03 18:41:53 christos Exp $");
327808771dSjtc #endif
337808771dSjtc 
3443fa6fe3Sjtc #include "namespace.h"
357808771dSjtc #include <string.h>
36e7cc5503Schristos #include <stdlib.h>
377808771dSjtc #include <rpc/rpc.h>
387808771dSjtc #include <rpcsvc/yp_prot.h>
397808771dSjtc #include <rpcsvc/ypclnt.h>
40e7cc5503Schristos #include "local.h"
417808771dSjtc 
4243fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(yp_master,_yp_master)4360549036Smycroft __weak_alias(yp_master,_yp_master)
4443fa6fe3Sjtc #endif
4543fa6fe3Sjtc 
467808771dSjtc int
479e66e6d7Sabs yp_master(const char *indomain, const char *inmap, char **outname)
487808771dSjtc {
497808771dSjtc 	struct dom_binding *ysd;
507808771dSjtc 	struct ypresp_master yprm;
517808771dSjtc 	struct ypreq_nokey yprnk;
52409a9590Schristos 	int r, nerrs = 0;
537808771dSjtc 
54301e6e6cSlukem 	if (outname == NULL)
55301e6e6cSlukem 		return YPERR_BADARGS;
56301e6e6cSlukem 	*outname = NULL;
57301e6e6cSlukem 
5842736edbSlukem 	if (_yp_invalid_domain(indomain))
590724069fSjtc 		return YPERR_BADARGS;
600724069fSjtc 	if (inmap == NULL || *inmap == '\0'
610724069fSjtc 	    || strlen(inmap) > YPMAXMAP)
620724069fSjtc 		return YPERR_BADARGS;
630724069fSjtc 
647808771dSjtc again:
657808771dSjtc 	if (_yp_dobind(indomain, &ysd) != 0)
667808771dSjtc 		return YPERR_DOMAIN;
677808771dSjtc 
687808771dSjtc 	yprnk.domain = indomain;
697808771dSjtc 	yprnk.map = inmap;
707808771dSjtc 
717808771dSjtc 	(void)memset(&yprm, 0, sizeof yprm);
727808771dSjtc 
73473ea2c4Schristos 	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_MASTER,
74b2a14ab2Schristos 		      (xdrproc_t)xdr_ypreq_nokey, &yprnk,
75b2a14ab2Schristos 		      (xdrproc_t)xdr_ypresp_master, &yprm, _yplib_timeout);
767808771dSjtc 	if (r != RPC_SUCCESS) {
772b01a8adSchristos 		if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
787808771dSjtc 			clnt_perror(ysd->dom_client, "yp_master: clnt_call");
79409a9590Schristos 			nerrs = 0;
802b01a8adSchristos 		} else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries)
812b01a8adSchristos 			return YPERR_YPSERV;
827808771dSjtc 		ysd->dom_vers = -1;
837808771dSjtc 		goto again;
847808771dSjtc 	}
857808771dSjtc 	if (!(r = ypprot_err(yprm.status))) {
867808771dSjtc 		if ((*outname = strdup(yprm.master)) == NULL)
870724069fSjtc 			r = YPERR_RESRC;
887808771dSjtc 	}
89b2a14ab2Schristos 	xdr_free((xdrproc_t)xdr_ypresp_master, (char *)(void *)&yprm);
9043fa6fe3Sjtc 	__yp_unbind(ysd);
91db4fd8d5Slukem 	if (r != 0) {
92db4fd8d5Slukem 		if (*outname) {
93db4fd8d5Slukem 			free(*outname);
94db4fd8d5Slukem 			*outname = NULL;
95db4fd8d5Slukem 		}
96db4fd8d5Slukem 	}
977808771dSjtc 	return r;
987808771dSjtc }
99