authdb_mod.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: authdb_mod.c 4689 2008-08-13 06:46:17Z henningw $
00003  *
00004  * Digest Authentication 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  * 2003-02-26: checks and group moved to separate modules (janakj)
00027  * 2003-03-11: New module interface (janakj)
00028  * 2003-03-16: flags export parameter added (janakj)
00029  * 2003-03-19  all mallocs/frees replaced w/ pkg_malloc/pkg_free (andrei)
00030  * 2003-04-05: default_uri #define used (jiri)
00031  * 2004-06-06  cleanup: static & auth_db_{init,bind,close.ver} used (andrei)
00032  * 2005-05-31  general definition of AVPs in credentials now accepted - ID AVP,
00033  *             STRING AVP, AVP aliases (bogdan)
00034  * 2006-03-01 pseudo variables support for domain name (bogdan)
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  * Module destroy function prototype
00056  */
00057 static void destroy(void);
00058 
00059 
00060 /*
00061  * Module child-init function prototype
00062  */
00063 static int child_init(int rank);
00064 
00065 
00066 /*
00067  * Module initialization function prototype
00068  */
00069 static int mod_init(void);
00070 
00071 
00072 static int auth_fixup(void** param, int param_no);
00073 
00074 /** SL binds */
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  * Module parameter variables
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; /* Use also domain when looking up in table */
00103 
00104 db_con_t* auth_db_handle    = 0; /* database connection handle */
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; /* Parsed list of credentials to load */
00110 int credentials_n           = 0; /* Number of credentials in the list */
00111 
00112 /*
00113  * Exported functions
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  * Exported parameters
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  * Module interface
00140  */
00141 struct module_exports exports = {
00142    "auth_db", 
00143    DEFAULT_DLFLAGS, /* dlopen flags */
00144    cmds,       /* Exported functions */
00145    params,     /* Exported parameters */
00146    0,          /* exported statistics */
00147    0,          /* exported MI functions */
00148    0,          /* exported pseudo-variables */
00149    0,          /* extra processes */
00150    mod_init,   /* module initialization function */
00151    0,          /* response function */
00152    destroy,    /* destroy function */
00153    child_init  /* child initialization function */
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    /* Find a database module */
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    /* bind to auth module and import the API */
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    /* load the SL API */
00198    if (load_sl_api(&slb)!=0) {
00199       LM_ERR("can't load SL API\n");
00200       return -1;
00201    }
00202 
00203    /* process additional list of credentials */
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  * Convert the char* parameters
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 }

Generated on Thu May 17 12:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6