Kannel: Open Source WAP and SMS gateway  svn-r5335
conn.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /* conn.c - implement Connection type
58  *
59  * This file implements the interface defined in conn.h.
60  *
61  * Richard Braakman
62  *
63  * SSL client implementation contributed by
64  * Jarkko Kovala <jarkko.kovala@iki.fi>
65  *
66  * SSL server implementation contributed by
67  * Stipe Tolj <stolj at kannel.org>
68  */
69 
70 /* TODO: unlocked_close() on error */
71 /* TODO: have I/O functions check if connection is open */
72 /* TODO: have conn_open_tcp do a non-blocking connect() */
73 
74 #include <signal.h>
75 #include <unistd.h>
76 #include <errno.h>
77 
78 #include <sys/types.h>
79 #include <sys/socket.h>
80 #include <string.h>
81 
82 #include "gwlib/gwlib.h"
83 
84 #ifdef HAVE_LIBSSL
85 #include <openssl/ssl.h>
86 #include <openssl/err.h>
87 #include <openssl/conf.h>
88 #include <openssl/engine.h>
89 
90 static SSL_CTX *global_ssl_context = NULL;
91 static SSL_CTX *global_server_ssl_context = NULL;
92 #endif /* HAVE_LIBSSL */
93 
94 typedef unsigned long (*CRYPTO_CALLBACK_PTR)(void);
95 
96 /*
97  * This used to be 4096. It is now 0 so that callers don't have to
98  * deal with the complexities of buffering (i.e. deciding when to
99  * flush) unless they want to.
100  * FIXME: Figure out how to combine buffering sensibly with use of
101  * conn_register.
102  */
103 #define DEFAULT_OUTPUT_BUFFERING 0
104 #define SSL_CONN_TIMEOUT 30
105 
107 {
108  /* We use separate locks for input and ouput fields, so that
109  * read and write activities don't have to get in each other's
110  * way. If you need both, then acquire the outlock first. */
113  volatile sig_atomic_t claimed;
114 #ifndef NO_GWASSERT
116 #endif
117 
118  /* fd value is read-only and is not locked */
119  int fd;
120 
121  /* socket state */
122  enum {yes,no} connected;
123 
124  /* Protected by outlock */
126  long outbufpos; /* start of unwritten data in outbuf */
127 
128  /* Try to buffer writes until there are this many octets to send.
129  * Set it to 0 to get an unbuffered connection. */
130  unsigned int output_buffering;
131 
132  /* Protected by inlock */
134  long inbufpos; /* start of unread data in inbuf */
135 
136  int read_eof; /* we encountered eof on read */
137  int io_error; /* we encountered error on IO operation */
138 
139  /* Protected by both locks when updating, so you need only one
140  * of the locks when reading. */
145  /* Protected by inlock */
147  /* Protected by outlock */
149 
150 #ifdef HAVE_LIBSSL
151  SSL *ssl;
152  X509 *peer_certificate;
153 #endif /* HAVE_LIBSSL */
154 };
155 
156 static void unlocked_register_pollin(Connection *conn, int onoff);
157 static void unlocked_register_pollout(Connection *conn, int onoff);
158 
159 /* There are a number of functions that play with POLLIN and POLLOUT flags.
160  * The general rule is that we always want to poll for POLLIN except when
161  * we have detected eof (which may be reported as eternal POLLIN), and
162  * we want to poll for POLLOUT only if there's data waiting in the
163  * output buffer. If output buffering is set, we may not want to poll for
164  * POLLOUT if there's not enough data waiting, which is why we have
165  * unlocked_try_write. */
166 
167 /* Macros to get more information for debugging purposes */
168 #define unlock_in(conn) unlock_in_real(conn, __FILE__, __LINE__, __func__)
169 #define unlock_out(conn) unlock_out_real(conn, __FILE__, __LINE__, __func__)
170 
171 /* Lock a Connection's read direction, if the Connection is unclaimed */
172 static void inline lock_in(Connection *conn)
173 {
174  gw_assert(conn != NULL);
175 
176  if (conn->claimed)
178  else
179  mutex_lock(conn->inlock);
180 }
181 
182 /* Unlock a Connection's read direction, if the Connection is unclaimed */
183 static void inline unlock_in_real(Connection *conn, char *file, int line, const char *func)
184 {
185  int ret;
186  gw_assert(conn != NULL);
187 
188  if (!conn->claimed && (ret = mutex_unlock(conn->inlock)) != 0) {
189  panic(0, "%s:%ld: %s: Mutex unlock failed. "
190  "(Called from %s:%ld:%s.)",
191  __FILE__, (long) __LINE__, __func__,
192  file, (long) line, func);
193  }
194 }
195 
196 /* Lock a Connection's write direction, if the Connection is unclaimed */
197 static void inline lock_out(Connection *conn)
198 {
199  gw_assert(conn != NULL);
200 
201  if (conn->claimed)
203  else
204  mutex_lock(conn->outlock);
205 }
206 
207 /* Unlock a Connection's write direction, if the Connection is unclaimed */
208 static void inline unlock_out_real(Connection *conn, char *file, int line, const char *func)
209 {
210  int ret;
211  gw_assert(conn != NULL);
212 
213  if (!conn->claimed && (ret = mutex_unlock(conn->outlock)) != 0) {
214  panic(0, "%s:%ld: %s: Mutex unlock failed. "
215  "(Called from %s:%ld:%s.)",
216  __FILE__, (long) __LINE__, __func__,
217  file, (long) line, func);
218  }
219 }
220 
221 /* Return the number of bytes in the Connection's output buffer */
222 static long inline unlocked_outbuf_len(Connection *conn)
223 {
224  return octstr_len(conn->outbuf) - conn->outbufpos;
225 }
226 
227 /* Return the number of bytes in the Connection's input buffer */
228 static long inline unlocked_inbuf_len(Connection *conn)
229 {
230  return octstr_len(conn->inbuf) - conn->inbufpos;
231 }
232 
233 /* Send as much data as can be sent without blocking. Return the number
234  * of bytes written, or -1 in case of error. */
235 static long unlocked_write(Connection *conn)
236 {
237  long ret = 0;
238 
239 #ifdef HAVE_LIBSSL
240  if (conn->ssl != NULL) {
241  if (octstr_len(conn->outbuf) - conn->outbufpos > 0)
242  ret = SSL_write(conn->ssl,
243  octstr_get_cstr(conn->outbuf) + conn->outbufpos,
244  octstr_len(conn->outbuf) - conn->outbufpos);
245 
246  if (ret < 0) {
247  int SSL_error = SSL_get_error(conn->ssl, ret);
248 
249  if (SSL_error == SSL_ERROR_WANT_READ || SSL_error == SSL_ERROR_WANT_WRITE) {
250  ret = 0; /* no error */
251  } else {
252  error(errno, "SSL write failed: OpenSSL error %d: %s",
253  SSL_error, ERR_error_string(SSL_error, NULL));
254  if (SSL_error == SSL_ERROR_SSL) { /* trace library errors */
255  long err;
256  while ((err = ERR_get_error()) != 0)
257  error(0, "SSL %s", ERR_error_string(err, NULL));
258  }
259  return -1;
260  }
261  }
262  } else
263 #endif /* HAVE_LIBSSL */
264  ret = octstr_write_data(conn->outbuf, conn->fd, conn->outbufpos);
265 
266  if (ret < 0) {
267  conn->io_error = 1;
268  return -1;
269  }
270 
271  conn->outbufpos += ret;
272 
273  /* Heuristic: Discard the already-written data if it's more than
274  * half of the total. This should keep the buffer size small
275  * without wasting too many cycles on moving data around. */
276  if (conn->outbufpos > octstr_len(conn->outbuf) / 2) {
277  octstr_delete(conn->outbuf, 0, conn->outbufpos);
278  conn->outbufpos = 0;
279  }
280 
281  if (conn->registered)
283 
284  return ret;
285 }
286 
287 /* Try to empty the output buffer without blocking. Return 0 for success,
288  * 1 if there is still data left in the buffer, and -1 for errors. */
290 {
291  long len;
292 
293  len = unlocked_outbuf_len(conn);
294  if (len == 0)
295  return 0;
296 
297  if (len < (long) conn->output_buffering)
298  return 1;
299 
300  if (unlocked_write(conn) < 0)
301  return -1;
302 
303  if (unlocked_outbuf_len(conn) > 0)
304  return 1;
305 
306  return 0;
307 }
308 
309 /* Read whatever data is currently available, up to an internal maximum. */
310 static void unlocked_read(Connection *conn)
311 {
312  unsigned char buf[4096];
313  long len = 0;
314 
315  if (conn->inbufpos > 0) {
316  octstr_delete(conn->inbuf, 0, conn->inbufpos);
317  conn->inbufpos = 0;
318  }
319 
320 #ifdef HAVE_LIBSSL
321  if (conn->ssl != NULL) {
322  /* We should empty out all data in the SSL read buffer.
323  * If we don't, then the next poll() on the file descriptor will *not* behave as expected.
324  */
325  do {
326  octstr_append_data(conn->inbuf, buf, len);
327  len = SSL_read(conn->ssl, buf, sizeof(buf));
328  } while (len > 0 && SSL_pending(conn->ssl) > 0);
329  } else
330 #endif /* HAVE_LIBSSL */
331  len = read(conn->fd, buf, sizeof(buf));
332 
333  if (len < 0) {
334  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
335  return;
336 #ifdef HAVE_LIBSSL
337  if (conn->ssl) {
338  int SSL_error = SSL_get_error(conn->ssl, len);
339  if (SSL_error == SSL_ERROR_WANT_WRITE || SSL_error == SSL_ERROR_WANT_READ)
340  return; /* no error */
341  error(errno, "SSL read failed: OpenSSL error %d: %s",
342  SSL_error, ERR_error_string(SSL_error, NULL));
343  }
344  else
345 #endif /* HAVE_LIBSSL */
346  error(errno, "Error reading from fd %d:", conn->fd);
347  conn->io_error = 1;
348  if (conn->registered)
349  unlocked_register_pollin(conn, 0);
350  return;
351  } else if (len == 0) {
352  conn->read_eof = 1;
353  if (conn->registered)
354  unlocked_register_pollin(conn, 0);
355  } else {
356  octstr_append_data(conn->inbuf, buf, len);
357  }
358 }
359 
360 /* Cut "length" octets from the input buffer and return them as an Octstr */
361 static Octstr *unlocked_get(Connection *conn, long length)
362 {
363  Octstr *result = NULL;
364 
365  gw_assert(unlocked_inbuf_len(conn) >= length);
366  result = octstr_copy(conn->inbuf, conn->inbufpos, length);
367  conn->inbufpos += length;
368 
369  return result;
370 }
371 
372 /* Tell the fdset whether we are interested in POLLIN events, but only
373  * if the status changed. (Calling fdset_listen can be expensive if
374  * it requires synchronization with the polling thread.)
375  * We must already have the inlock.
376  */
377 static void unlocked_register_pollin(Connection *conn, int onoff)
378 {
379  gw_assert(conn->registered);
380 
381  if (onoff == 1 && !conn->listening_pollin) {
382  /* Turn it on */
383  conn->listening_pollin = 1;
384  fdset_listen(conn->registered, conn->fd, POLLIN, POLLIN);
385  } else if (onoff == 0 && conn->listening_pollin) {
386  /* Turn it off */
387  conn->listening_pollin = 0;
388  fdset_listen(conn->registered, conn->fd, POLLIN, 0);
389  }
390 }
391 
392 /* Tell the fdset whether we are interested in POLLOUT events, but only
393  * if the status changed. (Calling fdset_listen can be expensive if
394  * it requires synchronization with the polling thread.)
395  * We must already have the outlock.
396  */
397 static void unlocked_register_pollout(Connection *conn, int onoff)
398 {
399  gw_assert(conn->registered);
400 
401  if (onoff == 1 && !conn->listening_pollout) {
402  /* Turn it on */
403  conn->listening_pollout = 1;
404  fdset_listen(conn->registered, conn->fd, POLLOUT, POLLOUT);
405  } else if (onoff == 0 && conn->listening_pollout) {
406  /* Turn it off */
407  conn->listening_pollout = 0;
408  fdset_listen(conn->registered, conn->fd, POLLOUT, 0);
409  }
410 }
411 
412 #ifdef HAVE_LIBSSL
413 static int conn_init_client_ssl(Connection *ret, Octstr *certkeyfile)
414 {
415  ret->ssl = SSL_new(global_ssl_context);
416 
417  /*
418  * The current thread's error queue must be empty before
419  * the TLS/SSL I/O operation is attempted, or SSL_get_error()
420  * will not work reliably.
421  */
422  ERR_clear_error();
423 
424  if (certkeyfile != NULL) {
425  SSL_use_certificate_file(ret->ssl, octstr_get_cstr(certkeyfile),
426  SSL_FILETYPE_PEM);
427  SSL_use_PrivateKey_file(ret->ssl, octstr_get_cstr(certkeyfile),
428  SSL_FILETYPE_PEM);
429  if (SSL_check_private_key(ret->ssl) != 1) {
430  error(0, "conn_open_ssl: private key isn't consistent with the "
431  "certificate from file %s (or failed reading the file)",
432  octstr_get_cstr(certkeyfile));
433  return -1;
434  }
435  }
436 
437  /* SSL_set_fd can fail, so check it */
438  if (SSL_set_fd(ret->ssl, ret->fd) == 0) {
439  /* SSL_set_fd failed, log error */
440  error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL));
441  return -1;
442  }
443 
444  /*
445  * make sure the socket is non-blocking while we do SSL_connect
446  */
447  if (socket_set_blocking(ret->fd, 0) < 0) {
448  return -1;
449  }
450  BIO_set_nbio(SSL_get_rbio(ret->ssl), 1);
451  BIO_set_nbio(SSL_get_wbio(ret->ssl), 1);
452 
453  SSL_set_connect_state(ret->ssl);
454 
455  return 0;
456 }
457 
458 Connection *conn_open_ssl_nb(Octstr *host, int port, Octstr *certkeyfile,
459  Octstr *our_host)
460 {
461  Connection *ret;
462 
463  /* open the TCP connection */
464  if (!(ret = conn_open_tcp_nb(host, port, our_host))) {
465  return NULL;
466  }
467 
468  if (conn_init_client_ssl(ret, certkeyfile) == -1) {
469  conn_destroy(ret);
470  return NULL;
471  }
472 
473  return ret;
474 }
475 
476 Connection *conn_open_ssl(Octstr *host, int port, Octstr *certkeyfile,
477  Octstr *our_host)
478 {
479  Connection *ret;
480 
481  /* open the TCP connection */
482  if (!(ret = conn_open_tcp(host, port, our_host))) {
483  return NULL;
484  }
485 
486  if (conn_init_client_ssl(ret, certkeyfile) == -1) {
487  conn_destroy(ret);
488  return NULL;
489  }
490 
491  return ret;
492 }
493 
494 #endif /* HAVE_LIBSSL */
495 
497 {
499 }
500 
502 {
504 }
505 
507  Octstr *our_host)
508 {
509  int sockfd;
510  int done = -1;
511  Connection *c;
512 
514  our_port, our_host == NULL ?
515  NULL : octstr_get_cstr(our_host), &done);
516  if (sockfd < 0)
517  return NULL;
518  c = conn_wrap_fd(sockfd, 0);
519  if (done != 0) {
520  c->connected = no;
521  }
522  return c;
523 }
524 
526 {
527  return conn->connected == yes ? 0 : -1;
528 }
529 
531 {
532  int err;
533  socklen_t len;
534 
535  len = sizeof(err);
536  if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
537  return -1;
538  }
539 
540  if (err) {
541  return -1;
542  }
543 
544  conn->connected = yes;
545  return 0;
546 }
547 
549  Octstr *our_host)
550 {
551  int sockfd;
552 
554  our_port, our_host == NULL ?
555  NULL : octstr_get_cstr(our_host));
556  if (sockfd < 0)
557  return NULL;
558  return conn_wrap_fd(sockfd, 0);
559 }
560 
561 
562 /*
563  * XXX bad assumption here that conn_wrap_fd for SSL can only happens
564  * for the server side!!!! FIXME !!!!
565  */
567 {
568  Connection *conn;
569 
570  if (socket_set_blocking(fd, 0) < 0)
571  return NULL;
572 
573  conn = gw_malloc(sizeof(*conn));
574  conn->inlock = mutex_create();
575  conn->outlock = mutex_create();
576  conn->claimed = 0;
577 
578  conn->outbuf = octstr_create("");
579  conn->outbufpos = 0;
580  conn->inbuf = octstr_create("");
581  conn->inbufpos = 0;
582 
583  conn->fd = fd;
584  conn->connected = yes;
585  conn->read_eof = 0;
586  conn->io_error = 0;
588 
589  conn->registered = NULL;
590  conn->callback = NULL;
591  conn->callback_data = NULL;
592  conn->callback_data_destroyer = NULL;
593  conn->listening_pollin = 0;
594  conn->listening_pollout = 0;
595 #ifdef HAVE_LIBSSL
596  /*
597  * do all the SSL magic for this connection
598  */
599  if (ssl) {
600  conn->ssl = SSL_new(global_server_ssl_context);
601  conn->peer_certificate = NULL;
602 
603  /* SSL_set_fd can fail, so check it */
604  if (SSL_set_fd(conn->ssl, conn->fd) == 0) {
605  /* SSL_set_fd failed, log error and return NULL */
606  error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL));
607  conn_destroy(conn);
608  return NULL;
609  }
610  /* SSL_set_verify(conn->ssl, 0, NULL); */
611 
612  /* set read/write BIO layer to non-blocking mode */
613  BIO_set_nbio(SSL_get_rbio(conn->ssl), 1);
614  BIO_set_nbio(SSL_get_wbio(conn->ssl), 1);
615 
616  /* set accept state , SSL-Handshake will be handled transparent while SSL_[read|write] */
617  SSL_set_accept_state(conn->ssl);
618  } else {
619  conn->ssl = NULL;
620  conn->peer_certificate = NULL;
621  }
622 #endif /* HAVE_LIBSSL */
623 
624  return conn;
625 }
626 
628 {
629  int ret;
630 
631  if (conn == NULL)
632  return;
633 
634  /* No locking done here. conn_destroy should not be called
635  * if any thread might still be interested in the connection. */
636 
637  if (conn->registered) {
638  fdset_unregister(conn->registered, conn->fd);
639  /* call data destroyer if any */
640  if (conn->callback_data != NULL && conn->callback_data_destroyer != NULL)
642  }
643 
644  if (conn->fd >= 0) {
645  /* Try to flush any remaining data */
646  unlocked_try_write(conn);
647 
648 #ifdef HAVE_LIBSSL
649  if (conn->ssl != NULL) {
650  SSL_smart_shutdown(conn->ssl);
651  SSL_free(conn->ssl);
652  if (conn->peer_certificate != NULL)
653  X509_free(conn->peer_certificate);
654  }
655 #endif /* HAVE_LIBSSL */
656 
657  ret = close(conn->fd);
658  if (ret < 0)
659  error(errno, "conn_destroy: error on close");
660  conn->fd = -1;
661  }
662 
663  octstr_destroy(conn->outbuf);
664  octstr_destroy(conn->inbuf);
665  mutex_destroy(conn->inlock);
666  mutex_destroy(conn->outlock);
667 
668  gw_free(conn);
669 }
670 
672 {
673  gw_assert(conn != NULL);
674 
675  if (conn->claimed)
676  panic(0, "Connection is being claimed twice!");
677  conn->claimed = 1;
678 #ifndef NO_GWASSERT
679  conn->claiming_thread = gwthread_self();
680 #endif
681 }
682 
684 {
685  long len;
686 
687  lock_out(conn);
688  len = unlocked_outbuf_len(conn);
689  unlock_out(conn);
690 
691  return len;
692 }
693 
695 {
696  long len;
697 
698  lock_in(conn);
699  len = unlocked_inbuf_len(conn);
700  unlock_in(conn);
701 
702  return len;
703 }
704 
706 {
707  int eof;
708 
709  lock_in(conn);
710  eof = conn->read_eof;
711  unlock_in(conn);
712 
713  return eof;
714 }
715 
717 {
718  int err;
719 
720  lock_out(conn);
721  lock_in(conn);
722  err = conn->io_error;
723  unlock_in(conn);
724  unlock_out(conn);
725 
726  return err;
727 }
728 
729 void conn_set_output_buffering(Connection *conn, unsigned int size)
730 {
731  lock_out(conn);
732  conn->output_buffering = size;
733  /* If the buffer size is smaller, we may have to write immediately. */
734  unlocked_try_write(conn);
735  unlock_out(conn);
736 }
737 
738 static void poll_callback(int fd, int revents, void *data)
739 {
740  Connection *conn;
741  int do_callback = 0;
742 
743  conn = data;
744 
745  if (conn == NULL) {
746  error(0, "poll_callback called with NULL connection.");
747  return;
748  }
749 
750  if (conn->fd != fd) {
751  error(0, "poll_callback called on wrong connection.");
752  return;
753  }
754 
755  /* Get result of nonblocking connect, before any reads and writes
756  * we must check result (it must be handled in initial callback) */
757  if (conn->connected == no) {
758  if (conn->callback)
759  conn->callback(conn, conn->callback_data);
760  return;
761  }
762 
763  /* If got POLLERR or POLHUP, then unregister the descriptor from the
764  * fdset and set the error condition variable to let the upper layer
765  * close and destroy the connection. */
766  if (revents & (POLLERR|POLLHUP)) {
767  lock_out(conn);
768  lock_in(conn);
769  if (conn->listening_pollin)
770  unlocked_register_pollin(conn, 0);
771  if (conn->listening_pollout)
772  unlocked_register_pollout(conn, 0);
773  conn->io_error = 1;
774  unlock_in(conn);
775  unlock_out(conn);
776  do_callback = 1;
777  }
778 
779  /* If unlocked_write manages to write all pending data, it will
780  * tell the fdset to stop listening for POLLOUT. */
781  if (revents & POLLOUT) {
782  lock_out(conn);
783  unlocked_write(conn);
784  if (unlocked_outbuf_len(conn) == 0)
785  do_callback = 1;
786  unlock_out(conn);
787  }
788 
789  /* We read only in unlocked_read in we received POLLIN, cause the
790  * descriptor is already broken and of no use anymore. */
791  if (revents & POLLIN) {
792  lock_in(conn);
793  unlocked_read(conn);
794  unlock_in(conn);
795  do_callback = 1;
796  }
797 
798  if (do_callback && conn->callback)
799  conn->callback(conn, conn->callback_data);
800 }
801 
803  conn_callback_t callback, void *data, conn_callback_data_destroyer_t *data_destroyer)
804 {
805  int events;
806  int result = 0;
807 
808  gw_assert(conn != NULL);
809 
810  if (conn->fd < 0)
811  return -1;
812 
813  /* We need both locks if we want to update the registration
814  * information. */
815  lock_out(conn);
816  lock_in(conn);
817 
818  if (conn->registered == fdset) {
819  /* Re-registering. Change only the callback info. */
820  conn->callback = callback;
821  /* call data destroyer if new data supplied */
822  if (conn->callback_data != NULL && conn->callback_data != data && conn->callback_data_destroyer != NULL)
824  conn->callback_data = data;
825  conn->callback_data_destroyer = data_destroyer;
826  result = 0;
827  } else if (conn->registered) {
828  /* Already registered to a different fdset. */
829  result = -1;
830  } else {
831  events = 0;
832  /* For nonconnected socket we must lesten both directions */
833  if (conn->connected == yes) {
834  if (conn->read_eof == 0 && conn->io_error == 0)
835  events |= POLLIN;
836  if (unlocked_outbuf_len(conn) > 0)
837  events |= POLLOUT;
838  } else {
839  events |= POLLIN | POLLOUT;
840  }
841 
842  conn->registered = fdset;
843  conn->callback = callback;
844  conn->callback_data = data;
845  conn->callback_data_destroyer = data_destroyer;
846  conn->listening_pollin = (events & POLLIN) != 0;
847  conn->listening_pollout = (events & POLLOUT) != 0;
848  fdset_register(fdset, conn->fd, events, poll_callback, conn);
849  result = 0;
850  }
851 
852  unlock_in(conn);
853  unlock_out(conn);
854 
855  return result;
856 }
857 
859 {
860  FDSet *set = NULL;
861  int fd = -1;
862  void *data = NULL;
863  conn_callback_data_destroyer_t *destroyer = NULL;
864 
865  gw_assert(conn != NULL);
866 
867  if (conn == NULL || conn->fd < 0)
868  return;
869 
870  /* We need both locks to update the registration information */
871  lock_out(conn);
872  lock_in(conn);
873 
874  if (conn->registered) {
875  set = conn->registered;
876  fd = conn->fd;
877  conn->registered = NULL;
878  conn->callback = NULL;
879  /*
880  * remember and don't destroy data and data_destroyer because we
881  * may be in callback right now. So destroy only after fdset_unregister
882  * call which guarantee us we are not in callback anymore.
883  */
884  data = conn->callback_data;
885  conn->callback_data = NULL;
886  destroyer = conn->callback_data_destroyer;
887  conn->callback_data_destroyer = NULL;
888  conn->listening_pollin = 0;
889  conn->listening_pollout = 0;
890  }
891 
892  unlock_in(conn);
893  unlock_out(conn);
894 
895  /* now unregister from FDSet */
896  if (set != NULL)
897  fdset_unregister(set, fd);
898 
899  /* ok we are not in callback anymore, destroy data if any */
900  if (data != NULL && destroyer != NULL)
901  destroyer(data);
902 }
903 
904 int conn_wait(Connection *conn, double seconds)
905 {
906  int events;
907  int ret;
908  int fd;
909 
910  lock_out(conn);
911 
912  /* Try to write any data that might still be waiting to be sent */
913  ret = unlocked_write(conn);
914  if (ret < 0) {
915  unlock_out(conn);
916  return -1;
917  }
918  if (ret > 0) {
919  /* We did something useful. No need to poll or wait now. */
920  unlock_out(conn);
921  return 0;
922  }
923 
924  fd = conn->fd;
925 
926  /* Normally, we block until there is more data available. But
927  * if any data still needs to be sent, we block until we can
928  * send it (or there is more data available). We always block
929  * for reading, unless we know there is no more data coming.
930  * (Because in that case, poll will keep reporting POLLIN to
931  * signal the end of the file). If the caller explicitly wants
932  * to wait even though there is no data to write and we're at
933  * end of file, then poll for new data anyway because the caller
934  * apparently doesn't trust eof. */
935  events = 0;
936  if (unlocked_outbuf_len(conn) > 0)
937  events |= POLLOUT;
938  /* Don't keep the connection locked while we wait */
939  unlock_out(conn);
940 
941  /* We need the in lock to query read_eof */
942  lock_in(conn);
943  if ((conn->read_eof == 0 && conn->io_error == 0) || events == 0)
944  events |= POLLIN;
945  unlock_in(conn);
946 
947  ret = gwthread_pollfd(fd, events, seconds);
948  if (ret < 0) {
949  if (errno == EINTR)
950  return 0;
951  error(0, "conn_wait: poll failed on fd %d:", fd);
952  return -1;
953  }
954 
955  if (ret == 0)
956  return 1;
957 
958  if (ret & POLLNVAL) {
959  error(0, "conn_wait: fd %d not open.", fd);
960  return -1;
961  }
962 
963  if (ret & (POLLERR | POLLHUP)) {
964  /* Call unlocked_read to report the specific error,
965  * and handle the results of the error. We can't be
966  * certain that the error still exists, because we
967  * released the lock for a while. */
968  lock_in(conn);
969  unlocked_read(conn);
970  unlock_in(conn);
971  return -1;
972  }
973 
974  /* If POLLOUT is on, then we must have wanted
975  * to write something. */
976  if (ret & POLLOUT) {
977  lock_out(conn);
978  unlocked_write(conn);
979  unlock_out(conn);
980  }
981 
982  /* Since we normally select for reading, we must
983  * try to read here. Otherwise, if the caller loops
984  * around conn_wait without making conn_read* calls
985  * in between, we will keep polling this same data. */
986  if (ret & POLLIN) {
987  lock_in(conn);
988  unlocked_read(conn);
989  unlock_in(conn);
990  }
991 
992  return 0;
993 }
994 
996 {
997  int ret;
998  int revents;
999  int fd;
1000 
1001  lock_out(conn);
1002  ret = unlocked_write(conn);
1003  if (ret < 0) {
1004  unlock_out(conn);
1005  return -1;
1006  }
1007 
1008  while (unlocked_outbuf_len(conn) != 0) {
1009  fd = conn->fd;
1010 
1011  unlock_out(conn);
1012  revents = gwthread_pollfd(fd, POLLOUT, -1.0);
1013 
1014  /* Note: Make sure we have the "out" lock when
1015  * going through the loop again, because the
1016  * loop condition needs it. */
1017 
1018  if (revents < 0) {
1019  if (errno == EINTR)
1020  return 1;
1021  error(0, "conn_flush: poll failed on fd %d:", fd);
1022  return -1;
1023  }
1024 
1025  if (revents == 0) {
1026  /* We were woken up */
1027  return 1;
1028  }
1029 
1030  if (revents & POLLNVAL) {
1031  error(0, "conn_flush: fd %d not open.", fd);
1032  return -1;
1033  }
1034 
1035  lock_out(conn);
1036 
1037  if (revents & (POLLOUT | POLLERR | POLLHUP)) {
1038  ret = unlocked_write(conn);
1039  if (ret < 0) {
1040  unlock_out(conn);
1041  return -1;
1042  }
1043  }
1044  }
1045 
1046  unlock_out(conn);
1047 
1048  return 0;
1049 }
1050 
1051 int conn_write(Connection *conn, Octstr *data)
1052 {
1053  int ret;
1054 
1055  lock_out(conn);
1056  octstr_append(conn->outbuf, data);
1057  ret = unlocked_try_write(conn);
1058  unlock_out(conn);
1059 
1060  return ret;
1061 }
1062 
1063 int conn_write_data(Connection *conn, unsigned char *data, long length)
1064 {
1065  int ret;
1066 
1067  lock_out(conn);
1068  octstr_append_data(conn->outbuf, data, length);
1069  ret = unlocked_try_write(conn);
1070  unlock_out(conn);
1071 
1072  return ret;
1073 }
1074 
1076 {
1077  int ret;
1078  unsigned char lengthbuf[4];
1079 
1080  encode_network_long(lengthbuf, octstr_len(data));
1081  lock_out(conn);
1082  octstr_append_data(conn->outbuf, lengthbuf, 4);
1083  octstr_append(conn->outbuf, data);
1084  ret = unlocked_try_write(conn);
1085  unlock_out(conn);
1086 
1087  return ret;
1088 }
1089 
1091 {
1092  Octstr *result = NULL;
1093 
1094  lock_in(conn);
1095  if (unlocked_inbuf_len(conn) == 0) {
1096  unlocked_read(conn);
1097  if (unlocked_inbuf_len(conn) == 0) {
1098  unlock_in(conn);
1099  return NULL;
1100  }
1101  }
1102 
1103  result = unlocked_get(conn, unlocked_inbuf_len(conn));
1104  gw_claim_area(result);
1105  unlock_in(conn);
1106 
1107  return result;
1108 }
1109 
1110 Octstr *conn_read_fixed(Connection *conn, long length)
1111 {
1112  Octstr *result = NULL;
1113 
1114  if (length < 1)
1115  return NULL;
1116 
1117  /* See if the data is already available. If not, try a read(),
1118  * then see if we have enough data after that. If not, give up. */
1119  lock_in(conn);
1120  if (unlocked_inbuf_len(conn) < length) {
1121  unlocked_read(conn);
1122  if (unlocked_inbuf_len(conn) < length) {
1123  unlock_in(conn);
1124  return NULL;
1125  }
1126  }
1127  result = unlocked_get(conn, length);
1128  gw_claim_area(result);
1129  unlock_in(conn);
1130 
1131  return result;
1132 }
1133 
1135 {
1136  Octstr *result = NULL;
1137  long pos;
1138 
1139  lock_in(conn);
1140  /* 10 is the code for linefeed. We don't rely on \n because that
1141  * might be a different value on some (strange) systems, and
1142  * we are reading from a network connection. */
1143  pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
1144  if (pos < 0) {
1145  unlocked_read(conn);
1146  pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
1147  if (pos < 0) {
1148  unlock_in(conn);
1149  return NULL;
1150  }
1151  }
1152 
1153  result = unlocked_get(conn, pos - conn->inbufpos);
1154  gw_claim_area(result);
1155 
1156  /* Skip the LF, which we left in the buffer */
1157  conn->inbufpos++;
1158 
1159  /* If the line was terminated with CR LF, we have to remove
1160  * the CR from the result. */
1161  if (octstr_len(result) > 0 &&
1162  octstr_get_char(result, octstr_len(result) - 1) == 13)
1163  octstr_delete(result, octstr_len(result) - 1, 1);
1164 
1165  unlock_in(conn);
1166  return result;
1167 }
1168 
1170 {
1171  Octstr *result = NULL;
1172  unsigned char lengthbuf[4];
1173  long length = 0; /* for compiler please */
1174  int try, retry;
1175 
1176  lock_in(conn);
1177 
1178  for (try = 1; try <= 2; try++) {
1179  if (try > 1)
1180  unlocked_read(conn);
1181 
1182  do {
1183  retry = 0;
1184  /* First get the length. */
1185  if (unlocked_inbuf_len(conn) < 4)
1186  continue;
1187 
1188  octstr_get_many_chars(lengthbuf, conn->inbuf, conn->inbufpos, 4);
1189  length = decode_network_long(lengthbuf);
1190 
1191  if (length < 0) {
1192  warning(0, "conn_read_withlen: got negative length, skipping");
1193  conn->inbufpos += 4;
1194  retry = 1;
1195  length = 0;
1196  }
1197  } while (retry == 1);
1198 
1199  /* Then get the data, as long as the read size is below
1200  * the indicated length. */
1201  if (unlocked_inbuf_len(conn) - 4 < length)
1202  continue;
1203 
1204  conn->inbufpos += 4;
1205  result = unlocked_get(conn, length);
1206  gw_claim_area(result);
1207  break;
1208  }
1209 
1210  unlock_in(conn);
1211  return result;
1212 }
1213 
1215 {
1216  int startpos, endpos;
1217  Octstr *result = NULL;
1218  int try;
1219 
1220  lock_in(conn);
1221 
1222  for (try = 1; try <= 2; try++) {
1223  if (try > 1)
1224  unlocked_read(conn);
1225 
1226  /* Find startmark, and discard everything up to it */
1227  if (startmark >= 0) {
1228  startpos = octstr_search_char(conn->inbuf, startmark, conn->inbufpos);
1229  if (startpos < 0) {
1230  conn->inbufpos = octstr_len(conn->inbuf);
1231  continue;
1232  } else {
1233  conn->inbufpos = startpos;
1234  }
1235  } else {
1236  startpos = conn->inbufpos;
1237  }
1238 
1239  /* Find first endmark after startmark */
1240  endpos = octstr_search_char(conn->inbuf, endmark, conn->inbufpos);
1241  if (endpos < 0)
1242  continue;
1243 
1244  result = unlocked_get(conn, endpos - startpos + 1);
1245  gw_claim_area(result);
1246  break;
1247  }
1248 
1249  unlock_in(conn);
1250  return result;
1251 }
1252 
1253 #ifdef HAVE_LIBSSL
1254 X509 *conn_get_peer_certificate(Connection *conn)
1255 {
1256  /* Don't know if it needed to be locked , but better safe as crash */
1257  lock_out(conn);
1258  lock_in(conn);
1259  if (conn->peer_certificate == NULL && conn->ssl != NULL)
1260  conn->peer_certificate = SSL_get_peer_certificate(conn->ssl);
1261  unlock_in(conn);
1262  unlock_out(conn);
1263 
1264  return conn->peer_certificate;
1265 }
1266 
1267 /*
1268  * XXX Alex decalred the RSA callback routine static and now we're getting
1269  * warning messages for our automatic compilation tests. So we are commenting
1270  * the function out to avoid the warnings.
1271  *
1272 
1273 static RSA *tmp_rsa_callback(SSL *ssl, int export, int key_len)
1274 {
1275  static RSA *rsa = NULL;
1276  debug("gwlib.http", 0, "SSL: Generating new RSA key (export=%d, keylen=%d)", export, key_len);
1277  if (export) {
1278  rsa = RSA_generate_key(key_len, RSA_F4, NULL, NULL);
1279  } else {
1280  debug("gwlib.http", 0, "SSL: Export not set");
1281  }
1282  return rsa;
1283 }
1284 */
1285 
1286 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1287 
1288 static Mutex **ssl_static_locks = NULL;
1289 
1290 /* the call-back function for the openssl crypto thread locking */
1291 static void openssl_locking_function(int mode, int n, const char *file, int line)
1292 {
1293  if (mode & CRYPTO_LOCK)
1294  mutex_lock(ssl_static_locks[n-1]);
1295  else
1296  mutex_unlock(ssl_static_locks[n-1]);
1297 }
1298 
1299 void openssl_init_locks(void)
1300 {
1301  int c, maxlocks = CRYPTO_num_locks();
1302 
1303  gw_assert(ssl_static_locks == NULL);
1304 
1305  ssl_static_locks = gw_malloc(sizeof(Mutex *) * maxlocks);
1306  for (c = 0; c < maxlocks; c++)
1307  ssl_static_locks[c] = mutex_create();
1308 
1309  /* after the mutexes have been created, apply the call-back to it */
1310  CRYPTO_set_locking_callback(openssl_locking_function);
1311  CRYPTO_set_id_callback((CRYPTO_CALLBACK_PTR)gwthread_self);
1312 }
1313 
1314 void openssl_shutdown_locks(void)
1315 {
1316  int c, maxlocks = CRYPTO_num_locks();
1317 
1318  gw_assert(ssl_static_locks != NULL);
1319 
1320  /* remove call-back from the locks */
1321  CRYPTO_set_locking_callback(NULL);
1322 
1323  for (c = 0; c < maxlocks; c++)
1324  mutex_destroy(ssl_static_locks[c]);
1325 
1326  gw_free(ssl_static_locks);
1327  ssl_static_locks = NULL;
1328 }
1329 
1330 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
1331 
1332 void conn_init_ssl(void)
1333 {
1334 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1335  openssl_init_locks();
1336 #endif
1337 
1338  SSL_library_init();
1339  SSL_load_error_strings();
1340  global_ssl_context = SSL_CTX_new(SSLv23_client_method());
1341  SSL_CTX_set_mode(global_ssl_context,
1342  SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1343 }
1344 
1345 void server_ssl_init(void)
1346 {
1347  SSLeay_add_ssl_algorithms();
1348  SSL_load_error_strings();
1349  global_server_ssl_context = SSL_CTX_new(SSLv23_server_method());
1350  SSL_CTX_set_mode(global_server_ssl_context,
1351  SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1352  if (!SSL_CTX_set_default_verify_paths(global_server_ssl_context)) {
1353  panic(0, "can not set default path for server");
1354  }
1355 }
1356 
1357 void conn_shutdown_ssl(void)
1358 {
1359 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1360  openssl_shutdown_locks();
1361 #endif
1362 
1363  if (global_ssl_context)
1364  SSL_CTX_free(global_ssl_context);
1365 
1366  CONF_modules_free();
1367  ERR_remove_state(0);
1368  ENGINE_cleanup();
1369  CONF_modules_unload(1);
1370  ERR_free_strings();
1371  EVP_cleanup();
1372  CRYPTO_cleanup_all_ex_data();
1373 }
1374 
1375 void server_shutdown_ssl(void)
1376 {
1377  if (global_server_ssl_context)
1378  SSL_CTX_free(global_server_ssl_context);
1379 
1380  CONF_modules_free();
1381  ERR_remove_state(0);
1382  ENGINE_cleanup();
1383  CONF_modules_unload(1);
1384  ERR_free_strings();
1385  EVP_cleanup();
1386  CRYPTO_cleanup_all_ex_data();
1387 }
1388 
1389 void conn_use_global_client_certkey_file(Octstr *certkeyfile)
1390 {
1391  SSL_CTX_use_certificate_chain_file(global_ssl_context,
1392  octstr_get_cstr(certkeyfile));
1393  SSL_CTX_use_PrivateKey_file(global_ssl_context,
1394  octstr_get_cstr(certkeyfile),
1395  SSL_FILETYPE_PEM);
1396  if (SSL_CTX_check_private_key(global_ssl_context) != 1)
1397  panic(0, "reading global client certificate file `%s', the certificate "
1398  "isn't consistent with the private key (or failed reading the file)",
1399  octstr_get_cstr(certkeyfile));
1400  info(0, "Using global SSL certificate and key from file `%s'",
1401  octstr_get_cstr(certkeyfile));
1402 }
1403 
1404 void conn_use_global_server_certkey_file(Octstr *certfile, Octstr *keyfile)
1405 {
1406  SSL_CTX_use_certificate_chain_file(global_server_ssl_context,
1407  octstr_get_cstr(certfile));
1408  SSL_CTX_use_PrivateKey_file(global_server_ssl_context,
1409  octstr_get_cstr(keyfile),
1410  SSL_FILETYPE_PEM);
1411  if (SSL_CTX_check_private_key(global_server_ssl_context) != 1) {
1412  error(0, "SSL: %s", ERR_error_string(ERR_get_error(), NULL));
1413  panic(0, "reading global server certificate file %s, the certificate \
1414  isn't consistent with the private key in file %s \
1415  (or failed reading the file)",
1416  octstr_get_cstr(certfile), octstr_get_cstr(keyfile));
1417  }
1418  info(0, "Using global server SSL certificate from file `%s'", octstr_get_cstr(certfile));
1419  info(0, "Using global server SSL key from file `%s'", octstr_get_cstr(keyfile));
1420 }
1421 
1422 void conn_use_global_client_cipher_list(Octstr *cipher)
1423 {
1424  if (SSL_CTX_set_cipher_list(global_ssl_context, octstr_get_cstr(cipher)) != 1) {
1425  error(0, "SSL: %s", ERR_error_string(ERR_get_error(), NULL));
1426  SSL_CTX_free(global_ssl_context);
1427  panic(0, "cipher list <%s> contains no supported ciphers!",
1428  octstr_get_cstr(cipher));
1429  }
1430 }
1431 
1432 void conn_use_global_server_cipher_list(Octstr *cipher)
1433 {
1434  if (SSL_CTX_set_cipher_list(global_server_ssl_context, octstr_get_cstr(cipher)) != 1) {
1435  error(0, "SSL: %s", ERR_error_string(ERR_get_error(), NULL));
1436  SSL_CTX_free(global_server_ssl_context);
1437  panic(0, "cipher list <%s> contains no supported ciphers!",
1438  octstr_get_cstr(cipher));
1439  }
1440 }
1441 
1442 static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
1443 {
1444  char subject[256];
1445  char issuer [256];
1446  char *status;
1447  X509 *curr_cert;
1448 
1449 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1450  curr_cert = ctx->current_cert;
1451  X509_NAME_oneline(X509_get_subject_name(curr_cert), subject, sizeof(subject));
1452  X509_NAME_oneline(X509_get_issuer_name(curr_cert), issuer, sizeof (issuer));
1453 #else
1454  curr_cert = X509_STORE_CTX_get_current_cert(ctx);
1455  X509_NAME_oneline(X509_get_subject_name(curr_cert), subject, sizeof(subject));
1456  X509_NAME_oneline(X509_get_issuer_name(curr_cert), issuer, sizeof (issuer));
1457 #endif
1458 
1459  status = preverify_ok ? "Accepting" : "Rejecting";
1460 
1461  info(0, "%s certificate for \"%s\" signed by \"%s\"", status, subject, issuer);
1462 
1463  return preverify_ok;
1464 }
1465 
1466 void conn_use_global_trusted_ca_file(Octstr *ssl_trusted_ca_file)
1467 {
1468  if (ssl_trusted_ca_file != NULL) {
1469  if (!SSL_CTX_load_verify_locations(global_ssl_context,
1470  octstr_get_cstr(ssl_trusted_ca_file),
1471  NULL)) {
1472  panic(0, "Failed to load SSL CA file: %s", octstr_get_cstr(ssl_trusted_ca_file));
1473  } else {
1474  info(0, "Using CA root certificates from file %s",
1475  octstr_get_cstr(ssl_trusted_ca_file));
1476  SSL_CTX_set_verify(global_ssl_context,
1477  SSL_VERIFY_PEER,
1478  verify_callback);
1479  }
1480 
1481  } else {
1482  SSL_CTX_set_verify(global_ssl_context,
1483  SSL_VERIFY_NONE,
1484  NULL);
1485  }
1486 }
1487 
1488 void conn_config_ssl (CfgGroup *grp)
1489 {
1491  Octstr *ssl_server_cert_file = NULL;
1492  Octstr *ssl_server_key_file = NULL;
1493  Octstr *ssl_trusted_ca_file = NULL;
1494  Octstr *ssl_client_cipher_list = NULL;
1495  Octstr *ssl_server_cipher_list = NULL;
1496 
1497  /*
1498  * check if SSL is desired for HTTP servers and then
1499  * load SSL client and SSL server public certificates
1500  * and private keys
1501  */
1502  ssl_client_certkey_file = cfg_get(grp, octstr_imm("ssl-client-certkey-file"));
1503  if (ssl_client_certkey_file != NULL)
1504  conn_use_global_client_certkey_file(ssl_client_certkey_file);
1505 
1506  ssl_server_cert_file = cfg_get(grp, octstr_imm("ssl-server-cert-file"));
1507  ssl_server_key_file = cfg_get(grp, octstr_imm("ssl-server-key-file"));
1508 
1509  if (ssl_server_cert_file != NULL && ssl_server_key_file != NULL) {
1510  conn_use_global_server_certkey_file(ssl_server_cert_file,
1511  ssl_server_key_file);
1512  }
1513 
1514  ssl_trusted_ca_file = cfg_get(grp, octstr_imm("ssl-trusted-ca-file"));
1515 
1516  conn_use_global_trusted_ca_file(ssl_trusted_ca_file);
1517 
1518  /*
1519  * Check if specific ciphers are selected/de-selected.
1520  */
1521  if ((ssl_client_cipher_list = cfg_get(grp, octstr_imm("ssl-client-cipher-list"))) != NULL) {
1522  conn_use_global_client_cipher_list(ssl_client_cipher_list);
1523  }
1524  if ((ssl_server_cipher_list = cfg_get(grp, octstr_imm("ssl-server-cipher-list"))) != NULL) {
1525  conn_use_global_server_cipher_list(ssl_server_cipher_list);
1526  }
1527 
1529  octstr_destroy(ssl_server_cert_file);
1530  octstr_destroy(ssl_server_key_file);
1531  octstr_destroy(ssl_trusted_ca_file);
1532  octstr_destroy(ssl_client_cipher_list);
1533  octstr_destroy(ssl_server_cipher_list);
1534 }
1535 
1536 SSL *conn_get_ssl(Connection *conn)
1537 {
1538  if (conn != NULL)
1539  return conn->ssl;
1540  else
1541  return NULL;
1542 }
1543 
1544 #else
1545 
1547 {
1548  info(0, "SSL not supported, no SSL initialization done.");
1549 }
1550 #endif /* HAVE_LIBSSL */
1551 
1553  if(conn == NULL)
1554  return 0;
1555  else
1556  return conn->fd;
1557 }
Octstr * conn_read_line(Connection *conn)
Definition: conn.c:1134
int listening_pollout
Definition: conn.c:148
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
static long our_port
Definition: radius_acct.c:87
int socket_set_blocking(int fd, int blocking)
Definition: socket.c:368
Definition: http.c:2014
int size
Definition: wsasm.c:84
Connection * conn_open_tcp(Octstr *host, int port, Octstr *our_host)
Definition: conn.c:496
long gwthread_self(void)
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1497
static int unlocked_try_write(Connection *conn)
Definition: conn.c:289
void fdset_listen(FDSet *set, int fd, int mask, int events)
Definition: fdset.c:470
conn_callback_t * callback
Definition: conn.c:142
gw_assert(wtls_machine->packet_to_send !=NULL)
int ssl
Mutex * outlock
Definition: conn.c:112
static void startmark(unsigned char *p, long number)
Definition: gwmem-check.c:263
#define mutex_unlock(m)
Definition: thread.h:136
void encode_network_long(unsigned char *data, unsigned long value)
Definition: utils.c:940
FDSet * registered
Definition: conn.c:141
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
unsigned int output_buffering
Definition: conn.c:130
int read_eof
Definition: conn.c:136
int conn_is_connected(Connection *conn)
Definition: conn.c:525
static void unlocked_register_pollin(Connection *conn, int onoff)
Definition: conn.c:377
#define mutex_create()
Definition: thread.h:96
static void unlocked_register_pollout(Connection *conn, int onoff)
Definition: conn.c:397
Connection * conn_open_tcp_nb(Octstr *host, int port, Octstr *our_host)
Definition: conn.c:501
int listening_pollin
Definition: conn.c:146
static void endmark(unsigned char *p, size_t size)
Definition: gwmem-check.c:255
static Octstr * host
Definition: fakesmsc.c:122
#define cfg_get(grp, varname)
Definition: cfg.h:86
static long unlocked_outbuf_len(Connection *conn)
Definition: conn.c:222
int conn_write_data(Connection *conn, unsigned char *data, long length)
Definition: conn.c:1063
#define POLLNVAL
Definition: gwpoll.h:98
long outbufpos
Definition: conn.c:126
void * callback_data
Definition: conn.c:143
long conn_inbuf_len(Connection *conn)
Definition: conn.c:694
Octstr * outbuf
Definition: conn.c:125
void fdset_unregister(FDSet *set, int fd)
Definition: fdset.c:510
int io_error
Definition: conn.c:137
long claiming_thread
Definition: conn.c:115
int conn_eof(Connection *conn)
Definition: conn.c:705
static void lock_out(Connection *conn)
Definition: conn.c:197
volatile sig_atomic_t claimed
Definition: conn.c:113
conn_callback_data_destroyer_t * callback_data_destroyer
Definition: conn.c:144
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1012
void fdset_register(FDSet *set, int fd, int events, fdset_callback_t callback, void *data)
Definition: fdset.c:425
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:361
#define DEFAULT_OUTPUT_BUFFERING
Definition: conn.c:103
FILE * file
Definition: log.c:169
static Octstr * our_host
Definition: radius_acct.c:86
Connection * conn_open_tcp_nb_with_port(Octstr *host, int port, int our_port, Octstr *our_host)
Definition: conn.c:506
void conn_callback_data_destroyer_t(void *data)
Definition: conn.h:113
static Octstr * ssl_client_certkey_file
Definition: test_http.c:83
void conn_claim(Connection *conn)
Definition: conn.c:671
#define POLLIN
Definition: gwpoll.h:91
void conn_set_output_buffering(Connection *conn, unsigned int size)
Definition: conn.c:729
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
int conn_write(Connection *conn, Octstr *data)
Definition: conn.c:1051
int conn_get_connect_result(Connection *conn)
Definition: conn.c:530
Octstr * conn_read_packet(Connection *conn, int startmark, int endmark)
Definition: conn.c:1214
int conn_get_id(Connection *conn)
Definition: conn.c:1552
void conn_config_ssl(CfgGroup *grp)
Definition: conn.c:1546
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
static void unlocked_read(Connection *conn)
Definition: conn.c:310
void conn_destroy(Connection *conn)
Definition: conn.c:627
#define unlock_in(conn)
Definition: conn.c:168
int conn_register_real(Connection *conn, FDSet *fdset, conn_callback_t callback, void *data, conn_callback_data_destroyer_t *data_destroyer)
Definition: conn.c:802
int fd
Definition: conn.c:119
Connection * conn_open_tcp_with_port(Octstr *host, int port, int our_port, Octstr *our_host)
Definition: conn.c:548
unsigned long(* CRYPTO_CALLBACK_PTR)(void)
Definition: conn.c:94
void warning(int err, const char *fmt,...)
Definition: log.c:660
long conn_outbuf_len(Connection *conn)
Definition: conn.c:683
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
Definition: socket.c:156
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define POLLERR
Definition: gwpoll.h:96
void conn_callback_t(Connection *conn, void *data)
Definition: conn.h:105
#define octstr_create(cstr)
Definition: octstr.h:125
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
Octstr * conn_read_withlen(Connection *conn)
Definition: conn.c:1169
int conn_write_withlen(Connection *conn, Octstr *data)
Definition: conn.c:1075
Mutex * inlock
Definition: conn.c:111
long octstr_write_data(Octstr *ostr, int fd, long from)
Definition: octstr.c:1255
int gwthread_pollfd(int fd, int events, double timeout)
int sockfd
Definition: test_cimd2.c:145
static void poll_callback(int fd, int revents, void *data)
Definition: conn.c:738
static void unlock_in_real(Connection *conn, char *file, int line, const char *func)
Definition: conn.c:183
int tcpip_connect_nb_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr, int *done)
Definition: socket.c:246
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
void conn_unregister(Connection *conn)
Definition: conn.c:858
long decode_network_long(unsigned char *data)
Definition: utils.c:935
enum Connection::@56 connected
static long unlocked_inbuf_len(Connection *conn)
Definition: conn.c:228
Definition: octstr.c:118
int conn_wait(Connection *conn, double seconds)
Definition: conn.c:904
static void unlock_out_real(Connection *conn, char *file, int line, const char *func)
Definition: conn.c:208
#define panic
Definition: log.h:87
Definition: cfg.c:73
static void lock_in(Connection *conn)
Definition: conn.c:172
static long unlocked_write(Connection *conn)
Definition: conn.c:235
int socklen_t
Definition: socket.h:73
Definition: fdset.c:70
Definition: thread.h:76
Octstr * conn_read_fixed(Connection *conn, long length)
Definition: conn.c:1110
int conn_error(Connection *conn)
Definition: conn.c:716
void octstr_get_many_chars(char *buf, Octstr *ostr, long pos, long len)
Definition: octstr.c:425
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406
#define mutex_lock(m)
Definition: thread.h:130
#define unlock_out(conn)
Definition: conn.c:169
Octstr * inbuf
Definition: conn.c:133
#define POLLHUP
Definition: gwpoll.h:97
Octstr * conn_read_everything(Connection *conn)
Definition: conn.c:1090
#define POLLOUT
Definition: gwpoll.h:93
Connection * conn_wrap_fd(int fd, int ssl)
Definition: conn.c:566
long inbufpos
Definition: conn.c:134
int conn_flush(Connection *conn)
Definition: conn.c:995
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.