chiark / gitweb /
Major overhaul. Now uses DSA signatures rather than the bogus symmetric
[become] / src / check.c
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
f60a3434 3 * $Id: check.c,v 1.11 2003/10/12 00:14:55 mdw Exp $
c4f2d992 4 *
5 * Check validity of requests
6 *
c758e654 7 * (c) 1998 EBI
c4f2d992 8 */
9
03f996bd 10/*----- Licensing notice --------------------------------------------------*
c4f2d992 11 *
12 * This file is part of `become'
13 *
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
03f996bd 25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
c4f2d992 27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: check.c,v $
f60a3434 32 * Revision 1.11 2003/10/12 00:14:55 mdw
33 * Major overhaul. Now uses DSA signatures rather than the bogus symmetric
34 * encrypt-and-hope thing. Integrated with mLib and Catacomb.
35 *
79fae27d 36 * Revision 1.10 1999/05/04 16:17:12 mdw
37 * Change to header file name for parser. See log for `parse.h' for
38 * details.
39 *
40 * Revision 1.9 1998/06/19 13:48:16 mdw
9739ca83 41 * Set close-on-exec flag for UDP socket.
42 *
ff2d3282 43 * Revision 1.8 1998/06/18 15:10:44 mdw
44 * SECURITY HOLE: the file descriptor for the secret key was left open and
45 * inherited by the target process. This is now fixed. Also set
46 * close-on-exec flags on key file, close config file carefully, and close
47 * UDP socket after receiving reply from server.
48 *
318c0b91 49 * Revision 1.7 1998/04/23 13:22:08 mdw
50 * Support no-network configuration option, and new interface to
51 * configuration file parser.
52 *
c758e654 53 * Revision 1.6 1998/01/12 16:45:47 mdw
54 * Fix copyright date.
55 *
9e5602f0 56 * Revision 1.5 1997/09/26 09:14:58 mdw
57 * Merged blowfish branch into trunk.
58 *
59 * Revision 1.4.2.1 1997/09/26 09:08:01 mdw
60 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
61 * because I prefer Blowfish (without any particularly strong evidence) but
62 * mainly because IDEA is patented and Blowfish isn't.
63 *
27d7bfc2 64 * Revision 1.4 1997/08/07 09:52:05 mdw
65 * (Log entry for previous version is bogus.) Added support for multiple
66 * servers.
607937a4 67 *
03f996bd 68 * Revision 1.2 1997/08/04 10:24:20 mdw
69 * Sources placed under CVS control.
70 *
71 * Revision 1.1 1997/07/21 13:47:53 mdw
c4f2d992 72 * Initial revision
73 *
74 */
75
76/*----- Header files ------------------------------------------------------*/
77
78/* --- ANSI headers --- */
79
80#include <ctype.h>
81#include <errno.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85#include <time.h>
86
87/* --- Unix headers --- */
88
89#include <sys/time.h>
90#include <sys/types.h>
91#include <sys/socket.h>
92
93#include <netinet/in.h>
94
95#include <arpa/inet.h>
96
ff2d3282 97#include <fcntl.h>
c4f2d992 98#include <netdb.h>
99#include <unistd.h>
100
f60a3434 101/* --- mLib headers --- */
102
103#include <mLib/alloc.h>
104#include <mLib/quis.h>
105#include <mLib/report.h>
106#include <mLib/sym.h>
107#include <mLib/trace.h>
108
109/* --- Catacomb headers --- */
110
111#include <catacomb/buf.h>
112#include <catacomb/dsa.h>
113#include <catacomb/key.h>
114#include <catacomb/mp.h>
115#include <catacomb/noise.h>
116#include <catacomb/rand.h>
117#include <catacomb/sha.h>
118
c4f2d992 119/* --- Local headers --- */
120
121#include "become.h"
122#include "config.h"
c4f2d992 123#include "lexer.h"
124#include "name.h"
607937a4 125#include "netg.h"
c4f2d992 126#include "rule.h"
79fae27d 127#include "parse.h"
03f996bd 128#include "userdb.h"
c4f2d992 129
318c0b91 130/*----- Client-end network support ----------------------------------------*/
131
132#ifndef NONETWORK
c4f2d992 133
03f996bd 134/* --- @check__send@ --- *
135 *
f60a3434 136 * Arguments: @char *buf@ = pointer to encrypted request
137 * @size_t sz@ = size of request
03f996bd 138 * @int fd@ = socket to send from
139 * @struct sockaddr_in *serv@ = pointer to table of servers
140 * @size_t n_serv@ = number of servers
141 *
142 * Returns: ---
143 *
144 * Use: Sends the request packet to the list of servers. If the
145 * message couldn't be sent to any of them, an error is
146 * reported.
147 */
148
f60a3434 149static void check__send(char *buf, size_t sz, int fd,
03f996bd 150 struct sockaddr_in *serv, size_t n_serv)
151{
152 size_t i;
153 int ok = 0;
154 int err = 0;
155
156 for (i = 0; i < n_serv; i++) {
f60a3434 157 if (sendto(fd, buf, sz, 0,
03f996bd 158 (struct sockaddr *)(serv + i), sizeof(serv[i])) < 0) {
159 T( trace(TRACE_CLIENT, "client: send to %s failed: %s",
160 inet_ntoa(serv[i].sin_addr), strerror(errno)); )
161 err = errno;
162 } else
163 ok = 1;
164 }
165
166 if (!ok)
f60a3434 167 die(1, "couldn't send request to server: %s", strerror(err));
03f996bd 168}
169
170/* --- @check__ask@ --- *
c4f2d992 171 *
172 * Arguments: @request *rq@ = pointer to request buffer
03f996bd 173 * @struct sockaddr_in *serv@ = pointer to table of servers
174 * @size_t n_serv@ = number of servers
c4f2d992 175 *
176 * Returns: Nonzero if OK, zero if forbidden
177 *
03f996bd 178 * Use: Contacts a number of servers to decide whether the request
179 * is OK.
c4f2d992 180 */
181
03f996bd 182static int check__ask(request *rq, struct sockaddr_in *serv, size_t n_serv)
c4f2d992 183{
f60a3434 184 static int tbl[] = { 0, 5, 10, 20, -1 };
185
186 char buff[2048], rbuff[2048];
187 size_t rqlen;
188 octet hmsg[SHA_HASHSZ];
189 octet h[SHA_HASHSZ];
190 key_packstruct kps[DSA_PUBFETCHSZ];
191 key_packdef *kp;
192 dsa_pub kpub;
193 buf b;
194 sha_ctx hc;
c4f2d992 195 int fd;
f60a3434 196 struct sockaddr_in sin;
197 socklen_t slen;
198 ssize_t sz;
199 int ans;
200 fd_set fds;
201 struct timeval start, now, tv;
202 mp *m, *r, *s;
203 key_file f;
204 key *k;
205 key_iter ki;
206 int ind;
207 size_t i;
c4f2d992 208
f60a3434 209 /* --- Open the public keyring --- */
c4f2d992 210
f60a3434 211 if ((key_open(&f, file_PUBKEY, KOPEN_READ, key_moan, 0)) != 0)
212 die(1, "couldn't open public keyring");
213 kp = key_fetchinit(dsa_pubfetch, kps, &kpub);
c4f2d992 214
f60a3434 215 /* --- Build the request packet --- */
c4f2d992 216
f60a3434 217 rand_noisesrc(RAND_GLOBAL, &noise_source);
218 rand_seed(RAND_GLOBAL, 160);
219 buf_init(&b, buff, sizeof(buff));
220 rand_get(RAND_GLOBAL, buf_get(&b, SHA_HASHSZ), SHA_HASHSZ);
221 buf_putu32(&b, rq->from);
222 buf_putu32(&b, rq->to);
223 buf_putu16(&b, strlen(rq->cmd));
224 buf_put(&b, rq->cmd, strlen(rq->cmd));
225 rqlen = BLEN(&b);
226 sha_init(&hc);
227 sha_hash(&hc, buff, rqlen);
228 sha_done(&hc, hmsg);
c4f2d992 229
03f996bd 230 /* --- Create my socket --- */
c4f2d992 231
f60a3434 232 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
233 die(1, "couldn't create socket: %s", strerror(errno));
234 if (fcntl(fd, F_SETFD, 1) < 0)
235 die(1, "couldn't set close-on-exec flag for socket: %s", strerror(errno));
c4f2d992 236
f60a3434 237 /* --- Bind myself to some address --- */
c4f2d992 238
f60a3434 239 sin.sin_family = AF_INET;
240 sin.sin_port = 0;
241 sin.sin_addr.s_addr = htonl(INADDR_ANY);
c4f2d992 242
f60a3434 243 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
244 die(1, "couldn't bind socket to address: %s", strerror(errno));
c4f2d992 245
f60a3434 246 /* --- Find out when we are --- */
c4f2d992 247
f60a3434 248 gettimeofday(&start, 0);
249 ind = 0;
c4f2d992 250
f60a3434 251 /* --- Now loop until everything's done --- */
252
253 for (;;) {
254 gettimeofday(&now, 0);
c4f2d992 255
f60a3434 256 /* --- If the current timer has expired, find one that hasn't --- *
03f996bd 257 *
f60a3434 258 * Also resend the request after I've found a timer which is still
259 * extant. If there aren't any, report an error.
03f996bd 260 */
c4f2d992 261
f60a3434 262 if (now.tv_sec >= start.tv_sec + tbl[ind] &&
263 now.tv_usec >= start.tv_usec) {
264 do {
265 ind++;
266 if (tbl[ind] < 0)
267 die(1, "no reply from servers");
268 } while (now.tv_sec >= start.tv_sec + tbl[ind] &&
269 now.tv_usec >= start.tv_usec);
270 check__send(buff, rqlen, fd, serv, n_serv);
271 T( trace(TRACE_CLIENT, "client: send request to servers"); )
272 }
273
274 /* --- Now wait for a packet to arrive --- */
c4f2d992 275
f60a3434 276 if (now.tv_usec > start.tv_usec) {
277 now.tv_usec -= 1000000;
278 now.tv_sec += 1;
279 }
280 tv.tv_sec = start.tv_sec + tbl[ind] - now.tv_sec;
281 tv.tv_usec = start.tv_usec - now.tv_usec;
c4f2d992 282
f60a3434 283 /* --- Sort out file descriptors to watch --- */
c4f2d992 284
f60a3434 285 FD_ZERO(&fds);
286 FD_SET(fd, &fds);
c4f2d992 287
f60a3434 288 /* --- Wait for them --- */
c4f2d992 289
f60a3434 290 i = select(FD_SETSIZE, &fds, 0, 0, &tv);
291 if (i == 0 || (i < 0 && errno == EINTR))
292 continue;
293 if (i < 0)
294 die(1, "error waiting for reply: %s", strerror(errno));
03f996bd 295
f60a3434 296 /* --- Read the reply data --- */
c4f2d992 297
f60a3434 298 slen = sizeof(sin);
299 if ((sz = recvfrom(fd, (char *)rbuff, sizeof(rbuff), 0,
300 (struct sockaddr *)&sin, &slen)) < 0)
301 die(1, "error reading server's reply: %s", strerror(errno));
03f996bd 302
f60a3434 303 IF_TRACING(TRACE_CLIENT, {
304 struct hostent *h = gethostbyaddr((char *)&sin.sin_addr,
305 sizeof(sin.sin_addr), AF_INET);
306 trace(TRACE_CLIENT, "client: reply received from %s port %i",
307 h ? h->h_name : inet_ntoa(sin.sin_addr),
308 ntohs(sin.sin_port));
309 })
310
311 /* --- Verify the sender --- *
312 *
313 * This is more to avoid confusion than for security: an active
314 * attacker is quite capable of forging the source address. We rely
315 * on the signature in the reply packet for authentication.
316 */
317
318 for (i = 0; i < n_serv; i++) {
319 if (sin.sin_addr.s_addr == serv[i].sin_addr.s_addr &&
320 sin.sin_port == serv[i].sin_port)
321 break;
322 }
323 if (i >= n_serv) {
324 T( trace(TRACE_CLIENT, "client: reply from unknown host"); )
325 continue;
326 }
327
328 /* --- Unpack and verify the response --- */
329
330 buf_init(&b, rbuff, sz);
331 if (buf_ensure(&b, sizeof(hmsg))) goto bad;
332 if (memcmp(BCUR(&b), hmsg, sizeof(hmsg)) != 0) goto bad;
333 BSTEP(&b, sizeof(hmsg));
334 if ((ans = buf_getbyte(&b)) < 0) goto bad;
335
336 sha_init(&hc);
337 sha_hash(&hc, BBASE(&b), BLEN(&b));
338 sha_done(&hc, h);
339 if ((r = buf_getmp(&b)) == 0 || (s = buf_getmp(&b)) == 0) goto bad;
340 m = mp_loadb(MP_NEW, h, sizeof(h));
341
342 key_mkiter(&ki, &f);
343 while ((k = key_next(&ki)) != 0) {
344 if (key_expired(k)) continue;
345 if (strcmp(k->type, "become-dsa") != 0) continue;
346 if (key_fetch(kp, k)) continue;
347 i = dsa_vrfy(&kpub.dp, kpub.y, m, r, s);
348 dsa_pubfree(&kpub);
349 if (i) {
350 key_fetchdone(kp);
351 key_close(&f);
ff2d3282 352 close(fd);
f60a3434 353 mp_drop(m);
354 mp_drop(r);
355 mp_drop(s);
356 return (ans);
c4f2d992 357 }
358 }
f60a3434 359
360 bad:
361 T( trace(TRACE_CLIENT,
362 "client: invalid or corrupt reply packet"); )
c4f2d992 363 }
364
f60a3434 365 die(1, "internal error: can't get here in check__ask");
c4f2d992 366 return (0);
367}
368
03f996bd 369/* --- @check__client@ --- *
370 *
371 * Arguments: @request *rq@ = pointer to a request block
372 * @FILE *fp@ = file containing server configuration
373 *
374 * Returns: Nonzero if OK, zero if forbidden
375 *
376 * Use: Asks one or several servers whether a request is acceptable.
377 */
378
379int check__client(request *rq, FILE *fp)
380{
381 /* --- Format of the file --- *
382 *
383 * The `servers' file contains entries of the form
384 *
385 * %%\syntax{<host> [`:' <port>]}%%
386 *
387 * separates by whitespace. I build them all into an array of socket
388 * addresses and pass the whole lot to another function.
389 */
390
391 struct sockaddr_in *serv; /* Array of servers */
392 size_t n_serv, max_serv; /* Number and maximum number */
393
394 /* --- Initialise the server array --- */
395
396 T( trace(TRACE_CLIENT, "client: reading server definitions"); )
397 n_serv = 0; max_serv = 4; /* Four seems reasonable */
398 serv = xmalloc(sizeof(*serv) * max_serv);
399
400 /* --- Start reading the file --- */
401
402 {
403 char buff[256], *p, *l; /* A buffer and pointers for it */
404 int port; /* Default port for servers */
405 int state; /* Current parser state */
406 struct in_addr t_host; /* Temp place for an address*/
407 int t_port; /* Temp place for a port */
408 int ch; /* The current character */
409
410 /* --- Parser states --- */
411
412 enum {
413 st_start, /* Waiting to begin */
414 st_host, /* Reading a new hostname */
415 st_colon, /* Expecting a colon, maybe */
416 st_preport, /* Waiting before reading port */
417 st_port, /* Reading a port number */
418 st_commit, /* Commit a newly read server */
419 st_done /* Finished reading the file */
420 };
421
422 /* --- Find a default port --- */
423
424 {
425 struct servent *s = getservbyname(quis(), "udp");
426 port = (s ? s->s_port : -1);
427 }
428
429 /* --- Initialise for scanning the file --- */
430
431 state = st_host;
432 p = buff;
433 l = buff + sizeof(buff);
434 t_port = port;
435 ch = getc(fp);
436
437 /* --- Go for it --- */
438
439 while (state != st_done) {
440 switch (state) {
441
442 /* --- Initial whitespace before hostname --- */
443
444 case st_start:
445 if (ch == EOF)
446 state = st_done;
447 else if (isspace((unsigned char)ch))
448 ch = getc(fp);
449 else
450 state = st_host;
451 break;
452
453 /* --- Read a host name --- */
454
455 case st_host:
456 if (p == l)
f60a3434 457 die(1, "string too long in `" file_SERVER "'");
03f996bd 458 if (ch != EOF && !isspace((unsigned char)ch) && ch != ':') {
459 *p++ = ch;
460 ch = getc(fp);
461 } else {
462 struct hostent *h;
463
464 *p++ = 0;
465 if ((h = gethostbyname(buff)) == 0)
f60a3434 466 die(1, "unknown host `%s' in `" file_SERVER "'", buff);
03f996bd 467 memcpy(&t_host, h->h_addr, sizeof(t_host));
468 state = st_colon;
469 }
470 break;
471
472 /* --- Waiting for a colon coming up --- */
473
474 case st_colon:
475 if (ch == EOF)
476 state = st_commit;
477 else if (isspace((unsigned char)ch))
478 ch = getc(fp);
479 else if (ch == ':') {
480 state = st_preport;
481 ch = getc(fp);
482 }
483 else
484 state = st_commit;
485 break;
486
487 /* --- Clearing whitespace before a port number --- */
488
489 case st_preport:
490 if (ch == EOF)
491 state = st_commit;
492 else if (isspace((unsigned char)ch))
493 ch = getc(fp);
494 else {
495 state = st_port;
496 p = buff;
497 }
498 break;
499
500 /* --- Read a port number --- */
501
502 case st_port:
503 if (p == l)
f60a3434 504 die(1, "string too long in `" file_SERVER "'");
03f996bd 505 if (ch != EOF && !isspace((unsigned char)ch) && ch != ':') {
506 *p++ = ch;
507 ch = getc(fp);
508 } else {
509 struct servent *s;
510
511 *p++ = 0;
512 s = getservbyname(buff, "udp");
513 if (!s && isdigit((unsigned char)buff[0]))
514 t_port = htons(atoi(buff));
515 else if (!s)
f60a3434 516 die(1, "unknown service `%s' in `" file_SERVER "'", buff);
03f996bd 517 else
518 t_port = s->s_port;
519 state = st_commit;
520 }
521 break;
522
523 /* --- A server has been read successfully --- */
524
525 case st_commit:
526 if (n_serv == max_serv) {
527 max_serv *= 2;
f60a3434 528 serv = xrealloc(serv, n_serv * sizeof(*serv),
529 max_serv * sizeof(*serv));
03f996bd 530 }
531 serv[n_serv].sin_family = AF_INET;
532 serv[n_serv].sin_addr = t_host;
533 serv[n_serv].sin_port = t_port;
534 n_serv++;
535 state = st_start;
536 p = buff;
537 t_port = port;
538 break;
539
540 /* --- A safety net for a broken parser --- */
541
542 default:
f60a3434 543 die(1, "internal error: can't get here in check__client");
03f996bd 544 break;
545 }
546 }
547 }
548
549 fclose(fp);
550
551 /* --- Now start sending requests --- */
552
553 if (!n_serv)
f60a3434 554 die(1, "no servers specified in `" file_SERVER "'");
03f996bd 555
556 IF_TRACING(TRACE_CLIENT, {
557 size_t i;
558
559 for (i = 0; i < n_serv; i++) {
560 trace(TRACE_CLIENT, "client: server %s port %i",
561 inet_ntoa(serv[i].sin_addr), ntohs(serv[i].sin_port));
562 }
563 })
564 return (check__ask(rq, serv, n_serv));
565}
566
318c0b91 567#endif
568
569/*----- Main checking function --------------------------------------------*/
570
c4f2d992 571/* --- @check@ --- *
572 *
573 * Arguments: @request *rq@ = pointer to request buffer
574 *
575 * Returns: Nonzero if OK, zero if forbidden
576 *
577 * Use: Checks to see if the request is acceptable.
578 */
579
580int check(request *rq)
581{
582 FILE *fp;
583
584 /* --- Check if we need to talk to a server --- */
585
318c0b91 586#ifndef NONETWORK
03f996bd 587 if ((fp = fopen(file_SERVER, "r")) != 0)
588 return (check__client(rq, fp));
318c0b91 589#endif
c4f2d992 590
03f996bd 591 /* --- Otherwise do this all the old-fashioned way --- */
c4f2d992 592
593 if ((fp = fopen(file_RULES, "r")) == 0) {
f60a3434 594 die(1, "couldn't read configuration file `%s': %s",
c4f2d992 595 file_RULES, strerror(errno));
596 }
597
03f996bd 598 userdb_init();
599 userdb_local();
600 userdb_yp();
607937a4 601 netg_init();
c4f2d992 602 name_init();
603 rule_init();
604 lexer_scan(fp);
318c0b91 605 parse();
ff2d3282 606 fclose(fp);
c4f2d992 607
608 return (rule_check(rq));
609}
610
611/*----- That's all, folks -------------------------------------------------*/