yp_master.c revision 1.11
11.11Smycroft/*	$NetBSD: yp_master.c,v 1.11 2000/01/22 22:19:22 mycroft 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 * 3. All advertising materials mentioning features or use of this software
161.1Sjtc *    must display the following acknowledgement:
171.1Sjtc *	This product includes software developed by Theo de Raadt.
181.1Sjtc * 4. The name of the author may not be used to endorse or promote products
191.1Sjtc *    derived from this software without specific prior written permission.
201.1Sjtc *
211.1Sjtc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
221.1Sjtc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
231.1Sjtc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241.1Sjtc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
251.1Sjtc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261.1Sjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271.1Sjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281.1Sjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291.1Sjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301.1Sjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Sjtc * SUCH DAMAGE.
321.1Sjtc */
331.1Sjtc
341.7Schristos#include <sys/cdefs.h>
351.1Sjtc#if defined(LIBC_SCCS) && !defined(lint)
361.11Smycroft__RCSID("$NetBSD: yp_master.c,v 1.11 2000/01/22 22:19:22 mycroft Exp $");
371.1Sjtc#endif
381.1Sjtc
391.8Sjtc#include "namespace.h"
401.1Sjtc#include <string.h>
411.7Schristos#include <stdlib.h>
421.1Sjtc#include <rpc/rpc.h>
431.1Sjtc#include <rpcsvc/yp_prot.h>
441.1Sjtc#include <rpcsvc/ypclnt.h>
451.7Schristos#include "local.h"
461.1Sjtc
471.2Sjtcextern struct timeval _yplib_timeout;
481.3Schristosextern int _yplib_nerrs;
491.1Sjtc
501.8Sjtc#ifdef __weak_alias
511.11Smycroft__weak_alias(yp_master,_yp_master)
521.8Sjtc#endif
531.8Sjtc
541.1Sjtcint
551.1Sjtcyp_master(indomain, inmap, outname)
561.1Sjtc	const char     *indomain;
571.1Sjtc	const char     *inmap;
581.1Sjtc	char          **outname;
591.1Sjtc{
601.1Sjtc	struct dom_binding *ysd;
611.1Sjtc	struct ypresp_master yprm;
621.1Sjtc	struct ypreq_nokey yprnk;
631.3Schristos	int r, nerrs = 0;
641.1Sjtc
651.4Slukem	if (outname == NULL)
661.4Slukem		return YPERR_BADARGS;
671.4Slukem	*outname = NULL;
681.4Slukem
691.6Slukem	if (_yp_invalid_domain(indomain))
701.2Sjtc		return YPERR_BADARGS;
711.2Sjtc	if (inmap == NULL || *inmap == '\0'
721.2Sjtc	    || strlen(inmap) > YPMAXMAP)
731.2Sjtc		return YPERR_BADARGS;
741.2Sjtc
751.1Sjtcagain:
761.1Sjtc	if (_yp_dobind(indomain, &ysd) != 0)
771.1Sjtc		return YPERR_DOMAIN;
781.1Sjtc
791.1Sjtc	yprnk.domain = indomain;
801.1Sjtc	yprnk.map = inmap;
811.1Sjtc
821.1Sjtc	(void)memset(&yprm, 0, sizeof yprm);
831.1Sjtc
841.1Sjtc	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
851.10Schristos		      (xdrproc_t)xdr_ypreq_nokey, &yprnk,
861.10Schristos		      (xdrproc_t)xdr_ypresp_master, &yprm, _yplib_timeout);
871.1Sjtc	if (r != RPC_SUCCESS) {
881.3Schristos		if (++nerrs == _yplib_nerrs) {
891.3Schristos			clnt_perror(ysd->dom_client, "yp_master: clnt_call");
901.3Schristos			nerrs = 0;
911.3Schristos		}
921.1Sjtc		ysd->dom_vers = -1;
931.1Sjtc		goto again;
941.1Sjtc	}
951.1Sjtc	if (!(r = ypprot_err(yprm.status))) {
961.1Sjtc		if ((*outname = strdup(yprm.master)) == NULL)
971.2Sjtc			r = YPERR_RESRC;
981.1Sjtc	}
991.10Schristos	xdr_free((xdrproc_t)xdr_ypresp_master, (char *)(void *)&yprm);
1001.8Sjtc	__yp_unbind(ysd);
1011.5Slukem	if (r != 0) {
1021.5Slukem		if (*outname) {
1031.5Slukem			free(*outname);
1041.5Slukem			*outname = NULL;
1051.5Slukem		}
1061.5Slukem	}
1071.1Sjtc	return r;
1081.1Sjtc}
109