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