使用openssl中的MD5函数,该函数返回16字节的unsigned char类型的数据,每个字节的范围都在0~255间,把
它格式化为十六进制就是32位md5编码。注:一个字节为8位,正好可以表示2位的十六进制。
使用登录客户端的用户名从Redis数据库中得到salt值和加密后的密码,然后把登录客户端的密码经过salt加密后,与
Redis数据库中的密码进行比较。相同则验证通过,否则验证失败。
Redis数据库中密码的存储格式为password:salt
用户验证算法如下:
int user_authenticate(char *username, char *password)
{
char *salt_pw, *salt, *pw;
char buf[40];
char tmp[3]={'\0'}, md5_str[33]={'\0'};
unsigned char md[16];
int i;
//get_salt_pw调用Redis数据库获得password:salt
salt_pw = get_salt_pw(db, username);
pw = strtok(salt_pw, ":");
if(!pw){
return 0;
}
salt = strtok(NULL, ":");
if(!salt){
return 0;
}
strcpy(buf, password);
strcat(buf, salt);
MD5((const unsigned char*)buf, strlen(buf), md);
//transform to md5 string
for(i = 0; i < 16; i++){
sprintf(tmp, "%02x", md[i]);
strcat(md5_str, tmp);
}
//compare encode password using md5
if(strcmp((char*)md5_str, pw)){
return 0;
}
return 1;
}
其中要注意strtok函数的使用,以及16字节的unsigned char转换为32位十六进制数的过程。
1.源代码下载
1.1 opensips源码包下载
最新的opensips下载位置:http://opensips.org/pub/opensips/latest/src/
所有版本的opensips下载位置:http://opensips.org/pub/opensips/
1.2 opensips依赖包下载
源代码安装软件要注意查看README,INSTALL等文件,这些文件里有很重要的说明和安装信息。
在INSTALL文件中有opensips所依赖的软件包的说明,这里仅缺少如下几个软件包:
1) bison or yacc (Berkley yacc)
2) flex
3) libncurses5-dev and m4
缺少什么软件包可以选择在线安装或下载源代码包安装。
$apt-cache search xxxx 搜索软件包
$apt-get install xxxxx 安装软件包
./configure,make,sudo make install 源代码安装的一般步骤
1.2.1 bison
bison的安装依赖m4, m4 是一个宏处理器,将输入拷贝到输出,同时将宏展开。
m4下载地址为:ftp://ftp.gnu.org/gnu/m4/
GNU http://www.gnu.org/software/bison/ 中对bison的介绍。
下载源代码包地址:http://ftp.gnu.org/gnu/bison/
安装bison很简单:./configure,make,sudo make install
1.2.2 flex
使用google搜索linux flex,不要用baidu!
flex是最快的词法分析器。flex:The Fast Lexical Analyzer,网址:http://flex.sourceforge.net/
如下链接中说明了在ubuntu系统中安装flex的具体过程。flex也依赖m4宏处理包,同上。
http://www.geeksww.com/tutorials/operating_systems/linux/installation/installing_flex_fast_lexical_analyzer_ubuntu_linux.php
选择一个版本下载后,解压安装,./configure,make,sudo make install
以上就是我安装opensips的过程中缺少的包,如果还缺少什么包都会提示的,下载安装即可。
2.opensips安装
2.1 安装
INSTALL文件中有关于opensips安装的说明:
最简单的安装方法:
$make all
$sudo make install
注意事项:如果在安装的过程中使用了prefix选项,那就必须要在前面的编译中也使用prefix选项,并且使用的目录
也是相同的。如果没有按照这样做的话,那就会导致opensips找不到默认的配置文件,也就是说opensips会在错误的
目录中寻找配置文件。因为中编译阶段,默认的配置文件目录已经被硬编码到opensips中。
默认安装的位置为/usr/local,而使用了prefix则指定了安装目录。
1)一个错误的例子: make all make prefix=/ install 2)正确的使用方法: make prefix=/ all make prefix=/ install 2.2 卸载方法(个人想法)
如果将opensips安装在/usr/local下,则与opensips相关的文件所在的位置如下所示: /usr/local/etc/opensips /usr/local/sbin/opensips* /usr/local/lib/opensips 此时如果想把opensips安装在根目录下 / ,则需要卸载opensips,方法是删除以上目录中的文件。2.3 安装后目录结构
opensips安装之后的文件目录:
/sbin/中的可执行命令有如下:opensips 、opensipsctl 、 opensipsdbctl 、 opensipsunix
/etc/opensips/中的配置文件有:opensips.cfg、opensipsctlrc和osipsconsolerc
/lib/opensips/中的库文件有:modules/ 和 opensipsctl/两个目录。modules/ 为当前opensips所支持的模块,
opensipsctl/中是/sbin中启动命令要使用到的文件。
3.opensips启动
3.1 opensips命令启动
$sudo opensips3.2 opensipsctl命令启动
启动opensips命令: $sudo opensipsctl start
如果出现一下错误:
INFO: Starting OpenSIPS :
ERROR: PID file /var/run/opensips.pid does not exist -- OpenSIPS start failed
可以查看系统日志来检查失败的原因:
$tail –n 20 /var/log/syslog
1) 错误一:ERROR:core:daemonize: unable to create pid file /var/run/opensips.pid: Permission denied
解决方法:这个问题就是权限问题,在命令前加上sudo。
2) 错误二:ERROR:core:main:loading config file(/usr/local/etc/opensips/opensips.cfg):No such file or directory
分析:这个问题就是启动配置文件找不到。经过分析发现opensips从/usr/local/中读取默认配置文件,原因是
犯了2.1节中的错误,在编译时使用默认的路径/usr/local/,而在安装时使用prefix=/选项,这就导致了配置文件找不到。
解决方法:make clean, make prefix=/ all, sudo make prefix=/ install重新编译安装,然后启动opensips。
4.opensips配置文件
opensips的配置文件都在/etc/opensips/中,分别为opensips.cfg、opensipsctlrc和osipsconsolerc。
opensips.cfg文件主要用于opensips启动的配置,所有应用功能的配置都在这个文件中说明。该配置文件主要由
三个部分组成:
第一部分是全局变量,如:
listen=udp:127.0.0.1:5060
disable_tcp=yes
disable_tls=yes等。
第二部分主要用来加载模块,并设置相应参数,如:
loadmodule "db_mysql.so"
loadmodule "auth.so"
loadmodule "auth_db.so"
modparam("auth", "calculate_ha1", yes)
modparam("auth_db", "password_column", "password")等。
第三部分主要是路由策略和功能应用,如:
route[relay] {
# for INVITEs enable some additional helper routes
if (is_method("INVITE")) {
t_on_branch("per_branch_ops");
t_on_reply("handle_nat");
t_on_failure("missed_call");
}
……
}
opensipsctlrc文件中包含了数据库配置的信息。
opensips用户认证配置文件-opensips.cfg opensips.cfg配置文件,红色标记的为添加修改的部分,亲自测试!
#
# $Id: opensips.cfg 9742 2013-02-05 10:24:48Z vladut-paiu $
#
# OpenSIPS residential configuration script
# by OpenSIPS Solutions <team@opensips-solutions.com>
#
# This script was generated via "make menuconfig", from
# the "Residential" scenario.
# You can enable / disable more features / functionalities by
# re-generating the scenario with different options.#
#
# Please refer to the Core CookBook at:
# http://www.opensips.org/Resources/DocsCookbooks
# for a explanation of possible statements, functions and parameters.
#
####### Global Parameters #########
debug=3
log_stderror=no
log_facility=LOG_LOCAL0
fork=yes
children=4
/* uncomment the following lines to enable debugging */
#debug=6
#fork=no
#log_stderror=yes
/* uncomment the next line to enable the auto temporary blacklisting of
not available destinations (default disabled) */
#disable_dns_blacklist=no
/* uncomment the next line to enable IPv6 lookup after IPv4 dns
lookup failures (default disabled) */
#dns_try_ipv6=yes
/* comment the next line to enable the auto discovery of local aliases
based on revers DNS on IPs */
auto_aliases=no
# listen=udp:127.0.0.1:5060 # CUSTOMIZE ME
listen=udp:192.168.139.121:5060 # CUSTOMIZE ME
disable_tcp=yes
#disable_tls=yes
####### Modules Section ########
#set module path
mpath="//lib/opensips/modules/"
#### MySQL
loadmodule "db_mysql.so"
#### SIGNALING module
loadmodule "signaling.so"
#### StateLess module
loadmodule "sl.so"
#### Transaction Module
loadmodule "tm.so"
modparam("tm", "fr_timer", 5)
modparam("tm", "fr_inv_timer", 30)
modparam("tm", "restart_fr_on_each_reply", 0)
modparam("tm", "onreply_avp_mode", 1)
#### Record Route Module
loadmodule "rr.so"
/* do not append from tag to the RR (no need for this script) */
modparam("rr", "append_fromtag", 0)
#### MAX ForWarD module
loadmodule "maxfwd.so"
#### SIP MSG OPerationS module
loadmodule "sipmsgops.so"
#### FIFO Management Interface
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
modparam("mi_fifo", "fifo_mode", 0666)
#### URI module
loadmodule "uri.so"
modparam("uri", "use_uri_table", 0)
#### USeR LOCation module
loadmodule "usrloc.so"
modparam("usrloc", "nat_bflag", "NAT")
modparam("usrloc", "db_url", "mysql://opensips:opensipsrw@localhost/opensips")
modparam("usrloc", "db_mode", 2)
#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "tcp_persistent_flag", "TCP_PERSISTENT")
/* uncomment the next line not to allow more than 10 contacts per AOR */
#modparam("registrar", "max_contacts", 10)
#### ACCounting module
loadmodule "acc.so"
/* what special events should be accounted ? */
modparam("acc", "early_media", 0)
modparam("acc", "report_cancels", 0)
/* by default we do not adjust the direct of the sequential requests.
if you enable this parameter, be sure the enable "append_fromtag"
in "rr" module */
modparam("acc", "detect_direction", 0)
modparam("acc", "failed_transaction_flag", "ACC_FAILED")
/* account triggers (flags) */
modparam("acc", "log_flag", "ACC_DO")
modparam("acc", "log_missed_flag", "ACC_MISSED")
#### Auth support
loadmodule "auth.so"
loadmodule "auth_db.so"
modparam("auth", "calculate_ha1", yes)
modparam("auth_db", "db_url", "mysql://opensips:opensipsrw@localhost/opensips")
modparam("auth_db", "password_column", "password")
####### Routing Logic ########
# main request routing logic
route{
if (!mf_process_maxfwd_header("10")) {
sl_send_reply("483","Too Many Hops");
exit;
}
if (has_totag()) {
# sequential request withing a dialog should
# take the path determined by record-routing
if (loose_route()) {
if (is_method("BYE")) {
setflag(ACC_DO); # do accounting ...
setflag(ACC_FAILED); # ... even if the transaction fails
} else if (is_method("INVITE")) {
# even if in most of the cases is useless, do RR for
# re-INVITEs alos, as some buggy clients do change route set
# during the dialog.
record_route();
}
# route it out to whatever destination was set by loose_route()
# in $du (destination URI).
route(relay);
} else {
if ( is_method("ACK") ) {
if ( t_check_trans() ) {
# non loose-route, but stateful ACK; must be an ACK after
# a 487 or e.g. 404 from upstream server
t_relay();
exit;
} else {
# ACK without matching transaction ->
# ignore and discard
exit;
}
}
sl_send_reply("404","Not here");
}
exit;
}
# CANCEL processing
if (is_method("CANCEL"))
{
if (t_check_trans())
t_relay();
exit;
}
t_check_trans();
if ( !(is_method("REGISTER") ) ) {
if (from_uri==myself)
{
} else {
# if caller is not local, then called number must be local
if (!uri==myself) {
send_reply("403","Rely forbidden");
exit;
}
}
}
# preloaded route checking
if (loose_route()) {
xlog("L_ERR",
"Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]");
if (!is_method("ACK"))
sl_send_reply("403","Preload Route denied");
exit;
}
# record routing
if (!is_method("REGISTER|MESSAGE"))
record_route();
# account only INVITEs
if (is_method("INVITE")) {
setflag(ACC_DO); # do accounting
}
if (!uri==myself) {
append_hf("P-hint: outbound\r\n");
route(relay);
}
# requests for my domain
if (is_method("PUBLISH|SUBSCRIBE"))
{
sl_send_reply("503", "Service Unavailable");
exit;
}
if (is_method("REGISTER"))
{
#auth user using mysql DB
if (!www_authorize("192.168.139.121", "subscriber")) {
www_challenge("192.168.139.121", "0");
exit;
}
#end auth user
if ( 0 ) setflag(TCP_PERSISTENT);
if (!save("location"))
sl_reply_error();
exit;
}
if ($rU==NULL) {
# request with no Username in RURI
sl_send_reply("484","Address Incomplete");
exit;
}
# do lookup with method filtering
if (!lookup("location","m")) {
t_newtran();
t_reply("404", "Not Found");
exit;
}
# when routing via usrloc, log the missed calls also
setflag(ACC_MISSED);
route(relay);
}
route[relay] {
# for INVITEs enable some additional helper routes
if (is_method("INVITE")) {
t_on_branch("per_branch_ops");
t_on_reply("handle_nat");
#t_on_reply();
t_on_failure("missed_call");
}
if (!t_relay()) {
send_reply("500","Internal Error");
};
exit;
}
branch_route[per_branch_ops] {
xlog("new branch at $ru\n");
}
onreply_route[handle_nat] {
xlog("incoming reply\n");
}
failure_route[missed_call] {
if (t_was_cancelled()) {
exit;
}
# uncomment the following lines if you want to block client
# redirect based on 3xx replies.
##if (t_check_status("3[0-9][0-9]")) {
##t_reply("404","Not found");
## exit;
##}
}