Debian Codegraph @

database/eurephiadb_mapping.c

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