yp_master.c revision 1.14
11.14Schristos/*	$NetBSD: yp_master.c,v 1.14 2012/03/02 17:27:49 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.14Schristos__RCSID("$NetBSD: yp_master.c,v 1.14 2012/03/02 17:27:49 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.2Sjtcextern struct timeval _yplib_timeout;
431.3Schristosextern int _yplib_nerrs;
441.14Schristosextern int _yplib_bindtries;
451.1Sjtc
461.8Sjtc#ifdef __weak_alias
471.11Smycroft__weak_alias(yp_master,_yp_master)
481.8Sjtc#endif
491.8Sjtc
501.1Sjtcint
511.1Sjtcyp_master(indomain, inmap, outname)
521.1Sjtc	const char     *indomain;
531.1Sjtc	const char     *inmap;
541.1Sjtc	char          **outname;
551.1Sjtc{
561.1Sjtc	struct dom_binding *ysd;
571.1Sjtc	struct ypresp_master yprm;
581.1Sjtc	struct ypreq_nokey yprnk;
591.3Schristos	int r, nerrs = 0;
601.1Sjtc
611.4Slukem	if (outname == NULL)
621.4Slukem		return YPERR_BADARGS;
631.4Slukem	*outname = NULL;
641.4Slukem
651.6Slukem	if (_yp_invalid_domain(indomain))
661.2Sjtc		return YPERR_BADARGS;
671.2Sjtc	if (inmap == NULL || *inmap == '\0'
681.2Sjtc	    || strlen(inmap) > YPMAXMAP)
691.2Sjtc		return YPERR_BADARGS;
701.2Sjtc
711.1Sjtcagain:
721.1Sjtc	if (_yp_dobind(indomain, &ysd) != 0)
731.1Sjtc		return YPERR_DOMAIN;
741.1Sjtc
751.1Sjtc	yprnk.domain = indomain;
761.1Sjtc	yprnk.map = inmap;
771.1Sjtc
781.1Sjtc	(void)memset(&yprm, 0, sizeof yprm);
791.1Sjtc
801.12Schristos	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_MASTER,
811.10Schristos		      (xdrproc_t)xdr_ypreq_nokey, &yprnk,
821.10Schristos		      (xdrproc_t)xdr_ypresp_master, &yprm, _yplib_timeout);
831.1Sjtc	if (r != RPC_SUCCESS) {
841.14Schristos		if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
851.3Schristos			clnt_perror(ysd->dom_client, "yp_master: clnt_call");
861.3Schristos			nerrs = 0;
871.14Schristos		} else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries)
881.14Schristos			return YPERR_YPSERV;
891.1Sjtc		ysd->dom_vers = -1;
901.1Sjtc		goto again;
911.1Sjtc	}
921.1Sjtc	if (!(r = ypprot_err(yprm.status))) {
931.1Sjtc		if ((*outname = strdup(yprm.master)) == NULL)
941.2Sjtc			r = YPERR_RESRC;
951.1Sjtc	}
961.10Schristos	xdr_free((xdrproc_t)xdr_ypresp_master, (char *)(void *)&yprm);
971.8Sjtc	__yp_unbind(ysd);
981.5Slukem	if (r != 0) {
991.5Slukem		if (*outname) {
1001.5Slukem			free(*outname);
1011.5Slukem			*outname = NULL;
1021.5Slukem		}
1031.5Slukem	}
1041.1Sjtc	return r;
1051.1Sjtc}
106