diff --git a/core/adminusers.c b/core/adminusers.c index ea0b72813..ec3cb796b 100644 --- a/core/adminusers.c +++ b/core/adminusers.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include /** * @file adminusers.c - Administration user account management @@ -32,6 +34,7 @@ * * Date Who Description * 18/07/13 Mark Riddoch Initial implementation + * 23/07/13 Mark Riddoch Addition of error mechanism to add user * * @endverbatim */ @@ -42,6 +45,11 @@ static void initialise(); static USERS *users = NULL; 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 */ @@ -119,9 +127,9 @@ char uname[80], passwd[80]; * * @param uname Name of 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) { FILE *fp; @@ -135,22 +143,32 @@ char fname[1024], *home, *cpasswd; if (users == NULL) { if ((users = users_alloc()) == NULL) - return 0; + return ADMIN_ERR_NOMEM; 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); } if (users_fetch(users, uname) != NULL) { - return 0; + return ADMIN_ERR_DUPLICATE; } cpasswd = crypt(passwd, ADMIN_SALT); users_add(users, uname, cpasswd); 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); fclose(fp); - return 1; + return NULL; } /** diff --git a/include/adminusers.h b/include/adminusers.h index ef1cf0814..1d38e24cb 100644 --- a/include/adminusers.h +++ b/include/adminusers.h @@ -32,7 +32,7 @@ #define ADMIN_SALT "MS" 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 *); #endif diff --git a/modules/monitor/galera_mon.c b/modules/monitor/galera_mon.c index b4cbedb79..2e8cab8a3 100644 --- a/modules/monitor/galera_mon.c +++ b/modules/monitor/galera_mon.c @@ -256,7 +256,7 @@ char *uname = defaultUser, *passwd = defaultPasswd; num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { - if (strncmp(row[0], "JOINED", 3) == 0) + if (strncasecmp(row[0], "JOINED", 3) == 0) isjoined = 1; } mysql_free_result(result); diff --git a/modules/routing/debugcmd.c b/modules/routing/debugcmd.c index b947c718e..fb2ee398e 100644 --- a/modules/routing/debugcmd.c +++ b/modules/routing/debugcmd.c @@ -465,7 +465,8 @@ unsigned int bitvalue; static void 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 telnetdAddUser(DCB *dcb, char *user, char *passwd) { +char *err; + if (admin_test_user(user)) { dcb_printf(dcb, "User %s already exists.\n", user); 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); else - dcb_printf(dcb, "Failed to add new user.\n"); + dcb_printf(dcb, "Failed to add new user. %s\n", err); }