xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_order.c (revision 61961e0f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /*	  All Rights Reserved   */
30 
31 /*
32  * Portions of this source code were derived from Berkeley
33  * under license from the Regents of the University of
34  * California.
35  */
36 
37 #pragma ident	"%Z%%M%	%I%	%E% SMI"
38 
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <rpc/rpc.h>
42 #include "yp_b.h"
43 #include <rpcsvc/yp_prot.h>
44 #include <rpcsvc/ypclnt.h>
45 #include <sys/types.h>
46 #include <string.h>
47 
48 static int doorder(char *, char *, struct dom_binding *, struct timeval,
49     unsigned long *);
50 
51 
52 /*
53  * This checks parameters, and implements the outer "until binding success"
54  * loop.
55  */
56 int
57 yp_order(char *domain, char *map, unsigned long *order)
58 {
59 	size_t domlen;
60 	size_t maplen;
61 	int reason;
62 	struct dom_binding *pdomb;
63 
64 	if ((map == NULL) || (domain == NULL))
65 		return (YPERR_BADARGS);
66 
67 	domlen = strlen(domain);
68 	maplen = strlen(map);
69 
70 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
71 	    (maplen == 0) || (maplen > YPMAXMAP) ||
72 	    (order == NULL))
73 		return (YPERR_BADARGS);
74 
75 	for (;;) {
76 
77 		if (reason = __yp_dobind(domain, &pdomb))
78 			return (reason);
79 
80 		if (pdomb->dom_binding->ypbind_hi_vers >= YPVERS) {
81 
82 			reason = doorder(domain, map, pdomb, _ypserv_timeout,
83 			    order);
84 
85 			__yp_rel_binding(pdomb);
86 			if (reason == YPERR_RPC) {
87 				yp_unbind(domain);
88 				(void) _sleep(_ypsleeptime);
89 			} else {
90 				break;
91 			}
92 		} else {
93 			__yp_rel_binding(pdomb);
94 			return (YPERR_VERS);
95 		}
96 	}
97 
98 	return (reason);
99 }
100 
101 /*
102  * This talks v3 to ypserv
103  */
104 static int
105 doorder(char *domain, char *map, struct dom_binding *pdomb,
106 				struct timeval timeout, unsigned long *order)
107 {
108 	struct ypreq_nokey req;
109 	struct ypresp_order resp;
110 	unsigned int retval = 0;
111 
112 	req.domain = domain;
113 	req.map = map;
114 	(void) memset((char *)&resp, 0, sizeof (struct ypresp_order));
115 
116 	/*
117 	 * Do the get_order request.  If the rpc call failed, return with
118 	 * status from this point.
119 	 */
120 
121 	if (clnt_call(pdomb->dom_client, YPPROC_ORDER,
122 			(xdrproc_t)xdr_ypreq_nokey,
123 		    (char *)&req, (xdrproc_t)xdr_ypresp_order, (char *)&resp,
124 		    timeout) != RPC_SUCCESS)
125 		return (YPERR_RPC);
126 
127 	/* See if the request succeeded */
128 
129 	if (resp.status != YP_TRUE) {
130 		retval = ypprot_err(resp.status);
131 	}
132 
133 	*order = (unsigned long)resp.ordernum;
134 	CLNT_FREERES(pdomb->dom_client,
135 		(xdrproc_t)xdr_ypresp_order, (char *)&resp);
136 	return (retval);
137 }
138