xref: /dragonfly/lib/librpcsvc/yp_update.c (revision 479ab7f0)
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  * $FreeBSD: src/lib/librpcsvc/yp_update.c,v 1.7 2003/10/26 03:43:35 peter Exp $
39  * $DragonFly: src/lib/librpcsvc/yp_update.c,v 1.3 2007/11/25 14:33:02 swildner Exp $
40  *
41  */
42 
43 #include <stdlib.h>
44 #include <rpc/rpc.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #include <rpcsvc/ypupdate_prot.h>
48 #include <rpc/key_prot.h>
49 
50 #ifndef WINDOW
51 #define WINDOW (60*60)
52 #endif
53 
54 #ifndef TIMEOUT
55 #define TIMEOUT 300
56 #endif
57 
58 int
59 yp_update(char *domain, char *map, unsigned int ypop, char *key, int keylen,
60 	  char *data, int datalen)
61 {
62 	char *master;
63 	int rval;
64 	unsigned int res;
65 	struct ypupdate_args upargs;
66 	struct ypdelete_args delargs;
67 	CLIENT *clnt;
68 	char netname[MAXNETNAMELEN+1];
69 	des_block des_key;
70 	struct timeval timeout;
71 
72 	/* Get the master server name for 'domain.' */
73 	if ((rval = yp_master(domain, map, &master)))
74 		return(rval);
75 
76 	/* Check that ypupdated is running there. */
77 	if (getrpcport(master, YPU_PROG, YPU_VERS, ypop))
78 		return(YPERR_DOMAIN);
79 
80 	/* Get a handle. */
81 	if ((clnt = clnt_create(master, YPU_PROG, YPU_VERS, "tcp")) == NULL)
82 		return(YPERR_RPC);
83 
84 	/*
85 	 * Assemble netname of server.
86 	 * NOTE: It's difficult to discern from the documentation, but
87 	 * when you make a Secure RPC call, the netname you pass should
88 	 * be the netname of the guy on the other side, not your own
89 	 * netname. This is how the client side knows what public key
90 	 * to use for the initial exchange. Passing your own netname
91 	 * only works if the server on the other side is running under
92 	 * your UID.
93 	 */
94 	if (!host2netname(netname, master, domain)) {
95 		clnt_destroy(clnt);
96 		return(YPERR_BADARGS);
97 	}
98 
99 	/* Make up a DES session key. */
100 	key_gendes(&des_key);
101 
102 	/* Set up DES authentication. */
103 	if ((clnt->cl_auth = (AUTH *)authdes_create(netname, WINDOW, NULL,
104 			&des_key)) == NULL) {
105 		clnt_destroy(clnt);
106 		return(YPERR_RESRC);
107 	}
108 
109 	/* Set a timeout for clnt_call(). */
110 	timeout.tv_usec = 0;
111 	timeout.tv_sec = TIMEOUT;
112 
113 	/*
114 	 * Make the call. Note that we use clnt_call() here rather than
115 	 * the rpcgen-erated client stubs. We could use those stubs, but
116 	 * then we'd have to do some gymnastics to get at the error
117 	 * information to figure out what error code to send back to the
118 	 * caller. With clnt_call(), we get the error status returned to
119 	 * us right away, and we only have to exert a small amount of
120 	 * extra effort.
121 	 */
122 	switch (ypop) {
123 	case YPOP_CHANGE:
124 		upargs.mapname = map;
125 		upargs.key.yp_buf_len = keylen;
126 		upargs.key.yp_buf_val = key;
127 		upargs.datum.yp_buf_len = datalen;
128 		upargs.datum.yp_buf_val = data;
129 
130 		if ((rval = clnt_call(clnt, YPU_CHANGE,
131 			(xdrproc_t)xdr_ypupdate_args, &upargs,
132 			(xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
133 			if (rval == RPC_AUTHERROR)
134 				res = YPERR_ACCESS;
135 			else
136 				res = YPERR_RPC;
137 		}
138 
139 		break;
140 	case YPOP_INSERT:
141 		upargs.mapname = map;
142 		upargs.key.yp_buf_len = keylen;
143 		upargs.key.yp_buf_val = key;
144 		upargs.datum.yp_buf_len = datalen;
145 		upargs.datum.yp_buf_val = data;
146 
147 		if ((rval = clnt_call(clnt, YPU_INSERT,
148 			(xdrproc_t)xdr_ypupdate_args, &upargs,
149 			(xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
150 			if (rval == RPC_AUTHERROR)
151 				res = YPERR_ACCESS;
152 			else
153 				res = YPERR_RPC;
154 		}
155 
156 		break;
157 	case YPOP_DELETE:
158 		delargs.mapname = map;
159 		delargs.key.yp_buf_len = keylen;
160 		delargs.key.yp_buf_val = key;
161 
162 		if ((rval = clnt_call(clnt, YPU_DELETE,
163 			(xdrproc_t)xdr_ypdelete_args, &delargs,
164 			(xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
165 			if (rval == RPC_AUTHERROR)
166 				res = YPERR_ACCESS;
167 			else
168 				res = YPERR_RPC;
169 		}
170 
171 		break;
172 	case YPOP_STORE:
173 		upargs.mapname = map;
174 		upargs.key.yp_buf_len = keylen;
175 		upargs.key.yp_buf_val = key;
176 		upargs.datum.yp_buf_len = datalen;
177 		upargs.datum.yp_buf_val = data;
178 
179 		if ((rval = clnt_call(clnt, YPU_STORE,
180 			(xdrproc_t)xdr_ypupdate_args, &upargs,
181 			(xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
182 			if (rval == RPC_AUTHERROR)
183 				res = YPERR_ACCESS;
184 			else
185 				res = YPERR_RPC;
186 		}
187 
188 		break;
189 	default:
190 		res = YPERR_BADARGS;
191 		break;
192 	}
193 
194 	/* All done: tear down the connection. */
195 	auth_destroy(clnt->cl_auth);
196 	clnt_destroy(clnt);
197 	free(master);
198 
199 	return(res);
200 }
201