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