Browse code

-fifo command added for removing CPl scripts from database

Bogdan-Andrei Iancu authored on 21/08/2003 10:50:44
Showing 5 changed files
... ...
@@ -217,14 +217,6 @@ static int cpl_init(void)
217 217
 		goto error;
218 218
 	}
219 219
 
220
-	/* bind the sl_send_reply function
221
-	sl_send_rpl = find_export("sl_send_reply", 2, REQUEST_ROUTE);
222
-	if (sl_send_rpl==0) {
223
-		LOG(L_CRIT,"ERROR:cpl_init: cannot find \"sl_send_reply\" function! "
224
-			"Did you forget to load the sl module ?\n");
225
-		goto error;
226
-	}*/
227
-
228 220
 	/* import the TM auto-loading function */
229 221
 	if ( !(load_tm=(load_tm_f)find_export("load_tm", NO_SCRIPT, 0))) {
230 222
 		LOG(L_ERR, "ERROR:cpl_c:cpl_init: cannot import load_tm\n");
... ...
@@ -234,9 +226,13 @@ static int cpl_init(void)
234 226
 	if (load_tm( &cpl_tmb )==-1)
235 227
 		goto error;
236 228
 
237
-	/* register the fifo command */
238
-	if (register_fifo_cmd( cpl_loader, "LOAD_CPL", 0)!=1) {
239
-		LOG(L_CRIT,"ERROR:cpl_init: cannot register fifo command!\n");
229
+	/* register the fifo commands */
230
+	if (register_fifo_cmd( cpl_load, "LOAD_CPL", 0)!=1) {
231
+		LOG(L_CRIT,"ERROR:cpl_init: cannot register LOAD_CPL fifo cmd!\n");
232
+		goto error;
233
+	}
234
+	if (register_fifo_cmd( cpl_remove, "REMOVE_CPL", 0)!=1) {
235
+		LOG(L_CRIT,"ERROR:cpl_init: cannot register REMOVE_CPL fifo cmd!\n");
240 236
 		goto error;
241 237
 	}
242 238
 
... ...
@@ -91,6 +91,11 @@ error:
91 91
 
92 92
 
93 93
 
94
+/* inserts into database a cpl script in XML format(xml) along with its binary
95
+ * format (bin)
96
+ * Returns:  1 - success
97
+ *          -1 - error
98
+ */
94 99
 int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin)
95 100
 {
96 101
 	db_key_t   keys[] = {"user","cpl_xml","cpl_bin"};
... ...
@@ -147,3 +152,27 @@ error:
147 152
 }
148 153
 
149 154
 
155
+/* delete from database the entiry record for a given user - if a user has no
156
+ * script, he will be removed complitly from db; users without script are not
157
+ * allowed into db ;-)
158
+ * Returns:  1 - success
159
+ *          -1 - error
160
+ */
161
+int rmv_from_db(db_con_t *db_con, char *usr)
162
+{
163
+	db_key_t   keys[] = {"user"};
164
+	db_val_t   vals[1];
165
+
166
+	/* username */
167
+	vals[0].type = DB_STRING;
168
+	vals[0].nul  = 0;
169
+	vals[0].val.string_val = usr;
170
+
171
+	if (db_delete(db_con, keys, NULL, vals, 1) < 0) {
172
+		LOG(L_ERR,"ERROR:cpl-c:rmv_from_db: error when deleting script for "
173
+			"user \"%s\"\n",usr);
174
+		return -1;
175
+	}
176
+
177
+	return 1;
178
+}
... ...
@@ -31,8 +31,28 @@
31 31
 #include "../../db/db.h"
32 32
 
33 33
 
34
+/* inserts into database a cpl script in XML format(xml) along with its binary
35
+ * format (bin)
36
+ * Returns:  1 - success
37
+ *          -1 - error
38
+ */
34 39
 int write_to_db(db_con_t *db_con, char *usr, str *xml, str *bin);
40
+
41
+
42
+/* fetch from database the binary format of the cpl script for a given user
43
+ * Returns:  1 - success
44
+ *          -1 - error
45
+ */
35 46
 int get_user_script( db_con_t *db_hdl, str *user, str *script);
36 47
 
37 48
 
49
+/* delete from database the entiry record for a given user - if a user has no
50
+ * script, he will be removed complitly from db; users without script are not
51
+ * allowed into db ;-)
52
+ * Returns:  1 - success
53
+ *          -1 - error
54
+ */
55
+int rmv_from_db(db_con_t *db_con, char *usr);
56
+
57
+
38 58
 #endif
... ...
@@ -27,6 +27,7 @@
27 27
  *
28 28
  * History:
29 29
  * -------
30
+ * 2003-08-21: cpl_remove() added (bogdan)
30 31
  * 2003-06-24: file created (bogdan)
31 32
  */
32 33
 
... ...
@@ -53,6 +54,10 @@
53 54
 extern char *dtd_file;
54 55
 extern db_con_t* db_hdl;
55 56
 
57
+
58
+
59
+
60
+/* debug function -> write into a file the content of a str stuct. */
56 61
 int write_to_file(char *filename, str *buf)
57 62
 {
58 63
 	int fd;
... ...
@@ -81,7 +86,11 @@ error:
81 86
 
82 87
 
83 88
 
84
-
89
+/* Loads a file into a buffer; first the file lenght will be determined for
90
+ * allocated an exact buffer len for storing the file content into.
91
+ * Returns:  1 - success
92
+ *          -1 - error
93
+ */
85 94
 int load_file( char *filename, str *xml)
86 95
 {
87 96
 	int n;
... ...
@@ -135,7 +144,7 @@ int load_file( char *filename, str *xml)
135 144
 		}
136 145
 	}
137 146
 	if (xml->len!=offset) {
138
-		LOG(L_ERR,"ERROR:cpl:load_file: couldn't read all file!\n");
147
+		LOG(L_ERR,"ERROR:cpl-c:load_file: couldn't read all file!\n");
139 148
 		goto error;
140 149
 	}
141 150
 	xml->s[xml->len] = 0;
... ...
@@ -148,56 +157,102 @@ error:
148 157
 
149 158
 
150 159
 
151
-
152
-
153
-
154
-int cpl_loader( FILE *fifo_stream, char *response_file )
160
+/* Triggered by fifo server -> implements LOAD_CPL command
161
+ * Command format:
162
+ * -----------------------
163
+ *   :LOAD_CPL:
164
+ *   username
165
+ *   cpl_filename
166
+ *   <empty line>
167
+ * -----------------------
168
+ * For the given user, loads the XML cpl file, compile it into binary format
169
+ * and store both format into database
170
+ */
171
+int cpl_load( FILE *fifo_stream, char *response_file )
155 172
 {
156 173
 	static char user[MAX_STATIC_BUF];
157 174
 	static char cpl_file[MAX_STATIC_BUF];
158 175
 	int user_len;
159 176
 	int cpl_file_len;
160
-	str xml;
161
-	str bin;
177
+	str xml = {0,0};
178
+	str bin = {0,0};
162 179
 
163
-	DBG("DEBUG:cpl_loader: FIFO commnad received!\n");
180
+	DBG("DEBUG:cpl-c:cpl_load: \"LOAD_CPL\" FIFO commnad received!\n");
164 181
 
165 182
 	/* first line must be the username */
166 183
 	if (read_line( user, MAX_STATIC_BUF-1 , fifo_stream, &user_len )!=1 ||
167 184
 	user_len<=0) {
168
-		LOG(L_ERR,"ERROR:cpl:cpl_loader: unable to read username from "
185
+		LOG(L_ERR,"ERROR:cpl-c:cpl_load: unable to read username from "
169 186
 			"FIFO command\n");
170 187
 		goto error;
171 188
 	}
172 189
 	user[user_len] = 0;
173
-	DBG("DEBUG:cpl_loader: user=%.*s\n",user_len,user);
190
+	DBG("DEBUG:cpl_load: user=%.*s\n",user_len,user);
174 191
 
175 192
 	/* second line must be the cpl file */
176 193
 	if (read_line( cpl_file, MAX_STATIC_BUF-1,fifo_stream,&cpl_file_len)!=1 ||
177 194
 	cpl_file_len<=0) {
178
-		LOG(L_ERR,"ERROR:cpl:cpl_loader: unable to read cpl_file name from "
195
+		LOG(L_ERR,"ERROR:cpl-c:cpl_load: unable to read cpl_file name from "
179 196
 			"FIFO command\n");
180 197
 		goto error;
181 198
 	}
182 199
 	cpl_file[cpl_file_len] = 0;
183
-	DBG("DEBUG:cpl_loader: cpl file=%.*s\n",cpl_file_len,cpl_file);
200
+	DBG("DEBUG:cpl-c:cpl_load: cpl file=%.*s\n",cpl_file_len,cpl_file);
184 201
 
185
-	/* load the xml file */
202
+	/* load the xml file - this function will allocted a buff for the loading
203
+	 * the cpl file and attach it to xml.s -> don't forget to free it! */
186 204
 	if (load_file( cpl_file, &xml)!=1)
187
-		return -1;
205
+		goto error;
188 206
 
189 207
 	/* get the binary coding for the XML file */
190 208
 	if ( encodeXML( &xml, dtd_file , &bin)!=1)
191
-		return -1;
209
+		goto error1;
192 210
 
193 211
 	/* write both the XML and binary formats into database */
194 212
 	if (write_to_db( db_hdl, user, &xml, &bin)!=1)
195
-		return -1;
213
+		goto error1;
196 214
 
197 215
 	/* free the memory used for storing the cpl script in XML format */
198 216
 	pkg_free( xml.s );
199 217
 
200
-	//write_to_file("cript.ccc", bin.s, bin.len);
218
+	return 1;
219
+error1:
220
+	pkg_free ( xml.s );
221
+error:
222
+	return -1;
223
+}
224
+
225
+
226
+
227
+/* Triggered by fifo server -> implements REMOVE_CPL command
228
+ * Command format:
229
+ * -----------------------
230
+ *   :REMOVE_CPL:
231
+ *   username
232
+ *   <empty line>
233
+ * -----------------------
234
+ * For the given user, remove the entire database record
235
+ * (XML cpl and binary cpl); user with empty cpl scripts are not accepted
236
+ */
237
+int cpl_remove( FILE *fifo_stream, char *response_file )
238
+{
239
+	static char user[MAX_STATIC_BUF];
240
+	int user_len;
241
+
242
+	DBG("DEBUG:cpl-c:cpl_remove: \"REMOVE_CPL\" FIFO commnad received!\n");
243
+
244
+	/* first line must be the username */
245
+	if (read_line( user, MAX_STATIC_BUF-1 , fifo_stream, &user_len )!=1 ||
246
+	user_len<=0) {
247
+		LOG(L_ERR,"ERROR:cpl-c:cpl_remove: unable to read username from "
248
+			"FIFO command\n");
249
+		goto error;
250
+	}
251
+	user[user_len] = 0;
252
+	DBG("DEBUG:cpl-c:cpl_remove: user=%.*s\n",user_len,user);
253
+
254
+	if (rmv_from_db( db_hdl, user)!=1)
255
+		goto error;
201 256
 
202 257
 	return 1;
203 258
 error:
... ...
@@ -34,7 +34,8 @@
34 34
 #define _CPL_LOADER_H
35 35
 
36 36
 
37
-int cpl_loader( FILE *fifo_stream, char *response_file );
37
+int cpl_load( FILE *fifo_stream, char *response_file );
38
+int cpl_remove( FILE *fifo_stream, char *response_file );
38 39
 
39 40
 #endif
40 41