Browse code

Group membership functions, originaly part of auth module.

Jan Janak authored on 26/02/2003 14:03:47
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+# $Id$
2
+#
3
+# group example 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=group.so
10
+LIBS=
11
+
12
+include ../../Makefile.modules
0 13
new file mode 100644
... ...
@@ -0,0 +1,208 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Group membership
5
+ *
6
+ * Copyright (C) 2001-2003 Fhg Fokus
7
+ *
8
+ * This file is part of ser, a free SIP server.
9
+ *
10
+ * ser is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * For a license to use the ser software under conditions
16
+ * other than those described here, or to purchase support for this
17
+ * software, please contact iptel.org by e-mail at the following addresses:
18
+ *    info@iptel.org
19
+ *
20
+ * ser is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License 
26
+ * along with this program; if not, write to the Free Software 
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History:
30
+ * --------
31
+ * 2003-02-25 - created by janakj
32
+ *
33
+ */
34
+
35
+
36
+#include <string.h>
37
+#include "../../dprint.h"               /* Logging */
38
+#include "../../db/db.h"                /* Generic database API */
39
+#include "../../parser/digest/digest.h" /* get_authorized_cred */
40
+#include "../../parser/hf.h"            /* Header Field types */
41
+#include "../../parser/parse_from.h"    /* From parser */
42
+#include "../../parser/parse_uri.h"
43
+#include "group.h"
44
+#include "group_mod.h"                   /* Module parameters */
45
+
46
+
47
+/*
48
+ * Extract username from Request-URI
49
+ */
50
+static inline int get_request_uri(struct sip_msg* _m, str* _u)
51
+{
52
+	if (_m->new_uri.s) {
53
+		_u->s = _m->new_uri.s;
54
+		_u->len = _m->new_uri.len;
55
+	} else {
56
+		_u->s = _m->first_line.u.request.uri.s;
57
+		_u->len = _m->first_line.u.request.uri.len;
58
+	}
59
+
60
+	return 0;
61
+}
62
+
63
+
64
+/*
65
+ * Extract username from To header field
66
+ */
67
+static inline int get_to_uri(struct sip_msg* _m, str* _u)
68
+{
69
+	if (!_m->to && ((parse_headers(_m, HDR_TO, 0) == -1) || (!_m->to))) {
70
+		LOG(L_ERR, "get_to_uri(): Can't get To header field\n");
71
+		return -1;
72
+	}
73
+	
74
+	_u->s = ((struct to_body*)_m->to->parsed)->uri.s;
75
+	_u->len = ((struct to_body*)_m->to->parsed)->uri.len;
76
+
77
+	return 0;
78
+}
79
+
80
+
81
+/*
82
+ * Extract username from From header field
83
+ */
84
+static inline int get_from_uri(struct sip_msg* _m, str* _u)
85
+{
86
+	if (parse_from_header(_m) < 0) {
87
+		LOG(L_ERR, "get_from_uri(): Error while parsing From body\n");
88
+		return -1;
89
+	}
90
+	
91
+	_u->s = ((struct to_body*)_m->from->parsed)->uri.s;
92
+	_u->len = ((struct to_body*)_m->from->parsed)->uri.len;
93
+
94
+	return 0;
95
+}
96
+
97
+
98
+/*
99
+ * Extract username from digest credentials
100
+ */
101
+static inline int get_cred_user(struct sip_msg* _m, str* _u)
102
+{
103
+	struct hdr_field* h;
104
+	auth_body_t* c;
105
+	
106
+	get_authorized_cred(_m->authorization, &h);
107
+	if (!h) {
108
+		get_authorized_cred(_m->proxy_auth, &h);
109
+		if (!h) {
110
+			LOG(L_ERR, "get_cred_user(): No authorized credentials found (error in scripts)\n");
111
+			return -1;
112
+		}
113
+	}
114
+	
115
+	c = (auth_body_t*)(h->parsed);
116
+
117
+	_u->s = c->digest.username.s;
118
+	_u->len = c->digest.username.len;
119
+
120
+	return 0;
121
+}
122
+
123
+
124
+/*
125
+ * Check if username in specified header field is in a table
126
+ */
127
+int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
128
+{
129
+	db_key_t keys[3] = {user_column, group_column, domain_column};
130
+	db_val_t vals[3];
131
+	db_key_t col[1] = {group_column};
132
+	db_res_t* res;
133
+	str uri, user;
134
+	int hf_type;
135
+	struct sip_uri puri;
136
+	
137
+	hf_type = (int)_hf;
138
+
139
+	switch(hf_type) {
140
+	case 1: /* Request-URI */
141
+		if (get_request_uri(_msg, &uri) < 0) {
142
+			LOG(L_ERR, "is_user_in(): Error while obtaining username from Request-URI\n");
143
+			return -1;
144
+		}
145
+		break;
146
+
147
+	case 2: /* To */
148
+		if (get_to_uri(_msg, &uri) < 0) {
149
+			LOG(L_ERR, "is_user_in(): Error while extracting To username\n");
150
+			return -2;
151
+		}
152
+		break;
153
+
154
+	case 3: /* From */
155
+		if (get_from_uri(_msg, &uri) < 0) {
156
+			LOG(L_ERR, "is_user_in(): Error while extracting From username\n");
157
+			return -3;
158
+		}
159
+		break;
160
+
161
+	case 4: /* Credentials */
162
+		if (get_cred_user(_msg, &user) < 0) {
163
+			LOG(L_ERR, "is_user_in(): Error while extracting digest username\n");
164
+			return -4;
165
+		}
166
+		break;
167
+	}
168
+
169
+	if (hf_type != 4) {
170
+		if (parse_uri(uri.s, uri.len, &puri) < 0) {
171
+			LOG(L_ERR, "is_user_in(): Error while parsing URI\n");
172
+			return -5;
173
+		}
174
+
175
+		if (use_domain) {
176
+			VAL_TYPE(vals + 2) = DB_STR;
177
+			VAL_NULL(vals + 2) = 0;
178
+			VAL_STR(vals + 2) = puri.host;
179
+		}
180
+
181
+		VAL_STR(vals) = puri.user;
182
+	} else {
183
+		VAL_STR(vals) = user;
184
+	}
185
+	
186
+	VAL_TYPE(vals) = VAL_TYPE(vals + 1) = DB_STR;
187
+	VAL_NULL(vals) = VAL_NULL(vals + 1) = 0;
188
+
189
+	VAL_STR(vals + 1) = *((str*)_grp);
190
+	
191
+	db_use_table(db_handle, table);
192
+	if (db_query(db_handle, keys, 0, vals, col, (use_domain && (hf_type != 4)) ? (3): (2), 1, 0, &res) < 0) {
193
+		LOG(L_ERR, "is_user_in(): Error while querying database\n");
194
+		return -5;
195
+	}
196
+	
197
+	if (RES_ROW_N(res) == 0) {
198
+		DBG("is_user_in(): User is not in group \'%.*s\'\n", 
199
+		    ((str*)_grp)->len, ((str*)_grp)->s);
200
+		db_free_query(db_handle, res);
201
+		return -6;
202
+	} else {
203
+		DBG("is_user_in(): User is in group \'%.*s\'\n", 
204
+		    ((str*)_grp)->len, ((str*)_grp)->s);
205
+		db_free_query(db_handle, res);
206
+		return 1;
207
+	}
208
+}
0 209
new file mode 100644
... ...
@@ -0,0 +1,48 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Group membership
5
+ *
6
+ * Copyright (C) 2001-2003 Fhg Fokus
7
+ *
8
+ * This file is part of ser, a free SIP server.
9
+ *
10
+ * ser is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * For a license to use the ser software under conditions
16
+ * other than those described here, or to purchase support for this
17
+ * software, please contact iptel.org by e-mail at the following addresses:
18
+ *    info@iptel.org
19
+ *
20
+ * ser is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License 
26
+ * along with this program; if not, write to the Free Software 
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History:
30
+ * --------
31
+ * 2003-02-25 - created by janakj
32
+ *
33
+ */
34
+
35
+
36
+#ifndef GROUP_H
37
+#define GROUP_H
38
+
39
+#include "../../parser/msg_parser.h"
40
+
41
+
42
+/*
43
+ * Check if username in specified header field is in a table
44
+ */
45
+int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp);
46
+
47
+
48
+#endif /* GROUP_H */
0 49
new file mode 100644
... ...
@@ -0,0 +1,214 @@
1
+/* 
2
+ * $Id$ 
3
+ *
4
+ * Group membership - module interface
5
+ *
6
+ * Copyright (C) 2001-2003 Fhg Fokus
7
+ *
8
+ * This file is part of ser, a free SIP server.
9
+ *
10
+ * ser is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * For a license to use the ser software under conditions
16
+ * other than those described here, or to purchase support for this
17
+ * software, please contact iptel.org by e-mail at the following addresses:
18
+ *    info@iptel.org
19
+ *
20
+ * ser is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License 
26
+ * along with this program; if not, write to the Free Software 
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History:
30
+ * --------
31
+ * 2003-02-25 - created by janakj
32
+ */
33
+
34
+
35
+#include <stdio.h>
36
+#include <stdlib.h>
37
+#include <string.h>
38
+#include "../../sr_module.h"
39
+#include "../../dprint.h"
40
+#include "../../ut.h"
41
+#include "../../error.h"
42
+#include "../../mem/mem.h"
43
+#include "group_mod.h"
44
+#include "group.h"
45
+
46
+
47
+/*
48
+ * Module destroy function prototype
49
+ */
50
+static void destroy(void);
51
+
52
+
53
+/*
54
+ * Module child-init function prototype
55
+ */
56
+static int child_init(int rank);
57
+
58
+
59
+/*
60
+ * Module initialization function prototype
61
+ */
62
+static int mod_init(void);
63
+
64
+
65
+/* Header field fixup */
66
+static int hf_fixup(void** param, int param_no);
67
+
68
+
69
+/*
70
+ * Module parameter variables
71
+ */
72
+char* db_url       = "sql://serro:47serro11@localhost/ser";
73
+
74
+char* table        = "grp";    /* Table name where group definitions are stored */
75
+char* user_col     = "user";
76
+char* domain_col   = "domain";
77
+char* grp_col      = "grp";
78
+int   use_domain   = 0;
79
+
80
+db_con_t* db_handle = 0;   /* Database connection handle */
81
+
82
+
83
+/*
84
+ * Module interface
85
+ */
86
+struct module_exports exports = {
87
+	"group", 
88
+	(char*[]) { 
89
+		"is_user_in"
90
+	},
91
+	(cmd_function[]) {
92
+		is_user_in
93
+	},
94
+	(int[]) {2},
95
+	(fixup_function[]) {
96
+		hf_fixup
97
+	},
98
+	1,
99
+	
100
+	(char*[]) {
101
+		"db_url",              /* Database URL */
102
+		"table",               /* Group table name */
103
+		"user_column",         /* Group table user column name */
104
+		"domain_column",       /* Group table domain column name */
105
+		"group_column",        /* Group table group column name */
106
+		"use_domain"   
107
+	},   /* Module parameter names */
108
+	(modparam_t[]) {
109
+		STR_PARAM,
110
+		STR_PARAM,
111
+		STR_PARAM,
112
+		STR_PARAM,
113
+		STR_PARAM,
114
+	        INT_PARAM
115
+	},   /* Module parameter types */
116
+	(void*[]) {
117
+		&db_url,
118
+		&table,
119
+		&user_column,
120
+		&domain_column,
121
+		&group_column,
122
+		&use_domain
123
+	},   /* Module parameter variable pointers */
124
+	6,
125
+	mod_init,   /* module initialization function */
126
+	NULL,       /* response function */
127
+	destroy,    /* destroy function */
128
+	0,       /* oncancel function */
129
+	child_init  /* child initialization function */
130
+};
131
+
132
+
133
+static int child_init(int rank)
134
+{
135
+	if (db_url == 0) {
136
+		LOG(L_ERR, "group:init_child(): Use db_url parameter\n");
137
+		return -1;
138
+	}
139
+
140
+	db_handle = db_init(db_url);
141
+	if (!db_handle) {
142
+		LOG(L_ERR, "group:init_child(): Unable to connect database\n");
143
+		return -1;
144
+	}
145
+
146
+	return 0;
147
+}
148
+
149
+
150
+static int mod_init(void)
151
+{
152
+	printf("group module - initializing\n");
153
+	
154
+	     /* Find a database module */
155
+	if (bind_dbmod()) {
156
+		LOG(L_ERR, "mod_init(): Unable to bind database module\n");
157
+		return -1;
158
+	}
159
+
160
+	return 0;
161
+}
162
+
163
+
164
+static void destroy(void)
165
+{
166
+	if (db_handle) {
167
+		db_close(db_handle);
168
+	}
169
+}
170
+
171
+
172
+/*
173
+ * Convert HF description string to hdr_field pointer
174
+ *
175
+ * Supported strings: 
176
+ * "Request-URI", "To", "From", "Credentials"
177
+ */
178
+static int hf_fixup(void** param, int param_no)
179
+{
180
+	void* ptr;
181
+	str* s;
182
+
183
+	if (param_no == 1) {
184
+		ptr = *param;
185
+		
186
+		if (!strcasecmp((char*)*param, "Request-URI")) {
187
+			*param = (void*)1;
188
+		} else if (!strcasecmp((char*)*param, "To")) {
189
+			*param = (void*)2;
190
+		} else if (!strcasecmp((char*)*param, "From")) {
191
+			*param = (void*)3;
192
+		} else if (!strcasecmp((char*)*param, "Credentials")) {
193
+			*param = (void*)4;
194
+		} else {
195
+			LOG(L_ERR, "hf_fixup(): Unsupported Header Field identifier\n");
196
+			return E_UNSPEC;
197
+		}
198
+
199
+		free(ptr);
200
+	} else if (param_no == 2) {
201
+		s = (str*)malloc(sizeof(str));
202
+		if (!s) {
203
+			LOG(L_ERR, "hf_fixup(): No memory left\n");
204
+			return E_UNSPEC;
205
+		}
206
+
207
+		s->s = (char*)*param;
208
+		s->len = strlen(s->s);
209
+		*param = (void*)s;
210
+	}
211
+
212
+	return 0;
213
+}
214
+
0 215
new file mode 100644
... ...
@@ -0,0 +1,56 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Group membership 
5
+ *
6
+ * Copyright (C) 2001-2003 Fhg Fokus
7
+ *
8
+ * This file is part of ser, a free SIP server.
9
+ *
10
+ * ser is free software; you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * For a license to use the ser software under conditions
16
+ * other than those described here, or to purchase support for this
17
+ * software, please contact iptel.org by e-mail at the following addresses:
18
+ *    info@iptel.org
19
+ *
20
+ * ser is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License 
26
+ * along with this program; if not, write to the Free Software 
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History:
30
+ * --------
31
+ * 2003-02-25 - created by janakj
32
+ */
33
+
34
+
35
+#ifndef GROUP_MOD_H
36
+#define GROUP_MOD_H
37
+
38
+#include "../../db/db.h"
39
+#include "../../str.h"
40
+#include "../../parser/digest/digest.h" /* auth_body_t */
41
+#include "../../parser/msg_parser.h"    /* struct sip_msg */
42
+
43
+
44
+/*
45
+ * Module parameters variables
46
+ */
47
+extern char* db_url;          /* Database URL */
48
+extern char* table;           /* 'group' table name */
49
+extern char* user_column;     /* 'user' column name in group table */
50
+extern char* domain_column;   /* 'domain' column name in group table */
51
+extern char* group_column;    /* "group' column name in group table */
52
+extern int use_domain;        /* Use domain in is_user_in */
53
+
54
+extern db_con_t* db_handle;   /* Database connection handle */
55
+
56
+#endif /* GROUP_MOD_H */