1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*
* $Id: uac.c,v 1.12 2006/03/03 18:36:48 bogdan_iancu Exp $
*
* Copyright (C) 2005 Voice Sistem SRL
*
* This file is part of openser, a free SIP server.
*
* UAC OpenSER-module is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* UAC OpenSER-module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* History:
* ---------
* 2005-01-31 first version (ramona)
* 2005-08-12 some TM callbacks replaced with RR callback - more efficient;
* (bogdan)
* 2006-03-02 UAC authentication looks first in AVPs for credential (bogdan)
* 2006-03-03 the RR parameter is encrypted via XOR with a password
* (bogdan)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../error.h"
#include "../../items.h"
#include "../../mem/mem.h"
#include "../tm/tm_load.h"
#include "../tm/t_hooks.h"
#include "../rr/api.h"
#include "from.h"
#include "auth.h"
MODULE_VERSION
/* local variable used for init */
static char* from_restore_mode_str = NULL;
static char* auth_username_avp = NULL;
static char* auth_realm_avp = NULL;
static char* auth_password_avp = NULL;
/* global param variables */
str rr_param = str_init("vsf");
str uac_passwd = str_init("");
int from_restore_mode = FROM_AUTO_RESTORE;
struct tm_binds uac_tmb;
struct rr_binds uac_rrb;
xl_spec_t auth_username_spec;
xl_spec_t auth_realm_spec;
xl_spec_t auth_password_spec;
static int w_replace_from1(struct sip_msg* msg, char* str, char* str2);
static int w_replace_from2(struct sip_msg* msg, char* str, char* str2);
static int w_restore_from(struct sip_msg* msg, char* foo, char* bar);
static int w_uac_auth(struct sip_msg* msg, char* str, char* str2);
static int fixup_replace_from1(void** param, int param_no);
static int fixup_replace_from2(void** param, int param_no);
static int mod_init(void);
static void mod_destroy();
/* Exported functions */
static cmd_export_t cmds[]={
{"uac_replace_from", w_replace_from2, 2, fixup_replace_from2,
REQUEST_ROUTE },
{"uac_replace_from", w_replace_from1, 1, fixup_replace_from1,
REQUEST_ROUTE },
{"uac_restore_from", w_restore_from, 0, 0,
REQUEST_ROUTE },
{"uac_auth", w_uac_auth, 0, 0,
FAILURE_ROUTE },
{0,0,0,0,0}
};
/* Exported parameters */
static param_export_t params[] = {
{"rr_store_param", STR_PARAM, &rr_param.s },
{"from_restore_mode", STR_PARAM, &from_restore_mode_str },
{"from_passwd", STR_PARAM, &uac_passwd.s },
{"credential", STR_PARAM|USE_FUNC_PARAM, &add_credential },
{"auth_username_avp", STR_PARAM, &auth_username_avp },
{"auth_realm_avp", STR_PARAM, &auth_realm_avp },
{"auth_password_avp", STR_PARAM, &auth_password_avp },
{0, 0, 0}
};
struct module_exports exports= {
"uac",
cmds, /* exported functions */
params, /* param exports */
0, /* exported statistics */
mod_init, /* module initialization function */
(response_function) 0,
mod_destroy,
0 /* per-child init function */
};
inline static int parse_auth_avp( char *avp_spec, xl_spec_t *avp, char *txt)
{
if (xl_parse_spec( avp_spec, avp, XL_THROW_ERROR|XL_DISABLE_MULTI|
XL_DISABLE_COLORS)==0 || avp->type!=XL_AVP) {
LOG(L_ERR, "ERROR:uac:parse_auth_avp: malformed or non AVP %s "
"AVP definition\n",txt);
return -1;
}
return 0;
}
static int mod_init(void)
{
LOG(L_INFO,"UAC - initializing\n");
if (from_restore_mode_str && *from_restore_mode_str) {
if (strcasecmp(from_restore_mode_str,"none")==0) {
from_restore_mode = FROM_NO_RESTORE;
} else if (strcasecmp(from_restore_mode_str,"manual")==0) {
from_restore_mode = FROM_MANUAL_RESTORE;
} else if (strcasecmp(from_restore_mode_str,"auto")==0) {
from_restore_mode = FROM_AUTO_RESTORE;
} else {
LOG(L_ERR,"ERROR:uac:mod_init: unsupported value '%s' for "
"from_restore_mode\n",from_restore_mode_str);
goto error;
}
}
rr_param.len = strlen(rr_param.s);
if (rr_param.len==0 && from_restore_mode!=FROM_NO_RESTORE)
{
LOG(L_ERR,"ERROR:uac:mod_init: rr_store_param cannot be empty "
"if FROM is restoreable\n");
goto error;
}
uac_passwd.len = strlen(uac_passwd.s);
/* parse the auth AVP spesc, if any */
if ( auth_username_avp || auth_password_avp || auth_realm_avp) {
if (!auth_username_avp || !auth_password_avp || !auth_realm_avp) {
LOG(L_ERR,"ERROR:uac:mod_init: partial definition of auth AVP!");
goto error;
}
if ( parse_auth_avp(auth_realm_avp, &auth_realm_spec, "realm")<0
|| parse_auth_avp(auth_username_avp, &auth_username_spec, "username")<0
|| parse_auth_avp(auth_password_avp, &auth_password_spec, "password")<0
) {
goto error;
}
} else {
memset( &auth_realm_spec, 0, sizeof(xl_spec_t));
memset( &auth_password_spec, 0, sizeof(xl_spec_t));
memset( &auth_username_spec, 0, sizeof(xl_spec_t));
}
/* load the TM API - FIXME it should be loaded only
* if NO_RESTORE and AUTH */
if (load_tm_api(&uac_tmb)!=0) {
LOG(L_ERR, "ERROR:uac:mod_init: can't load TM API\n");
goto error;
}
if (from_restore_mode!=FROM_NO_RESTORE) {
/* load the RR API */
if (load_rr_api(&uac_rrb)!=0) {
LOG(L_ERR, "ERROR:uac:mod_init: can't load RR API\n");
goto error;
}
if (from_restore_mode==FROM_AUTO_RESTORE) {
/* get all requests doing loose route */
if (uac_rrb.register_rrcb( rr_checker, 0)!=0) {
LOG(L_ERR,"ERROR:uac:mod_init: failed to install "
"RR callback\n");
goto error;
}
}
}
init_from_replacer();
return 0;
error:
return -1;
}
static void mod_destroy()
{
destroy_credentials();
}
/************************** fixup functions ******************************/
static int fixup_replace_from1(void** param, int param_no)
{
xl_elem_t *model;
model=NULL;
if(xl_parse_format((char*)(*param),&model,XL_DISABLE_COLORS)<0)
{
LOG(L_ERR, "ERROR:uac:fixup_replace_from1: wrong format[%s]!\n",
(char*)(*param));
return E_UNSPEC;
}
if (model==NULL)
{
LOG(L_ERR, "ERROR:uac:fixup_replace_from1: empty parameter!\n");
return E_UNSPEC;
}
*param = (void*)model;
return 0;
}
static int fixup_replace_from2(void** param, int param_no)
{
xl_elem_t *model;
char *p;
str s;
/* convert to str */
s.s = (char*)*param;
s.len = strlen(s.s);
model=NULL;
if (param_no==1)
{
if (s.len)
{
/* put " around display name */
p = (char*)pkg_malloc(s.len+3);
if (p==0)
{
LOG(L_CRIT,"ERROR:uac:fixup_replace_from2: no more pkg mem\n");
return E_OUT_OF_MEM;
}
p[0] = '\"';
memcpy(p+1, s.s, s.len);
p[s.len+1] = '\"';
p[s.len+2] = '\0';
pkg_free(s.s);
s.s = p;
s.len += 2;
}
}
if(s.len!=0)
{
if(xl_parse_format(s.s,&model,XL_DISABLE_COLORS)<0)
{
LOG(L_ERR, "ERROR:uac:fixup_replace_from2: wrong format [%s] "
"for param no %d!\n", s.s, param_no);
pkg_free(s.s);
return E_UNSPEC;
}
}
*param = (void*)model;
return 0;
}
/************************** wrapper functions ******************************/
static int w_restore_from(struct sip_msg *msg, char* foo, char* bar)
{
/* safety checks - must be a request */
if (msg->first_line.type!=SIP_REQUEST) {
LOG(L_ERR,"ERROR:uac:w_restore_from: called for something "
"not request\n");
return -1;
}
return (restore_from(msg,0)==0)?1:-1;
}
static int w_replace_from1(struct sip_msg* msg, char* uri, char* str2)
{
str uri_s;
if(xl_printf_s( msg, (xl_elem_p)uri, &uri_s)!=0)
return -1;
return (replace_from(msg, 0, &uri_s)==0)?1:-1;
}
static int w_replace_from2(struct sip_msg* msg, char* dsp, char* uri)
{
str uri_s;
str dsp_s;
if (dsp!=NULL)
{
if(dsp!=NULL)
if(xl_printf_s( msg, (xl_elem_p)dsp, &dsp_s)!=0)
return -1;
} else {
dsp_s.s = 0;
dsp_s.len = 0;
}
if(uri!=NULL)
{
if(xl_printf_s( msg, (xl_elem_p)uri, &uri_s)!=0)
return -1;
}
return (replace_from(msg, &dsp_s, (uri)?&uri_s:0)==0)?1:-1;
}
static int w_uac_auth(struct sip_msg* msg, char* str, char* str2)
{
return (uac_auth(msg)==0)?1:-1;
}
|