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