security: Inhibit execution of privilege escalating functions
[asterisk/asterisk.git] / main / tcptls.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2008, Digium, Inc.
5  *
6  * Luigi Rizzo (TCP and TLS server code)
7  * Brett Bryant <brettbryant@gmail.com> (updated for client support)
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*!
21  * \file
22  * \brief Code to support TCP and TLS server/client
23  *
24  * \author Luigi Rizzo
25  * \author Brett Bryant <brettbryant@gmail.com>
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39
40 #include <signal.h>
41 #include <sys/signal.h>
42
43 #include "asterisk/compat.h"
44 #include "asterisk/tcptls.h"
45 #include "asterisk/http.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/strings.h"
48 #include "asterisk/options.h"
49 #include "asterisk/manager.h"
50 #include "asterisk/astobj2.h"
51 #include "asterisk/pbx.h"
52
53 /*! \brief
54  * replacement read/write functions for SSL support.
55  * We use wrappers rather than SSL_read/SSL_write directly so
56  * we can put in some debugging.
57  */
58
59 #ifdef DO_SSL
60 static HOOK_T ssl_read(void *cookie, char *buf, LEN_T len)
61 {
62         int i = SSL_read(cookie, buf, len-1);
63 #if 0
64         if (i >= 0) {
65                 buf[i] = '\0';
66         }
67         ast_verb(0, "ssl read size %d returns %d <%s>\n", (int)len, i, buf);
68 #endif
69         return i;
70 }
71
72 static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
73 {
74 #if 0
75         char *s = ast_alloca(len+1);
76
77         strncpy(s, buf, len);
78         s[len] = '\0';
79         ast_verb(0, "ssl write size %d <%s>\n", (int)len, s);
80 #endif
81         return SSL_write(cookie, buf, len);
82 }
83
84 static int ssl_close(void *cookie)
85 {
86         int cookie_fd = SSL_get_fd(cookie);
87         int ret;
88
89         if (cookie_fd > -1) {
90                 /*
91                  * According to the TLS standard, it is acceptable for an application to only send its shutdown
92                  * alert and then close the underlying connection without waiting for the peer's response (this
93                  * way resources can be saved, as the process can already terminate or serve another connection).
94                  */
95                 if ((ret = SSL_shutdown(cookie)) < 0) {
96                         ast_log(LOG_ERROR, "SSL_shutdown() failed: %d\n", SSL_get_error(cookie, ret));
97                 }
98
99                 if (!((SSL*)cookie)->server) {
100                         /* For client threads, ensure that the error stack is cleared */
101                         ERR_remove_state(0);
102                 }
103
104                 SSL_free(cookie);
105                 /* adding shutdown(2) here has no added benefit */
106                 if (close(cookie_fd)) {
107                         ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
108                 }
109         }
110         return 0;
111 }
112 #endif  /* DO_SSL */
113
114 HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *tcptls_session, void *buf, size_t count)
115 {
116         if (tcptls_session->fd == -1) {
117                 ast_log(LOG_ERROR, "server_read called with an fd of -1\n");
118                 errno = EIO;
119                 return -1;
120         }
121
122 #ifdef DO_SSL
123         if (tcptls_session->ssl) {
124                 return ssl_read(tcptls_session->ssl, buf, count);
125         }
126 #endif
127         return read(tcptls_session->fd, buf, count);
128 }
129
130 HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *tcptls_session, const void *buf, size_t count)
131 {
132         if (tcptls_session->fd == -1) {
133                 ast_log(LOG_ERROR, "server_write called with an fd of -1\n");
134                 errno = EIO;
135                 return -1;
136         }
137
138 #ifdef DO_SSL
139         if (tcptls_session->ssl) {
140                 return ssl_write(tcptls_session->ssl, buf, count);
141         }
142 #endif
143         return write(tcptls_session->fd, buf, count);
144 }
145
146 static void session_instance_destructor(void *obj)
147 {
148         struct ast_tcptls_session_instance *i = obj;
149         ast_free(i->overflow_buf);
150 }
151
152 /*! \brief
153 * creates a FILE * from the fd passed by the accept thread.
154 * This operation is potentially expensive (certificate verification),
155 * so we do it in the child thread context.
156 *
157 * \note must decrement ref count before returning NULL on error
158 */
159 static void *handle_tcptls_connection(void *data)
160 {
161         struct ast_tcptls_session_instance *tcptls_session = data;
162 #ifdef DO_SSL
163         int (*ssl_setup)(SSL *) = (tcptls_session->client) ? SSL_connect : SSL_accept;
164         int ret;
165         char err[256];
166 #endif
167
168         /* TCP/TLS connections are associated with external protocols, and
169          * should not be allowed to execute 'dangerous' functions. This may
170          * need to be pushed down into the individual protocol handlers, but
171          * this seems like a good general policy.
172          */
173         if (ast_thread_inhibit_escalations()) {
174                 ast_log(LOG_ERROR, "Failed to inhibit privilege escalations; killing connection\n");
175                 return NULL;
176         }
177
178         /*
179         * open a FILE * as appropriate.
180         */
181         if (!tcptls_session->parent->tls_cfg) {
182                 if ((tcptls_session->f = fdopen(tcptls_session->fd, "w+"))) {
183                         if(setvbuf(tcptls_session->f, NULL, _IONBF, 0)) {
184                                 ast_tcptls_close_session_file(tcptls_session);
185                         }
186                 }
187         }
188 #ifdef DO_SSL
189         else if ( (tcptls_session->ssl = SSL_new(tcptls_session->parent->tls_cfg->ssl_ctx)) ) {
190                 SSL_set_fd(tcptls_session->ssl, tcptls_session->fd);
191                 if ((ret = ssl_setup(tcptls_session->ssl)) <= 0) {
192                         ast_verb(2, "Problem setting up ssl connection: %s\n", ERR_error_string(ERR_get_error(), err));
193                 } else {
194 #if defined(HAVE_FUNOPEN)       /* the BSD interface */
195                         tcptls_session->f = funopen(tcptls_session->ssl, ssl_read, ssl_write, NULL, ssl_close);
196
197 #elif defined(HAVE_FOPENCOOKIE) /* the glibc/linux interface */
198                         static const cookie_io_functions_t cookie_funcs = {
199                                 ssl_read, ssl_write, NULL, ssl_close
200                         };
201                         tcptls_session->f = fopencookie(tcptls_session->ssl, "w+", cookie_funcs);
202 #else
203                         /* could add other methods here */
204                         ast_debug(2, "no tcptls_session->f methods attempted!\n");
205 #endif
206                         if ((tcptls_session->client && !ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_DONT_VERIFY_SERVER))
207                                 || (!tcptls_session->client && ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_VERIFY_CLIENT))) {
208                                 X509 *peer;
209                                 long res;
210                                 peer = SSL_get_peer_certificate(tcptls_session->ssl);
211                                 if (!peer) {
212                                         ast_log(LOG_ERROR, "No peer SSL certificate to verify\n");
213                                         ast_tcptls_close_session_file(tcptls_session);
214                                         ao2_ref(tcptls_session, -1);
215                                         return NULL;
216                                 }
217
218                                 res = SSL_get_verify_result(tcptls_session->ssl);
219                                 if (res != X509_V_OK) {
220                                         ast_log(LOG_ERROR, "Certificate did not verify: %s\n", X509_verify_cert_error_string(res));
221                                         X509_free(peer);
222                                         ast_tcptls_close_session_file(tcptls_session);
223                                         ao2_ref(tcptls_session, -1);
224                                         return NULL;
225                                 }
226                                 if (!ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_IGNORE_COMMON_NAME)) {
227                                         ASN1_STRING *str;
228                                         unsigned char *str2;
229                                         X509_NAME *name = X509_get_subject_name(peer);
230                                         int pos = -1;
231                                         int found = 0;
232
233                                         for (;;) {
234                                                 /* Walk the certificate to check all available "Common Name" */
235                                                 /* XXX Probably should do a gethostbyname on the hostname and compare that as well */
236                                                 pos = X509_NAME_get_index_by_NID(name, NID_commonName, pos);
237                                                 if (pos < 0) {
238                                                         break;
239                                                 }
240                                                 str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, pos));
241                                                 ASN1_STRING_to_UTF8(&str2, str);
242                                                 if (str2) {
243                                                         if (!strcasecmp(tcptls_session->parent->hostname, (char *) str2)) {
244                                                                 found = 1;
245                                                         }
246                                                         ast_debug(3, "SSL Common Name compare s1='%s' s2='%s'\n", tcptls_session->parent->hostname, str2);
247                                                         OPENSSL_free(str2);
248                                                 }
249                                                 if (found) {
250                                                         break;
251                                                 }
252                                         }
253                                         if (!found) {
254                                                 ast_log(LOG_ERROR, "Certificate common name did not match (%s)\n", tcptls_session->parent->hostname);
255                                                 X509_free(peer);
256                                                 ast_tcptls_close_session_file(tcptls_session);
257                                                 ao2_ref(tcptls_session, -1);
258                                                 return NULL;
259                                         }
260                                 }
261                                 X509_free(peer);
262                         }
263                 }
264                 if (!tcptls_session->f) {       /* no success opening descriptor stacking */
265                         SSL_free(tcptls_session->ssl);
266                 }
267         }
268 #endif /* DO_SSL */
269
270         if (!tcptls_session->f) {
271                 ast_tcptls_close_session_file(tcptls_session);
272                 ast_log(LOG_WARNING, "FILE * open failed!\n");
273 #ifndef DO_SSL
274                 if (tcptls_session->parent->tls_cfg) {
275                         ast_log(LOG_WARNING, "Attempted a TLS connection without OpenSSL support. This will not work!\n");
276                 }
277 #endif
278                 ao2_ref(tcptls_session, -1);
279                 return NULL;
280         }
281
282         if (tcptls_session->parent->worker_fn) {
283                 return tcptls_session->parent->worker_fn(tcptls_session);
284         } else {
285                 return tcptls_session;
286         }
287 }
288
289 void *ast_tcptls_server_root(void *data)
290 {
291         struct ast_tcptls_session_args *desc = data;
292         int fd;
293         struct ast_sockaddr addr;
294         struct ast_tcptls_session_instance *tcptls_session;
295         pthread_t launched;
296
297         for (;;) {
298                 int i, flags;
299
300                 if (desc->periodic_fn) {
301                         desc->periodic_fn(desc);
302                 }
303                 i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
304                 if (i <= 0) {
305                         continue;
306                 }
307                 fd = ast_accept(desc->accept_fd, &addr);
308                 if (fd < 0) {
309                         if ((errno != EAGAIN) && (errno != EINTR)) {
310                                 ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
311                         }
312                         continue;
313                 }
314                 tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
315                 if (!tcptls_session) {
316                         ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
317                         if (close(fd)) {
318                                 ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
319                         }
320                         continue;
321                 }
322
323                 tcptls_session->overflow_buf = ast_str_create(128);
324                 flags = fcntl(fd, F_GETFL);
325                 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
326                 tcptls_session->fd = fd;
327                 tcptls_session->parent = desc;
328                 ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
329
330                 tcptls_session->client = 0;
331
332                 /* This thread is now the only place that controls the single ref to tcptls_session */
333                 if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
334                         ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
335                         ast_tcptls_close_session_file(tcptls_session);
336                         ao2_ref(tcptls_session, -1);
337                 }
338         }
339         return NULL;
340 }
341
342 static int __ssl_setup(struct ast_tls_config *cfg, int client)
343 {
344 #ifndef DO_SSL
345         cfg->enabled = 0;
346         return 0;
347 #else
348         if (!cfg->enabled) {
349                 return 0;
350         }
351
352         /* Get rid of an old SSL_CTX since we're about to
353          * allocate a new one
354          */
355         if (cfg->ssl_ctx) {
356                 SSL_CTX_free(cfg->ssl_ctx);
357                 cfg->ssl_ctx = NULL;
358         }
359
360         if (client) {
361 #ifndef OPENSSL_NO_SSL2
362                 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV2_CLIENT)) {
363                         cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
364                 } else
365 #endif
366                 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
367                         cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
368                 } else if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
369                         cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
370                 } else {
371                         /* SSLv23_client_method() sends SSLv2, this was the original
372                          * default for ssl clients before the option was given to
373                          * pick what protocol a client should use.  In order not
374                          * to break expected behavior it remains the default. */
375                         cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
376                 }
377         } else {
378                 /* SSLv23_server_method() supports TLSv1, SSLv2, and SSLv3 inbound connections. */
379                 cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
380         }
381
382         if (!cfg->ssl_ctx) {
383                 ast_debug(1, "Sorry, SSL_CTX_new call returned null...\n");
384                 cfg->enabled = 0;
385                 return 0;
386         }
387
388         SSL_CTX_set_verify(cfg->ssl_ctx,
389                 ast_test_flag(&cfg->flags, AST_SSL_VERIFY_CLIENT) ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE,
390                 NULL);
391
392         if (!ast_strlen_zero(cfg->certfile)) {
393                 char *tmpprivate = ast_strlen_zero(cfg->pvtfile) ? cfg->certfile : cfg->pvtfile;
394                 if (SSL_CTX_use_certificate_file(cfg->ssl_ctx, cfg->certfile, SSL_FILETYPE_PEM) == 0) {
395                         if (!client) {
396                                 /* Clients don't need a certificate, but if its setup we can use it */
397                                 ast_verb(0, "SSL error loading cert file. <%s>\n", cfg->certfile);
398                                 cfg->enabled = 0;
399                                 SSL_CTX_free(cfg->ssl_ctx);
400                                 cfg->ssl_ctx = NULL;
401                                 return 0;
402                         }
403                 }
404                 if ((SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0 )) {
405                         if (!client) {
406                                 /* Clients don't need a private key, but if its setup we can use it */
407                                 ast_verb(0, "SSL error loading private key file. <%s>\n", tmpprivate);
408                                 cfg->enabled = 0;
409                                 SSL_CTX_free(cfg->ssl_ctx);
410                                 cfg->ssl_ctx = NULL;
411                                 return 0;
412                         }
413                 }
414         }
415         if (!ast_strlen_zero(cfg->cipher)) {
416                 if (SSL_CTX_set_cipher_list(cfg->ssl_ctx, cfg->cipher) == 0 ) {
417                         if (!client) {
418                                 ast_verb(0, "SSL cipher error <%s>\n", cfg->cipher);
419                                 cfg->enabled = 0;
420                                 SSL_CTX_free(cfg->ssl_ctx);
421                                 cfg->ssl_ctx = NULL;
422                                 return 0;
423                         }
424                 }
425         }
426         if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
427                 if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0) {
428                         ast_verb(0, "SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
429                 }
430         }
431
432         ast_verb(0, "SSL certificate ok\n");
433         return 1;
434 #endif
435 }
436
437 int ast_ssl_setup(struct ast_tls_config *cfg)
438 {
439         return __ssl_setup(cfg, 0);
440 }
441
442 void ast_ssl_teardown(struct ast_tls_config *cfg)
443 {
444 #ifdef DO_SSL
445         if (cfg->ssl_ctx) {
446                 SSL_CTX_free(cfg->ssl_ctx);
447                 cfg->ssl_ctx = NULL;
448         }
449 #endif
450 }
451
452 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
453 {
454         struct ast_tcptls_session_args *desc;
455         int flags;
456
457         if (!(desc = tcptls_session->parent)) {
458                 goto client_start_error;
459         }
460
461         if (ast_connect(desc->accept_fd, &desc->remote_address)) {
462                 ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",
463                         desc->name,
464                         ast_sockaddr_stringify(&desc->remote_address),
465                         strerror(errno));
466                 goto client_start_error;
467         }
468
469         flags = fcntl(desc->accept_fd, F_GETFL);
470         fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
471
472         if (desc->tls_cfg) {
473                 desc->tls_cfg->enabled = 1;
474                 __ssl_setup(desc->tls_cfg, 1);
475         }
476
477         return handle_tcptls_connection(tcptls_session);
478
479 client_start_error:
480         if (desc) {
481                 close(desc->accept_fd);
482                 desc->accept_fd = -1;
483         }
484         ao2_ref(tcptls_session, -1);
485         return NULL;
486
487 }
488
489 struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
490 {
491         int x = 1;
492         struct ast_tcptls_session_instance *tcptls_session = NULL;
493
494         /* Do nothing if nothing has changed */
495         if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
496                 ast_debug(1, "Nothing changed in %s\n", desc->name);
497                 return NULL;
498         }
499
500         /* If we return early, there is no connection */
501         ast_sockaddr_setnull(&desc->old_address);
502
503         if (desc->accept_fd != -1) {
504                 close(desc->accept_fd);
505         }
506
507         desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->remote_address) ?
508                                  AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
509         if (desc->accept_fd < 0) {
510                 ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
511                         desc->name, strerror(errno));
512                 return NULL;
513         }
514
515         /* if a local address was specified, bind to it so the connection will
516            originate from the desired address */
517         if (!ast_sockaddr_isnull(&desc->local_address)) {
518                 setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
519                 if (ast_bind(desc->accept_fd, &desc->local_address)) {
520                         ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
521                                 desc->name,
522                                 ast_sockaddr_stringify(&desc->local_address),
523                                 strerror(errno));
524                         goto error;
525                 }
526         }
527
528         if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor))) {
529                 goto error;
530         }
531
532         tcptls_session->overflow_buf = ast_str_create(128);
533         tcptls_session->client = 1;
534         tcptls_session->fd = desc->accept_fd;
535         tcptls_session->parent = desc;
536         tcptls_session->parent->worker_fn = NULL;
537         ast_sockaddr_copy(&tcptls_session->remote_address,
538                           &desc->remote_address);
539
540         /* Set current info */
541         ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
542         return tcptls_session;
543
544 error:
545         close(desc->accept_fd);
546         desc->accept_fd = -1;
547         if (tcptls_session) {
548                 ao2_ref(tcptls_session, -1);
549         }
550         return NULL;
551 }
552
553 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
554 {
555         int flags;
556         int x = 1;
557
558         /* Do nothing if nothing has changed */
559         if (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
560                 ast_debug(1, "Nothing changed in %s\n", desc->name);
561                 return;
562         }
563
564         /* If we return early, there is no one listening */
565         ast_sockaddr_setnull(&desc->old_address);
566
567         /* Shutdown a running server if there is one */
568         if (desc->master != AST_PTHREADT_NULL) {
569                 pthread_cancel(desc->master);
570                 pthread_kill(desc->master, SIGURG);
571                 pthread_join(desc->master, NULL);
572         }
573
574         if (desc->accept_fd != -1) {
575                 close(desc->accept_fd);
576         }
577
578         /* If there's no new server, stop here */
579         if (ast_sockaddr_isnull(&desc->local_address)) {
580                 ast_debug(2, "Server disabled:  %s\n", desc->name);
581                 return;
582         }
583
584         desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->local_address) ?
585                                  AF_INET6 : AF_INET, SOCK_STREAM, 0);
586         if (desc->accept_fd < 0) {
587                 ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
588                 return;
589         }
590
591         setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
592         if (ast_bind(desc->accept_fd, &desc->local_address)) {
593                 ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
594                         desc->name,
595                         ast_sockaddr_stringify(&desc->local_address),
596                         strerror(errno));
597                 goto error;
598         }
599         if (listen(desc->accept_fd, 10)) {
600                 ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
601                 goto error;
602         }
603         flags = fcntl(desc->accept_fd, F_GETFL);
604         fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
605         if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
606                 ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
607                         desc->name,
608                         ast_sockaddr_stringify(&desc->local_address),
609                         strerror(errno));
610                 goto error;
611         }
612
613         /* Set current info */
614         ast_sockaddr_copy(&desc->old_address, &desc->local_address);
615
616         return;
617
618 error:
619         close(desc->accept_fd);
620         desc->accept_fd = -1;
621 }
622
623 void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
624 {
625         if (tcptls_session->f) {
626                 if (fclose(tcptls_session->f)) {
627                         ast_log(LOG_ERROR, "fclose() failed: %s\n", strerror(errno));
628                 }
629                 tcptls_session->f = NULL;
630                 tcptls_session->fd = -1;
631         } else if (tcptls_session->fd != -1) {
632                 if (close(tcptls_session->fd)) {
633                         ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
634                 }
635                 tcptls_session->fd = -1;
636         } else {
637                 ast_log(LOG_ERROR, "ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
638         }
639 }
640
641 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
642 {
643         if (desc->master != AST_PTHREADT_NULL) {
644                 pthread_cancel(desc->master);
645                 pthread_kill(desc->master, SIGURG);
646                 pthread_join(desc->master, NULL);
647                 desc->master = AST_PTHREADT_NULL;
648         }
649         if (desc->accept_fd != -1) {
650                 close(desc->accept_fd);
651         }
652         desc->accept_fd = -1;
653         ast_debug(2, "Stopped server :: %s\n", desc->name);
654 }
655
656 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
657 {
658         if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
659                 tls_cfg->enabled = ast_true(value) ? 1 : 0;
660         } else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
661                 ast_free(tls_cfg->certfile);
662                 tls_cfg->certfile = ast_strdup(value);
663         } else if (!strcasecmp(varname, "tlsprivatekey") || !strcasecmp(varname, "sslprivatekey")) {
664                 ast_free(tls_cfg->pvtfile);
665                 tls_cfg->pvtfile = ast_strdup(value);
666         } else if (!strcasecmp(varname, "tlscipher") || !strcasecmp(varname, "sslcipher")) {
667                 ast_free(tls_cfg->cipher);
668                 tls_cfg->cipher = ast_strdup(value);
669         } else if (!strcasecmp(varname, "tlscafile")) {
670                 ast_free(tls_cfg->cafile);
671                 tls_cfg->cafile = ast_strdup(value);
672         } else if (!strcasecmp(varname, "tlscapath") || !strcasecmp(varname, "tlscadir")) {
673                 ast_free(tls_cfg->capath);
674                 tls_cfg->capath = ast_strdup(value);
675         } else if (!strcasecmp(varname, "tlsverifyclient")) {
676                 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
677         } else if (!strcasecmp(varname, "tlsdontverifyserver")) {
678                 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
679         } else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
680                 if (ast_parse_arg(value, PARSE_ADDR, &tls_desc->local_address))
681                         ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
682         } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
683                 if (!strcasecmp(value, "tlsv1")) {
684                         ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
685                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
686                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
687                 } else if (!strcasecmp(value, "sslv3")) {
688                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
689                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
690                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
691                 } else if (!strcasecmp(value, "sslv2")) {
692                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
693                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
694                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
695                 }
696         } else {
697                 return -1;
698         }
699
700         return 0;
701 }