2 * ircd-ratbox: A slightly useful ircd.
3 * s_auth.c: Functions for querying a users ident.
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
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.
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.
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
24 * $Id: s_auth.c 3354 2007-04-03 09:21:31Z nenolod $ */
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>
51 #include "blacklist.h"
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 */
66 * flag values for AuthRequest
67 * NAMESPACE: AM_xxx - Authentication Module
69 #define AM_AUTH_CONNECTING (1 << 0)
70 #define AM_AUTH_PENDING (1 << 1)
71 #define AM_DNS_PENDING (1 << 2)
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)
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)
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)
85 #define ClearAuth(x) ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
86 #define IsDoingAuth(x) ((x)->flags & (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
89 * a bit different approach
90 * this replaces the original sendheader macros
93 static const char *HeaderMessages
[] =
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",
115 REPORT_HOST_MISMATCH
,
120 #define sendheader(c, r) sendto_one_notice(c, "%s", HeaderMessages[(r)])
122 static rb_dlink_list auth_poll_list
;
123 static rb_bh
*auth_heap
;
124 static EVH timeout_auth_queries_event
;
126 static PF read_auth_reply
;
127 static CNCB auth_connect_callback
;
132 * Initialise the auth code
137 /* This hook takes a struct Client for its argument */
138 memset(&auth_poll_list
, 0, sizeof(auth_poll_list
));
139 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event
, NULL
, 1);
140 auth_heap
= rb_bh_create(sizeof(struct AuthRequest
), LCLIENT_HEAP_SIZE
, "auth_heap");
144 * make_auth_request - allocate a new auth request
146 static struct AuthRequest
*
147 make_auth_request(struct Client
*client
)
149 struct AuthRequest
*request
= rb_bh_alloc(auth_heap
);
150 client
->localClient
->auth_request
= request
;
152 request
->client
= client
;
153 request
->timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
;
158 * free_auth_request - cleanup auth request allocations
161 free_auth_request(struct AuthRequest
*request
)
163 rb_bh_free(auth_heap
, request
);
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
172 release_auth_client(struct AuthRequest
*auth
)
174 struct Client
*client
= auth
->client
;
176 if(IsDNSPending(auth
) || IsDoingAuth(auth
))
179 client
->localClient
->auth_request
= NULL
;
180 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
181 free_auth_request(auth
);
184 * When a client has auth'ed, we want to start reading what it sends
185 * us. This is what read_packet() does.
188 rb_dlinkAddTail(client
, &client
->node
, &global_client_list
);
189 read_packet(client
->localClient
->F
, client
);
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
200 auth_dns_callback(void *vptr
, struct DNSReply
*reply
)
202 struct AuthRequest
*auth
= (struct AuthRequest
*) vptr
;
203 ClearDNSPending(auth
);
205 /* XXX: this shouldn't happen, but it does. -nenolod */
206 if(auth
->client
->localClient
== NULL
)
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
));
211 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
212 free_auth_request(auth
);
214 /* and they will silently drop through and all will hopefully be ok... -nenolod */
222 if(auth
->client
->localClient
->ip
.ss_family
== AF_INET
)
224 struct sockaddr_in
*ip
, *ip_fwd
;
226 ip
= (struct sockaddr_in
*) &auth
->client
->localClient
->ip
;
227 ip_fwd
= (struct sockaddr_in
*) &reply
->addr
;
229 if(ip
->sin_addr
.s_addr
!= ip_fwd
->sin_addr
.s_addr
)
231 sendheader(auth
->client
, REPORT_HOST_MISMATCH
);
236 else if(auth
->client
->localClient
->ip
.ss_family
== AF_INET6
)
238 struct sockaddr_in6
*ip
, *ip_fwd
;
240 ip
= (struct sockaddr_in6
*) &auth
->client
->localClient
->ip
;
241 ip_fwd
= (struct sockaddr_in6
*) &reply
->addr
;
243 if(memcmp(&ip
->sin6_addr
, &ip_fwd
->sin6_addr
, sizeof(struct in6_addr
)) != 0)
245 sendheader(auth
->client
, REPORT_HOST_MISMATCH
);
250 else /* can't verify it, don't know how. reject it. */
252 sendheader(auth
->client
, REPORT_HOST_UNKNOWN
);
256 if(good
&& strlen(reply
->h_name
) <= HOSTLEN
)
258 rb_strlcpy(auth
->client
->host
, reply
->h_name
, sizeof(auth
->client
->host
));
259 sendheader(auth
->client
, REPORT_FIN_DNS
);
261 else if (strlen(reply
->h_name
) > HOSTLEN
)
262 sendheader(auth
->client
, REPORT_HOST_TOOLONG
);
265 sendheader(auth
->client
, REPORT_FAIL_DNS
);
267 release_auth_client(auth
);
271 * authsenderr - handle auth send errors
274 auth_error(struct AuthRequest
*auth
)
276 ++ServerStats
.is_abad
;
282 sendheader(auth
->client
, REPORT_FAIL_ID
);
284 release_auth_client(auth
);
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
296 start_auth_query(struct AuthRequest
*auth
)
298 struct rb_sockaddr_storage localaddr
, destaddr
;
302 if(IsAnyDead(auth
->client
))
305 family
= auth
->client
->localClient
->ip
.ss_family
;
306 if((F
= rb_socket(family
, SOCK_STREAM
, 0, "ident")) == NULL
)
308 ilog_error("creating auth stream socket");
309 ++ServerStats
.is_abad
;
314 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
316 if((maxconnections
- 10) < rb_get_fd(F
))
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
));
325 sendheader(auth
->client
, REPORT_DO_ID
);
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
334 localaddr
= auth
->client
->preClient
->lip
;
336 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
338 if(localaddr
.ss_family
== AF_INET6
)
340 auth
->lport
= ntohs(((struct sockaddr_in6
*)&localaddr
)->sin6_port
);
341 ((struct sockaddr_in6
*)&localaddr
)->sin6_port
= 0;
346 auth
->lport
= ntohs(((struct sockaddr_in
*)&localaddr
)->sin_port
);
347 ((struct sockaddr_in
*)&localaddr
)->sin_port
= 0;
350 destaddr
= auth
->client
->localClient
->ip
;
352 if(localaddr
.ss_family
== AF_INET6
)
354 auth
->rport
= ntohs(((struct sockaddr_in6
*)&destaddr
)->sin6_port
);
355 ((struct sockaddr_in6
*)&destaddr
)->sin6_port
= htons(113);
360 auth
->rport
= ntohs(((struct sockaddr_in
*)&destaddr
)->sin_port
);
361 ((struct sockaddr_in
*)&destaddr
)->sin_port
= htons(113);
365 SetAuthConnect(auth
);
367 rb_connect_tcp(F
, (struct sockaddr
*)&destaddr
,
368 (struct sockaddr
*) &localaddr
, GET_SS_LEN(&localaddr
),
369 auth_connect_callback
, auth
,
370 GlobalSetOptions
.ident_timeout
);
371 return 1; /* We suceed here for now */
375 * GetValidIdent - parse ident query reply from identd server
377 * Inputs - pointer to ident buf
378 * Output - NULL if no valid ident found, otherwise pointer to name
382 GetValidIdent(char *buf
)
390 char *remotePortString
;
392 /* All this to get rid of a sscanf() fun. */
393 remotePortString
= buf
;
395 colon1Ptr
= strchr(remotePortString
, ':');
401 colon2Ptr
= strchr(colon1Ptr
, ':');
407 commaPtr
= strchr(remotePortString
, ',');
415 remp
= atoi(remotePortString
);
419 locp
= atoi(commaPtr
);
423 /* look for USERID bordered by first pair of colons */
424 if(!strstr(colon1Ptr
, "USERID"))
427 colon3Ptr
= strchr(colon2Ptr
, ':');
437 * start_auth - starts auth (identd) and dns queries for a client
440 start_auth(struct Client
*client
)
442 struct AuthRequest
*auth
= 0;
443 s_assert(0 != client
);
447 auth
= make_auth_request(client
);
449 auth
->dns_query
.ptr
= auth
;
450 auth
->dns_query
.callback
= auth_dns_callback
;
452 sendheader(client
, REPORT_DO_DNS
);
454 /* No DNS cache now, remember? -- adrian */
455 gethost_byaddr(&client
->localClient
->ip
, &auth
->dns_query
);
459 if(ConfigFileEntry
.disable_auth
== 0)
460 start_auth_query(auth
);
462 rb_dlinkAdd(auth
, &auth
->node
, &auth_poll_list
);
466 * timeout_auth_queries - timeout resolver and identd requests
467 * allow clients through if requests failed
470 timeout_auth_queries_event(void *notused
)
473 rb_dlink_node
*next_ptr
;
474 struct AuthRequest
*auth
;
476 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, auth_poll_list
.head
)
480 if(auth
->timeout
< rb_current_time())
485 if(IsDoingAuth(auth
))
488 ++ServerStats
.is_abad
;
489 sendheader(auth
->client
, REPORT_FAIL_ID
);
490 auth
->client
->localClient
->auth_request
= NULL
;
492 if(IsDNSPending(auth
))
494 ClearDNSPending(auth
);
495 delete_resolver_queries(&auth
->dns_query
);
496 sendheader(auth
->client
, REPORT_FAIL_DNS
);
499 auth
->client
->localClient
->lasttime
= rb_current_time();
500 release_auth_client(auth
);
506 * auth_connect_callback() - deal with the result of rb_connect_tcp()
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
517 auth_connect_callback(rb_fde_t
*F
, int error
, void *data
)
519 struct AuthRequest
*auth
= data
;
522 /* Check the error */
525 /* We had an error during connection :( */
530 rb_snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
531 auth
->rport
, auth
->lport
);
533 if(rb_write(auth
->F
, authbuf
, strlen(authbuf
)) != strlen(authbuf
))
538 ClearAuthConnect(auth
);
539 SetAuthPending(auth
);
540 read_auth_reply(auth
->F
, auth
);
545 * read_auth_reply - read the reply (if any) from the ident server
547 * We only give it one shot, if the reply isn't good the first time
548 * fail the authentication entirely. --Bleep
550 #define AUTH_BUFSIZ 128
553 read_auth_reply(rb_fde_t
*F
, void *data
)
555 struct AuthRequest
*auth
= data
;
560 char buf
[AUTH_BUFSIZ
+ 1]; /* buffer to read auth reply into */
562 len
= rb_read(F
, buf
, AUTH_BUFSIZ
);
564 if(len
< 0 && rb_ignore_errno(errno
))
566 rb_setselect(F
, RB_SELECT_READ
, read_auth_reply
, auth
);
574 if((s
= GetValidIdent(buf
)))
576 t
= auth
->client
->username
;
578 while (*s
== '~' || *s
== '^')
581 for (count
= USERLEN
; *s
&& count
; s
++)
587 if(!IsSpace(*s
) && *s
!= ':' && *s
!= '[')
603 ++ServerStats
.is_abad
;
604 strcpy(auth
->client
->username
, "unknown");
605 sendheader(auth
->client
, REPORT_FAIL_ID
);
609 sendheader(auth
->client
, REPORT_FIN_ID
);
610 ++ServerStats
.is_asuc
;
611 SetGotId(auth
->client
);
614 release_auth_client(auth
);
620 * delete_auth_queries()
624 delete_auth_queries(struct Client
*target_p
)
626 struct AuthRequest
*auth
;
628 if(target_p
== NULL
|| target_p
->localClient
== NULL
||
629 target_p
->localClient
->auth_request
== NULL
)
632 auth
= target_p
->localClient
->auth_request
;
633 target_p
->localClient
->auth_request
= NULL
;
635 if(IsDNSPending(auth
))
636 delete_resolver_queries(&auth
->dns_query
);
641 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
642 free_auth_request(auth
);