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 */
64 * flag values for AuthRequest
65 * NAMESPACE: AM_xxx - Authentication Module
67 #define AM_AUTH_CONNECTING (1 << 0)
68 #define AM_AUTH_PENDING (1 << 1)
69 #define AM_DNS_PENDING (1 << 2)
71 #define SetDNSPending(x) ((x)->flags |= AM_DNS_PENDING)
72 #define ClearDNSPending(x) ((x)->flags &= ~AM_DNS_PENDING)
73 #define IsDNSPending(x) ((x)->flags & AM_DNS_PENDING)
75 #define SetAuthConnect(x) ((x)->flags |= AM_AUTH_CONNECTING)
76 #define ClearAuthConnect(x) ((x)->flags &= ~AM_AUTH_CONNECTING)
77 #define IsAuthConnect(x) ((x)->flags & AM_AUTH_CONNECTING)
79 #define SetAuthPending(x) ((x)->flags |= AM_AUTH_PENDING)
80 #define ClearAuthPending(x) ((x)->flags &= AM_AUTH_PENDING)
81 #define IsAuthPending(x) ((x)->flags & AM_AUTH_PENDING)
83 #define ClearAuth(x) ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
84 #define IsDoingAuth(x) ((x)->flags & (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
87 * a bit different approach
88 * this replaces the original sendheader macros
91 static const char *HeaderMessages
[] =
93 ":*** Looking up your hostname...",
94 ":*** Found your hostname",
95 ":*** Couldn't look up your hostname",
96 ":*** Checking Ident",
97 ":*** Got Ident response",
98 ":*** No Ident response",
99 ":*** Your hostname is too long, ignoring hostname",
100 ":*** Your forward and reverse DNS do not match, ignoring hostname",
101 ":*** Cannot verify hostname validity, ignoring hostname",
113 REPORT_HOST_MISMATCH
,
118 #define sendheader(c, r) sendto_one_notice(c, HeaderMessages[(r)])
120 static rb_dlink_list auth_poll_list
;
121 static rb_bh
*auth_heap
;
122 static EVH timeout_auth_queries_event
;
124 static PF read_auth_reply
;
125 static CNCB auth_connect_callback
;
130 * Initialise the auth code
135 /* This hook takes a struct Client for its argument */
136 memset(&auth_poll_list
, 0, sizeof(auth_poll_list
));
137 rb_event_addish("timeout_auth_queries_event", timeout_auth_queries_event
, NULL
, 1);
138 auth_heap
= rb_bh_create(sizeof(struct AuthRequest
), LCLIENT_HEAP_SIZE
, "auth_heap");
142 * make_auth_request - allocate a new auth request
144 static struct AuthRequest
*
145 make_auth_request(struct Client
*client
)
147 struct AuthRequest
*request
= rb_bh_alloc(auth_heap
);
148 client
->localClient
->auth_request
= request
;
150 request
->client
= client
;
151 request
->timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
;
156 * free_auth_request - cleanup auth request allocations
159 free_auth_request(struct AuthRequest
*request
)
161 rb_bh_free(auth_heap
, request
);
165 * release_auth_client - release auth client from auth system
166 * this adds the client into the local client lists so it can be read by
167 * the main io processing loop
170 release_auth_client(struct AuthRequest
*auth
)
172 struct Client
*client
= auth
->client
;
174 if(IsDNSPending(auth
) || IsDoingAuth(auth
))
177 client
->localClient
->auth_request
= NULL
;
178 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
179 free_auth_request(auth
);
182 * When a client has auth'ed, we want to start reading what it sends
183 * us. This is what read_packet() does.
186 client
->localClient
->allow_read
= MAX_FLOOD
;
187 rb_dlinkAddTail(client
, &client
->node
, &global_client_list
);
188 read_packet(client
->localClient
->F
, client
);
192 * auth_dns_callback - called when resolver query finishes
193 * if the query resulted in a successful search, hp will contain
194 * a non-null pointer, otherwise hp will be null.
195 * set the client on it's way to a connection completion, regardless
196 * of success of failure
199 auth_dns_callback(void *vptr
, struct DNSReply
*reply
)
201 struct AuthRequest
*auth
= (struct AuthRequest
*) vptr
;
202 ClearDNSPending(auth
);
204 /* XXX: this shouldn't happen, but it does. -nenolod */
205 if(auth
->client
->localClient
== NULL
)
207 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
208 "auth_dns_callback(): auth->client->localClient (%s) is NULL", get_client_name(auth
->client
, HIDE_IP
));
210 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
211 free_auth_request(auth
);
213 /* and they will silently drop through and all will hopefully be ok... -nenolod */
221 if(auth
->client
->localClient
->ip
.ss_family
== AF_INET
)
223 struct sockaddr_in
*ip
, *ip_fwd
;
225 ip
= (struct sockaddr_in
*) &auth
->client
->localClient
->ip
;
226 ip_fwd
= (struct sockaddr_in
*) &reply
->addr
;
228 if(ip
->sin_addr
.s_addr
!= ip_fwd
->sin_addr
.s_addr
)
230 sendheader(auth
->client
, REPORT_HOST_MISMATCH
);
235 else if(auth
->client
->localClient
->ip
.ss_family
== AF_INET6
)
237 struct sockaddr_in6
*ip
, *ip_fwd
;
239 ip
= (struct sockaddr_in6
*) &auth
->client
->localClient
->ip
;
240 ip_fwd
= (struct sockaddr_in6
*) &reply
->addr
;
242 if(memcmp(&ip
->sin6_addr
, &ip_fwd
->sin6_addr
, sizeof(struct in6_addr
)) != 0)
244 sendheader(auth
->client
, REPORT_HOST_MISMATCH
);
249 else /* can't verify it, don't know how. reject it. */
251 sendheader(auth
->client
, REPORT_HOST_UNKNOWN
);
255 if(good
&& strlen(reply
->h_name
) <= HOSTLEN
)
257 rb_strlcpy(auth
->client
->host
, reply
->h_name
, sizeof(auth
->client
->host
));
258 sendheader(auth
->client
, REPORT_FIN_DNS
);
260 else if (strlen(reply
->h_name
) > HOSTLEN
)
261 sendheader(auth
->client
, REPORT_HOST_TOOLONG
);
264 sendheader(auth
->client
, REPORT_FAIL_DNS
);
266 release_auth_client(auth
);
270 * authsenderr - handle auth send errors
273 auth_error(struct AuthRequest
*auth
)
275 ++ServerStats
.is_abad
;
281 sendheader(auth
->client
, REPORT_FAIL_ID
);
283 release_auth_client(auth
);
287 * start_auth_query - Flag the client to show that an attempt to
288 * contact the ident server on
289 * the client's host. The connect and subsequently the socket are all put
290 * into 'non-blocking' mode. Should the connect or any later phase of the
291 * identifing process fail, it is aborted and the user is given a username
295 start_auth_query(struct AuthRequest
*auth
)
297 struct rb_sockaddr_storage localaddr
, destaddr
;
298 socklen_t locallen
= sizeof(struct rb_sockaddr_storage
);
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 memset(&localaddr
, 0, locallen
);
335 if(getsockname(rb_get_fd(auth
->client
->localClient
->F
),
336 (struct sockaddr
*) &localaddr
, &locallen
) == -1)
338 /* can happen if connection was just closed */
343 /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
345 if(localaddr
.ss_family
== AF_INET6
)
347 ((struct sockaddr_in6
*)&localaddr
)->sin6_port
= 0;
350 ((struct sockaddr_in
*)&localaddr
)->sin_port
= 0;
352 destaddr
= auth
->client
->localClient
->ip
;
354 if(localaddr
.ss_family
== AF_INET6
)
356 ((struct sockaddr_in6
*)&destaddr
)->sin6_port
= htons(113);
359 ((struct sockaddr_in
*)&destaddr
)->sin_port
= htons(113);
362 SetAuthConnect(auth
);
364 rb_connect_tcp(F
, (struct sockaddr
*)&destaddr
,
365 (struct sockaddr
*) &localaddr
, GET_SS_LEN(&localaddr
),
366 auth_connect_callback
, auth
,
367 GlobalSetOptions
.ident_timeout
);
368 return 1; /* We suceed here for now */
372 * GetValidIdent - parse ident query reply from identd server
374 * Inputs - pointer to ident buf
375 * Output - NULL if no valid ident found, otherwise pointer to name
379 GetValidIdent(char *buf
)
387 char *remotePortString
;
389 /* All this to get rid of a sscanf() fun. */
390 remotePortString
= buf
;
392 colon1Ptr
= strchr(remotePortString
, ':');
398 colon2Ptr
= strchr(colon1Ptr
, ':');
404 commaPtr
= strchr(remotePortString
, ',');
412 remp
= atoi(remotePortString
);
416 locp
= atoi(commaPtr
);
420 /* look for USERID bordered by first pair of colons */
421 if(!strstr(colon1Ptr
, "USERID"))
424 colon3Ptr
= strchr(colon2Ptr
, ':');
434 * start_auth - starts auth (identd) and dns queries for a client
437 start_auth(struct Client
*client
)
439 struct AuthRequest
*auth
= 0;
440 s_assert(0 != client
);
444 auth
= make_auth_request(client
);
446 auth
->dns_query
.ptr
= auth
;
447 auth
->dns_query
.callback
= auth_dns_callback
;
449 sendheader(client
, REPORT_DO_DNS
);
451 /* No DNS cache now, remember? -- adrian */
452 gethost_byaddr(&client
->localClient
->ip
, &auth
->dns_query
);
456 if(ConfigFileEntry
.disable_auth
== 0)
457 start_auth_query(auth
);
459 rb_dlinkAdd(auth
, &auth
->node
, &auth_poll_list
);
463 * timeout_auth_queries - timeout resolver and identd requests
464 * allow clients through if requests failed
467 timeout_auth_queries_event(void *notused
)
470 rb_dlink_node
*next_ptr
;
471 struct AuthRequest
*auth
;
473 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, auth_poll_list
.head
)
477 if(auth
->timeout
< rb_current_time())
482 if(IsDoingAuth(auth
))
485 ++ServerStats
.is_abad
;
486 sendheader(auth
->client
, REPORT_FAIL_ID
);
487 auth
->client
->localClient
->auth_request
= NULL
;
489 if(IsDNSPending(auth
))
491 ClearDNSPending(auth
);
492 delete_resolver_queries(&auth
->dns_query
);
493 sendheader(auth
->client
, REPORT_FAIL_DNS
);
496 auth
->client
->localClient
->lasttime
= rb_current_time();
497 release_auth_client(auth
);
503 * auth_connect_callback() - deal with the result of rb_connect_tcp()
505 * If the connection failed, we simply close the auth fd and report
506 * a failure. If the connection suceeded send the ident server a query
507 * giving "theirport , ourport". The write is only attempted *once* so
508 * it is deemed to be a fail if the entire write doesn't write all the
509 * data given. This shouldnt be a problem since the socket should have
510 * a write buffer far greater than this message to store it in should
511 * problems arise. -avalon
514 auth_connect_callback(rb_fde_t
*F
, int error
, void *data
)
516 struct AuthRequest
*auth
= data
;
517 struct sockaddr_in us
;
518 struct sockaddr_in them
;
520 socklen_t ulen
= sizeof(struct sockaddr_in
);
521 socklen_t tlen
= sizeof(struct sockaddr_in
);
523 /* Check the error */
526 /* We had an error during connection :( */
532 (rb_get_fd(auth
->client
->localClient
->F
), (struct sockaddr
*) &us
,
533 (socklen_t
*) & ulen
)
534 || getpeername(rb_get_fd(auth
->client
->localClient
->F
),
535 (struct sockaddr
*) &them
, (socklen_t
*) & tlen
))
537 ilog(L_IOERROR
, "auth get{sock,peer}name error for %s:%m",
538 log_client_name(auth
->client
, SHOW_IP
));
542 rb_snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
543 (unsigned int) ntohs(them
.sin_port
), (unsigned int) ntohs(us
.sin_port
));
545 if(write(rb_get_fd(auth
->F
), authbuf
, strlen(authbuf
)) == -1)
550 ClearAuthConnect(auth
);
551 SetAuthPending(auth
);
552 read_auth_reply(auth
->F
, auth
);
557 * read_auth_reply - read the reply (if any) from the ident server
559 * We only give it one shot, if the reply isn't good the first time
560 * fail the authentication entirely. --Bleep
562 #define AUTH_BUFSIZ 128
565 read_auth_reply(rb_fde_t
*F
, void *data
)
567 struct AuthRequest
*auth
= data
;
572 char buf
[AUTH_BUFSIZ
+ 1]; /* buffer to read auth reply into */
574 len
= read(rb_get_fd(F
), buf
, AUTH_BUFSIZ
);
576 if(len
< 0 && rb_ignore_errno(errno
))
578 rb_setselect(F
, RB_SELECT_READ
, read_auth_reply
, auth
);
586 if((s
= GetValidIdent(buf
)))
588 t
= auth
->client
->username
;
590 while (*s
== '~' || *s
== '^')
593 for (count
= USERLEN
; *s
&& count
; s
++)
599 if(!IsSpace(*s
) && *s
!= ':' && *s
!= '[')
615 ++ServerStats
.is_abad
;
616 strcpy(auth
->client
->username
, "unknown");
617 sendheader(auth
->client
, REPORT_FAIL_ID
);
621 sendheader(auth
->client
, REPORT_FIN_ID
);
622 ++ServerStats
.is_asuc
;
623 SetGotId(auth
->client
);
626 release_auth_client(auth
);
632 * delete_auth_queries()
636 delete_auth_queries(struct Client
*target_p
)
638 struct AuthRequest
*auth
;
640 if(target_p
== NULL
|| target_p
->localClient
== NULL
||
641 target_p
->localClient
->auth_request
== NULL
)
644 auth
= target_p
->localClient
->auth_request
;
645 target_p
->localClient
->auth_request
= NULL
;
647 if(IsDNSPending(auth
))
648 delete_resolver_queries(&auth
->dns_query
);
653 rb_dlinkDelete(&auth
->node
, &auth_poll_list
);
654 free_auth_request(auth
);