3 * $Id: check.c,v 1.6 1998/01/12 16:45:47 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.6 1998/01/12 16:45:47 mdw
35 * Revision 1.5 1997/09/26 09:14:58 mdw
36 * Merged blowfish branch into trunk.
38 * Revision 1.4.2.1 1997/09/26 09:08:01 mdw
39 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
40 * because I prefer Blowfish (without any particularly strong evidence) but
41 * mainly because IDEA is patented and Blowfish isn't.
43 * Revision 1.4 1997/08/07 09:52:05 mdw
44 * (Log entry for previous version is bogus.) Added support for multiple
47 * Revision 1.2 1997/08/04 10:24:20 mdw
48 * Sources placed under CVS control.
50 * Revision 1.1 1997/07/21 13:47:53 mdw
55 /*----- Header files ------------------------------------------------------*/
57 /* --- ANSI headers --- */
66 /* --- Unix headers --- */
69 #include <sys/types.h>
70 #include <sys/socket.h>
72 #include <netinet/in.h>
74 #include <arpa/inet.h>
79 /* --- Local headers --- */
94 /*----- Main code ---------------------------------------------------------*/
96 /* --- @check__send@ --- *
98 * Arguments: @unsigned char *crq@ = pointer to encrypted request
99 * @int fd@ = socket to send from
100 * @struct sockaddr_in *serv@ = pointer to table of servers
101 * @size_t n_serv@ = number of servers
105 * Use: Sends the request packet to the list of servers. If the
106 * message couldn't be sent to any of them, an error is
110 static void check__send(unsigned char *crq, int fd,
111 struct sockaddr_in *serv, size_t n_serv)
117 for (i = 0; i < n_serv; i++) {
118 if (sendto(fd, (char *)crq, crq_size, 0,
119 (struct sockaddr *)(serv + i), sizeof(serv[i])) < 0) {
120 T( trace(TRACE_CLIENT, "client: send to %s failed: %s",
121 inet_ntoa(serv[i].sin_addr), strerror(errno)); )
128 die("couldn't send request to server: %s", strerror(err));
131 /* --- @check__ask@ --- *
133 * Arguments: @request *rq@ = pointer to request buffer
134 * @struct sockaddr_in *serv@ = pointer to table of servers
135 * @size_t n_serv@ = number of servers
137 * Returns: Nonzero if OK, zero if forbidden
139 * Use: Contacts a number of servers to decide whether the request
143 static int check__ask(request *rq, struct sockaddr_in *serv, size_t n_serv)
146 unsigned char crq[crq_size];
147 unsigned char sk[BLOWFISH_KEYSIZE];
151 /* --- First, build the encrypted request packet --- */
154 unsigned char k[BLOWFISH_KEYSIZE];
157 /* --- Read in the encryption key --- */
159 if ((fp = fopen(file_KEY, "r")) == 0) {
160 die("couldn't open key file `%s': %s", file_KEY,
163 tx_getBits(k, 128, fp);
165 /* --- Now build a request packet --- */
169 crypt_packRequest(rq, crq, t, pid, k, sk);
171 T( trace(TRACE_CLIENT, "client: encrypted request packet"); )
174 /* --- Create my socket --- */
177 struct sockaddr_in sin;
179 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
180 die("couldn't create socket: %s", strerror(errno));
182 /* --- Bind myself to some address --- */
184 sin.sin_family = AF_INET;
186 sin.sin_addr.s_addr = htonl(INADDR_ANY);
188 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
189 die("couldn't bind socket to address: %s", strerror(errno));
192 /* --- Now wait for a reply --- */
196 struct timeval start, now, tv;
200 /* --- State table for waiting for replies --- *
202 * For each number, send off the request to our servers, and wait for
203 * that many seconds to have elapsed since we started. If the number is
204 * %$-1$% then it's time to give up.
207 static int tbl[] = { 0, 5, 10, 20, -1 };
209 /* --- Find out when we are --- */
211 gettimeofday(&start, 0);
214 /* --- Now loop until everything's done --- */
217 gettimeofday(&now, 0);
219 /* --- If the current timer has expired, find one that hasn't --- *
221 * Also resend the request after I've found a timer which is still
222 * extant. If there aren't any, report an error.
225 if (now.tv_sec >= start.tv_sec + tbl[ind] &&
226 now.tv_usec >= start.tv_usec) {
230 die("no reply from servers");
231 } while (now.tv_sec >= start.tv_sec + tbl[ind] &&
232 now.tv_usec >= start.tv_usec);
233 check__send(crq, fd, serv, n_serv);
234 T( trace(TRACE_CLIENT, "client: send request to servers"); )
237 /* --- Now wait for a packet to arrive --- */
239 if (now.tv_usec > start.tv_usec) {
240 now.tv_usec -= 1000000;
243 tv.tv_sec = start.tv_sec + tbl[ind] - now.tv_sec;
244 tv.tv_usec = start.tv_usec - now.tv_usec;
246 /* --- Sort out file descriptors to watch --- */
251 /* --- Wait for them --- */
253 i = select(FD_SETSIZE, &fds, 0, 0, &tv);
254 if (i == 0 || (i < 0 && errno == EINTR))
257 die("error waiting for reply: %s", strerror(errno));
259 /* --- A reply should be waiting now --- */
262 struct sockaddr_in sin;
263 int slen = sizeof(sin);
264 unsigned char buff[256];
267 /* --- Read the reply data --- */
269 if (recvfrom(fd, (char *)buff, sizeof(buff), 0,
270 (struct sockaddr *)&sin, &slen) < 0)
271 die("error reading server's reply: %s", strerror(errno));
273 IF_TRACING(TRACE_CLIENT, {
274 struct hostent *h = gethostbyaddr((char *)&sin.sin_addr,
275 sizeof(sin.sin_addr), AF_INET);
276 trace(TRACE_CLIENT, "client: reply received from %s port %i",
277 h ? h->h_name : inet_ntoa(sin.sin_addr),
278 ntohs(sin.sin_port));
281 /* --- Verify the sender --- *
283 * This is more to avoid confusion than for security: an active
284 * attacker is quite capable of forging the source address. We rely
285 * on the checksum in the reply packet for authentication.
288 for (i = 0; i < n_serv; i++) {
289 if (sin.sin_addr.s_addr == serv[i].sin_addr.s_addr &&
290 sin.sin_port == serv[i].sin_port)
294 T( trace(TRACE_CLIENT, "client: reply from unknown host"); )
298 /* --- Unpack and verify the response --- */
300 answer = crypt_unpackReply(buff, sk, t, pid);
302 T( trace(TRACE_CLIENT,
303 "client: invalid or corrupt reply packet"); )
311 die("internal error: can't get here in check__ask");
315 /* --- @check__client@ --- *
317 * Arguments: @request *rq@ = pointer to a request block
318 * @FILE *fp@ = file containing server configuration
320 * Returns: Nonzero if OK, zero if forbidden
322 * Use: Asks one or several servers whether a request is acceptable.
325 int check__client(request *rq, FILE *fp)
327 /* --- Format of the file --- *
329 * The `servers' file contains entries of the form
331 * %%\syntax{<host> [`:' <port>]}%%
333 * separates by whitespace. I build them all into an array of socket
334 * addresses and pass the whole lot to another function.
337 struct sockaddr_in *serv; /* Array of servers */
338 size_t n_serv, max_serv; /* Number and maximum number */
340 /* --- Initialise the server array --- */
342 T( trace(TRACE_CLIENT, "client: reading server definitions"); )
343 n_serv = 0; max_serv = 4; /* Four seems reasonable */
344 serv = xmalloc(sizeof(*serv) * max_serv);
346 /* --- Start reading the file --- */
349 char buff[256], *p, *l; /* A buffer and pointers for it */
350 int port; /* Default port for servers */
351 int state; /* Current parser state */
352 struct in_addr t_host; /* Temp place for an address*/
353 int t_port; /* Temp place for a port */
354 int ch; /* The current character */
356 /* --- Parser states --- */
359 st_start, /* Waiting to begin */
360 st_host, /* Reading a new hostname */
361 st_colon, /* Expecting a colon, maybe */
362 st_preport, /* Waiting before reading port */
363 st_port, /* Reading a port number */
364 st_commit, /* Commit a newly read server */
365 st_done /* Finished reading the file */
368 /* --- Find a default port --- */
371 struct servent *s = getservbyname(quis(), "udp");
372 port = (s ? s->s_port : -1);
375 /* --- Initialise for scanning the file --- */
379 l = buff + sizeof(buff);
383 /* --- Go for it --- */
385 while (state != st_done) {
388 /* --- Initial whitespace before hostname --- */
393 else if (isspace((unsigned char)ch))
399 /* --- Read a host name --- */
403 die("string too long in `" file_SERVER "'");
404 if (ch != EOF && !isspace((unsigned char)ch) && ch != ':') {
411 if ((h = gethostbyname(buff)) == 0)
412 die("unknown host `%s' in `" file_SERVER "'", buff);
413 memcpy(&t_host, h->h_addr, sizeof(t_host));
418 /* --- Waiting for a colon coming up --- */
423 else if (isspace((unsigned char)ch))
425 else if (ch == ':') {
433 /* --- Clearing whitespace before a port number --- */
438 else if (isspace((unsigned char)ch))
446 /* --- Read a port number --- */
450 die("string too long in `" file_SERVER "'");
451 if (ch != EOF && !isspace((unsigned char)ch) && ch != ':') {
458 s = getservbyname(buff, "udp");
459 if (!s && isdigit((unsigned char)buff[0]))
460 t_port = htons(atoi(buff));
462 die("unknown service `%s' in `" file_SERVER "'", buff);
469 /* --- A server has been read successfully --- */
472 if (n_serv == max_serv) {
474 serv = xrealloc(serv, max_serv * sizeof(*serv));
476 serv[n_serv].sin_family = AF_INET;
477 serv[n_serv].sin_addr = t_host;
478 serv[n_serv].sin_port = t_port;
485 /* --- A safety net for a broken parser --- */
488 die("internal error: can't get here in check__client");
496 /* --- Now start sending requests --- */
499 die("no servers specified in `" file_SERVER "'");
501 IF_TRACING(TRACE_CLIENT, {
504 for (i = 0; i < n_serv; i++) {
505 trace(TRACE_CLIENT, "client: server %s port %i",
506 inet_ntoa(serv[i].sin_addr), ntohs(serv[i].sin_port));
509 return (check__ask(rq, serv, n_serv));
514 * Arguments: @request *rq@ = pointer to request buffer
516 * Returns: Nonzero if OK, zero if forbidden
518 * Use: Checks to see if the request is acceptable.
521 int check(request *rq)
525 /* --- Check if we need to talk to a server --- */
527 if ((fp = fopen(file_SERVER, "r")) != 0)
528 return (check__client(rq, fp));
530 /* --- Otherwise do this all the old-fashioned way --- */
532 if ((fp = fopen(file_RULES, "r")) == 0) {
533 die("couldn't read configuration file `%s': %s",
534 file_RULES, strerror(errno));
546 return (rule_check(rq));
549 /*----- That's all, folks -------------------------------------------------*/