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
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"
57 struct Client
*client
; /* pointer to client struct for request */
58 uint16_t dns_id
; /* DNS Query */
59 unsigned int flags
; /* current state of request */
60 rb_fde_t
*F
; /* file descriptor for auth queries */
61 time_t timeout
; /* time when query expires */
67 * flag values for AuthRequest
68 * NAMESPACE: AM_xxx - Authentication Module
70 #define AM_AUTH_CONNECTING (1 << 0)
71 #define AM_AUTH_PENDING (1 << 1)
72 #define AM_DNS_PENDING (1 << 2)
74 #define SetDNSPending(x) ((x)->flags |= AM_DNS_PENDING)
75 #define ClearDNSPending(x) ((x)->flags &= ~AM_DNS_PENDING)
76 #define IsDNSPending(x) ((x)->flags & AM_DNS_PENDING)
78 #define SetAuthConnect(x) ((x)->flags |= AM_AUTH_CONNECTING)
79 #define ClearAuthConnect(x) ((x)->flags &= ~AM_AUTH_CONNECTING)
80 #define IsAuthConnect(x) ((x)->flags & AM_AUTH_CONNECTING)
82 #define SetAuthPending(x) ((x)->flags |= AM_AUTH_PENDING)
83 #define ClearAuthPending(x) ((x)->flags &= AM_AUTH_PENDING)
84 #define IsAuthPending(x) ((x)->flags & AM_AUTH_PENDING)
86 #define ClearAuth(x) ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
87 #define IsDoingAuth(x) ((x)->flags & (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
90 * a bit different approach
91 * this replaces the original sendheader macros
94 static const char *HeaderMessages
[] =
96 ":*** Looking up your hostname...",
97 ":*** Found your hostname",
98 ":*** Couldn't look up your hostname",
99 ":*** Checking Ident",
100 ":*** Got Ident response",
101 ":*** No Ident response",
102 ":*** Your hostname is too long, ignoring hostname",
103 ":*** Your forward and reverse DNS do not match, ignoring hostname",
104 ":*** Cannot verify hostname validity, ignoring hostname",
116 REPORT_HOST_MISMATCH
,
121 #define sendheader(c, r) sendto_one_notice(c, "%s", HeaderMessages[(r)])
123 static rb_dlink_list auth_poll_list
;
124 static rb_bh
*auth_heap
;
125 static EVH timeout_auth_queries_event
;
127 static PF read_auth_reply
;
128 static CNCB auth_connect_callback
;
133 * Initialise the auth code
138 /* This hook takes a struct Client for its argument */
139 memset(&auth_poll_list
, 0, sizeof(auth_poll_list
));
140 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event
, NULL
, 1);
141 auth_heap
= rb_bh_create(sizeof(struct AuthRequest
), LCLIENT_HEAP_SIZE
, "auth_heap");
145 * make_auth_request - allocate a new auth request
147 static struct AuthRequest
*
148 make_auth_request(struct Client
*client
)
150 struct AuthRequest
*request
= rb_bh_alloc(auth_heap
);
151 client
->localClient
->auth_request
= request
;
153 request
->client
= client
;
154 request
->timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
;
159 * free_auth_request - cleanup auth request allocations
162 free_auth_request(struct AuthRequest
*request
)
164 rb_bh_free(auth_heap
, request
);
168 * release_auth_client - release auth client from auth system
169 * this adds the client into the local client lists so it can be read by
170 * the main io processing loop
173 release_auth_client(struct AuthRequest
*auth
)
175 struct Client
*client
= auth
->client
;
177 if(IsDNSPending(auth
) || IsDoingAuth(auth
))
180 client
->localClient
->auth_request
= NULL
;
181 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
182 free_auth_request(auth
);
185 * When a client has auth'ed, we want to start reading what it sends
186 * us. This is what read_packet() does.
189 rb_dlinkAddTail(client
, &client
->node
, &global_client_list
);
190 read_packet(client
->localClient
->F
, client
);
194 * auth_dns_callback - called when resolver query finishes
195 * if the query resulted in a successful search, hp will contain
196 * a non-null pointer, otherwise hp will be null.
197 * set the client on it's way to a connection completion, regardless
198 * of success of failure
201 auth_dns_callback(const char *result
, int status
, int aftype
, void *vptr
)
203 struct AuthRequest
*auth
= (struct AuthRequest
*) vptr
;
204 ClearDNSPending(auth
);
206 /* XXX: this shouldn't happen, but it does. -nenolod */
207 if(auth
->client
->localClient
== NULL
)
209 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
210 "auth_dns_callback(): auth->client->localClient (%s) is NULL", get_client_name(auth
->client
, HIDE_IP
));
212 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
213 free_auth_request(auth
);
215 /* and they will silently drop through and all will hopefully be ok... -nenolod */
219 if(result
!= NULL
&& status
)
223 if(good
&& strlen(result
) <= HOSTLEN
)
225 rb_strlcpy(auth
->client
->host
, result
, sizeof(auth
->client
->host
));
226 sendheader(auth
->client
, REPORT_FIN_DNS
);
228 else if (strlen(result
) > HOSTLEN
)
229 sendheader(auth
->client
, REPORT_HOST_TOOLONG
);
232 sendheader(auth
->client
, REPORT_FAIL_DNS
);
234 release_auth_client(auth
);
238 * authsenderr - handle auth send errors
241 auth_error(struct AuthRequest
*auth
)
243 ++ServerStats
.is_abad
;
249 sendheader(auth
->client
, REPORT_FAIL_ID
);
251 release_auth_client(auth
);
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
263 start_auth_query(struct AuthRequest
*auth
)
265 struct rb_sockaddr_storage localaddr
, destaddr
;
269 if(IsAnyDead(auth
->client
))
272 family
= GET_SS_FAMILY(&auth
->client
->localClient
->ip
);
273 if((F
= rb_socket(family
, SOCK_STREAM
, 0, "ident")) == NULL
)
275 ilog_error("creating auth stream socket");
276 ++ServerStats
.is_abad
;
281 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
283 if((maxconnections
- 10) < rb_get_fd(F
))
285 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
286 "Can't allocate fd for auth on %s",
287 get_client_name(auth
->client
, SHOW_IP
));
292 sendheader(auth
->client
, REPORT_DO_ID
);
295 * get the local address of the client and bind to that to
296 * make the auth request. This used to be done only for
297 * ifdef VIRTUAL_HOST, but needs to be done for all clients
298 * since the ident request must originate from that same address--
299 * and machines with multiple IP addresses are common now
301 localaddr
= auth
->client
->preClient
->lip
;
303 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
305 if(GET_SS_FAMILY(&localaddr
) == AF_INET6
)
307 auth
->lport
= ntohs(((struct sockaddr_in6
*)&localaddr
)->sin6_port
);
308 ((struct sockaddr_in6
*)&localaddr
)->sin6_port
= 0;
313 auth
->lport
= ntohs(((struct sockaddr_in
*)&localaddr
)->sin_port
);
314 ((struct sockaddr_in
*)&localaddr
)->sin_port
= 0;
317 destaddr
= auth
->client
->localClient
->ip
;
319 if(GET_SS_FAMILY(&localaddr
) == AF_INET6
)
321 auth
->rport
= ntohs(((struct sockaddr_in6
*)&destaddr
)->sin6_port
);
322 ((struct sockaddr_in6
*)&destaddr
)->sin6_port
= htons(113);
327 auth
->rport
= ntohs(((struct sockaddr_in
*)&destaddr
)->sin_port
);
328 ((struct sockaddr_in
*)&destaddr
)->sin_port
= htons(113);
332 SetAuthConnect(auth
);
334 rb_connect_tcp(F
, (struct sockaddr
*)&destaddr
,
335 (struct sockaddr
*) &localaddr
, GET_SS_LEN(&localaddr
),
336 auth_connect_callback
, auth
,
337 GlobalSetOptions
.ident_timeout
);
338 return 1; /* We suceed here for now */
342 * GetValidIdent - parse ident query reply from identd server
344 * Inputs - pointer to ident buf
345 * Output - NULL if no valid ident found, otherwise pointer to name
349 GetValidIdent(char *buf
)
357 char *remotePortString
;
359 /* All this to get rid of a sscanf() fun. */
360 remotePortString
= buf
;
362 colon1Ptr
= strchr(remotePortString
, ':');
368 colon2Ptr
= strchr(colon1Ptr
, ':');
374 commaPtr
= strchr(remotePortString
, ',');
382 remp
= atoi(remotePortString
);
386 locp
= atoi(commaPtr
);
390 /* look for USERID bordered by first pair of colons */
391 if(!strstr(colon1Ptr
, "USERID"))
394 colon3Ptr
= strchr(colon2Ptr
, ':');
404 * start_auth - starts auth (identd) and dns queries for a client
407 start_auth(struct Client
*client
)
409 struct AuthRequest
*auth
= 0;
410 s_assert(0 != client
);
414 auth
= make_auth_request(client
);
416 sendheader(client
, REPORT_DO_DNS
);
418 /* No DNS cache now, remember? -- adrian */
419 auth
->dns_id
= lookup_ip(client
->sockhost
, GET_SS_FAMILY(&client
->localClient
->ip
), auth_dns_callback
, auth
);
423 if(ConfigFileEntry
.disable_auth
== 0)
424 start_auth_query(auth
);
426 rb_dlinkAdd(auth
, &auth
->node
, &auth_poll_list
);
430 * timeout_auth_queries - timeout resolver and identd requests
431 * allow clients through if requests failed
434 timeout_auth_queries_event(void *notused
)
437 rb_dlink_node
*next_ptr
;
438 struct AuthRequest
*auth
;
440 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, auth_poll_list
.head
)
444 if(auth
->timeout
< rb_current_time())
449 if(IsDoingAuth(auth
))
452 ++ServerStats
.is_abad
;
453 sendheader(auth
->client
, REPORT_FAIL_ID
);
454 auth
->client
->localClient
->auth_request
= NULL
;
456 if(IsDNSPending(auth
))
458 ClearDNSPending(auth
);
459 cancel_lookup(auth
->dns_id
);
460 sendheader(auth
->client
, REPORT_FAIL_DNS
);
463 auth
->client
->localClient
->lasttime
= rb_current_time();
464 release_auth_client(auth
);
470 * auth_connect_callback() - deal with the result of rb_connect_tcp()
472 * If the connection failed, we simply close the auth fd and report
473 * a failure. If the connection suceeded send the ident server a query
474 * giving "theirport , ourport". The write is only attempted *once* so
475 * it is deemed to be a fail if the entire write doesn't write all the
476 * data given. This shouldnt be a problem since the socket should have
477 * a write buffer far greater than this message to store it in should
478 * problems arise. -avalon
481 auth_connect_callback(rb_fde_t
*F
, int error
, void *data
)
483 struct AuthRequest
*auth
= data
;
487 /* Check the error */
490 /* We had an error during connection :( */
495 snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
496 auth
->rport
, auth
->lport
);
497 authlen
= strlen(authbuf
);
499 if(rb_write(auth
->F
, authbuf
, authlen
) != authlen
)
504 ClearAuthConnect(auth
);
505 SetAuthPending(auth
);
506 read_auth_reply(auth
->F
, auth
);
511 * read_auth_reply - read the reply (if any) from the ident server
513 * We only give it one shot, if the reply isn't good the first time
514 * fail the authentication entirely. --Bleep
516 #define AUTH_BUFSIZ 128
519 read_auth_reply(rb_fde_t
*F
, void *data
)
521 struct AuthRequest
*auth
= data
;
526 char buf
[AUTH_BUFSIZ
+ 1]; /* buffer to read auth reply into */
528 len
= rb_read(F
, buf
, AUTH_BUFSIZ
);
530 if(len
< 0 && rb_ignore_errno(errno
))
532 rb_setselect(F
, RB_SELECT_READ
, read_auth_reply
, auth
);
540 if((s
= GetValidIdent(buf
)))
542 t
= auth
->client
->username
;
544 while (*s
== '~' || *s
== '^')
547 for (count
= USERLEN
; *s
&& count
; s
++)
553 if(!IsSpace(*s
) && *s
!= ':' && *s
!= '[')
569 ++ServerStats
.is_abad
;
570 strcpy(auth
->client
->username
, "unknown");
571 sendheader(auth
->client
, REPORT_FAIL_ID
);
575 sendheader(auth
->client
, REPORT_FIN_ID
);
576 ++ServerStats
.is_asuc
;
577 SetGotId(auth
->client
);
580 release_auth_client(auth
);
586 * delete_auth_queries()
589 delete_auth_queries(struct Client
*target_p
)
591 struct AuthRequest
*auth
;
593 if(target_p
== NULL
|| target_p
->localClient
== NULL
||
594 target_p
->localClient
->auth_request
== NULL
)
597 auth
= target_p
->localClient
->auth_request
;
598 target_p
->localClient
->auth_request
= NULL
;
600 if(IsDNSPending(auth
))
601 cancel_lookup(auth
->dns_id
);
606 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
607 free_auth_request(auth
);