]> jfr.im git - solanum.git/blame - ircd/s_auth.c
s_auth: remove trailing whitespace at end of usernames.
[solanum.git] / ircd / 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 *
881acf00 24 */
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"
9b8e9eb3 37#include "defaults.h"
212380e3
AC
38#include "s_auth.h"
39#include "s_conf.h"
40#include "client.h"
4562c604 41#include "match.h"
212380e3
AC
42#include "ircd.h"
43#include "numeric.h"
44#include "packet.h"
1d02144f 45#include "dns.h"
4016731b 46#include "logger.h"
212380e3
AC
47#include "s_stats.h"
48#include "send.h"
212380e3
AC
49#include "hook.h"
50#include "blacklist.h"
77d3d2db 51#include "s_assert.h"
212380e3 52
1a530728
JT
53struct AuthRequest
54{
55 rb_dlink_node node;
56 struct Client *client; /* pointer to client struct for request */
1d02144f 57 uint16_t dns_id; /* DNS Query */
1a530728
JT
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
55abcbb2 120#define sendheader(c, r) sendto_one_notice(c, "%s", 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;
55abcbb2 175
212380e3
AC
176 if(IsDNSPending(auth) || IsDoingAuth(auth))
177 return;
178
179 client->localClient->auth_request = NULL;
330fc5c1 180 rb_dlinkDelete(&auth->node, &auth_poll_list);
55abcbb2 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
1d02144f 200auth_dns_callback(const char *result, int status, int aftype, void *vptr)
212380e3
AC
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
1d02144f 218 if(result != NULL && status)
212380e3
AC
219 {
220 int good = 1;
221
1d02144f 222 if(good && strlen(result) <= HOSTLEN)
212380e3 223 {
1d02144f 224 rb_strlcpy(auth->client->host, result, sizeof(auth->client->host));
212380e3
AC
225 sendheader(auth->client, REPORT_FIN_DNS);
226 }
1d02144f 227 else if (strlen(result) > HOSTLEN)
212380e3
AC
228 sendheader(auth->client, REPORT_HOST_TOOLONG);
229 }
230 else
231 sendheader(auth->client, REPORT_FAIL_DNS);
232
233 release_auth_client(auth);
234}
235
236/*
237 * authsenderr - handle auth send errors
238 */
239static void
240auth_error(struct AuthRequest *auth)
241{
47adde3d 242 ++ServerStats.is_abad;
212380e3 243
c6f49c9a
JT
244 rb_close(auth->F);
245 auth->F = NULL;
212380e3
AC
246
247 ClearAuth(auth);
248 sendheader(auth->client, REPORT_FAIL_ID);
55abcbb2 249
212380e3
AC
250 release_auth_client(auth);
251}
252
253/*
55abcbb2 254 * start_auth_query - Flag the client to show that an attempt to
212380e3
AC
255 * contact the ident server on
256 * the client's host. The connect and subsequently the socket are all put
257 * into 'non-blocking' mode. Should the connect or any later phase of the
258 * identifing process fail, it is aborted and the user is given a username
259 * of "unknown".
260 */
261static int
262start_auth_query(struct AuthRequest *auth)
263{
c6f49c9a 264 struct rb_sockaddr_storage localaddr, destaddr;
c6f49c9a 265 rb_fde_t *F;
212380e3 266 int family;
55abcbb2 267
212380e3
AC
268 if(IsAnyDead(auth->client))
269 return 0;
55abcbb2 270
e867208d 271 family = GET_SS_FAMILY(&auth->client->localClient->ip);
c6f49c9a 272 if((F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL)
212380e3 273 {
825ddf13 274 ilog_error("creating auth stream socket");
47adde3d 275 ++ServerStats.is_abad;
212380e3
AC
276 return 0;
277 }
6fcb8629
AC
278
279 /*
280 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
281 */
c6f49c9a 282 if((maxconnections - 10) < rb_get_fd(F))
212380e3
AC
283 {
284 sendto_realops_snomask(SNO_GENERAL, L_ALL,
285 "Can't allocate fd for auth on %s",
286 get_client_name(auth->client, SHOW_IP));
c6f49c9a 287 rb_close(F);
212380e3
AC
288 return 0;
289 }
290
291 sendheader(auth->client, REPORT_DO_ID);
292
55abcbb2 293 /*
212380e3
AC
294 * get the local address of the client and bind to that to
295 * make the auth request. This used to be done only for
296 * ifdef VIRTUAL_HOST, but needs to be done for all clients
297 * since the ident request must originate from that same address--
298 * and machines with multiple IP addresses are common now
299 */
3540120a 300 localaddr = auth->client->preClient->lip;
55abcbb2 301
c6f49c9a 302 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
ccda6e3f 303#ifdef RB_IPV6
e867208d 304 if(GET_SS_FAMILY(&localaddr) == AF_INET6)
212380e3 305 {
d0ebe1bc 306 auth->lport = ntohs(((struct sockaddr_in6 *)&localaddr)->sin6_port);
212380e3 307 ((struct sockaddr_in6 *)&localaddr)->sin6_port = 0;
d0ebe1bc
JT
308 }
309 else
212380e3 310#endif
d0ebe1bc
JT
311 {
312 auth->lport = ntohs(((struct sockaddr_in *)&localaddr)->sin_port);
313 ((struct sockaddr_in *)&localaddr)->sin_port = 0;
314 }
c6f49c9a
JT
315
316 destaddr = auth->client->localClient->ip;
ccda6e3f 317#ifdef RB_IPV6
e867208d 318 if(GET_SS_FAMILY(&localaddr) == AF_INET6)
c6f49c9a 319 {
d0ebe1bc 320 auth->rport = ntohs(((struct sockaddr_in6 *)&destaddr)->sin6_port);
e33c4818 321 ((struct sockaddr_in6 *)&destaddr)->sin6_port = htons(113);
d0ebe1bc
JT
322 }
323 else
c6f49c9a 324#endif
d0ebe1bc
JT
325 {
326 auth->rport = ntohs(((struct sockaddr_in *)&destaddr)->sin_port);
327 ((struct sockaddr_in *)&destaddr)->sin_port = htons(113);
328 }
55abcbb2 329
c6f49c9a 330 auth->F = F;
212380e3
AC
331 SetAuthConnect(auth);
332
c6f49c9a
JT
333 rb_connect_tcp(F, (struct sockaddr *)&destaddr,
334 (struct sockaddr *) &localaddr, GET_SS_LEN(&localaddr),
55abcbb2 335 auth_connect_callback, auth,
c6f49c9a 336 GlobalSetOptions.ident_timeout);
212380e3
AC
337 return 1; /* We suceed here for now */
338}
339
340/*
341 * GetValidIdent - parse ident query reply from identd server
55abcbb2 342 *
212380e3
AC
343 * Inputs - pointer to ident buf
344 * Output - NULL if no valid ident found, otherwise pointer to name
345 * Side effects -
346 */
347static char *
348GetValidIdent(char *buf)
349{
350 int remp = 0;
351 int locp = 0;
352 char *colon1Ptr;
353 char *colon2Ptr;
354 char *colon3Ptr;
355 char *commaPtr;
356 char *remotePortString;
357
358 /* All this to get rid of a sscanf() fun. */
359 remotePortString = buf;
360
361 colon1Ptr = strchr(remotePortString, ':');
362 if(!colon1Ptr)
363 return 0;
364
365 *colon1Ptr = '\0';
366 colon1Ptr++;
367 colon2Ptr = strchr(colon1Ptr, ':');
368 if(!colon2Ptr)
369 return 0;
370
371 *colon2Ptr = '\0';
372 colon2Ptr++;
373 commaPtr = strchr(remotePortString, ',');
374
375 if(!commaPtr)
376 return 0;
377
378 *commaPtr = '\0';
379 commaPtr++;
380
381 remp = atoi(remotePortString);
382 if(!remp)
383 return 0;
384
385 locp = atoi(commaPtr);
386 if(!locp)
387 return 0;
388
389 /* look for USERID bordered by first pair of colons */
390 if(!strstr(colon1Ptr, "USERID"))
391 return 0;
392
393 colon3Ptr = strchr(colon2Ptr, ':');
394 if(!colon3Ptr)
395 return 0;
396
397 *colon3Ptr = '\0';
398 colon3Ptr++;
399 return (colon3Ptr);
400}
401
402/*
403 * start_auth - starts auth (identd) and dns queries for a client
404 */
405void
406start_auth(struct Client *client)
407{
408 struct AuthRequest *auth = 0;
409 s_assert(0 != client);
410 if(client == NULL)
411 return;
412
212380e3
AC
413 auth = make_auth_request(client);
414
212380e3
AC
415 sendheader(client, REPORT_DO_DNS);
416
417 /* No DNS cache now, remember? -- adrian */
1d02144f 418 auth->dns_id = lookup_ip(client->sockhost, GET_SS_FAMILY(&client->localClient->ip), auth_dns_callback, auth);
212380e3
AC
419
420 SetDNSPending(auth);
421
422 if(ConfigFileEntry.disable_auth == 0)
423 start_auth_query(auth);
424
330fc5c1 425 rb_dlinkAdd(auth, &auth->node, &auth_poll_list);
212380e3
AC
426}
427
428/*
429 * timeout_auth_queries - timeout resolver and identd requests
430 * allow clients through if requests failed
431 */
432static void
433timeout_auth_queries_event(void *notused)
434{
330fc5c1 435 rb_dlink_node *ptr;
637c4932 436 rb_dlink_node *next_ptr;
212380e3
AC
437 struct AuthRequest *auth;
438
637c4932 439 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, auth_poll_list.head)
212380e3
AC
440 {
441 auth = ptr->data;
442
e3354945 443 if(auth->timeout < rb_current_time())
212380e3 444 {
c6f49c9a
JT
445 if(auth->F != NULL)
446 rb_close(auth->F);
212380e3
AC
447
448 if(IsDoingAuth(auth))
449 {
450 ClearAuth(auth);
47adde3d 451 ++ServerStats.is_abad;
212380e3
AC
452 sendheader(auth->client, REPORT_FAIL_ID);
453 auth->client->localClient->auth_request = NULL;
454 }
455 if(IsDNSPending(auth))
456 {
457 ClearDNSPending(auth);
1d02144f 458 cancel_lookup(auth->dns_id);
212380e3
AC
459 sendheader(auth->client, REPORT_FAIL_DNS);
460 }
461
e3354945 462 auth->client->localClient->lasttime = rb_current_time();
212380e3
AC
463 release_auth_client(auth);
464 }
465 }
466}
467
468/*
b2f0da88 469 * auth_connect_callback() - deal with the result of rb_connect_tcp()
212380e3
AC
470 *
471 * If the connection failed, we simply close the auth fd and report
472 * a failure. If the connection suceeded send the ident server a query
473 * giving "theirport , ourport". The write is only attempted *once* so
474 * it is deemed to be a fail if the entire write doesn't write all the
475 * data given. This shouldnt be a problem since the socket should have
476 * a write buffer far greater than this message to store it in should
477 * problems arise. -avalon
478 */
479static void
c6f49c9a 480auth_connect_callback(rb_fde_t *F, int error, void *data)
212380e3
AC
481{
482 struct AuthRequest *auth = data;
212380e3 483 char authbuf[32];
9279ad64 484 int authlen;
212380e3
AC
485
486 /* Check the error */
c6f49c9a 487 if(error != RB_OK)
212380e3
AC
488 {
489 /* We had an error during connection :( */
490 auth_error(auth);
491 return;
492 }
493
5203cba5 494 snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
d0ebe1bc 495 auth->rport, auth->lport);
9279ad64 496 authlen = strlen(authbuf);
212380e3 497
9279ad64 498 if(rb_write(auth->F, authbuf, authlen) != authlen)
212380e3
AC
499 {
500 auth_error(auth);
501 return;
502 }
503 ClearAuthConnect(auth);
504 SetAuthPending(auth);
c6f49c9a 505 read_auth_reply(auth->F, auth);
212380e3
AC
506}
507
508
509/*
55abcbb2 510 * read_auth_reply - read the reply (if any) from the ident server
212380e3
AC
511 * we connected to.
512 * We only give it one shot, if the reply isn't good the first time
513 * fail the authentication entirely. --Bleep
514 */
515#define AUTH_BUFSIZ 128
516
517static void
c6f49c9a 518read_auth_reply(rb_fde_t *F, void *data)
212380e3
AC
519{
520 struct AuthRequest *auth = data;
521 char *s = NULL;
522 char *t = NULL;
523 int len;
524 int count;
525 char buf[AUTH_BUFSIZ + 1]; /* buffer to read auth reply into */
526
dd1b211f 527 len = rb_read(F, buf, AUTH_BUFSIZ);
212380e3 528
c6f49c9a 529 if(len < 0 && rb_ignore_errno(errno))
212380e3 530 {
c6f49c9a 531 rb_setselect(F, RB_SELECT_READ, read_auth_reply, auth);
212380e3
AC
532 return;
533 }
534
535 if(len > 0)
536 {
537 buf[len] = '\0';
538
539 if((s = GetValidIdent(buf)))
540 {
541 t = auth->client->username;
542
543 while (*s == '~' || *s == '^')
544 s++;
545
546 for (count = USERLEN; *s && count; s++)
547 {
3f703993 548 if(*s == '@' || *s == '\r' || *s == '\n')
212380e3
AC
549 {
550 break;
551 }
552 if(!IsSpace(*s) && *s != ':' && *s != '[')
553 {
554 *t++ = *s;
555 count--;
556 }
557 }
558 *t = '\0';
559 }
560 }
561
c6f49c9a
JT
562 rb_close(auth->F);
563 auth->F = NULL;
212380e3
AC
564 ClearAuth(auth);
565
566 if(s == NULL)
567 {
47adde3d 568 ++ServerStats.is_abad;
212380e3
AC
569 strcpy(auth->client->username, "unknown");
570 sendheader(auth->client, REPORT_FAIL_ID);
571 }
572 else
573 {
574 sendheader(auth->client, REPORT_FIN_ID);
47adde3d 575 ++ServerStats.is_asuc;
212380e3
AC
576 SetGotId(auth->client);
577 }
578
579 release_auth_client(auth);
580}
581
582
583
584/*
585 * delete_auth_queries()
212380e3
AC
586 */
587void
588delete_auth_queries(struct Client *target_p)
589{
590 struct AuthRequest *auth;
591
592 if(target_p == NULL || target_p->localClient == NULL ||
593 target_p->localClient->auth_request == NULL)
594 return;
55abcbb2 595
212380e3
AC
596 auth = target_p->localClient->auth_request;
597 target_p->localClient->auth_request = NULL;
598
599 if(IsDNSPending(auth))
1d02144f 600 cancel_lookup(auth->dns_id);
212380e3 601
c6f49c9a
JT
602 if(auth->F != NULL)
603 rb_close(auth->F);
55abcbb2 604
330fc5c1 605 rb_dlinkDelete(&auth->node, &auth_poll_list);
212380e3
AC
606 free_auth_request(auth);
607}