xref: /netbsd-src/lib/libc/yp/yp_order.c (revision 4be2f99d7b796075be09b940699672ca22c01984)
1*4be2f99dSchristos /*	$NetBSD: yp_order.c,v 1.15 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_order.c,v 1.15 2024/01/03 18:41:53 christos Exp $");
327808771dSjtc #endif
337808771dSjtc 
3443fa6fe3Sjtc #include "namespace.h"
3564331ae1Scgd #include <string.h>
367808771dSjtc #include <rpc/rpc.h>
377808771dSjtc #include <rpcsvc/yp_prot.h>
387808771dSjtc #include <rpcsvc/ypclnt.h>
39e7cc5503Schristos #include "local.h"
407808771dSjtc 
4143fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(yp_order,_yp_order)4260549036Smycroft __weak_alias(yp_order,_yp_order)
4343fa6fe3Sjtc #endif
4443fa6fe3Sjtc 
457808771dSjtc int
469e66e6d7Sabs yp_order(const char *indomain, const char *inmap, int *outorder)
477808771dSjtc {
487808771dSjtc 	struct dom_binding *ysd;
497808771dSjtc 	struct ypresp_order ypro;
507808771dSjtc 	struct ypreq_nokey yprnk;
51409a9590Schristos 	int r, nerrs = 0;
527808771dSjtc 
5342736edbSlukem 	if (_yp_invalid_domain(indomain))
540724069fSjtc 		return YPERR_BADARGS;
550724069fSjtc 	if (inmap == NULL || *inmap == '\0'
560724069fSjtc 	    || strlen(inmap) > YPMAXMAP)
570724069fSjtc 		return YPERR_BADARGS;
580724069fSjtc 	if (outorder == NULL)
590724069fSjtc 		return YPERR_BADARGS;
600724069fSjtc 
617808771dSjtc again:
627808771dSjtc 	if (_yp_dobind(indomain, &ysd) != 0)
637808771dSjtc 		return YPERR_DOMAIN;
647808771dSjtc 
657808771dSjtc 	yprnk.domain = indomain;
667808771dSjtc 	yprnk.map = inmap;
677808771dSjtc 
687808771dSjtc 	(void)memset(&ypro, 0, sizeof ypro);
697808771dSjtc 
70473ea2c4Schristos 	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_ORDER,
71b2a14ab2Schristos 		      (xdrproc_t)xdr_ypreq_nokey, &yprnk,
72b2a14ab2Schristos 		      (xdrproc_t)xdr_ypresp_order, &ypro,
730724069fSjtc 		      _yplib_timeout);
747808771dSjtc 	if (r != RPC_SUCCESS) {
752b01a8adSchristos 		if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
767808771dSjtc 			clnt_perror(ysd->dom_client, "yp_order: clnt_call");
77409a9590Schristos 			nerrs = 0;
782b01a8adSchristos 		} else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries)
792b01a8adSchristos 			return YPERR_YPSERV;
807808771dSjtc 	        if (r == RPC_PROCUNAVAIL) {
817808771dSjtc 			/* Case of NIS+ server in NIS compat mode */
827808771dSjtc 			r = YPERR_YPERR;
837808771dSjtc 			goto bail;
847808771dSjtc 	        }
857808771dSjtc 		ysd->dom_vers = -1;
867808771dSjtc 		goto again;
877808771dSjtc 	}
887808771dSjtc 	*outorder = ypro.ordernum;
89b2a14ab2Schristos 	xdr_free((xdrproc_t)xdr_ypresp_order, (char *)(void *)&ypro);
907808771dSjtc 	r = ypprot_err(ypro.status);
917808771dSjtc bail:
9243fa6fe3Sjtc 	__yp_unbind(ysd);
937808771dSjtc 	return r;
947808771dSjtc }
95