ld_session.c

Go to the documentation of this file.
00001 /*
00002 * $Id: ld_session.c 4585 2008-08-06 08:20:30Z klaus_darilion $
00003 *
00004 * Kamailio LDAP Module
00005 *
00006 * Copyright (C) 2007 University of North Carolina
00007 *
00008 * Original author: Christian Schlatter, cs@unc.edu
00009 *
00010 *
00011 * This file is part of Kamailio, a free SIP server.
00012 *
00013 * Kamailio is free software; you can redistribute it and/or modify
00014 * it under the terms of the GNU General Public License as published by
00015 * the Free Software Foundation; either version 2 of the License, or
00016 * (at your option) any later version
00017 *
00018 * Kamailio is distributed in the hope that it will be useful,
00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 * GNU General Public License for more details.
00022 *
00023 * You should have received a copy of the GNU General Public License
00024 * along with this program; if not, write to the Free Software
00025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 *
00027 * History:
00028 * --------
00029 * 2007-02-18: Initial version
00030 */
00031 
00032 
00033 #include <string.h>
00034 #include "ld_session.h"
00035 #include "../../mem/mem.h"
00036 #include "../../sr_module.h"
00037 
00038 
00039 static struct ld_session* ld_sessions = NULL;
00040 static char ini_key_name[512];
00041 
00042 int add_ld_session(char* _name, LDAP* _ldh, dictionary* _d)
00043 {
00044    struct ld_session* current = ld_sessions;
00045    struct ld_session* new_lds = NULL;
00046    char *host_name, *bind_dn, *bind_pwd;
00047    int client_search_timeout_ms, client_bind_timeout_ms, network_timeout_ms;
00048    
00049    new_lds = (struct ld_session*)pkg_malloc(sizeof(struct ld_session));
00050    if (new_lds == NULL)
00051    {
00052       LM_ERR("no memory\n");
00053       return -1;
00054    }
00055    memset( new_lds, 0, sizeof(struct ld_session));
00056 
00057    /* name */
00058    strncpy(new_lds->name, _name, 255);
00059    /* handle */
00060    new_lds->handle = _ldh;
00061    
00062    /* host_name */
00063    host_name = iniparser_getstring(
00064       _d,
00065       get_ini_key_name(_name, CFG_N_LDAP_HOST),
00066       CFG_DEF_HOST_NAME);
00067    new_lds->host_name = (char*)pkg_malloc(strlen(host_name)+1);
00068    if (new_lds->host_name == NULL) {
00069       LM_ERR("no memory\n");
00070       return -1;
00071    }
00072    strcpy(new_lds->host_name, host_name);
00073 
00074    /* version */
00075    new_lds->version = iniparser_getint(
00076       _d,
00077       get_ini_key_name(_name, CFG_N_LDAP_VERSION),
00078       CFG_DEF_LDAP_VERSION);
00079    
00080    /* client_search_timeout */
00081    client_search_timeout_ms = iniparser_getint(
00082       _d,
00083       get_ini_key_name(_name, CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT),
00084       CFG_DEF_LDAP_CLIENT_SEARCH_TIMEOUT);
00085    if (client_search_timeout_ms < CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN)
00086    {
00087       LM_INFO("[%s = %d ms] is below allowed min"
00088          " [%d ms] - [%s] set to [%d ms]\n",
00089          CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT,
00090          client_search_timeout_ms,
00091          CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN,
00092          CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT,
00093          CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN);
00094       client_search_timeout_ms = CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN;
00095    }
00096    new_lds->client_search_timeout.tv_sec = client_search_timeout_ms / 1000;
00097    new_lds->client_search_timeout.tv_usec =
00098                            (client_search_timeout_ms % 1000) * 1000;
00099 
00100    /* client_bind_timeout */
00101    client_bind_timeout_ms = iniparser_getint(
00102       _d,
00103       get_ini_key_name(_name, CFG_N_LDAP_CLIENT_BIND_TIMEOUT),
00104       CFG_DEF_LDAP_CLIENT_BIND_TIMEOUT);
00105    new_lds->client_bind_timeout.tv_sec = client_bind_timeout_ms / 1000;
00106    new_lds->client_bind_timeout.tv_usec = 
00107                            (client_bind_timeout_ms % 1000) * 1000;
00108    
00109    /* network_timeout */
00110    network_timeout_ms = iniparser_getint(
00111       _d,
00112       get_ini_key_name(_name, CFG_N_LDAP_NETWORK_TIMEOUT),
00113       LDAP_NO_LIMIT);
00114    new_lds->network_timeout.tv_sec = network_timeout_ms / 1000;
00115    new_lds->network_timeout.tv_usec = (network_timeout_ms % 1000) * 1000;
00116 
00117    /* bind_dn */
00118    bind_dn = iniparser_getstring(
00119       _d,
00120       get_ini_key_name(_name, CFG_N_LDAP_BIND_DN),
00121       CFG_DEF_LDAP_BIND_DN);
00122    new_lds->bind_dn = (char*)pkg_malloc(strlen(bind_dn)+1);
00123    if (new_lds->bind_dn == NULL) {
00124       LM_ERR("no memory\n");
00125       return -1;
00126    }
00127    strcpy(new_lds->bind_dn, bind_dn);
00128 
00129    /* bind_pwd */
00130    bind_pwd = iniparser_getstring(
00131       _d,
00132       get_ini_key_name(_name, CFG_N_LDAP_BIND_PWD),
00133       CFG_DEF_LDAP_BIND_PWD);
00134    new_lds->bind_pwd = (char*)pkg_malloc(strlen(bind_pwd)+1);
00135    if (new_lds->bind_pwd == NULL) {
00136       LM_ERR("no memory\n");
00137       return -1;
00138    }
00139    strcpy(new_lds->bind_pwd, bind_pwd);
00140 
00141    /* calculate_ha1 */
00142    new_lds->calculate_ha1 = iniparser_getboolean(
00143       _d, 
00144       get_ini_key_name(_name, CFG_N_CALCULATE_HA1), 
00145       CFG_DEF_CALCULATE_HA1);
00146    
00147    
00148    if (current == NULL)
00149    {
00150       ld_sessions = new_lds;
00151    } else
00152    {
00153       while (current->next != NULL) { current = current->next; };
00154       current->next = new_lds;
00155    }
00156 
00157    return 0;
00158 }
00159 
00160 
00161 struct ld_session* get_ld_session(char* _name)
00162 {
00163    struct ld_session* current = ld_sessions;
00164    
00165    if (_name == NULL)
00166    {
00167       LM_ERR("lds_name == NULL\n");
00168       return NULL;
00169    }
00170    while (current != NULL)
00171    {
00172       if (strcmp(current->name, _name) == 0)
00173       {
00174          return current;
00175       }
00176       current = current->next;
00177    }
00178 
00179    return NULL;
00180 }
00181 
00182 
00183 int free_ld_sessions(void)
00184 {
00185    struct ld_session* current = ld_sessions;
00186    struct ld_session* tmp;
00187 
00188    while (current != NULL)
00189    {
00190       tmp = current->next;
00191 
00192       if (current->handle != NULL)
00193       {
00194          ldap_unbind_ext(current->handle, NULL, NULL);
00195       }
00196       if (current->host_name != NULL)
00197       {
00198          pkg_free(current->host_name);
00199       }
00200       if (current->bind_dn != NULL)
00201       {
00202          pkg_free(current->bind_dn);
00203       }
00204       if (current->bind_pwd != NULL)
00205       {
00206          pkg_free(current->bind_pwd);
00207       }
00208       
00209       pkg_free(current);
00210       current = tmp;
00211    }
00212    
00213    ld_sessions = NULL;
00214 
00215    return 0;
00216 }
00217 
00218 char* get_ini_key_name(char* _section, char* _key)
00219 {
00220    sprintf(ini_key_name, "%s:%s", _section, _key);
00221    return ini_key_name;
00222 }
00223 

Generated on Wed May 23 08:00:57 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6