Browse code

Added domain module contributed by Juha Heinanen

Jan Janak authored on 14/01/2003 23:09:47
Showing 8 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+# Makefile v 1.0 2002/12/27
2
+#
3
+# Domain module makefile
4
+#
5
+# 
6
+# WARNING: do not run this directly, it should be run by the master Makefile
7
+
8
+auto_gen=
9
+NAME=domain.so
10
+LIBS=
11
+
12
+include ../../Makefile.modules
0 13
new file mode 100644
... ...
@@ -0,0 +1,40 @@
1
+#
2
+# README of domain module -- Juha Heinanen Dec 27, 2002
3
+#
4
+
5
+Domain module implements checks that based on domain table determine if
6
+a host part of an URI is "local" or not.  A "local" domain is one that
7
+the proxy is responsible of.
8
+
9
+Depends on:
10
+----------
11
+
12
+mysql
13
+
14
+Exported parameters:
15
+-------------------
16
+
17
+Name:	 domain_table
18
+Type:    string
19
+Default: "realm"
20
+Desc:    Name of table containing names of local realms that the proxy
21
+	 is responsible for.  Local users must have in their sip uri a
22
+	 host part that is equal to one of these realms.
23
+
24
+Name:	 domain_domain_column
25
+Type:	 string
26
+Default: "realm"
27
+Desc:	 Name of column containing realms in realm table
28
+
29
+Exported Functions:
30
+------------------
31
+
32
+Name:	is_from_local
33
+Params:	-
34
+Desc:	Checks based on realm table if host part of From header uri is
35
+	one of the local realms that the proxy is responsible for
36
+
37
+Name:	is_uri_host_local
38
+Params:	-
39
+Desc:	Checks based on realm table if request uri belongs to one of the
40
+	local realms
0 41
new file mode 100644
... ...
@@ -0,0 +1,115 @@
1
+/* domain.c v 0.2 2002/12/27
2
+ *
3
+ * Domain table related functions
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#include "domain_mod.h"
31
+#include "../../db/db.h"
32
+#include "../../parser/parse_uri.h"
33
+#include "../../parser/parse_from.h"
34
+#include "../../ut.h"
35
+
36
+/*
37
+ * Check if domain is local
38
+ */
39
+int is_domain_local(str* _host)
40
+{
41
+	db_key_t keys[] = {domain_domain_col};
42
+	db_val_t vals[1];
43
+	db_key_t cols[] = {domain_domain_col};
44
+	db_res_t* res;
45
+
46
+	if (db_use_table(db_handle, domain_table) < 0) {
47
+		LOG(L_ERR, "is_local(): Error while trying to use domain table\n");
48
+	}
49
+
50
+	VAL_TYPE(vals) = DB_STR;
51
+	VAL_NULL(vals) = 0;
52
+    
53
+	VAL_STR(vals).s = _host->s;
54
+	VAL_STR(vals).len = _host->len;
55
+
56
+	if (db_query(db_handle, keys, vals, cols, 1, 1, 0, &res) < 0) {
57
+		LOG(L_ERR, "is_local(): Error while querying database\n");
58
+		return -1;
59
+	}
60
+
61
+	if (RES_ROW_N(res) == 0) {
62
+		DBG("is_local(): Realm \'%.*s\' is not local\n", 
63
+		    _host->len, _host->s);
64
+		db_free_query(db_handle, res);
65
+		return -1;
66
+	} else {
67
+		DBG("is_local(): Realm \'%.*s\' is local\n", 
68
+		    _host->len, _host->s);
69
+		db_free_query(db_handle, res);
70
+		return 1;
71
+	}
72
+}
73
+
74
+/*
75
+ * Check if host in From uri is local
76
+ */
77
+int is_from_local(struct sip_msg* _msg, char* _s1, char* _s2)
78
+{
79
+	struct sip_uri uri;
80
+	int ret;
81
+
82
+	if (!_msg->from && ((parse_headers(_msg, HDR_FROM, 0) == -1) || (!_msg->from))) {
83
+		LOG(L_ERR, "is_from_local(): Can't find From header\n");
84
+		return -1;
85
+	}
86
+
87
+	if (!(_msg->from->parsed)) {
88
+		if (parse_from_header(_msg->from) == -1) {
89
+			LOG(L_ERR, "is_from_local(): Can't parse from header\n");
90
+			return -1;
91
+		}
92
+	}
93
+
94
+	if (parse_uri(get_from(_msg)->uri.s, get_from(_msg)->uri.len, &uri) < 0) {
95
+		LOG(L_ERR, "is_from_local(): Error while parsing From uri\n");
96
+		return -1;
97
+	}
98
+
99
+	ret = is_domain_local(&(uri.host));
100
+	free_uri(&uri);
101
+	return ret;
102
+}
103
+
104
+/*
105
+ * Check if host in Request URI is local
106
+ */
107
+int is_uri_host_local(struct sip_msg* _msg, char* _s1, char* _s2)
108
+{
109
+	if (parse_sip_msg_uri(_msg) < 0) {
110
+	    LOG(L_ERR, "is_uri_host_local(): Error while parsing URI\n");
111
+	    return -1;
112
+	}
113
+
114
+	return is_domain_local(&(_msg->parsed_uri.host));
115
+}
0 116
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+domain.o domain.d : domain.c domain_mod.h ../../db/db.h ../../db/db_key.h \
2
+  ../../db/db_val.h ../../str.h ../../db/db_con.h ../../db/db_row.h \
3
+  ../../db/db_res.h ../../parser/parse_uri.h ../../parser/msg_parser.h \
4
+  ../../data_lump.h ../../flags.h ../../ip_addr.h ../../dprint.h \
5
+  ../../md5utils.h ../../config.h ../../types.h ../../parser/parse_def.h \
6
+  ../../parser/parse_cseq.h ../../parser/parse_to.h \
7
+  ../../parser/parse_via.h ../../parser/parse_fline.h ../../parser/hf.h \
8
+  ../../parser/parse_from.h ../../ut.h
0 9
new file mode 100644
... ...
@@ -0,0 +1,49 @@
1
+/* domain.h v 0.1 2002/12/27
2
+ *
3
+ * Header file for domain table relates functions
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#ifndef DOMAIN_H
31
+#define DOMAIN_H
32
+		
33
+
34
+#include "../../parser/msg_parser.h"
35
+
36
+
37
+/*
38
+ * Check if host in From uri is local
39
+ */
40
+int is_from_local(struct sip_msg* _msg, char* _s1, char* _s2);
41
+
42
+
43
+/*
44
+ * Check if host in Request URI is local
45
+ */
46
+int is_uri_host_local(struct sip_msg* _msg, char* _s1, char* _s2);
47
+
48
+
49
+#endif /* DOMAIN_H */
0 50
new file mode 100644
... ...
@@ -0,0 +1,111 @@
1
+/* domain_mod.c v 0.1 2002/12/27
2
+ *
3
+ * Domain module
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#include "domain_mod.h"
31
+#include <stdio.h>
32
+#include "../../sr_module.h"
33
+#include "domain.h"
34
+
35
+
36
+/*
37
+ * Module management function prototypes
38
+ */
39
+static int mod_init(void);
40
+static void destroy(void);
41
+static int child_init(int rank);
42
+
43
+/*
44
+ * Module parameter variables
45
+ */
46
+char* db_url = "sql://serro:47serro11@localhost/ser";
47
+char* domain_table = "domain";        /* Name of domain table */
48
+char* domain_domain_col = "domain";   /* Name of domain column */
49
+
50
+
51
+/*
52
+ * Other module variables
53
+ */
54
+db_con_t* db_handle;   /* Database connection handle */
55
+
56
+
57
+struct module_exports exports = {
58
+	"domain", 
59
+	(char*[]) {"is_from_local", "is_uri_host_local"},
60
+	(cmd_function[]) {is_from_local, is_uri_host_local},
61
+	(int[]) {0, 0},
62
+	(fixup_function[]) {0, 0},
63
+	2, /* number of functions*/
64
+	(char*[]){"db_url", "domain_table", "domain_domain_column"},
65
+	(modparam_t[]){STR_PARAM, STR_PARAM, STR_PARAM},
66
+	(void*[]){&db_url, &domain_table, &domain_domain_col},
67
+	3,
68
+	
69
+	mod_init,  /* module initialization function */
70
+	NULL,      /* response function*/
71
+	destroy,   /* destroy function */
72
+	NULL,      /* cancel function */
73
+	child_init /* per-child init function */
74
+};
75
+
76
+
77
+static int mod_init(void)
78
+{
79
+	printf("Domain module - initializing\n");
80
+	
81
+	/* Check if database modulke has been laoded */
82
+	if (bind_dbmod()) {
83
+		LOG(L_ERR, "domain:mod_init(): Unable to bind database module\n");
84
+		return -1;
85
+	}
86
+
87
+	return 0;
88
+}
89
+
90
+
91
+static int child_init(int rank)
92
+{
93
+	if (db_url == NULL) {
94
+		LOG(L_ERR, "domain:init_child(): Use db_url parameter\n");
95
+		return -1;
96
+	}
97
+
98
+	db_handle = db_init(db_url);
99
+	if (!db_handle) {
100
+		LOG(L_ERR, "auth:init_child(): Unable to connect database\n");
101
+		return -1;
102
+	}
103
+	return 0;
104
+
105
+}
106
+
107
+
108
+static void destroy(void)
109
+{
110
+	db_close(db_handle);
111
+}
0 112
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+domain_mod.o domain_mod.d : domain_mod.c domain_mod.h ../../db/db.h ../../db/db_key.h \
2
+  ../../db/db_val.h ../../str.h ../../db/db_con.h ../../db/db_row.h \
3
+  ../../db/db_res.h ../../sr_module.h ../../parser/msg_parser.h \
4
+  ../../data_lump.h ../../flags.h ../../ip_addr.h ../../dprint.h \
5
+  ../../md5utils.h ../../config.h ../../types.h ../../parser/parse_def.h \
6
+  ../../parser/parse_cseq.h ../../parser/parse_to.h \
7
+  ../../parser/parse_via.h ../../parser/parse_fline.h ../../parser/hf.h \
8
+  domain.h
0 9
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+/* domain_mod.h v 0.2 2002/12/27
2
+ *
3
+ * Domain module headers
4
+ *
5
+ * Copyright (C) 2002-2003 Juha Heinanen
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ */
28
+
29
+
30
+#ifndef DOMAIN_MOD_H
31
+#define DOMAIN_MOD_H
32
+
33
+
34
+#include "../../db/db.h"
35
+
36
+
37
+/*
38
+ * Module parameters variables
39
+ */
40
+
41
+extern char* db_url;              /* Database URL */
42
+extern char* domain_table;        /* Domain table name */
43
+extern char* domain_domain_col;   /* Domain column name */
44
+
45
+
46
+/*
47
+ * Other module variables
48
+ */
49
+ 
50
+extern db_con_t* db_handle; /* Database connection handle */
51
+
52
+
53
+#endif /* DOMAIN_MOD_H */