yp_update.c revision 84220
1/*
2 * Copyright (c) 1995, 1996
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * ypupdate client-side library function.
33 *
34 * Written by Bill Paul <wpaul@ctr.columbia.edu>
35 * Center for Telecommunications Research
36 * Columbia University, New York City
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/lib/librpcsvc/yp_update.c 84220 2001-09-30 22:15:15Z dillon $");
41
42#include <stdlib.h>
43#include <rpc/rpc.h>
44#include <rpcsvc/yp_prot.h>
45#include <rpcsvc/ypclnt.h>
46#include <rpcsvc/ypupdate_prot.h>
47#include <rpc/key_prot.h>
48
49#ifndef WINDOW
50#define WINDOW (60*60)
51#endif
52
53#ifndef TIMEOUT
54#define TIMEOUT 300
55#endif
56
57int
58yp_update(domain, map, ypop, key, keylen, data, datalen)
59	char		*domain;
60	char		*map;
61	unsigned int	ypop;
62	char		*key;
63	int		keylen;
64	char		*data;
65	int		datalen;
66{
67	char *master;
68	int rval;
69	unsigned int res;
70	struct ypupdate_args upargs;
71	struct ypdelete_args delargs;
72	CLIENT *clnt;
73	char netname[MAXNETNAMELEN+1];
74	des_block des_key;
75	struct timeval timeout;
76
77	/* Get the master server name for 'domain.' */
78	if ((rval = yp_master(domain, map, &master)))
79		return(rval);
80
81	/* Check that ypupdated is running there. */
82	if (getrpcport(master, YPU_PROG, YPU_VERS, ypop))
83		return(YPERR_DOMAIN);
84
85	/* Get a handle. */
86	if ((clnt = clnt_create(master, YPU_PROG, YPU_VERS, "tcp")) == NULL)
87		return(YPERR_RPC);
88
89	/*
90	 * Assemble netname of server.
91	 * NOTE: It's difficult to discern from the documentation, but
92	 * when you make a Secure RPC call, the netname you pass should
93	 * be the netname of the guy on the other side, not your own
94	 * netname. This is how the client side knows what public key
95	 * to use for the initial exchange. Passing your own netname
96	 * only works if the server on the other side is running under
97	 * your UID.
98	 */
99	if (!host2netname(netname, master, domain)) {
100		clnt_destroy(clnt);
101		return(YPERR_BADARGS);
102	}
103
104	/* Make up a DES session key. */
105	key_gendes(&des_key);
106
107	/* Set up DES authentication. */
108	if ((clnt->cl_auth = (AUTH *)authdes_create(netname, WINDOW, NULL,
109			&des_key)) == NULL) {
110		clnt_destroy(clnt);
111		return(YPERR_RESRC);
112	}
113
114	/* Set a timeout for clnt_call(). */
115	timeout.tv_usec = 0;
116	timeout.tv_sec = TIMEOUT;
117
118	/*
119	 * Make the call. Note that we use clnt_call() here rather than
120	 * the rpcgen-erated client stubs. We could use those stubs, but
121	 * then we'd have to do some gymnastics to get at the error
122	 * information to figure out what error code to send back to the
123	 * caller. With clnt_call(), we get the error status returned to
124	 * us right away, and we only have to exert a small amount of
125	 * extra effort.
126	 */
127	switch(ypop) {
128	case YPOP_CHANGE:
129		upargs.mapname = map;
130		upargs.key.yp_buf_len = keylen;
131		upargs.key.yp_buf_val = key;
132		upargs.datum.yp_buf_len = datalen;
133		upargs.datum.yp_buf_val = data;
134
135		if ((rval = clnt_call(clnt, YPU_CHANGE, xdr_ypupdate_args,
136			&upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
137			if (rval == RPC_AUTHERROR)
138				res = YPERR_ACCESS;
139			else
140				res = YPERR_RPC;
141		}
142
143		break;
144	case YPOP_INSERT:
145		upargs.mapname = map;
146		upargs.key.yp_buf_len = keylen;
147		upargs.key.yp_buf_val = key;
148		upargs.datum.yp_buf_len = datalen;
149		upargs.datum.yp_buf_val = data;
150
151		if ((rval = clnt_call(clnt, YPU_INSERT, xdr_ypupdate_args,
152			&upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
153			if (rval == RPC_AUTHERROR)
154				res = YPERR_ACCESS;
155			else
156				res = YPERR_RPC;
157		}
158
159		break;
160	case YPOP_DELETE:
161		delargs.mapname = map;
162		delargs.key.yp_buf_len = keylen;
163		delargs.key.yp_buf_val = key;
164
165		if ((rval = clnt_call(clnt, YPU_DELETE, xdr_ypdelete_args,
166			&delargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
167			if (rval == RPC_AUTHERROR)
168				res = YPERR_ACCESS;
169			else
170				res = YPERR_RPC;
171		}
172
173		break;
174	case YPOP_STORE:
175		upargs.mapname = map;
176		upargs.key.yp_buf_len = keylen;
177		upargs.key.yp_buf_val = key;
178		upargs.datum.yp_buf_len = datalen;
179		upargs.datum.yp_buf_val = data;
180
181		if ((rval = clnt_call(clnt, YPU_STORE, xdr_ypupdate_args,
182			&upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
183			if (rval == RPC_AUTHERROR)
184				res = YPERR_ACCESS;
185			else
186				res = YPERR_RPC;
187		}
188
189		break;
190	default:
191		res = YPERR_BADARGS;
192		break;
193	}
194
195	/* All done: tear down the connection. */
196	auth_destroy(clnt->cl_auth);
197	clnt_destroy(clnt);
198	free(master);
199
200	return(res);
201}
202