Reindent server/core/adminusers.c

This commit is contained in:
Johan Wikman
2015-11-30 10:38:21 +02:00
parent 6f737d9484
commit f6e8662933
2 changed files with 264 additions and 233 deletions

View File

@ -70,7 +70,10 @@ static void
initialise() initialise()
{ {
if (admin_init) if (admin_init)
{
return; return;
}
admin_init = 1; admin_init = 1;
users = loadUsers(); users = loadUsers();
} }
@ -91,15 +94,21 @@ char *pw;
if (users == NULL) if (users == NULL)
{ {
if (strcmp(username, "admin") == 0 && strcmp(password, "mariadb") == 0) if (strcmp(username, "admin") == 0 && strcmp(password, "mariadb") == 0)
{
return 1; return 1;
} }
}
else else
{ {
if ((pw = users_fetch(users, username)) == NULL) if ((pw = users_fetch(users, username)) == NULL)
{
return 0; return 0;
}
if (strcmp(pw, crypt(password, ADMIN_SALT)) == 0) if (strcmp(pw, crypt(password, ADMIN_SALT)) == 0)
{
return 1; return 1;
} }
}
return 0; return 0;
} }
@ -121,7 +130,9 @@ char uname[80], passwd[80];
snprintf(fname,1023, "%s/passwd", get_datadir()); snprintf(fname,1023, "%s/passwd", get_datadir());
fname[1023] = '\0'; fname[1023] = '\0';
if ((fp = fopen(fname, "r")) == NULL) if ((fp = fopen(fname, "r")) == NULL)
{
return NULL; return NULL;
}
if ((rval = users_alloc()) == NULL) if ((rval = users_alloc()) == NULL)
{ {
fclose(fp); fclose(fp);
@ -152,8 +163,12 @@ char fname[1024], *home, *cpasswd;
initialise(); initialise();
if (access(get_datadir(), F_OK) != 0) if (access(get_datadir(), F_OK) != 0)
{
if (mkdir(get_datadir(), S_IRWXU) != 0 && errno != EEXIST) if (mkdir(get_datadir(), S_IRWXU) != 0 && errno != EEXIST)
{
return ADMIN_ERR_PWDFILEOPEN; return ADMIN_ERR_PWDFILEOPEN;
}
}
snprintf(fname,1023, "%s/passwd", get_datadir()); snprintf(fname,1023, "%s/passwd", get_datadir());
fname[1023] = '\0'; fname[1023] = '\0';
@ -162,7 +177,9 @@ char fname[1024], *home, *cpasswd;
MXS_NOTICE("Create initial password file."); MXS_NOTICE("Create initial password file.");
if ((users = users_alloc()) == NULL) if ((users = users_alloc()) == NULL)
{
return ADMIN_ERR_NOMEM; return ADMIN_ERR_NOMEM;
}
if ((fp = fopen(fname, "w")) == NULL) if ((fp = fopen(fname, "w")) == NULL)
{ {
MXS_ERROR("Unable to create password file %s.", fname); MXS_ERROR("Unable to create password file %s.", fname);
@ -209,12 +226,14 @@ char* admin_remove_user(
fpos_t rpos; fpos_t rpos;
int n_deleted; int n_deleted;
if (!admin_search_user(uname)) { if (!admin_search_user(uname))
{
MXS_ERROR("Couldn't find user %s. Removing user failed.", uname); MXS_ERROR("Couldn't find user %s. Removing user failed.", uname);
return ADMIN_ERR_USERNOTFOUND; return ADMIN_ERR_USERNOTFOUND;
} }
if (admin_verify(uname, passwd) == 0) { if (admin_verify(uname, passwd) == 0)
{
MXS_ERROR("Authentication failed, wrong user/password " MXS_ERROR("Authentication failed, wrong user/password "
"combination. Removing user failed."); "combination. Removing user failed.");
return ADMIN_ERR_AUTHENTICATION; return ADMIN_ERR_AUTHENTICATION;
@ -224,7 +243,8 @@ char* admin_remove_user(
/** Remove user from in-memory structure */ /** Remove user from in-memory structure */
n_deleted = users_delete(users, uname); n_deleted = users_delete(users, uname);
if (n_deleted == 0) { if (n_deleted == 0)
{
MXS_ERROR("Deleting the only user is forbidden. Add new " MXS_ERROR("Deleting the only user is forbidden. Add new "
"user before deleting the one."); "user before deleting the one.");
return ADMIN_ERR_DELLASTUSER; return ADMIN_ERR_DELLASTUSER;
@ -267,7 +287,8 @@ char* admin_remove_user(
/** /**
* Scan passwd and copy all but matching lines to temp file. * Scan passwd and copy all but matching lines to temp file.
*/ */
if (fgetpos(fp, &rpos) != 0) { if (fgetpos(fp, &rpos) != 0)
{
int err = errno; int err = errno;
MXS_ERROR("Unable to process passwd file %s : errno %d.\n" MXS_ERROR("Unable to process passwd file %s : errno %d.\n"
"Removing user from file failed, and must be done " "Removing user from file failed, and must be done "
@ -286,15 +307,18 @@ char* admin_remove_user(
* Compare username what was found from passwd file. * Compare username what was found from passwd file.
* Unmatching lines are copied to tmp file. * Unmatching lines are copied to tmp file.
*/ */
if (strncmp(uname, fusr, strlen(uname)+1) != 0) { if (strncmp(uname, fusr, strlen(uname)+1) != 0)
if(fsetpos(fp, &rpos) != 0){ /** one step back */ {
if(fsetpos(fp, &rpos) != 0)
{ /** one step back */
MXS_ERROR("Unable to set stream position. "); MXS_ERROR("Unable to set stream position. ");
} }
fgets(line, LINELEN, fp); fgets(line, LINELEN, fp);
fputs(line, fp_tmp); fputs(line, fp_tmp);
} }
if (fgetpos(fp, &rpos) != 0) { if (fgetpos(fp, &rpos) != 0)
{
int err = errno; int err = errno;
MXS_ERROR("Unable to process passwd file %s : " MXS_ERROR("Unable to process passwd file %s : "
"errno %d.\n" "errno %d.\n"
@ -312,7 +336,8 @@ char* admin_remove_user(
/** /**
* Replace original passwd file with new. * Replace original passwd file with new.
*/ */
if (rename(fname_tmp, fname)) { if (rename(fname_tmp, fname))
{
int err = errno; int err = errno;
MXS_ERROR("Unable to rename new passwd file %s : errno " MXS_ERROR("Unable to rename new passwd file %s : errno "
"%d.\n" "%d.\n"
@ -341,7 +366,9 @@ admin_search_user(char *user)
{ {
initialise(); initialise();
if (users == NULL) if (users == NULL)
{
return 0; return 0;
}
return users_fetch(users, user) != NULL; return users_fetch(users, user) != NULL;
} }
@ -354,7 +381,11 @@ void
dcb_PrintAdminUsers(DCB *dcb) dcb_PrintAdminUsers(DCB *dcb)
{ {
if (users) if (users)
{
dcb_usersPrint(dcb, users); dcb_usersPrint(dcb, users);
}
else else
{
dcb_printf(dcb, "No administration users have been defined.\n"); dcb_printf(dcb, "No administration users have been defined.\n");
} }
}