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 "yp_b.h"
44#include <rpcsvc/yp_prot.h>
45#include <rpcsvc/ypclnt.h>
46#include <sys/types.h>
47#include <string.h>
48
49static int doorder(char *, char *, struct dom_binding *, struct timeval,
50    unsigned long *);
51
52
53/*
54 * This checks parameters, and implements the outer "until binding success"
55 * loop.
56 */
57int
58yp_order(char *domain, char *map, unsigned long *order)
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	    (order == 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 = doorder(domain, map, pdomb, _ypserv_timeout,
84			    order);
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	return (reason);
100}
101
102/*
103 * This talks v3 to ypserv
104 */
105static int
106doorder(char *domain, char *map, struct dom_binding *pdomb,
107				struct timeval timeout, unsigned long *order)
108{
109	struct ypreq_nokey req;
110	struct ypresp_order resp;
111	unsigned int retval = 0;
112
113	req.domain = domain;
114	req.map = map;
115	(void) memset((char *)&resp, 0, sizeof (struct ypresp_order));
116
117	/*
118	 * Do the get_order request.  If the rpc call failed, return with
119	 * status from this point.
120	 */
121
122	if (clnt_call(pdomb->dom_client, YPPROC_ORDER,
123			(xdrproc_t)xdr_ypreq_nokey,
124		    (char *)&req, (xdrproc_t)xdr_ypresp_order, (char *)&resp,
125		    timeout) != RPC_SUCCESS)
126		return (YPERR_RPC);
127
128	/* See if the request succeeded */
129
130	if (resp.status != YP_TRUE) {
131		retval = ypprot_err(resp.status);
132	}
133
134	*order = (unsigned long)resp.ordernum;
135	CLNT_FREERES(pdomb->dom_client,
136		(xdrproc_t)xdr_ypresp_order, (char *)&resp);
137	return (retval);
138}
139