Sun Oct 16 2011 08:41:48

Asterisk developer's documentation


tcptls.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007 - 2008, Digium, Inc.
00005  *
00006  * Luigi Rizzo (TCP and TLS server code)
00007  * Brett Bryant <brettbryant@gmail.com> (updated for client support)
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*!
00021  * \file
00022  * \brief Code to support TCP and TLS server/client
00023  *
00024  * \author Luigi Rizzo
00025  * \author Brett Bryant <brettbryant@gmail.com>
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 320568 $")
00031 
00032 #ifdef HAVE_FCNTL_H
00033 #include <fcntl.h>
00034 #endif
00035 
00036 #include <signal.h>
00037 #include <sys/signal.h>
00038 
00039 #include "asterisk/compat.h"
00040 #include "asterisk/tcptls.h"
00041 #include "asterisk/http.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/strings.h"
00044 #include "asterisk/options.h"
00045 #include "asterisk/manager.h"
00046 #include "asterisk/astobj2.h"
00047 
00048 /*! \brief
00049  * replacement read/write functions for SSL support.
00050  * We use wrappers rather than SSL_read/SSL_write directly so
00051  * we can put in some debugging.
00052  */
00053 
00054 #ifdef DO_SSL
00055 static HOOK_T ssl_read(void *cookie, char *buf, LEN_T len)
00056 {
00057    int i = SSL_read(cookie, buf, len-1);
00058 #if 0
00059    if (i >= 0)
00060       buf[i] = '\0';
00061    ast_verb(0, "ssl read size %d returns %d <%s>\n", (int)len, i, buf);
00062 #endif
00063    return i;
00064 }
00065 
00066 static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
00067 {
00068 #if 0
00069    char *s = alloca(len+1);
00070    strncpy(s, buf, len);
00071    s[len] = '\0';
00072    ast_verb(0, "ssl write size %d <%s>\n", (int)len, s);
00073 #endif
00074    return SSL_write(cookie, buf, len);
00075 }
00076 
00077 static int ssl_close(void *cookie)
00078 {
00079    close(SSL_get_fd(cookie));
00080    SSL_shutdown(cookie);
00081    SSL_free(cookie);
00082    return 0;
00083 }
00084 #endif   /* DO_SSL */
00085 
00086 HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *tcptls_session, void *buf, size_t count)
00087 {
00088    if (tcptls_session->fd == -1) {
00089       ast_log(LOG_ERROR, "server_read called with an fd of -1\n");
00090       errno = EIO;
00091       return -1;
00092    }
00093 
00094 #ifdef DO_SSL
00095    if (tcptls_session->ssl)
00096       return ssl_read(tcptls_session->ssl, buf, count);
00097 #endif
00098    return read(tcptls_session->fd, buf, count);
00099 }
00100 
00101 HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *tcptls_session, const void *buf, size_t count)
00102 {
00103    if (tcptls_session->fd == -1) {
00104       ast_log(LOG_ERROR, "server_write called with an fd of -1\n");
00105       errno = EIO;
00106       return -1;
00107    }
00108 
00109 #ifdef DO_SSL
00110    if (tcptls_session->ssl)
00111       return ssl_write(tcptls_session->ssl, buf, count);
00112 #endif
00113    return write(tcptls_session->fd, buf, count);
00114 }
00115 
00116 static void session_instance_destructor(void *obj)
00117 {
00118    struct ast_tcptls_session_instance *i = obj;
00119    ast_mutex_destroy(&i->lock);
00120 }
00121 
00122 /*! \brief
00123 * creates a FILE * from the fd passed by the accept thread.
00124 * This operation is potentially expensive (certificate verification),
00125 * so we do it in the child thread context.
00126 *
00127 * \note must decrement ref count before returning NULL on error
00128 */
00129 static void *handle_tcptls_connection(void *data)
00130 {
00131    struct ast_tcptls_session_instance *tcptls_session = data;
00132 #ifdef DO_SSL
00133    int (*ssl_setup)(SSL *) = (tcptls_session->client) ? SSL_connect : SSL_accept;
00134    int ret;
00135    char err[256];
00136 #endif
00137 
00138    /*
00139    * open a FILE * as appropriate.
00140    */
00141    if (!tcptls_session->parent->tls_cfg) {
00142       if ((tcptls_session->f = fdopen(tcptls_session->fd, "w+"))) {
00143          if(setvbuf(tcptls_session->f, NULL, _IONBF, 0)) {
00144             fclose(tcptls_session->f);
00145             tcptls_session->f = NULL;
00146          }
00147       }
00148    }
00149 #ifdef DO_SSL
00150    else if ( (tcptls_session->ssl = SSL_new(tcptls_session->parent->tls_cfg->ssl_ctx)) ) {
00151       SSL_set_fd(tcptls_session->ssl, tcptls_session->fd);
00152       if ((ret = ssl_setup(tcptls_session->ssl)) <= 0) {
00153          ast_verb(2, "Problem setting up ssl connection: %s\n", ERR_error_string(ERR_get_error(), err));
00154       } else {
00155 #if defined(HAVE_FUNOPEN)  /* the BSD interface */
00156          tcptls_session->f = funopen(tcptls_session->ssl, ssl_read, ssl_write, NULL, ssl_close);
00157 
00158 #elif defined(HAVE_FOPENCOOKIE)  /* the glibc/linux interface */
00159          static const cookie_io_functions_t cookie_funcs = {
00160             ssl_read, ssl_write, NULL, ssl_close
00161          };
00162          tcptls_session->f = fopencookie(tcptls_session->ssl, "w+", cookie_funcs);
00163 #else
00164          /* could add other methods here */
00165          ast_debug(2, "no tcptls_session->f methods attempted!");
00166 #endif
00167          if ((tcptls_session->client && !ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_DONT_VERIFY_SERVER))
00168             || (!tcptls_session->client && ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_VERIFY_CLIENT))) {
00169             X509 *peer;
00170             long res;
00171             peer = SSL_get_peer_certificate(tcptls_session->ssl);
00172             if (!peer)
00173                ast_log(LOG_WARNING, "No peer SSL certificate\n");
00174             res = SSL_get_verify_result(tcptls_session->ssl);
00175             if (res != X509_V_OK)
00176                ast_log(LOG_ERROR, "Certificate did not verify: %s\n", X509_verify_cert_error_string(res));
00177             if (!ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_IGNORE_COMMON_NAME)) {
00178                ASN1_STRING *str;
00179                unsigned char *str2;
00180                X509_NAME *name = X509_get_subject_name(peer);
00181                int pos = -1;
00182                int found = 0;
00183 
00184                for (;;) {
00185                   /* Walk the certificate to check all available "Common Name" */
00186                   /* XXX Probably should do a gethostbyname on the hostname and compare that as well */
00187                   pos = X509_NAME_get_index_by_NID(name, NID_commonName, pos);
00188                   if (pos < 0)
00189                      break;
00190                   str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, pos));
00191                   ASN1_STRING_to_UTF8(&str2, str);
00192                   if (str2) {
00193                      if (!strcasecmp(tcptls_session->parent->hostname, (char *) str2))
00194                         found = 1;
00195                      ast_debug(3, "SSL Common Name compare s1='%s' s2='%s'\n", tcptls_session->parent->hostname, str2);
00196                      OPENSSL_free(str2);
00197                   }
00198                   if (found)
00199                      break;
00200                }
00201                if (!found) {
00202                   ast_log(LOG_ERROR, "Certificate common name did not match (%s)\n", tcptls_session->parent->hostname);
00203                   if (peer)
00204                      X509_free(peer);
00205                   close(tcptls_session->fd);
00206                   fclose(tcptls_session->f);
00207                   ao2_ref(tcptls_session, -1);
00208                   return NULL;
00209                }
00210             }
00211             if (peer)
00212                X509_free(peer);
00213          }
00214       }
00215       if (!tcptls_session->f) /* no success opening descriptor stacking */
00216          SSL_free(tcptls_session->ssl);
00217    }
00218 #endif /* DO_SSL */
00219 
00220    if (!tcptls_session->f) {
00221       close(tcptls_session->fd);
00222       ast_log(LOG_WARNING, "FILE * open failed!\n");
00223 #ifndef DO_SSL
00224       if (tcptls_session->parent->tls_cfg) {
00225          ast_log(LOG_WARNING, "Attempted a TLS connection without OpenSSL support.  This will not work!\n");
00226       }
00227 #endif
00228       ao2_ref(tcptls_session, -1);
00229       return NULL;
00230    }
00231 
00232    if (tcptls_session && tcptls_session->parent->worker_fn)
00233       return tcptls_session->parent->worker_fn(tcptls_session);
00234    else
00235       return tcptls_session;
00236 }
00237 
00238 void *ast_tcptls_server_root(void *data)
00239 {
00240    struct ast_tcptls_session_args *desc = data;
00241    int fd;
00242    struct ast_sockaddr addr;
00243    struct ast_tcptls_session_instance *tcptls_session;
00244    pthread_t launched;
00245 
00246    for (;;) {
00247       int i, flags;
00248 
00249       if (desc->periodic_fn)
00250          desc->periodic_fn(desc);
00251       i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
00252       if (i <= 0)
00253          continue;
00254       fd = ast_accept(desc->accept_fd, &addr);
00255       if (fd < 0) {
00256          if ((errno != EAGAIN) && (errno != EINTR))
00257             ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
00258          continue;
00259       }
00260       tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
00261       if (!tcptls_session) {
00262          ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
00263          close(fd);
00264          continue;
00265       }
00266 
00267       ast_mutex_init(&tcptls_session->lock);
00268 
00269       flags = fcntl(fd, F_GETFL);
00270       fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
00271       tcptls_session->fd = fd;
00272       tcptls_session->parent = desc;
00273       ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
00274 
00275       tcptls_session->client = 0;
00276 
00277       /* This thread is now the only place that controls the single ref to tcptls_session */
00278       if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
00279          ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
00280          close(tcptls_session->fd);
00281          ao2_ref(tcptls_session, -1);
00282       }
00283    }
00284    return NULL;
00285 }
00286 
00287 static int __ssl_setup(struct ast_tls_config *cfg, int client)
00288 {
00289 #ifndef DO_SSL
00290    cfg->enabled = 0;
00291    return 0;
00292 #else
00293    if (!cfg->enabled)
00294       return 0;
00295 
00296    SSL_load_error_strings();
00297    SSLeay_add_ssl_algorithms();
00298 
00299    if (client) {
00300 #ifndef OPENSSL_NO_SSL2
00301       if (ast_test_flag(&cfg->flags, AST_SSL_SSLV2_CLIENT)) {
00302          cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
00303       } else
00304 #endif
00305       if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
00306          cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
00307       } else if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
00308          cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
00309       } else {
00310          /* SSLv23_client_method() sends SSLv2, this was the original
00311           * default for ssl clients before the option was given to
00312           * pick what protocol a client should use.  In order not
00313           * to break expected behavior it remains the default. */
00314          cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
00315       }
00316    } else {
00317       /* SSLv23_server_method() supports TLSv1, SSLv2, and SSLv3 inbound connections. */
00318       cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
00319    }
00320 
00321    if (!cfg->ssl_ctx) {
00322       ast_debug(1, "Sorry, SSL_CTX_new call returned null...\n");
00323       cfg->enabled = 0;
00324       return 0;
00325    }
00326    if (!ast_strlen_zero(cfg->certfile)) {
00327       char *tmpprivate = ast_strlen_zero(cfg->pvtfile) ? cfg->certfile : cfg->pvtfile;
00328       if (SSL_CTX_use_certificate_file(cfg->ssl_ctx, cfg->certfile, SSL_FILETYPE_PEM) == 0) {
00329          if (!client) {
00330             /* Clients don't need a certificate, but if its setup we can use it */
00331             ast_verb(0, "SSL error loading cert file. <%s>", cfg->certfile);
00332             sleep(2);
00333             cfg->enabled = 0;
00334             return 0;
00335          }
00336       }
00337       if ((SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0 )) {
00338          if (!client) {
00339             /* Clients don't need a private key, but if its setup we can use it */
00340             ast_verb(0, "SSL error loading private key file. <%s>", tmpprivate);
00341             sleep(2);
00342             cfg->enabled = 0;
00343             return 0;
00344          }
00345       }
00346    }
00347    if (!ast_strlen_zero(cfg->cipher)) {
00348       if (SSL_CTX_set_cipher_list(cfg->ssl_ctx, cfg->cipher) == 0 ) {
00349          if (!client) {
00350             ast_verb(0, "SSL cipher error <%s>", cfg->cipher);
00351             sleep(2);
00352             cfg->enabled = 0;
00353             return 0;
00354          }
00355       }
00356    }
00357    if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
00358       if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0)
00359          ast_verb(0, "SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
00360    }
00361 
00362    ast_verb(0, "SSL certificate ok\n");
00363    return 1;
00364 #endif
00365 }
00366 
00367 int ast_ssl_setup(struct ast_tls_config *cfg)
00368 {
00369    return __ssl_setup(cfg, 0);
00370 }
00371 
00372 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
00373 {
00374    struct ast_tcptls_session_args *desc;
00375    int flags;
00376 
00377    if (!(desc = tcptls_session->parent)) {
00378       goto client_start_error;
00379    }
00380 
00381    if (ast_connect(desc->accept_fd, &desc->remote_address)) {
00382       ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",
00383          desc->name,
00384          ast_sockaddr_stringify(&desc->remote_address),
00385          strerror(errno));
00386       goto client_start_error;
00387    }
00388 
00389    flags = fcntl(desc->accept_fd, F_GETFL);
00390    fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
00391 
00392    if (desc->tls_cfg) {
00393       desc->tls_cfg->enabled = 1;
00394       __ssl_setup(desc->tls_cfg, 1);
00395    }
00396 
00397    return handle_tcptls_connection(tcptls_session);
00398 
00399 client_start_error:
00400    close(desc->accept_fd);
00401    desc->accept_fd = -1;
00402    if (tcptls_session) {
00403       ao2_ref(tcptls_session, -1);
00404    }
00405    return NULL;
00406 
00407 }
00408 
00409 struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
00410 {
00411    int x = 1;
00412    struct ast_tcptls_session_instance *tcptls_session = NULL;
00413 
00414    /* Do nothing if nothing has changed */
00415    if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
00416       ast_debug(1, "Nothing changed in %s\n", desc->name);
00417       return NULL;
00418    }
00419 
00420    /* If we return early, there is no connection */
00421    ast_sockaddr_setnull(&desc->old_address);
00422 
00423    if (desc->accept_fd != -1)
00424       close(desc->accept_fd);
00425 
00426    desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->remote_address) ?
00427              AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
00428    if (desc->accept_fd < 0) {
00429       ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
00430          desc->name, strerror(errno));
00431       return NULL;
00432    }
00433 
00434    /* if a local address was specified, bind to it so the connection will
00435       originate from the desired address */
00436    if (!ast_sockaddr_isnull(&desc->local_address)) {
00437       setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
00438       if (ast_bind(desc->accept_fd, &desc->local_address)) {
00439          ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
00440             desc->name,
00441             ast_sockaddr_stringify(&desc->local_address),
00442             strerror(errno));
00443          goto error;
00444       }
00445    }
00446 
00447    if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor)))
00448       goto error;
00449 
00450    ast_mutex_init(&tcptls_session->lock);
00451    tcptls_session->client = 1;
00452    tcptls_session->fd = desc->accept_fd;
00453    tcptls_session->parent = desc;
00454    tcptls_session->parent->worker_fn = NULL;
00455    ast_sockaddr_copy(&tcptls_session->remote_address,
00456            &desc->remote_address);
00457 
00458    /* Set current info */
00459    ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
00460    return tcptls_session;
00461 
00462 error:
00463    close(desc->accept_fd);
00464    desc->accept_fd = -1;
00465    if (tcptls_session)
00466       ao2_ref(tcptls_session, -1);
00467    return NULL;
00468 }
00469 
00470 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
00471 {
00472    int flags;
00473    int x = 1;
00474 
00475    /* Do nothing if nothing has changed */
00476    if (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
00477       ast_debug(1, "Nothing changed in %s\n", desc->name);
00478       return;
00479    }
00480 
00481    /* If we return early, there is no one listening */
00482    ast_sockaddr_setnull(&desc->old_address);
00483 
00484    /* Shutdown a running server if there is one */
00485    if (desc->master != AST_PTHREADT_NULL) {
00486       pthread_cancel(desc->master);
00487       pthread_kill(desc->master, SIGURG);
00488       pthread_join(desc->master, NULL);
00489    }
00490 
00491    if (desc->accept_fd != -1)
00492       close(desc->accept_fd);
00493 
00494    /* If there's no new server, stop here */
00495    if (ast_sockaddr_isnull(&desc->local_address)) {
00496       ast_debug(2, "Server disabled:  %s\n", desc->name);
00497       return;
00498    }
00499 
00500    desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->local_address) ?
00501              AF_INET6 : AF_INET, SOCK_STREAM, 0);
00502    if (desc->accept_fd < 0) {
00503       ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
00504       return;
00505    }
00506 
00507    setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
00508    if (ast_bind(desc->accept_fd, &desc->local_address)) {
00509       ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
00510          desc->name,
00511          ast_sockaddr_stringify(&desc->local_address),
00512          strerror(errno));
00513       goto error;
00514    }
00515    if (listen(desc->accept_fd, 10)) {
00516       ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
00517       goto error;
00518    }
00519    flags = fcntl(desc->accept_fd, F_GETFL);
00520    fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
00521    if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
00522       ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
00523          desc->name,
00524          ast_sockaddr_stringify(&desc->local_address),
00525          strerror(errno));
00526       goto error;
00527    }
00528 
00529    /* Set current info */
00530    ast_sockaddr_copy(&desc->old_address, &desc->local_address);
00531 
00532    return;
00533 
00534 error:
00535    close(desc->accept_fd);
00536    desc->accept_fd = -1;
00537 }
00538 
00539 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
00540 {
00541    if (desc->master != AST_PTHREADT_NULL) {
00542       pthread_cancel(desc->master);
00543       pthread_kill(desc->master, SIGURG);
00544       pthread_join(desc->master, NULL);
00545       desc->master = AST_PTHREADT_NULL;
00546    }
00547    if (desc->accept_fd != -1)
00548       close(desc->accept_fd);
00549    desc->accept_fd = -1;
00550    ast_debug(2, "Stopped server :: %s\n", desc->name);
00551 }
00552 
00553 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
00554 {
00555    if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
00556       tls_cfg->enabled = ast_true(value) ? 1 : 0;
00557    } else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
00558       ast_free(tls_cfg->certfile);
00559       tls_cfg->certfile = ast_strdup(value);
00560    } else if (!strcasecmp(varname, "tlsprivatekey") || !strcasecmp(varname, "sslprivatekey")) {
00561       ast_free(tls_cfg->pvtfile);
00562       tls_cfg->pvtfile = ast_strdup(value);
00563    } else if (!strcasecmp(varname, "tlscipher") || !strcasecmp(varname, "sslcipher")) {
00564       ast_free(tls_cfg->cipher);
00565       tls_cfg->cipher = ast_strdup(value);
00566    } else if (!strcasecmp(varname, "tlscafile")) {
00567       ast_free(tls_cfg->cafile);
00568       tls_cfg->cafile = ast_strdup(value);
00569    } else if (!strcasecmp(varname, "tlscapath") || !strcasecmp(varname, "tlscadir")) {
00570       ast_free(tls_cfg->capath);
00571       tls_cfg->capath = ast_strdup(value);
00572    } else if (!strcasecmp(varname, "tlsverifyclient")) {
00573       ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
00574    } else if (!strcasecmp(varname, "tlsdontverifyserver")) {
00575       ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
00576    } else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
00577       if (ast_parse_arg(value, PARSE_ADDR, &tls_desc->local_address))
00578          ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
00579    } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
00580       if (!strcasecmp(value, "tlsv1")) {
00581          ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
00582          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
00583          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
00584       } else if (!strcasecmp(value, "sslv3")) {
00585          ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
00586          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
00587          ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
00588       } else if (!strcasecmp(value, "sslv2")) {
00589          ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
00590          ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
00591          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
00592       }
00593    } else {
00594       return -1;
00595    }
00596 
00597    return 0;
00598 }