ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/s_auth.c
Revision: 651
Committed: Thu Jun 8 07:38:56 2006 UTC (20 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 17169 byte(s)
Log Message:
- Backported r650 (clients are exited in check_unknowsn_list() while still
  processing auth/dns)
- Changed Client::flags to 64bit integer type to allow more flags

File Contents

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

Properties

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