]> jfr.im git - solanum.git/blame - src/s_auth.c
Minor cleanup to command throttling code:
[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 */
d0ebe1bc
JT
61 uint16_t lport;
62 uint16_t rport;
1a530728
JT
63};
64
65/*
66 * flag values for AuthRequest
67 * NAMESPACE: AM_xxx - Authentication Module
68 */
69#define AM_AUTH_CONNECTING (1 << 0)
70#define AM_AUTH_PENDING (1 << 1)
71#define AM_DNS_PENDING (1 << 2)
72
73#define SetDNSPending(x) ((x)->flags |= AM_DNS_PENDING)
74#define ClearDNSPending(x) ((x)->flags &= ~AM_DNS_PENDING)
75#define IsDNSPending(x) ((x)->flags & AM_DNS_PENDING)
76
77#define SetAuthConnect(x) ((x)->flags |= AM_AUTH_CONNECTING)
78#define ClearAuthConnect(x) ((x)->flags &= ~AM_AUTH_CONNECTING)
79#define IsAuthConnect(x) ((x)->flags & AM_AUTH_CONNECTING)
80
81#define SetAuthPending(x) ((x)->flags |= AM_AUTH_PENDING)
82#define ClearAuthPending(x) ((x)->flags &= AM_AUTH_PENDING)
83#define IsAuthPending(x) ((x)->flags & AM_AUTH_PENDING)
84
85#define ClearAuth(x) ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
86#define IsDoingAuth(x) ((x)->flags & (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
87
212380e3
AC
88/*
89 * a bit different approach
90 * this replaces the original sendheader macros
91 */
92
93static const char *HeaderMessages[] =
94{
5366977b
AC
95 ":*** Looking up your hostname...",
96 ":*** Found your hostname",
97 ":*** Couldn't look up your hostname",
98 ":*** Checking Ident",
99 ":*** Got Ident response",
100 ":*** No Ident response",
101 ":*** Your hostname is too long, ignoring hostname",
102 ":*** Your forward and reverse DNS do not match, ignoring hostname",
103 ":*** Cannot verify hostname validity, ignoring hostname",
212380e3
AC
104};
105
106typedef enum
107{
108 REPORT_DO_DNS,
109 REPORT_FIN_DNS,
110 REPORT_FAIL_DNS,
111 REPORT_DO_ID,
112 REPORT_FIN_ID,
113 REPORT_FAIL_ID,
114 REPORT_HOST_TOOLONG,
115 REPORT_HOST_MISMATCH,
116 REPORT_HOST_UNKNOWN
117}
118ReportType;
119
5366977b 120#define sendheader(c, r) sendto_one_notice(c, HeaderMessages[(r)])
212380e3 121
330fc5c1 122static rb_dlink_list auth_poll_list;
398b6a73 123static rb_bh *auth_heap;
212380e3
AC
124static EVH timeout_auth_queries_event;
125
126static PF read_auth_reply;
127static CNCB auth_connect_callback;
128
129/*
130 * init_auth()
131 *
132 * Initialise the auth code
133 */
134void
135init_auth(void)
136{
137 /* This hook takes a struct Client for its argument */
138 memset(&auth_poll_list, 0, sizeof(auth_poll_list));
12aea5fe 139 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event, NULL, 1);
c6f49c9a 140 auth_heap = rb_bh_create(sizeof(struct AuthRequest), LCLIENT_HEAP_SIZE, "auth_heap");
212380e3
AC
141}
142
143/*
144 * make_auth_request - allocate a new auth request
145 */
146static struct AuthRequest *
147make_auth_request(struct Client *client)
148{
398b6a73 149 struct AuthRequest *request = rb_bh_alloc(auth_heap);
212380e3 150 client->localClient->auth_request = request;
c6f49c9a 151 request->F = NULL;
212380e3 152 request->client = client;
e3354945 153 request->timeout = rb_current_time() + ConfigFileEntry.connect_timeout;
212380e3
AC
154 return request;
155}
156
157/*
158 * free_auth_request - cleanup auth request allocations
159 */
160static void
161free_auth_request(struct AuthRequest *request)
162{
398b6a73 163 rb_bh_free(auth_heap, request);
212380e3
AC
164}
165
166/*
167 * release_auth_client - release auth client from auth system
168 * this adds the client into the local client lists so it can be read by
169 * the main io processing loop
170 */
171static void
172release_auth_client(struct AuthRequest *auth)
173{
174 struct Client *client = auth->client;
175
176 if(IsDNSPending(auth) || IsDoingAuth(auth))
177 return;
178
179 client->localClient->auth_request = NULL;
330fc5c1 180 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3 181 free_auth_request(auth);
212380e3
AC
182
183 /*
184 * When a client has auth'ed, we want to start reading what it sends
185 * us. This is what read_packet() does.
186 * -- adrian
187 */
330fc5c1 188 rb_dlinkAddTail(client, &client->node, &global_client_list);
c6f49c9a 189 read_packet(client->localClient->F, client);
212380e3
AC
190}
191
192/*
193 * auth_dns_callback - called when resolver query finishes
194 * if the query resulted in a successful search, hp will contain
195 * a non-null pointer, otherwise hp will be null.
196 * set the client on it's way to a connection completion, regardless
197 * of success of failure
198 */
199static void
200auth_dns_callback(void *vptr, struct DNSReply *reply)
201{
202 struct AuthRequest *auth = (struct AuthRequest *) vptr;
203 ClearDNSPending(auth);
204
205 /* XXX: this shouldn't happen, but it does. -nenolod */
206 if(auth->client->localClient == NULL)
207 {
208 sendto_realops_snomask(SNO_GENERAL, L_ALL,
209 "auth_dns_callback(): auth->client->localClient (%s) is NULL", get_client_name(auth->client, HIDE_IP));
210
330fc5c1 211 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3
AC
212 free_auth_request(auth);
213
214 /* and they will silently drop through and all will hopefully be ok... -nenolod */
215 return;
216 }
217
218 if(reply)
219 {
220 int good = 1;
221
222 if(auth->client->localClient->ip.ss_family == AF_INET)
223 {
224 struct sockaddr_in *ip, *ip_fwd;
225
226 ip = (struct sockaddr_in *) &auth->client->localClient->ip;
227 ip_fwd = (struct sockaddr_in *) &reply->addr;
228
229 if(ip->sin_addr.s_addr != ip_fwd->sin_addr.s_addr)
230 {
231 sendheader(auth->client, REPORT_HOST_MISMATCH);
232 good = 0;
233 }
234 }
ccda6e3f 235#ifdef RB_IPV6
212380e3
AC
236 else if(auth->client->localClient->ip.ss_family == AF_INET6)
237 {
238 struct sockaddr_in6 *ip, *ip_fwd;
239
240 ip = (struct sockaddr_in6 *) &auth->client->localClient->ip;
241 ip_fwd = (struct sockaddr_in6 *) &reply->addr;
242
243 if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) != 0)
244 {
245 sendheader(auth->client, REPORT_HOST_MISMATCH);
246 good = 0;
247 }
248 }
249#endif
250 else /* can't verify it, don't know how. reject it. */
251 {
252 sendheader(auth->client, REPORT_HOST_UNKNOWN);
253 good = 0;
254 }
255
256 if(good && strlen(reply->h_name) <= HOSTLEN)
257 {
f427c8b0 258 rb_strlcpy(auth->client->host, reply->h_name, sizeof(auth->client->host));
212380e3
AC
259 sendheader(auth->client, REPORT_FIN_DNS);
260 }
261 else if (strlen(reply->h_name) > HOSTLEN)
262 sendheader(auth->client, REPORT_HOST_TOOLONG);
263 }
264 else
265 sendheader(auth->client, REPORT_FAIL_DNS);
266
267 release_auth_client(auth);
268}
269
270/*
271 * authsenderr - handle auth send errors
272 */
273static void
274auth_error(struct AuthRequest *auth)
275{
47adde3d 276 ++ServerStats.is_abad;
212380e3 277
c6f49c9a
JT
278 rb_close(auth->F);
279 auth->F = NULL;
212380e3
AC
280
281 ClearAuth(auth);
282 sendheader(auth->client, REPORT_FAIL_ID);
283
284 release_auth_client(auth);
285}
286
287/*
288 * start_auth_query - Flag the client to show that an attempt to
289 * contact the ident server on
290 * the client's host. The connect and subsequently the socket are all put
291 * into 'non-blocking' mode. Should the connect or any later phase of the
292 * identifing process fail, it is aborted and the user is given a username
293 * of "unknown".
294 */
295static int
296start_auth_query(struct AuthRequest *auth)
297{
c6f49c9a 298 struct rb_sockaddr_storage localaddr, destaddr;
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 */
3540120a 334 localaddr = auth->client->preClient->lip;
212380e3 335
c6f49c9a 336 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
ccda6e3f 337#ifdef RB_IPV6
212380e3
AC
338 if(localaddr.ss_family == AF_INET6)
339 {
d0ebe1bc 340 auth->lport = ntohs(((struct sockaddr_in6 *)&localaddr)->sin6_port);
212380e3 341 ((struct sockaddr_in6 *)&localaddr)->sin6_port = 0;
d0ebe1bc
JT
342 }
343 else
212380e3 344#endif
d0ebe1bc
JT
345 {
346 auth->lport = ntohs(((struct sockaddr_in *)&localaddr)->sin_port);
347 ((struct sockaddr_in *)&localaddr)->sin_port = 0;
348 }
c6f49c9a
JT
349
350 destaddr = auth->client->localClient->ip;
ccda6e3f 351#ifdef RB_IPV6
c6f49c9a
JT
352 if(localaddr.ss_family == AF_INET6)
353 {
d0ebe1bc 354 auth->rport = ntohs(((struct sockaddr_in6 *)&destaddr)->sin6_port);
e33c4818 355 ((struct sockaddr_in6 *)&destaddr)->sin6_port = htons(113);
d0ebe1bc
JT
356 }
357 else
c6f49c9a 358#endif
d0ebe1bc
JT
359 {
360 auth->rport = ntohs(((struct sockaddr_in *)&destaddr)->sin_port);
361 ((struct sockaddr_in *)&destaddr)->sin_port = htons(113);
362 }
212380e3 363
c6f49c9a 364 auth->F = F;
212380e3
AC
365 SetAuthConnect(auth);
366
c6f49c9a
JT
367 rb_connect_tcp(F, (struct sockaddr *)&destaddr,
368 (struct sockaddr *) &localaddr, GET_SS_LEN(&localaddr),
212380e3 369 auth_connect_callback, auth,
c6f49c9a 370 GlobalSetOptions.ident_timeout);
212380e3
AC
371 return 1; /* We suceed here for now */
372}
373
374/*
375 * GetValidIdent - parse ident query reply from identd server
376 *
377 * Inputs - pointer to ident buf
378 * Output - NULL if no valid ident found, otherwise pointer to name
379 * Side effects -
380 */
381static char *
382GetValidIdent(char *buf)
383{
384 int remp = 0;
385 int locp = 0;
386 char *colon1Ptr;
387 char *colon2Ptr;
388 char *colon3Ptr;
389 char *commaPtr;
390 char *remotePortString;
391
392 /* All this to get rid of a sscanf() fun. */
393 remotePortString = buf;
394
395 colon1Ptr = strchr(remotePortString, ':');
396 if(!colon1Ptr)
397 return 0;
398
399 *colon1Ptr = '\0';
400 colon1Ptr++;
401 colon2Ptr = strchr(colon1Ptr, ':');
402 if(!colon2Ptr)
403 return 0;
404
405 *colon2Ptr = '\0';
406 colon2Ptr++;
407 commaPtr = strchr(remotePortString, ',');
408
409 if(!commaPtr)
410 return 0;
411
412 *commaPtr = '\0';
413 commaPtr++;
414
415 remp = atoi(remotePortString);
416 if(!remp)
417 return 0;
418
419 locp = atoi(commaPtr);
420 if(!locp)
421 return 0;
422
423 /* look for USERID bordered by first pair of colons */
424 if(!strstr(colon1Ptr, "USERID"))
425 return 0;
426
427 colon3Ptr = strchr(colon2Ptr, ':');
428 if(!colon3Ptr)
429 return 0;
430
431 *colon3Ptr = '\0';
432 colon3Ptr++;
433 return (colon3Ptr);
434}
435
436/*
437 * start_auth - starts auth (identd) and dns queries for a client
438 */
439void
440start_auth(struct Client *client)
441{
442 struct AuthRequest *auth = 0;
443 s_assert(0 != client);
444 if(client == NULL)
445 return;
446
212380e3
AC
447 auth = make_auth_request(client);
448
449 auth->dns_query.ptr = auth;
450 auth->dns_query.callback = auth_dns_callback;
451
452 sendheader(client, REPORT_DO_DNS);
453
454 /* No DNS cache now, remember? -- adrian */
455 gethost_byaddr(&client->localClient->ip, &auth->dns_query);
456
457 SetDNSPending(auth);
458
459 if(ConfigFileEntry.disable_auth == 0)
460 start_auth_query(auth);
461
330fc5c1 462 rb_dlinkAdd(auth, &auth->node, &auth_poll_list);
212380e3
AC
463}
464
465/*
466 * timeout_auth_queries - timeout resolver and identd requests
467 * allow clients through if requests failed
468 */
469static void
470timeout_auth_queries_event(void *notused)
471{
330fc5c1 472 rb_dlink_node *ptr;
637c4932 473 rb_dlink_node *next_ptr;
212380e3
AC
474 struct AuthRequest *auth;
475
637c4932 476 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, auth_poll_list.head)
212380e3
AC
477 {
478 auth = ptr->data;
479
e3354945 480 if(auth->timeout < rb_current_time())
212380e3 481 {
c6f49c9a
JT
482 if(auth->F != NULL)
483 rb_close(auth->F);
212380e3
AC
484
485 if(IsDoingAuth(auth))
486 {
487 ClearAuth(auth);
47adde3d 488 ++ServerStats.is_abad;
212380e3
AC
489 sendheader(auth->client, REPORT_FAIL_ID);
490 auth->client->localClient->auth_request = NULL;
491 }
492 if(IsDNSPending(auth))
493 {
494 ClearDNSPending(auth);
495 delete_resolver_queries(&auth->dns_query);
496 sendheader(auth->client, REPORT_FAIL_DNS);
497 }
498
e3354945 499 auth->client->localClient->lasttime = rb_current_time();
212380e3
AC
500 release_auth_client(auth);
501 }
502 }
503}
504
505/*
b2f0da88 506 * auth_connect_callback() - deal with the result of rb_connect_tcp()
212380e3
AC
507 *
508 * If the connection failed, we simply close the auth fd and report
509 * a failure. If the connection suceeded send the ident server a query
510 * giving "theirport , ourport". The write is only attempted *once* so
511 * it is deemed to be a fail if the entire write doesn't write all the
512 * data given. This shouldnt be a problem since the socket should have
513 * a write buffer far greater than this message to store it in should
514 * problems arise. -avalon
515 */
516static void
c6f49c9a 517auth_connect_callback(rb_fde_t *F, int error, void *data)
212380e3
AC
518{
519 struct AuthRequest *auth = data;
212380e3 520 char authbuf[32];
212380e3
AC
521
522 /* Check the error */
c6f49c9a 523 if(error != RB_OK)
212380e3
AC
524 {
525 /* We had an error during connection :( */
526 auth_error(auth);
527 return;
528 }
529
b2f0da88 530 rb_snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
d0ebe1bc 531 auth->rport, auth->lport);
212380e3 532
dd1b211f 533 if(rb_write(auth->F, authbuf, strlen(authbuf)) != strlen(authbuf))
212380e3
AC
534 {
535 auth_error(auth);
536 return;
537 }
538 ClearAuthConnect(auth);
539 SetAuthPending(auth);
c6f49c9a 540 read_auth_reply(auth->F, auth);
212380e3
AC
541}
542
543
544/*
545 * read_auth_reply - read the reply (if any) from the ident server
546 * we connected to.
547 * We only give it one shot, if the reply isn't good the first time
548 * fail the authentication entirely. --Bleep
549 */
550#define AUTH_BUFSIZ 128
551
552static void
c6f49c9a 553read_auth_reply(rb_fde_t *F, void *data)
212380e3
AC
554{
555 struct AuthRequest *auth = data;
556 char *s = NULL;
557 char *t = NULL;
558 int len;
559 int count;
560 char buf[AUTH_BUFSIZ + 1]; /* buffer to read auth reply into */
561
dd1b211f 562 len = rb_read(F, buf, AUTH_BUFSIZ);
212380e3 563
c6f49c9a 564 if(len < 0 && rb_ignore_errno(errno))
212380e3 565 {
c6f49c9a 566 rb_setselect(F, RB_SELECT_READ, read_auth_reply, auth);
212380e3
AC
567 return;
568 }
569
570 if(len > 0)
571 {
572 buf[len] = '\0';
573
574 if((s = GetValidIdent(buf)))
575 {
576 t = auth->client->username;
577
578 while (*s == '~' || *s == '^')
579 s++;
580
581 for (count = USERLEN; *s && count; s++)
582 {
583 if(*s == '@')
584 {
585 break;
586 }
587 if(!IsSpace(*s) && *s != ':' && *s != '[')
588 {
589 *t++ = *s;
590 count--;
591 }
592 }
593 *t = '\0';
594 }
595 }
596
c6f49c9a
JT
597 rb_close(auth->F);
598 auth->F = NULL;
212380e3
AC
599 ClearAuth(auth);
600
601 if(s == NULL)
602 {
47adde3d 603 ++ServerStats.is_abad;
212380e3
AC
604 strcpy(auth->client->username, "unknown");
605 sendheader(auth->client, REPORT_FAIL_ID);
606 }
607 else
608 {
609 sendheader(auth->client, REPORT_FIN_ID);
47adde3d 610 ++ServerStats.is_asuc;
212380e3
AC
611 SetGotId(auth->client);
612 }
613
614 release_auth_client(auth);
615}
616
617
618
619/*
620 * delete_auth_queries()
621 *
622 */
623void
624delete_auth_queries(struct Client *target_p)
625{
626 struct AuthRequest *auth;
627
628 if(target_p == NULL || target_p->localClient == NULL ||
629 target_p->localClient->auth_request == NULL)
630 return;
631
632 auth = target_p->localClient->auth_request;
633 target_p->localClient->auth_request = NULL;
634
635 if(IsDNSPending(auth))
636 delete_resolver_queries(&auth->dns_query);
637
c6f49c9a
JT
638 if(auth->F != NULL)
639 rb_close(auth->F);
212380e3 640
330fc5c1 641 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3
AC
642 free_auth_request(auth);
643}