summaryrefslogtreecommitdiffstats
path: root/eurephiadm
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-18 11:29:24 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-18 11:29:24 +0100
commit39cae7bac832d9e1279291dcf9e814d51ba97c9f (patch)
treec8a7c7ce096422cb6cbf2a0540b4afb96c35507d /eurephiadm
parenta451275dbac5614b98ee331cc10151d8b3435d00 (diff)
downloadeurephia-39cae7bac832d9e1279291dcf9e814d51ba97c9f.tar.gz
eurephia-39cae7bac832d9e1279291dcf9e814d51ba97c9f.tar.xz
eurephia-39cae7bac832d9e1279291dcf9e814d51ba97c9f.zip
users command: Added first attempt for add user
Diffstat (limited to 'eurephiadm')
-rw-r--r--eurephiadm/commands/users.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/eurephiadm/commands/users.c b/eurephiadm/commands/users.c
index 89a8641..0032259 100644
--- a/eurephiadm/commands/users.c
+++ b/eurephiadm/commands/users.c
@@ -21,6 +21,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
#include <assert.h>
#ifdef HAVE_LIBXML2
@@ -41,6 +45,7 @@
#include <eurephiadb_driver.h>
#include "../argparser.h"
+#include "../get_console_input.h"
inline void field_print_int(char *label, int val) {
printf("%25.25s: %i\n", label, val);
@@ -574,6 +579,151 @@ int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *
return (rc != 1);
}
+int add_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
+ xmlDoc *user_xml = NULL;
+ xmlNode *node = NULL, *node2 = NULL;
+ struct stat cert_stat;
+ int i = 0, certid = 0, rc = 0;
+ char *uname = NULL, *passwd = NULL, *certfile = NULL, *digest = NULL;
+
+ e_options addu_args[] = {
+ {"--username", "-u", 1},
+ {"--password", "-P", 1},
+ {"--certid", "-C", 1},
+ {"--certfile", "-c", 1},
+ {"--digest", "-d", 1},
+ {"--help", "-h", 0},
+ {NULL, NULL, 0}
+ };
+
+ assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL));
+
+ // Parse arguments
+ for( i = 1; i < argc; i++ ) {
+ switch( eurephia_getopt(&i, argc, argv, addu_args) ) {
+ case 'u':
+ if( strlen_nullsafe(optargs[0]) < 3 ) {
+ fprintf(stderr, "%s: username is too short\n", MODULE);
+ return 1;
+ }
+ uname = optargs[0];
+ break;
+
+ case 'P':
+ if( strlen_nullsafe(optargs[0]) < 6 ) {
+ fprintf(stderr, "%s: password is too short\n", MODULE);
+ return 1;
+ }
+ passwd = strdup_nullsafe(optargs[0]);
+ break;
+
+ case 'C':
+ if( (certid = atoi_nullsafe(optargs[0])) < 1 ) {
+ fprintf(stderr, "%s: Invalid certid (numeric value > 0)\n", MODULE);
+ return 1;
+ }
+ break;
+
+ case 'c': // Check if certfile exists and is readable for us
+ if( strlen_nullsafe(optargs[0]) < 1 ) {
+ fprintf(stderr, "%s: certfile is too short\n", MODULE);
+ return 1;
+ }
+ certfile = optargs[0];
+
+ if( stat(certfile, &cert_stat) == -1 ) {
+ fprintf(stderr, "%s: Could not access certfile: %s (%s)\n", MODULE,
+ certfile, strerror(errno));
+ return 1;
+ }
+
+ if( cert_stat.st_size == 0 ) {
+ fprintf(stderr, "%s: certfile '%s' is empty\n", MODULE, certfile);
+ return 1;
+ }
+ break;
+
+ case 'd':
+ if( strlen_nullsafe(optargs[0]) < 59 ) {
+ fprintf(stderr, "%s: Certificate digest is too short\n", MODULE);
+ return 1;
+ }
+ digest = optargs[0];
+ break;
+
+ case 'h':
+ display_users_help('A');
+ return 0;
+
+ default:
+ return 1;
+ }
+ }
+
+ // Make sure received arguments are sane
+ if( uname == NULL ) {
+ fprintf(stderr, "%s: Missing user name\n", MODULE);
+ return 1;
+ }
+
+ if( ((certid > 0) && (digest != NULL))
+ || ((certid > 0) && (certfile != NULL))
+ || ((digest != NULL) && (certfile != NULL)) ) {
+ fprintf(stderr, "%s: --certid, --certfile and --digest cannot be used together\n", MODULE);
+ return 1;
+ }
+
+ // If we do not have a password .... ask for password via console
+ if( passwd == NULL ) {
+ char *chkpwd = NULL;
+
+ passwd = (char *) malloc(66);
+ assert(passwd != NULL);
+ memset(passwd, 0, 66);
+
+ chkpwd = (char *) malloc(66);
+ assert(chkpwd != NULL);
+ memset(chkpwd, 0, 66);
+
+ get_console_input(passwd, 64, "Password for user:", 1);
+ if( strlen_nullsafe(passwd) < 4 ) {
+ free_nullsafe(passwd);
+ fprintf(stderr, "%s: Password is too short\n", MODULE);
+ return 1;
+ }
+
+ get_console_input(chkpwd, 64, "Verify password for user:", 1);
+ if( strcmp(passwd, chkpwd) != 0 ) {
+ free_nullsafe(passwd);
+ free_nullsafe(chkpwd);
+ fprintf(stderr, "%s: Passwords didn't match\n", MODULE);
+ return 1;
+ }
+ free_nullsafe(chkpwd);
+ }
+
+
+ // Prepare add user XML document with fieldMapping
+ eurephiaXML_CreateDoc(ctx, 1, "add_user", &user_xml, &node);
+ node = xmlNewChild(node, NULL, (xmlChar *) "fieldMapping", NULL);
+ xmlNewProp(node, (xmlChar *) "table", (xmlChar *) "users");
+
+ xmlNewChild(node, NULL, (xmlChar *) "username", (xmlChar *) uname);
+ node2 = xmlNewChild(node, NULL, (xmlChar *) "password", (xmlChar *) passwd);
+ xmlNewProp(node2, (xmlChar *) "pwhash", (xmlChar *) "none");
+
+ xmlSaveFormatFileEnc("-", user_xml, "UTF-8", 1);
+
+ // Add the user
+ rc = eDBadminAddUser(ctx, user_xml);
+ fprintf(stdout, "%s: %s\n", MODULE,
+ (rc == 1 ? "User registered successfully" : "Failed to register user"));
+ memset(passwd, 0, strlen_nullsafe(passwd));
+ free_nullsafe(passwd);
+
+ return (rc != 1);
+}
+
int cmd_Users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
char **mode_argv;
@@ -616,6 +766,13 @@ int cmd_Users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int
mode_fnc = help_Users2;
break;
+ case 'A':
+ mode_fnc = add_user;
+ break;
+
+ case 'D':
+ // mode_fnc = delete_user;
+ break;
default:
break;
}