Browse code

- check of database table version

Jan Janak authored on 29/11/2003 01:07:32
Showing 3 changed files
... ...
@@ -111,10 +111,10 @@ int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
111 111
 	struct hdr_field* h;
112 112
 	struct auth_body* c = 0; /* Makes gcc happy */
113 113
 
114
-	keys[0] = user_column;
115
-	keys[1] = group_column;
116
-	keys[2] = domain_column;
117
-	col[0] = group_column;
114
+	keys[0] = user_column.s;
115
+	keys[1] = group_column.s;
116
+	keys[2] = domain_column.s;
117
+	col[0] = group_column.s;
118 118
 	
119 119
 	hf_type = (long)_hf;
120 120
 
... ...
@@ -172,7 +172,7 @@ int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
172 172
 
173 173
 	VAL_STR(vals + 1) = *((str*)_grp);
174 174
 	
175
-	db_use_table(db_handle, table);
175
+	db_use_table(db_handle, table.s);
176 176
 	if (db_query(db_handle, keys, 0, vals, col, (use_domain) ? (3): (2), 1, 0, &res) < 0) {
177 177
 		LOG(L_ERR, "is_user_in(): Error while querying database\n");
178 178
 		return -5;
... ...
@@ -49,6 +49,7 @@
49 49
 
50 50
 MODULE_VERSION
51 51
 
52
+#define TABLE_VERSION 2
52 53
 
53 54
 /*
54 55
  * Module destroy function prototype
... ...
@@ -72,16 +73,28 @@ static int mod_init(void);
72 73
 static int hf_fixup(void** param, int param_no);
73 74
 
74 75
 
76
+#define TABLE "grp"
77
+#define TABLE_LEN (sizeof(TABLE) - 1)
78
+
79
+#define USER_COL "username"
80
+#define USER_COL_LEN (sizeof(USER_COL) - 1)
81
+
82
+#define DOMAIN_COL "domain"
83
+#define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
84
+
85
+#define GROUP_COL "grp"
86
+#define GROUP_COL_LEN (sizeof(GROUP_COL) - 1)
87
+
88
+
75 89
 /*
76 90
  * Module parameter variables
77 91
  */
78
-char* db_url       = DEFAULT_RODB_URL;
79
-
80
-char* table         = "grp";    /* Table name where group definitions are stored */
81
-char* user_column   = "username";
82
-char* domain_column = "domain";
83
-char* group_column  = "grp";
84
-int   use_domain    = 0;
92
+str db_url        = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
93
+str table         = {TABLE, TABLE_LEN};         /* Table name where group definitions are stored */
94
+str user_column   = {USER_COL, USER_COL_LEN};
95
+str domain_column = {DOMAIN_COL, DOMAIN_COL_LEN};
96
+str group_column  = {GROUP_COL, GROUP_COL_LEN};
97
+int use_domain    = 0;
85 98
 
86 99
 db_con_t* db_handle = 0;   /* Database connection handle */
87 100
 
... ...
@@ -99,11 +112,11 @@ static cmd_export_t cmds[] = {
99 112
  * Exported parameters
100 113
  */
101 114
 static param_export_t params[] = {
102
-	{"db_url",        STR_PARAM, &db_url       },
103
-	{"table",         STR_PARAM, &table        },
104
-	{"user_column",   STR_PARAM, &user_column  },
105
-	{"domain_column", STR_PARAM, &domain_column},
106
-	{"group_column",  STR_PARAM, &group_column },
115
+	{"db_url",        STR_PARAM, &db_url.s       },
116
+	{"table",         STR_PARAM, &table.s        },
117
+	{"user_column",   STR_PARAM, &user_column.s  },
118
+	{"domain_column", STR_PARAM, &domain_column.s},
119
+	{"group_column",  STR_PARAM, &group_column.s },
107 120
 	{"use_domain",    INT_PARAM, &use_domain},
108 121
 	{0, 0, 0}
109 122
 };
... ...
@@ -126,7 +139,7 @@ struct module_exports exports = {
126 139
 
127 140
 static int child_init(int rank)
128 141
 {
129
-	db_handle = db_init(db_url);
142
+	db_handle = db_init(db_url.s);
130 143
 	if (!db_handle) {
131 144
 		LOG(L_ERR, "group:init_child(): Unable to connect database\n");
132 145
 		return -1;
... ...
@@ -138,14 +151,41 @@ static int child_init(int rank)
138 151
 
139 152
 static int mod_init(void)
140 153
 {
154
+	int ver;
155
+
141 156
 	DBG("group module - initializing\n");
142
-	
157
+
158
+	     /* Calculate lengths */
159
+	db_url.len = strlen(db_url.s);
160
+	table.len = strlen(table.s);
161
+	user_column.len = strlen(user_column.s);
162
+	domain_column.len = strlen(domain_column.s);
163
+	group_column.len = strlen(group_column.s);
164
+
143 165
 	     /* Find a database module */
144
-	if (bind_dbmod(db_url)) {
166
+	if (bind_dbmod(db_url.s)) {
145 167
 		LOG(L_ERR, "mod_init(): Unable to bind database module\n");
146 168
 		return -1;
147 169
 	}
148 170
 
171
+	db_handle = db_init(db_url.s);
172
+	if (!db_handle) {
173
+		LOG(L_ERR, "group:mod_init(): Unable to connect database\n");
174
+		return -1;
175
+	}
176
+
177
+	ver = table_version(db_handle, &table);
178
+	if (ver < 0) {
179
+		LOG(L_ERR, "group:mod_init(): Error while querying table version\n");
180
+		db_close(db_handle);
181
+		return -1;
182
+	} else if (ver < TABLE_VERSION) {
183
+		LOG(L_ERR, "group:mod_init(): Invalid table version (use ser_mysql.sh reinstall)\n");
184
+		db_close(db_handle);
185
+		return -1;
186
+	}
187
+	
188
+	db_close(db_handle);
149 189
 	return 0;
150 190
 }
151 191
 
... ...
@@ -44,12 +44,12 @@
44 44
 /*
45 45
  * Module parameters variables
46 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 */
47
+extern str db_url;          /* Database URL */
48
+extern str table;           /* 'group' table name */
49
+extern str user_column;     /* 'user' column name in group table */
50
+extern str domain_column;   /* 'domain' column name in group table */
51
+extern str group_column;    /* "group' column name in group table */
52
+extern int use_domain;      /* Use domain in is_user_in */
53 53
 
54 54
 extern db_con_t* db_handle;   /* Database connection handle */
55 55