Debian Codegraph @

database/sqlite/administration/useraccount.c

raw
1 /* administration.c -- Functions for user account management
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 useraccount.c
24 * @author David Sommerseth <[email protected]>
25 * @date 2009-09-13
26 *
27 * @brief Functions for user account management
28 *
29 */
30
31 #include <string.h>
32 #include <unistd.h>
33 #include <assert.h>
34
35 #include <libxml/tree.h>
36
37 #include <sqlite3.h>
38
39 #include <eurephia_nullsafe.h>
40 #include <eurephia_context.h>
41 #include <eurephia_log.h>
42 #include <eurephia_xml.h>
43 #include <eurephia_values.h>
44 #include <eurephiadb_session_struct.h>
45 #include <eurephiadb_mapping.h>
46 #include <passwd.h>
47
48 #ifndef DRIVER_MODE
49 #define DRIVER_MODE
50 #endif
51 #include <eurephiadb_driver.h>
52
53 #include "../sqlite.h"
54
55 #define FMAP_USERS /**< fieldmapping.h: Include declaration of tbl_sqlite_users */
56 #define FMAP_ADMINACCESS /**< fieldmapping.h: Include declaration of tbl_sqlite_eurephiaadmacc */
57 #include "../fieldmapping.h"
58
59
60 /**
61 * Internal function. Adds a child node named \<flag\> to an xmlNode containing a flag value
62 *
63 * @param node xmlNode pointer where to add the new flag
64 * @param flagname String containing a name of the flag
65 * @param flagged Is the flag set or not. The tag will only be added if the flag is set
66 *
67 * @return Returns the \c flagged value
68 */
69 static inline int xml_set_flag(xmlNode *node, char *flagname, int flagged) {
70 if( flagged ) {
71 xmlNewChild(node, NULL, (xmlChar *) "flag", (xmlChar *) flagname);
72 }
73 return flagged;
74 }
75
76
77 /**
78 * Internal function. Retrieves information about user accounts
79 *
80 * @param ctx eurephiaCTX
81 * @param infoType Flags defining which information to be included in the result
82 * @param uinfo_map eDBfieldMap containing information needed for the new user account
83 * @param sortkeys String containing list of fields defining data sorting
84 *
85 * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors,
86 * NULL is returned.
87 */
88 static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType,
89 eDBfieldMap *uinfo_map, const char *sortkeys)
90 {
91 dbresult *uinf = NULL, *qres = NULL;
92 unsigned int flag = 0, uid = 0, recid = 0;
93 char *username = NULL;
94 xmlDoc *doc = NULL;
95 xmlNode *root_n = NULL, *info_n = NULL;
96
97 DEBUG(ctx, 20, "Function call: eDBadminGetUserUserInfo(ctx, %i, {xmlDoc})", infoType);
98 assert( ctx != NULL );
99
100 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
101 eurephia_log(ctx, LOG_CRITICAL, 0,
102 "eurephia admin function call attempted with wrong context type");
103 return NULL;
104 }
105
106 // Query the database, find the user defined in the user map
107 uinf = sqlite_query_mapped(ctx, SQL_SELECT,
108 "SELECT users.username, locdt(users.activated), locdt(users.deactivated),"
109 " locdt(users.last_accessed), users.uid,"
110 " (bl.username IS NOT NULL), opensess, logincount,"
111 " (at.attempts > 0)"
112 " FROM openvpn_users users"
113 " LEFT JOIN openvpn_blacklist bl USING(username)"
114 " LEFT JOIN openvpn_attempts at ON(at.username = users.username)"
115 " LEFT JOIN (SELECT uid, count(*) AS logincount "
116 " FROM openvpn_lastlog"
117 " GROUP BY uid) lc"
118 " ON (lc.uid = users.uid)"
119 " LEFT JOIN (SELECT uid, count(*) > 0 AS opensess"
120 " FROM openvpn_lastlog"
121 " WHERE sessionstatus = 2"
122 " GROUP BY uid) os"
123 " ON (os.uid = users.uid)",
124 NULL, uinfo_map, sortkeys);
125
126 if( sqlite_query_status(uinf) != dbSUCCESS ) {
127 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a user");
128 info_n = sqlite_log_error_xml(ctx, uinf);
129 doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n, "Failed to query the user database");
130 xmlFreeNode(info_n);
131 goto exit;
132 }
133
134 eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &doc, &root_n);
135 xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "view");
136
137 for( recid = 0; recid < sqlite_get_numtuples(uinf); recid++ ) {
138 xmlNode *user_n = xmlNewChild(root_n, NULL, (xmlChar *) "Account", NULL);
139 assert( user_n != NULL );
140
141 sqlite_xml_value(user_n, XML_ATTR, "uid", uinf, recid, 4);
142 sqlite_xml_value(user_n, XML_NODE, "username", uinf, recid, 0);
143
144 uid = atoi_nullsafe(sqlite_get_value(uinf, recid, 4));
145 username = sqlite_get_value(uinf, recid, 0);
146
147 if( infoType & USERINFO_user ) {
148 info_n = xmlNewChild(user_n, NULL, (xmlChar *) "flags", NULL);
149 assert( info_n != NULL );
150
151 // set DEACTIVATED flag, if deactivated field is not NULL
152 xml_set_flag(info_n, "DEACTIVATED", (sqlite_get_value(uinf, recid, 2) != NULL));
153
154 // set BLACKLISTED flag, if username is found in blacklist table
155 xml_set_flag(info_n, "BLACKLISTED", (atoi_nullsafe(sqlite_get_value(uinf, recid, 5))==1));
156
157 // set OPENSESSION flag, if user has a lastlog entry with sessionstatus == 2
158 xml_set_flag(info_n, "OPENSESSION", (atoi_nullsafe(sqlite_get_value(uinf, recid, 6))==1));
159
160 // set ERRATTEMPT flag, if user has an entry in attempts log with attemtps > 0
161 xml_set_flag(info_n, "ERRATTEMPT", (atoi_nullsafe(sqlite_get_value(uinf, recid, 8))==1));
162
163 // set NEVERUSED flag, if login count == 0 and last_accessed == NULL
164 flag = xml_set_flag(info_n, "NEVERUSED", ((atoi_nullsafe(sqlite_get_value(uinf,0, 7))==0)
165 && (sqlite_get_value(uinf, recid, 3) == NULL)));
166
167 // set RSETLASTUSED flag, if login count == 0 and last_accessed == NULL
168 xml_set_flag(info_n, "RSETLASTUSED", !flag && (sqlite_get_value(uinf,0,3)) == NULL);
169
170 // set RSETLOGINCNT flag, if login count == 0 and last_accessed != NULL
171 xml_set_flag(info_n, "RSETLOGINCNT", ((atoi_nullsafe(sqlite_get_value(uinf,0, 7))==0)
172 && (sqlite_get_value(uinf,0,3)) != NULL));
173
174 sqlite_xml_value(user_n, XML_NODE, "activated", uinf, recid, 1);
175 sqlite_xml_value(user_n, XML_NODE, "deactivated", uinf, recid, 2);
176 info_n = sqlite_xml_value(user_n, XML_NODE, "last_accessed", uinf, recid, 3);
177 sqlite_xml_value(user_n, XML_ATTR, "logincount", uinf, recid, 7);
178 }
179
180 if( infoType & USERINFO_certs ) {
181 // Extract certificate info
182 qres = sqlite_query(ctx,
183 "SELECT depth, lower(digest), common_name, organisation, email, "
184 " locdt(c.registered), c.certid,"
185 " uc.accessprofile, access_descr,"
186 " fw_profile"
187 " FROM openvpn_certificates c"
188 " LEFT JOIN openvpn_usercerts uc ON (c.certid = uc.certid)"
189 " LEFT JOIN openvpn_accesses a "
190 " ON (uc.accessprofile = a.accessprofile)"
191 " WHERE uid = '%i' ORDER BY c.certid DESC", uid);
192
193 info_n = xmlNewChild(user_n, NULL, (xmlChar *) "certificates", NULL);
194 assert( info_n != NULL );
195 if( (sqlite_query_status(qres) == dbSUCCESS) && (sqlite_get_numtuples(qres) > 0) ) {
196 int i;
197 xmlNode *cert, *acpr;
198 xmlChar *tmp = NULL;
199
200 for( i = 0; i < sqlite_get_numtuples(qres); i++ ) {
201 cert = xmlNewChild(info_n, NULL, (xmlChar *) "certificate", NULL);
202 assert( cert != NULL );
203
204 sqlite_xml_value(cert, XML_ATTR, "certid", qres, i, 6);
205 sqlite_xml_value(cert, XML_ATTR, "depth", qres, i, 0);
206 sqlite_xml_value(cert, XML_ATTR, "registered", qres, i, 5);
207 sqlite_xml_value(cert, XML_NODE, "digest", qres, i, 1);
208
209 tmp = (xmlChar *)sqlite_get_value(qres, i, 2);
210 xmlReplaceChars(tmp, '_', ' ');
211 xmlNewChild(cert, NULL, (xmlChar *) "common_name", tmp);
212
213 tmp = (xmlChar *)sqlite_get_value(qres, i, 3);
214 xmlReplaceChars(tmp, '_', ' ');
215 xmlNewChild(cert, NULL, (xmlChar *) "organisation", tmp);
216
217 sqlite_xml_value(cert, XML_NODE, "email", qres, i, 4);
218
219 acpr = sqlite_xml_value(cert, XML_NODE, "access_profile", qres, i, 8);
220 sqlite_xml_value(acpr, XML_ATTR, "accessprofile", qres, i, 7);
221 sqlite_xml_value(acpr, XML_ATTR, "fwdestination", qres, i, 9);
222 }
223 } else if( sqlite_query_status(qres) == dbERROR ) {
224 sqlite_log_error(ctx, qres);
225 }
226 sqlite_free_results(qres);
227 }
228
229 if( infoType & USERINFO_lastlog ) {
230 int i = 0;
231 xmlNode *lastl = NULL, *sess = NULL, *tmp1 = NULL, *tmp2 = NULL;
232 xmlChar *tmp = NULL;
233
234 qres = sqlite_query(ctx,
235 "SELECT llid, ll.certid,protocol,remotehost,remoteport,macaddr,"
236 " vpnipaddr, vpnipmask, sessionstatus, sessionkey,"
237 " locdt(login), locdt(logout),"
238 " session_duration, locdt(session_deleted),"
239 " bytes_sent, bytes_received, uicid, accessprofile,"
240 " access_descr, fw_profile, depth, lower(digest),"
241 " common_name, organisation, email"
242 " FROM openvpn_lastlog ll"
243 " LEFT JOIN openvpn_usercerts USING (uid, certid)"
244 " LEFT JOIN openvpn_accesses USING (accessprofile)"
245 " LEFT JOIN openvpn_certificates cert ON(ll.certid=cert.certid)"
246 " WHERE uid = '%i' ORDER BY login, logout", uid);
247
248 if( sqlite_query_status(qres) != dbSUCCESS ) {
249 eurephia_log(ctx, LOG_ERROR, 0, "Querying the lastlog failed");
250 xmlFreeDoc(doc);
251 info_n = sqlite_log_error_xml(ctx, qres);
252 doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n,
253 "Failed to query the lastlog");
254 sqlite_free_results(qres);
255 xmlFreeNode(info_n);
256 return doc;
257 }
258
259 lastl = xmlNewChild(user_n, NULL, (xmlChar *) "lastlog", NULL);
260 for( i = 0; i < sqlite_get_numtuples(qres); i++ ) {
261
262 sess = xmlNewChild(lastl, NULL, (xmlChar*) "session", NULL);
263 assert( sess != NULL );
264
265 sqlite_xml_value(sess, XML_ATTR, "llid", qres, i, 0);
266 xmlNewProp(sess, (xmlChar *) "session_status",
267 (xmlChar *)SESSION_STATUS[atoi_nullsafe(sqlite_get_value(qres, i, 8))]);
268 sqlite_xml_value(sess, XML_ATTR, "session_duration", qres, i, 12);
269 sqlite_xml_value(sess, XML_NODE, "sessionkey", qres, i, 9);
270 sqlite_xml_value(sess, XML_NODE, "login", qres, i, 10);
271 sqlite_xml_value(sess, XML_NODE, "logout", qres, i, 11);
272 sqlite_xml_value(sess, XML_NODE, "session_closed", qres, i, 13);
273
274 tmp1 = xmlNewChild(sess, NULL, (xmlChar *) "connection", NULL);
275 assert( tmp1 != NULL );
276 sqlite_xml_value(tmp1, XML_ATTR, "bytes_sent", qres, i, 14);
277 sqlite_xml_value(tmp1, XML_ATTR, "bytes_received", qres, i, 15);
278 sqlite_xml_value(tmp1, XML_NODE, "protocol", qres, i, 2);
279 sqlite_xml_value(tmp1, XML_NODE, "remote_host", qres, i, 3);
280 sqlite_xml_value(tmp1, XML_NODE, "remote_port", qres, i, 4);
281 sqlite_xml_value(tmp1, XML_NODE, "vpn_macaddr", qres, i, 5);
282 sqlite_xml_value(tmp1, XML_NODE, "vpn_ipaddr" , qres, i, 6);
283 sqlite_xml_value(tmp1, XML_NODE, "vpn_netmask", qres, i, 7);
284
285 tmp1 = xmlNewChild(sess, NULL, (xmlChar *) "certificate", NULL);
286 assert( tmp1 != NULL );
287 sqlite_xml_value(tmp1, XML_ATTR, "certid", qres, i, 1);
288 sqlite_xml_value(tmp1, XML_ATTR, "uicid", qres, i, 16);
289 sqlite_xml_value(tmp1, XML_ATTR, "depth", qres, i, 20);
290 sqlite_xml_value(tmp1, XML_NODE, "digest", qres, i, 21);
291
292 tmp = (xmlChar *)sqlite_get_value(qres, 0, 22);
293 xmlReplaceChars(tmp, '_', ' ');
294 xmlNewChild(tmp1, NULL, (xmlChar *) "common_name", tmp);
295
296 tmp = (xmlChar *)sqlite_get_value(qres, 0, 23);
297 xmlReplaceChars(tmp, '_', ' ');
298 xmlNewChild(tmp1, NULL, (xmlChar *) "organisation", tmp);
299
300 sqlite_xml_value(tmp1, XML_NODE, "email", qres, i, 24);
301
302 tmp2 = sqlite_xml_value(tmp1, XML_NODE, "access_profile", qres, i, 18);
303 sqlite_xml_value(tmp2, XML_ATTR, "accessprofile", qres, i, 17);
304 sqlite_xml_value(tmp2, XML_ATTR, "fwdestination", qres, i, 19);
305 }
306 sqlite_free_results(qres);
307 }
308
309 if( infoType & USERINFO_attempts ) {
310 xmlNode *atmpt = NULL;
311
312 qres = sqlite_query(ctx,
313 "SELECT attempts, locdt(registered), locdt(last_attempt), atpid"
314 " FROM openvpn_attempts "
315 " WHERE username = '%q'", username);
316
317 if( (sqlite_query_status(qres) != dbSUCCESS) || (sqlite_get_numtuples(qres) > 1) ) {
318 eurephia_log(ctx, LOG_ERROR, 0, "Querying for login attempts failed");
319 info_n = NULL;
320 if( sqlite_query_status(qres) == dbERROR ) {
321 info_n = sqlite_log_error_xml(ctx, qres);
322 }
323 xmlFreeDoc(doc);
324 doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n,
325 "Failed to query the login attempts log");
326 sqlite_free_results(qres);
327 xmlFreeNode(info_n);
328 return doc;
329 }
330
331 atmpt = xmlNewChild(user_n, NULL, (xmlChar *) "attempts", NULL);
332 assert( atmpt != NULL );
333
334 if( sqlite_get_numtuples(qres) == 1 ) {
335 sqlite_xml_value(atmpt, XML_ATTR, "atpid", qres, 0, 3);
336 sqlite_xml_value(atmpt, XML_ATTR, "attempts", qres, 0, 0);
337 sqlite_xml_value(atmpt, XML_NODE, "first_attempt", qres, 0, 1);
338 sqlite_xml_value(atmpt, XML_NODE, "last_attempt", qres, 0, 2);
339 }
340 sqlite_free_results(qres);
341 }
342
343 if( infoType & USERINFO_blacklist ) {
344 xmlNode *atmpt = NULL;
345
346 qres = sqlite_query(ctx,
347 "SELECT locdt(registered), locdt(last_accessed), blid"
348 " FROM openvpn_blacklist "
349 " WHERE username = '%q'", username);
350
351 if( (sqlite_query_status(qres) != dbSUCCESS) || (sqlite_get_numtuples(qres) > 1) ) {
352 xmlFreeDoc(doc);
353 eurephia_log(ctx, LOG_ERROR, 0, "Querying blacklist log failed");
354 info_n = NULL;
355 if( sqlite_query_status(qres) == dbERROR ) {
356 info_n = sqlite_log_error_xml(ctx, qres);
357 }
358 doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n,
359 "Failed to query the blacklist log");
360 sqlite_free_results(qres);
361 xmlFreeNode(info_n);
362 return doc;
363 }
364
365 atmpt = xmlNewChild(user_n, NULL, (xmlChar *) "blacklist", NULL);
366 assert( atmpt != NULL );
367
368 if( sqlite_get_numtuples(qres) == 1 ) {
369 sqlite_xml_value(atmpt, XML_ATTR, "blid", qres, 0, 2);
370 sqlite_xml_value(atmpt, XML_NODE, "blacklisted", qres, 0, 0);
371 sqlite_xml_value(atmpt, XML_NODE, "last_accessed", qres, 0, 1);
372 }
373 sqlite_free_results(qres);
374 }
375
376 }
377 exit:
378 sqlite_free_results(uinf);
379 return doc;
380 }
381
382
383 /**
384 * Internal function. Creates a new user account in the database.
385 *
386 * @param ctx eurephiaCTX
387 * @param usrinf_map eDBfieldMap containing information needed for the new user account
388 *
389 * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors,
390 * NULL is returned.
391 */
392 static xmlDoc *useracc_add(eurephiaCTX *ctx, eDBfieldMap *usrinf_map) {
393 xmlDoc *res_d = NULL;
394 dbresult *res = NULL;
395
396 DEBUG(ctx, 21, "Function call: useracc_add(ctx, eDBfieldMap)");
397 assert( (ctx != NULL) && (usrinf_map != NULL) );
398
399 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
400 eurephia_log(ctx, LOG_CRITICAL, 0,
401 "eurephia admin function call attempted with wrong context type");
402 return NULL;
403 }
404
405 // Register the user
406 res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_users", usrinf_map, NULL, NULL);
407 if( (sqlite_query_status(res) != dbSUCCESS) || (sqlite_get_affected_rows(res) == 0) ) {
408 xmlNode *err_n = NULL;
409
410 eurephia_log(ctx, LOG_FATAL, 0, "Could not register the new user account");
411 if( sqlite_query_status(res) == dbERROR ) {
412 err_n = sqlite_log_error_xml(ctx, res);
413 }
414 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n,
415 "Failed to register the user account");
416 xmlFreeNode(err_n);
417 } else {
418 xmlChar *uid = malloc_nullsafe(ctx, 34);
419 xmlNode *info_n = NULL;
420 assert( uid != NULL );
421
422 // Prepare an information tag/node with the new uid value
423 xmlStrPrintf(uid, 32, "%lld", res->last_insert_id);
424 info_n = xmlNewNode(NULL, (xmlChar *)"UserAccount");
425 xmlNewProp(info_n, (xmlChar *) "mode", (xmlChar *) "add");
426 xmlNewProp(info_n, (xmlChar *) "uid", uid);
427
428 eurephia_log(ctx, LOG_INFO, 1, "New user account created (uid %i)", res->last_insert_id);
429 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, info_n,
430 "New user account created with uid %i", res->last_insert_id);
431 free_nullsafe(ctx, uid);
432 xmlFreeNode(info_n);
433 }
434 sqlite_free_results(res);
435
436 return res_d;
437 }
438
439
440 /**
441 * Internal function. Updates a user account
442 *
443 * @param ctx eurephiaCTX
444 * @param uid Numeric user id to be updated
445 * @param value_map eDBfieldMap containing new values
446 *
447 * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors,
448 * NULL is returned.
449 */
450 static xmlDoc *useracc_update(eurephiaCTX *ctx, const int uid, eDBfieldMap *value_map) {
451 dbresult *uinf = NULL;
452 xmlDoc *res_d = NULL, *srch_xml = NULL;
453 xmlNode *srch_n = NULL;
454 xmlChar *xmluid = NULL;
455 eDBfieldMap *srch_map = NULL;
456
457 DEBUG(ctx, 21, "Function call: useracc_update(ctx, %i, eDBfieldMap)", uid);
458 assert( (ctx != NULL) && (value_map != NULL) );
459
460 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
461 eurephia_log(ctx, LOG_CRITICAL, 0,
462 "eurephia admin function call attempted with wrong context type");
463 return NULL;
464 }
465
466 // Create an eDBfieldMap structure for the srch_map (to be used in the WHERE clause)
467 xmluid = (xmlChar *) malloc_nullsafe(ctx, 34);
468 xmlStrPrintf(xmluid, 32, "%u", uid);
469 eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch_xml, &srch_n);
470 xmlNewProp(srch_n, (xmlChar *) "table", (xmlChar *) "users");
471 xmlNewChild(srch_n, NULL, (xmlChar *) "uid", xmluid); // Add uid as the only criteria
472 srch_map = eDBxmlMapping(ctx, tbl_sqlite_users, NULL, srch_n);
473 assert( srch_map != NULL );
474
475 // UPDATE the database
476 uinf = sqlite_query_mapped(ctx, SQL_UPDATE, "UPDATE openvpn_users", value_map, srch_map, NULL);
477
478 if( sqlite_query_status(uinf) != dbSUCCESS ) {
479 xmlNode *err_n = NULL;
480
481 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a user");
482 err_n = sqlite_log_error_xml(ctx, uinf);
483 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to update user (uid %i)", uid);
484 xmlFreeNode(err_n);
485 } else if( sqlite_get_affected_rows(uinf) == 0 ) {
486 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
487 "Could not find any user account with uid %i", uid);
488 } else {
489 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
490 "User account with uid %i is updated", uid);
491 }
492 sqlite_free_results(uinf);
493 eDBfreeMapping(srch_map);
494 xmlFreeDoc(srch_xml);
495 free_nullsafe(ctx, xmluid);
496
497 return res_d;
498 }
499
500
501 /**
502 * Internal function. Deletes a user account from the users table
503 *
504 * @param ctx eurephiaCTX
505 * @param uid Numeric user id of user to be deleted.
506 *
507 * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors,
508 * NULL is returned.
509 */
510 static xmlDoc *useracc_delete(eurephiaCTX *ctx, const unsigned int uid) {
511 xmlDoc *res_d = NULL;
512 dbresult *res = NULL;
513
514 DEBUG(ctx, 21, "Function call: useracc_delete(ctx, %i)", uid);
515 assert( ctx != NULL );
516
517 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
518 eurephia_log(ctx, LOG_CRITICAL, 0,
519 "eurephia admin function call attempted with wrong context type");
520 return NULL;
521 }
522
523 // Delete the user
524 res = sqlite_query(ctx, "DELETE FROM openvpn_users WHERE uid = '%i'", uid);
525 if( sqlite_query_status(res) != dbSUCCESS ) {
526 xmlNode *err_n = NULL;
527
528 eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the user account (uid %i)", uid);
529 err_n = sqlite_log_error_xml(ctx, res);
530 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n,
531 "Failed to delete the user account (uid %i)", uid);
532 xmlFreeNode(err_n);
533 } else if( sqlite_get_affected_rows(res) == 0 ) {
534 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
535 "Could not find any user account with uid %i", uid);
536 } else {
537 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
538 "User account with uid %i is deleted", uid);
539 }
540 sqlite_free_results(res);
541 return res_d;
542 }
543
544
545 /**
546 * @copydoc eDBadminUserAccount()
547 */
548 xmlDoc *eDBadminUserAccount(eurephiaCTX *ctx, xmlDoc *qryxml) {
549 xmlDoc *res_d = NULL;
550 xmlNode *qry_n = NULL, *fmap_n = NULL;
551 eDBfieldMap *fmap_m = NULL;
552 char *mode = NULL;
553 int uid;
554
555 DEBUG(ctx, 20, "Function call: eDBadminUserAccount(ctx, xmlDoc)");
556 assert( (ctx != NULL) && (qryxml != NULL) );
557
558 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
559 eurephia_log(ctx, LOG_CRITICAL, 0,
560 "eurephia admin function call attempted with wrong context type");
561 return NULL;
562 }
563
564 qry_n = eurephiaXML_getRoot(ctx, qryxml, "UserAccount", 1);
565 if( qry_n == NULL ) {
566 eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user account request");
567 return NULL;
568 }
569 mode = xmlGetAttrValue(qry_n->properties, "mode");
570 if( mode == NULL ) {
571 eurephia_log(ctx, LOG_ERROR, 0, "Invalid user account request (1).");
572 return NULL;
573 }
574
575 fmap_n = xmlFindNode(qry_n, "fieldMapping");
576 if( fmap_n == NULL ) {
577 eurephia_log(ctx, LOG_ERROR, 0, "Invalid user account request (2).");
578 return NULL;
579 }
580 fmap_m = eDBxmlMapping(ctx, tbl_sqlite_users, "users", fmap_n);
581 assert(fmap_m != NULL);
582
583 // Extract the value of the uid attribute in the UserAccount tag. If not found, set value to -1.
584 uid = atoi_nullsafe(defaultValue(xmlGetAttrValue(qry_n->properties, "uid"), "-1"));
585
586 if( strcmp(mode, "view") == 0 ) {
587 unsigned int flags = atoi_nullsafe(defaultValue(xmlGetNodeContent(qry_n,"extractFlags"),"0"));
588 const char *sortkeys = xmlGetNodeContent(qry_n, "sortkeys");
589 res_d = useracc_view(ctx, flags, fmap_m, eDBmkSortKeyString(fmap_m, sortkeys));
590 } else if( strcmp(mode, "add") == 0 ) {
591 res_d = useracc_add(ctx, fmap_m);
592 } else if( strcmp(mode, "update") == 0 ) {
593 if( uid == -1 ) {
594 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
595 "Can not update user account without an uid value");
596 } else {
597 res_d = useracc_update(ctx, uid, fmap_m);
598 }
599 } else if( strcmp(mode, "delete") == 0 ) {
600 if( uid == -1 ) {
601 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
602 "Can not delete user account without an uid value");
603 } else {
604 res_d = useracc_delete(ctx, uid);
605 }
606 } else {
607 eurephia_log(ctx, LOG_ERROR, 0, "UserAccount - Unknown mode: '%s'", mode);
608 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
609 }
610 eDBfreeMapping(fmap_m);
611
612 return res_d;
613 }
614
615
616 /**
617 * Internal function. Retrieves all administrator access levels granted.
618 *
619 * @param ctx eurephiaCTX
620 * @param fmap eDBfieldMap with field values to narrow the SQL query
621 *
622 * @return Returns an eurephia XML document on success or with a failure message. NULL is returned
623 * on fatal errors.
624 */
625 xmlDoc *adminacclvl_Get(eurephiaCTX *ctx, eDBfieldMap *fmap) {
626 dbresult *res = NULL;
627 int last_uid = -1, i = 0;
628
629 xmlDoc *doc = NULL;
630 xmlNode *root_n = NULL, *rec_n = NULL, *acl_n = NULL, *tmp_n;
631
632 DEBUG(ctx, 21, "Function call: adminacclvl_Get(ctx, {fieldMapping})");
633 assert( (ctx != NULL) && (fmap != NULL) );
634
635 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
636 eurephia_log(ctx, LOG_CRITICAL, 0,
637 "eurephia admin function call attempted with wrong context type");
638 return 0;
639 }
640
641 // Query the database, find the user defined in the user map
642 res = sqlite_query_mapped(ctx, SQL_SELECT,
643 "SELECT eac.uid, username, interface, access"
644 " FROM eurephia_adminaccess eac"
645 " LEFT JOIN openvpn_users USING(uid)",
646 NULL, fmap, "uid, interface, access");
647 if( sqlite_query_status(res) != dbSUCCESS ) {
648 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a access levels");
649 tmp_n = sqlite_log_error_xml(ctx, res);
650 doc = eurephiaXML_ResultMsg(ctx, exmlERROR, tmp_n,
651 "Error querying the database for a access levels");
652 xmlFreeNode(tmp_n);
653 goto exit;
654 }
655
656 eurephiaXML_CreateDoc(ctx, 1, "admin_access_list", &doc, &root_n);
657 for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
658 if( last_uid != atoi_nullsafe(sqlite_get_value(res, i, 0)) ) {
659 // Create a new block element when we get a new uid
660 rec_n = xmlNewChild(root_n, NULL, (xmlChar *) "user_access", NULL);
661 last_uid = atoi_nullsafe(sqlite_get_value(res, i, 0));
662
663 tmp_n = sqlite_xml_value(rec_n, XML_NODE, "username", res, i, 1);
664 sqlite_xml_value(tmp_n, XML_ATTR, "uid", res, i, 0);
665
666 acl_n = xmlNewChild(rec_n, NULL, (xmlChar *) "access_levels", NULL);
667 }
668
669 tmp_n = sqlite_xml_value(acl_n, XML_NODE, "access", res, i, 3);
670 sqlite_xml_value(tmp_n, XML_ATTR, "interface", res, i, 2);
671 }
672 exit:
673 sqlite_free_results(res);
674 return doc;
675 }
676
677
678 /**
679 * @copydoc eDBadminAccessLevel()
680 */
681 xmlDoc *eDBadminAccessLevel(eurephiaCTX *ctx, xmlDoc *qryxml) {
682 dbresult *sqlres = NULL;
683 xmlDoc *res_d = NULL;
684 xmlNode *qry_n = NULL, *fmap_n = NULL, *err_n = NULL;
685 eDBfieldMap *fmap_m = NULL;
686 char *mode = NULL;
687
688 DEBUG(ctx, 20, "Function call: eDBadminAccessLevel(ctx, xmlDoc)");
689 assert( (ctx != NULL) && (qryxml != NULL) );
690
691 if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
692 eurephia_log(ctx, LOG_CRITICAL, 0,
693 "eurephia admin function call attempted with wrong context type");
694 return 0;
695 }
696
697 qry_n = eurephiaXML_getRoot(ctx, qryxml, "admin_access", 1);
698 if( qry_n == NULL ) {
699 eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user-certs link request");
700 return 0;
701 }
702 mode = xmlGetAttrValue(qry_n->properties, "mode");
703 if( mode == NULL ) {
704 eurephia_log(ctx, LOG_ERROR, 0, "Invalid edit admin access request (1).");
705 return 0;
706 }
707
708 fmap_n = xmlFindNode(qry_n, "fieldMapping");
709 if( fmap_n == NULL ) {
710 eurephia_log(ctx, LOG_ERROR, 0, "Invalid edit admin access request (2).");
711 return 0;
712 }
713
714 fmap_m = eDBxmlMapping(ctx, tbl_sqlite_eurephiaadmacc, NULL, fmap_n);
715 assert(fmap_m != NULL);
716
717 if( strcmp(mode, "grant") == 0 ) {
718 sqlres = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO eurephia_adminaccess",
719 fmap_m, NULL, NULL);
720 if( (sqlite_query_status(sqlres) == dbSUCCESS) && (sqlite_get_affected_rows(sqlres) > 0) ) {
721 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
722 "Access level %s (%s) was granted to uid %s",
723 eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL),
724 eDBmappingGetValue(fmap_m, FIELD_INTERFACE),
725 eDBmappingGetValue(fmap_m, FIELD_UID));
726 } else if( sqlite_query_status(sqlres) == dbERROR ) {
727 err_n = sqlite_log_error_xml(ctx, sqlres);
728 }
729 } else if( strcmp(mode, "revoke") == 0 ) {
730 sqlres = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM eurephia_adminaccess",
731 NULL, fmap_m, NULL);
732 if( (sqlite_query_status(sqlres) == dbSUCCESS) && (sqlite_get_affected_rows(sqlres) > 0) ) {
733 const char *uid = eDBmappingGetValue(fmap_m, FIELD_UID);
734 const char *acclvl = eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL);
735
736 if( acclvl != NULL ) {
737 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
738 "Access level %s (%s) was revoked from uid %s",
739 acclvl, eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL),
740 uid);
741 } else {
742 res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
743 "%i access levels was removed from uid %s",
744 sqlite_get_affected_rows(sqlres), uid);
745 }
746 } else if( sqlite_query_status(sqlres) == dbERROR ) {
747 err_n = sqlite_log_error_xml(ctx, sqlres);
748 }
749 } else if( strcmp(mode, "list") == 0 ) {
750 res_d = adminacclvl_Get(ctx, fmap_m);
751 }
752
753 if( res_d == NULL ) {
754 eurephia_log(ctx, LOG_ERROR, 0, "Failed to update admin access");
755 res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to complete %s operation", mode);
756 }
757 if( err_n != NULL ) {
758 xmlFreeNode(err_n);
759 }
760 sqlite_free_results(sqlres);
761 eDBfreeMapping(fmap_m);
762
763 return res_d;
764 }