11.16Schristos/*	$NetBSD: yp_master.c,v 1.16 2024/01/03 18:41:53 christos Exp $	 */
21.1Sjtc
31.1Sjtc/*
41.1Sjtc * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
51.1Sjtc * All rights reserved.
61.1Sjtc *
71.1Sjtc * Redistribution and use in source and binary forms, with or without
81.1Sjtc * modification, are permitted provided that the following conditions
91.1Sjtc * are met:
101.1Sjtc * 1. Redistributions of source code must retain the above copyright
111.1Sjtc *    notice, this list of conditions and the following disclaimer.
121.1Sjtc * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjtc *    notice, this list of conditions and the following disclaimer in the
141.1Sjtc *    documentation and/or other materials provided with the distribution.
151.1Sjtc *
161.1Sjtc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
171.1Sjtc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
181.1Sjtc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Sjtc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
201.1Sjtc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Sjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Sjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Sjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Sjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjtc * SUCH DAMAGE.
271.1Sjtc */
281.1Sjtc
291.7Schristos#include <sys/cdefs.h>
301.1Sjtc#if defined(LIBC_SCCS) && !defined(lint)
311.16Schristos__RCSID("$NetBSD: yp_master.c,v 1.16 2024/01/03 18:41:53 christos Exp $");
321.1Sjtc#endif
331.1Sjtc
341.8Sjtc#include "namespace.h"
351.1Sjtc#include <string.h>
361.7Schristos#include <stdlib.h>
371.1Sjtc#include <rpc/rpc.h>
381.1Sjtc#include <rpcsvc/yp_prot.h>
391.1Sjtc#include <rpcsvc/ypclnt.h>
401.7Schristos#include "local.h"
411.1Sjtc
421.8Sjtc#ifdef __weak_alias
431.11Smycroft__weak_alias(yp_master,_yp_master)
441.8Sjtc#endif
451.8Sjtc
461.1Sjtcint
471.15Sabsyp_master(const char *indomain, const char *inmap, char **outname)
481.1Sjtc{
491.1Sjtc	struct dom_binding *ysd;
501.1Sjtc	struct ypresp_master yprm;
511.1Sjtc	struct ypreq_nokey yprnk;
521.3Schristos	int r, nerrs = 0;
531.1Sjtc
541.4Slukem	if (outname == NULL)
551.4Slukem		return YPERR_BADARGS;
561.4Slukem	*outname = NULL;
571.4Slukem
581.6Slukem	if (_yp_invalid_domain(indomain))
591.2Sjtc		return YPERR_BADARGS;
601.2Sjtc	if (inmap == NULL || *inmap == '\0'
611.2Sjtc	    || strlen(inmap) > YPMAXMAP)
621.2Sjtc		return YPERR_BADARGS;
631.2Sjtc
641.1Sjtcagain:
651.1Sjtc	if (_yp_dobind(indomain, &ysd) != 0)
661.1Sjtc		return YPERR_DOMAIN;
671.1Sjtc
681.1Sjtc	yprnk.domain = indomain;
691.1Sjtc	yprnk.map = inmap;
701.1Sjtc
711.1Sjtc	(void)memset(&yprm, 0, sizeof yprm);
721.1Sjtc
731.12Schristos	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_MASTER,
741.10Schristos		      (xdrproc_t)xdr_ypreq_nokey, &yprnk,
751.10Schristos		      (xdrproc_t)xdr_ypresp_master, &yprm, _yplib_timeout);
761.1Sjtc	if (r != RPC_SUCCESS) {
771.14Schristos		if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
781.3Schristos			clnt_perror(ysd->dom_client, "yp_master: clnt_call");
791.3Schristos			nerrs = 0;
801.14Schristos		} else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries)
811.14Schristos			return YPERR_YPSERV;
821.1Sjtc		ysd->dom_vers = -1;
831.1Sjtc		goto again;
841.1Sjtc	}
851.1Sjtc	if (!(r = ypprot_err(yprm.status))) {
861.1Sjtc		if ((*outname = strdup(yprm.master)) == NULL)
871.2Sjtc			r = YPERR_RESRC;
881.1Sjtc	}
891.10Schristos	xdr_free((xdrproc_t)xdr_ypresp_master, (char *)(void *)&yprm);
901.8Sjtc	__yp_unbind(ysd);
911.5Slukem	if (r != 0) {
921.5Slukem		if (*outname) {
931.5Slukem			free(*outname);
941.5Slukem			*outname = NULL;
951.5Slukem		}
961.5Slukem	}
971.1Sjtc	return r;
981.1Sjtc}
99