Addition of daignostics for failure to add users

This commit is contained in:
Mark Riddoch
2013-07-23 10:46:58 +02:00
parent d536ca1c2c
commit 36f62637c9
4 changed files with 33 additions and 12 deletions

View File

@ -23,6 +23,8 @@
#include <crypt.h> #include <crypt.h>
#include <users.h> #include <users.h>
#include <adminusers.h> #include <adminusers.h>
#include <skygw_utils.h>
#include <log_manager.h>
/** /**
* @file adminusers.c - Administration user account management * @file adminusers.c - Administration user account management
@ -32,6 +34,7 @@
* *
* Date Who Description * Date Who Description
* 18/07/13 Mark Riddoch Initial implementation * 18/07/13 Mark Riddoch Initial implementation
* 23/07/13 Mark Riddoch Addition of error mechanism to add user
* *
* @endverbatim * @endverbatim
*/ */
@ -42,6 +45,11 @@ static void initialise();
static USERS *users = NULL; static USERS *users = NULL;
static int admin_init = 0; static int admin_init = 0;
static char *ADMIN_ERR_NOMEM = "Out of memory";
static char *ADMIN_ERR_FILEOPEN = "Unable to create password file";
static char *ADMIN_ERR_DUPLICATE = "Duplicate username specified";
static char *ADMIN_ERR_FILEAPPEND = "Unable to append to password file";
/** /**
* Admin Users initialisation * Admin Users initialisation
*/ */
@ -119,9 +127,9 @@ char uname[80], passwd[80];
* *
* @param uname Name of the new user * @param uname Name of the new user
* @param passwd Password for the new user * @param passwd Password for the new user
* @return The number of users added * @return NULL on success or an error string on failure
*/ */
int char *
admin_add_user(char *uname, char *passwd) admin_add_user(char *uname, char *passwd)
{ {
FILE *fp; FILE *fp;
@ -135,22 +143,32 @@ char fname[1024], *home, *cpasswd;
if (users == NULL) if (users == NULL)
{ {
if ((users = users_alloc()) == NULL) if ((users = users_alloc()) == NULL)
return 0; return ADMIN_ERR_NOMEM;
if ((fp = fopen(fname, "w")) == NULL) if ((fp = fopen(fname, "w")) == NULL)
return 0; {
skygw_log_write(NULL, LOGFILE_ERROR,
"Unable to create password file %s.\n",
fname);
return ADMIN_ERR_FILEOPEN;
}
fclose(fp); fclose(fp);
} }
if (users_fetch(users, uname) != NULL) if (users_fetch(users, uname) != NULL)
{ {
return 0; return ADMIN_ERR_DUPLICATE;
} }
cpasswd = crypt(passwd, ADMIN_SALT); cpasswd = crypt(passwd, ADMIN_SALT);
users_add(users, uname, cpasswd); users_add(users, uname, cpasswd);
if ((fp = fopen(fname, "a")) == NULL) if ((fp = fopen(fname, "a")) == NULL)
return 0; {
skygw_log_write(NULL, LOGFILE_ERROR,
"Unable to append to password file %s.\n",
fname);
return ADMIN_ERR_FILEAPPEND;
}
fprintf(fp, "%s:%s\n", uname, cpasswd); fprintf(fp, "%s:%s\n", uname, cpasswd);
fclose(fp); fclose(fp);
return 1; return NULL;
} }
/** /**

View File

@ -32,7 +32,7 @@
#define ADMIN_SALT "MS" #define ADMIN_SALT "MS"
extern int admin_verify(char *, char *); extern int admin_verify(char *, char *);
extern int admin_add_user(char *, char *); extern char *admin_add_user(char *, char *);
extern int admin_test_user(char *); extern int admin_test_user(char *);
#endif #endif

View File

@ -256,7 +256,7 @@ char *uname = defaultUser, *passwd = defaultPasswd;
num_fields = mysql_num_fields(result); num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) while ((row = mysql_fetch_row(result)))
{ {
if (strncmp(row[0], "JOINED", 3) == 0) if (strncasecmp(row[0], "JOINED", 3) == 0)
isjoined = 1; isjoined = 1;
} }
mysql_free_result(result); mysql_free_result(result);

View File

@ -465,7 +465,8 @@ unsigned int bitvalue;
static void static void
reload_users(DCB *dcb, SERVICE *service) reload_users(DCB *dcb, SERVICE *service)
{ {
dcb_printf(dcb, "Loaded %d users.\n", reload_mysql_users(service)); dcb_printf(dcb, "Loaded %d database users for server %s.\n",
reload_mysql_users(service), service->name);
} }
/** /**
@ -490,13 +491,15 @@ reload_config(DCB *dcb)
static void static void
telnetdAddUser(DCB *dcb, char *user, char *passwd) telnetdAddUser(DCB *dcb, char *user, char *passwd)
{ {
char *err;
if (admin_test_user(user)) if (admin_test_user(user))
{ {
dcb_printf(dcb, "User %s already exists.\n", user); dcb_printf(dcb, "User %s already exists.\n", user);
return; return;
} }
if (admin_add_user(user, passwd)) if ((err = admin_add_user(user, passwd)) == NULL)
dcb_printf(dcb, "User %s has been succesfully added.\n", user); dcb_printf(dcb, "User %s has been succesfully added.\n", user);
else else
dcb_printf(dcb, "Failed to add new user.\n"); dcb_printf(dcb, "Failed to add new user. %s\n", err);
} }