carrierroute.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007-2008 1&1 Internet AG
00005  *
00006  * This file is part of SIP-router, a free SIP server.
00007  *
00008  * SIP-router is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * SIP-router is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00040 #include "../../sr_module.h"
00041 #include "../../str.h"
00042 #include "../../mem/mem.h"
00043 #include "../../ut.h" /* for user2uid() */
00044 #include "carrierroute.h"
00045 #include "cr_fixup.h"
00046 #include "cr_map.h"
00047 #include "cr_fifo.h"
00048 #include "cr_data.h"
00049 #include "cr_func.h"
00050 #include "db_carrierroute.h"
00051 #include "config.h"
00052 #include <sys/stat.h>
00053 
00054 MODULE_VERSION
00055 
00056 str carrierroute_db_url = str_init(DEFAULT_RODB_URL);
00057 str subscriber_table = str_init("subscriber");
00058 
00059 static str subscriber_username_col = str_init("username");
00060 static str subscriber_domain_col = str_init("domain");
00061 static str cr_preferred_carrier_col = str_init("cr_preferred_carrier");
00062 
00063 str * subscriber_columns[SUBSCRIBER_COLUMN_NUM] = {
00064         &subscriber_username_col,
00065         &subscriber_domain_col,
00066         &cr_preferred_carrier_col
00067 };
00068 
00069 char * config_source = "file";
00070 char * config_file = CFG_DIR"carrierroute.conf";
00071 
00072 str default_tree = str_init("default");
00073 const str CR_EMPTY_PREFIX = str_init("null");
00074 
00075 int mode = 0;
00076 int cr_match_mode = 10;
00077 
00078 
00079 /************* Declaration of Interface Functions **************************/
00080 static int mod_init(void);
00081 static int child_init(int);
00082 static int mi_child_init(void);
00083 static void mod_destroy(void);
00084 
00085 
00086 /************* Module Exports **********************************************/
00087 static cmd_export_t cmds[]={
00088         {"cr_user_carrier",  (cmd_function)cr_load_user_carrier,  3, cr_load_user_carrier_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
00089         {"cr_route",         (cmd_function)cr_route5,              5, cr_route_fixup,             0, REQUEST_ROUTE | FAILURE_ROUTE },
00090         {"cr_route",         (cmd_function)cr_route,              6, cr_route_fixup,             0, REQUEST_ROUTE | FAILURE_ROUTE },
00091         {"cr_nofallback_route",(cmd_function)cr_nofallback_route5,     5, cr_route_fixup,             0, REQUEST_ROUTE | FAILURE_ROUTE },
00092         {"cr_nofallback_route",(cmd_function)cr_nofallback_route,     6, cr_route_fixup,             0, REQUEST_ROUTE | FAILURE_ROUTE },
00093         {"cr_next_domain",   (cmd_function)cr_load_next_domain,   6, cr_load_next_domain_fixup,  0, REQUEST_ROUTE | FAILURE_ROUTE },
00094         {0, 0, 0, 0, 0, 0}
00095 };
00096 
00097 static param_export_t params[]= {
00098         carrierroute_DB_URL
00099         carrierroute_DB_TABLE
00100         carrierfailureroute_DB_TABLE
00101         carrier_name_DB_TABLE
00102         domain_name_DB_TABLE
00103         carrierroute_DB_COLS
00104         carrierfailureroute_DB_COLS
00105         carrier_name_DB_COLS
00106         domain_name_DB_COLS
00107         {"subscriber_table",       STR_PARAM, &subscriber_table.s },
00108         {"subscriber_user_col",    STR_PARAM, &subscriber_username_col.s },
00109         {"subscriber_domain_col",  STR_PARAM, &subscriber_domain_col.s },
00110         {"subscriber_carrier_col", STR_PARAM, &cr_preferred_carrier_col.s },
00111         {"config_source",          STR_PARAM, &config_source },
00112         {"default_tree",           STR_PARAM, &default_tree.s },
00113         {"config_file",            STR_PARAM, &config_file },
00114         {"use_domain",             INT_PARAM, &default_carrierroute_cfg.use_domain },
00115         {"fallback_default",       INT_PARAM, &default_carrierroute_cfg.fallback_default },
00116         {"fetch_rows",             INT_PARAM, &default_carrierroute_cfg.fetch_rows },
00117         {"match_mode",             INT_PARAM, &cr_match_mode },
00118         {0,0,0}
00119 };
00120 
00121 static mi_export_t mi_cmds[] = {
00122         { "cr_reload_routes",   reload_fifo,     MI_NO_INPUT_FLAG, 0,  mi_child_init },
00123         { "cr_dump_routes",     dump_fifo,       MI_NO_INPUT_FLAG, 0,  0 },
00124         { "cr_replace_host",    replace_host,    0,                0,  0 },
00125         { "cr_deactivate_host", deactivate_host, 0,                0,  0 },
00126         { "cr_activate_host",   activate_host,   0,                0,  0 },
00127         { "cr_add_host",        add_host,        0,                0,  0 },
00128         { "cr_delete_host",     delete_host,     0,                0,  0 },
00129         { 0, 0, 0, 0, 0}
00130 };
00131 
00132 struct module_exports exports = {
00133         "carrierroute",
00134         DEFAULT_DLFLAGS, /* dlopen flags */
00135         cmds,       /* Exported functions */
00136         params,     /* Export parameters */
00137         0,          /* exported statistics */
00138         mi_cmds,    /* exported MI functions */
00139         0,          /* exported pseudo-variables */
00140         0,          /* extra processes */
00141         mod_init,   /* Module initialization function */
00142         0,          /* Response function */
00143         mod_destroy,/* Destroy function */
00144         child_init  /* Child initialization function */
00145 };
00146 
00147 
00148 /************* Interface Functions *****************************************/
00149 
00156 static int mod_init(void) {
00157         struct stat fs;
00158         extern char* user; /*from main.c*/
00159         int uid, gid;
00160 
00161         if(register_mi_mod(exports.name, mi_cmds)!=0)
00162         {
00163                 LM_ERR("failed to register MI commands\n");
00164                 return -1;
00165         }
00166 
00167         subscriber_table.len = strlen(subscriber_table.s);
00168         subscriber_username_col.len = strlen(subscriber_username_col.s);
00169         subscriber_domain_col.len = strlen(subscriber_domain_col.s);
00170         cr_preferred_carrier_col.len = strlen(cr_preferred_carrier_col.s);
00171         default_tree.len = strlen(default_tree.s);
00172 
00173         carrierroute_db_vars();
00174 
00175         if (cr_match_mode != 10 && cr_match_mode != 128) {
00176                 LM_ERR("invalid matching mode %d specific, please use 10 or 128\n", cr_match_mode);
00177                 return -1;
00178         }
00179 
00180         if (strcmp(config_source, "db") == 0) {
00181                 mode = CARRIERROUTE_MODE_DB;
00182 
00183                 LM_INFO("use database as configuration source\n");
00184                 if(carrierroute_db_init() < 0){
00185                         return -1;
00186                 }
00187                 // FIXME, move data initialization into child process
00188                 if(carrierroute_db_open() < 0){
00189                         return -1;
00190                 }
00191         }
00192         else if (strcmp(config_source, "file") == 0) {
00193                 mode = CARRIERROUTE_MODE_FILE;
00194 
00195                 LM_INFO("use file as configuration source\n");
00196                 if(stat(config_file, &fs) != 0){
00197                         LM_ERR("can't stat config file\n");
00198                         return -1;
00199                 }
00200                 if(fs.st_mode & S_IWOTH){
00201                         LM_WARN("insecure file permissions, routing data is world writeable\n");
00202                 }
00203 
00204                 if (user){
00205                         if (user2uid(&uid, &gid, user)<0){
00206                                 LM_ERR("bad user name/uid number: -u %s\n", user);
00207                                 return -1;
00208                         }
00209                 } else {
00210                         uid = geteuid();
00211                         gid = getegid();
00212                 }
00213 
00214                 if( !( fs.st_mode & S_IWOTH) &&
00215                         !((fs.st_mode & S_IWGRP) && (fs.st_gid == uid)) &&
00216                         !((fs.st_mode & S_IWUSR) && (fs.st_uid == gid))) {
00217                                 LM_ERR("config file %s not writable\n", config_file);
00218                                 return -1;
00219                 }
00220         }
00221         else {
00222                 LM_ERR("invalid config_source parameter: %s\n", config_source);
00223                 return -1;
00224         }
00225 
00226         if(cfg_declare("carrierroute", carrierroute_cfg_def, &default_carrierroute_cfg, cfg_sizeof(carrierroute), &carrierroute_cfg)){
00227                 LM_ERR("Fail to declare the configuration\n");
00228                 return -1;
00229         }
00230 
00231         if (init_route_data() < 0) {
00232                 LM_ERR("could not init route data\n");
00233                 return -1;
00234         }
00235 
00236         if (reload_route_data() == -1) {
00237                 LM_ERR("could not prepare route data\n");
00238                 return -1;
00239         }
00240 
00241         if(mode == CARRIERROUTE_MODE_DB){
00242                 carrierroute_db_close();
00243         }
00244         return 0;
00245 }
00246 
00247 
00248 static int child_init(int rank) {
00249         if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
00250                 return 0; /* do nothing for the main process */
00251 
00252         if(mode == CARRIERROUTE_MODE_DB){
00253                 return carrierroute_db_open();
00254         }
00255         return 0;
00256 }
00257 
00258 
00259 static int mi_child_init(void) {
00260         if(mode == CARRIERROUTE_MODE_DB){
00261                 return carrierroute_db_open();
00262         }
00263         return 0;
00264 }
00265 
00266 
00267 static void mod_destroy(void) {
00268         if(mode == CARRIERROUTE_MODE_DB){
00269                 carrierroute_db_close();
00270         }
00271         destroy_route_data();
00272 }