xref: /titanic_50/usr/src/lib/libnsl/yp/yp_master.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
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 2006 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 "mt.h"
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <rpc/rpc.h>
43 #include <sys/types.h>
44 #include "yp_b.h"
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #include <string.h>
48 
49 static int domaster(char *, char *, struct dom_binding *, struct timeval,
50     char **);
51 extern int __yp_master_rsvdport(char *, char *, char **);
52 
53 /*
54  * This checks parameters, and implements the outer "until binding success"
55  * loop.
56  */
57 int
58 yp_master(char *domain, char *map, char **master)
59 {
60 	size_t domlen;
61 	size_t maplen;
62 	int reason;
63 	struct dom_binding *pdomb;
64 
65 	if ((map == NULL) || (domain == NULL))
66 		return (YPERR_BADARGS);
67 
68 	domlen = strlen(domain);
69 	maplen = strlen(map);
70 
71 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
72 	    (maplen == 0) || (maplen > YPMAXMAP) ||
73 	    (master == NULL))
74 		return (YPERR_BADARGS);
75 
76 	for (;;) {
77 
78 		if (reason = __yp_dobind(domain, &pdomb))
79 			return (reason);
80 
81 		if (pdomb->dom_binding->ypbind_hi_vers >= YPVERS) {
82 
83 			reason = domaster(domain, map, pdomb, _ypserv_timeout,
84 			    master);
85 
86 			__yp_rel_binding(pdomb);
87 			if (reason == YPERR_RPC) {
88 				yp_unbind(domain);
89 				(void) sleep(_ypsleeptime);
90 			} else {
91 				break;
92 			}
93 		} else {
94 			__yp_rel_binding(pdomb);
95 			return (YPERR_VERS);
96 		}
97 	}
98 
99 	if (reason == YPERR_MAP && geteuid() == 0) {
100 		/*
101 		 * Lookup could be for a secure map; fail over to retry
102 		 * from a reserved port. Only useful to try this if we're
103 		 * the super user.
104 		 */
105 		int rsvdreason;
106 		rsvdreason = __yp_master_rsvdport(domain, map, master);
107 		if (rsvdreason == 0)
108 			reason = rsvdreason;
109 	}
110 
111 	return (reason);
112 }
113 
114 
115 /*
116  * This function is identical to 'yp_master' with the exception that it calls
117  * '__yp_dobind_rsvdport' rather than '__yp_dobind'
118  */
119 int
120 __yp_master_rsvdport(char *domain, char *map, char **master)
121 {
122 	size_t domlen;
123 	size_t maplen;
124 	int reason;
125 	struct dom_binding *pdomb;
126 
127 	if ((map == NULL) || (domain == NULL))
128 		return (YPERR_BADARGS);
129 
130 	domlen = strlen(domain);
131 	maplen = strlen(map);
132 
133 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
134 	    (maplen == 0) || (maplen > YPMAXMAP) ||
135 	    (master == NULL))
136 		return (YPERR_BADARGS);
137 
138 	for (;;) {
139 
140 		if (reason = __yp_dobind_rsvdport(domain, &pdomb))
141 			return (reason);
142 
143 		if (pdomb->dom_binding->ypbind_hi_vers >= YPVERS) {
144 
145 			reason = domaster(domain, map, pdomb, _ypserv_timeout,
146 			    master);
147 
148 			/*
149 			 * Have to free the binding since the reserved
150 			 * port bindings are not cached.
151 			 */
152 			__yp_rel_binding(pdomb);
153 			free_dom_binding(pdomb);
154 			if (reason == YPERR_RPC) {
155 				yp_unbind(domain);
156 				(void) sleep(_ypsleeptime);
157 			} else {
158 				break;
159 			}
160 		} else {
161 			/*
162 			 * Have to free the binding since the reserved
163 			 * port bindings are not cached.
164 			 */
165 			__yp_rel_binding(pdomb);
166 			free_dom_binding(pdomb);
167 			return (YPERR_VERS);
168 		}
169 	}
170 	return (reason);
171 }
172 
173 /*
174  * This talks v2 to ypserv
175  */
176 static int
177 domaster(char *domain, char *map, struct dom_binding *pdomb,
178 					struct timeval timeout, char **master)
179 {
180 	struct ypreq_nokey req;
181 	struct ypresp_master resp;
182 	unsigned int retval = 0;
183 
184 	req.domain = domain;
185 	req.map = map;
186 	(void) memset(&resp, 0, sizeof (struct ypresp_master));
187 
188 	/*
189 	 * Do the get_master request.  If the rpc call failed, return with
190 	 * status from this point.
191 	 */
192 
193 	if (clnt_call(pdomb->dom_client,
194 			YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
195 		    (char *)&req, (xdrproc_t)xdr_ypresp_master, (char *)&resp,
196 		    timeout) != RPC_SUCCESS)
197 		return (YPERR_RPC);
198 
199 	/* See if the request succeeded */
200 
201 	if (resp.status != YP_TRUE)
202 		retval = ypprot_err(resp.status);
203 
204 	/* Get some memory which the user can get rid of as he likes */
205 
206 	if (!retval && ((*master = malloc(strlen(resp.master) + 1)) == NULL))
207 		retval = YPERR_RESRC;
208 
209 	if (!retval)
210 		(void) strcpy(*master, resp.master);
211 
212 	CLNT_FREERES(pdomb->dom_client,
213 		(xdrproc_t)xdr_ypresp_master, (char *)&resp);
214 	return (retval);
215 }
216