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