Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
adab9ce3
提交
adab9ce3
authored
10月 11, 2008
作者:
John Skopis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
oops
git-svn-id:
http://svn.freeswitch.org/svn/freeswitch/trunk@9979
d0543943-73ff-0310-b7d9-9358b9ac24b2
上级
00c3b962
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
399 行增加
和
240 行删除
+399
-240
mod_xml_ldap.c
src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c
+399
-240
没有找到文件。
src/mod/xml_int/mod_xml_ldap/mod_xml_ldap.c
浏览文件 @
adab9ce3
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthmct@yahoo.com>
* Bret McDanel <trixter AT 0xdecafbad.com>
* Justin Cassidy <xachenant@hotmail.com>
* John Skopis <john+fs@skopis.com>
*
* mod_xml_ldap.c -- LDAP XML Gateway
*
*/
#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>
#
define PCACHE_TTL 300
#
include "lutil_ldap.h"
#
define NCACHE_TTL 900
#
endif
typedef
enum
{
typedef
enum
{
XML_LDAP_CONFIG
=
0
,
XML_LDAP_CONFIG
=
0
,
...
@@ -14,344 +53,464 @@ typedef enum {
...
@@ -14,344 +53,464 @@ typedef enum {
XML_LDAP_PHRASE
XML_LDAP_PHRASE
}
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
;
xml_ldap_query_type_t
bt
;
char
*
host
;
char
*
url
;
char
*
basedn
;
char
*
basedn
;
char
*
binddn
;
char
*
binddn
;
char
*
bindpass
;
char
*
bindpass
;
char
*
filter
;
char
*
filter
;
char
**
attrs
;
lutilSASLdefaults
*
defaults
;
}
xml_binding_t
;
}
xml_binding_t
;
typedef
struct
ldap_c
{
LDAP
*
ld
;
LDAPMessage
*
msg
;
LDAPMessage
*
entry
;
BerElement
*
berkey
;
BerElement
*
berval
;
char
*
key
;
char
*
val
;
char
**
keyvals
;
char
**
valvals
;
char
*
sp
;
}
ldap_ctx
;
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
);
static
switch_status_t
xml_ldap_directory_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
);
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
);
static
switch_status_t
xml_ldap_dialplan_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
);
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
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 ""
#define XML_LDAP_SYNTAX "
[debug_on|debug_off]
"
SWITCH_STANDARD_API
(
xml_ldap_function
)
SWITCH_STANDARD_API
(
xml_ldap_function
)
{
{
if
(
session
)
{
return
SWITCH_STATUS_FALSE
;
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
);
if
(
switch_strlen_zero
(
cmd
))
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_INFO
,
"XML LDAP module loading...
\n
"
);
goto
usage
;
}
if
(
do_config
()
!=
SWITCH_STATUS_SUCCESS
)
{
if
(
!
strcasecmp
(
cmd
,
"debug_on"
))
{
return
SWITCH_STATUS_FALSE
;
}
else
if
(
!
strcasecmp
(
cmd
,
"debug_off"
))
{
}
else
{
goto
usage
;
}
}
stream
->
write_function
(
stream
,
"OK
\n
"
);
return
SWITCH_STATUS_SUCCESS
;
/* indicate that the module should continue to be loaded */
usage
:
stream
->
write_function
(
stream
,
"USAGE: %s
\n
"
,
XML_LDAP_SYNTAX
);
return
SWITCH_STATUS_SUCCESS
;
return
SWITCH_STATUS_SUCCESS
;
}
}
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
)
static
switch_status_t
xml_ldap_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
,
const
xml_ldap_query_type_t
query_type
)
{
{
return
SWITCH_STATUS_SUCCESS
;
switch
(
query_type
)
{
case
XML_LDAP_DIRECTORY
:
return
xml_ldap_directory_result
(
ldap_connection
,
binding
,
xml
,
off
);
case
XML_LDAP_DIALPLAN
:
return
xml_ldap_dialplan_result
(
ldap_connection
,
binding
,
xml
,
off
);
default
:
return
SWITCH_STATUS_FALSE
;
}
}
}
static
switch_status_t
do_config
(
void
)
{
static
switch_status_t
xml_ldap_dialplan_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
)
char
*
cf
=
"xml_ldap.conf"
;
{
switch_xml_t
cfg
,
xml
,
bindings_tag
,
binding_tag
,
param
;
return
SWITCH_STATUS_FALSE
;
xml_binding_t
*
binding
=
NULL
;
}
int
x
=
0
;
if
(
!
(
xml
=
switch_xml_open_cfg
(
cf
,
&
cfg
,
NULL
)))
{
static
switch_status_t
xml_ldap_directory_result
(
void
*
ldap_connection
,
xml_binding_t
*
binding
,
switch_xml_t
*
xml
,
int
*
off
)
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"open of %s failed
\n
"
,
cf
);
{
return
SWITCH_STATUS_TERM
;
struct
ldap_c
*
ldap
=
ldap_connection
;
switch_xml_t
asdf
=
*
xml
;
switch_xml_t
param
,
variable
,
params
=
NULL
,
variables
=
NULL
;
int
i
=
0
;
int
loff
=
*
off
;
for
(
ldap
->
entry
=
ldap_first_entry
(
ldap
->
ld
,
ldap
->
msg
);
ldap
->
entry
!=
NULL
;
ldap
->
entry
=
ldap_next_entry
(
ldap
->
ld
,
ldap
->
entry
))
{
ldap
->
key
=
ldap_first_attribute
(
ldap
->
ld
,
ldap
->
entry
,
&
ldap
->
berkey
);
do
{
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
++
);
}
}
if
(
!
(
bindings_tag
=
switch_xml_child
(
cfg
,
"bindings"
)))
{
ldap
->
keyvals
=
ldap_get_values
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
key
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Missing <bindings> tag!
\n
"
);
ldap
->
valvals
=
ldap_get_values
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
val
);
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
)
{
if
(
ldap
->
keyvals
)
{
char
*
bname
=
(
char
*
)
switch_xml_attr_soft
(
binding_tag
,
"name"
);
ldap_value_free
(
ldap
->
keyvals
);
}
if
(
!
(
binding
=
malloc
(
sizeof
(
*
binding
))))
{
if
(
ldap
->
valvals
)
{
goto
done
;
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
);
}
}
memset
(
binding
,
0
,
sizeof
(
*
binding
));
for
(
param
=
switch_xml_child
(
binding_tag
,
"param"
);
param
;
param
=
param
->
next
)
{
ldap
->
val
=
ldap_next_attribute
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
berval
);
char
*
var
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"name"
);
}
while
(
ldap
->
val
!=
NULL
);
char
*
val
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"value"
);
if
(
!
strcasecmp
(
var
,
"filter"
))
{
if
(
ldap
->
key
)
{
binding
->
bindings
=
(
char
*
)
switch_xml_attr_soft
(
param
,
"bindings"
);
ldap_memfree
(
ldap
->
key
);
}
if
(
!
strncmp
(
binding
->
bindings
,
"configuration"
,
strlen
(
binding
->
bindings
)))
{
if
(
ldap
->
berval
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"setting type XML_LDAP_CONFIG
\n
"
);
ber_free
(
ldap
->
berval
,
0
);
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
)
{
ldap
->
key
=
ldap_next_attribute
(
ldap
->
ld
,
ldap
->
entry
,
ldap
->
berkey
);
binding
->
filter
=
strdup
(
val
);
printf
(
"binding filter %s to %s
\n
"
,
binding
->
filter
,
binding
->
bindings
);
}
while
(
ldap
->
key
!=
NULL
);
if
(
ldap
->
berkey
)
{
ber_free
(
ldap
->
berkey
,
0
);
}
}
}
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
);
}
}
return
SWITCH_STATUS_SUCCESS
;
}
}
if
(
!
binding
->
basedn
||
!
binding
->
filter
||
!
binding
->
url
)
{
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
,
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
void
*
user_data
)
"You must define
\"
basedn
\"
, and
\"
filter
\"
in mod_xml_ldap.conf.xml
\n
"
);
{
continue
;
xml_binding_t
*
binding
=
(
xml_binding_t
*
)
user_data
;
}
switch_event_header_t
*
hi
;
switch_xml_t
xml
=
NULL
,
sub
=
NULL
;
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"Binding [%s] XML Fetch Function [%s] (%s) [%s]
\n
"
,
struct
ldap_c
ldap_connection
;
switch_strlen_zero
(
bname
)
?
"N/A"
:
bname
,
binding
->
basedn
,
binding
->
filter
,
binding
->
bindings
?
binding
->
bindings
:
"all"
)
;
struct
ldap_c
*
ldap
=
&
ldap_connection
;
switch_xml_bind_search_function
(
xml_ldap_search
,
switch_xml_parse_section_string
(
bname
),
binding
);
int
auth_method
=
LDAP_AUTH_SIMPLE
;
int
desired_version
=
LDAP_VERSION3
;
xml_ldap_query_type_t
query_type
;
char
*
dir_exten
=
NULL
,
*
dir_domain
=
NULL
;
x
++
;
char
*
search_filter
=
NULL
,
*
search_base
=
NULL
;
binding
=
NULL
;
int
off
=
0
,
ret
=
1
;
//char *buf;
//buf = malloc(4096);
if
(
!
binding
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"No bindings...sorry bud returning now
\n
"
);
return
NULL
;
}
}
done
:
if
(
!
strcmp
(
section
,
"configuration"
))
{
switch_xml_free
(
xml
);
query_type
=
XML_LDAP_CONFIG
;
}
else
if
(
!
strcmp
(
section
,
"directory"
))
{
query_type
=
XML_LDAP_DIRECTORY
;
}
else
if
(
!
strcmp
(
section
,
"dialplan"
))
{
query_type
=
XML_LDAP_DIALPLAN
;
}
else
if
(
!
strcmp
(
section
,
"phrases"
))
{
query_type
=
XML_LDAP_PHRASE
;
}
else
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Invalid section
\n
"
);
return
NULL
;
}
return
SWITCH_STATUS_SUCCESS
;
if
(
params
)
{
}
if
((
hi
=
params
->
headers
))
{
for
(;
hi
;
hi
=
hi
->
next
)
{
switch
(
query_type
)
{
case
XML_LDAP_CONFIG
:
break
;
static
switch_status_t
trysearch
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
basedn
,
char
*
filter
)
{
case
XML_LDAP_DIRECTORY
:
switch_status_t
ret
;
if
(
!
strcmp
(
hi
->
name
,
"user"
))
{
int
off
=
*
xoff
;
dir_exten
=
strdup
(
hi
->
value
);
char
*
key
=
NULL
;
}
else
if
(
!
strcmp
(
hi
->
name
,
"domain"
))
{
char
*
dn
=
NULL
;
dir_domain
=
strdup
(
hi
->
value
);
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
);
ldap_search_s
(
ld
,
basedn
,
LDAP_SCOPE_ONE
,
filter
,
NULL
,
0
,
&
msg
);
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
;
}
}
break
;
val
=
ldap_get_values
(
ld
,
entry
,
key
);
case
XML_LDAP_DIALPLAN
:
switch_xml_set_attr_d
(
xml
,
key
,
val
[
0
]);
case
XML_LDAP_PHRASE
:
break
;
}
}
switch
(
query_type
)
{
case
XML_LDAP_CONFIG
:
break
;
ldap_memfree
(
key
);
case
XML_LDAP_DIRECTORY
:
ldap_value_free
(
val
);
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"
);
}
}
ber_free
(
ber
,
0
);
dn
=
ldap_get_dn
(
ld
,
entry
);
if
((
sub
=
switch_xml_add_child_d
(
sub
,
"domain"
,
off
++
)))
{
rec
(
&
xml
,
&
off
,
ld
,
dn
);
switch_xml_set_attr_d
(
sub
,
"name"
,
dir_domain
);
}
*
xoff
=
1
;
if
((
sub
=
switch_xml_add_child_d
(
sub
,
"user"
,
off
++
)))
{
switch_xml_set_attr_d
(
sub
,
"id"
,
dir_exten
);
}
}
ldap_msgfree
(
entry
);
ldap_msgfree
(
msg
);
}
else
{
ret
=
SWITCH_STATUS_FALSE
;
}
}
switch_safe_free
(
filter
);
search_filter
=
switch_mprintf
(
binding
->
filter
,
dir_exten
);
switch_safe_free
(
key
);
search_base
=
switch_mprintf
(
binding
->
basedn
,
dir_domain
);
return
ret
;
free
(
dir_exten
);
dir_exten
=
NULL
;
}
free
(
dir_domain
);
dir_domain
=
NULL
;
}
else
{
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
,
dir_domain
);
goto
cleanup
;
}
break
;
case
XML_LDAP_DIALPLAN
:
if
((
xml
=
switch_xml_new
(
"document"
)))
{
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"
,
"dialplan"
);
}
void
rec
(
switch_xml_t
*
pxml
,
int
*
xoff
,
LDAP
*
ld
,
char
*
dn
)
{
sub
=
switch_xml_add_child_d
(
xml
,
"context"
,
off
++
);
int
off
=
*
xoff
;
}
char
*
key
;
char
**
val
;
break
;
case
XML_LDAP_PHRASE
:
break
;
}
}
else
{
goto
cleanup
;
}
}
switch_xml_t
xml
=
*
pxml
,
new
;
LDAPMessage
*
msg
,
*
entry
;
BerElement
*
ber
;
ldap_search_s
(
ld
,
dn
,
LDAP_SCOPE_ONE
,
NULL
,
NULL
,
0
,
&
msg
);
if
((
ldap
->
ld
=
(
LDAP
*
)
ldap_init
(
binding
->
host
,
LDAP_PORT
))
==
NULL
)
{
switch_safe_free
(
dn
);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Unable to connect to ldap server.%s
\n
"
,
binding
->
host
);
goto
cleanup
;
}
if
(
ldap_count_entries
(
ld
,
msg
)
>
0
)
{
if
(
ldap_set_option
(
ldap
->
ld
,
LDAP_OPT_PROTOCOL_VERSION
,
&
desired_version
)
!=
LDAP_OPT_SUCCESS
)
{
goto
cleanup
;
}
for
(
ldap_set_option
(
ldap
->
ld
,
LDAP_OPT_X_SASL_SECPROPS
,
&
ldap
->
sp
);
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
)
)
{
if
(
binding
->
binddn
)
{
ldap_memfree
(
key
);
if
(
ldap_bind_s
(
ldap
->
ld
,
binding
->
binddn
,
binding
->
bindpass
,
auth_method
)
!=
LDAP_SUCCESS
)
{
continue
;
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
)
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
;
}
}
}
val
=
ldap_get_values
(
ld
,
entry
,
key
);
if
(
ldap_search_s
(
ldap
->
ld
,
search_base
,
LDAP_SCOPE_SUBTREE
,
search_filter
,
NULL
,
0
,
&
ldap
->
msg
)
!=
LDAP_SUCCESS
)
{
switch_xml_set_attr_d
(
new
,
key
,
val
[
0
]);
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Query failed: -b
\"
%s
\"
\"
%s
\"\n
"
,
search_base
,
search_filter
);
ldap_memfree
(
key
);
goto
cleanup
;
ldap_value_free
(
val
);
}
}
ber_free
(
ber
,
0
);
rec
(
&
new
,
xoff
,
ld
,
ldap_get_dn
(
ld
,
entry
)
);
if
(
ldap_count_entries
(
ldap
->
ld
,
ldap
->
msg
)
<=
0
)
{
goto
cleanup
;
}
if
(
sub
&&
xml_ldap_result
(
&
ldap_connection
,
binding
,
&
sub
,
&
off
,
query_type
)
!=
SWITCH_STATUS_SUCCESS
)
{
goto
cleanup
;
}
}
ldap_msgfree
(
entry
)
;
ret
=
0
;
cleanup
:
if
(
ldap
->
msg
)
{
ldap_msgfree
(
ldap
->
msg
);
}
}
ldap_msgfree
(
msg
);
}
if
(
ldap
->
ld
)
{
ldap_unbind_s
(
ldap
->
ld
);
}
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
,
switch_safe_free
(
search_filter
);
void
*
user_data
)
{
switch_safe_free
(
search_base
);
xml_binding_t
*
binding
=
(
xml_binding_t
*
)
user_data
;
//switch_xml_toxml_buf(xml,buf,0,0,1)
;
switch_event_header_t
*
hi
;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Providing:\n%s\n", buf)
;
int
desired_version
=
LDAP_VERSION3
;
if
(
ret
)
{
int
auth_method
=
LDAP_AUTH_SIMPLE
;
switch_xml_free
(
xml
);
return
NULL
;
}
char
*
basedn
=
NULL
,
*
filter
=
NULL
;
return
xml
;
char
*
dir_domain
=
NULL
,
*
dir_exten
=
NULL
;
}
char
*
buf
;
buf
=
malloc
(
4096
);
LDAP
*
ld
;
static
switch_status_t
do_config
(
void
)
switch_xml_t
xml
=
NULL
;
{
char
*
cf
=
"xml_ldap.conf"
;
switch_xml_t
cfg
,
xml
,
bindings_tag
,
binding_tag
,
param
;
xml_binding_t
*
binding
=
NULL
;
int
x
=
0
;
int
xoff
=
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
(
params
)
{
if
(
!
(
bindings_tag
=
switch_xml_child
(
cfg
,
"bindings"
)))
{
if
((
hi
=
params
->
headers
))
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"Missing <bindings> tag!
\n
"
);
for
(;
hi
;
hi
=
hi
->
next
)
{
goto
done
;
switch
(
binding
->
bt
)
{
}
case
XML_LDAP_CONFIG
:
break
;
case
XML_LDAP_DIRECTORY
:
for
(
binding_tag
=
switch_xml_child
(
bindings_tag
,
"binding"
);
binding_tag
;
binding_tag
=
binding_tag
->
next
)
{
if
(
!
strncmp
(
hi
->
name
,
"user"
,
strlen
(
hi
->
name
)))
{
char
*
bname
=
(
char
*
)
switch_xml_attr_soft
(
binding_tag
,
"name"
);
dir_exten
=
strdup
(
hi
->
value
);
}
else
if
(
!
strncmp
(
hi
->
name
,
"domain"
,
strlen
(
hi
->
name
)))
{
if
(
!
(
binding
=
malloc
(
sizeof
(
*
binding
)
)))
{
dir_domain
=
strdup
(
hi
->
value
)
;
goto
done
;
}
}
break
;
memset
(
binding
,
0
,
sizeof
(
*
binding
))
;
case
XML_LDAP_DIALPLAN
:
if
(
!
(
binding
->
defaults
=
malloc
(
sizeof
(
lutilSASLdefaults
))))
{
case
XML_LDAP_PHRASE
:
goto
done
;
break
;
}
}
memset
(
binding
->
defaults
,
0
,
sizeof
(
lutilSASLdefaults
));
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
(
val
)
{
binding
->
filter
=
strdup
(
val
);
}
}
}
else
if
(
!
strcasecmp
(
var
,
"basedn"
))
{
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
);
}
}
}
}
switch
(
binding
->
bt
)
{
case
XML_LDAP_CONFIG
:
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
"humm %s"
,
binding
->
filter
);
filter
=
switch_mprintf
(
binding
->
filter
,
key_name
,
key_value
);
basedn
=
switch_mprintf
(
binding
->
basedn
,
tag_name
);
break
;
case
XML_LDAP_DIRECTORY
:
if
(
!
binding
->
basedn
||
!
binding
->
filter
)
{
if
(
!
dir_exten
)
{
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_ERROR
,
filter
=
switch_mprintf
(
binding
->
filter
,
"objectclass"
,
"*"
,
"(!(objectclass=fsuser))"
);
"You must define
\"
basedn
\"
, and
\"
filter
\"
in mod_xml_ldap.conf.xml
\n
"
);
basedn
=
switch_mprintf
(
binding
->
basedn
,
dir_domain
);
continue
;
}
else
{
filter
=
switch_mprintf
(
binding
->
filter
,
key_name
,
key_value
,
"object_class=*"
);
basedn
=
switch_mprintf
(
binding
->
basedn
,
dir_domain
);
}
}
break
;
case
XML_LDAP_DIALPLAN
:
break
;
case
XML_LDAP_PHRASE
:
switch_log_printf
(
SWITCH_CHANNEL_LOG
,
SWITCH_LOG_NOTICE
,
"Binding [%s] XML Fetch Function [%s] (%s) [%s]
\n
"
,
break
;
switch_strlen_zero
(
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
);
ldap_initialize
(
&
ld
,
binding
->
url
)
;
x
++
;
ldap_set_option
(
ld
,
LDAP_OPT_PROTOCOL_VERSION
,
&
desired_version
)
;
binding
=
NULL
;
ldap_bind_s
(
ld
,
binding
->
binddn
,
binding
->
bindpass
,
auth_method
);
}
xml
=
switch_xml_new
(
"document"
);
done
:
switch_xml_
set_attr_d
(
xml
,
"type"
,
"freeswitch/xml"
);
switch_xml_
free
(
xml
);
return
SWITCH_STATUS_SUCCESS
;
}
trysearch
(
&
xml
,
&
xoff
,
ld
,
basedn
,
filter
);
SWITCH_MODULE_LOAD_FUNCTION
(
mod_xml_ldap_load
)
{
switch_api_interface_t
*
xml_ldap_api_interface
;
ldap_unbind_s
(
ld
);
/* 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
;
}
switch_xml_toxml_buf
(
xml
,
buf
,
0
,
0
,
1
);
/* indicate that the module should continue to be loaded */
printf
(
"providing:
\n
%s
\n
"
,
buf
)
;
return
SWITCH_STATUS_SUCCESS
;
switch_safe_free
(
buf
);
}
return
xml
;
SWITCH_MODULE_SHUTDOWN_FUNCTION
(
mod_xml_ldap_shutdown
)
{
return
SWITCH_STATUS_SUCCESS
;
}
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
*/
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论