3 * $Id: check.c,v 1.9 1998/06/19 13:48:16 mdw Exp $
5 * Check validity of requests
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
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.
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.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.9 1998/06/19 13:48:16 mdw
33 * Set close-on-exec flag for UDP socket.
35 * Revision 1.8 1998/06/18 15:10:44 mdw
36 * SECURITY HOLE: the file descriptor for the secret key was left open and
37 * inherited by the target process. This is now fixed. Also set
38 * close-on-exec flags on key file, close config file carefully, and close
39 * UDP socket after receiving reply from server.
41 * Revision 1.7 1998/04/23 13:22:08 mdw
42 * Support no-network configuration option, and new interface to
43 * configuration file parser.
45 * Revision 1.6 1998/01/12 16:45:47 mdw
48 * Revision 1.5 1997/09/26 09:14:58 mdw
49 * Merged blowfish branch into trunk.
51 * Revision 1.4.2.1 1997/09/26 09:08:01 mdw
52 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
53 * because I prefer Blowfish (without any particularly strong evidence) but
54 * mainly because IDEA is patented and Blowfish isn't.
56 * Revision 1.4 1997/08/07 09:52:05 mdw
57 * (Log entry for previous version is bogus.) Added support for multiple
60 * Revision 1.2 1997/08/04 10:24:20 mdw
61 * Sources placed under CVS control.
63 * Revision 1.1 1997/07/21 13:47:53 mdw
68 /*----- Header files ------------------------------------------------------*/
70 /* --- ANSI headers --- */
79 /* --- Unix headers --- */
82 #include <sys/types.h>
83 #include <sys/socket.h>
85 #include <netinet/in.h>
87 #include <arpa/inet.h>
93 /* --- Local headers --- */
108 /*----- Client-end network support ----------------------------------------*/
112 /* --- @check__send@ --- *
114 * Arguments: @unsigned char *crq@ = pointer to encrypted request
115 * @int fd@ = socket to send from
116 * @struct sockaddr_in *serv@ = pointer to table of servers
117 * @size_t n_serv@ = number of servers
121 * Use: Sends the request packet to the list of servers. If the
122 * message couldn't be sent to any of them, an error is
126 static void check__send(unsigned char *crq, int fd,
127 struct sockaddr_in *serv, size_t n_serv)
133 for (i = 0; i < n_serv; i++) {
134 if (sendto(fd, (char *)crq, crq_size, 0,
135 (struct sockaddr *)(serv + i), sizeof(serv[i])) < 0) {
136 T( trace(TRACE_CLIENT, "client: send to %s failed: %s",
137 inet_ntoa(serv[i].sin_addr), strerror(errno)); )
144 die("couldn't send request to server: %s", strerror(err));
147 /* --- @check__ask@ --- *
149 * Arguments: @request *rq@ = pointer to request buffer
150 * @struct sockaddr_in *serv@ = pointer to table of servers
151 * @size_t n_serv@ = number of servers
153 * Returns: Nonzero if OK, zero if forbidden
155 * Use: Contacts a number of servers to decide whether the request
159 static int check__ask(request *rq, struct sockaddr_in *serv, size_t n_serv)
162 unsigned char crq[crq_size];
163 unsigned char sk[BLOWFISH_KEYSIZE];
167 /* --- First, build the encrypted request packet --- */
170 unsigned char k[BLOWFISH_KEYSIZE];
173 /* --- Read in the encryption key --- */
175 if ((fp = fopen(file_KEY, "r")) == 0) {
176 die("couldn't open key file `%s': %s", file_KEY,
179 if (fcntl(fileno(fp), F_SETFD, 1) < 0) {
180 die("couldn't set close-on-exec on key file `%s': %s", file_KEY,
183 tx_getBits(k, 128, fp);
186 /* --- Now build a request packet --- */
190 crypt_packRequest(rq, crq, t, pid, k, sk);
192 T( trace(TRACE_CLIENT, "client: encrypted request packet"); )
195 /* --- Create my socket --- */
198 struct sockaddr_in sin;
200 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
201 die("couldn't create socket: %s", strerror(errno));
202 if (fcntl(fd, F_SETFD, 1) < 0)
203 die("couldn't set close-on-exec flag for socket: %s", strerror(errno));
205 /* --- Bind myself to some address --- */
207 sin.sin_family = AF_INET;
209 sin.sin_addr.s_addr = htonl(INADDR_ANY);
211 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
212 die("couldn't bind socket to address: %s", strerror(errno));
215 /* --- Now wait for a reply --- */
219 struct timeval start, now, tv;
223 /* --- State table for waiting for replies --- *
225 * For each number, send off the request to our servers, and wait for
226 * that many seconds to have elapsed since we started. If the number is
227 * %$-1$% then it's time to give up.
230 static int tbl[] = { 0, 5, 10, 20, -1 };
232 /* --- Find out when we are --- */
234 gettimeofday(&start, 0);
237 /* --- Now loop until everything's done --- */
240 gettimeofday(&now, 0);
242 /* --- If the current timer has expired, find one that hasn't --- *
244 * Also resend the request after I've found a timer which is still
245 * extant. If there aren't any, report an error.
248 if (now.tv_sec >= start.tv_sec + tbl[ind] &&
249 now.tv_usec >= start.tv_usec) {
253 die("no reply from servers");
254 } while (now.tv_sec >= start.tv_sec + tbl[ind] &&
255 now.tv_usec >= start.tv_usec);
256 check__send(crq, fd, serv, n_serv);
257 T( trace(TRACE_CLIENT, "client: send request to servers"); )
260 /* --- Now wait for a packet to arrive --- */
262 if (now.tv_usec > start.tv_usec) {
263 now.tv_usec -= 1000000;
266 tv.tv_sec = start.tv_sec + tbl[ind] - now.tv_sec;
267 tv.tv_usec = start.tv_usec - now.tv_usec;
269 /* --- Sort out file descriptors to watch --- */
274 /* --- Wait for them --- */
276 i = select(FD_SETSIZE, &fds, 0, 0, &tv);
277 if (i == 0 || (i < 0 && errno == EINTR))
280 die("error waiting for reply: %s", strerror(errno));
282 /* --- A reply should be waiting now --- */
285 struct sockaddr_in sin;
286 int slen = sizeof(sin);
287 unsigned char buff[256];
290 /* --- Read the reply data --- */
292 if (recvfrom(fd, (char *)buff, sizeof(buff), 0,
293 (struct sockaddr *)&sin, &slen) < 0)
294 die("error reading server's reply: %s", strerror(errno));
296 IF_TRACING(TRACE_CLIENT, {
297 struct hostent *h = gethostbyaddr((char *)&sin.sin_addr,
298 sizeof(sin.sin_addr), AF_INET);
299 trace(TRACE_CLIENT, "client: reply received from %s port %i",
300 h ? h->h_name : inet_ntoa(sin.sin_addr),
301 ntohs(sin.sin_port));
304 /* --- Verify the sender --- *
306 * This is more to avoid confusion than for security: an active
307 * attacker is quite capable of forging the source address. We rely
308 * on the checksum in the reply packet for authentication.
311 for (i = 0; i < n_serv; i++) {
312 if (sin.sin_addr.s_addr == serv[i].sin_addr.s_addr &&
313 sin.sin_port == serv[i].sin_port)
317 T( trace(TRACE_CLIENT, "client: reply from unknown host"); )
321 /* --- Unpack and verify the response --- */
323 answer = crypt_unpackReply(buff, sk, t, pid);
325 T( trace(TRACE_CLIENT,
326 "client: invalid or corrupt reply packet"); )
335 die("internal error: can't get here in check__ask");
339 /* --- @check__client@ --- *
341 * Arguments: @request *rq@ = pointer to a request block
342 * @FILE *fp@ = file containing server configuration
344 * Returns: Nonzero if OK, zero if forbidden
346 * Use: Asks one or several servers whether a request is acceptable.
349 int check__client(request *rq, FILE *fp)
351 /* --- Format of the file --- *
353 * The `servers' file contains entries of the form
355 * %%\syntax{<host> [`:' <port>]}%%
357 * separates by whitespace. I build them all into an array of socket
358 * addresses and pass the whole lot to another function.
361 struct sockaddr_in *serv; /* Array of servers */
362 size_t n_serv, max_serv; /* Number and maximum number */
364 /* --- Initialise the server array --- */
366 T( trace(TRACE_CLIENT, "client: reading server definitions"); )
367 n_serv = 0; max_serv = 4; /* Four seems reasonable */
368 serv = xmalloc(sizeof(*serv) * max_serv);
370 /* --- Start reading the file --- */
373 char buff[256], *p, *l; /* A buffer and pointers for it */
374 int port; /* Default port for servers */
375 int state; /* Current parser state */
376 struct in_addr t_host; /* Temp place for an address*/
377 int t_port; /* Temp place for a port */
378 int ch; /* The current character */
380 /* --- Parser states --- */
383 st_start, /* Waiting to begin */
384 st_host, /* Reading a new hostname */
385 st_colon, /* Expecting a colon, maybe */
386 st_preport, /* Waiting before reading port */
387 st_port, /* Reading a port number */
388 st_commit, /* Commit a newly read server */
389 st_done /* Finished reading the file */
392 /* --- Find a default port --- */
395 struct servent *s = getservbyname(quis(), "udp");
396 port = (s ? s->s_port : -1);
399 /* --- Initialise for scanning the file --- */
403 l = buff + sizeof(buff);
407 /* --- Go for it --- */
409 while (state != st_done) {
412 /* --- Initial whitespace before hostname --- */
417 else if (isspace((unsigned char)ch))
423 /* --- Read a host name --- */
427 die("string too long in `" file_SERVER "'");
428 if (ch != EOF && !isspace((unsigned char)ch) && ch != ':') {
435 if ((h = gethostbyname(buff)) == 0)
436 die("unknown host `%s' in `" file_SERVER "'", buff);
437 memcpy(&t_host, h->h_addr, sizeof(t_host));
442 /* --- Waiting for a colon coming up --- */
447 else if (isspace((unsigned char)ch))
449 else if (ch == ':') {
457 /* --- Clearing whitespace before a port number --- */
462 else if (isspace((unsigned char)ch))
470 /* --- Read a port number --- */
474 die("string too long in `" file_SERVER "'");
475 if (ch != EOF && !isspace((unsigned char)ch) && ch != ':') {
482 s = getservbyname(buff, "udp");
483 if (!s && isdigit((unsigned char)buff[0]))
484 t_port = htons(atoi(buff));
486 die("unknown service `%s' in `" file_SERVER "'", buff);
493 /* --- A server has been read successfully --- */
496 if (n_serv == max_serv) {
498 serv = xrealloc(serv, max_serv * sizeof(*serv));
500 serv[n_serv].sin_family = AF_INET;
501 serv[n_serv].sin_addr = t_host;
502 serv[n_serv].sin_port = t_port;
509 /* --- A safety net for a broken parser --- */
512 die("internal error: can't get here in check__client");
520 /* --- Now start sending requests --- */
523 die("no servers specified in `" file_SERVER "'");
525 IF_TRACING(TRACE_CLIENT, {
528 for (i = 0; i < n_serv; i++) {
529 trace(TRACE_CLIENT, "client: server %s port %i",
530 inet_ntoa(serv[i].sin_addr), ntohs(serv[i].sin_port));
533 return (check__ask(rq, serv, n_serv));
538 /*----- Main checking function --------------------------------------------*/
542 * Arguments: @request *rq@ = pointer to request buffer
544 * Returns: Nonzero if OK, zero if forbidden
546 * Use: Checks to see if the request is acceptable.
549 int check(request *rq)
553 /* --- Check if we need to talk to a server --- */
556 if ((fp = fopen(file_SERVER, "r")) != 0)
557 return (check__client(rq, fp));
560 /* --- Otherwise do this all the old-fashioned way --- */
562 if ((fp = fopen(file_RULES, "r")) == 0) {
563 die("couldn't read configuration file `%s': %s",
564 file_RULES, strerror(errno));
577 return (rule_check(rq));
580 /*----- That's all, folks -------------------------------------------------*/