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