]> jfr.im git - irc/rqf/shadowircd.git/blame - src/s_auth.c
Add topic TS and channel TS constraints for /LIST.
[irc/rqf/shadowircd.git] / src / s_auth.c
CommitLineData
212380e3 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
9e8efa00 23 */
212380e3 24
25/*
26 * Changes:
27 * July 6, 1999 - Rewrote most of the code here. When a client connects
28 * to the server and passes initial socket validation checks, it
29 * is owned by this module (auth) which returns it to the rest of the
30 * server when dns and auth queries are finished. Until the client is
31 * released, the server does not know it exists and does not process
32 * any messages from it.
33 * --Bleep Thomas Helvey <tomh@inxpress.net>
34 */
35#include "stdinc.h"
36#include "config.h"
212380e3 37#include "s_auth.h"
38#include "s_conf.h"
39#include "client.h"
40#include "common.h"
13ae2f4b 41#include "match.h"
212380e3 42#include "ircd.h"
43#include "numeric.h"
44#include "packet.h"
45#include "res.h"
d3455e2c 46#include "logger.h"
212380e3 47#include "s_stats.h"
48#include "send.h"
212380e3 49#include "hook.h"
50#include "blacklist.h"
51
ed28210c
JT
52struct AuthRequest
53{
54 rb_dlink_node node;
55 struct Client *client; /* pointer to client struct for request */
56 struct DNSQuery dns_query; /* DNS Query */
57 unsigned int flags; /* current state of request */
58 rb_fde_t *F; /* file descriptor for auth queries */
59 time_t timeout; /* time when query expires */
0a93f25c
JT
60 uint16_t lport;
61 uint16_t rport;
ed28210c
JT
62};
63
64/*
65 * flag values for AuthRequest
66 * NAMESPACE: AM_xxx - Authentication Module
67 */
68#define AM_AUTH_CONNECTING (1 << 0)
69#define AM_AUTH_PENDING (1 << 1)
70#define AM_DNS_PENDING (1 << 2)
71
72#define SetDNSPending(x) ((x)->flags |= AM_DNS_PENDING)
73#define ClearDNSPending(x) ((x)->flags &= ~AM_DNS_PENDING)
74#define IsDNSPending(x) ((x)->flags & AM_DNS_PENDING)
75
76#define SetAuthConnect(x) ((x)->flags |= AM_AUTH_CONNECTING)
77#define ClearAuthConnect(x) ((x)->flags &= ~AM_AUTH_CONNECTING)
78#define IsAuthConnect(x) ((x)->flags & AM_AUTH_CONNECTING)
79
80#define SetAuthPending(x) ((x)->flags |= AM_AUTH_PENDING)
81#define ClearAuthPending(x) ((x)->flags &= AM_AUTH_PENDING)
82#define IsAuthPending(x) ((x)->flags & AM_AUTH_PENDING)
83
84#define ClearAuth(x) ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
85#define IsDoingAuth(x) ((x)->flags & (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
86
212380e3 87/*
88 * a bit different approach
89 * this replaces the original sendheader macros
90 */
91
92static const char *HeaderMessages[] =
93{
5366977b 94 ":*** Looking up your hostname...",
95 ":*** Found your hostname",
96 ":*** Couldn't look up your hostname",
97 ":*** Checking Ident",
98 ":*** Got Ident response",
99 ":*** No Ident response",
100 ":*** Your hostname is too long, ignoring hostname",
101 ":*** Your forward and reverse DNS do not match, ignoring hostname",
102 ":*** Cannot verify hostname validity, ignoring hostname",
212380e3 103};
104
105typedef enum
106{
107 REPORT_DO_DNS,
108 REPORT_FIN_DNS,
109 REPORT_FAIL_DNS,
110 REPORT_DO_ID,
111 REPORT_FIN_ID,
112 REPORT_FAIL_ID,
113 REPORT_HOST_TOOLONG,
114 REPORT_HOST_MISMATCH,
115 REPORT_HOST_UNKNOWN
116}
117ReportType;
118
5366977b 119#define sendheader(c, r) sendto_one_notice(c, HeaderMessages[(r)])
212380e3 120
af81d5a0 121static rb_dlink_list auth_poll_list;
6e9b4415 122static rb_bh *auth_heap;
212380e3 123static EVH timeout_auth_queries_event;
124
125static PF read_auth_reply;
126static CNCB auth_connect_callback;
127
128/*
129 * init_auth()
130 *
131 * Initialise the auth code
132 */
133void
134init_auth(void)
135{
136 /* This hook takes a struct Client for its argument */
137 memset(&auth_poll_list, 0, sizeof(auth_poll_list));
9e29fe51 138 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event, NULL, 1);
cb1130e0 139 auth_heap = rb_bh_create(sizeof(struct AuthRequest), LCLIENT_HEAP_SIZE, "auth_heap");
212380e3 140}
141
142/*
143 * make_auth_request - allocate a new auth request
144 */
145static struct AuthRequest *
146make_auth_request(struct Client *client)
147{
6e9b4415 148 struct AuthRequest *request = rb_bh_alloc(auth_heap);
212380e3 149 client->localClient->auth_request = request;
cb1130e0 150 request->F = NULL;
212380e3 151 request->client = client;
9f6bbe3c 152 request->timeout = rb_current_time() + ConfigFileEntry.connect_timeout;
212380e3 153 return request;
154}
155
156/*
157 * free_auth_request - cleanup auth request allocations
158 */
159static void
160free_auth_request(struct AuthRequest *request)
161{
6e9b4415 162 rb_bh_free(auth_heap, request);
212380e3 163}
164
165/*
166 * release_auth_client - release auth client from auth system
167 * this adds the client into the local client lists so it can be read by
168 * the main io processing loop
169 */
170static void
171release_auth_client(struct AuthRequest *auth)
172{
173 struct Client *client = auth->client;
174
175 if(IsDNSPending(auth) || IsDoingAuth(auth))
176 return;
177
178 client->localClient->auth_request = NULL;
af81d5a0 179 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3 180 free_auth_request(auth);
212380e3 181
182 /*
183 * When a client has auth'ed, we want to start reading what it sends
184 * us. This is what read_packet() does.
185 * -- adrian
186 */
187 client->localClient->allow_read = MAX_FLOOD;
af81d5a0 188 rb_dlinkAddTail(client, &client->node, &global_client_list);
cb1130e0 189 read_packet(client->localClient->F, client);
212380e3 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
af81d5a0 211 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3 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 }
2c2e0aa9 235#ifdef RB_IPV6
212380e3 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 {
907468c4 258 rb_strlcpy(auth->client->host, reply->h_name, sizeof(auth->client->host));
212380e3 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{
83251205 276 ++ServerStats.is_abad;
212380e3 277
cb1130e0
JT
278 rb_close(auth->F);
279 auth->F = NULL;
212380e3 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{
cb1130e0 298 struct rb_sockaddr_storage localaddr, destaddr;
cb1130e0 299 rb_fde_t *F;
212380e3 300 int family;
301
302 if(IsAnyDead(auth->client))
303 return 0;
304
305 family = auth->client->localClient->ip.ss_family;
cb1130e0 306 if((F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL)
212380e3 307 {
d7b7d8bb 308 ilog_error("creating auth stream socket");
83251205 309 ++ServerStats.is_abad;
212380e3 310 return 0;
311 }
6fcb8629 312
313 /*
314 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
315 */
cb1130e0 316 if((maxconnections - 10) < rb_get_fd(F))
212380e3 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));
cb1130e0 321 rb_close(F);
212380e3 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 */
07c8448a 334 localaddr = auth->client->preClient->lip;
212380e3 335
cb1130e0 336 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
2c2e0aa9 337#ifdef RB_IPV6
212380e3 338 if(localaddr.ss_family == AF_INET6)
339 {
0a93f25c 340 auth->lport = ntohs(((struct sockaddr_in6 *)&localaddr)->sin6_port);
212380e3 341 ((struct sockaddr_in6 *)&localaddr)->sin6_port = 0;
0a93f25c
JT
342 }
343 else
212380e3 344#endif
0a93f25c
JT
345 {
346 auth->lport = ntohs(((struct sockaddr_in *)&localaddr)->sin_port);
347 ((struct sockaddr_in *)&localaddr)->sin_port = 0;
348 }
cb1130e0
JT
349
350 destaddr = auth->client->localClient->ip;
2c2e0aa9 351#ifdef RB_IPV6
cb1130e0
JT
352 if(localaddr.ss_family == AF_INET6)
353 {
0a93f25c 354 auth->rport = ntohs(((struct sockaddr_in6 *)&destaddr)->sin6_port);
72745c2b 355 ((struct sockaddr_in6 *)&destaddr)->sin6_port = htons(113);
0a93f25c
JT
356 }
357 else
cb1130e0 358#endif
0a93f25c
JT
359 {
360 auth->rport = ntohs(((struct sockaddr_in *)&destaddr)->sin_port);
361 ((struct sockaddr_in *)&destaddr)->sin_port = htons(113);
362 }
212380e3 363
cb1130e0 364 auth->F = F;
212380e3 365 SetAuthConnect(auth);
366
cb1130e0
JT
367 rb_connect_tcp(F, (struct sockaddr *)&destaddr,
368 (struct sockaddr *) &localaddr, GET_SS_LEN(&localaddr),
212380e3 369 auth_connect_callback, auth,
cb1130e0 370 GlobalSetOptions.ident_timeout);
212380e3 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 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
af81d5a0 462 rb_dlinkAdd(auth, &auth->node, &auth_poll_list);
212380e3 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{
af81d5a0 472 rb_dlink_node *ptr;
90a3c35b 473 rb_dlink_node *next_ptr;
212380e3 474 struct AuthRequest *auth;
475
90a3c35b 476 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, auth_poll_list.head)
212380e3 477 {
478 auth = ptr->data;
479
9f6bbe3c 480 if(auth->timeout < rb_current_time())
212380e3 481 {
cb1130e0
JT
482 if(auth->F != NULL)
483 rb_close(auth->F);
212380e3 484
485 if(IsDoingAuth(auth))
486 {
487 ClearAuth(auth);
83251205 488 ++ServerStats.is_abad;
212380e3 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
9f6bbe3c 499 auth->client->localClient->lasttime = rb_current_time();
212380e3 500 release_auth_client(auth);
501 }
502 }
503}
504
505/*
38e6acdd 506 * auth_connect_callback() - deal with the result of rb_connect_tcp()
212380e3 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
cb1130e0 517auth_connect_callback(rb_fde_t *F, int error, void *data)
212380e3 518{
519 struct AuthRequest *auth = data;
212380e3 520 char authbuf[32];
212380e3 521
522 /* Check the error */
cb1130e0 523 if(error != RB_OK)
212380e3 524 {
525 /* We had an error during connection :( */
526 auth_error(auth);
527 return;
528 }
529
38e6acdd 530 rb_snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
0a93f25c 531 auth->rport, auth->lport);
212380e3 532
30e08082 533 if(rb_write(auth->F, authbuf, strlen(authbuf)) != strlen(authbuf))
212380e3 534 {
535 auth_error(auth);
536 return;
537 }
538 ClearAuthConnect(auth);
539 SetAuthPending(auth);
cb1130e0 540 read_auth_reply(auth->F, auth);
212380e3 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
cb1130e0 553read_auth_reply(rb_fde_t *F, void *data)
212380e3 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
30e08082 562 len = rb_read(F, buf, AUTH_BUFSIZ);
212380e3 563
cb1130e0 564 if(len < 0 && rb_ignore_errno(errno))
212380e3 565 {
cb1130e0 566 rb_setselect(F, RB_SELECT_READ, read_auth_reply, auth);
212380e3 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
cb1130e0
JT
597 rb_close(auth->F);
598 auth->F = NULL;
212380e3 599 ClearAuth(auth);
600
601 if(s == NULL)
602 {
83251205 603 ++ServerStats.is_abad;
212380e3 604 strcpy(auth->client->username, "unknown");
605 sendheader(auth->client, REPORT_FAIL_ID);
606 }
607 else
608 {
609 sendheader(auth->client, REPORT_FIN_ID);
83251205 610 ++ServerStats.is_asuc;
212380e3 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
cb1130e0
JT
638 if(auth->F != NULL)
639 rb_close(auth->F);
212380e3 640
af81d5a0 641 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3 642 free_auth_request(auth);
643}