Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
9a4e29d7
提交
9a4e29d7
authored
7月 09, 2014
作者:
Brian West
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
FS-4870
上级
8ec7b25c
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
525 行增加
和
963 行删除
+525
-963
mod_xml_ldap.c
src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c
+525
-398
mod_xml_ldapv2.c
src/mod/xml_int/mod_xml_ldap/mod_xml_ldapv2.c
+0
-565
没有找到文件。
src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c
浏览文件 @
9a4e29d7
...
@@ -34,483 +34,610 @@
...
@@ -34,483 +34,610 @@
#include <switch.h>
#include <switch.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#ifdef MSLDAP
#include <windows.h>
#include <winldap.h>
#include <winber.h>
#define LDAP_OPT_SUCCESS LDAP_SUCCESS
#else
#include <lber.h>
#include <lber.h>
#include <ldap.h>
#include <ldap.h>
#include <sasl/sasl.h>
#include "lutil_ldap.h"
#define PCACHE_TTL 300
#endif
#define NCACHE_TTL 900
typedef
struct
xml_ldap_attribute
xml_ldap_attribute_t
;
typedef
enum
{
typedef
enum
{
XML_LDAP_CONFIG
=
0
,
XML_LDAP_CONFIG
=
0
,
XML_LDAP_DIRECTORY
,
XML_LDAP_DIRECTORY
,
XML_LDAP_DIALPLAN
,
XML_LDAP_DIALPLAN
,
XML_LDAP_PHRASE
,
XML_LDAP_PHRASE
XML_LDAP_LANGUAGE
}
xml_ldap_query_type_t
;
}
xml_ldap_query_type_t
;
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
);
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
);
SWITCH_MODULE_DEFINITION
(
mod_xml_ldap
,
mod_xml_ldap_load
,
mod_xml_ldap_shutdown
,
NULL
);
typedef
struct
xml_binding
{
typedef
struct
xml_binding
{
char
*
bindings
;
char
*
bindings
;
char
*
host
;
xml_ldap_query_type_t
bt
;
char
*
url
;
char
*
basedn
;
char
*
basedn
;
char
*
binddn
;
char
*
binddn
;
char
*
bindpass
;
char
*
bindpass
;
char
*
filter
;
char
*
filter
;
char
**
attrs
;
xml_ldap_attribute_t
*
attr_list
;
lutilSASLdefaults
*
defaults
;
}
xml_binding_t
;
}
xml_binding_t
;
typedef
struct
ldap_c
{
typedef
enum
exten_types
{
LDAP
*
ld
;
LDAP_EXTEN_ID
=
0
,
LDAPMessage
*
msg
;
LDAP_EXTEN_CIDR
,
LDAPMessage
*
entry
;
LDAP_EXTEN_PASSWORD
,
BerElement
*
berkey
;
LDAP_EXTEN_VM_ENABLED
,
BerElement
*
berval
;
LDAP_EXTEN_VM_PASSWORD
,
char
*
key
;
LDAP_EXTEN_VM_MAILFROM
,
LDAP_EXTEN_VM_MAILTO
,
LDAP_EXTEN_VM_EMAILMSG
,
LDAP_EXTEN_VM_NOTEMAILMSG
,
LDAP_EXTEN_VM_ATTACHFILE
,
LDAP_EXTEN_USER_CONTEXT
,
LDAP_EXTEN_EFF_CLIDNAME
,
LDAP_EXTEN_EFF_CLIDNUM
,
LDAP_EXTEN_ACCOUNTCODE
,
LDAP_EXTEN_RULESET
,
LDAP_EXTEN_AREACODE
,
LDAP_EXTEN_CID_EXTNAME
,
LDAP_EXTEN_CID_EXTNUM
,
LDAP_EXTEN_INTNAME
,
LDAP_EXTEN_INTNUM
,
LDAP_EXTEN_RECORD_CALLS
,
LDAP_EXTEN_ACTIVE
,
LDAP_EXTEN_CFWD_REWRITECID
,
LDAP_EXTEN_CFWD_ACTIVE
,
LDAP_EXTEN_CFWD_DEST
,
LDAP_EXTEN_CFWD_BUSYACTIVE
,
LDAP_EXTEN_CFWD_BUSYDEST
,
LDAP_EXTEN_NOANSWERACTIVE
,
LDAP_EXTEN_NOANSWERDEST
,
LDAP_EXTEN_NOANSWERSECONDS
,
LDAP_EXTEN_PROGRESSAUDIO
,
LDAP_EXTEN_ALLOW_OUTBOUND
,
LDAP_EXTEN_ALLOW_XFER
,
LDAP_EXTEN_HOTLINE_ACTIVE
,
LDAP_EXTEN_HOTLINE_DEST
,
LDAP_EXTEN_CLASSOFSERVICE
}
exten_type_t
;
struct
xml_ldap_attribute
{
exten_type_t
type
;
uint64_t
len
;
char
*
val
;
char
*
val
;
char
**
keyvals
;
xml_ldap_attribute_t
*
next
;
char
**
valvals
;
};
char
*
sp
;
}
ldap_ctx
;
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
);
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
);
SWITCH_MODULE_DEFINITION
(
mod_xml_ldap
,
mod_xml_ldap_load
,
mod_xml_ldap_shutdown
,
NULL
);
static
switch_status_t
xml_ldap_directory_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
);
static
switch_status_t
xml_ldap_dialplan_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
);
static
switch_xml_t
xml_ldap_search
(
const
char
*
section
,
const
char
*
tag_name
,
const
char
*
key_name
,
const
char
*
key_value
,
switch_event_t
*
params
,
void
*
user_data
);
static
switch_status_t
trydir
(
switch_xml_t
*
,
int
*
,
LDAP
*
,
char
*
,
char
*
,
xml_binding_t
*
);
static
switch_status_t
do_config
(
void
);
static
switch_status_t
trysearch
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
basedn
,
char
*
filter
);
void
rec
(
switch_xml_t
*
,
int
*
,
LDAP
*
ld
,
char
*
);
#define XML_LDAP_SYNTAX "
[debug_on|debug_off]
"
#define XML_LDAP_SYNTAX ""
SWITCH_STANDARD_API
(
xml_ldap_function
)
SWITCH_STANDARD_API
(
xml_ldap_function
)
{
{
if
(
session
)
{
return
SWITCH_STATUS_FALSE
;
return
SWITCH_STATUS_FALSE
;
}
if
(
zstr
(
cmd
))
{
goto
usage
;
}
if
(
!
strcasecmp
(
cmd
,
"debug_on"
))
{
}
else
if
(
!
strcasecmp
(
cmd
,
"debug_off"
))
{
}
else
{
goto
usage
;
}
stream
->
write_function
(
stream
,
"OK
\n
"
);
return
SWITCH_STATUS_SUCCESS
;
usage
:
stream
->
write_function
(
stream
,
"USAGE: %s
\n
"
,
XML_LDAP_SYNTAX
);
return
SWITCH_STATUS_SUCCESS
;
}
}
static
switch_status_t
xml_ldap_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
,
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
)
const
xml_ldap_query_type_t
query_type
)
{
{
switch
(
query_type
)
{
switch_api_interface_t
*
xml_ldap_api_interface
;
case
XML_LDAP_DIRECTORY
:
return
xml_ldap_directory_result
(
ldap_connection
,
binding
,
xml
,
off
);
case
XML_LDAP_DIALPLAN
:
/* connect my internal structure to the blank pointer passed to me */
return
xml_ldap_dialplan_result
(
ldap_connection
,
binding
,
xml
,
off
);
*
module_interface
=
switch_loadable_module_create_module_interface
(
pool
,
modname
);
SWITCH_ADD_API
(
xml_ldap_api_interface
,
"xml_ldap"
,
"XML LDAP"
,
xml_ldap_function
,
XML_LDAP_SYNTAX
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_INFO
,
"XML LDAP module loading...
\n
"
);
default
:
if
(
do_config
()
!=
SWITCH_STATUS_SUCCESS
)
{
return
SWITCH_STATUS_FALSE
;
return
SWITCH_STATUS_FALSE
;
}
}
/* indicate that the module should continue to be loaded */
return
SWITCH_STATUS_SUCCESS
;
}
}
static
switch_status_t
xml_ldap_dialplan_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
)
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
)
{
{
return
SWITCH_STATUS_FALSE
;
return
SWITCH_STATUS_SUCCESS
;
}
}
static
switch_status_t
xml_ldap_directory_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
)
static
switch_status_t
do_config
(
void
)
{
{
struct
ldap_c
*
ldap
=
ldap_connection
;
char
*
cf
=
"xml_ldap.conf"
;
switch_xml_t
asdf
=
*
xml
;
switch_xml_t
cfg
,
xml
,
bindings_tag
,
binding_tag
,
param
,
tran
;
switch_xml_t
param
,
variable
,
params
=
NULL
,
variables
=
NULL
;
xml_binding_t
*
binding
=
NULL
;
int
i
=
0
;
xml_ldap_attribute_t
*
attr_list
=
NULL
;
int
loff
=
*
off
;
int
x
=
0
;
for
(
ldap
->
entry
=
ldap_first_entry
(
ldap
->
ld
,
ldap
->
msg
);
ldap
->
entry
!=
NULL
;
ldap
->
entry
=
ldap_next_entry
(
ldap
->
ld
,
ldap
->
entry
))
{
if
(
!
(
xml
=
switch_xml_open_cfg
(
cf
,
&
cfg
,
NULL
)))
{
ldap
->
key
=
ldap_first_attribute
(
ldap
->
ld
,
ldap
->
entry
,
&
ldap
->
berkey
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"open of %s failed
\n
"
,
cf
);
do
{
return
SWITCH_STATUS_TERM
;
ldap
->
val
=
ldap_first_attribute
(
ldap
->
ld
,
ldap
->
entry
,
&
ldap
->
berval
);
do
{
if
(
strstr
(
ldap
->
val
,
"value"
))
{
if
(
strstr
(
ldap
->
val
,
ldap
->
key
)
&&
strcmp
(
ldap
->
val
,
ldap
->
key
))
{
if
(
!
strcmp
(
ldap
->
key
,
"param"
))
{
params
=
switch_xml_add_child_d
(
asdf
,
"params"
,
loff
++
);
}
else
if
(
!
strcmp
(
ldap
->
key
,
"variable"
))
{
variables
=
switch_xml_add_child_d
(
asdf
,
"variables"
,
loff
++
);
}
}
ldap
->
keyvals
=
ldap_get_values
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
key
);
if
(
!
(
bindings_tag
=
switch_xml_child
(
cfg
,
"bindings"
)))
{
ldap
->
valvals
=
ldap_get_values
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
val
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Missing <bindings> tag!
\n
"
);
goto
done
;
if
(
ldap
->
keyvals
&&
ldap
->
valvals
)
{
if
(
ldap_count_values
(
ldap
->
valvals
)
==
ldap_count_values
(
ldap
->
keyvals
))
{
for
(
i
=
0
;
ldap
->
keyvals
[
i
]
!=
NULL
&&
ldap
->
valvals
[
i
]
!=
NULL
;
i
++
)
{
if
(
!
strcmp
(
ldap
->
key
,
"param"
))
{
param
=
switch_xml_add_child_d
(
params
,
"param"
,
loff
++
);
switch_xml_set_attr_d
(
param
,
"name"
,
ldap
->
keyvals
[
i
]);
switch_xml_set_attr_d
(
param
,
"value"
,
ldap
->
valvals
[
i
]);
}
else
if
(
!
strcmp
(
ldap
->
key
,
"variable"
))
{
variable
=
switch_xml_add_child_d
(
variables
,
"variable"
,
loff
++
);
switch_xml_set_attr_d
(
variable
,
"name"
,
ldap
->
keyvals
[
i
]);
switch_xml_set_attr_d
(
variable
,
"value"
,
ldap
->
valvals
[
i
]);
}
}
}
for
(
binding_tag
=
switch_xml_child
(
bindings_tag
,
"binding"
);
binding_tag
;
binding_tag
=
binding_tag
->
next
)
{
char
*
bname
=
(
char
*
)
switch_xml_attr_soft
(
binding_tag
,
"name"
);
if
(
ldap
->
keyvals
)
{
if
(
!
(
binding
=
malloc
(
sizeof
(
*
binding
)))
)
{
ldap_value_free
(
ldap
->
keyvals
)
;
goto
done
;
}
}
memset
(
binding
,
0
,
sizeof
(
*
binding
));
binding
->
attr_list
=
attr_list
;
if
(
ldap
->
valvals
)
{
for
(
param
=
switch_xml_child
(
binding_tag
,
"param"
);
param
;
param
=
param
->
next
)
{
ldap_value_free
(
ldap
->
valvals
);
}
}
else
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Seems the values %d and %d are not the same??
\n
"
,
ldap_count_values
(
ldap
->
valvals
),
ldap_count_values
(
ldap
->
keyvals
));
}
}
}
}
if
(
ldap
->
val
)
{
ldap_memfree
(
ldap
->
val
);
}
ldap
->
val
=
ldap_next_attribute
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
berval
);
char
*
var
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"name"
);
char
*
val
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"value"
);
}
while
(
ldap
->
val
!=
NULL
);
if
(
!
strcasecmp
(
var
,
"filter"
))
{
binding
->
bindings
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"bindings"
);
if
(
ldap
->
key
)
{
if
(
!
strncmp
(
binding
->
bindings
,
"configuration"
,
strlen
(
binding
->
bindings
)))
{
ldap_memfree
(
ldap
->
key
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"setting type XML_LDAP_CONFIG
\n
"
);
binding
->
bt
=
XML_LDAP_CONFIG
;
}
else
if
(
!
strncmp
(
binding
->
bindings
,
"directory"
,
strlen
(
binding
->
bindings
)))
{
binding
->
bt
=
XML_LDAP_DIRECTORY
;
}
else
if
(
!
strncmp
(
binding
->
bindings
,
"dialplan"
,
strlen
(
binding
->
bindings
)))
{
binding
->
bt
=
XML_LDAP_DIALPLAN
;
}
else
if
(
!
strncmp
(
binding
->
bindings
,
"phrases"
,
strlen
(
binding
->
bindings
)))
{
binding
->
bt
=
XML_LDAP_PHRASE
;
}
}
if
(
ldap
->
berval
)
{
if
(
val
)
{
ber_free
(
ldap
->
berval
,
0
);
binding
->
filter
=
strdup
(
val
);
printf
(
"binding filter %s to %s
\n
"
,
binding
->
filter
,
binding
->
bindings
);
}
}
else
if
(
!
strncasecmp
(
var
,
"basedn"
,
strlen
(
val
)))
{
binding
->
basedn
=
strdup
(
val
);
}
else
if
(
!
strncasecmp
(
var
,
"binddn"
,
strlen
(
val
)))
{
binding
->
binddn
=
strdup
(
val
);
}
else
if
(
!
strncasecmp
(
var
,
"bindpass"
,
strlen
(
val
)))
{
binding
->
bindpass
=
strdup
(
val
);
}
else
if
(
!
strncasecmp
(
var
,
"url"
,
strlen
(
val
)))
{
binding
->
url
=
strdup
(
val
);
}
}
if
(
binding
&&
binding
->
bt
==
XML_LDAP_DIRECTORY
)
{
attr_list
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
=
memset
(
attr_list
,
0
,
sizeof
(
*
attr_list
));
binding
->
attr_list
=
attr_list
;
param
=
switch_xml_child
(
binding_tag
,
"trans"
);
for
(
tran
=
switch_xml_child
(
param
,
"tran"
);
tran
;
tran
=
tran
->
next
)
{
char
*
n
=
(
char
*
)
switch_xml_attr_soft
(
tran
,
"name"
);
char
*
m
=
(
char
*
)
switch_xml_attr_soft
(
tran
,
"mapfrom"
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
" adding map %s => %s
\n
"
,
m
,
n
);
/* Params */
if
(
!
strncasecmp
(
"id"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_ID
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"cidr"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_CIDR
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"password"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_PASSWORD
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-enabled"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_ENABLED
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-password"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_PASSWORD
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-mailfrom"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_MAILFROM
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-mailto"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_MAILTO
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-email-all-messages"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_EMAILMSG
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-notify-email-all-messages"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_NOTEMAILMSG
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-attach-file"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_ATTACHFILE
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
/* Variables */
}
else
if
(
!
strncasecmp
(
"user_context"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_USER_CONTEXT
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"effective_caller_id_name"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_EFF_CLIDNAME
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"effective_caller_id_number"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_EFF_CLIDNUM
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"accountcode"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_ACCOUNTCODE
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"ruleset"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_RULESET
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
}
attr_list
->
next
=
NULL
;
}
if
(
!
binding
->
basedn
||
!
binding
->
filter
||
!
binding
->
url
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"You must define
\"
basedn
\"
, and
\"
filter
\"
in mod_xml_ldap.conf.xml
\n
"
);
continue
;
}
}
ldap
->
key
=
ldap_next_attribute
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
berkey
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"Binding [%s] XML Fetch Function [%s] (%s) [%s]
\n
"
,
zstr
(
bname
)
?
"N/A"
:
bname
,
binding
->
basedn
,
binding
->
filter
,
binding
->
bindings
?
binding
->
bindings
:
"all"
);
}
while
(
ldap
->
key
!=
NULL
);
switch_xml_bind_search_function
(
xml_ldap_search
,
switch_xml_parse_section_string
(
bname
),
binding
);
if
(
ldap
->
berkey
)
{
x
++
;
ber_free
(
ldap
->
berkey
,
0
);
binding
=
NULL
;
}
}
}
done
:
switch_xml_free
(
xml
);
return
SWITCH_STATUS_SUCCESS
;
return
SWITCH_STATUS_SUCCESS
;
}
}
static
switch_xml_t
xml_ldap_search
(
const
char
*
section
,
const
char
*
tag_name
,
const
char
*
key_name
,
const
char
*
key_value
,
switch_event_t
*
params
,
static
switch_status_t
trydir
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
dir_domain
,
char
*
dir_exten
,
xml_binding_t
*
binding
)
void
*
user_data
)
{
{
xml_binding_t
*
binding
=
(
xml_binding_t
*
)
user_data
;
switch_status_t
ret
=
SWITCH_STATUS_FALSE
;
switch_event_header_t
*
hi
;
int
off
=
*
xoff
;
char
*
key
=
NULL
;
switch_xml_t
xml
=
NULL
,
sub
=
NULL
;
char
*
basedn
=
NULL
,
*
filter
=
NULL
;
struct
berval
**
val
=
NULL
;
struct
ldap_c
ldap_connection
;
BerElement
*
ber
=
NULL
;
struct
ldap_c
*
ldap
=
&
ldap_connection
;
switch_xml_t
xml
=
*
pxml
,
params
=
NULL
,
param
=
NULL
,
vars
=
NULL
,
cur
=
NULL
;
LDAPMessage
*
msg
,
*
entry
;
int
auth_method
=
LDAP_AUTH_SIMPLE
;
xml_ldap_attribute_t
*
attr
=
NULL
;
int
desired_version
=
LDAP_VERSION3
;
static
char
*
fsattr
[]
=
xml_ldap_query_type_t
query_type
;
{
"id"
,
"cidr"
,
"password"
,
"vm-enabled"
,
"vm-password"
,
"vm-mailfrom"
,
"vm-mailto"
,
char
*
dir_exten
=
NULL
,
*
dir_domain
=
NULL
;
"vm-email-all-messages"
,
"vm-notify-email-all-messages"
,
"vm-attach-file"
,
"user_context"
,
"effective_caller_id_name"
,
"effective_caller_id_number"
,
char
*
search_filter
=
NULL
,
*
search_base
=
NULL
;
"accountcode"
,
"ruleset"
,
NULL
};
int
off
=
0
,
ret
=
1
;
basedn
=
switch_mprintf
(
binding
->
basedn
,
dir_domain
);
//char *buf;
filter
=
switch_mprintf
(
binding
->
filter
,
dir_exten
);
//buf = malloc(4096);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"searching in basedn %s with filter %s
\n
"
,
basedn
,
filter
);
if
(
!
binding
)
{
if
((
ldap_search_s
(
ld
,
basedn
,
LDAP_SCOPE_SUB
,
filter
,
NULL
,
0
,
&
msg
)
!=
LDAP_SUCCESS
))
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"No bindings...sorry bud returning now
\n
"
);
goto
cleanup
;
return
NULL
;
}
if
(
!
strcmp
(
section
,
"configuration"
))
{
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
query_type
=
XML_LDAP_CONFIG
;
ret
=
SWITCH_STATUS_SUCCESS
;
}
else
if
(
!
strcmp
(
section
,
"directory"
))
{
xml
=
switch_xml_add_child_d
(
xml
,
"section"
,
off
++
);
query_type
=
XML_LDAP_DIRECTORY
;
switch_xml_set_attr_d
(
xml
,
"name"
,
"directory"
);
}
else
if
(
!
strcmp
(
section
,
"dialplan"
))
{
query_type
=
XML_LDAP_DIALPLAN
;
xml
=
switch_xml_add_child_d
(
xml
,
"domain"
,
off
++
);
}
else
if
(
!
strcmp
(
section
,
"phrases"
))
{
switch_xml_set_attr_d
(
xml
,
"name"
,
dir_domain
);
query_type
=
XML_LDAP_PHRASE
;
}
else
if
(
!
strcmp
(
section
,
"languages"
))
{
params
=
switch_xml_add_child_d
(
xml
,
"params"
,
off
++
);
query_type
=
XML_LDAP_LANGUAGE
;
param
=
switch_xml_add_child_d
(
params
,
"param"
,
off
++
);
switch_xml_set_attr_d
(
param
,
"name"
,
"dial-string"
);
switch_xml_set_attr_d
(
param
,
"value"
,
"{^^:sip_invite_domain=${dialed_domain}:presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(*/${dialed_user}@${dialed_domain})}"
);
xml
=
switch_xml_add_child_d
(
xml
,
"user"
,
off
++
);
params
=
switch_xml_add_child_d
(
xml
,
"params"
,
off
++
);
vars
=
switch_xml_add_child_d
(
xml
,
"variables"
,
off
++
);
for
(
entry
=
ldap_first_entry
(
ld
,
msg
);
entry
!=
NULL
;
entry
=
ldap_next_entry
(
ld
,
entry
))
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"entry searched by filter %s
\n
"
,
filter
);
for
(
key
=
ldap_first_attribute
(
ld
,
entry
,
&
ber
);
key
!=
NULL
;
key
=
ldap_next_attribute
(
ld
,
entry
,
ber
))
{
for
(
attr
=
binding
->
attr_list
;
attr
;
attr
=
attr
->
next
)
{
if
(
strlen
(
key
)
==
attr
->
len
)
{
if
(
!
strncasecmp
(
attr
->
val
,
key
,
strlen
(
key
)))
{
val
=
ldap_get_values_len
(
ld
,
entry
,
key
);
if
(
val
!=
NULL
)
{
if
(
ldap_count_values_len
(
val
)
>
0
&&
val
[
0
]
!=
NULL
&&
val
[
0
]
->
bv_val
!=
NULL
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
" attribute %s => %s
\n
"
,
fsattr
[
attr
->
type
],
val
[
0
]
->
bv_val
);
if
(
attr
->
type
<
2
)
{
switch_xml_set_attr_d
(
xml
,
fsattr
[
attr
->
type
],
val
[
0
]
->
bv_val
);
}
else
if
(
attr
->
type
<
10
)
{
cur
=
switch_xml_add_child_d
(
params
,
"param"
,
0
);
switch_xml_set_attr_d
(
cur
,
"name"
,
fsattr
[
attr
->
type
]);
switch_xml_set_attr_d
(
cur
,
"value"
,
val
[
0
]
->
bv_val
);
}
else
{
}
else
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Invalid section
\n
"
);
cur
=
switch_xml_add_child_d
(
vars
,
"variable"
,
0
);
return
NULL
;
switch_xml_set_attr_d
(
cur
,
"name"
,
fsattr
[
attr
->
type
]);
switch_xml_set_attr_d
(
cur
,
"value"
,
val
[
0
]
->
bv_val
);
}
}
if
(
params
)
{
if
((
hi
=
params
->
headers
))
{
for
(;
hi
;
hi
=
hi
->
next
)
{
switch
(
query_type
)
{
case
XML_LDAP_CONFIG
:
break
;
case
XML_LDAP_DIRECTORY
:
if
(
!
strcmp
(
hi
->
name
,
"user"
))
{
dir_exten
=
strdup
(
hi
->
value
);
}
else
if
(
!
strcmp
(
hi
->
name
,
"domain"
))
{
dir_domain
=
strdup
(
hi
->
value
);
}
}
break
;
ldap_value_free_len
(
val
);
case
XML_LDAP_DIALPLAN
:
case
XML_LDAP_PHRASE
:
case
XML_LDAP_LANGUAGE
:
break
;
}
}
continue
;
}
}
switch
(
query_type
)
{
case
XML_LDAP_CONFIG
:
break
;
case
XML_LDAP_DIRECTORY
:
if
(
dir_exten
&&
dir_domain
)
{
if
((
xml
=
switch_xml_new
(
"directory"
)))
{
switch_xml_set_attr_d
(
xml
,
"type"
,
"freeswitch/xml"
);
if
((
sub
=
switch_xml_add_child_d
(
xml
,
"section"
,
off
++
)))
{
switch_xml_set_attr_d
(
sub
,
"name"
,
"directory"
);
}
}
if
((
sub
=
switch_xml_add_child_d
(
sub
,
"domain"
,
off
++
)))
{
switch_xml_set_attr_d
(
sub
,
"name"
,
dir_domain
);
}
}
ldap_memfree
(
key
);
if
((
sub
=
switch_xml_add_child_d
(
sub
,
"user"
,
off
++
)))
{
switch_xml_set_attr_d
(
sub
,
"id"
,
dir_exten
);
}
}
ber_free
(
ber
,
0
);
}
}
search_filter
=
switch_mprintf
(
binding
->
filter
,
dir_exten
);
ldap_msgfree
(
entry
);
search_base
=
switch_mprintf
(
binding
->
basedn
,
dir_domain
);
ldap_msgfree
(
msg
);
}
else
{
ret
=
SWITCH_STATUS_FALSE
;
}
free
(
dir_exten
);
cleanup
:
dir_exten
=
NULL
;
switch_safe_free
(
filter
);
switch_safe_free
(
basedn
)
switch_safe_free
(
dir_exten
);
switch_safe_free
(
dir_domain
);
free
(
dir_domain
)
;
return
ret
;
dir_domain
=
NULL
;
}
}
else
{
static
switch_status_t
trysearch
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
basedn
,
char
*
filter
)
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
{
"Something bad happened during the query construction phase likely exten(%s) or domain(%s) is null
\n
"
,
dir_exten
,
switch_status_t
ret
=
SWITCH_STATUS_FALSE
;
dir_domain
);
int
off
=
*
xoff
;
char
*
key
=
NULL
;
char
*
dn
=
NULL
;
char
**
val
=
NULL
;
BerElement
*
ber
=
NULL
;
switch_xml_t
xml
=
*
pxml
;
LDAPMessage
*
msg
,
*
entry
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"trying search in base %s with filter %s
\n
"
,
basedn
,
filter
);
if
((
ldap_search_s
(
ld
,
basedn
,
LDAP_SCOPE_ONE
,
filter
,
NULL
,
0
,
&
msg
)
!=
LDAP_SUCCESS
))
goto
cleanup
;
goto
cleanup
;
}
break
;
case
XML_LDAP_DIALPLAN
:
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
if
((
xml
=
switch_xml_new
(
"document"
)))
{
ret
=
SWITCH_STATUS_SUCCESS
;
switch_xml_set_attr_d
(
xml
,
"type"
,
"freeswitch/xml"
);
for
(
entry
=
ldap_first_entry
(
ld
,
msg
);
entry
!=
NULL
;
entry
=
ldap_next_entry
(
ld
,
entry
))
{
if
((
sub
=
switch_xml_add_child_d
(
xml
,
"section"
,
off
++
)))
{
val
=
ldap_get_values
(
ld
,
entry
,
"fstag"
);
switch_xml_set_attr_d
(
sub
,
"name"
,
"dialplan"
);
xml
=
switch_xml_add_child_d
(
xml
,
val
[
0
],
off
);
}
ldap_value_free
(
val
);
for
(
key
=
ldap_first_attribute
(
ld
,
entry
,
&
ber
);
key
!=
NULL
;
key
=
ldap_next_attribute
(
ld
,
entry
,
ber
))
{
sub
=
switch_xml_add_child_d
(
xml
,
"context"
,
off
++
);
if
(
!
strncasecmp
(
key
,
"fstag"
,
strlen
(
key
))
||
!
strncasecmp
(
key
,
"objectclass"
,
strlen
(
key
)))
{
ldap_memfree
(
key
);
continue
;
}
}
break
;
val
=
ldap_get_values
(
ld
,
entry
,
key
);
switch_xml_set_attr_d
(
xml
,
key
,
val
[
0
]);
case
XML_LDAP_PHRASE
:
ldap_memfree
(
key
);
case
XML_LDAP_LANGUAGE
:
ldap_value_free
(
val
);
break
;
}
}
else
{
goto
cleanup
;
}
}
}
ber_free
(
ber
,
0
);
dn
=
ldap_get_dn
(
ld
,
entry
);
rec
(
&
xml
,
&
off
,
ld
,
dn
);
if
((
ldap
->
ld
=
(
LDAP
*
)
ldap_init
(
binding
->
host
,
LDAP_PORT
))
==
NULL
)
{
*
xoff
=
1
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Unable to connect to ldap server.%s
\n
"
,
binding
->
host
);
goto
cleanup
;
}
}
if
(
ldap_set_option
(
ldap
->
ld
,
LDAP_OPT_PROTOCOL_VERSION
,
&
desired_version
)
!=
LDAP_OPT_SUCCESS
)
{
ldap_msgfree
(
entry
);
goto
cleanup
;
ldap_msgfree
(
msg
)
;
}
}
ldap_set_option
(
ldap
->
ld
,
LDAP_OPT_X_SASL_SECPROPS
,
&
ldap
->
sp
);
cleanup
:
switch_safe_free
(
basedn
);
switch_safe_free
(
filter
);
switch_safe_free
(
key
);
return
ret
;
}
void
rec
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
dn
)
{
int
off
=
*
xoff
;
char
*
key
;
char
**
val
;
if
(
binding
->
binddn
)
{
switch_xml_t
xml
=
*
pxml
,
new
;
if
(
ldap_bind_s
(
ldap
->
ld
,
binding
->
binddn
,
binding
->
bindpass
,
auth_method
)
!=
LDAP_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Unable to bind to ldap server %s as %s
\n
"
,
binding
->
host
,
binding
->
binddn
);
goto
cleanup
;
}
}
else
{
if
(
ldap_sasl_interactive_bind_s
(
ldap
->
ld
,
NULL
,
binding
->
defaults
->
mech
,
NULL
,
NULL
,
(
unsigned
)
(
intptr_t
)
LDAP_SASL_SIMPLE
,
lutil_sasl_interact
,
binding
->
defaults
)
!=
LDAP_SUCCESS
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Unable to sasl_bind to ldap server %s as %s
\n
"
,
binding
->
host
,
binding
->
defaults
->
authcid
);
goto
cleanup
;
}
}
if
(
ldap_search_s
(
ldap
->
ld
,
search_base
,
LDAP_SCOPE_SUBTREE
,
search_filter
,
NULL
,
0
,
&
ldap
->
msg
)
!=
LDAP_SUCCESS
)
{
LDAPMessage
*
msg
,
*
entry
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Query failed: -b
\"
%s
\"
\"
%s
\"\n
"
,
search_base
,
search_filter
);
BerElement
*
ber
;
goto
cleanup
;
}
if
(
ldap_count_entries
(
ldap
->
ld
,
ldap
->
msg
)
<=
0
)
{
ldap_search_s
(
ld
,
dn
,
LDAP_SCOPE_ONE
,
NULL
,
NULL
,
0
,
&
msg
);
goto
cleanup
;
switch_safe_free
(
dn
);
}
if
(
sub
&&
xml_ldap_result
(
&
ldap_connection
,
binding
,
&
sub
,
&
off
,
query_type
)
!=
SWITCH_STATUS_SUCCESS
)
{
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
goto
cleanup
;
}
ret
=
0
;
for
(
entry
=
ldap_first_entry
(
ld
,
msg
);
entry
!=
NULL
;
entry
=
ldap_next_entry
(
ld
,
entry
))
{
cleanup
:
val
=
ldap_get_values
(
ld
,
entry
,
"fstag"
);
if
(
ldap
->
msg
)
{
new
=
switch_xml_add_child_d
(
xml
,
val
[
0
],
off
);
ldap_msgfree
(
ldap
->
msg
);
ldap_value_free
(
val
);
}
if
(
ldap
->
ld
)
{
for
(
key
=
ldap_first_attribute
(
ld
,
entry
,
&
ber
);
key
!=
NULL
;
key
=
ldap_next_attribute
(
ld
,
entry
,
ber
))
{
ldap_unbind_s
(
ldap
->
ld
);
}
switch_safe_free
(
search_filter
);
if
(
!
strncasecmp
(
"fstag"
,
key
,
5
)
||
!
strncasecmp
(
"objectclass"
,
key
,
10
))
{
switch_safe_free
(
search_base
);
ldap_memfree
(
key
);
continue
;
}
//switch_xml_toxml_buf(xml,buf,0,0,1);
val
=
ldap_get_values
(
ld
,
entry
,
key
);
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Providing:\n%s\n", buf);
switch_xml_set_attr_d
(
new
,
key
,
val
[
0
]);
ldap_memfree
(
key
);
ldap_value_free
(
val
);
}
ber_free
(
ber
,
0
);
rec
(
&
new
,
xoff
,
ld
,
ldap_get_dn
(
ld
,
entry
));
}
ldap_msgfree
(
entry
);
if
(
ret
)
{
switch_xml_free
(
xml
);
return
NULL
;
}
}
return
xml
;
ldap_msgfree
(
msg
)
;
}
}
static
switch_xml_t
xml_ldap_search
(
const
char
*
section
,
const
char
*
tag_name
,
const
char
*
key_name
,
const
char
*
key_value
,
switch_event_t
*
params
,
static
switch_status_t
do_config
(
void
)
void
*
user_data
)
{
{
char
*
cf
=
"xml_ldap.conf"
;
switch_xml_t
cfg
,
xml
,
bindings_tag
,
binding_tag
,
param
;
xml_binding_t
*
binding
=
NULL
;
int
x
=
0
;
if
(
!
(
xml
=
switch_xml_open_cfg
(
cf
,
&
cfg
,
NULL
)))
{
xml_binding_t
*
binding
=
(
xml_binding_t
*
)
user_data
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Open of %s failed
\n
"
,
cf
);
switch_event_header_t
*
hi
;
return
SWITCH_STATUS_TERM
;
switch_status_t
ret
=
SWITCH_STATUS_FALSE
;
}
if
(
!
(
bindings_tag
=
switch_xml_child
(
cfg
,
"bindings"
)))
{
int
desired_version
=
LDAP_VERSION3
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Missing <bindings> tag!
\n
"
);
int
auth_method
=
LDAP_AUTH_SIMPLE
;
goto
done
;
}
for
(
binding_tag
=
switch_xml_child
(
bindings_tag
,
"binding"
);
binding_tag
;
binding_tag
=
binding_tag
->
next
)
{
char
*
basedn
=
NULL
,
*
filter
=
NULL
;
char
*
bname
=
(
char
*
)
switch_xml_attr_soft
(
binding_tag
,
"name"
)
;
char
*
dir_domain
=
NULL
,
*
dir_exten
=
NULL
;
if
(
!
(
binding
=
malloc
(
sizeof
(
*
binding
))))
{
LDAP
*
ld
;
goto
done
;
switch_xml_t
xml
=
NULL
;
}
memset
(
binding
,
0
,
sizeof
(
*
binding
));
if
(
!
(
binding
->
defaults
=
malloc
(
sizeof
(
lutilSASLdefaults
))))
{
int
xoff
=
0
;
goto
done
;
}
memset
(
binding
->
defaults
,
0
,
sizeof
(
lutilSASLdefaults
));
for
(
param
=
switch_xml_child
(
binding_tag
,
"param"
);
param
;
param
=
param
->
next
)
{
char
*
buf
;
buf
=
malloc
(
4096
);
char
*
var
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"name
"
);
xml
=
switch_xml_new
(
"document
"
);
char
*
val
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"value
"
);
switch_xml_set_attr_d
(
xml
,
"type"
,
"freeswitch/xml
"
);
if
(
!
strcasecmp
(
var
,
"filter"
))
{
if
(
params
)
{
binding
->
bindings
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"bindings"
);
if
((
hi
=
params
->
headers
))
{
if
(
val
)
{
for
(;
hi
;
hi
=
hi
->
next
)
{
binding
->
filter
=
strdup
(
val
);
switch
(
binding
->
bt
)
{
}
case
XML_LDAP_CONFIG
:
}
else
if
(
!
strcasecmp
(
var
,
"basedn"
))
{
break
;
binding
->
basedn
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"binddn"
))
{
binding
->
binddn
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"bindpass"
))
{
binding
->
bindpass
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"host"
))
{
binding
->
host
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"mech"
))
{
binding
->
defaults
->
mech
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"realm"
))
{
binding
->
defaults
->
realm
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"authcid"
))
{
binding
->
defaults
->
authcid
=
strdup
(
val
);
}
else
if
(
!
strcasecmp
(
var
,
"authzid"
))
{
binding
->
defaults
->
authzid
=
strdup
(
val
);
}
case
XML_LDAP_DIRECTORY
:
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_DEBUG
,
"from cb got %s=%s
\n
"
,
hi
->
name
,
hi
->
value
);
if
(
!
strncmp
(
hi
->
name
,
"user"
,
strlen
(
hi
->
name
)))
{
dir_exten
=
strdup
(
hi
->
value
);
}
else
if
(
!
strncmp
(
hi
->
name
,
"domain"
,
strlen
(
hi
->
name
)))
{
dir_domain
=
strdup
(
hi
->
value
);
}
}
break
;
if
(
!
binding
->
basedn
||
!
binding
->
filter
)
{
case
XML_LDAP_DIALPLAN
:
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"You must define
\"
basedn
\"
, and
\"
filter
\"
in mod_xml_ldap.conf.xml
\n
"
);
case
XML_LDAP_PHRASE
:
continue
;
break
;
}
}
}
}
}
if
((
ldap_initialize
(
&
ld
,
binding
->
url
))
!=
LDAP_SUCCESS
)
goto
cleanup
;
if
((
ldap_set_option
(
ld
,
LDAP_OPT_PROTOCOL_VERSION
,
&
desired_version
))
!=
LDAP_SUCCESS
)
goto
cleanup
;
if
((
ldap_bind_s
(
ld
,
binding
->
binddn
,
binding
->
bindpass
,
auth_method
))
!=
LDAP_SUCCESS
)
goto
cleanup
;
switch
(
binding
->
bt
)
{
case
XML_LDAP_CONFIG
:
xml
=
switch_xml_add_child_d
(
xml
,
"section"
,
xoff
++
);
switch_xml_set_attr_d
(
xml
,
"name"
,
"configuration"
);
filter
=
switch_mprintf
(
binding
->
filter
,
key_name
,
key_value
);
basedn
=
switch_mprintf
(
binding
->
basedn
,
tag_name
);
ret
=
trysearch
(
&
xml
,
&
xoff
,
ld
,
basedn
,
filter
);
break
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"Binding [%s] XML Fetch Function [%s] (%s) [%s]
\n
"
,
case
XML_LDAP_DIRECTORY
:
zstr
(
bname
)
?
"N/A"
:
bname
,
binding
->
basedn
,
binding
->
filter
,
binding
->
bindings
?
binding
->
bindings
:
"all"
);
ret
=
trydir
(
&
xml
,
&
xoff
,
ld
,
dir_domain
,
dir_exten
,
binding
);
break
;
switch_xml_bind_search_function
(
xml_ldap_search
,
switch_xml_parse_section_string
(
bname
),
binding
);
case
XML_LDAP_DIALPLAN
:
break
;
x
++
;
case
XML_LDAP_PHRASE
:
binding
=
NULL
;
break
;
}
}
done
:
switch_xml_free
(
xml
);
return
SWITCH_STATUS_SUCCESS
;
}
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
)
{
switch_api_interface_t
*
xml_ldap_api_interface
;
/* connect my internal structure to the blank pointer passed to me */
cleanup
:
*
module_interface
=
switch_loadable_module_create_module_interface
(
pool
,
modname
);
ldap_unbind_s
(
ld
);
SWITCH_ADD_API
(
xml_ldap_api_interface
,
"xml_ldap"
,
"XML LDAP"
,
xml_ldap_function
,
XML_LDAP_SYNTAX
);
switch_xml_toxml_buf
(
xml
,
buf
,
0
,
0
,
1
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_INFO
,
"XML LDAP module loading...
\n
"
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_DEBUG
,
"XML providing:
\n
%s
\n
"
,
buf
);
switch_safe_free
(
buf
);
if
(
do_config
()
!=
SWITCH_STATUS_SUCCESS
)
{
if
(
ret
!=
SWITCH_STATUS_SUCCESS
)
{
return
SWITCH_STATUS_FALSE
;
switch_xml_free
(
xml
);
return
NULL
;
}
}
/* indicate that the module should continue to be loaded */
return
xml
;
return
SWITCH_STATUS_SUCCESS
;
}
}
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
)
{
return
SWITCH_STATUS_SUCCESS
;
}
/* For Emacs:
/* For Emacs:
* Local Variables:
* Local Variables:
...
...
src/mod/xml_int/mod_xml_ldap/mod_xml_ldapv2.c
deleted
100644 → 0
浏览文件 @
8ec7b25c
#include <switch.h>
#include <stdlib.h>
#include <string.h>
#include <lber.h>
#include <ldap.h>
#define PCACHE_TTL 300
#define NCACHE_TTL 900
typedef
struct
xml_ldap_attribute
xml_ldap_attribute_t
;
typedef
enum
{
XML_LDAP_CONFIG
=
0
,
XML_LDAP_DIRECTORY
,
XML_LDAP_DIALPLAN
,
XML_LDAP_PHRASE
}
xml_ldap_query_type_t
;
typedef
struct
xml_binding
{
char
*
bindings
;
xml_ldap_query_type_t
bt
;
char
*
url
;
char
*
basedn
;
char
*
binddn
;
char
*
bindpass
;
char
*
filter
;
xml_ldap_attribute_t
*
attr_list
;
}
xml_binding_t
;
typedef
enum
exten_types
{
LDAP_EXTEN_ID
=
0
,
LDAP_EXTEN_VM_MAILBOX
,
LDAP_EXTEN_PASSWORD
,
LDAP_EXTEN_VM_PASSWORD
,
LDAP_EXTEN_VM_EMAILADDR
,
LDAP_EXTEN_VM_EMAILMSG
,
LDAP_EXTEN_VM_DELETE
,
LDAP_EXTEN_VM_ATTACHAUDIO
,
LDAP_EXTEN_NAME
,
LDAP_EXTEN_LABEL
,
LDAP_EXTEN_AREACODE
,
LDAP_EXTEN_CID_EXTNAME
,
LDAP_EXTEN_CID_EXTNUM
,
LDAP_EXTEN_INTNAME
,
LDAP_EXTEN_INTNUM
,
LDAP_EXTEN_RECORD_CALLS
,
LDAP_EXTEN_ACTIVE
,
LDAP_EXTEN_CFWD_REWRITECID
,
LDAP_EXTEN_CFWD_ACTIVE
,
LDAP_EXTEN_CFWD_DEST
,
LDAP_EXTEN_CFWD_BUSYACTIVE
,
LDAP_EXTEN_CFWD_BUSYDEST
,
LDAP_EXTEN_NOANSWERACTIVE
,
LDAP_EXTEN_NOANSWERDEST
,
LDAP_EXTEN_NOANSWERSECONDS
,
LDAP_EXTEN_PROGRESSAUDIO
,
LDAP_EXTEN_ALLOW_OUTOBUND
,
LDAP_EXTEN_ALLOW_XFER
,
LDAP_EXTEN_HOTLINE_ACTIVE
,
LDAP_EXTEN_HOTLINE_DEST
,
LDAP_EXTEN_CLASSOFSERVICE
}
exten_type_t
;
struct
xml_ldap_attribute
{
exten_type_t
type
;
uint64_t
len
;
char
*
val
;
xml_ldap_attribute_t
*
next
;
};
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
);
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
);
SWITCH_MODULE_DEFINITION
(
mod_xml_ldap
,
mod_xml_ldap_load
,
mod_xml_ldap_shutdown
,
NULL
);
static
switch_xml_t
xml_ldap_search
(
const
char
*
section
,
const
char
*
tag_name
,
const
char
*
key_name
,
const
char
*
key_value
,
switch_event_t
*
params
,
void
*
user_data
);
static
switch_status_t
trydir
(
switch_xml_t
*
,
int
*
,
LDAP
*
,
char
*
,
char
*
,
xml_binding_t
*
);
static
switch_status_t
do_config
(
void
);
static
switch_status_t
trysearch
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
basedn
,
char
*
filter
);
void
rec
(
switch_xml_t
*
,
int
*
,
LDAP
*
ld
,
char
*
);
#define XML_LDAP_SYNTAX ""
SWITCH_STANDARD_API
(
xml_ldap_function
)
{
return
SWITCH_STATUS_FALSE
;
}
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
)
{
switch_api_interface_t
*
xml_ldap_api_interface
;
/* connect my internal structure to the blank pointer passed to me */
*
module_interface
=
switch_loadable_module_create_module_interface
(
pool
,
modname
);
SWITCH_ADD_API
(
xml_ldap_api_interface
,
"xml_ldap"
,
"XML LDAP"
,
xml_ldap_function
,
XML_LDAP_SYNTAX
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_INFO
,
"XML LDAP module loading...
\n
"
);
if
(
do_config
()
!=
SWITCH_STATUS_SUCCESS
)
{
return
SWITCH_STATUS_FALSE
;
}
/* indicate that the module should continue to be loaded */
return
SWITCH_STATUS_SUCCESS
;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
)
{
return
SWITCH_STATUS_SUCCESS
;
}
static
switch_status_t
do_config
(
void
)
{
char
*
cf
=
"xml_ldap.conf"
;
switch_xml_t
cfg
,
xml
,
bindings_tag
,
binding_tag
,
param
,
tran
;
xml_binding_t
*
binding
=
NULL
;
xml_ldap_attribute_t
*
attr_list
=
NULL
;
int
x
=
0
;
if
(
!
(
xml
=
switch_xml_open_cfg
(
cf
,
&
cfg
,
NULL
)))
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"open of %s failed
\n
"
,
cf
);
return
SWITCH_STATUS_TERM
;
}
if
(
!
(
bindings_tag
=
switch_xml_child
(
cfg
,
"bindings"
)))
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Missing <bindings> tag!
\n
"
);
goto
done
;
}
for
(
binding_tag
=
switch_xml_child
(
bindings_tag
,
"binding"
);
binding_tag
;
binding_tag
=
binding_tag
->
next
)
{
char
*
bname
=
(
char
*
)
switch_xml_attr_soft
(
binding_tag
,
"name"
);
if
(
!
(
binding
=
malloc
(
sizeof
(
*
binding
))))
{
goto
done
;
}
memset
(
binding
,
0
,
sizeof
(
*
binding
));
binding
->
attr_list
=
attr_list
;
for
(
param
=
switch_xml_child
(
binding_tag
,
"param"
);
param
;
param
=
param
->
next
)
{
char
*
var
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"name"
);
char
*
val
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"value"
);
if
(
!
strcasecmp
(
var
,
"filter"
))
{
binding
->
bindings
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"bindings"
);
if
(
!
strncmp
(
binding
->
bindings
,
"configuration"
,
strlen
(
binding
->
bindings
)))
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"setting type XML_LDAP_CONFIG
\n
"
);
binding
->
bt
=
XML_LDAP_CONFIG
;
}
else
if
(
!
strncmp
(
binding
->
bindings
,
"directory"
,
strlen
(
binding
->
bindings
)))
{
binding
->
bt
=
XML_LDAP_DIRECTORY
;
}
else
if
(
!
strncmp
(
binding
->
bindings
,
"dialplain"
,
strlen
(
binding
->
bindings
)))
{
binding
->
bt
=
XML_LDAP_DIALPLAN
;
}
else
if
(
!
strncmp
(
binding
->
bindings
,
"phrases"
,
strlen
(
binding
->
bindings
)))
{
binding
->
bt
=
XML_LDAP_PHRASE
;
}
if
(
val
)
{
binding
->
filter
=
strdup
(
val
);
printf
(
"binding filter %s to %s
\n
"
,
binding
->
filter
,
binding
->
bindings
);
}
}
else
if
(
!
strncasecmp
(
var
,
"basedn"
,
strlen
(
val
)))
{
binding
->
basedn
=
strdup
(
val
);
}
else
if
(
!
strncasecmp
(
var
,
"binddn"
,
strlen
(
val
)))
{
binding
->
binddn
=
strdup
(
val
);
}
else
if
(
!
strncasecmp
(
var
,
"bindpass"
,
strlen
(
val
)))
{
binding
->
bindpass
=
strdup
(
val
);
}
else
if
(
!
strncasecmp
(
var
,
"url"
,
strlen
(
val
)))
{
binding
->
url
=
strdup
(
val
);
}
}
if
(
binding
&&
binding
->
bt
==
XML_LDAP_DIRECTORY
)
{
attr_list
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
=
memset
(
attr_list
,
0
,
sizeof
(
*
attr_list
));
binding
->
attr_list
=
attr_list
;
param
=
switch_xml_child
(
binding_tag
,
"trans"
);
for
(
tran
=
switch_xml_child
(
param
,
"tran"
);
tran
;
tran
=
tran
->
next
)
{
char
*
n
=
(
char
*
)
switch_xml_attr_soft
(
tran
,
"name"
);
char
*
m
=
(
char
*
)
switch_xml_attr_soft
(
tran
,
"mapfrom"
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
" adding map %s => %s
\n
"
,
m
,
n
);
if
(
!
strncasecmp
(
"id"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_ID
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"mailbox"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_MAILBOX
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"password"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_PASSWORD
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-password"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_PASSWORD
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"email-addr"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_EMAILADDR
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-email-all-messages"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_EMAILMSG
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-delete-file"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_DELETE
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
else
if
(
!
strncasecmp
(
"vm-attach-file"
,
n
,
strlen
(
n
)))
{
attr_list
->
type
=
LDAP_EXTEN_VM_ATTACHAUDIO
;
attr_list
->
len
=
strlen
(
m
);
attr_list
->
val
=
strdup
(
m
);
attr_list
->
next
=
malloc
(
sizeof
(
*
attr_list
));
attr_list
->
next
=
memset
(
attr_list
->
next
,
0
,
sizeof
(
*
attr_list
));
attr_list
=
attr_list
->
next
;
}
}
attr_list
->
next
=
NULL
;
}
if
(
!
binding
->
basedn
||
!
binding
->
filter
||
!
binding
->
url
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"You must define
\"
basedn
\"
, and
\"
filter
\"
in mod_xml_ldap.conf.xml
\n
"
);
continue
;
}
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"Binding [%s] XML Fetch Function [%s] (%s) [%s]
\n
"
,
zstr
(
bname
)
?
"N/A"
:
bname
,
binding
->
basedn
,
binding
->
filter
,
binding
->
bindings
?
binding
->
bindings
:
"all"
);
switch_xml_bind_search_function
(
xml_ldap_search
,
switch_xml_parse_section_string
(
bname
),
binding
);
x
++
;
binding
=
NULL
;
}
done
:
switch_xml_free
(
xml
);
return
SWITCH_STATUS_SUCCESS
;
}
static
switch_status_t
trydir
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
dir_domain
,
char
*
dir_exten
,
xml_binding_t
*
binding
)
{
switch_status_t
ret
=
SWITCH_STATUS_FALSE
;
int
off
=
*
xoff
;
char
*
key
=
NULL
;
char
*
basedn
=
NULL
,
*
filter
=
NULL
;
char
**
val
=
NULL
;
BerElement
*
ber
=
NULL
;
switch_xml_t
xml
=
*
pxml
,
params
=
NULL
,
vars
=
NULL
,
cur
=
NULL
;
LDAPMessage
*
msg
,
*
entry
;
static
char
*
fsattr
[]
=
{
"id"
,
"mailbox"
,
"password"
,
"vm-password"
,
"email-addr"
,
"vm-email-all-messages"
,
"vm-delete-file"
,
"vm-attach-file"
,
NULL
};
basedn
=
switch_mprintf
(
binding
->
basedn
,
dir_domain
);
filter
=
switch_mprintf
(
binding
->
filter
,
dir_exten
);
xml_ldap_attribute_t
*
attr
=
NULL
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"searching in basedn %s with filter %s
\n
"
,
basedn
,
filter
);
if
((
ldap_search_s
(
ld
,
basedn
,
LDAP_SCOPE_SUB
,
filter
,
NULL
,
0
,
&
msg
)
!=
LDAP_SUCCESS
))
goto
cleanup
;
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
ret
=
SWITCH_STATUS_SUCCESS
;
xml
=
switch_xml_add_child_d
(
xml
,
"section"
,
off
++
);
switch_xml_set_attr_d
(
xml
,
"name"
,
"directory"
);
xml
=
switch_xml_add_child_d
(
xml
,
"domain"
,
off
++
);
switch_xml_set_attr_d
(
xml
,
"name"
,
dir_domain
);
xml
=
switch_xml_add_child_d
(
xml
,
"user"
,
off
++
);
vars
=
switch_xml_add_child_d
(
xml
,
"variables"
,
off
++
);
params
=
switch_xml_add_child_d
(
xml
,
"params"
,
off
++
);
for
(
entry
=
ldap_first_entry
(
ld
,
msg
);
entry
!=
NULL
;
entry
=
ldap_next_entry
(
ld
,
entry
))
{
for
(
key
=
ldap_first_attribute
(
ld
,
entry
,
&
ber
);
key
!=
NULL
;
key
=
ldap_next_attribute
(
ld
,
entry
,
ber
))
{
for
(
attr
=
binding
->
attr_list
;
attr
;
attr
=
attr
->
next
)
{
if
(
strlen
(
key
)
==
attr
->
len
)
{
if
(
!
strncasecmp
(
attr
->
val
,
key
,
strlen
(
key
)))
{
val
=
ldap_get_values
(
ld
,
entry
,
key
);
if
(
ldap_count_values
(
val
)
==
1
)
{
if
(
attr
->
type
<
2
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"setting %s = %s "
,
fsattr
[
attr
->
type
],
val
[
0
]);
switch_xml_set_attr_d
(
xml
,
fsattr
[
attr
->
type
],
val
[
0
]);
}
else
if
(
attr
->
type
<
8
)
{
cur
=
switch_xml_add_child_d
(
params
,
"param"
,
0
);
switch_xml_set_attr_d
(
cur
,
fsattr
[
attr
->
type
],
val
[
0
]);
}
else
{
cur
=
switch_xml_add_child_d
(
vars
,
"variable"
,
0
);
switch_xml_set_attr_d
(
cur
,
fsattr
[
attr
->
type
],
val
[
0
]);
}
}
else
{
/* multi val attrs */
}
ldap_value_free
(
val
);
continue
;
}
}
}
ldap_memfree
(
key
);
}
ber_free
(
ber
,
0
);
}
ldap_msgfree
(
entry
);
ldap_msgfree
(
msg
);
}
else
{
ret
=
SWITCH_STATUS_FALSE
;
}
cleanup
:
switch_safe_free
(
filter
);
switch_safe_free
(
basedn
)
switch_safe_free
(
dir_exten
);
switch_safe_free
(
dir_domain
);
return
ret
;
}
static
switch_status_t
trysearch
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
basedn
,
char
*
filter
)
{
switch_status_t
ret
;
int
off
=
*
xoff
;
char
*
key
=
NULL
;
char
*
dn
=
NULL
;
char
**
val
=
NULL
;
BerElement
*
ber
=
NULL
;
switch_xml_t
xml
=
*
pxml
;
LDAPMessage
*
msg
,
*
entry
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"trying search in base %s with filter %s
\n
"
,
basedn
,
filter
);
if
((
ldap_search_s
(
ld
,
basedn
,
LDAP_SCOPE_ONE
,
filter
,
NULL
,
0
,
&
msg
)
!=
LDAP_SUCCESS
))
goto
cleanup
;
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
ret
=
SWITCH_STATUS_SUCCESS
;
for
(
entry
=
ldap_first_entry
(
ld
,
msg
);
entry
!=
NULL
;
entry
=
ldap_next_entry
(
ld
,
entry
))
{
val
=
ldap_get_values
(
ld
,
entry
,
"fstag"
);
xml
=
switch_xml_add_child_d
(
xml
,
val
[
0
],
off
);
ldap_value_free
(
val
);
for
(
key
=
ldap_first_attribute
(
ld
,
entry
,
&
ber
);
key
!=
NULL
;
key
=
ldap_next_attribute
(
ld
,
entry
,
ber
))
{
if
(
!
strncasecmp
(
key
,
"fstag"
,
strlen
(
key
))
||
!
strncasecmp
(
key
,
"objectclass"
,
strlen
(
key
)))
{
ldap_memfree
(
key
);
continue
;
}
val
=
ldap_get_values
(
ld
,
entry
,
key
);
switch_xml_set_attr_d
(
xml
,
key
,
val
[
0
]);
ldap_memfree
(
key
);
ldap_value_free
(
val
);
}
ber_free
(
ber
,
0
);
dn
=
ldap_get_dn
(
ld
,
entry
);
rec
(
&
xml
,
&
off
,
ld
,
dn
);
*
xoff
=
1
;
}
ldap_msgfree
(
entry
);
ldap_msgfree
(
msg
);
}
else
{
ret
=
SWITCH_STATUS_FALSE
;
}
cleanup
:
switch_safe_free
(
basedn
);
switch_safe_free
(
filter
);
switch_safe_free
(
key
);
return
ret
;
}
void
rec
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
dn
)
{
int
off
=
*
xoff
;
char
*
key
;
char
**
val
;
switch_xml_t
xml
=
*
pxml
,
new
;
LDAPMessage
*
msg
,
*
entry
;
BerElement
*
ber
;
ldap_search_s
(
ld
,
dn
,
LDAP_SCOPE_ONE
,
NULL
,
NULL
,
0
,
&
msg
);
switch_safe_free
(
dn
);
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
for
(
entry
=
ldap_first_entry
(
ld
,
msg
);
entry
!=
NULL
;
entry
=
ldap_next_entry
(
ld
,
entry
))
{
val
=
ldap_get_values
(
ld
,
entry
,
"fstag"
);
new
=
switch_xml_add_child_d
(
xml
,
val
[
0
],
off
);
ldap_value_free
(
val
);
for
(
key
=
ldap_first_attribute
(
ld
,
entry
,
&
ber
);
key
!=
NULL
;
key
=
ldap_next_attribute
(
ld
,
entry
,
ber
))
{
if
(
!
strncasecmp
(
"fstag"
,
key
,
5
)
||
!
strncasecmp
(
"objectclass"
,
key
,
10
))
{
ldap_memfree
(
key
);
continue
;
}
val
=
ldap_get_values
(
ld
,
entry
,
key
);
switch_xml_set_attr_d
(
new
,
key
,
val
[
0
]);
ldap_memfree
(
key
);
ldap_value_free
(
val
);
}
ber_free
(
ber
,
0
);
rec
(
&
new
,
xoff
,
ld
,
ldap_get_dn
(
ld
,
entry
));
}
ldap_msgfree
(
entry
);
}
ldap_msgfree
(
msg
);
}
static
switch_xml_t
xml_ldap_search
(
const
char
*
section
,
const
char
*
tag_name
,
const
char
*
key_name
,
const
char
*
key_value
,
switch_event_t
*
params
,
void
*
user_data
)
{
xml_binding_t
*
binding
=
(
xml_binding_t
*
)
user_data
;
switch_event_header_t
*
hi
;
switch_status_t
ret
=
SWITCH_STATUS_FALSE
;
int
desired_version
=
LDAP_VERSION3
;
int
auth_method
=
LDAP_AUTH_SIMPLE
;
char
*
basedn
=
NULL
,
*
filter
=
NULL
;
char
*
dir_domain
=
NULL
,
*
dir_exten
=
NULL
;
char
*
buf
;
buf
=
malloc
(
4096
);
LDAP
*
ld
;
switch_xml_t
xml
=
NULL
;
int
xoff
=
0
;
xml
=
switch_xml_new
(
"document"
);
switch_xml_set_attr_d
(
xml
,
"type"
,
"freeswitch/xml"
);
if
(
params
)
{
if
((
hi
=
params
->
headers
))
{
for
(;
hi
;
hi
=
hi
->
next
)
{
switch
(
binding
->
bt
)
{
case
XML_LDAP_CONFIG
:
break
;
case
XML_LDAP_DIRECTORY
:
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"from cb got %s=%s
\n
"
,
hi
->
name
,
hi
->
value
);
if
(
!
strncmp
(
hi
->
name
,
"user"
,
strlen
(
hi
->
name
)))
{
dir_exten
=
strdup
(
hi
->
value
);
}
else
if
(
!
strncmp
(
hi
->
name
,
"domain"
,
strlen
(
hi
->
name
)))
{
dir_domain
=
strdup
(
hi
->
value
);
}
break
;
case
XML_LDAP_DIALPLAN
:
case
XML_LDAP_PHRASE
:
break
;
}
}
}
}
if
((
ldap_initialize
(
&
ld
,
binding
->
url
))
!=
LDAP_SUCCESS
)
goto
cleanup
;
if
((
ldap_set_option
(
ld
,
LDAP_OPT_PROTOCOL_VERSION
,
&
desired_version
))
!=
LDAP_SUCCESS
)
goto
cleanup
;
if
((
ldap_bind_s
(
ld
,
binding
->
binddn
,
binding
->
bindpass
,
auth_method
))
!=
LDAP_SUCCESS
)
goto
cleanup
;
switch
(
binding
->
bt
)
{
case
XML_LDAP_CONFIG
:
xml
=
switch_xml_add_child_d
(
xml
,
"section"
,
xoff
++
);
switch_xml_set_attr_d
(
xml
,
"name"
,
"configuration"
);
filter
=
switch_mprintf
(
binding
->
filter
,
key_name
,
key_value
);
basedn
=
switch_mprintf
(
binding
->
basedn
,
tag_name
);
ret
=
trysearch
(
&
xml
,
&
xoff
,
ld
,
basedn
,
filter
);
break
;
case
XML_LDAP_DIRECTORY
:
ret
=
trydir
(
&
xml
,
&
xoff
,
ld
,
dir_domain
,
dir_exten
,
binding
);
break
;
case
XML_LDAP_DIALPLAN
:
break
;
case
XML_LDAP_PHRASE
:
break
;
}
cleanup
:
ldap_unbind_s
(
ld
);
switch_xml_toxml_buf
(
xml
,
buf
,
0
,
0
,
1
);
printf
(
"providing:
\n
%s
\n
"
,
buf
);
switch_safe_free
(
buf
);
if
(
ret
!=
SWITCH_STATUS_SUCCESS
)
{
switch_xml_free
(
xml
);
return
NULL
;
}
return
xml
;
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论