Debian Codegraph @

database/eurephiadb_mapping.c

raw ◀ ▶
1 Debian patch touch this file
1 /* eurephiadb_common.c -- Database field mapping between database and the C code
2 *
3 * GPLv2 only - Copyright (C) 2008 - 2012
4 * David Sommerseth <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
9 * of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
21
22 /**
23 * @file eurephiadb_mapping.c
24 * @author David Sommerseth <[email protected]>
25 * @date 2008-12-06
26 *
27 * @brief Database field mapping between database and the C code/programs
28 *
29 * This feature makes it possible to let each database driver implementation to
30 * use its own table and column/field names in the database, while having a
31 * unified interface for table names and field names in the application.
32 *
33 */
34
•35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38
39 #include <libxml/tree.h>
40
41 #define EUREPHIADB_MAPPING_C
42 #include <eurephia_context.h>
43 #include <eurephia_log.h>
44 #include <eurephiadb_mapping.h>
45 #include <eurephia_nullsafe.h>
46 #include <passwd.h>
47
48 /**
49 * Simple mapping table for table_id to string. Must match the order of the
50 * defined tables in eurephia_mapping.h
51 */
52 const char *TABLE_NAME[] = {
53 NULL,
54 "users", // TABLE_USERS
55 "certificates", // TABLE_CERTS
56 "usercerts", // TABLE_USERCERTS
57 "lastlog", // TABLE_LASTLOG
58 "attemptslog", // TABLE_ATTEMPTS
59 "blacklist", // TABLE_BLACKLIST
60 "eurephia_adminaccess", // TABLE_EUREPHIAADMACC
61 "firewall_profiles", // TABLE_FWPROFILES
62 NULL};
63
64 /**
65 * Simple mapping table for session status
66 */
67 const char *SESSION_STATUS[] = {
68 "UNKNOWN",
69 "STARTED",
70 "OPEN",
71 "CLOSING",
72 "CLOSED",
73 "CLEANEDUP",
74 NULL};
75
76
77 // Extra forward declarations to avoid compiler warnings. These functions are inline functions
78 // in eurephia_xml.h, which would then cause multiple declarations of these functions if that
79 // include file gets included here.
80 char *xmlGetAttrValue(xmlAttr *properties, const char *key);
81 char *xmlExtractContent(xmlNode *n);
82
83
84 /**
85 * Frees the memory used by a eDBfieldMap structure
86 *
87 * @param p Pointer to the eDBfieldMap to be freed
88 */
89 void eDBfreeMapping(eDBfieldMap *p) {
90 if( p == NULL ) {
91 return;
92 }
93 eDBfreeMapping(p->next);
94
95 // Release value and this pointer.
96 // Do not attempt to release field_name, as it is a constant char *
97 free_nullsafe(NULL, p->value);
98 free_nullsafe(NULL, p->table_alias);
99 free(p);
100 }
101
102
103 /**
104 * Internal function for mapping the unified eurephia mapping table from a table index.
105 * The table index is defined in eurephiadb_mapping.h, prefixed with TABLE_
106 *
107 * @param table table index (integer)
108 *
109 * @return Returns a pointer to the corresponding eDBfieldMap if table index is found, otherwise
110 * NULL is returned.
111 * @see eurephiadb_mapping.h
112 */
113 eDBfieldMap *eDBgetTableFieldMapping(int table) {
114 eDBfieldMap *srcmap, *newmap = NULL, *ptr = NULL;
115 int i;
116
117 switch( table ) {
118 case TABLE_USERS:
119 srcmap = eTblMap_user;
120 break;
121
122 case TABLE_CERTS:
123 srcmap = eTblMap_certificates;
124 break;
125
126 case TABLE_LASTLOG:
127 srcmap = eTblMap_lastlog;
128 break;
129
130 case TABLE_ATTEMPTS:
131 srcmap = eTblMap_attempts;
132 break;
133
134 case TABLE_BLACKLIST:
135 srcmap = eTblMap_blacklist;
136 break;
137
138 case TABLE_USERCERTS:
139 srcmap = eTblMap_usercerts;
140 break;
141
142 case TABLE_EUREPHIAADMACC:
143 srcmap = eTblMap_eurephiaadmacc;
144 break;
145
146 case TABLE_FWPROFILES:
147 srcmap = eTblMap_fwprofiles;
148 break;
149
150 default:
151 return NULL;
152 }
153
154 // Copy the mapping into a pointer chain
155 for( i = 0; srcmap[i].field_id != FIELD_NONE; i++ ) {
156 ptr = (eDBfieldMap *) malloc_nullsafe(NULL, sizeof(eDBfieldMap)+2);
157 assert(ptr != NULL);
158
159 ptr->tableid = srcmap[i].tableid;
160 ptr->table_alias = NULL;
161 ptr->field_id = srcmap[i].field_id;
162 ptr->field_type = srcmap[i].field_type;
163 ptr->filter_type = srcmap[i].filter_type;
164 ptr->field_name = srcmap[i].field_name;
165 ptr->value = NULL;
166 ptr->next = newmap;
167 newmap = ptr;
168 }
169
170 return newmap;
171 }
172
173
174 /**
175 * Internal function for copying over a particular field from one eDBfieldMap to another one
176 *
177 * @param newmap The destination map to be updated
178 * @param dbmap The source map where to copy the data from
179 * @param field The ID of the field which is being copied over
180 */
181 inline void eDBcopyMapAttribs(eDBfieldMap *newmap, eDBfieldMap *dbmap, int field) {
182 int i = 0;
183
184 for( i = 0; dbmap[i].field_name != NULL; i++ ) {
185 if( dbmap[i].field_id == field ) {
186 newmap->field_name = dbmap[i].field_name;
187 if( newmap->field_type != ft_SETNULL ) {
188 newmap->field_type = dbmap[i].field_type;
189 }
190 }
191 }
192 }
193
194
195 /**
196 * Create a eDBfieldMap to be used by the database driver, with the values from an fieldMapping XML node.
197 * This function will go through all fields in the XML tags, and associate the unified field names and
198 * table name with the database specific field/table names.
199 *
200 * @param ctx eurephiaCTX
201 * @param dbmap The database specific eDBfieldMap containing its own fields and table names
202 * @param tblalias If the SQL needs to use an alias, this alias prefix will be used for the fields
203 * @param fmapnode An xmlNode pointing at the fieldMapping node/tag.
204 *
205 * @return On success, a pointer to a eDBfieldMap specific for the database driver will be returned,
206 * otherwise NULL.
207 */
208 eDBfieldMap *eDBxmlMapping(eurephiaCTX *ctx, eDBfieldMap *dbmap, const char *tblalias, xmlNode *fmapnode) {
209 eDBfieldMap *map = NULL, *ptr = NULL;
210 char *fmap_table = NULL;
211 xmlNode *nptr = NULL;
212
213 // Check that the node we got is the right one
214 if( xmlStrcmp(fmapnode->name, (xmlChar *)"fieldMapping") != 0 ) {
215 eurephia_log(ctx, LOG_ERROR, 0, "Invalid fieldMapping XML node");
216 return NULL;
217 }
218
219 // Make sure the fmapnode is suitable for the given dbmap
220 assert( TABLE_NAME[dbmap->tableid] != NULL );
221 fmap_table = xmlGetAttrValue(fmapnode->properties, "table");
222 if( fmap_table == NULL ) {
223 eurephia_log(ctx, LOG_ERROR, 0, "Invalid fieldMapping XML document");
224 return NULL;
225 }
226 if( strcmp(fmap_table, TABLE_NAME[dbmap->tableid]) != 0 ) {
227 eurephia_log(ctx, LOG_ERROR, 0, "Mismatch between XML fieldMapping and eDBfieldMap");
228 return NULL;
229 }
230
231 // Get a copy of the system map
232 map = eDBgetTableFieldMapping(dbmap->tableid);
233
234 // Loop through the XML, and register the different values to the system map fields
235 nptr = fmapnode->children;
236 while( nptr != NULL ) {
237 // We are only interested in element nodes
238 if( (nptr->type != XML_ELEMENT_NODE) ) {
239 nptr = nptr->next;
240 continue;
241 }
242
243 if( nptr->name != NULL ) {
244 int setnull = 0;
245 xmlAttr *atr;
246 char *pwhash = NULL;
247 eDBfieldFilterType filter = flt_NOTSET;
248
249 // Check attributes given. Looking for: setnull, pwhash, filter
250 for( atr = nptr->properties; atr != NULL; atr = atr->next ) {
251 if( atr->name == NULL ) {
252 // Skip nodes without name (just in case)
253 continue;
254 }
255 if( xmlStrcmp(atr->name, (xmlChar *)"setnull") == 0 ) {
256 xmlNode *n = atr->children;
257 setnull = (((n != NULL) && (n->content != NULL)
258 && (xmlStrcmp(n->content,(xmlChar *)"1") == 0)) ? 1 : 0);
259 } else if( xmlStrcmp(atr->name, (xmlChar *)"pwhash") == 0 ) {
260 pwhash = (atr->children != NULL ? (char *) atr->children->content : NULL);
261 } else if( xmlStrcmp(atr->name, (xmlChar *)"filter") == 0 ) {
262 if( xmlStrcmp(atr->children->content,
263 (xmlChar *)"not-equals") == 0 ) {
264 filter = flt_NEQ;
265 } else if ( xmlStrcmp(atr->children->content,
266 (xmlChar *)"less-than") == 0 ) {
267 filter = flt_LT;
268 } else if ( xmlStrcmp(atr->children->content,
269 (xmlChar *)"less-than-equals") == 0 ) {
270 filter = flt_LTE;
271 } else if ( xmlStrcmp(atr->children->content,
272 (xmlChar *)"greater-than") == 0 ) {
273 filter = flt_GT;
274 } else if ( xmlStrcmp(atr->children->content,
275 (xmlChar *)"greater-than-equals") == 0 ) {
276 filter = flt_GTE;
277 } else {
278 filter = flt_EQ;
279 }
280 }
281 }
282
283 // Look up field in our copy of the system map
284 for( ptr = map; ptr != NULL; ptr = ptr->next ) {
285 // If we find the field, copy the value into our map
286 if( xmlStrcmp((xmlChar *)ptr->field_name, nptr->name) == 0 ) {
287 if( setnull ) {
288 // If flagged for being set to NULL, change the field type
289 ptr->field_type = ft_SETNULL;
290 }
291
292 switch( ptr->field_type ) {
293 case ft_SETNULL:
294 // Don't set any values if we want to set it to NULL
295 ptr->value = NULL;
296 break;
297
298 case ft_PASSWD: // If it is a password field type, hash the value
299 if( (pwhash != NULL) && (strcmp(pwhash, "sha512") == 0) ) {
300 ptr->value = (nptr->children != NULL
301 ? (char *)nptr->children->content
302 : strdup(""));
303 } else {
304 // Force hashing of value if it is an unknown hash type
305 ptr->value = (nptr->children != NULL
306 ? eurephia_pwd_crypt(ctx,
307 (char *)nptr->children->content,
308 NULL)
309 : strdup(""));
310 }
311 break;
312
313 default:
314 ptr->value = (nptr->children != NULL
315 ? strdup_nullsafe((char *)nptr->children->content)
316 : strdup(""));
317 break;
318 }
319 if( filter != flt_NOTSET ) {
320 ptr->filter_type = filter;
321 }
322 break;
323 }
324 }
325
326 // Go to next XML node
327 nptr = nptr->next;
328 } else {
329 eurephia_log(ctx, LOG_ERROR, 0, "*** Illegal XML - unaccepted node: (%i) %s\n",
330 nptr->type, nptr->name);
331 return NULL;
332 }
333 }
334
335 for( ptr = map; ptr != NULL; ptr = ptr->next ) {
336 // Copy over field name - translated via the db mapping table
337 eDBcopyMapAttribs(ptr, dbmap, ptr->field_id);
338 // Set correct table alias in the new map
339 ptr->table_alias = strdup_nullsafe(tblalias);
340 }
341 return map;
342 }
343
344
345 /**
346 * Generates a database specific list which is comma separated for the database driver to
347 * use in the ORDER BY section of SQL queries. The unified database fields are being translated
348 * into the database specific field names.
349 *
350 * @param tfmap eDBfieldMap specific to the database driver.
351 * @param skeys_str Comma separated string containing the field names for the ORDER BY clause
352 *
353 * @return Returns a comma separated string with translated field names on success, otherwise NULL.
354 * @remark The resulting pointer is pointing at a static char pointer and should not be freed.
355 */
356 const char *eDBmkSortKeyString(eDBfieldMap *tfmap, const char *skeys_str) {
357 eDBfieldMap *sk_map = NULL, *ptr1 = NULL, *tfmp = NULL;
358 char *cp = NULL, *tok = NULL, *delims = ",";
359 static char sortkeys[8194];
360
361 if( skeys_str == NULL ) {
362 return NULL;
363 }
364
365 // Make sure we have table field map
366 assert( tfmap != NULL );
367
368 // Get the correct table mapping for user input
369 sk_map = eDBgetTableFieldMapping(tfmap->tableid);
370 assert( sk_map != NULL );
371
372 // Split up the skeys_str (sort keys string) and build up a map
373 cp = strdup_nullsafe(skeys_str);
374 tok = strtok(cp, delims);
375 memset(&sortkeys, 0, 8194);
376 while( tok != NULL ) {
377 for( ptr1 = sk_map; ptr1 != NULL; ptr1 = ptr1->next) {
378 // If we find the the field in the unified mapping table ...
379 if( strcmp(tok, ptr1->field_name) == 0 ) {
380 // look up the proper field name for the current database
381 for( tfmp = tfmap; tfmp != NULL; tfmp = tfmp->next ) {
382 if( ptr1->field_id == tfmp->field_id ) {
383 if( tfmp->table_alias != NULL ) {
384 append_str(sortkeys, tfmp->table_alias, 8192);
385 append_str(sortkeys, ".", 8192);
386 }
387 append_str(sortkeys, tfmp->field_name, 8192);
388 append_str(sortkeys, ",", 8192);
389 }
390 }
391 }
392 }
393 tok = strtok(NULL, delims);
394 }
395 free_nullsafe(NULL, cp);
396 sortkeys[strlen(sortkeys)-1] = '\0';
397 eDBfreeMapping(sk_map);
398
399 return (strlen_nullsafe(sortkeys) > 0 ? sortkeys : NULL);
400 }
401
402
403 /**
404 * Tells which database fields in a eDBfieldMap are set and available.
405 *
406 * @param map eDBfieldMap containing the database specific mapping and field values set
407 *
408 * @return Returns a bit-wise OR'ed value of all fields in an eDBfieldMap which have their values
409 * set to something else than NULL.
410 */
411 long int eDBmappingFieldsPresent(eDBfieldMap *map) {
412 long int ret = 0;
413 eDBfieldMap *p = NULL;
414
415 // Loops through all elements and flags those
416 // fields with a value set.
417 for( p = map; p != NULL; p = p->next ) {
418 if( p->value != NULL ) {
419 ret |= p->field_id;
420 }
421 }
422 return ret;
423 }
424
425
426 /**
427 * Retrieves the value of a field in a eDBfieldMap pointer chain.
428 *
429 * @param map eDBfieldMap with the values
430 * @param field_id The field ID to retrieve the value from
431 *
432 * @return Returns const char * to the value in the eDBfieldMap on success, or NULL if either the
433 * value is not found or if the value is not set.
434 */
435 const char *eDBmappingGetValue(eDBfieldMap *map, long field_id) {
436 eDBfieldMap *ptr = NULL;
437
438 if( map == NULL ) {
439 return NULL;
440 }
441
442 for( ptr = map; ptr != NULL; ptr = ptr->next ) {
443 if( ptr->field_id == field_id ) {
444 return ptr->value;
445 }
446 }
447 return NULL;
448 }
449