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