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