[sr-dev] git:refs/tags/req_uri: Use (FROM, request URI) pair instead of (FROM, TO)

Andrei Pelinescu-Onciul andrei at iptel.org
Fri Sep 18 12:44:47 CEST 2009


Module: sip-router
Branch: refs/tags/req_uri
Commit: 9191576d6d861bbc56ac01283f85377bd8aa0499
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=9191576d6d861bbc56ac01283f85377bd8aa0499

Author: Miklos Tirpak <miklos at iptel.org>
Committer: Miklos Tirpak <miklos at iptel.org>
Date:   Mon Apr 28 07:04:40 2003 +0000

Use (FROM, request URI) pair instead of (FROM, TO)
New parameters exported

---

 modules_s/permissions/permissions.c |   75 +++++++++++++++++++++++------------
 1 files changed, 50 insertions(+), 25 deletions(-)

diff --git a/modules_s/permissions/permissions.c b/modules_s/permissions/permissions.c
index fbfb61a..884101e 100644
--- a/modules_s/permissions/permissions.c
+++ b/modules_s/permissions/permissions.c
@@ -33,20 +33,34 @@
 #include "rule.h"
 #include "parse_config.h"
 #include "../../parser/parse_from.h"
+#include "../../parser/parse_uri.h"
+
+MODULE_VERSION
 
 rule	*allow_rules = NULL, *deny_rules = NULL;
 
+/* Module parameter variables */
+char	*allow_file = ALLOW_FILE;
+char	*deny_file = DENY_FILE;
+
 /* Exported functions */
 static cmd_export_t cmds[] = {
         {"allow_routing", allow_routing, 0, 0, REQUEST_ROUTE},
         {0, 0, 0, 0, 0}
 };
 
+/* Exported parameters */
+static param_export_t params[] = {
+        {"allow_file", 	STR_PARAM,	&allow_file},
+        {"deny_file", 	STR_PARAM,	&deny_file},
+        {0, 0, 0}
+};
+
 /* Module interface */
 struct module_exports exports = {
         "permissions",
         cmds,      /* Exported functions */
-        0,    /* Exported parameters */
+        params,    /* Exported parameters */
         mod_init,  /* module initialization function */
         0,         /* response function */
         mod_exit,   /* destroy function */
@@ -60,8 +74,13 @@ int mod_init(void)
 	
 	fprintf(stderr, "print - initializing\n");
 
-	allow_rules = parse_config_file(ALLOW_FILE);
-	deny_rules = parse_config_file(DENY_FILE);
+	allow_rules = parse_config_file(allow_file);
+	if (allow_rules) LOG(L_INFO, "Allow file (%s) parsed\n", allow_file);
+	else LOG(L_WARN, "Allow file (%s) not found\n", allow_file);
+
+	deny_rules = parse_config_file(deny_file);
+	if (deny_rules) LOG(L_INFO, "Deny file (%s) parsed\n", deny_file);
+	else LOG(L_WARN, "Deny file (%s) not found\n", deny_file);
 	
 	return 0;
 }
@@ -80,12 +99,15 @@ return values:
 1:	allow
 */
 int allow_routing(struct sip_msg* msg, char* str1, char* str2) {
-	struct hdr_field *from, *to;
+	struct hdr_field *from;
 	int len;
-	char from_str[EXPRESSION_LENGTH+1], to_str[EXPRESSION_LENGTH+1];
+	char from_str[EXPRESSION_LENGTH+1], req_uri_str[EXPRESSION_LENGTH+1];
 	
 	/* turn off control, allow any routing */
-	if ((!allow_rules) && (!deny_rules)) return 1;
+	if ((!allow_rules) && (!deny_rules)) {
+	    LOG(L_INFO, "allow_routing(): (module permissions) No rules => allow any routing\n");
+	    return 1;
+	}
 	
 	/* looking for FROM HF */
         if ((!msg->from) && (parse_headers(msg, HDR_FROM, 0) == -1)) {
@@ -113,35 +135,38 @@ int allow_routing(struct sip_msg* msg, char* str1, char* str2) {
 	strncpy(from_str, ((struct to_body*)from->parsed)->uri.s, len);
 	from_str[len] = '\0';
 	
-	/* looking for TO HF */
-        if ((!msg->to) && (parse_headers(msg, HDR_TO, 0) == -1)) {
-                LOG(L_ERR, "allow_routing(): (module permissions) Error while parsing message\n");
-                return -1;
-        }
-	
-	to = msg->to;
-	if (!to) {
-		LOG(L_ERR, "allow_ruting(): (module permissions) TO header field not found\n");
-		return -1;
+	/* looking for request URI */
+	if (parse_sip_msg_uri(msg) < 0) {
+	        LOG(L_ERR, "allow_routing(): uri parsing failed\n");
+	        return -1;
 	}
 	
-	/* parse_to_headers is called automaticaly by parse_headers */
-	
-	len = ((struct to_body*)to->parsed)->uri.len;
+	len = msg->parsed_uri.user.len + msg->parsed_uri.host.len + 5;
 	if (len > EXPRESSION_LENGTH) {
-                LOG(L_ERR, "allow_ruting(): (module permissions) TO header field is too long: %d chars\n", len);
+                LOG(L_ERR, "allow_ruting(): (module permissions) Request URI is too long: %d chars\n", len);
                 return -1;
 	}
-	strncpy(to_str, ((struct to_body*)to->parsed)->uri.s, len);
-	to_str[len] = '\0';
 	
+	strcpy(req_uri_str, "sip:");
+	memcpy(req_uri_str + 4, msg->parsed_uri.user.s, msg->parsed_uri.user.len);
+	req_uri_str[msg->parsed_uri.user.len + 4] = '@';
+	memcpy(req_uri_str + msg->parsed_uri.user.len + 5, msg->parsed_uri.host.s, msg->parsed_uri.host.len);
+	req_uri_str[len] = '\0';
+	
+        LOG(L_INFO, "allow_ruting(): (module permissions) looking for FROM: %s Request URI: %s\n", from_str, req_uri_str);
 	/* rule exists in allow file */
-	if (search_rule(allow_rules, from_str, to_str)) return 1;
+	if (search_rule(allow_rules, from_str, req_uri_str)) {
+    		LOG(L_INFO, "allow_ruting(): (module permissions) allow roule found => routing is allowed\n");
+		return 1;
+	}
 	
 	/* rule exists in deny file */
-	if (search_rule(deny_rules, from_str, to_str)) return -1;
-	
+	if (search_rule(deny_rules, from_str, req_uri_str)) {
+		LOG(L_INFO, "allow_ruting(): (module permissions) deny roule found => routing is denied\n");
+		return -1;
+	}
 	/* allow any other rule */
+	LOG(L_INFO, "allow_ruting(): (module permissions) neither allow nor deny roule found => routing is allowed\n");
 	return 1;
 }
 




More information about the sr-dev mailing list