00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include "../../sr_module.h"
00040 #include "../../db/db.h"
00041 #include "../../dprint.h"
00042 #include "../../error.h"
00043 #include "../../mod_fix.h"
00044 #include "../../mem/mem.h"
00045 #include "../auth/api.h"
00046 #include "../sl/sl_api.h"
00047 #include "aaa_avps.h"
00048 #include "authorize.h"
00049
00050 MODULE_VERSION
00051
00052 #define TABLE_VERSION 6
00053
00054
00055
00056
00057 static void destroy(void);
00058
00059
00060
00061
00062
00063 static int child_init(int rank);
00064
00065
00066
00067
00068
00069 static int mod_init(void);
00070
00071
00072 static int auth_fixup(void** param, int param_no);
00073
00074
00075 struct sl_binds slb;
00076
00077 #define USER_COL "username"
00078 #define USER_COL_LEN (sizeof(USER_COL) - 1)
00079
00080 #define DOMAIN_COL "domain"
00081 #define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
00082
00083 #define PASS_COL "ha1"
00084 #define PASS_COL_LEN (sizeof(PASS_COL) - 1)
00085
00086 #define PASS_COL_2 "ha1b"
00087 #define PASS_COL_2_LEN (sizeof(PASS_COL_2) - 1)
00088
00089 #define DEFAULT_CRED_LIST "rpid"
00090
00091
00092
00093
00094 static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00095 str user_column = {USER_COL, USER_COL_LEN};
00096 str domain_column = {DOMAIN_COL, DOMAIN_COL_LEN};
00097 str pass_column = {PASS_COL, PASS_COL_LEN};
00098 str pass_column_2 = {PASS_COL_2, PASS_COL_2_LEN};
00099
00100
00101 int calc_ha1 = 0;
00102 int use_domain = 0;
00103
00104 db_con_t* auth_db_handle = 0;
00105 db_func_t auth_dbf;
00106 auth_api_t auth_api;
00107
00108 char *credentials_list = DEFAULT_CRED_LIST;
00109 struct aaa_avp *credentials = 0;
00110 int credentials_n = 0;
00111
00112
00113
00114
00115 static cmd_export_t cmds[] = {
00116 {"www_authorize", (cmd_function)www_authorize, 2, auth_fixup, 0, REQUEST_ROUTE},
00117 {"proxy_authorize", (cmd_function)proxy_authorize, 2, auth_fixup, 0, REQUEST_ROUTE},
00118 {0, 0, 0, 0, 0, 0}
00119 };
00120
00121
00122
00123
00124
00125 static param_export_t params[] = {
00126 {"db_url", STR_PARAM, &db_url.s },
00127 {"user_column", STR_PARAM, &user_column.s },
00128 {"domain_column", STR_PARAM, &domain_column.s },
00129 {"password_column", STR_PARAM, &pass_column.s },
00130 {"password_column_2", STR_PARAM, &pass_column_2.s },
00131 {"calculate_ha1", INT_PARAM, &calc_ha1 },
00132 {"use_domain", INT_PARAM, &use_domain },
00133 {"load_credentials", STR_PARAM, &credentials_list },
00134 {0, 0, 0}
00135 };
00136
00137
00138
00139
00140
00141 struct module_exports exports = {
00142 "auth_db",
00143 DEFAULT_DLFLAGS,
00144 cmds,
00145 params,
00146 0,
00147 0,
00148 0,
00149 0,
00150 mod_init,
00151 0,
00152 destroy,
00153 child_init
00154 };
00155
00156
00157 static int child_init(int rank)
00158 {
00159 auth_db_handle = auth_dbf.init(&db_url);
00160 if (auth_db_handle == 0){
00161 LM_ERR("unable to connect to the database\n");
00162 return -1;
00163 }
00164
00165 return 0;
00166 }
00167
00168
00169 static int mod_init(void)
00170 {
00171 bind_auth_t bind_auth;
00172
00173 db_url.len = strlen(db_url.s);
00174 user_column.len = strlen(user_column.s);
00175 domain_column.len = strlen(domain_column.s);
00176 pass_column.len = strlen(pass_column.s);
00177 pass_column_2.len = strlen(pass_column.s);
00178
00179
00180 if (db_bind_mod(&db_url, &auth_dbf) < 0){
00181 LM_ERR("unable to bind to a database driver\n");
00182 return -1;
00183 }
00184
00185
00186 bind_auth = (bind_auth_t)find_export("bind_auth", 0, 0);
00187 if (!bind_auth) {
00188 LM_ERR("unable to find bind_auth function. Check if you load the auth module.\n");
00189 return -2;
00190 }
00191
00192 if (bind_auth(&auth_api) < 0) {
00193 LM_ERR("unable to bind auth module\n");
00194 return -3;
00195 }
00196
00197
00198 if (load_sl_api(&slb)!=0) {
00199 LM_ERR("can't load SL API\n");
00200 return -1;
00201 }
00202
00203
00204 if (parse_aaa_avps( credentials_list, &credentials, &credentials_n)!=0) {
00205 LM_ERR("failed to parse credentials\n");
00206 return -5;
00207 }
00208
00209 return 0;
00210 }
00211
00212
00213 static void destroy(void)
00214 {
00215 if (auth_db_handle) {
00216 auth_dbf.close(auth_db_handle);
00217 auth_db_handle = 0;
00218 }
00219 if (credentials) {
00220 free_aaa_avp_list(credentials);
00221 credentials = 0;
00222 credentials_n = 0;
00223 }
00224 }
00225
00226
00227
00228
00229
00230 static int auth_fixup(void** param, int param_no)
00231 {
00232 db_con_t* dbh = NULL;
00233 str name;
00234
00235 if (param_no == 1) {
00236 return fixup_spve_null(param, 1);
00237 } else if (param_no == 2) {
00238 name.s = (char*)*param;
00239 name.len = strlen(name.s);
00240
00241 dbh = auth_dbf.init(&db_url);
00242 if (!dbh) {
00243 LM_ERR("unable to open database connection\n");
00244 return -1;
00245 }
00246 if(db_check_table_version(&auth_dbf, dbh, &name, TABLE_VERSION) < 0) {
00247 LM_ERR("error during table version check.\n");
00248 auth_dbf.close(dbh);
00249 return -1;
00250 }
00251 }
00252 auth_dbf.close(dbh);
00253 return 0;
00254 }