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

Comparing:
ircd-hybrid-7.3/src/s_bsd.c (file contents), Revision 1123 by michael, Sun Feb 6 21:57:50 2011 UTC vs.
ircd-hybrid/branches/8.2.x/src/s_bsd.c (file contents), Revision 4341 by michael, Sat Aug 2 16:53:48 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  s_bsd.c: Network functions.
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_bsd.c
23 > * \brief Network functions.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
28 + #ifdef HAVE_LIBCRYPTO
29 + #include "rsa.h"
30 + #endif
31   #include <netinet/in_systm.h>
32   #include <netinet/ip.h>
33   #include <netinet/tcp.h>
# Line 30 | Line 35
35   #include "fdlist.h"
36   #include "s_bsd.h"
37   #include "client.h"
33 #include "common.h"
38   #include "dbuf.h"
39   #include "event.h"
40   #include "irc_string.h"
# Line 38 | Line 42
42   #include "listener.h"
43   #include "numeric.h"
44   #include "packet.h"
45 < #include "irc_res.h"
45 > #include "res.h"
46   #include "restart.h"
47 < #include "s_auth.h"
48 < #include "s_conf.h"
49 < #include "s_log.h"
50 < #include "s_serv.h"
47 > #include "auth.h"
48 > #include "conf.h"
49 > #include "log.h"
50 > #include "server.h"
51   #include "send.h"
52   #include "memory.h"
53 < #include "s_user.h"
54 < #include "hook.h"
53 > #include "user.h"
54 >
55  
56   static const char *comm_err_str[] = { "Comm OK", "Error during bind()",
57    "Error during DNS lookup", "connect timeout", "Error during connect()",
58    "Comm Error" };
59  
56 struct Callback *setup_socket_cb = NULL;
57
60   static void comm_connect_callback(fde_t *, int);
61   static PF comm_connect_timeout;
62   static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
# Line 71 | Line 73 | check_can_use_v6(void)
73    int v6;
74  
75    if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
76 <    ServerInfo.can_use_v6 = 0;
76 >    ConfigServerInfo.can_use_v6 = 0;
77    else
78    {
79 <    ServerInfo.can_use_v6 = 1;
79 >    ConfigServerInfo.can_use_v6 = 1;
80      close(v6);
81    }
82   #else
83 <  ServerInfo.can_use_v6 = 0;
83 >  ConfigServerInfo.can_use_v6 = 0;
84   #endif
85   }
86  
# Line 107 | Line 109 | get_sockerr(int fd)
109   }
110  
111   /*
112 < * report_error - report an error from an errno.
112 > * report_error - report an error from an errno.
113   * Record error to log and also send a copy to all *LOCAL* opers online.
114   *
115   *        text        is a *format* string for outputing error. It must
# Line 121 | Line 123 | get_sockerr(int fd)
123   * Cannot use perror() within daemon. stderr is closed in
124   * ircd and cannot be used. And, worse yet, it might have
125   * been reassigned to a normal connection...
126 < *
126 > *
127   * Actually stderr is still there IFF ircd was run with -s --Rodder
128   */
129  
130   void
131 < report_error(int level, const char* text, const char* who, int error)
131 > report_error(int level, const char* text, const char* who, int error)
132   {
133    who = (who) ? who : "";
134  
135 <  sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error));
136 <  log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error));
137 <  ilog(L_ERROR, text, who, strerror(error));
135 >  sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
136 >                       text, who, strerror(error));
137 >  ilog(LOG_TYPE_IRCD, text, who, strerror(error));
138   }
139  
140   /*
# Line 140 | Line 142 | report_error(int level, const char* text
142   *
143   * Set the socket non-blocking, and other wonderful bits.
144   */
145 < static void *
146 < setup_socket(va_list args)
145 > static void
146 > setup_socket(int fd)
147   {
146  int fd = va_arg(args, int);
148    int opt = 1;
149  
150    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
# Line 154 | Line 155 | setup_socket(va_list args)
155   #endif
156  
157    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
157
158  return NULL;
159 }
160
161 /*
162 * init_comm()
163 *
164 * Initializes comm subsystem.
165 */
166 void
167 init_comm(void)
168 {
169  setup_socket_cb = register_callback("setup_socket", setup_socket);
170  init_netio();
158   }
159  
160   /*
# Line 178 | Line 165 | init_comm(void)
165   void
166   close_connection(struct Client *client_p)
167   {
168 <  struct ConfItem *conf;
182 <  struct AccessItem *aconf;
183 <  struct ClassItem *aclass;
168 >  dlink_node *ptr = NULL;
169  
170 <  assert(NULL != client_p);
170 >  assert(client_p);
171  
172    if (!IsDead(client_p))
173    {
# Line 191 | Line 176 | close_connection(struct Client *client_p
176       * even if it is marked as blocked (COMM_SELECT_READ handler is called
177       * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
178       */
179 <    ClearSendqBlocked(client_p);
179 >    DelFlag(client_p, FLAGS_BLOCKED);
180      send_queued_write(client_p);
181    }
182  
183 <  if (IsServer(client_p))
183 >  if (IsClient(client_p))
184 >  {
185 >    ++ServerStats.is_cl;
186 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
187 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
188 >    ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
189 >  }
190 >  else if (IsServer(client_p))
191    {
192      ++ServerStats.is_sv;
193      ServerStats.is_sbs += client_p->localClient->send.bytes;
194      ServerStats.is_sbr += client_p->localClient->recv.bytes;
195 <    ServerStats.is_sti += CurrentTime - client_p->firsttime;
195 >    ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
196  
197 <    /* XXX Does this even make any sense at all anymore?
206 <     * scheduling a 'quick' reconnect could cause a pile of
207 <     * nick collides under TSora protocol... -db
208 <     */
209 <    /*
210 <     * If the connection has been up for a long amount of time, schedule
211 <     * a 'quick' reconnect, else reset the next-connect cycle.
212 <     */
213 <    if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
214 <                                client_p->username, client_p->host)))
197 >    DLINK_FOREACH(ptr, server_items.head)
198      {
199 +      struct MaskItem *conf = ptr->data;
200 +
201 +      if (irccmp(conf->name, client_p->name))
202 +        continue;
203 +
204        /*
205 <       * Reschedule a faster reconnect, if this was a automatically
206 <       * connected configuration entry. (Note that if we have had
219 <       * a rehash in between, the status has been changed to
220 <       * CONF_ILLEGAL). But only do this if it was a "good" link.
205 >       * Reset next-connect cycle of all connect{} blocks that match
206 >       * this servername.
207         */
208 <      aconf  = map_to_conf(conf);
223 <      aclass = map_to_conf(aconf->class_ptr);
224 <      aconf->hold = time(NULL);
225 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
226 <        HANGONRETRYDELAY : ConFreq(aclass);
208 >      conf->until = CurrentTime + conf->class->con_freq;
209      }
210    }
229  else if (IsClient(client_p))
230  {
231    ++ServerStats.is_cl;
232    ServerStats.is_cbs += client_p->localClient->send.bytes;
233    ServerStats.is_cbr += client_p->localClient->recv.bytes;
234    ServerStats.is_cti += CurrentTime - client_p->firsttime;
235  }
211    else
212      ++ServerStats.is_ni;
213  
# Line 248 | Line 223 | close_connection(struct Client *client_p
223    if (client_p->localClient->fd.flags.open)
224      fd_close(&client_p->localClient->fd);
225  
251  if (HasServlink(client_p))
252  {
253    if (client_p->localClient->ctrlfd.flags.open)
254      fd_close(&client_p->localClient->ctrlfd);
255  }
256
226    dbuf_clear(&client_p->localClient->buf_sendq);
227    dbuf_clear(&client_p->localClient->buf_recvq);
228 <  
228 >
229    MyFree(client_p->localClient->passwd);
230 <  detach_conf(client_p, CONF_TYPE);
262 <  client_p->from = NULL; /* ...this should catch them! >:) --msa */
230 >  detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
231   }
232  
233   #ifdef HAVE_LIBCRYPTO
# Line 270 | Line 238 | close_connection(struct Client *client_p
238   static void
239   ssl_handshake(int fd, struct Client *client_p)
240   {
241 <  int ret = SSL_accept(client_p->localClient->fd.ssl);
241 >  X509 *cert = NULL;
242 >  int ret = 0;
243 >
244 >  if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0)
245 >  {
246 >    if ((CurrentTime - client_p->localClient->firsttime) > 30)
247 >    {
248 >      exit_client(client_p, "Timeout during SSL handshake");
249 >      return;
250 >    }
251  
275  if (ret <= 0)
252      switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
253      {
254        case SSL_ERROR_WANT_WRITE:
255          comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
256 <                       (PF *) ssl_handshake, client_p, 0);
256 >                       (PF *)ssl_handshake, client_p, 30);
257          return;
258  
259        case SSL_ERROR_WANT_READ:
260          comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
261 <                       (PF *) ssl_handshake, client_p, 0);
261 >                       (PF *)ssl_handshake, client_p, 30);
262          return;
263  
264        default:
265 <        exit_client(client_p, client_p, "Error during SSL handshake");
266 <        return;
265 >        exit_client(client_p, "Error during SSL handshake");
266 >        return;
267 >    }
268 >  }
269 >
270 >  comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL);
271 >
272 >  if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
273 >  {
274 >    int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
275 >    char buf[EVP_MAX_MD_SIZE * 2 + 1] = "";
276 >    unsigned char md[EVP_MAX_MD_SIZE] = "";
277 >
278 >    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
279 >        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
280 >        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
281 >    {
282 >      unsigned int n = 0;
283 >
284 >      if (X509_digest(cert, ConfigServerInfo.message_digest_algorithm, md, &n))
285 >      {
286 >        binary_to_hex(md, buf, n);
287 >        client_p->certfp = xstrdup(buf);
288 >      }
289      }
290 +    else
291 +      ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d",
292 +           client_p->name, client_p->username, client_p->host, res);
293 +    X509_free(cert);
294 +  }
295  
296 <  execute_callback(auth_cb, client_p);
296 >  start_auth(client_p);
297   }
298   #endif
299  
300   /*
301 < * add_connection - creates a client which has just connected to us on
301 > * add_connection - creates a client which has just connected to us on
302   * the given fd. The sockhost field is initialized with the ip# of the host.
303   * An unique id is calculated now, in case it is needed for auth.
304   * The client is sent to the auth module for verification, and not put in
# Line 304 | Line 307 | ssl_handshake(int fd, struct Client *cli
307   void
308   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
309   {
310 <  struct Client *new_client = make_client(NULL);
310 >  struct Client *client_p = make_client(NULL);
311  
312 <  fd_open(&new_client->localClient->fd, fd, 1,
312 >  fd_open(&client_p->localClient->fd, fd, 1,
313            (listener->flags & LISTENER_SSL) ?
314 <          "Incoming SSL connection" : "Incoming connection");
314 >          "Incoming SSL connection" : "Incoming connection");
315  
316    /*
317     * copy address to 'sockhost' as a string, copy it to host too
318     * so we have something valid to put into error messages...
319     */
320 <  memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
320 >  memcpy(&client_p->localClient->ip, irn, sizeof(struct irc_ssaddr));
321 >
322 >  getnameinfo((struct sockaddr *)&client_p->localClient->ip,
323 >              client_p->localClient->ip.ss_len, client_p->sockhost,
324 >              sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST);
325 >  client_p->localClient->aftype = client_p->localClient->ip.ss.ss_family;
326 >
327 > #ifdef HAVE_LIBGEOIP
328 >  /* XXX IPV6 SUPPORT XXX */
329 >  if (irn->ss.ss_family == AF_INET && geoip_ctx)
330 >  {
331 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)&client_p->localClient->ip;
332 >    client_p->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr));
333 >  }
334 > #endif
335  
336 <  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
337 <              new_client->localClient->ip.ss_len, new_client->sockhost,
338 <              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
339 <  new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
340 <
341 <  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
325 <  {
326 <    strlcpy(new_client->host, "0", sizeof(new_client->host));
327 <    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
328 <    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
329 <    new_client->sockhost[0] = '0';
336 >  if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':')
337 >  {
338 >    strlcpy(client_p->host, "0", sizeof(client_p->host));
339 >    strlcpy(client_p->host+1, client_p->sockhost, sizeof(client_p->host)-1);
340 >    memmove(client_p->sockhost+1, client_p->sockhost, sizeof(client_p->sockhost)-1);
341 >    client_p->sockhost[0] = '0';
342    }
343    else
344 <    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
344 >    strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host));
345  
346 <  new_client->localClient->listener = listener;
346 >  client_p->localClient->listener = listener;
347    ++listener->ref_count;
348  
349   #ifdef HAVE_LIBCRYPTO
350    if (listener->flags & LISTENER_SSL)
351    {
352 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
352 >    if ((client_p->localClient->fd.ssl = SSL_new(ConfigServerInfo.server_ctx)) == NULL)
353      {
354 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
354 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
355             ERR_error_string(ERR_get_error(), NULL));
356  
357 <      SetDead(new_client);
358 <      exit_client(new_client, new_client, "SSL_new failed");
357 >      SetDead(client_p);
358 >      exit_client(client_p, "SSL_new failed");
359        return;
360      }
361  
362 <    SSL_set_fd(new_client->localClient->fd.ssl, fd);
363 <    ssl_handshake(0, new_client);
362 >    AddFlag(client_p, FLAGS_SSL);
363 >    SSL_set_fd(client_p->localClient->fd.ssl, fd);
364 >    ssl_handshake(0, client_p);
365    }
366    else
367   #endif
368 <    execute_callback(auth_cb, new_client);
368 >    start_auth(client_p);
369   }
370  
371   /*
# Line 407 | Line 420 | comm_settimeout(fde_t *fd, time_t timeou
420   * flush functions, and when comm_close() is implemented correctly
421   * with close functions, we _actually_ don't call comm_close() here ..
422   * -- originally Adrian's notes
423 < * comm_close() is replaced with fd_close() in fdlist.c
423 > * comm_close() is replaced with fd_close() in fdlist.c
424   */
425   void
426   comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
# Line 499 | Line 512 | comm_connect_tcp(fde_t *fd, const char *
512     *   -- adrian
513     */
514    if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
515 <  {
515 >  {
516      /* Failure, call the callback with COMM_ERR_BIND */
517      comm_connect_callback(fd, COMM_ERR_BIND);
518      /* ... and quit */
# Line 596 | Line 609 | comm_connect_dns_callback(void *vptr, co
609    /* Copy over the DNS reply info so we can use it in the connect() */
610    /*
611     * Note we don't fudge the refcount here, because we aren't keeping
612 <   * the DNS record around, and the DNS cache is gone anyway..
612 >   * the DNS record around, and the DNS cache is gone anyway..
613     *     -- adrian
614     */
615    memcpy(&F->connect.hostaddr, addr, addr->ss_len);
# Line 627 | Line 640 | comm_connect_tryconnect(fde_t *fd, void
640      return;
641  
642    /* Try the connect() */
643 <  retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
643 >  retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
644      fd->connect.hostaddr.ss_len);
645  
646    /* Error? */
# Line 693 | Line 706 | comm_open(fde_t *F, int family, int sock
706    if (fd < 0)
707      return -1; /* errno will be passed through, yay.. */
708  
709 <  execute_callback(setup_socket_cb, fd);
709 >  setup_socket(fd);
710  
711    /* update things in our fd tracking */
712    fd_open(F, fd, 1, note);
# Line 734 | Line 747 | comm_accept(struct Listener *lptr, struc
747    pn->ss_len = addrlen;
748   #endif
749  
750 <  execute_callback(setup_socket_cb, newfd);
750 >  setup_socket(newfd);
751  
752    /* .. and return */
753    return newfd;
754   }
755  
756 < /*
756 > /*
757   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
758   * OSes with IPv6 mapping listening on both
759   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
760 < *
760 > *
761   */
762   #ifdef IPV6
763   void
# Line 754 | Line 767 | remove_ipv6_mapping(struct irc_ssaddr *a
767    {
768      if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
769      {
770 <      struct sockaddr_in6 v6;
770 >      struct sockaddr_in6 v6;
771        struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
772  
773        memcpy(&v6, addr, sizeof(v6));
# Line 764 | Line 777 | remove_ipv6_mapping(struct irc_ssaddr *a
777        addr->ss.ss_family = AF_INET;
778        addr->ss_len = sizeof(struct sockaddr_in);
779      }
780 <    else
780 >    else
781        addr->ss_len = sizeof(struct sockaddr_in6);
782    }
783    else
784      addr->ss_len = sizeof(struct sockaddr_in);
785 < }
785 > }
786   #endif

Diff Legend

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