src/modules/auth_diameter/auth_diameter.c
31ccf6a2
 /*
  * Digest Authentication - Diameter support
  *
  * Copyright (C) 2001-2003 FhG Fokus
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
31ccf6a2
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
31ccf6a2
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  * 
27642a08
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
31ccf6a2
  *
  */
cfbd60f8
 
31ccf6a2
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netdb.h> 
 
cf83221d
 #include "../../core/sr_module.h"
 #include "../../core/error.h"
 #include "../../core/dprint.h"
 #include "../../core/pvar.h"
 #include "../../core/mem/mem.h"
31ccf6a2
 
 #include "diameter_msg.h"
 #include "auth_diameter.h"
 #include "authorize.h"
 #include "tcp_comm.h"
 
 MODULE_VERSION
 
 
a5b9f42d
 /** SL API structure */
 sl_api_t slb;
31ccf6a2
 
 static int mod_init(void);                        /* Module initialization function*/
 static int mod_child_init(int r);                 /* Child initialization function*/
cfbd60f8
 static int auth_fixup(void** param, int param_no);
 static int group_fixup(void** param, int param_no);
31ccf6a2
 
 int diameter_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2);
 int diameter_proxy_authorize(struct sip_msg* _msg, char* _realm, char* _s2);
 int diameter_is_user_in(struct sip_msg* _msg, char* group, char* _s2);
 
 /*
  * Module parameter variables
  */
 char* diameter_client_host = "localhost";
 int diameter_client_port = 3000;
 int use_domain = 0;
f3a37060
 int sockfd = -1;
31ccf6a2
 
 rd_buf_t *rb;
 
 /*
  * Exported functions
  */
 static cmd_export_t cmds[] = {
c4d2e802
 	{"diameter_www_authorize",   (cmd_function)diameter_www_authorize,   1, auth_fixup,
80998a7f
 			0, REQUEST_ROUTE},
c4d2e802
 	{"diameter_proxy_authorize", (cmd_function)diameter_proxy_authorize, 1, auth_fixup,
80998a7f
 			0, REQUEST_ROUTE},
c4d2e802
 	{"diameter_is_user_in",      (cmd_function)diameter_is_user_in,      2, group_fixup,
80998a7f
 			0, REQUEST_ROUTE},
 	{0, 0, 0, 0, 0, 0}
31ccf6a2
 };
 
 
 /*
  * Exported parameters
  */
 static param_export_t params[] = {
0ce3ac80
 	{"diameter_client_host", PARAM_STRING, &diameter_client_host},
31ccf6a2
 	{"diameter_client_port", INT_PARAM, &diameter_client_port},
 	{"use_domain", INT_PARAM, &use_domain},
 	{0, 0, 0}
 };
 
 
 /*
  * Module interface
  */
 struct module_exports exports = {
de26de44
 	"auth_diameter",
51716422
 	DEFAULT_DLFLAGS, /* dlopen flags */
83fd4841
 	cmds,            /* Exported functions */
 	params,          /* Exported parameters */
 	0,               /* exported RPC methods */
 	0,               /* exported pseudo-variables */
 	0,               /* response function */
 	mod_init,        /* module initialization function */
 	mod_child_init,  /* child initialization function */
 	0                /* destroy function */
31ccf6a2
 };
 
 
 /*
  * Module initialization function
  */
 static int mod_init(void)
 {
789504a6
 	LM_DBG("auth_diameter - Initializing\n");
31ccf6a2
 
a5b9f42d
 	/* bind the SL API */
 	if (sl_load_api(&slb)!=0) {
 		LM_ERR("cannot bind to SL API\n");
31ccf6a2
 		return -1;
 	}
 	
 	return 0;
 }
 
 static int mod_child_init(int r)
 {	
 	/* open TCP connection */
789504a6
 	LM_DBG("initializing TCP connection\n");
31ccf6a2
 
 	sockfd = init_mytcp(diameter_client_host, diameter_client_port);
 	if(sockfd==-1) 
 	{
789504a6
 		LM_DBG("the TCP connection was not established\n");
31ccf6a2
 		return -1;
 	}
 
789504a6
 	LM_DBG("the TCP connection was established on socket=%d\n", sockfd);
31ccf6a2
 	
 	rb = (rd_buf_t*)pkg_malloc(sizeof(rd_buf_t));
 	if(!rb)
 	{
789504a6
 		LM_DBG("no more free pkg memory\n");
31ccf6a2
 		return -1;
 	}
 	rb->buf = 0;
 	rb->chall = 0;
 
 	return 0;
 }
 
 #if 0
 static void destroy(void)
 {
 	close_tcp_connection(sockfd);
 }
 #endif
 
 
 /*
b0a7f212
  * Convert char* parameter to pv_elem_t* parameter
31ccf6a2
  */
cfbd60f8
 static int auth_fixup(void** param, int param_no)
31ccf6a2
 {
b0a7f212
 	pv_elem_t *model;
 	str s;
cfbd60f8
 
 	if (param_no == 1) {
b0a7f212
 		s.s = (char*)*param;
 		if (s.s==0 || s.s[0]==0) {
cfbd60f8
 			model = 0;
 		} else {
b0a7f212
 			s.len = strlen(s.s);
 			if (pv_parse_format(&s,&model)<0) {
789504a6
 				LM_ERR("pv_parse_format failed\n");
cfbd60f8
 				return E_OUT_OF_MEM;
 			}
31ccf6a2
 		}
cfbd60f8
 		*param = (void*)model;
31ccf6a2
 	}
 
 	return 0;
 }
 
cfbd60f8
 
31ccf6a2
 /*
  * Authorize using Proxy-Authorization header field
  */
 int diameter_proxy_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
 {
75739b85
 	/* realm parameter is converted in fixup */
b0a7f212
 	return authorize(_msg, (pv_elem_t*)_realm, HDR_PROXYAUTH_T);
31ccf6a2
 }
 
 
 /*
  * Authorize using WWW-Authorization header field
  */
 int diameter_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
 {
b0a7f212
 	return authorize(_msg, (pv_elem_t*)_realm, HDR_AUTHORIZATION_T);
31ccf6a2
 }
 
cfbd60f8
 
31ccf6a2
 static int group_fixup(void** param, int param_no)
 {
 	str* s;
 
 	if (param_no == 1) 
 	{
 		if (!strcasecmp((char*)*param, "Request-URI")) 
 		{
 			*param = (void*)1;
 			goto end;
 		} 
 
 		if(!strcasecmp((char*)*param, "To")) 
 		{
 			*param = (void*)2;
 			goto end;
 		} 
 
 		if (!strcasecmp((char*)*param, "From")) 
 		{
 			*param = (void*)3;
 			goto end;
 		} 
 
 		if (!strcasecmp((char*)*param, "Credentials")) 
 		{
 			*param = (void*)4;
 			goto end;
 		}
 				
789504a6
 		LM_ERR("unsupported Header Field identifier\n");
31ccf6a2
 		return E_UNSPEC;
 	} 
 	
 	if (param_no == 2) 
 	{
 		s = (str*)pkg_malloc(sizeof(str));
 		if (!s) 
 		{
789504a6
 			LM_ERR("no pkg memory left\n");
31ccf6a2
 			return E_UNSPEC;
 		}
 		s->s = (char*)*param;
 		s->len = strlen(s->s);
 		*param = (void*)s;
 	}
 
 end:
 	return 0;
 }