ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-7.2/src/s_auth.c
Revision: 992
Committed: Mon Aug 17 19:19:16 2009 UTC (14 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 15448 byte(s)
Log Message:
- fix possible auth/dns related memleaks

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

Properties

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