]> jfr.im git - solanum.git/blame - src/s_auth.c
ident: Check getsockname() return value.
[solanum.git] / src / s_auth.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_auth.c: Functions for querying a users ident.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
6fcb8629 24 * $Id: s_auth.c 3354 2007-04-03 09:21:31Z nenolod $ */
212380e3
AC
25
26/*
27 * Changes:
28 * July 6, 1999 - Rewrote most of the code here. When a client connects
29 * to the server and passes initial socket validation checks, it
30 * is owned by this module (auth) which returns it to the rest of the
31 * server when dns and auth queries are finished. Until the client is
32 * released, the server does not know it exists and does not process
33 * any messages from it.
34 * --Bleep Thomas Helvey <tomh@inxpress.net>
35 */
36#include "stdinc.h"
37#include "config.h"
212380e3
AC
38#include "s_auth.h"
39#include "s_conf.h"
40#include "client.h"
41#include "common.h"
4562c604 42#include "match.h"
212380e3
AC
43#include "ircd.h"
44#include "numeric.h"
45#include "packet.h"
46#include "res.h"
4016731b 47#include "logger.h"
212380e3
AC
48#include "s_stats.h"
49#include "send.h"
212380e3
AC
50#include "hook.h"
51#include "blacklist.h"
52
1a530728
JT
53struct AuthRequest
54{
55 rb_dlink_node node;
56 struct Client *client; /* pointer to client struct for request */
57 struct DNSQuery dns_query; /* DNS Query */
58 unsigned int flags; /* current state of request */
59 rb_fde_t *F; /* file descriptor for auth queries */
60 time_t timeout; /* time when query expires */
61};
62
63/*
64 * flag values for AuthRequest
65 * NAMESPACE: AM_xxx - Authentication Module
66 */
67#define AM_AUTH_CONNECTING (1 << 0)
68#define AM_AUTH_PENDING (1 << 1)
69#define AM_DNS_PENDING (1 << 2)
70
71#define SetDNSPending(x) ((x)->flags |= AM_DNS_PENDING)
72#define ClearDNSPending(x) ((x)->flags &= ~AM_DNS_PENDING)
73#define IsDNSPending(x) ((x)->flags & AM_DNS_PENDING)
74
75#define SetAuthConnect(x) ((x)->flags |= AM_AUTH_CONNECTING)
76#define ClearAuthConnect(x) ((x)->flags &= ~AM_AUTH_CONNECTING)
77#define IsAuthConnect(x) ((x)->flags & AM_AUTH_CONNECTING)
78
79#define SetAuthPending(x) ((x)->flags |= AM_AUTH_PENDING)
80#define ClearAuthPending(x) ((x)->flags &= AM_AUTH_PENDING)
81#define IsAuthPending(x) ((x)->flags & AM_AUTH_PENDING)
82
83#define ClearAuth(x) ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
84#define IsDoingAuth(x) ((x)->flags & (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
85
212380e3
AC
86/*
87 * a bit different approach
88 * this replaces the original sendheader macros
89 */
90
91static const char *HeaderMessages[] =
92{
5366977b
AC
93 ":*** Looking up your hostname...",
94 ":*** Found your hostname",
95 ":*** Couldn't look up your hostname",
96 ":*** Checking Ident",
97 ":*** Got Ident response",
98 ":*** No Ident response",
99 ":*** Your hostname is too long, ignoring hostname",
100 ":*** Your forward and reverse DNS do not match, ignoring hostname",
101 ":*** Cannot verify hostname validity, ignoring hostname",
212380e3
AC
102};
103
104typedef enum
105{
106 REPORT_DO_DNS,
107 REPORT_FIN_DNS,
108 REPORT_FAIL_DNS,
109 REPORT_DO_ID,
110 REPORT_FIN_ID,
111 REPORT_FAIL_ID,
112 REPORT_HOST_TOOLONG,
113 REPORT_HOST_MISMATCH,
114 REPORT_HOST_UNKNOWN
115}
116ReportType;
117
5366977b 118#define sendheader(c, r) sendto_one_notice(c, HeaderMessages[(r)])
212380e3 119
330fc5c1 120static rb_dlink_list auth_poll_list;
398b6a73 121static rb_bh *auth_heap;
212380e3
AC
122static EVH timeout_auth_queries_event;
123
124static PF read_auth_reply;
125static CNCB auth_connect_callback;
126
127/*
128 * init_auth()
129 *
130 * Initialise the auth code
131 */
132void
133init_auth(void)
134{
135 /* This hook takes a struct Client for its argument */
136 memset(&auth_poll_list, 0, sizeof(auth_poll_list));
12aea5fe 137 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event, NULL, 1);
c6f49c9a 138 auth_heap = rb_bh_create(sizeof(struct AuthRequest), LCLIENT_HEAP_SIZE, "auth_heap");
212380e3
AC
139}
140
141/*
142 * make_auth_request - allocate a new auth request
143 */
144static struct AuthRequest *
145make_auth_request(struct Client *client)
146{
398b6a73 147 struct AuthRequest *request = rb_bh_alloc(auth_heap);
212380e3 148 client->localClient->auth_request = request;
c6f49c9a 149 request->F = NULL;
212380e3 150 request->client = client;
e3354945 151 request->timeout = rb_current_time() + ConfigFileEntry.connect_timeout;
212380e3
AC
152 return request;
153}
154
155/*
156 * free_auth_request - cleanup auth request allocations
157 */
158static void
159free_auth_request(struct AuthRequest *request)
160{
398b6a73 161 rb_bh_free(auth_heap, request);
212380e3
AC
162}
163
164/*
165 * release_auth_client - release auth client from auth system
166 * this adds the client into the local client lists so it can be read by
167 * the main io processing loop
168 */
169static void
170release_auth_client(struct AuthRequest *auth)
171{
172 struct Client *client = auth->client;
173
174 if(IsDNSPending(auth) || IsDoingAuth(auth))
175 return;
176
177 client->localClient->auth_request = NULL;
330fc5c1 178 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3 179 free_auth_request(auth);
212380e3
AC
180
181 /*
182 * When a client has auth'ed, we want to start reading what it sends
183 * us. This is what read_packet() does.
184 * -- adrian
185 */
186 client->localClient->allow_read = MAX_FLOOD;
330fc5c1 187 rb_dlinkAddTail(client, &client->node, &global_client_list);
c6f49c9a 188 read_packet(client->localClient->F, client);
212380e3
AC
189}
190
191/*
192 * auth_dns_callback - called when resolver query finishes
193 * if the query resulted in a successful search, hp will contain
194 * a non-null pointer, otherwise hp will be null.
195 * set the client on it's way to a connection completion, regardless
196 * of success of failure
197 */
198static void
199auth_dns_callback(void *vptr, struct DNSReply *reply)
200{
201 struct AuthRequest *auth = (struct AuthRequest *) vptr;
202 ClearDNSPending(auth);
203
204 /* XXX: this shouldn't happen, but it does. -nenolod */
205 if(auth->client->localClient == NULL)
206 {
207 sendto_realops_snomask(SNO_GENERAL, L_ALL,
208 "auth_dns_callback(): auth->client->localClient (%s) is NULL", get_client_name(auth->client, HIDE_IP));
209
330fc5c1 210 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3
AC
211 free_auth_request(auth);
212
213 /* and they will silently drop through and all will hopefully be ok... -nenolod */
214 return;
215 }
216
217 if(reply)
218 {
219 int good = 1;
220
221 if(auth->client->localClient->ip.ss_family == AF_INET)
222 {
223 struct sockaddr_in *ip, *ip_fwd;
224
225 ip = (struct sockaddr_in *) &auth->client->localClient->ip;
226 ip_fwd = (struct sockaddr_in *) &reply->addr;
227
228 if(ip->sin_addr.s_addr != ip_fwd->sin_addr.s_addr)
229 {
230 sendheader(auth->client, REPORT_HOST_MISMATCH);
231 good = 0;
232 }
233 }
ccda6e3f 234#ifdef RB_IPV6
212380e3
AC
235 else if(auth->client->localClient->ip.ss_family == AF_INET6)
236 {
237 struct sockaddr_in6 *ip, *ip_fwd;
238
239 ip = (struct sockaddr_in6 *) &auth->client->localClient->ip;
240 ip_fwd = (struct sockaddr_in6 *) &reply->addr;
241
242 if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) != 0)
243 {
244 sendheader(auth->client, REPORT_HOST_MISMATCH);
245 good = 0;
246 }
247 }
248#endif
249 else /* can't verify it, don't know how. reject it. */
250 {
251 sendheader(auth->client, REPORT_HOST_UNKNOWN);
252 good = 0;
253 }
254
255 if(good && strlen(reply->h_name) <= HOSTLEN)
256 {
f427c8b0 257 rb_strlcpy(auth->client->host, reply->h_name, sizeof(auth->client->host));
212380e3
AC
258 sendheader(auth->client, REPORT_FIN_DNS);
259 }
260 else if (strlen(reply->h_name) > HOSTLEN)
261 sendheader(auth->client, REPORT_HOST_TOOLONG);
262 }
263 else
264 sendheader(auth->client, REPORT_FAIL_DNS);
265
266 release_auth_client(auth);
267}
268
269/*
270 * authsenderr - handle auth send errors
271 */
272static void
273auth_error(struct AuthRequest *auth)
274{
47adde3d 275 ++ServerStats.is_abad;
212380e3 276
c6f49c9a
JT
277 rb_close(auth->F);
278 auth->F = NULL;
212380e3
AC
279
280 ClearAuth(auth);
281 sendheader(auth->client, REPORT_FAIL_ID);
282
283 release_auth_client(auth);
284}
285
286/*
287 * start_auth_query - Flag the client to show that an attempt to
288 * contact the ident server on
289 * the client's host. The connect and subsequently the socket are all put
290 * into 'non-blocking' mode. Should the connect or any later phase of the
291 * identifing process fail, it is aborted and the user is given a username
292 * of "unknown".
293 */
294static int
295start_auth_query(struct AuthRequest *auth)
296{
c6f49c9a 297 struct rb_sockaddr_storage localaddr, destaddr;
e7046ee5 298 socklen_t locallen = sizeof(struct rb_sockaddr_storage);
c6f49c9a 299 rb_fde_t *F;
212380e3
AC
300 int family;
301
302 if(IsAnyDead(auth->client))
303 return 0;
304
305 family = auth->client->localClient->ip.ss_family;
c6f49c9a 306 if((F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL)
212380e3 307 {
825ddf13 308 ilog_error("creating auth stream socket");
47adde3d 309 ++ServerStats.is_abad;
212380e3
AC
310 return 0;
311 }
6fcb8629
AC
312
313 /*
314 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
315 */
c6f49c9a 316 if((maxconnections - 10) < rb_get_fd(F))
212380e3
AC
317 {
318 sendto_realops_snomask(SNO_GENERAL, L_ALL,
319 "Can't allocate fd for auth on %s",
320 get_client_name(auth->client, SHOW_IP));
c6f49c9a 321 rb_close(F);
212380e3
AC
322 return 0;
323 }
324
325 sendheader(auth->client, REPORT_DO_ID);
326
327 /*
328 * get the local address of the client and bind to that to
329 * make the auth request. This used to be done only for
330 * ifdef VIRTUAL_HOST, but needs to be done for all clients
331 * since the ident request must originate from that same address--
332 * and machines with multiple IP addresses are common now
333 */
334 memset(&localaddr, 0, locallen);
d5d5e89c
JT
335 if(getsockname(rb_get_fd(auth->client->localClient->F),
336 (struct sockaddr *) &localaddr, &locallen) == -1)
337 {
338 /* can happen if connection was just closed */
339 rb_close(F);
340 return 0;
341 }
212380e3 342
c6f49c9a 343 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
ccda6e3f 344#ifdef RB_IPV6
212380e3
AC
345 if(localaddr.ss_family == AF_INET6)
346 {
347 ((struct sockaddr_in6 *)&localaddr)->sin6_port = 0;
348 } else
349#endif
350 ((struct sockaddr_in *)&localaddr)->sin_port = 0;
c6f49c9a
JT
351
352 destaddr = auth->client->localClient->ip;
ccda6e3f 353#ifdef RB_IPV6
c6f49c9a
JT
354 if(localaddr.ss_family == AF_INET6)
355 {
e33c4818 356 ((struct sockaddr_in6 *)&destaddr)->sin6_port = htons(113);
c6f49c9a
JT
357 } else
358#endif
e33c4818 359 ((struct sockaddr_in *)&destaddr)->sin_port = htons(113);
212380e3 360
c6f49c9a 361 auth->F = F;
212380e3
AC
362 SetAuthConnect(auth);
363
c6f49c9a
JT
364 rb_connect_tcp(F, (struct sockaddr *)&destaddr,
365 (struct sockaddr *) &localaddr, GET_SS_LEN(&localaddr),
212380e3 366 auth_connect_callback, auth,
c6f49c9a 367 GlobalSetOptions.ident_timeout);
212380e3
AC
368 return 1; /* We suceed here for now */
369}
370
371/*
372 * GetValidIdent - parse ident query reply from identd server
373 *
374 * Inputs - pointer to ident buf
375 * Output - NULL if no valid ident found, otherwise pointer to name
376 * Side effects -
377 */
378static char *
379GetValidIdent(char *buf)
380{
381 int remp = 0;
382 int locp = 0;
383 char *colon1Ptr;
384 char *colon2Ptr;
385 char *colon3Ptr;
386 char *commaPtr;
387 char *remotePortString;
388
389 /* All this to get rid of a sscanf() fun. */
390 remotePortString = buf;
391
392 colon1Ptr = strchr(remotePortString, ':');
393 if(!colon1Ptr)
394 return 0;
395
396 *colon1Ptr = '\0';
397 colon1Ptr++;
398 colon2Ptr = strchr(colon1Ptr, ':');
399 if(!colon2Ptr)
400 return 0;
401
402 *colon2Ptr = '\0';
403 colon2Ptr++;
404 commaPtr = strchr(remotePortString, ',');
405
406 if(!commaPtr)
407 return 0;
408
409 *commaPtr = '\0';
410 commaPtr++;
411
412 remp = atoi(remotePortString);
413 if(!remp)
414 return 0;
415
416 locp = atoi(commaPtr);
417 if(!locp)
418 return 0;
419
420 /* look for USERID bordered by first pair of colons */
421 if(!strstr(colon1Ptr, "USERID"))
422 return 0;
423
424 colon3Ptr = strchr(colon2Ptr, ':');
425 if(!colon3Ptr)
426 return 0;
427
428 *colon3Ptr = '\0';
429 colon3Ptr++;
430 return (colon3Ptr);
431}
432
433/*
434 * start_auth - starts auth (identd) and dns queries for a client
435 */
436void
437start_auth(struct Client *client)
438{
439 struct AuthRequest *auth = 0;
440 s_assert(0 != client);
441 if(client == NULL)
442 return;
443
212380e3
AC
444 auth = make_auth_request(client);
445
446 auth->dns_query.ptr = auth;
447 auth->dns_query.callback = auth_dns_callback;
448
449 sendheader(client, REPORT_DO_DNS);
450
451 /* No DNS cache now, remember? -- adrian */
452 gethost_byaddr(&client->localClient->ip, &auth->dns_query);
453
454 SetDNSPending(auth);
455
456 if(ConfigFileEntry.disable_auth == 0)
457 start_auth_query(auth);
458
330fc5c1 459 rb_dlinkAdd(auth, &auth->node, &auth_poll_list);
212380e3
AC
460}
461
462/*
463 * timeout_auth_queries - timeout resolver and identd requests
464 * allow clients through if requests failed
465 */
466static void
467timeout_auth_queries_event(void *notused)
468{
330fc5c1 469 rb_dlink_node *ptr;
637c4932 470 rb_dlink_node *next_ptr;
212380e3
AC
471 struct AuthRequest *auth;
472
637c4932 473 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, auth_poll_list.head)
212380e3
AC
474 {
475 auth = ptr->data;
476
e3354945 477 if(auth->timeout < rb_current_time())
212380e3 478 {
c6f49c9a
JT
479 if(auth->F != NULL)
480 rb_close(auth->F);
212380e3
AC
481
482 if(IsDoingAuth(auth))
483 {
484 ClearAuth(auth);
47adde3d 485 ++ServerStats.is_abad;
212380e3
AC
486 sendheader(auth->client, REPORT_FAIL_ID);
487 auth->client->localClient->auth_request = NULL;
488 }
489 if(IsDNSPending(auth))
490 {
491 ClearDNSPending(auth);
492 delete_resolver_queries(&auth->dns_query);
493 sendheader(auth->client, REPORT_FAIL_DNS);
494 }
495
e3354945 496 auth->client->localClient->lasttime = rb_current_time();
212380e3
AC
497 release_auth_client(auth);
498 }
499 }
500}
501
502/*
b2f0da88 503 * auth_connect_callback() - deal with the result of rb_connect_tcp()
212380e3
AC
504 *
505 * If the connection failed, we simply close the auth fd and report
506 * a failure. If the connection suceeded send the ident server a query
507 * giving "theirport , ourport". The write is only attempted *once* so
508 * it is deemed to be a fail if the entire write doesn't write all the
509 * data given. This shouldnt be a problem since the socket should have
510 * a write buffer far greater than this message to store it in should
511 * problems arise. -avalon
512 */
513static void
c6f49c9a 514auth_connect_callback(rb_fde_t *F, int error, void *data)
212380e3
AC
515{
516 struct AuthRequest *auth = data;
517 struct sockaddr_in us;
518 struct sockaddr_in them;
519 char authbuf[32];
520 socklen_t ulen = sizeof(struct sockaddr_in);
521 socklen_t tlen = sizeof(struct sockaddr_in);
522
523 /* Check the error */
c6f49c9a 524 if(error != RB_OK)
212380e3
AC
525 {
526 /* We had an error during connection :( */
527 auth_error(auth);
528 return;
529 }
530
531 if(getsockname
c6f49c9a 532 (rb_get_fd(auth->client->localClient->F), (struct sockaddr *) &us,
212380e3 533 (socklen_t *) & ulen)
c6f49c9a 534 || getpeername(rb_get_fd(auth->client->localClient->F),
212380e3
AC
535 (struct sockaddr *) &them, (socklen_t *) & tlen))
536 {
537 ilog(L_IOERROR, "auth get{sock,peer}name error for %s:%m",
538 log_client_name(auth->client, SHOW_IP));
539 auth_error(auth);
540 return;
541 }
b2f0da88 542 rb_snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
212380e3
AC
543 (unsigned int) ntohs(them.sin_port), (unsigned int) ntohs(us.sin_port));
544
c6f49c9a 545 if(write(rb_get_fd(auth->F), authbuf, strlen(authbuf)) == -1)
212380e3
AC
546 {
547 auth_error(auth);
548 return;
549 }
550 ClearAuthConnect(auth);
551 SetAuthPending(auth);
c6f49c9a 552 read_auth_reply(auth->F, auth);
212380e3
AC
553}
554
555
556/*
557 * read_auth_reply - read the reply (if any) from the ident server
558 * we connected to.
559 * We only give it one shot, if the reply isn't good the first time
560 * fail the authentication entirely. --Bleep
561 */
562#define AUTH_BUFSIZ 128
563
564static void
c6f49c9a 565read_auth_reply(rb_fde_t *F, void *data)
212380e3
AC
566{
567 struct AuthRequest *auth = data;
568 char *s = NULL;
569 char *t = NULL;
570 int len;
571 int count;
572 char buf[AUTH_BUFSIZ + 1]; /* buffer to read auth reply into */
573
c6f49c9a 574 len = read(rb_get_fd(F), buf, AUTH_BUFSIZ);
212380e3 575
c6f49c9a 576 if(len < 0 && rb_ignore_errno(errno))
212380e3 577 {
c6f49c9a 578 rb_setselect(F, RB_SELECT_READ, read_auth_reply, auth);
212380e3
AC
579 return;
580 }
581
582 if(len > 0)
583 {
584 buf[len] = '\0';
585
586 if((s = GetValidIdent(buf)))
587 {
588 t = auth->client->username;
589
590 while (*s == '~' || *s == '^')
591 s++;
592
593 for (count = USERLEN; *s && count; s++)
594 {
595 if(*s == '@')
596 {
597 break;
598 }
599 if(!IsSpace(*s) && *s != ':' && *s != '[')
600 {
601 *t++ = *s;
602 count--;
603 }
604 }
605 *t = '\0';
606 }
607 }
608
c6f49c9a
JT
609 rb_close(auth->F);
610 auth->F = NULL;
212380e3
AC
611 ClearAuth(auth);
612
613 if(s == NULL)
614 {
47adde3d 615 ++ServerStats.is_abad;
212380e3
AC
616 strcpy(auth->client->username, "unknown");
617 sendheader(auth->client, REPORT_FAIL_ID);
618 }
619 else
620 {
621 sendheader(auth->client, REPORT_FIN_ID);
47adde3d 622 ++ServerStats.is_asuc;
212380e3
AC
623 SetGotId(auth->client);
624 }
625
626 release_auth_client(auth);
627}
628
629
630
631/*
632 * delete_auth_queries()
633 *
634 */
635void
636delete_auth_queries(struct Client *target_p)
637{
638 struct AuthRequest *auth;
639
640 if(target_p == NULL || target_p->localClient == NULL ||
641 target_p->localClient->auth_request == NULL)
642 return;
643
644 auth = target_p->localClient->auth_request;
645 target_p->localClient->auth_request = NULL;
646
647 if(IsDNSPending(auth))
648 delete_resolver_queries(&auth->dns_query);
649
c6f49c9a
JT
650 if(auth->F != NULL)
651 rb_close(auth->F);
212380e3 652
330fc5c1 653 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3
AC
654 free_auth_request(auth);
655}