ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/auth.c
Revision: 2916
Committed: Sat Jan 25 21:09:18 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_auth.c
File size: 15212 byte(s)
Log Message:
- Clean up all files in include/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision