xref: /freebsd/usr.sbin/rpc.ypupdated/ypupdated_server.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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 server implementation
33  *
34  * Written by Bill Paul <wpaul@ctr.columbia.edu>
35  * Center for Telecommunications Research
36  * Columbia University, New York City
37  */
38 
39 #ifndef lint
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43 
44 #include <stdio.h>
45 #include <rpc/rpc.h>
46 #include <rpc/key_prot.h>
47 #include <sys/param.h>
48 #include <sys/cdefs.h>
49 #include <rpcsvc/yp.h>
50 #include "ypupdate_prot.h"
51 #include "ypupdated_extern.h"
52 #include "yp_extern.h"
53 #include "ypxfr_extern.h"
54 
55 int children = 0;
56 int forked = 0;
57 
58 /*
59  * Try to avoid spoofing: if a client chooses to use a very large
60  * window and then tries a bunch of randomly chosen encrypted timestamps,
61  * there's a chance he might stumble onto a valid combination.
62  * We therefore reject any RPCs with a window size larger than a preset
63  * value.
64  */
65 #ifndef WINDOW
66 #define WINDOW (60*60)
67 #endif
68 
69 static enum auth_stat yp_checkauth(svcreq)
70 	struct svc_req *svcreq;
71 {
72 	struct authdes_cred *des_cred;
73 
74 	switch (svcreq->rq_cred.oa_flavor) {
75 	case AUTH_DES:
76 		des_cred = (struct authdes_cred *) svcreq->rq_clntcred;
77 		if (des_cred->adc_fullname.window > WINDOW) {
78 			yp_error("warning: client-specified window size \
79 was too large -- possible spoof attempt");
80 			return(AUTH_BADCRED);
81 		}
82 		return(AUTH_OK);
83 		break;
84 	case AUTH_UNIX:
85 	case AUTH_NONE:
86 		yp_error("warning: client didn't use DES authentication");
87 		return(AUTH_TOOWEAK);
88 		break;
89 	default:
90 		yp_error("client used unknown auth flavor");
91 		return(AUTH_REJECTEDCRED);
92 		break;
93 	}
94 }
95 
96 unsigned int *ypu_change_1_svc(args, svcreq)
97 	struct ypupdate_args *args;
98 	struct svc_req *svcreq;
99 {
100 	struct authdes_cred *des_cred;
101 	static int res;
102 	char *netname;
103 	enum auth_stat astat;
104 
105 	res = 0;
106 
107 	astat = yp_checkauth(svcreq);
108 
109 	if (astat != AUTH_OK) {
110 		svcerr_auth(svcreq->rq_xprt, astat);
111 		return(&res);
112 	}
113 
114 	des_cred = (struct authdes_cred *) svcreq->rq_clntcred;
115 	netname = des_cred->adc_fullname.name;
116 
117 	res = localupdate(netname, "/etc/publickey", YPOP_CHANGE,
118 		args->key.yp_buf_len, args->key.yp_buf_val,
119 		args->datum.yp_buf_len, args->datum.yp_buf_val);
120 
121 	if (res)
122 		return (&res);
123 
124 	res = ypmap_update(netname, args->mapname, YPOP_CHANGE,
125 		args->key.yp_buf_len, args->key.yp_buf_val,
126 		args->datum.yp_buf_len, args->datum.yp_buf_val);
127 
128 	return (&res);
129 }
130 
131 unsigned int *ypu_insert_1_svc(args, svcreq)
132 	struct ypupdate_args *args;
133 	struct svc_req *svcreq;
134 {
135 	struct authdes_cred *des_cred;
136 	static int res;
137 	char *netname;
138 	enum auth_stat astat;
139 
140 	res = 0;
141 
142 	astat = yp_checkauth(svcreq);
143 
144 	if (astat != AUTH_OK) {
145 		svcerr_auth(svcreq->rq_xprt, astat);
146 		return(&res);
147 	}
148 
149 	des_cred = (struct authdes_cred *) svcreq->rq_clntcred;
150 	netname = des_cred->adc_fullname.name;
151 
152 	res = localupdate(netname, "/etc/publickey", YPOP_INSERT,
153 		args->key.yp_buf_len, args->key.yp_buf_val,
154 		args->datum.yp_buf_len, args->datum.yp_buf_val);
155 
156 	if (res)
157 		return (&res);
158 
159 	res = ypmap_update(netname, args->mapname, YPOP_INSERT,
160 		args->key.yp_buf_len, args->key.yp_buf_val,
161 		args->datum.yp_buf_len, args->datum.yp_buf_val);
162 
163 	return (&res);
164 }
165 
166 unsigned int *ypu_delete_1_svc(args, svcreq)
167 	struct ypdelete_args *args;
168 	struct svc_req *svcreq;
169 {
170 	struct authdes_cred *des_cred;
171 	static int res;
172 	char *netname;
173 	enum auth_stat astat;
174 
175 	res = 0;
176 
177 	astat = yp_checkauth(svcreq);
178 
179 	if (astat != AUTH_OK) {
180 		svcerr_auth(svcreq->rq_xprt, astat);
181 		return(&res);
182 	}
183 
184 	des_cred = (struct authdes_cred *) svcreq->rq_clntcred;
185 	netname = des_cred->adc_fullname.name;
186 
187 	res = localupdate(netname, "/etc/publickey", YPOP_DELETE,
188 		args->key.yp_buf_len, args->key.yp_buf_val,
189 		0,			NULL);
190 
191 	if (res)
192 		return (&res);
193 
194 	res = ypmap_update(netname, args->mapname, YPOP_DELETE,
195 		args->key.yp_buf_len, args->key.yp_buf_val,
196 		0,			NULL);
197 
198 	return (&res);
199 }
200 
201 unsigned int *ypu_store_1_svc(args, svcreq)
202 	struct ypupdate_args *args;
203 	struct svc_req *svcreq;
204 {
205 	struct authdes_cred *des_cred;
206 	static int res;
207 	char *netname;
208 	enum auth_stat astat;
209 
210 	res = 0;
211 
212 	astat = yp_checkauth(svcreq);
213 
214 	if (astat != AUTH_OK) {
215 		svcerr_auth(svcreq->rq_xprt, astat);
216 		return(&res);
217 	}
218 
219 	des_cred = (struct authdes_cred *) svcreq->rq_clntcred;
220 	netname = des_cred->adc_fullname.name;
221 
222 	res = localupdate(netname, "/etc/publickey", YPOP_STORE,
223 		args->key.yp_buf_len, args->key.yp_buf_val,
224 		args->datum.yp_buf_len, args->datum.yp_buf_val);
225 
226 	if (res)
227 		return (&res);
228 
229 	res = ypmap_update(netname, args->mapname, YPOP_STORE,
230 		args->key.yp_buf_len, args->key.yp_buf_val,
231 		args->datum.yp_buf_len, args->datum.yp_buf_val);
232 
233 	return (&res);
234 }
235