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