c8991965ec7363bba5c2e92541bee81c68878282
3 * $Id: check.c,v 1.13 2004/04/08 01:36:20 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 /*----- Header files ------------------------------------------------------*/
31 /* --- ANSI headers --- */
40 /* --- Unix headers --- */
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
48 #include <arpa/inet.h>
54 /* --- mLib headers --- */
56 #include <mLib/alloc.h>
57 #include <mLib/quis.h>
58 #include <mLib/report.h>
60 #include <mLib/trace.h>
62 /* --- Catacomb headers --- */
64 #include <catacomb/buf.h>
65 #include <catacomb/dsa.h>
66 #include <catacomb/key.h>
67 #include <catacomb/mp.h>
68 #include <catacomb/noise.h>
69 #include <catacomb/rand.h>
70 #include <catacomb/sha.h>
72 /* --- Local headers --- */
83 /*----- Client-end network support ----------------------------------------*/
87 /* --- @check__send@ --- *
89 * Arguments: @char *buf@ = pointer to encrypted request
90 * @size_t sz@ = size of request
91 * @int fd@ = socket to send from
92 * @struct sockaddr_in *serv@ = pointer to table of servers
93 * @size_t n_serv@ = number of servers
97 * Use: Sends the request packet to the list of servers. If the
98 * message couldn't be sent to any of them, an error is
102 static void check__send(char *buf
, size_t sz
, int fd
,
103 struct sockaddr_in
*serv
, size_t n_serv
)
109 for (i
= 0; i
< n_serv
; i
++) {
110 if (sendto(fd
, buf
, sz
, 0,
111 (struct sockaddr
*)(serv
+ i
), sizeof(serv
[i
])) < 0) {
112 T( trace(TRACE_CLIENT
, "client: send to %s failed: %s",
113 inet_ntoa(serv
[i
].sin_addr
), strerror(errno
)); )
120 die(1, "couldn't send request to server: %s", strerror(err
));
123 /* --- @check__ask@ --- *
125 * Arguments: @request *rq@ = pointer to request buffer
126 * @struct sockaddr_in *serv@ = pointer to table of servers
127 * @size_t n_serv@ = number of servers
129 * Returns: Nonzero if OK, zero if forbidden
131 * Use: Contacts a number of servers to decide whether the request
135 static int check__ask(request
*rq
, struct sockaddr_in
*serv
, size_t n_serv
)
137 static int tbl
[] = { 0, 5, 10, 20, -1 };
139 char buff
[2048], rbuff
[2048];
141 octet hmsg
[SHA_HASHSZ
];
143 key_packstruct kps
[DSA_PUBFETCHSZ
];
149 struct sockaddr_in sin
;
154 struct timeval start
, now
, tv
;
162 /* --- Open the public keyring --- */
164 if ((key_open(&f
, file_PUBKEY
, KOPEN_READ
, key_moan
, 0)) != 0)
165 die(1, "couldn't open public keyring");
166 kp
= key_fetchinit(dsa_pubfetch
, kps
, &kpub
);
168 /* --- Build the request packet --- */
170 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
171 rand_seed(RAND_GLOBAL
, 160);
172 buf_init(&b
, buff
, sizeof(buff
));
173 rand_get(RAND_GLOBAL
, buf_get(&b
, SHA_HASHSZ
), SHA_HASHSZ
);
174 buf_putu32(&b
, rq
->from
);
175 buf_putu32(&b
, rq
->to
);
176 buf_putu16(&b
, strlen(rq
->cmd
));
177 buf_put(&b
, rq
->cmd
, strlen(rq
->cmd
));
180 sha_hash(&hc
, buff
, rqlen
);
183 /* --- Create my socket --- */
185 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
186 die(1, "couldn't create socket: %s", strerror(errno
));
187 if (fcntl(fd
, F_SETFD
, 1) < 0)
188 die(1, "couldn't set close-on-exec flag for socket: %s", strerror(errno
));
190 /* --- Bind myself to some address --- */
192 sin
.sin_family
= AF_INET
;
194 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
196 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0)
197 die(1, "couldn't bind socket to address: %s", strerror(errno
));
199 /* --- Find out when we are --- */
201 gettimeofday(&start
, 0);
204 /* --- Now loop until everything's done --- */
207 gettimeofday(&now
, 0);
209 /* --- If the current timer has expired, find one that hasn't --- *
211 * Also resend the request after I've found a timer which is still
212 * extant. If there aren't any, report an error.
215 if (now
.tv_sec
>= start
.tv_sec
+ tbl
[ind
] &&
216 now
.tv_usec
>= start
.tv_usec
) {
220 die(1, "no reply from servers");
221 } while (now
.tv_sec
>= start
.tv_sec
+ tbl
[ind
] &&
222 now
.tv_usec
>= start
.tv_usec
);
223 check__send(buff
, rqlen
, fd
, serv
, n_serv
);
224 T( trace(TRACE_CLIENT
, "client: send request to servers"); )
227 /* --- Now wait for a packet to arrive --- */
229 if (now
.tv_usec
> start
.tv_usec
) {
230 now
.tv_usec
-= 1000000;
233 tv
.tv_sec
= start
.tv_sec
+ tbl
[ind
] - now
.tv_sec
;
234 tv
.tv_usec
= start
.tv_usec
- now
.tv_usec
;
236 /* --- Sort out file descriptors to watch --- */
241 /* --- Wait for them --- */
243 i
= select(FD_SETSIZE
, &fds
, 0, 0, &tv
);
244 if (i
== 0 || (i
< 0 && errno
== EINTR
))
247 die(1, "error waiting for reply: %s", strerror(errno
));
249 /* --- Read the reply data --- */
252 if ((sz
= recvfrom(fd
, (char *)rbuff
, sizeof(rbuff
), 0,
253 (struct sockaddr
*)&sin
, &slen
)) < 0)
254 die(1, "error reading server's reply: %s", strerror(errno
));
256 IF_TRACING(TRACE_CLIENT
, {
257 struct hostent
*h
= gethostbyaddr((char *)&sin
.sin_addr
,
258 sizeof(sin
.sin_addr
), AF_INET
);
259 trace(TRACE_CLIENT
, "client: reply received from %s port %i",
260 h ? h
->h_name
: inet_ntoa(sin
.sin_addr
),
261 ntohs(sin
.sin_port
));
264 /* --- Verify the sender --- *
266 * This is more to avoid confusion than for security: an active
267 * attacker is quite capable of forging the source address. We rely
268 * on the signature in the reply packet for authentication.
271 for (i
= 0; i
< n_serv
; i
++) {
272 if (sin
.sin_addr
.s_addr
== serv
[i
].sin_addr
.s_addr
&&
273 sin
.sin_port
== serv
[i
].sin_port
)
277 T( trace(TRACE_CLIENT
, "client: reply from unknown host"); )
281 /* --- Unpack and verify the response --- */
283 buf_init(&b
, rbuff
, sz
);
284 if (buf_ensure(&b
, sizeof(hmsg
))) goto bad
;
285 if (memcmp(BCUR(&b
), hmsg
, sizeof(hmsg
)) != 0) goto bad
;
286 BSTEP(&b
, sizeof(hmsg
));
287 if ((ans
= buf_getbyte(&b
)) < 0) goto bad
;
290 sha_hash(&hc
, BBASE(&b
), BLEN(&b
));
292 if ((r
= buf_getmp(&b
)) == 0 || (s
= buf_getmp(&b
)) == 0) goto bad
;
293 m
= mp_loadb(MP_NEW
, h
, sizeof(h
));
296 while ((k
= key_next(&ki
)) != 0) {
297 if (key_expired(k
)) continue;
298 if (strcmp(k
->type
, "become-dsa") != 0) continue;
299 if (key_fetch(kp
, k
)) continue;
300 i
= dsa_vrfy(&kpub
.dp
, kpub
.y
, m
, r
, s
);
314 T( trace(TRACE_CLIENT
,
315 "client: invalid or corrupt reply packet"); )
318 die(1, "internal error: can't get here in check__ask");
322 /* --- @check__client@ --- *
324 * Arguments: @request *rq@ = pointer to a request block
325 * @FILE *fp@ = file containing server configuration
327 * Returns: Nonzero if OK, zero if forbidden
329 * Use: Asks one or several servers whether a request is acceptable.
332 int check__client(request
*rq
, FILE *fp
)
334 /* --- Format of the file --- *
336 * The `servers' file contains entries of the form
338 * %%\syntax{<host> [`:' <port>]}%%
340 * separates by whitespace. I build them all into an array of socket
341 * addresses and pass the whole lot to another function.
344 struct sockaddr_in
*serv
; /* Array of servers */
345 size_t n_serv
, max_serv
; /* Number and maximum number */
347 /* --- Initialise the server array --- */
349 T( trace(TRACE_CLIENT
, "client: reading server definitions"); )
350 n_serv
= 0; max_serv
= 4; /* Four seems reasonable */
351 serv
= xmalloc(sizeof(*serv
) * max_serv
);
353 /* --- Start reading the file --- */
356 char buff
[256], *p
, *l
; /* A buffer and pointers for it */
357 int port
; /* Default port for servers */
358 int state
; /* Current parser state */
359 struct in_addr t_host
; /* Temp place for an address*/
360 int t_port
; /* Temp place for a port */
361 int ch
; /* The current character */
363 /* --- Parser states --- */
366 st_start
, /* Waiting to begin */
367 st_host
, /* Reading a new hostname */
368 st_colon
, /* Expecting a colon, maybe */
369 st_preport
, /* Waiting before reading port */
370 st_port
, /* Reading a port number */
371 st_commit
, /* Commit a newly read server */
372 st_done
/* Finished reading the file */
375 /* --- Find a default port --- */
378 struct servent
*s
= getservbyname(quis(), "udp");
379 port
= (s ? s
->s_port
: htons(SERVER_PORT
));
382 /* --- Initialise for scanning the file --- */
386 l
= buff
+ sizeof(buff
);
390 /* --- Go for it --- */
392 while (state
!= st_done
) {
395 /* --- Initial whitespace before hostname --- */
400 else if (isspace((unsigned char)ch
))
406 /* --- Read a host name --- */
410 die(1, "string too long in `" file_SERVER
"'");
411 if (ch
!= EOF
&& !isspace((unsigned char)ch
) && ch
!= ':') {
418 if ((h
= gethostbyname(buff
)) == 0)
419 die(1, "unknown host `%s' in `" file_SERVER
"'", buff
);
420 memcpy(&t_host
, h
->h_addr
, sizeof(t_host
));
425 /* --- Waiting for a colon coming up --- */
430 else if (isspace((unsigned char)ch
))
432 else if (ch
== ':') {
440 /* --- Clearing whitespace before a port number --- */
445 else if (isspace((unsigned char)ch
))
453 /* --- Read a port number --- */
457 die(1, "string too long in `" file_SERVER
"'");
458 if (ch
!= EOF
&& !isspace((unsigned char)ch
) && ch
!= ':') {
465 s
= getservbyname(buff
, "udp");
466 if (!s
&& isdigit((unsigned char)buff
[0]))
467 t_port
= htons(atoi(buff
));
469 die(1, "unknown service `%s' in `" file_SERVER
"'", buff
);
476 /* --- A server has been read successfully --- */
479 if (n_serv
== max_serv
) {
481 serv
= xrealloc(serv
, n_serv
* sizeof(*serv
),
482 max_serv
* sizeof(*serv
));
484 serv
[n_serv
].sin_family
= AF_INET
;
485 serv
[n_serv
].sin_addr
= t_host
;
486 serv
[n_serv
].sin_port
= t_port
;
493 /* --- A safety net for a broken parser --- */
496 die(1, "internal error: can't get here in check__client");
504 /* --- Now start sending requests --- */
507 die(1, "no servers specified in `" file_SERVER
"'");
509 IF_TRACING(TRACE_CLIENT
, {
512 for (i
= 0; i
< n_serv
; i
++) {
513 trace(TRACE_CLIENT
, "client: server %s port %i",
514 inet_ntoa(serv
[i
].sin_addr
), ntohs(serv
[i
].sin_port
));
517 return (check__ask(rq
, serv
, n_serv
));
522 /*----- Main checking function --------------------------------------------*/
526 * Arguments: @request *rq@ = pointer to request buffer
528 * Returns: Nonzero if OK, zero if forbidden
530 * Use: Checks to see if the request is acceptable.
533 int check(request
*rq
)
537 /* --- Check if we need to talk to a server --- */
540 if ((fp
= fopen(file_SERVER
, "r")) != 0)
541 return (check__client(rq
, fp
));
544 /* --- Otherwise do this all the old-fashioned way --- */
546 if ((fp
= fopen(file_RULES
, "r")) == 0) {
547 die(1, "couldn't read configuration file `%s': %s",
548 file_RULES
, strerror(errno
));
561 return (rule_check(rq
));
564 /*----- That's all, folks -------------------------------------------------*/