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

Comparing:
ircd-hybrid-7.2/src/s_bsd.c (file contents), Revision 683 by michael, Thu Jun 15 14:28:59 2006 UTC vs.
ircd-hybrid/trunk/src/s_bsd.c (file contents), Revision 2733 by michael, Fri Jan 3 17:30:13 2014 UTC

# Line 23 | Line 23
23   */
24  
25   #include "stdinc.h"
26 #ifndef _WIN32
26   #include <netinet/in_systm.h>
27   #include <netinet/ip.h>
28   #include <netinet/tcp.h>
29 < #endif
29 > #include "list.h"
30   #include "fdlist.h"
31   #include "s_bsd.h"
32   #include "client.h"
34 #include "common.h"
33   #include "dbuf.h"
34   #include "event.h"
35   #include "irc_string.h"
38 #include "irc_getnameinfo.h"
39 #include "irc_getaddrinfo.h"
36   #include "ircd.h"
41 #include "list.h"
37   #include "listener.h"
38   #include "numeric.h"
39   #include "packet.h"
40   #include "irc_res.h"
46 #include "inet_misc.h"
41   #include "restart.h"
42   #include "s_auth.h"
43 < #include "s_conf.h"
44 < #include "s_log.h"
43 > #include "conf.h"
44 > #include "log.h"
45   #include "s_serv.h"
52 #include "s_stats.h"
46   #include "send.h"
47   #include "memory.h"
48   #include "s_user.h"
# Line 59 | Line 52 | static const char *comm_err_str[] = { "C
52    "Error during DNS lookup", "connect timeout", "Error during connect()",
53    "Comm Error" };
54  
55 < struct Callback *setup_socket_cb = NULL;
63 <
64 < static void comm_connect_callback(fde_t *fd, int status);
55 > static void comm_connect_callback(fde_t *, int);
56   static PF comm_connect_timeout;
57 < static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
57 > static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
58   static PF comm_connect_tryconnect;
59  
69 extern void init_netio(void);
60  
61   /* check_can_use_v6()
62   *  Check if the system can open AF_INET6 sockets
# Line 82 | Line 72 | check_can_use_v6(void)
72    else
73    {
74      ServerInfo.can_use_v6 = 1;
85 #ifdef _WIN32
86    closesocket(v6);
87 #else
75      close(v6);
89 #endif
76    }
77   #else
78    ServerInfo.can_use_v6 = 0;
# Line 102 | Line 88 | check_can_use_v6(void)
88   int
89   get_sockerr(int fd)
90   {
105 #ifndef _WIN32
91    int errtmp = errno;
107 #else
108  int errtmp = WSAGetLastError();
109 #endif
92   #ifdef SO_ERROR
93    int err = 0;
94    socklen_t len = sizeof(err);
95  
96 <  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
96 >  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
97    {
98      if (err)
99        errtmp = err;
# Line 145 | Line 127 | report_error(int level, const char* text
127   {
128    who = (who) ? who : "";
129  
130 <  sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error));
131 <  log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error));
132 <  ilog(L_ERROR, text, who, strerror(error));
130 >  sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
131 >                       text, who, strerror(error));
132 >  ilog(LOG_TYPE_IRCD, text, who, strerror(error));
133   }
134  
135   /*
# Line 155 | Line 137 | report_error(int level, const char* text
137   *
138   * Set the socket non-blocking, and other wonderful bits.
139   */
140 < static void *
141 < setup_socket(va_list args)
140 > static void
141 > setup_socket(int fd)
142   {
161  int fd = va_arg(args, int);
143    int opt = 1;
144  
145 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
145 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
146  
147   #ifdef IPTOS_LOWDELAY
148    opt = IPTOS_LOWDELAY;
149 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
149 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
150   #endif
151  
171 #ifndef _WIN32
152    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
173 #endif
174
175  return NULL;
176 }
177
178 /*
179 * init_comm()
180 *
181 * Initializes comm subsystem.
182 */
183 void
184 init_comm(void)
185 {
186  setup_socket_cb = register_callback("setup_socket", setup_socket);
187  init_netio();
153   }
154  
155   /*
# Line 195 | Line 160 | init_comm(void)
160   void
161   close_connection(struct Client *client_p)
162   {
163 <  struct ConfItem *conf;
199 <  struct AccessItem *aconf;
200 <  struct ClassItem *aclass;
163 >  dlink_node *ptr = NULL;
164  
165 <  assert(NULL != client_p);
165 >  assert(client_p);
166  
167    if (!IsDead(client_p))
168    {
# Line 212 | Line 175 | close_connection(struct Client *client_p
175      send_queued_write(client_p);
176    }
177  
178 <  if (IsServer(client_p))
178 >  if (IsClient(client_p))
179    {
180 <    ServerStats->is_sv++;
181 <    ServerStats->is_sbs += client_p->localClient->send.bytes;
182 <    ServerStats->is_sbr += client_p->localClient->recv.bytes;
183 <    ServerStats->is_sti += CurrentTime - client_p->firsttime;
184 <
185 <    /* XXX Does this even make any sense at all anymore?
186 <     * scheduling a 'quick' reconnect could cause a pile of
187 <     * nick collides under TSora protocol... -db
188 <     */
189 <    /*
190 <     * If the connection has been up for a long amount of time, schedule
191 <     * a 'quick' reconnect, else reset the next-connect cycle.
192 <     */
230 <    if ((conf = find_conf_exact(SERVER_TYPE,
231 <                                  client_p->name, client_p->username,
232 <                                  client_p->host)))
180 >    ++ServerStats.is_cl;
181 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
182 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
183 >    ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
184 >  }
185 >  else if (IsServer(client_p))
186 >  {
187 >    ++ServerStats.is_sv;
188 >    ServerStats.is_sbs += client_p->localClient->send.bytes;
189 >    ServerStats.is_sbr += client_p->localClient->recv.bytes;
190 >    ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
191 >
192 >    DLINK_FOREACH(ptr, server_items.head)
193      {
194 +      struct MaskItem *conf = ptr->data;
195 +
196 +      if (irccmp(conf->name, client_p->name))
197 +        continue;
198 +
199        /*
200 <       * Reschedule a faster reconnect, if this was a automatically
201 <       * connected configuration entry. (Note that if we have had
237 <       * a rehash in between, the status has been changed to
238 <       * CONF_ILLEGAL). But only do this if it was a "good" link.
200 >       * Reset next-connect cycle of all connect{} blocks that match
201 >       * this servername.
202         */
203 <      aconf = (struct AccessItem *)map_to_conf(conf);
241 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
242 <      aconf->hold = time(NULL);
243 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
244 <        HANGONRETRYDELAY : ConFreq(aclass);
245 <      if (nextconnect > aconf->hold)
246 <        nextconnect = aconf->hold;
203 >      conf->until = CurrentTime + conf->class->con_freq;
204      }
205    }
249  else if (IsClient(client_p))
250  {
251    ServerStats->is_cl++;
252    ServerStats->is_cbs += client_p->localClient->send.bytes;
253    ServerStats->is_cbr += client_p->localClient->recv.bytes;
254    ServerStats->is_cti += CurrentTime - client_p->firsttime;
255  }
206    else
207 <    ServerStats->is_ni++;
207 >    ++ServerStats.is_ni;
208  
209   #ifdef HAVE_LIBCRYPTO
210    if (client_p->localClient->fd.ssl)
# Line 268 | Line 218 | close_connection(struct Client *client_p
218    if (client_p->localClient->fd.flags.open)
219      fd_close(&client_p->localClient->fd);
220  
271  if (HasServlink(client_p))
272  {
273    if (client_p->localClient->ctrlfd.flags.open)
274      fd_close(&client_p->localClient->ctrlfd);
275  }
276
221    dbuf_clear(&client_p->localClient->buf_sendq);
222    dbuf_clear(&client_p->localClient->buf_recvq);
223    
224    MyFree(client_p->localClient->passwd);
225 <  detach_conf(client_p, CONF_TYPE);
225 >  detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
226    client_p->from = NULL; /* ...this should catch them! >:) --msa */
227   }
228  
# Line 290 | Line 234 | close_connection(struct Client *client_p
234   static void
235   ssl_handshake(int fd, struct Client *client_p)
236   {
237 <  int ret = SSL_accept(client_p->localClient->fd.ssl);
237 >  X509 *cert = NULL;
238 >  int ret = 0;
239 >
240 >  if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0)
241 >  {
242 >    if ((CurrentTime - client_p->localClient->firsttime) > 30)
243 >    {
244 >      exit_client(client_p, client_p, "Timeout during SSL handshake");
245 >      return;
246 >    }
247  
295  if (ret <= 0)
248      switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
249      {
250        case SSL_ERROR_WANT_WRITE:
251          comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
252 <                       (PF *) ssl_handshake, client_p, 0);
252 >                       (PF *) ssl_handshake, client_p, 30);
253          return;
254  
255        case SSL_ERROR_WANT_READ:
256          comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
257 <                       (PF *) ssl_handshake, client_p, 0);
257 >                       (PF *) ssl_handshake, client_p, 30);
258          return;
259  
260        default:
261          exit_client(client_p, client_p, "Error during SSL handshake");
262          return;
263      }
264 +  }
265  
266 <  execute_callback(auth_cb, client_p);
266 >  comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL);
267 >
268 >  if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
269 >  {
270 >    int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
271 >    char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
272 >    unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
273 >
274 >    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
275 >        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
276 >        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
277 >    {
278 >      unsigned int i = 0, n = 0;
279 >
280 >      if (X509_digest(cert, EVP_sha256(), md, &n))
281 >      {
282 >        for (; i < n; ++i)
283 >          snprintf(buf + 2 * i, 3, "%02X", md[i]);
284 >        client_p->certfp = xstrdup(buf);
285 >      }
286 >    }
287 >    else
288 >      ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d",
289 >           client_p->name, client_p->username, client_p->host, res);
290 >    X509_free(cert);
291 >  }
292 >
293 >  start_auth(client_p);
294   }
295   #endif
296  
# Line 324 | Line 304 | ssl_handshake(int fd, struct Client *cli
304   void
305   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
306   {
307 <  struct Client *new_client;
328 <
329 <  assert(NULL != listener);
330 <
331 <  new_client = make_client(NULL);
307 >  struct Client *new_client = make_client(NULL);
308  
309    fd_open(&new_client->localClient->fd, fd, 1,
310            (listener->flags & LISTENER_SSL) ?
311            "Incoming SSL connection" : "Incoming connection");
312  
313 <  /*
313 >  /*
314     * copy address to 'sockhost' as a string, copy it to host too
315     * so we have something valid to put into error messages...
316     */
317    memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
318  
319 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
320 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
321 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
319 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
320 >              new_client->localClient->ip.ss_len, new_client->sockhost,
321 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
322    new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
347 #ifdef IPV6
348  if (new_client->sockhost[0] == ':')
349    strlcat(new_client->host, "0", HOSTLEN+1);
323  
324 <  if (new_client->localClient->aftype == AF_INET6 &&
325 <      ConfigFileEntry.dot_in_ip6_addr == 1)
324 > #ifdef HAVE_LIBGEOIP
325 >  /* XXX IPV6 SUPPORT XXX */
326 >  if (irn->ss.ss_family == AF_INET && geoip_ctx)
327    {
328 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
329 <    strlcat(new_client->host, ".", HOSTLEN+1);
328 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)&new_client->localClient->ip;
329 >    new_client->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr));
330    }
357  else
331   #endif
359    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
332  
333 <  new_client->connect_id = ++connect_id;
333 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
334 >  {
335 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
336 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
337 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
338 >    new_client->sockhost[0] = '0';
339 >  }
340 >  else
341 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
342 >
343    new_client->localClient->listener = listener;
344    ++listener->ref_count;
345  
346   #ifdef HAVE_LIBCRYPTO
347    if (listener->flags & LISTENER_SSL)
348    {
349 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
349 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
350      {
351 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
351 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
352             ERR_error_string(ERR_get_error(), NULL));
353  
354        SetDead(new_client);
# Line 375 | Line 356 | add_connection(struct Listener *listener
356        return;
357      }
358  
359 +    AddFlag(new_client, FLAGS_SSL);
360      SSL_set_fd(new_client->localClient->fd.ssl, fd);
361      ssl_handshake(0, new_client);
362    }
363    else
364   #endif
365 <    execute_callback(auth_cb, new_client);
365 >    start_auth(new_client);
366   }
367  
368   /*
# Line 510 | Line 492 | comm_connect_tcp(fde_t *fd, const char *
492                   void *data, int aftype, int timeout)
493   {
494    struct addrinfo hints, *res;
495 <  char portname[PORTNAMELEN+1];
495 >  char portname[PORTNAMELEN + 1];
496  
497    assert(callback);
498    fd->connect.callback = callback;
# Line 542 | Line 524 | comm_connect_tcp(fde_t *fd, const char *
524    hints.ai_socktype = SOCK_STREAM;
525    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
526  
527 <  snprintf(portname, PORTNAMELEN, "%d", port);
527 >  snprintf(portname, sizeof(portname), "%d", port);
528  
529 <  if (irc_getaddrinfo(host, portname, &hints, &res))
529 >  if (getaddrinfo(host, portname, &hints, &res))
530    {
531      /* Send the DNS request, for the next level */
532 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
533 <    fd->dns_query->ptr = fd;
534 <    fd->dns_query->callback = comm_connect_dns_callback;
535 <    gethost_byname(host, fd->dns_query);
532 >    if (aftype == AF_INET6)
533 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
534 >    else
535 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
536    }
537    else
538    {
# Line 560 | Line 542 | comm_connect_tcp(fde_t *fd, const char *
542      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
543      fd->connect.hostaddr.ss_len = res->ai_addrlen;
544      fd->connect.hostaddr.ss.ss_family = res->ai_family;
545 <    irc_freeaddrinfo(res);
545 >    freeaddrinfo(res);
546      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
547      comm_connect_tryconnect(fd, NULL);
548    }
# Line 608 | Line 590 | comm_connect_timeout(fde_t *fd, void *no
590   * otherwise we initiate the connect()
591   */
592   static void
593 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
593 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
594   {
595    fde_t *F = vptr;
596  
597 <  if (reply == NULL)
597 >  if (name == NULL)
598    {
617    MyFree(F->dns_query);
618    F->dns_query = NULL;
599      comm_connect_callback(F, COMM_ERR_DNS);
600      return;
601    }
# Line 629 | Line 609 | comm_connect_dns_callback(void *vptr, st
609     * the DNS record around, and the DNS cache is gone anyway..
610     *     -- adrian
611     */
612 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
612 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
613    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
614    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
615      F->connect.hostaddr.ss_port;
616 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
616 >  F->connect.hostaddr.ss_len = addr->ss_len;
617  
618    /* Now, call the tryconnect() routine to try a connect() */
639  MyFree(F->dns_query);
640  F->dns_query = NULL;
619    comm_connect_tryconnect(F, NULL);
620   }
621  
# Line 665 | Line 643 | comm_connect_tryconnect(fde_t *fd, void
643    /* Error? */
644    if (retval < 0)
645    {
668 #ifdef _WIN32
669    errno = WSAGetLastError();
670 #endif
646      /*
647       * If we get EISCONN, then we've already connect()ed the socket,
648       * which is a good thing.
# Line 726 | Line 701 | comm_open(fde_t *F, int family, int sock
701     */
702    fd = socket(family, sock_type, proto);
703    if (fd < 0)
729  {
730 #ifdef _WIN32
731    errno = WSAGetLastError();
732 #endif
704      return -1; /* errno will be passed through, yay.. */
734  }
705  
706 <  execute_callback(setup_socket_cb, fd);
706 >  setup_socket(fd);
707  
708    /* update things in our fd tracking */
709    fd_open(F, fd, 1, note);
# Line 764 | Line 734 | comm_accept(struct Listener *lptr, struc
734     * reserved fd limit, but we can deal with that when comm_open()
735     * also does it. XXX -- adrian
736     */
737 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
737 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
738    if (newfd < 0)
769  {
770 #ifdef _WIN32
771    errno = WSAGetLastError();
772 #endif
739      return -1;
774  }
740  
741   #ifdef IPV6
742    remove_ipv6_mapping(pn);
# Line 779 | Line 744 | comm_accept(struct Listener *lptr, struc
744    pn->ss_len = addrlen;
745   #endif
746  
747 <  execute_callback(setup_socket_cb, newfd);
747 >  setup_socket(newfd);
748  
749    /* .. and return */
750    return newfd;
# Line 787 | Line 752 | comm_accept(struct Listener *lptr, struc
752  
753   /*
754   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
755 < * This function should really inspect the struct itself rather than relying
791 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
755 > * OSes with IPv6 mapping listening on both
756   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
757   *
758   */
# Line 798 | Line 762 | remove_ipv6_mapping(struct irc_ssaddr *a
762   {
763    if (addr->ss.ss_family == AF_INET6)
764    {
765 <    struct sockaddr_in6 *v6;
802 <
803 <    v6 = (struct sockaddr_in6*)addr;
804 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
765 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
766      {
767 <      char v4ip[HOSTIPLEN];
768 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
769 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
770 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
767 >      struct sockaddr_in6 v6;
768 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
769 >
770 >      memcpy(&v6, addr, sizeof(v6));
771 >      memset(v4, 0, sizeof(struct sockaddr_in));
772 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
773 >
774        addr->ss.ss_family = AF_INET;
775        addr->ss_len = sizeof(struct sockaddr_in);
776      }

Diff Legend

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