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>
42 #include "irc_string.h"
43 #include "sprintf_irc.h"
52 #include "blacklist.h"
55 * a bit different approach
56 * this replaces the original sendheader macros
59 static const char *HeaderMessages
[] =
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",
86 #define sendheader(c, r) sendto_one_notice(c, HeaderMessages[(r)])
88 static rb_dlink_list auth_poll_list
;
89 static rb_bh
*auth_heap
;
90 static EVH timeout_auth_queries_event
;
92 static PF read_auth_reply
;
93 static CNCB auth_connect_callback
;
98 * Initialise the auth code
103 /* This hook takes a struct Client for its argument */
104 memset(&auth_poll_list
, 0, sizeof(auth_poll_list
));
105 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event
, NULL
, 1);
106 auth_heap
= rb_bh_create(sizeof(struct AuthRequest
), LCLIENT_HEAP_SIZE
, "auth_heap");
110 * make_auth_request - allocate a new auth request
112 static struct AuthRequest
*
113 make_auth_request(struct Client
*client
)
115 struct AuthRequest
*request
= rb_bh_alloc(auth_heap
);
116 client
->localClient
->auth_request
= request
;
118 request
->client
= client
;
119 request
->timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
;
124 * free_auth_request - cleanup auth request allocations
127 free_auth_request(struct AuthRequest
*request
)
129 rb_bh_free(auth_heap
, request
);
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
138 release_auth_client(struct AuthRequest
*auth
)
140 struct Client
*client
= auth
->client
;
142 if(IsDNSPending(auth
) || IsDoingAuth(auth
))
145 client
->localClient
->auth_request
= NULL
;
146 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
147 free_auth_request(auth
);
150 * When a client has auth'ed, we want to start reading what it sends
151 * us. This is what read_packet() does.
154 client
->localClient
->allow_read
= MAX_FLOOD
;
155 rb_dlinkAddTail(client
, &client
->node
, &global_client_list
);
156 read_packet(client
->localClient
->F
, client
);
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
167 auth_dns_callback(void *vptr
, struct DNSReply
*reply
)
169 struct AuthRequest
*auth
= (struct AuthRequest
*) vptr
;
170 ClearDNSPending(auth
);
172 /* XXX: this shouldn't happen, but it does. -nenolod */
173 if(auth
->client
->localClient
== NULL
)
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
));
178 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
179 free_auth_request(auth
);
181 /* and they will silently drop through and all will hopefully be ok... -nenolod */
189 if(auth
->client
->localClient
->ip
.ss_family
== AF_INET
)
191 struct sockaddr_in
*ip
, *ip_fwd
;
193 ip
= (struct sockaddr_in
*) &auth
->client
->localClient
->ip
;
194 ip_fwd
= (struct sockaddr_in
*) &reply
->addr
;
196 if(ip
->sin_addr
.s_addr
!= ip_fwd
->sin_addr
.s_addr
)
198 sendheader(auth
->client
, REPORT_HOST_MISMATCH
);
203 else if(auth
->client
->localClient
->ip
.ss_family
== AF_INET6
)
205 struct sockaddr_in6
*ip
, *ip_fwd
;
207 ip
= (struct sockaddr_in6
*) &auth
->client
->localClient
->ip
;
208 ip_fwd
= (struct sockaddr_in6
*) &reply
->addr
;
210 if(memcmp(&ip
->sin6_addr
, &ip_fwd
->sin6_addr
, sizeof(struct in6_addr
)) != 0)
212 sendheader(auth
->client
, REPORT_HOST_MISMATCH
);
217 else /* can't verify it, don't know how. reject it. */
219 sendheader(auth
->client
, REPORT_HOST_UNKNOWN
);
223 if(good
&& strlen(reply
->h_name
) <= HOSTLEN
)
225 strlcpy(auth
->client
->host
, reply
->h_name
, sizeof(auth
->client
->host
));
226 sendheader(auth
->client
, REPORT_FIN_DNS
);
228 else if (strlen(reply
->h_name
) > 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
;
266 socklen_t locallen
= sizeof(struct rb_sockaddr_storage
);
270 if(IsAnyDead(auth
->client
))
273 family
= auth
->client
->localClient
->ip
.ss_family
;
274 if((F
= rb_socket(family
, SOCK_STREAM
, 0, "ident")) == NULL
)
276 ilog_error("creating auth stream socket");
277 ++ServerStats
->is_abad
;
282 * TBD: this is a pointless arbitrary limit .. we either have a socket or not. -nenolod
284 if((maxconnections
- 10) < rb_get_fd(F
))
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
));
293 sendheader(auth
->client
, REPORT_DO_ID
);
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
302 memset(&localaddr
, 0, locallen
);
303 getsockname(rb_get_fd(auth
->client
->localClient
->F
),
304 (struct sockaddr
*) &localaddr
, &locallen
);
306 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
308 if(localaddr
.ss_family
== AF_INET6
)
310 ((struct sockaddr_in6
*)&localaddr
)->sin6_port
= 0;
313 ((struct sockaddr_in
*)&localaddr
)->sin_port
= 0;
315 destaddr
= auth
->client
->localClient
->ip
;
317 if(localaddr
.ss_family
== AF_INET6
)
319 ((struct sockaddr_in6
*)&destaddr
)->sin6_port
= 113;
322 ((struct sockaddr_in
*)&destaddr
)->sin_port
= 113;
325 SetAuthConnect(auth
);
327 rb_connect_tcp(F
, (struct sockaddr
*)&destaddr
,
328 (struct sockaddr
*) &localaddr
, GET_SS_LEN(&localaddr
),
329 auth_connect_callback
, auth
,
330 GlobalSetOptions
.ident_timeout
);
331 return 1; /* We suceed here for now */
335 * GetValidIdent - parse ident query reply from identd server
337 * Inputs - pointer to ident buf
338 * Output - NULL if no valid ident found, otherwise pointer to name
342 GetValidIdent(char *buf
)
350 char *remotePortString
;
352 /* All this to get rid of a sscanf() fun. */
353 remotePortString
= buf
;
355 colon1Ptr
= strchr(remotePortString
, ':');
361 colon2Ptr
= strchr(colon1Ptr
, ':');
367 commaPtr
= strchr(remotePortString
, ',');
375 remp
= atoi(remotePortString
);
379 locp
= atoi(commaPtr
);
383 /* look for USERID bordered by first pair of colons */
384 if(!strstr(colon1Ptr
, "USERID"))
387 colon3Ptr
= strchr(colon2Ptr
, ':');
397 * start_auth - starts auth (identd) and dns queries for a client
400 start_auth(struct Client
*client
)
402 struct AuthRequest
*auth
= 0;
403 s_assert(0 != client
);
407 auth
= make_auth_request(client
);
409 auth
->dns_query
.ptr
= auth
;
410 auth
->dns_query
.callback
= auth_dns_callback
;
412 sendheader(client
, REPORT_DO_DNS
);
414 /* No DNS cache now, remember? -- adrian */
415 gethost_byaddr(&client
->localClient
->ip
, &auth
->dns_query
);
419 if(ConfigFileEntry
.disable_auth
== 0)
420 start_auth_query(auth
);
422 rb_dlinkAdd(auth
, &auth
->node
, &auth_poll_list
);
426 * timeout_auth_queries - timeout resolver and identd requests
427 * allow clients through if requests failed
430 timeout_auth_queries_event(void *notused
)
433 rb_dlink_node
*next_ptr
;
434 struct AuthRequest
*auth
;
436 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, auth_poll_list
.head
)
440 if(auth
->timeout
< rb_current_time())
445 if(IsDoingAuth(auth
))
448 ++ServerStats
->is_abad
;
449 sendheader(auth
->client
, REPORT_FAIL_ID
);
450 auth
->client
->localClient
->auth_request
= NULL
;
452 if(IsDNSPending(auth
))
454 ClearDNSPending(auth
);
455 delete_resolver_queries(&auth
->dns_query
);
456 sendheader(auth
->client
, REPORT_FAIL_DNS
);
459 auth
->client
->localClient
->lasttime
= rb_current_time();
460 release_auth_client(auth
);
466 * auth_connect_callback() - deal with the result of rb_connect_tcp()
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
477 auth_connect_callback(rb_fde_t
*F
, int error
, void *data
)
479 struct AuthRequest
*auth
= data
;
480 struct sockaddr_in us
;
481 struct sockaddr_in them
;
483 socklen_t ulen
= sizeof(struct sockaddr_in
);
484 socklen_t tlen
= sizeof(struct sockaddr_in
);
486 /* Check the error */
489 /* We had an error during connection :( */
490 ilog_error("identd check");
496 (rb_get_fd(auth
->client
->localClient
->F
), (struct sockaddr
*) &us
,
497 (socklen_t
*) & ulen
)
498 || getpeername(rb_get_fd(auth
->client
->localClient
->F
),
499 (struct sockaddr
*) &them
, (socklen_t
*) & tlen
))
501 ilog(L_IOERROR
, "auth get{sock,peer}name error for %s:%m",
502 log_client_name(auth
->client
, SHOW_IP
));
506 rb_snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
507 (unsigned int) ntohs(them
.sin_port
), (unsigned int) ntohs(us
.sin_port
));
509 if(write(rb_get_fd(auth
->F
), authbuf
, strlen(authbuf
)) == -1)
514 ClearAuthConnect(auth
);
515 SetAuthPending(auth
);
516 read_auth_reply(auth
->F
, auth
);
521 * read_auth_reply - read the reply (if any) from the ident server
523 * We only give it one shot, if the reply isn't good the first time
524 * fail the authentication entirely. --Bleep
526 #define AUTH_BUFSIZ 128
529 read_auth_reply(rb_fde_t
*F
, void *data
)
531 struct AuthRequest
*auth
= data
;
536 char buf
[AUTH_BUFSIZ
+ 1]; /* buffer to read auth reply into */
538 len
= read(rb_get_fd(F
), buf
, AUTH_BUFSIZ
);
540 if(len
< 0 && rb_ignore_errno(errno
))
542 rb_setselect(F
, RB_SELECT_READ
, read_auth_reply
, auth
);
550 if((s
= GetValidIdent(buf
)))
552 t
= auth
->client
->username
;
554 while (*s
== '~' || *s
== '^')
557 for (count
= USERLEN
; *s
&& count
; s
++)
563 if(!IsSpace(*s
) && *s
!= ':' && *s
!= '[')
579 ++ServerStats
->is_abad
;
580 strcpy(auth
->client
->username
, "unknown");
581 sendheader(auth
->client
, REPORT_FAIL_ID
);
585 sendheader(auth
->client
, REPORT_FIN_ID
);
586 ++ServerStats
->is_asuc
;
587 SetGotId(auth
->client
);
590 release_auth_client(auth
);
596 * delete_auth_queries()
600 delete_auth_queries(struct Client
*target_p
)
602 struct AuthRequest
*auth
;
604 if(target_p
== NULL
|| target_p
->localClient
== NULL
||
605 target_p
->localClient
->auth_request
== NULL
)
608 auth
= target_p
->localClient
->auth_request
;
609 target_p
->localClient
->auth_request
= NULL
;
611 if(IsDNSPending(auth
))
612 delete_resolver_queries(&auth
->dns_query
);
617 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
618 free_auth_request(auth
);