acc_radius_mod.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  * 
00004  * Accounting module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * -------
00026  * 2010-07-28 - moved out radius account out of acc module (daniel)
00027  */
00028 
00042 #include <stdio.h>
00043 #include <string.h>
00044 
00045 #include "../../sr_module.h"
00046 #include "../../dprint.h"
00047 #include "../../mem/mem.h"
00048 #include "../../parser/parse_to.h"
00049 #include "../../lib/kcore/radius.h"
00050 #include "../../modules_k/acc/acc_api.h"
00051 #include "acc_radius_mod.h"
00052 
00053 MODULE_VERSION
00054 
00055 static int mod_init(void);
00056 static void destroy(void);
00057 static int child_init(int rank);
00058 
00059 int acc_radius_init(acc_init_info_t *inf);
00060 int acc_radius_send_request(struct sip_msg *req, acc_info_t *inf);
00061 
00062 static int w_acc_radius_request(struct sip_msg *rq, char *comment, char *foo);
00063 static int acc_api_fixup(void** param, int param_no);
00064 static int free_acc_api_fixup(void** param, int param_no);
00065 
00066 int init_acc_rad(acc_extra_t *leg_info, char *rad_cfg, int srv_type);
00067 int extra2attrs(struct acc_extra *extra, struct attr *attrs, int offset);
00068 
00070 acc_api_t accb;
00071 acc_engine_t _acc_radius_engine;
00072 
00073 /* ----- RADIUS acc variables ----------- */
00076 
00077 static char *radius_config = 0;
00078 int radius_flag = -1;
00079 int radius_missed_flag = -1;
00080 static int service_type = -1;
00081 void *rh;
00082 /* rad extra variables */
00083 static char *rad_extra_str = 0;
00084 acc_extra_t *rad_extra = 0;
00088 static cmd_export_t cmds[] = {
00089         {"acc_rad_request", (cmd_function)w_acc_radius_request, 1,
00090                 acc_api_fixup, free_acc_api_fixup,
00091                 ANY_ROUTE},
00092         {0, 0, 0, 0, 0, 0}
00093 };
00094 
00095 
00096 
00097 static param_export_t params[] = {
00098         {"radius_config",        STR_PARAM, &radius_config        },
00099         {"radius_flag",          INT_PARAM, &radius_flag          },
00100         {"radius_missed_flag",   INT_PARAM, &radius_missed_flag   },
00101         {"service_type",         INT_PARAM, &service_type         },
00102         {"radius_extra",         STR_PARAM, &rad_extra_str        },
00103         {0,0,0}
00104 };
00105 
00106 
00107 struct module_exports exports= {
00108         "acc_radius",
00109         DEFAULT_DLFLAGS, /* dlopen flags */
00110         cmds,       /* exported functions */
00111         params,     /* exported params */
00112         0,          /* exported statistics */
00113         0,          /* exported MI functions */
00114         0,          /* exported pseudo-variables */
00115         0,          /* extra processes */
00116         mod_init,   /* initialization module */
00117         0,          /* response function */
00118         destroy,    /* destroy function */
00119         child_init  /* per-child init function */
00120 };
00121 
00122 
00123 
00124 /************************** INTERFACE functions ****************************/
00125 
00126 static int mod_init( void )
00127 {
00128         if (radius_config==NULL || radius_config[0]=='\0')
00129                 return 0;
00130         
00131         /* bind the ACC API */
00132         if (acc_load_api(&accb)<0) {
00133                 LM_ERR("cannot bind to ACC API\n");
00134                 return -1;
00135         }
00136 
00137         /* parse the extra string, if any */
00138         if (rad_extra_str && (rad_extra=accb.parse_extra(rad_extra_str))==0 ) {
00139                 LM_ERR("failed to parse rad_extra param\n");
00140                 return -1;
00141         }
00142                 
00143         memset(&_acc_radius_engine, 0, sizeof(acc_engine_t));
00144 
00145         if(radius_flag != -1)
00146                 _acc_radius_engine.acc_flag        = 1<<radius_flag;
00147         if(radius_missed_flag != -1)
00148                 _acc_radius_engine.missed_flag = 1<<radius_missed_flag;
00149         _acc_radius_engine.acc_req     = acc_radius_send_request;
00150         _acc_radius_engine.acc_init    = acc_radius_init;
00151         memcpy(_acc_radius_engine.name, "radius", 6);
00152         if(accb.register_engine(&_acc_radius_engine)<0)
00153         {
00154                 LM_ERR("cannot register ACC RADIUS engine\n");
00155                 return -1;
00156         }
00157 
00158         return 0;
00159 }
00160 
00161 
00162 static int child_init(int rank)
00163 {
00164         if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
00165                 return 0; /* do nothing for the main process */
00166 
00167         return 0;
00168 }
00169 
00170 
00171 static void destroy(void)
00172 {
00173 }
00174 
00175 static int acc_api_fixup(void** param, int param_no)
00176 {
00177         struct acc_param *accp;
00178         char *p;
00179 
00180         p = (char*)*param;
00181         if (p==0 || p[0]==0) {
00182                 LM_ERR("first parameter is empty\n");
00183                 return E_SCRIPT;
00184         }
00185 
00186         if (param_no == 1) {
00187                 accp = (struct acc_param*)pkg_malloc(sizeof(struct acc_param));
00188                 if (!accp) {
00189                         LM_ERR("no more pkg mem\n");
00190                         return E_OUT_OF_MEM;
00191                 }
00192                 memset( accp, 0, sizeof(struct acc_param));
00193                 accp->reason.s = p;
00194                 accp->reason.len = strlen(p);
00195                 /* any code? */
00196                 if (accp->reason.len>=3 && isdigit((int)p[0])
00197                 && isdigit((int)p[1]) && isdigit((int)p[2]) ) {
00198                         accp->code = (p[0]-'0')*100 + (p[1]-'0')*10 + (p[2]-'0');
00199                         accp->code_s.s = p;
00200                         accp->code_s.len = 3;
00201                         accp->reason.s += 3;
00202                         for( ; isspace((int)accp->reason.s[0]) ; accp->reason.s++ );
00203                         accp->reason.len = strlen(accp->reason.s);
00204                 }
00205                 *param = (void*)accp;
00206 #ifdef SQL_ACC
00207         } else if (param_no == 2) {
00208                 /* only for db acc - the table name */
00209                 if (db_url.s==0) {
00210                         pkg_free(p);
00211                         *param = 0;
00212                 }
00213 #endif
00214         }
00215         return 0;
00216 }
00217 
00218 static int free_acc_api_fixup(void** param, int param_no)
00219 {
00220         if(*param)
00221         {
00222                 pkg_free(*param);
00223                 *param = 0;
00224         }
00225         return 0;
00226 }
00227 
00228 
00229 enum { RA_ACCT_STATUS_TYPE=0, RA_SERVICE_TYPE, RA_SIP_RESPONSE_CODE,
00230         RA_SIP_METHOD, RA_TIME_STAMP, RA_STATIC_MAX};
00231 enum {RV_STATUS_START=0, RV_STATUS_STOP, RV_STATUS_ALIVE, RV_STATUS_FAILED,
00232         RV_SIP_SESSION, RV_STATIC_MAX};
00233 static struct attr
00234         rd_attrs[RA_STATIC_MAX+ACC_CORE_LEN-2+MAX_ACC_EXTRA+MAX_ACC_LEG];
00235 static struct val rd_vals[RV_STATIC_MAX];
00236 
00237 int init_acc_rad(acc_extra_t *leg_info, char *rad_cfg, int srv_type)
00238 {
00239         int n;
00240 
00241         memset(rd_attrs, 0, sizeof(rd_attrs));
00242         memset(rd_vals, 0, sizeof(rd_vals));
00243         rd_attrs[RA_ACCT_STATUS_TYPE].n  = "Acct-Status-Type";
00244         rd_attrs[RA_SERVICE_TYPE].n      = "Service-Type";
00245         rd_attrs[RA_SIP_RESPONSE_CODE].n = "Sip-Response-Code";
00246         rd_attrs[RA_SIP_METHOD].n        = "Sip-Method";
00247         rd_attrs[RA_TIME_STAMP].n        = "Event-Timestamp";
00248         n = RA_STATIC_MAX;
00249         /* caution: keep these aligned to core acc output */
00250         rd_attrs[n++].n                  = "Sip-From-Tag";
00251         rd_attrs[n++].n                  = "Sip-To-Tag";
00252         rd_attrs[n++].n                  = "Acct-Session-Id";
00253 
00254         rd_vals[RV_STATUS_START].n        = "Start";
00255         rd_vals[RV_STATUS_STOP].n         = "Stop";
00256         rd_vals[RV_STATUS_ALIVE].n        = "Alive";
00257         rd_vals[RV_STATUS_FAILED].n       = "Failed";
00258         rd_vals[RV_SIP_SESSION].n         = "Sip-Session";
00259 
00260         /* add and count the extras as attributes */
00261         n += extra2attrs( rad_extra, rd_attrs, n);
00262         /* add and count the legs as attributes */
00263         n += extra2attrs( leg_info, rd_attrs, n);
00264 
00265         /* read config */
00266         if ((rh = rc_read_config(rad_cfg)) == NULL) {
00267                 LM_ERR("failed to open radius config file: %s\n", rad_cfg );
00268                 return -1;
00269         }
00270         /* read dictionary */
00271         if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))!=0) {
00272                 LM_ERR("failed to read radius dictionary\n");
00273                 return -1;
00274         }
00275 
00276         INIT_AV(rh, rd_attrs, n, rd_vals, RV_STATIC_MAX, "acc", -1, -1);
00277 
00278         if (srv_type != -1)
00279                 rd_vals[RV_SIP_SESSION].v = srv_type;
00280 
00281         return 0;
00282 }
00283 
00284 
00285 int acc_radius_init(acc_init_info_t *inf)
00286 {
00287         if (radius_config && radius_config[0]) {
00288                 if (init_acc_rad(inf->leg_info, radius_config, service_type)!=0 ) {
00289                         LM_ERR("failed to init radius\n");
00290                         return -1;
00291                 }
00292         }
00293         return 0;
00294 }
00295 
00296 static inline uint32_t rad_status( struct sip_msg *req, int code )
00297 {
00298         str tag;
00299         unsigned int in_dialog_req = 0;
00300 
00301         tag = get_to(req)->tag_value;
00302         if(tag.s!=0 && tag.len!=0)
00303                 in_dialog_req = 1;
00304 
00305         if (req->REQ_METHOD==METHOD_INVITE && in_dialog_req == 0
00306                     && code>=200 && code<300)
00307                 return rd_vals[RV_STATUS_START].v;
00308         if ((req->REQ_METHOD==METHOD_BYE || req->REQ_METHOD==METHOD_CANCEL))
00309                 return rd_vals[RV_STATUS_STOP].v;
00310         if (in_dialog_req != 0)
00311                 return rd_vals[RV_STATUS_ALIVE].v;
00312         return rd_vals[RV_STATUS_FAILED].v;
00313  }
00314 
00315 #define ADD_RAD_AVPAIR(_attr,_val,_len)         \
00316     do {                                                                \
00317         if (!rc_avpair_add(rh, &send, rd_attrs[_attr].v, _val, _len, 0)) { \
00318             LM_ERR("failed to add %s, %d\n", rd_attrs[_attr].n, _attr); \
00319             goto error;                                                 \
00320         } \
00321     }while(0)
00322 
00323 int acc_radius_send_request(struct sip_msg *req, acc_info_t *inf)
00324 {
00325         int attr_cnt;
00326         VALUE_PAIR *send;
00327         uint32_t av_type;
00328         int offset;
00329         int i;
00330 
00331         send=NULL;
00332 
00333         attr_cnt = accb.get_core_attrs( req, inf->varr, inf->iarr, inf->tarr );
00334         /* not interested in the last 2 values */
00335         attr_cnt -= 2;
00336 
00337         av_type = rad_status( req, inf->env->code); /* RADIUS status */
00338         ADD_RAD_AVPAIR( RA_ACCT_STATUS_TYPE, &av_type, -1);
00339 
00340         av_type = rd_vals[RV_SIP_SESSION].v; /* session*/
00341         ADD_RAD_AVPAIR( RA_SERVICE_TYPE, &av_type, -1);
00342 
00343         av_type = (uint32_t)inf->env->code; /* status=integer */
00344         ADD_RAD_AVPAIR( RA_SIP_RESPONSE_CODE, &av_type, -1);
00345 
00346         av_type = req->REQ_METHOD; /* method */
00347         ADD_RAD_AVPAIR( RA_SIP_METHOD, &av_type, -1);
00348 
00349         /* unix time */
00350         av_type = (uint32_t)inf->env->ts;
00351         ADD_RAD_AVPAIR( RA_TIME_STAMP, &av_type, -1);
00352 
00353         /* add extra also */
00354         attr_cnt += accb.get_extra_attrs(rad_extra, req, inf->varr+attr_cnt,
00355                                 inf->iarr+attr_cnt, inf->tarr+attr_cnt);
00356 
00357         /* add the values for the vector - start from 1 instead of
00358          * 0 to skip the first value which is the METHOD as string */
00359         offset = RA_STATIC_MAX-1;
00360         for( i=1; i<attr_cnt; i++) {
00361             switch (inf->tarr[i]) {
00362             case TYPE_STR:
00363                 ADD_RAD_AVPAIR(offset+i, inf->varr[i].s, inf->varr[i].len);
00364                 break;
00365             case TYPE_INT:
00366                 ADD_RAD_AVPAIR(offset+i, &(inf->iarr[i]), -1);
00367                 break;
00368             default:
00369                 break;
00370             }
00371         }
00372 
00373         /* call-legs attributes also get inserted */
00374         if ( inf->leg_info ) {
00375                 offset += attr_cnt;
00376                 attr_cnt = accb.get_leg_attrs(inf->leg_info,req,inf->varr,inf->iarr,inf->tarr,1);
00377                 do {
00378                         for (i=0; i<attr_cnt; i++)
00379                                 ADD_RAD_AVPAIR( offset+i, inf->varr[i].s, inf->varr[i].len );
00380                 }while ( (attr_cnt=accb.get_leg_attrs(inf->leg_info,req,inf->varr,inf->iarr,
00381                                               inf->tarr, 0))!=0 );
00382         }
00383 
00384         if (rc_acct(rh, SIP_PORT, send)!=OK_RC) {
00385                 LM_ERR("radius-ing failed\n");
00386                 goto error;
00387         }
00388         rc_avpair_free(send);
00389         return 1;
00390 
00391 error:
00392         rc_avpair_free(send);
00393         return -1;
00394 }
00395 
00399 int extra2attrs(struct acc_extra *extra, struct attr *attrs, int offset)
00400 {
00401         int i;
00402 
00403         for(i=0 ; extra ; i++, extra=extra->next) {
00404                 attrs[offset+i].n = extra->name.s;
00405         }
00406         return i;
00407 }
00408 
00412 static int w_acc_radius_request(struct sip_msg *rq, char *comment, char *foo)
00413 {
00414         return accb.exec(rq, &_acc_radius_engine, (acc_param_t*)comment);
00415 }
00416