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