ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/auth.c
(Generate patch)

Comparing:
ircd-hybrid-7.2/src/s_auth.c (file contents), Revision 650 by michael, Thu Jun 8 07:00:17 2006 UTC vs.
ircd-hybrid/trunk/src/s_auth.c (file contents), Revision 2916 by michael, Sat Jan 25 21:09:18 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  s_auth.c: Functions for querying a users ident.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
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
# Line 18 | Line 17
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 < *  $Id$
20 > */
21 >
22 > /*! \file s_auth.c
23 > * \brief Functions for querying a users ident.
24 > * \version $Id$
25   */
26  
27   /*
# Line 32 | Line 34
34   *     any messages from it.
35   *     --Bleep  Thomas Helvey <tomh@inxpress.net>
36   */
37 +
38   #include "stdinc.h"
36 #include "tools.h"
39   #include "list.h"
40 + #include "ircd_defs.h"
41 + #include "fdlist.h"
42   #include "s_auth.h"
43 < #include "s_conf.h"
43 > #include "conf.h"
44   #include "client.h"
41 #include "common.h"
45   #include "event.h"
43 #include "fdlist.h"              /* fdlist_add */
46   #include "hook.h"
47   #include "irc_string.h"
46 #include "sprintf_irc.h"
48   #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 "s_stats.h"
52 > #include "log.h"
53   #include "send.h"
54 < #include "memory.h"
54 > #include "mempool.h"
55 >
56  
57 < static const char *HeaderMessages[] = {
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",
# Line 65 | Line 66 | static const char *HeaderMessages[] = {
66    ":%s NOTICE AUTH :*** Your hostname is too long, ignoring hostname"
67   };
68  
69 < enum {
69 > enum
70 > {
71    REPORT_DO_DNS,
72    REPORT_FIN_DNS,
73    REPORT_FAIL_DNS,
# Line 78 | Line 80 | enum {
80  
81   #define sendheader(c, i) sendto_one((c), HeaderMessages[(i)], me.name)
82  
83 < /*
82 < * Ok, the original was confusing.
83 < * Now there are two lists, an auth request can be on both at the same time
84 < * or only on one or the other.
85 < * - Dianora
86 < */
87 < static dlink_list auth_doing_dns_list   = { NULL, NULL, 0 };
88 < static dlink_list auth_doing_ident_list = { NULL, NULL, 0 };
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;
94 static CBFUNC start_auth;
89  
90 < struct Callback *auth_cb = NULL;
97 <
98 < /* init_auth()
90 > /* auth_init
91   *
92   * Initialise the auth code
93   */
94   void
95 < init_auth(void)
95 > auth_init(void)
96   {
105  auth_cb = register_callback("start_auth", start_auth);
97    eventAddIsh("timeout_auth_queries_event", timeout_auth_queries_event, NULL, 1);
98   }
99  
# Line 112 | Line 103 | init_auth(void)
103   static struct AuthRequest *
104   make_auth_request(struct Client *client)
105   {
106 <  struct AuthRequest *request = MyMalloc(sizeof(struct AuthRequest));
106 >  struct AuthRequest *request = &client->localClient->auth;
107 >
108 >  memset(request, 0, sizeof(*request));
109  
110    request->client  = client;
111    request->timeout = CurrentTime + CONNECTTIMEOUT;
# Line 126 | Line 119 | make_auth_request(struct Client *client)
119   * the main io processing loop
120   */
121   void
122 < release_auth_client(struct Client *client)
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.
# Line 136 | Line 137 | release_auth_client(struct Client *clien
137    client->localClient->allow_read = MAX_FLOOD;
138    comm_setflush(&client->localClient->fd, 1000, flood_recalc, client);
139  
140 <  if ((client->node.prev != NULL) || (client->node.next != NULL))
140 <  {
141 <    sendto_realops_flags(UMODE_ALL, L_OPER,
142 <                         "already linked %s at %s:%d", client->name,
143 <                         __FILE__, __LINE__);
144 <    ilog(L_ERROR, "already linked %s at %s:%d", client->name,
145 <         __FILE__, __LINE__);
146 <    assert(0==5);
147 <  }
148 <  else
149 <    dlinkAdd(client, &client->node, &global_client_list);
140 >  dlinkAdd(client, &client->node, &global_client_list);
141  
142 <  client_p->since  = client_p->lasttime = client_p->firsttime = CurrentTime;
143 <  client_p->flags |= FLAGS_FINISHED_AUTH;
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 <
149 >
150   /*
151   * auth_dns_callback - called when resolver query finishes
152 < * if the query resulted in a successful search, hp will contain
153 < * a non-null pointer, otherwise hp will be null.
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, struct DNSReply *reply)
158 > auth_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
159   {
160 <  struct AuthRequest *auth = (struct AuthRequest *)vptr;
160 >  struct AuthRequest *auth = vptr;
161  
169  dlinkDelete(&auth->dns_node, &auth_doing_dns_list);
162    ClearDNSPending(auth);
163  
164 <  if (reply != NULL)
164 >  if (name != NULL)
165    {
166 <    struct sockaddr_in *v4, *v4dns;
166 >    const struct sockaddr_in *v4, *v4dns;
167   #ifdef IPV6
168 <    struct sockaddr_in6 *v6, *v6dns;
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 = (struct sockaddr_in6 *)&auth->client->localClient->ip;
176 <      v6dns = (struct sockaddr_in6 *)&reply->addr;
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);
# Line 191 | Line 183 | auth_dns_callback(void *vptr, struct DNS
183      else
184   #endif
185      {
186 <      v4 = (struct sockaddr_in *)&auth->client->localClient->ip;
187 <      v4dns = (struct sockaddr_in *)&reply->addr;
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(reply->h_name) <= HOSTLEN)
194 >    if (good && strlen(name) <= HOSTLEN)
195      {
196 <      strlcpy(auth->client->host, reply->h_name,
197 <              sizeof(auth->client->host));
196 >      strlcpy(auth->client->host, name,
197 >              sizeof(auth->client->host));
198        sendheader(auth->client, REPORT_FIN_DNS);
199      }
200 <    else if (strlen(reply->h_name) > HOSTLEN)
200 >    else if (strlen(name) > HOSTLEN)
201        sendheader(auth->client, REPORT_HOST_TOOLONG);
202    }
203    else
204 <      sendheader(auth->client, REPORT_FAIL_DNS);
213 <
214 <  MyFree(auth->client->localClient->dns_query);
215 <  auth->client->localClient->dns_query = NULL;
204 >    sendheader(auth->client, REPORT_FAIL_DNS);
205  
206 <  if (!IsDoingAuth(auth))
218 <  {
219 <    struct Client *client_p = auth->client;
220 <    MyFree(auth);
221 <    release_auth_client(client_p);
222 <  }
206 >  release_auth_client(auth);
207   }
208  
209   /*
# Line 228 | Line 212 | auth_dns_callback(void *vptr, struct DNS
212   static void
213   auth_error(struct AuthRequest *auth)
214   {
215 <  ++ServerStats->is_abad;
215 >  ++ServerStats.is_abad;
216  
217    fd_close(&auth->fd);
218  
235  dlinkDelete(&auth->ident_node, &auth_doing_ident_list);
219    ClearAuth(auth);
220  
221    sendheader(auth->client, REPORT_FAIL_ID);
222  
223 <  if (!IsDNSPending(auth) && !IsCrit(auth))
241 <  {
242 <    release_auth_client(auth->client);
243 <    MyFree(auth);
244 <  }
223 >  release_auth_client(auth);
224   }
225  
226   /*
227 < * start_auth_query - Flag the client to show that an attempt to
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
# Line 267 | Line 246 | start_auth_query(struct AuthRequest *aut
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(L_ERROR, "Unable to create auth socket for %s",
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;
253 >    ++ServerStats.is_abad;
254      return 0;
255    }
256  
257    sendheader(auth->client, REPORT_DO_ID);
258  
259 <  /*
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
# Line 299 | Line 278 | start_auth_query(struct AuthRequest *aut
278   #endif
279    localaddr.ss_port = htons(0);
280  
281 <  SetDoingAuth(auth);
282 <  dlinkAdd(auth, &auth->ident_node, &auth_doing_ident_list);
283 <
305 <  comm_connect_tcp(&auth->fd, auth->client->sockhost, 113,
306 <      (struct sockaddr *)&localaddr, localaddr.ss_len, auth_connect_callback,
307 <      auth, auth->client->localClient->ip.ss.ss_family,
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 < *
290 > *
291   * Inputs        - pointer to ident buf
292   * Output        - NULL if no valid ident found, otherwise pointer to name
293   * Side effects  -
# Line 344 | Line 320 | GetValidIdent(char *buf)
320  
321    /* All this to get rid of a sscanf() fun. */
322    remotePortString = buf;
323 <  
323 >
324    if ((colon1Ptr = strchr(remotePortString,':')) == NULL)
325      return 0;
326    *colon1Ptr = '\0';
# Line 354 | Line 330 | GetValidIdent(char *buf)
330      return 0;
331    *colon2Ptr = '\0';
332    colon2Ptr++;
333 <  
333 >
334    if ((commaPtr = strchr(remotePortString, ',')) == NULL)
335      return 0;
336    *commaPtr = '\0';
# Line 362 | Line 338 | GetValidIdent(char *buf)
338  
339    if ((remp = atoi(remotePortString)) == 0)
340      return 0;
341 <              
341 >
342    if ((locp = atoi(commaPtr)) == 0)
343      return 0;
344  
# Line 378 | Line 354 | GetValidIdent(char *buf)
354   }
355  
356   /*
357 < * start_auth
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 < static void *
364 < start_auth(va_list args)
363 > void
364 > start_auth(struct Client *client)
365   {
390  struct Client *client = va_arg(args, struct Client *);
366    struct AuthRequest *auth = NULL;
367  
368    assert(client != NULL);
369  
370    auth = make_auth_request(client);
371 <  SetCrit(auth);
397 <
398 <  client->localClient->dns_query = MyMalloc(sizeof(struct DNSQuery));
399 <  client->localClient->dns_query->ptr = auth;
400 <  client->localClient->dns_query->callback = auth_dns_callback;
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 <  /* auth order changed, before gethost_byaddr can immediately call
408 <   * dns callback under win32 when the lookup cannot be started.
409 <   * And that would do MyFree(auth) etc -adx */
410 <  SetDNSPending(auth);
411 <  dlinkAdd(auth, &auth->dns_node, &auth_doing_dns_list);
412 <  ClearCrit(auth);
413 <  gethost_byaddr(&client->localClient->ip, client->localClient->dns_query);
414 <
415 <  return NULL;
383 >  gethost_byaddr(auth_dns_callback, auth, &client->localClient->ip);
384   }
385  
386   /*
# Line 422 | Line 390 | start_auth(va_list args)
390   static void
391   timeout_auth_queries_event(void *notused)
392   {
393 <  dlink_node *ptr;
426 <  dlink_node *next_ptr;
427 <  struct AuthRequest* auth;
393 >  dlink_node *ptr = NULL, *next_ptr = NULL;
394  
395 <  DLINK_FOREACH_SAFE(ptr, next_ptr, auth_doing_ident_list.head)
395 >  DLINK_FOREACH_SAFE(ptr, next_ptr, auth_doing_list.head)
396    {
397 <    auth = ptr->data;
397 >    struct AuthRequest *auth = ptr->data;
398  
399 <    if (auth->timeout <= CurrentTime)
399 >    if (auth->timeout > CurrentTime)
400 >      continue;
401 >
402 >    if (IsDoingAuth(auth))
403      {
404 +      ++ServerStats.is_abad;
405        fd_close(&auth->fd);
406 <
437 <      ++ServerStats->is_abad;
406 >      ClearAuth(auth);
407        sendheader(auth->client, REPORT_FAIL_ID);
408 +    }
409  
410 <      if (IsDNSPending(auth))
411 <      {
412 <        struct Client *client_p = auth->client;
413 <
414 <        dlinkDelete(&auth->dns_node, &auth_doing_dns_list);
445 <        if (client_p->localClient->dns_query != NULL)
446 <        {
447 <          delete_resolver_queries(client_p->localClient->dns_query);
448 <          MyFree(client_p->localClient->dns_query);
449 <        }
450 <        auth->client->localClient->dns_query = NULL;
451 <        sendheader(client_p, REPORT_FAIL_DNS);
452 <      }
453 <
454 <      ilog(L_INFO, "DNS/AUTH timeout %s",
455 <           get_client_name(auth->client, SHOW_IP));
456 <
457 <      dlinkDelete(&auth->ident_node, &auth_doing_ident_list);
458 <      release_auth_client(auth->client);
459 <      MyFree(auth);
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  
# Line 481 | Line 440 | auth_connect_callback(fde_t *fd, int err
440    char authbuf[32];
441    socklen_t ulen = sizeof(struct irc_ssaddr);
442    socklen_t tlen = sizeof(struct irc_ssaddr);
443 <  u_int16_t uport, tport;
443 >  uint16_t uport, tport;
444   #ifdef IPV6
445    struct sockaddr_in6 *v6;
446   #else
# Line 494 | Line 453 | auth_connect_callback(fde_t *fd, int err
453      return;
454    }
455  
456 <  if (getsockname(auth->client->localClient->fd.fd, (struct sockaddr *) &us,
457 <      (socklen_t *) &ulen) ||
458 <      getpeername(auth->client->localClient->fd.fd, (struct sockaddr *) &them,
459 <      (socklen_t *) &tlen))
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(L_INFO, "auth get{sock,peer}name error for %s",
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;
# Line 520 | Line 479 | auth_connect_callback(fde_t *fd, int err
479    us.ss_len = ulen;
480    them.ss_len = tlen;
481   #endif
482 <  
483 <  ircsprintf(authbuf, "%u , %u\r\n", tport, uport);
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
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
# Line 560 | Line 520 | read_auth_reply(fde_t *fd, void *data)
520     *
521     *    --nenolod
522     */
563 #ifndef _WIN32
523    len = read(fd->fd, buf, AUTH_BUFSIZ);
524 < #else
566 <  len = recv(fd->fd, buf, AUTH_BUFSIZ, 0);
567 < #endif
568 <  
524 >
525    if (len < 0)
526    {
571 #ifdef _WIN32
572    errno = WSAGetLastError();
573 #endif
527      if (ignoreErrno(errno))
528        comm_setselect(fd, COMM_SELECT_READ, read_auth_reply, auth, 0);
529      else
# Line 606 | Line 559 | read_auth_reply(fde_t *fd, void *data)
559  
560    fd_close(fd);
561  
609  dlinkDelete(&auth->ident_node, &auth_doing_ident_list);  
562    ClearAuth(auth);
563  
564    if (s == NULL)
565    {
566      sendheader(auth->client, REPORT_FAIL_ID);
567 <    ++ServerStats->is_abad;
567 >    ++ServerStats.is_abad;
568    }
569    else
570    {
571      sendheader(auth->client, REPORT_FIN_ID);
572 <    ++ServerStats->is_asuc;
572 >    ++ServerStats.is_asuc;
573      SetGotId(auth->client);
574    }
575  
576 <  if (!IsDNSPending(auth) && !IsCrit(auth))
625 <  {
626 <    release_auth_client(auth->client);
627 <    MyFree(auth);
628 <  }
576 >  release_auth_client(auth);
577   }
578  
579   /*
580   * delete_auth()
581   */
582 < void
583 < delete_auth(struct Client *target_p)
582 > void
583 > delete_auth(struct AuthRequest *auth)
584   {
585 <  dlink_node *ptr;
586 <  dlink_node *next_ptr;
639 <  struct AuthRequest *auth;
640 <
641 <  if (!IsUnknown(target_p))
642 <    return;
643 <
644 <  if (target_p->localClient->dns_query != NULL)
645 <    DLINK_FOREACH_SAFE(ptr, next_ptr, auth_doing_dns_list.head)
646 <    {
647 <      auth = ptr->data;
585 >  if (IsDNSPending(auth))
586 >    delete_resolver_queries(auth);
587  
588 <      if (auth->client == target_p)
589 <      {
651 <        delete_resolver_queries(target_p->localClient->dns_query);
588 >  if (IsDoingAuth(auth))
589 >    fd_close(&auth->fd);
590  
591 <        dlinkDelete(&auth->dns_node, &auth_doing_dns_list);
592 <        if (!IsDoingAuth(auth))
655 <        {
656 <          MyFree(auth);
657 <          return;
658 <        }
659 <      }
660 <    }
661 <
662 <  DLINK_FOREACH_SAFE(ptr, next_ptr, auth_doing_ident_list.head)
663 <  {
664 <    auth = ptr->data;
665 <
666 <    if (auth->client == target_p)
667 <    {
668 <      fd_close(&auth->fd);
669 <
670 <      dlinkDelete(&auth->ident_node, &auth_doing_ident_list);
671 <      MyFree(auth);
672 <    }
673 <  }
591 >  if (dlinkFind(&auth_doing_list, auth))
592 >    dlinkDelete(&auth->node, &auth_doing_list);
593   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)