]> jfr.im git - irc/rqf/shadowircd.git/blame - src/s_auth.c
ilog_error() replaces legacy report_error() craq
[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"
212380e3 48#include "s_log.h"
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 {
276 report_error("creating auth stream socket %s:%s",
277 get_client_name(auth->client, SHOW_IP),
278 log_client_name(auth->client, SHOW_IP), errno);
21c9d815 279 ++ServerStats->is_abad;
212380e3 280 return 0;
281 }
6fcb8629 282
283 /*
284 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
285 */
cb1130e0 286 if((maxconnections - 10) < rb_get_fd(F))
212380e3 287 {
288 sendto_realops_snomask(SNO_GENERAL, L_ALL,
289 "Can't allocate fd for auth on %s",
290 get_client_name(auth->client, SHOW_IP));
cb1130e0 291 rb_close(F);
212380e3 292 return 0;
293 }
294
295 sendheader(auth->client, REPORT_DO_ID);
296
297 /*
298 * get the local address of the client and bind to that to
299 * make the auth request. This used to be done only for
300 * ifdef VIRTUAL_HOST, but needs to be done for all clients
301 * since the ident request must originate from that same address--
302 * and machines with multiple IP addresses are common now
303 */
304 memset(&localaddr, 0, locallen);
cb1130e0 305 getsockname(rb_get_fd(auth->client->localClient->F),
212380e3 306 (struct sockaddr *) &localaddr, &locallen);
307
cb1130e0 308 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
212380e3 309#ifdef IPV6
310 if(localaddr.ss_family == AF_INET6)
311 {
312 ((struct sockaddr_in6 *)&localaddr)->sin6_port = 0;
313 } else
314#endif
315 ((struct sockaddr_in *)&localaddr)->sin_port = 0;
cb1130e0
JT
316
317 destaddr = auth->client->localClient->ip;
318#ifdef IPV6
319 if(localaddr.ss_family == AF_INET6)
320 {
321 ((struct sockaddr_in6 *)&localaddr)->sin6_port = 113;
322 } else
323#endif
324 ((struct sockaddr_in *)&localaddr)->sin_port = 113;
212380e3 325
cb1130e0 326 auth->F = F;
212380e3 327 SetAuthConnect(auth);
328
cb1130e0
JT
329 rb_connect_tcp(F, (struct sockaddr *)&destaddr,
330 (struct sockaddr *) &localaddr, GET_SS_LEN(&localaddr),
212380e3 331 auth_connect_callback, auth,
cb1130e0 332 GlobalSetOptions.ident_timeout);
212380e3 333 return 1; /* We suceed here for now */
334}
335
336/*
337 * GetValidIdent - parse ident query reply from identd server
338 *
339 * Inputs - pointer to ident buf
340 * Output - NULL if no valid ident found, otherwise pointer to name
341 * Side effects -
342 */
343static char *
344GetValidIdent(char *buf)
345{
346 int remp = 0;
347 int locp = 0;
348 char *colon1Ptr;
349 char *colon2Ptr;
350 char *colon3Ptr;
351 char *commaPtr;
352 char *remotePortString;
353
354 /* All this to get rid of a sscanf() fun. */
355 remotePortString = buf;
356
357 colon1Ptr = strchr(remotePortString, ':');
358 if(!colon1Ptr)
359 return 0;
360
361 *colon1Ptr = '\0';
362 colon1Ptr++;
363 colon2Ptr = strchr(colon1Ptr, ':');
364 if(!colon2Ptr)
365 return 0;
366
367 *colon2Ptr = '\0';
368 colon2Ptr++;
369 commaPtr = strchr(remotePortString, ',');
370
371 if(!commaPtr)
372 return 0;
373
374 *commaPtr = '\0';
375 commaPtr++;
376
377 remp = atoi(remotePortString);
378 if(!remp)
379 return 0;
380
381 locp = atoi(commaPtr);
382 if(!locp)
383 return 0;
384
385 /* look for USERID bordered by first pair of colons */
386 if(!strstr(colon1Ptr, "USERID"))
387 return 0;
388
389 colon3Ptr = strchr(colon2Ptr, ':');
390 if(!colon3Ptr)
391 return 0;
392
393 *colon3Ptr = '\0';
394 colon3Ptr++;
395 return (colon3Ptr);
396}
397
398/*
399 * start_auth - starts auth (identd) and dns queries for a client
400 */
401void
402start_auth(struct Client *client)
403{
404 struct AuthRequest *auth = 0;
405 s_assert(0 != client);
406 if(client == NULL)
407 return;
408
212380e3 409 auth = make_auth_request(client);
410
411 auth->dns_query.ptr = auth;
412 auth->dns_query.callback = auth_dns_callback;
413
414 sendheader(client, REPORT_DO_DNS);
415
416 /* No DNS cache now, remember? -- adrian */
417 gethost_byaddr(&client->localClient->ip, &auth->dns_query);
418
419 SetDNSPending(auth);
420
421 if(ConfigFileEntry.disable_auth == 0)
422 start_auth_query(auth);
423
af81d5a0 424 rb_dlinkAdd(auth, &auth->node, &auth_poll_list);
212380e3 425}
426
427/*
428 * timeout_auth_queries - timeout resolver and identd requests
429 * allow clients through if requests failed
430 */
431static void
432timeout_auth_queries_event(void *notused)
433{
af81d5a0 434 rb_dlink_node *ptr;
90a3c35b 435 rb_dlink_node *next_ptr;
212380e3 436 struct AuthRequest *auth;
437
90a3c35b 438 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, auth_poll_list.head)
212380e3 439 {
440 auth = ptr->data;
441
9f6bbe3c 442 if(auth->timeout < rb_current_time())
212380e3 443 {
cb1130e0
JT
444 if(auth->F != NULL)
445 rb_close(auth->F);
212380e3 446
447 if(IsDoingAuth(auth))
448 {
449 ClearAuth(auth);
21c9d815 450 ++ServerStats->is_abad;
212380e3 451 sendheader(auth->client, REPORT_FAIL_ID);
452 auth->client->localClient->auth_request = NULL;
453 }
454 if(IsDNSPending(auth))
455 {
456 ClearDNSPending(auth);
457 delete_resolver_queries(&auth->dns_query);
458 sendheader(auth->client, REPORT_FAIL_DNS);
459 }
460
9f6bbe3c 461 auth->client->localClient->lasttime = rb_current_time();
212380e3 462 release_auth_client(auth);
463 }
464 }
465}
466
467/*
38e6acdd 468 * auth_connect_callback() - deal with the result of rb_connect_tcp()
212380e3 469 *
470 * If the connection failed, we simply close the auth fd and report
471 * a failure. If the connection suceeded send the ident server a query
472 * giving "theirport , ourport". The write is only attempted *once* so
473 * it is deemed to be a fail if the entire write doesn't write all the
474 * data given. This shouldnt be a problem since the socket should have
475 * a write buffer far greater than this message to store it in should
476 * problems arise. -avalon
477 */
478static void
cb1130e0 479auth_connect_callback(rb_fde_t *F, int error, void *data)
212380e3 480{
481 struct AuthRequest *auth = data;
482 struct sockaddr_in us;
483 struct sockaddr_in them;
484 char authbuf[32];
485 socklen_t ulen = sizeof(struct sockaddr_in);
486 socklen_t tlen = sizeof(struct sockaddr_in);
487
488 /* Check the error */
cb1130e0 489 if(error != RB_OK)
212380e3 490 {
491 /* We had an error during connection :( */
492 auth_error(auth);
493 return;
494 }
495
496 if(getsockname
cb1130e0 497 (rb_get_fd(auth->client->localClient->F), (struct sockaddr *) &us,
212380e3 498 (socklen_t *) & ulen)
cb1130e0 499 || getpeername(rb_get_fd(auth->client->localClient->F),
212380e3 500 (struct sockaddr *) &them, (socklen_t *) & tlen))
501 {
502 ilog(L_IOERROR, "auth get{sock,peer}name error for %s:%m",
503 log_client_name(auth->client, SHOW_IP));
504 auth_error(auth);
505 return;
506 }
38e6acdd 507 rb_snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
212380e3 508 (unsigned int) ntohs(them.sin_port), (unsigned int) ntohs(us.sin_port));
509
cb1130e0 510 if(write(rb_get_fd(auth->F), authbuf, strlen(authbuf)) == -1)
212380e3 511 {
512 auth_error(auth);
513 return;
514 }
515 ClearAuthConnect(auth);
516 SetAuthPending(auth);
cb1130e0 517 read_auth_reply(auth->F, auth);
212380e3 518}
519
520
521/*
522 * read_auth_reply - read the reply (if any) from the ident server
523 * we connected to.
524 * We only give it one shot, if the reply isn't good the first time
525 * fail the authentication entirely. --Bleep
526 */
527#define AUTH_BUFSIZ 128
528
529static void
cb1130e0 530read_auth_reply(rb_fde_t *F, void *data)
212380e3 531{
532 struct AuthRequest *auth = data;
533 char *s = NULL;
534 char *t = NULL;
535 int len;
536 int count;
537 char buf[AUTH_BUFSIZ + 1]; /* buffer to read auth reply into */
538
cb1130e0 539 len = read(rb_get_fd(F), buf, AUTH_BUFSIZ);
212380e3 540
cb1130e0 541 if(len < 0 && rb_ignore_errno(errno))
212380e3 542 {
cb1130e0 543 rb_setselect(F, RB_SELECT_READ, read_auth_reply, auth);
212380e3 544 return;
545 }
546
547 if(len > 0)
548 {
549 buf[len] = '\0';
550
551 if((s = GetValidIdent(buf)))
552 {
553 t = auth->client->username;
554
555 while (*s == '~' || *s == '^')
556 s++;
557
558 for (count = USERLEN; *s && count; s++)
559 {
560 if(*s == '@')
561 {
562 break;
563 }
564 if(!IsSpace(*s) && *s != ':' && *s != '[')
565 {
566 *t++ = *s;
567 count--;
568 }
569 }
570 *t = '\0';
571 }
572 }
573
cb1130e0
JT
574 rb_close(auth->F);
575 auth->F = NULL;
212380e3 576 ClearAuth(auth);
577
578 if(s == NULL)
579 {
21c9d815 580 ++ServerStats->is_abad;
212380e3 581 strcpy(auth->client->username, "unknown");
582 sendheader(auth->client, REPORT_FAIL_ID);
583 }
584 else
585 {
586 sendheader(auth->client, REPORT_FIN_ID);
21c9d815 587 ++ServerStats->is_asuc;
212380e3 588 SetGotId(auth->client);
589 }
590
591 release_auth_client(auth);
592}
593
594
595
596/*
597 * delete_auth_queries()
598 *
599 */
600void
601delete_auth_queries(struct Client *target_p)
602{
603 struct AuthRequest *auth;
604
605 if(target_p == NULL || target_p->localClient == NULL ||
606 target_p->localClient->auth_request == NULL)
607 return;
608
609 auth = target_p->localClient->auth_request;
610 target_p->localClient->auth_request = NULL;
611
612 if(IsDNSPending(auth))
613 delete_resolver_queries(&auth->dns_query);
614
cb1130e0
JT
615 if(auth->F != NULL)
616 rb_close(auth->F);
212380e3 617
af81d5a0 618 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3 619 free_auth_request(auth);
620}