1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* s_auth.c: Functions for querying a users ident. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
/* |
26 |
* Changes: |
27 |
* July 6, 1999 - Rewrote most of the code here. When a client connects |
28 |
* to the server and passes initial socket validation checks, it |
29 |
* is owned by this module (auth) which returns it to the rest of the |
30 |
* server when dns and auth queries are finished. Until the client is |
31 |
* released, the server does not know it exists and does not process |
32 |
* any messages from it. |
33 |
* --Bleep Thomas Helvey <tomh@inxpress.net> |
34 |
*/ |
35 |
|
36 |
#include "stdinc.h" |
37 |
#include "list.h" |
38 |
#include "ircd_defs.h" |
39 |
#include "fdlist.h" |
40 |
#include "s_auth.h" |
41 |
#include "conf.h" |
42 |
#include "client.h" |
43 |
#include "event.h" |
44 |
#include "hook.h" |
45 |
#include "irc_string.h" |
46 |
#include "ircd.h" |
47 |
#include "packet.h" |
48 |
#include "irc_res.h" |
49 |
#include "s_bsd.h" |
50 |
#include "log.h" |
51 |
#include "send.h" |
52 |
#include "mempool.h" |
53 |
|
54 |
|
55 |
static const char *HeaderMessages[] = { |
56 |
":%s NOTICE AUTH :*** Looking up your hostname...", |
57 |
":%s NOTICE AUTH :*** Found your hostname", |
58 |
":%s NOTICE AUTH :*** Couldn't look up your hostname", |
59 |
":%s NOTICE AUTH :*** Checking Ident", |
60 |
":%s NOTICE AUTH :*** Got Ident response", |
61 |
":%s NOTICE AUTH :*** No Ident response", |
62 |
":%s NOTICE AUTH :*** Your forward and reverse DNS do not match, ignoring hostname.", |
63 |
":%s NOTICE AUTH :*** Your hostname is too long, ignoring hostname" |
64 |
}; |
65 |
|
66 |
enum { |
67 |
REPORT_DO_DNS, |
68 |
REPORT_FIN_DNS, |
69 |
REPORT_FAIL_DNS, |
70 |
REPORT_DO_ID, |
71 |
REPORT_FIN_ID, |
72 |
REPORT_FAIL_ID, |
73 |
REPORT_IP_MISMATCH, |
74 |
REPORT_HOST_TOOLONG |
75 |
}; |
76 |
|
77 |
#define sendheader(c, i) sendto_one((c), HeaderMessages[(i)], me.name) |
78 |
|
79 |
static dlink_list auth_doing_list = { NULL, NULL, 0 }; |
80 |
|
81 |
static EVH timeout_auth_queries_event; |
82 |
|
83 |
static PF read_auth_reply; |
84 |
static CNCB auth_connect_callback; |
85 |
|
86 |
/* auth_init |
87 |
* |
88 |
* Initialise the auth code |
89 |
*/ |
90 |
void |
91 |
auth_init(void) |
92 |
{ |
93 |
eventAddIsh("timeout_auth_queries_event", timeout_auth_queries_event, NULL, 1); |
94 |
} |
95 |
|
96 |
/* |
97 |
* make_auth_request - allocate a new auth request |
98 |
*/ |
99 |
static struct AuthRequest * |
100 |
make_auth_request(struct Client *client) |
101 |
{ |
102 |
struct AuthRequest *request = &client->localClient->auth; |
103 |
|
104 |
memset(request, 0, sizeof(*request)); |
105 |
|
106 |
request->client = client; |
107 |
request->timeout = CurrentTime + CONNECTTIMEOUT; |
108 |
|
109 |
return request; |
110 |
} |
111 |
|
112 |
/* |
113 |
* release_auth_client - release auth client from auth system |
114 |
* this adds the client into the local client lists so it can be read by |
115 |
* the main io processing loop |
116 |
*/ |
117 |
void |
118 |
release_auth_client(struct AuthRequest *auth) |
119 |
{ |
120 |
struct Client *client = auth->client; |
121 |
|
122 |
if (IsDoingAuth(auth) || IsDNSPending(auth)) |
123 |
return; |
124 |
|
125 |
if (dlinkFind(&auth_doing_list, auth)) |
126 |
dlinkDelete(&auth->node, &auth_doing_list); |
127 |
|
128 |
/* |
129 |
* When a client has auth'ed, we want to start reading what it sends |
130 |
* us. This is what read_packet() does. |
131 |
* -- adrian |
132 |
*/ |
133 |
client->localClient->allow_read = MAX_FLOOD; |
134 |
comm_setflush(&client->localClient->fd, 1000, flood_recalc, client); |
135 |
|
136 |
dlinkAdd(client, &client->node, &global_client_list); |
137 |
|
138 |
client->localClient->since = CurrentTime; |
139 |
client->localClient->lasttime = CurrentTime; |
140 |
client->localClient->firsttime = CurrentTime; |
141 |
client->flags |= FLAGS_FINISHED_AUTH; |
142 |
|
143 |
read_packet(&client->localClient->fd, client); |
144 |
} |
145 |
|
146 |
/* |
147 |
* auth_dns_callback - called when resolver query finishes |
148 |
* if the query resulted in a successful search, name will contain |
149 |
* a non-NULL pointer, otherwise name will be NULL. |
150 |
* set the client on it's way to a connection completion, regardless |
151 |
* of success of failure |
152 |
*/ |
153 |
static void |
154 |
auth_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
155 |
{ |
156 |
struct AuthRequest *auth = vptr; |
157 |
|
158 |
ClearDNSPending(auth); |
159 |
|
160 |
if (name != NULL) |
161 |
{ |
162 |
const struct sockaddr_in *v4, *v4dns; |
163 |
#ifdef IPV6 |
164 |
const struct sockaddr_in6 *v6, *v6dns; |
165 |
#endif |
166 |
int good = 1; |
167 |
|
168 |
#ifdef IPV6 |
169 |
if (auth->client->localClient->ip.ss.ss_family == AF_INET6) |
170 |
{ |
171 |
v6 = (const struct sockaddr_in6 *)&auth->client->localClient->ip; |
172 |
v6dns = (const struct sockaddr_in6 *)addr; |
173 |
if (memcmp(&v6->sin6_addr, &v6dns->sin6_addr, sizeof(struct in6_addr)) != 0) |
174 |
{ |
175 |
sendheader(auth->client, REPORT_IP_MISMATCH); |
176 |
good = 0; |
177 |
} |
178 |
} |
179 |
else |
180 |
#endif |
181 |
{ |
182 |
v4 = (const struct sockaddr_in *)&auth->client->localClient->ip; |
183 |
v4dns = (const struct sockaddr_in *)addr; |
184 |
if(v4->sin_addr.s_addr != v4dns->sin_addr.s_addr) |
185 |
{ |
186 |
sendheader(auth->client, REPORT_IP_MISMATCH); |
187 |
good = 0; |
188 |
} |
189 |
} |
190 |
if (good && strlen(name) <= HOSTLEN) |
191 |
{ |
192 |
strlcpy(auth->client->host, name, |
193 |
sizeof(auth->client->host)); |
194 |
sendheader(auth->client, REPORT_FIN_DNS); |
195 |
} |
196 |
else if (strlen(name) > HOSTLEN) |
197 |
sendheader(auth->client, REPORT_HOST_TOOLONG); |
198 |
} |
199 |
else |
200 |
sendheader(auth->client, REPORT_FAIL_DNS); |
201 |
|
202 |
release_auth_client(auth); |
203 |
} |
204 |
|
205 |
/* |
206 |
* authsenderr - handle auth send errors |
207 |
*/ |
208 |
static void |
209 |
auth_error(struct AuthRequest *auth) |
210 |
{ |
211 |
++ServerStats.is_abad; |
212 |
|
213 |
fd_close(&auth->fd); |
214 |
|
215 |
ClearAuth(auth); |
216 |
|
217 |
sendheader(auth->client, REPORT_FAIL_ID); |
218 |
|
219 |
release_auth_client(auth); |
220 |
} |
221 |
|
222 |
/* |
223 |
* start_auth_query - Flag the client to show that an attempt to |
224 |
* contact the ident server on |
225 |
* the client's host. The connect and subsequently the socket are all put |
226 |
* into 'non-blocking' mode. Should the connect or any later phase of the |
227 |
* identifing process fail, it is aborted and the user is given a username |
228 |
* of "unknown". |
229 |
*/ |
230 |
static int |
231 |
start_auth_query(struct AuthRequest *auth) |
232 |
{ |
233 |
struct irc_ssaddr localaddr; |
234 |
socklen_t locallen = sizeof(struct irc_ssaddr); |
235 |
#ifdef IPV6 |
236 |
struct sockaddr_in6 *v6; |
237 |
#else |
238 |
struct sockaddr_in *v4; |
239 |
#endif |
240 |
|
241 |
/* open a socket of the same type as the client socket */ |
242 |
if (comm_open(&auth->fd, auth->client->localClient->ip.ss.ss_family, |
243 |
SOCK_STREAM, 0, "ident") == -1) |
244 |
{ |
245 |
report_error(L_ALL, "creating auth stream socket %s:%s", |
246 |
get_client_name(auth->client, SHOW_IP), errno); |
247 |
ilog(LOG_TYPE_IRCD, "Unable to create auth socket for %s", |
248 |
get_client_name(auth->client, SHOW_IP)); |
249 |
++ServerStats.is_abad; |
250 |
return 0; |
251 |
} |
252 |
|
253 |
sendheader(auth->client, REPORT_DO_ID); |
254 |
|
255 |
/* |
256 |
* get the local address of the client and bind to that to |
257 |
* make the auth request. This used to be done only for |
258 |
* ifdef VIRTUAL_HOST, but needs to be done for all clients |
259 |
* since the ident request must originate from that same address-- |
260 |
* and machines with multiple IP addresses are common now |
261 |
*/ |
262 |
memset(&localaddr, 0, locallen); |
263 |
getsockname(auth->client->localClient->fd.fd, (struct sockaddr*)&localaddr, |
264 |
&locallen); |
265 |
|
266 |
#ifdef IPV6 |
267 |
remove_ipv6_mapping(&localaddr); |
268 |
v6 = (struct sockaddr_in6 *)&localaddr; |
269 |
v6->sin6_port = htons(0); |
270 |
#else |
271 |
localaddr.ss_len = locallen; |
272 |
v4 = (struct sockaddr_in *)&localaddr; |
273 |
v4->sin_port = htons(0); |
274 |
#endif |
275 |
localaddr.ss_port = htons(0); |
276 |
|
277 |
comm_connect_tcp(&auth->fd, auth->client->sockhost, 113, |
278 |
(struct sockaddr *)&localaddr, localaddr.ss_len, auth_connect_callback, |
279 |
auth, auth->client->localClient->ip.ss.ss_family, |
280 |
GlobalSetOptions.ident_timeout); |
281 |
return 1; /* We suceed here for now */ |
282 |
} |
283 |
|
284 |
/* |
285 |
* GetValidIdent - parse ident query reply from identd server |
286 |
* |
287 |
* Inputs - pointer to ident buf |
288 |
* Output - NULL if no valid ident found, otherwise pointer to name |
289 |
* Side effects - |
290 |
*/ |
291 |
/* |
292 |
* A few questions have been asked about this mess, obviously |
293 |
* it should have been commented better the first time. |
294 |
* The original idea was to remove all references to libc from ircd-hybrid. |
295 |
* Instead of having to write a replacement for sscanf(), I did a |
296 |
* rather gruseome parser here so we could remove this function call. |
297 |
* Note, that I had also removed a few floating point printfs as well (though |
298 |
* now we are still stuck with a few...) |
299 |
* Remember, we have a replacement ircd sprintf, we have bleeps fputs lib |
300 |
* it would have been nice to remove some unneeded code. |
301 |
* Oh well. If we don't remove libc stuff totally, then it would be |
302 |
* far cleaner to use sscanf() |
303 |
* |
304 |
* - Dianora |
305 |
*/ |
306 |
static char * |
307 |
GetValidIdent(char *buf) |
308 |
{ |
309 |
int remp = 0; |
310 |
int locp = 0; |
311 |
char* colon1Ptr; |
312 |
char* colon2Ptr; |
313 |
char* colon3Ptr; |
314 |
char* commaPtr; |
315 |
char* remotePortString; |
316 |
|
317 |
/* All this to get rid of a sscanf() fun. */ |
318 |
remotePortString = buf; |
319 |
|
320 |
if ((colon1Ptr = strchr(remotePortString,':')) == NULL) |
321 |
return 0; |
322 |
*colon1Ptr = '\0'; |
323 |
colon1Ptr++; |
324 |
|
325 |
if ((colon2Ptr = strchr(colon1Ptr,':')) == NULL) |
326 |
return 0; |
327 |
*colon2Ptr = '\0'; |
328 |
colon2Ptr++; |
329 |
|
330 |
if ((commaPtr = strchr(remotePortString, ',')) == NULL) |
331 |
return 0; |
332 |
*commaPtr = '\0'; |
333 |
commaPtr++; |
334 |
|
335 |
if ((remp = atoi(remotePortString)) == 0) |
336 |
return 0; |
337 |
|
338 |
if ((locp = atoi(commaPtr)) == 0) |
339 |
return 0; |
340 |
|
341 |
/* look for USERID bordered by first pair of colons */ |
342 |
if (strstr(colon1Ptr, "USERID") == NULL) |
343 |
return 0; |
344 |
|
345 |
if ((colon3Ptr = strchr(colon2Ptr,':')) == NULL) |
346 |
return 0; |
347 |
*colon3Ptr = '\0'; |
348 |
colon3Ptr++; |
349 |
return (colon3Ptr); |
350 |
} |
351 |
|
352 |
/* |
353 |
* start_auth |
354 |
* |
355 |
* inputs - pointer to client to auth |
356 |
* output - NONE |
357 |
* side effects - starts auth (identd) and dns queries for a client |
358 |
*/ |
359 |
void |
360 |
start_auth(struct Client *client) |
361 |
{ |
362 |
struct AuthRequest *auth = NULL; |
363 |
|
364 |
assert(client != NULL); |
365 |
|
366 |
auth = make_auth_request(client); |
367 |
dlinkAdd(auth, &auth->node, &auth_doing_list); |
368 |
|
369 |
sendheader(client, REPORT_DO_DNS); |
370 |
|
371 |
SetDNSPending(auth); |
372 |
|
373 |
if (ConfigFileEntry.disable_auth == 0) |
374 |
{ |
375 |
SetDoingAuth(auth); |
376 |
start_auth_query(auth); |
377 |
} |
378 |
|
379 |
gethost_byaddr(auth_dns_callback, auth, &client->localClient->ip); |
380 |
} |
381 |
|
382 |
/* |
383 |
* timeout_auth_queries - timeout resolver and identd requests |
384 |
* allow clients through if requests failed |
385 |
*/ |
386 |
static void |
387 |
timeout_auth_queries_event(void *notused) |
388 |
{ |
389 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
390 |
|
391 |
DLINK_FOREACH_SAFE(ptr, next_ptr, auth_doing_list.head) |
392 |
{ |
393 |
struct AuthRequest *auth = ptr->data; |
394 |
|
395 |
if (auth->timeout > CurrentTime) |
396 |
continue; |
397 |
|
398 |
if (IsDoingAuth(auth)) |
399 |
{ |
400 |
++ServerStats.is_abad; |
401 |
fd_close(&auth->fd); |
402 |
ClearAuth(auth); |
403 |
sendheader(auth->client, REPORT_FAIL_ID); |
404 |
} |
405 |
|
406 |
if (IsDNSPending(auth)) |
407 |
{ |
408 |
delete_resolver_queries(auth); |
409 |
ClearDNSPending(auth); |
410 |
sendheader(auth->client, REPORT_FAIL_DNS); |
411 |
} |
412 |
|
413 |
ilog(LOG_TYPE_IRCD, "DNS/AUTH timeout %s", |
414 |
get_client_name(auth->client, SHOW_IP)); |
415 |
release_auth_client(auth); |
416 |
} |
417 |
} |
418 |
|
419 |
/* |
420 |
* auth_connect_callback() - deal with the result of comm_connect_tcp() |
421 |
* |
422 |
* If the connection failed, we simply close the auth fd and report |
423 |
* a failure. If the connection suceeded send the ident server a query |
424 |
* giving "theirport , ourport". The write is only attempted *once* so |
425 |
* it is deemed to be a fail if the entire write doesn't write all the |
426 |
* data given. This shouldnt be a problem since the socket should have |
427 |
* a write buffer far greater than this message to store it in should |
428 |
* problems arise. -avalon |
429 |
*/ |
430 |
static void |
431 |
auth_connect_callback(fde_t *fd, int error, void *data) |
432 |
{ |
433 |
struct AuthRequest *auth = data; |
434 |
struct irc_ssaddr us; |
435 |
struct irc_ssaddr them; |
436 |
char authbuf[32]; |
437 |
socklen_t ulen = sizeof(struct irc_ssaddr); |
438 |
socklen_t tlen = sizeof(struct irc_ssaddr); |
439 |
uint16_t uport, tport; |
440 |
#ifdef IPV6 |
441 |
struct sockaddr_in6 *v6; |
442 |
#else |
443 |
struct sockaddr_in *v4; |
444 |
#endif |
445 |
|
446 |
if (error != COMM_OK) |
447 |
{ |
448 |
auth_error(auth); |
449 |
return; |
450 |
} |
451 |
|
452 |
if (getsockname(auth->client->localClient->fd.fd, (struct sockaddr *)&us, |
453 |
&ulen) || |
454 |
getpeername(auth->client->localClient->fd.fd, (struct sockaddr *)&them, |
455 |
&tlen)) |
456 |
{ |
457 |
ilog(LOG_TYPE_IRCD, "auth get{sock,peer}name error for %s", |
458 |
get_client_name(auth->client, SHOW_IP)); |
459 |
auth_error(auth); |
460 |
return; |
461 |
} |
462 |
|
463 |
#ifdef IPV6 |
464 |
v6 = (struct sockaddr_in6 *)&us; |
465 |
uport = ntohs(v6->sin6_port); |
466 |
v6 = (struct sockaddr_in6 *)&them; |
467 |
tport = ntohs(v6->sin6_port); |
468 |
remove_ipv6_mapping(&us); |
469 |
remove_ipv6_mapping(&them); |
470 |
#else |
471 |
v4 = (struct sockaddr_in *)&us; |
472 |
uport = ntohs(v4->sin_port); |
473 |
v4 = (struct sockaddr_in *)&them; |
474 |
tport = ntohs(v4->sin_port); |
475 |
us.ss_len = ulen; |
476 |
them.ss_len = tlen; |
477 |
#endif |
478 |
|
479 |
snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n", tport, uport); |
480 |
|
481 |
if (send(fd->fd, authbuf, strlen(authbuf), 0) == -1) |
482 |
{ |
483 |
auth_error(auth); |
484 |
return; |
485 |
} |
486 |
|
487 |
read_auth_reply(&auth->fd, auth); |
488 |
} |
489 |
|
490 |
/* |
491 |
* read_auth_reply - read the reply (if any) from the ident server |
492 |
* we connected to. |
493 |
* We only give it one shot, if the reply isn't good the first time |
494 |
* fail the authentication entirely. --Bleep |
495 |
*/ |
496 |
#define AUTH_BUFSIZ 128 |
497 |
|
498 |
static void |
499 |
read_auth_reply(fde_t *fd, void *data) |
500 |
{ |
501 |
struct AuthRequest *auth = data; |
502 |
char *s = NULL; |
503 |
char *t = NULL; |
504 |
int len; |
505 |
int count; |
506 |
char buf[AUTH_BUFSIZ + 1]; /* buffer to read auth reply into */ |
507 |
|
508 |
/* Why? |
509 |
* Well, recv() on many POSIX systems is a per-packet operation, |
510 |
* and we do not necessarily want this, because on lowspec machines, |
511 |
* the ident response may come back fragmented, thus resulting in an |
512 |
* invalid ident response, even if the ident response was really OK. |
513 |
* |
514 |
* So PLEASE do not change this code to recv without being aware of the |
515 |
* consequences. |
516 |
* |
517 |
* --nenolod |
518 |
*/ |
519 |
len = read(fd->fd, buf, AUTH_BUFSIZ); |
520 |
|
521 |
if (len < 0) |
522 |
{ |
523 |
if (ignoreErrno(errno)) |
524 |
comm_setselect(fd, COMM_SELECT_READ, read_auth_reply, auth, 0); |
525 |
else |
526 |
auth_error(auth); |
527 |
return; |
528 |
} |
529 |
|
530 |
if (len > 0) |
531 |
{ |
532 |
buf[len] = '\0'; |
533 |
|
534 |
if ((s = GetValidIdent(buf))) |
535 |
{ |
536 |
t = auth->client->username; |
537 |
|
538 |
while (*s == '~' || *s == '^') |
539 |
s++; |
540 |
|
541 |
for (count = USERLEN; *s && count; s++) |
542 |
{ |
543 |
if (*s == '@') |
544 |
break; |
545 |
if (!IsSpace(*s) && *s != ':' && *s != '[') |
546 |
{ |
547 |
*t++ = *s; |
548 |
count--; |
549 |
} |
550 |
} |
551 |
|
552 |
*t = '\0'; |
553 |
} |
554 |
} |
555 |
|
556 |
fd_close(fd); |
557 |
|
558 |
ClearAuth(auth); |
559 |
|
560 |
if (s == NULL) |
561 |
{ |
562 |
sendheader(auth->client, REPORT_FAIL_ID); |
563 |
++ServerStats.is_abad; |
564 |
} |
565 |
else |
566 |
{ |
567 |
sendheader(auth->client, REPORT_FIN_ID); |
568 |
++ServerStats.is_asuc; |
569 |
SetGotId(auth->client); |
570 |
} |
571 |
|
572 |
release_auth_client(auth); |
573 |
} |
574 |
|
575 |
/* |
576 |
* delete_auth() |
577 |
*/ |
578 |
void |
579 |
delete_auth(struct AuthRequest *auth) |
580 |
{ |
581 |
if (IsDNSPending(auth)) |
582 |
delete_resolver_queries(auth); |
583 |
|
584 |
if (IsDoingAuth(auth)) |
585 |
fd_close(&auth->fd); |
586 |
|
587 |
if (dlinkFind(&auth_doing_list, auth)) |
588 |
dlinkDelete(&auth->node, &auth_doing_list); |
589 |
} |