auth_diameter.c

Go to the documentation of this file.
00001 /*
00002  * $Id: auth_diameter.c 4518 2008-07-28 15:39:28Z henningw $ 
00003  *
00004  * Digest Authentication - Diameter support
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  *  
00027  *  
00028  * 2006-03-01 pseudo variables support for domain name (bogdan)
00029  */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <netinet/in.h>
00037 #include <netdb.h> 
00038 
00039 #include "../../sr_module.h"
00040 #include "../../error.h"
00041 #include "../../dprint.h"
00042 #include "../../pvar.h"
00043 #include "../../mem/mem.h"
00044 #include "../sl/sl_api.h"
00045 
00046 #include "diameter_msg.h"
00047 #include "auth_diameter.h"
00048 #include "authorize.h"
00049 #include "tcp_comm.h"
00050 
00051 MODULE_VERSION
00052 
00053 
00054 /** SL binds */
00055 struct sl_binds slb;
00056 
00057 static int mod_init(void);                        /* Module initialization function*/
00058 static int mod_child_init(int r);                 /* Child initialization function*/
00059 static int auth_fixup(void** param, int param_no);
00060 static int group_fixup(void** param, int param_no);
00061 
00062 int diameter_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2);
00063 int diameter_proxy_authorize(struct sip_msg* _msg, char* _realm, char* _s2);
00064 int diameter_is_user_in(struct sip_msg* _msg, char* group, char* _s2);
00065 
00066 /*
00067  * Module parameter variables
00068  */
00069 char* diameter_client_host = "localhost";
00070 int diameter_client_port = 3000;
00071 int use_domain = 0;
00072 
00073 rd_buf_t *rb;
00074 
00075 /*
00076  * Exported functions
00077  */
00078 static cmd_export_t cmds[] = {
00079    {"diameter_www_authorize",   (cmd_function)diameter_www_authorize,   1, auth_fixup,
00080          0, REQUEST_ROUTE},
00081    {"diameter_proxy_authorize", (cmd_function)diameter_proxy_authorize, 1, auth_fixup,
00082          0, REQUEST_ROUTE},
00083    {"diameter_is_user_in",      (cmd_function)diameter_is_user_in,      2, group_fixup,
00084          0, REQUEST_ROUTE},
00085    {0, 0, 0, 0, 0, 0}
00086 };
00087 
00088 
00089 /*
00090  * Exported parameters
00091  */
00092 static param_export_t params[] = {
00093    {"diameter_client_host", STR_PARAM, &diameter_client_host},
00094    {"diameter_client_port", INT_PARAM, &diameter_client_port},
00095    {"use_domain", INT_PARAM, &use_domain},
00096    {0, 0, 0}
00097 };
00098 
00099 
00100 /*
00101  * Module interface
00102  */
00103 struct module_exports exports = {
00104    "auth_diameter",
00105    DEFAULT_DLFLAGS, /* dlopen flags */
00106    cmds,          /* Exported functions */
00107    params,        /* Exported parameters */
00108    0,             /* exported statistics */
00109    0,             /* exported MI functions */
00110    0,             /* exported pseudo-variables */
00111    0,             /* extra processes */
00112    mod_init,      /* module initialization function */
00113    0,             /* response function */
00114    0,             /* destroy function */
00115    mod_child_init /* child initialization function */
00116 };
00117 
00118 
00119 /*
00120  * Module initialization function
00121  */
00122 static int mod_init(void)
00123 {
00124    LM_DBG("auth_diameter - Initializing\n");
00125 
00126    /* load the SL API */
00127    if (load_sl_api(&slb)!=0) {
00128       LM_ERR("can't load SL API\n");
00129       return -1;
00130    }
00131    
00132    return 0;
00133 }
00134 
00135 static int mod_child_init(int r)
00136 {  
00137    /* open TCP connection */
00138    LM_DBG("initializing TCP connection\n");
00139 
00140    sockfd = init_mytcp(diameter_client_host, diameter_client_port);
00141    if(sockfd==-1) 
00142    {
00143       LM_DBG("the TCP connection was not established\n");
00144       return -1;
00145    }
00146 
00147    LM_DBG("the TCP connection was established on socket=%d\n", sockfd);
00148    
00149    rb = (rd_buf_t*)pkg_malloc(sizeof(rd_buf_t));
00150    if(!rb)
00151    {
00152       LM_DBG("no more free pkg memory\n");
00153       return -1;
00154    }
00155    rb->buf = 0;
00156    rb->chall = 0;
00157 
00158    return 0;
00159 }
00160 
00161 #if 0
00162 static void destroy(void)
00163 {
00164    close_tcp_connection(sockfd);
00165 }
00166 #endif
00167 
00168 
00169 /*
00170  * Convert char* parameter to pv_elem_t* parameter
00171  */
00172 static int auth_fixup(void** param, int param_no)
00173 {
00174    pv_elem_t *model;
00175    str s;
00176 
00177    if (param_no == 1) {
00178       s.s = (char*)*param;
00179       if (s.s==0 || s.s[0]==0) {
00180          model = 0;
00181       } else {
00182          s.len = strlen(s.s);
00183          if (pv_parse_format(&s,&model)<0) {
00184             LM_ERR("pv_parse_format failed\n");
00185             return E_OUT_OF_MEM;
00186          }
00187       }
00188       *param = (void*)model;
00189    }
00190 
00191    return 0;
00192 }
00193 
00194 
00195 /*
00196  * Authorize using Proxy-Authorization header field
00197  */
00198 int diameter_proxy_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
00199 {
00200    /* realm parameter is converted in fixup */
00201    return authorize(_msg, (pv_elem_t*)_realm, HDR_PROXYAUTH_T);
00202 }
00203 
00204 
00205 /*
00206  * Authorize using WWW-Authorization header field
00207  */
00208 int diameter_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
00209 {
00210    return authorize(_msg, (pv_elem_t*)_realm, HDR_AUTHORIZATION_T);
00211 }
00212 
00213 
00214 static int group_fixup(void** param, int param_no)
00215 {
00216    void* ptr;
00217    str* s;
00218 
00219    if (param_no == 1) 
00220    {
00221       ptr = *param;
00222       
00223       if (!strcasecmp((char*)*param, "Request-URI")) 
00224       {
00225          *param = (void*)1;
00226          goto end;
00227       } 
00228 
00229       if(!strcasecmp((char*)*param, "To")) 
00230       {
00231          *param = (void*)2;
00232          goto end;
00233       } 
00234 
00235       if (!strcasecmp((char*)*param, "From")) 
00236       {
00237          *param = (void*)3;
00238          goto end;
00239       } 
00240 
00241       if (!strcasecmp((char*)*param, "Credentials")) 
00242       {
00243          *param = (void*)4;
00244          goto end;
00245       }
00246             
00247       LM_ERR("unsupported Header Field identifier\n");
00248       return E_UNSPEC;
00249       
00250       //pkg_free(ptr);
00251    } 
00252    
00253    if (param_no == 2) 
00254    {
00255       s = (str*)pkg_malloc(sizeof(str));
00256       if (!s) 
00257       {
00258          LM_ERR("no pkg memory left\n");
00259          return E_UNSPEC;
00260       }
00261       ptr = *param;
00262       s->s = (char*)*param;
00263       s->len = strlen(s->s);
00264       *param = (void*)s;
00265    }
00266 
00267 end:
00268 // pkg_free(ptr);
00269    return 0;
00270 }
00271 
00272 

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