1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/* eurephiadm.c -- CLI based admin program for eurephia
*
* GPLv2 only - Copyright (C) 2008, 2009
* David Sommerseth <dazo@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file eurephiadm.c
* @author David Sommerseth <dazo@users.sourceforge.net>
* @date 2008-11-28
*
* @brief Main part of the eurephiadm command line administration utility for eurephia
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <libgen.h>
#include <libxml/tree.h>
#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
#include <eurephia_values.h>
#include <eurephia_xml.h>
#include <eurephiadb_driver.h>
#include <eurephiadb_session_common.h>
#include <eurephiadb.h>
#define MODULE "eurephiadm" /**< Need to define the active module before including argparser.h */
#include "argparser.h"
#include "client_context.h"
#include "client_config.h"
#include "client_session.h"
#include "get_console_input.h"
#include "commands.h"
/**
* Prints version and copyright info to stdout
*
* @param fprg Full path to binary
*
* @return Returns the basename part of the binary file name
*/
char *print_version(char *fprg) {
char *prg = basename(fprg);
fprintf(stdout, "%s (v%s) - eurephia administration utility\n"
"Copyright (C) 2008, 2009 David Sommerseth <dazo@users.sourceforge.net>\n",
prg, EUREPHIAVERSION);
return prg;
}
/**
* Wrapper function to be used by the command API in eurephiadm
*
*/
void help_Help() {
char *argv[] = { MODULE, NULL };
cmd_Help(NULL, NULL, NULL, 1, argv);
}
/**
* Displays a generic help screen, if no arguments where given, or else the
* the help screen for the eurephiadm command if it was found.
*
* @param ctx eurephiaCTX
* @param sess eurephiaSESSION of the current logged in user
* @param cfg eurephiaVALUES struct of the current configuration
* @param argc argument count for the eurephiadm command
* @param argv argument table for the eurephiadm command
*
* @return returns 0 on success, otherwise 1.
*/
int cmd_Help(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
eurephiadm_functions *func = NULL;
int i;
char *prg = NULL;
// Print the modules help screen if module name is given
if( ((argc == 2) && (strcmp(argv[0], "help") == 0))
|| ((argc == 3) && (*argv[1] == '-')) ) {
char *help = (argc == 2 ? argv[1] : argv[2]);
for( i = 0; cmdline_functions[i].command != NULL; i++ ) {
func = (eurephiadm_functions *)&cmdline_functions[i];
if( (strcmp(func->command, help) == 0) && (func->helpfunc != NULL) ) {
func->helpfunc();
return 0;
}
}
fprintf(stderr, "%s: ERROR :: Could not find help for module '%s'\n", MODULE , help);
return 1;
}
prg = print_version((strcmp(argv[0], "help") == 0) ? MODULE : argv[0]);
fprintf(stdout, "\n Usage: %s [global options] <command> [command options]\n\n", prg);
fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
"-V | --version", "", "Print version of eurephiadm");
fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
"-l | --log", "<file name>", "Log to file");
fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
"-L | --log-level", "<log level>", "Set log verbosity");
fprintf(stdout,"\n %-20.20s %-20.20s %-30.30s\n",
"Command","Arguments","Description");
fprintf(stdout, " ------------------------------------------------------------------------\n");
for( i = 0; cmdline_functions[i].command != NULL; i++ ) {
func = (eurephiadm_functions *)&cmdline_functions[i];
fprintf(stdout," %-20.20s %-20.20s %-30.30s\n",
func->command,
(func->arghint != NULL ? func->arghint : ""),
func->helpinfo);
}
fprintf(stdout, "\n");
return 0;
}
/**
* eurephiadm logout command. Logs out the current active session.
*
* @param ctx eurephiaCTX
* @param sess eurephiaSESSION of the current logged in user
* @param cfg eurephiaVALUES struct of the current configuration
* @param argc argument count for the logout command
* @param argv argument table for the logout command
*
* @return returns 0 on success, otherwise 1.
*/
int cmd_Logout(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
xmlDoc *logoutxml = NULL, *resxml = NULL;
xmlNode *logout_n = NULL;
eurephiaRESULT *res = NULL;
int rc = 0;
eurephiaXML_CreateDoc(ctx, 1, "Register", &logoutxml, &logout_n);
assert( (logoutxml != NULL) && (logout_n != NULL) );
xmlNewProp(logout_n, (xmlChar *) "mode", (xmlChar *) "logout");
xmlAddChild(logout_n, xmlNewText((xmlChar *) argv[0])); // A dirty hack, but the sesskey is here
resxml = eDBadminAuthenticate(ctx, logoutxml);
xmlFreeDoc(logoutxml);
if( !eurephiaXML_IsResultMsg(ctx, resxml) ) {
eurephia_log(ctx, LOG_FATAL, 0, "Failed to logout session");
rc = 1;
} else {
res = eurephiaXML_ParseResultMsg(ctx, resxml);
if( res->resultType == exmlERROR ) {
fprintf(stderr, "%s: %s\n", MODULE, res->message);
rc = 1;
} else {
fprintf(stdout, "%s: %s\n", MODULE, res->message);
rc = 0;
}
}
xmlFreeDoc(resxml);
free_nullsafe(ctx, res);
return rc;
}
/**
* eurephiadm show-config command. Dumps configuration to stdout. If the user is logged in, the
* configuration saved in the database is also dumped.
*
* @param ctx eurephiaCTX
* @param sess eurephiaSESSION of the current logged in user
* @param cfg eurephiaVALUES struct of the current configuration
* @param argc argument count for the show-config command
* @param argv argument table for the show-config command
*
* @return returns 0 on success, otherwise 1.
*/
int cmd_ShowCfg(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
void dump_values(eurephiaVALUES *vls) {
eurephiaVALUES *ptr = NULL;
fprintf(stdout, "----------------------------------------------------------------------\n");
for( ptr = vls; ptr != NULL; ptr = ptr->next) {
if( ptr->key != NULL ) {
fprintf(stdout, "%20s = %s\n", ptr->key, ptr->val);
}
}
fprintf(stdout, "----------------------------------------------------------------------\n");
}
if( cfg != NULL ) {
fprintf(stdout, "\n** eurephiadm configuration settings (config file) **\n");
dump_values(cfg);
fprintf(stdout, "\n");
}
if( ctx != NULL ) {
fprintf(stdout, "** eurephia configuration settings (set in database) **\n");
dump_values(ctx->dbc->config);
fprintf(stdout, "\n");
}
return 0;
}
/**
* Establish a connection to the database and register the database connection in the eurephiaCTX
*
* @param ctx eurephiaCTX
* @param argstr argument string for the database connection
*
* @return Returns 1 on success, otherwise 0.
*/
int eurephia_ConnectDB(eurephiaCTX *ctx, const char *argstr) {
char *delims = " ";
char *cp = NULL;
const char *dbargv[MAX_ARGUMENTS];
int dbargc = 0;
if( (argstr == NULL) || (strlen(argstr) < 1) ) {
eurephia_log(ctx, LOG_FATAL, 0, "No database connection string given");
return 0;
}
// Split up argstr into separate arguments
cp = strdup(argstr);
assert(cp != NULL);
dbargc = 0;
dbargv[dbargc] = strtok(cp, delims);
while( (dbargv[dbargc] != NULL) && (dbargc <= MAX_ARGUMENTS) ) {
dbargv[++dbargc] = strtok(NULL, delims);
}
if( !eDBconnect(ctx, dbargc, dbargv) ) {
eurephia_log(ctx, LOG_PANIC, 0, "Could not connect to the database");
eDBlink_close(ctx);
return 0;
}
free_nullsafe(ctx, cp);
return 1;
}
/**
* Log in a user. The function will ask for user name and password and authenticate the user against
* the eurephia database and make sure the user is granted access to the requested access level.
*
* @param ctx eurephiaCTX
* @param cfg eurephiaVALUES with configuration settings
* @param req_access String (const char *) containing the requested access level.
*
* @return Returns a eurephiaSESSION pointer to the authenticated user session on success, otherwise NULL.
*/
static eurephiaSESSION *do_login(eurephiaCTX *ctx, eurephiaVALUES *cfg, const char *req_access) {
xmlDoc *loginxml = NULL, *resxml = NULL;
xmlNode *login_n = NULL;
eurephiaRESULT *res = NULL;
eurephiaSESSION *session = NULL;
char username[33], password[33], *tmp = NULL;
memset(&username, 0, 33);
memset(&password, 0, 33);
if( (tmp = eGet_value(cfg, "username")) == NULL ) {
memset(username, 0, 34);
get_console_input(username, 32, "User:", 0);
} else {
strncpy(username, tmp, 32);
}
get_console_input(password, 32, "Password:", 1);
eurephiaXML_CreateDoc(ctx, 1, "Authenticate", &loginxml, &login_n);
assert( (loginxml != NULL) && (login_n != NULL) );
xmlNewProp(login_n, (xmlChar *) "mode", (xmlChar *) "user");
xmlNewChild(login_n, NULL, (xmlChar *) "username", (xmlChar *) username);
xmlNewChild(login_n, NULL, (xmlChar *) "password", (xmlChar *) password);
xmlNewChild(login_n, NULL, (xmlChar *) "accesslevel", (xmlChar *) req_access);
resxml = eDBadminAuthenticate(ctx, loginxml);
xmlFreeDoc(loginxml);
memset(username, 0, 33);
memset(password, 0, 33);
if( !eurephiaXML_IsResultMsg(ctx, resxml) ) {
return NULL;
}
res = eurephiaXML_ParseResultMsg(ctx, resxml);
if( res->resultType == exmlERROR ) {
fprintf(stderr, "%s: %s\n", MODULE, res->message);
free_nullsafe(ctx, res);
xmlFreeDoc(resxml);
return NULL;
}
session = create_session(ctx, NULL);
login_n = xmlFindNode(res->details, "UserAccount");
// Borrow username for converting uid to a string
snprintf(username, 30, "%s", xmlGetAttrValue(login_n->properties, "uid"));
xmlFreeDoc(resxml);
free_nullsafe(ctx, res);
if( !eDBset_session_value(ctx, session, "uid", username) ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update session variables (uid)");
eDBfree_session(ctx, session);
return NULL;
};
// Register the session as logged in
eurephiaXML_CreateDoc(ctx, 1, "Register", &loginxml, &login_n);
assert( (loginxml != NULL) && (login_n != NULL) );
xmlNewProp(login_n, (xmlChar *) "mode", (xmlChar *) "login");
xmlNewProp(login_n, (xmlChar *) "uid", (xmlChar *) username);
xmlAddChild(login_n, xmlNewText((xmlChar *) session->sessionkey));
resxml = eDBadminAuthenticate(ctx, loginxml);
xmlFreeDoc(loginxml);
if( !eurephiaXML_IsResultMsg(ctx, resxml) ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not register login");
eDBfree_session(ctx, session);
xmlFreeDoc(resxml);
return NULL;
}
res = eurephiaXML_ParseResultMsg(ctx, resxml);
if( res->resultType == exmlERROR ) {
fprintf(stderr, "%s: %s\n", MODULE, res->message);
free_nullsafe(ctx, res);
xmlFreeDoc(resxml);
eDBfree_session(ctx, session);
return NULL;
}
xmlFreeDoc(resxml);
free_nullsafe(ctx, res);
return session;
}
/**
* Validates an exisiting session key against a requested access level
*
* @param ctx eurephiaCTX
* @param sesskey String containing the session key
* @param accesslvl String containing the requested access level
*
* @return Returns 1 if session is valid and access level is granted to the session, otherwise 0
*/
static int validate_session(eurephiaCTX *ctx, const char *sesskey, const char *accesslvl) {
xmlDoc *sessxml = NULL, *resxml = NULL;
xmlNode *sess_n = NULL;
int ret = 0;
eurephiaXML_CreateDoc(ctx, 1, "Authenticate", &sessxml, &sess_n);
assert( (sessxml != NULL) && (sess_n != NULL) );
xmlNewProp(sess_n, (xmlChar *) "mode", (xmlChar *) "session");
xmlNewChild(sess_n, NULL, (xmlChar *) "sessionkey", (xmlChar *) sesskey);
xmlNewChild(sess_n, NULL, (xmlChar *) "accesslevel", (xmlChar *) accesslvl);
resxml = eDBadminAuthenticate(ctx, sessxml);
xmlFreeDoc(sessxml);
if( !eurephiaXML_IsResultMsg(ctx, resxml) ) {
ret = 0;
} else {
eurephiaRESULT *res = NULL;
res = eurephiaXML_ParseResultMsg(ctx, resxml);
if( res->resultType == exmlERROR ) {
fprintf(stderr, "%s: %s\n", MODULE, res->message);
ret = 0;
} else {
ret = 1;
}
free_nullsafe(ctx, res);
}
if( resxml ) {
xmlFreeDoc(resxml);
}
return ret;
}
/**
* Converts a argument table (char **) into a space separated string (char *)
*
* @param argc argument counter
* @param argv argument array
*
* @return String (char *) of all arguments, separated by space.
*/
char *args2string(int argc, char **argv) {
char *res = NULL;
int len = 0, i = 0;
for( i = 0; i < argc; i++ ) {
len += strlen_nullsafe(argv[i]) + 1;
}
res = (char *) malloc_nullsafe(NULL, len+2);
assert( res != NULL );
for( i = 0; i < argc; i++ ) {
strcat(res, argv[i]);
strcat(res, " ");
}
return res;
}
/**
* eurephiadm main function
*
* @param argc argument counter
* @param argv argument table
*
* @return returns 0 on success, otherwise a value > 0
*/
int main(int argc, char **argv) {
char *logfile = NULL;
int loglevel = 0;
eurephiaCTX *ctx = NULL;
eurephiaSESSION *session = NULL;
eurephiaVALUES *cfg = NULL;
char *sesskey_file = NULL;
char *dbparams = NULL, *cmdargv[MAX_ARGUMENTS];
int rc = 0, i = 0, found = 0, cmdargc = 0, argi = 0;
eurephiadm_functions *call_fnc;
// Global arguments we accept
static e_options argopts[] = {
{"--version", "-V", 0},
{"--help", "-h", 0},
{"--log", "-l", 1},
{"--log-level", "-L", 1},
{NULL, NULL, 0}
};
if( argc < 2 ) {
cmd_Help(NULL, NULL, NULL, argc, argv);
return 1;
}
// Parse argument line
argi = 1;
for( argi = 1; argi < argc; argi++ ) {
if( *argv[argi] != '-' ) {
break;
}
switch( eurephia_getopt(&argi, argc, argv, argopts) ) {
case 'V':
print_version(argv[0]);
return 0;
case 'h':
cmd_Help(NULL, NULL, NULL, argc, argv);
return 0;
case 'l':
logfile = optargs[0];
break;
case 'L':
loglevel = atoi_nullsafe(optargs[0]);
break;
default:
return 1;
break;
}
}
if( argi >= argc ) {
fprintf(stderr, "%s: ERROR :: No command given\n", basename(argv[0]));
return 1;
} else {
for( i = argi; ((i < argc) && (i < MAX_ARGUMENTS)); i++ ) {
cmdargv[cmdargc++] = argv[i];
}
}
// Find the command requested and save a pointer to that commands C function
for( i = 0; cmdline_functions[i].command != NULL; i++ ) {
call_fnc = (eurephiadm_functions *)&cmdline_functions[i];
if( strcmp(call_fnc->command, cmdargv[0]) == 0 ) {
found = 1;
break;
}
}
// Exit with error if command is not found
if( found == 0 ) {
fprintf(stderr, "%s: No such command '%s'\n", basename(argv[0]), argv[argi]);
return 5;
}
// Read configuration file
cfg = ReadConfig("EUREPHIADM_CONFIG", "eurephiadm.cfg");
if( cfg == NULL ) {
fprintf(stderr, "No configuration file found.\n");
return 2;
}
// If function do not need a logged in session, go a head process it now and exit
if( call_fnc->need_session == 0 ) {
rc = call_fnc->function(NULL, NULL, cfg, cmdargc, cmdargv);
eFree_values(NULL, cfg);
return rc;
}
//
// Load database driver, setup a context and create or reuse an open session
// and then call the commando's function
//
// Create a eurephia context and load database driver
ctx = eurephiaCTX_init("eurephiadm", logfile, loglevel, cfg);
if( ctx == NULL ) {
fprintf(stderr, "Could not initialise a eurephia context.\n");
return 3;
}
// Connect to the database
dbparams = eGet_value(cfg, "database_params");
if( !eurephia_ConnectDB(ctx, dbparams) ) {
return 4;
}
// Check if we have XSLT path available, if not setup a default path
if( eGet_value(ctx->dbc->config, "eurephiadm_xslt_path") ) {
eAdd_value(ctx, cfg, "eurephiadm_xslt_path", eGet_value(ctx->dbc->config, "eurephiadm_xslt_path"));
} else {
eAdd_value(ctx, cfg, "eurephiadm_xslt_path", EUREPHIADM_XSLT_PATH);
}
// Load session file, if it exists. (reuse open session)
if( (sesskey_file = read_session_file(ctx)) == NULL ) {
// No session file - go straight to login
if( strcmp(call_fnc->command, "logout") == 0) {
fprintf(stderr, "You are already logged out\n");
rc = 0;
goto exit;
}
session = do_login(ctx, cfg, call_fnc->accesslvl);
} else {
// If we are logging out, do so here - without checking the session
if( strcmp(call_fnc->command, "logout") == 0) {
char *logout_argv[] = { sesskey_file };
rc = cmd_Logout(ctx, NULL, cfg, 0, logout_argv);
remove_session_file(ctx);
free_nullsafe(ctx, sesskey_file);
goto exit;
}
// Session file found, check if it still is a valid session
if( validate_session(ctx, sesskey_file, call_fnc->accesslvl) ) {;
// If valid, load this session
session = create_session(ctx, sesskey_file);
} else {
// If not valid, remove session file and go to login
remove_session_file(ctx);
session = do_login(ctx, cfg, call_fnc->accesslvl);
}
}
free_nullsafe(ctx, sesskey_file);
// If we have an open session now, call the requested command
if(session != NULL ) {
char *argstr = NULL;
// Write session info to a file
if( !write_session_file(ctx, session) ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could not write session file. Login needed for actions");
};
// Execute the requested command
rc = call_fnc->function(ctx, session, cfg, cmdargc, cmdargv);
argstr = args2string(cmdargc, cmdargv);
eurephia_log(ctx, LOG_INFO, 0, "Command: %s (result: %i)", argstr, rc);
free_nullsafe(ctx, argstr);
// Remove session info from memory
eDBfree_session(ctx, session);
} else {
// If not an open session, make sure the session file is gone
remove_session_file(ctx);
}
exit:
// Disconnect from the database, and remove config and our context before exiting
eFree_values(ctx, cfg);
eDBdisconnect(ctx);
eurephiaCTX_destroy(ctx);
return rc;
}
|