src/auth/pool_passwd.c | 99 +++++++++++++++++++++++++++++++----------- src/include/auth/pool_passwd.h | 1 + 2 files changed, 74 insertions(+), 26 deletions(-) diff --git a/src/auth/pool_passwd.c b/src/auth/pool_passwd.c index bbc33b9..1a15988 100644 --- a/src/auth/pool_passwd.c +++ b/src/auth/pool_passwd.c @@ -93,11 +93,17 @@ pool_init_pool_passwd(char *pool_passwd_filename, POOL_PASSWD_MODE mode) int pool_create_passwdent(char *username, char *passwd) { + char name[MAX_USER_NAME_LEN + 1]; + char password[MAX_POOL_PASSWD_LEN + 1]; +#define LINE_LEN \ + MAX_USER_NAME_LEN + 1 + MAX_POOL_PASSWD_LEN + 2 + char linebuf[LINE_LEN]; + char *writebuf = NULL; int len; int c; - char name[MAX_USER_NAME_LEN]; char *p; int readlen; + bool found = false; if (!passwd_fd) ereport(ERROR, @@ -117,9 +123,9 @@ pool_create_passwdent(char *username, char *passwd) while (!feof(passwd_fd)) { + /* read one line of pool_passwd file. */ p = name; readlen = 0; - while (readlen < sizeof(name)) { c = fgetc(passwd_fd); @@ -133,42 +139,83 @@ pool_create_passwdent(char *username, char *passwd) } *p = '\0'; - if (!strcmp(username, name)) + p = password; + readlen = 0; + while (readlen < sizeof(password)) + { + c = fgetc(passwd_fd); + if (c == EOF) + break; + else if (c == '\n') + break; + readlen++; + *p++ = c; + } + *p = '\0'; + + if (!found) { - /* User name found. Update password. */ - while ((c = *passwd++)) + /* User name found. */ + if (!strcmp(username, name)) { - fputc(c, passwd_fd); + /* Update pssword. */ + strncpy(password, passwd, sizeof(password)); + found = true; } - fputc('\n', passwd_fd); - return 0; } - else + + if (strlen(name) > 0 && strlen(password) > 0) { - /* Skip password */ - while ((c = fgetc(passwd_fd)) != EOF && - c != '\n') - ; + snprintf(linebuf, sizeof(linebuf), "%s:%s\n", name, password); + + if (writebuf == NULL) + { + writebuf = malloc(strlen(linebuf) + 1); + memset(writebuf, 0, strlen(linebuf) + 1); + } + else + writebuf = realloc(writebuf, strlen(linebuf) + strlen(writebuf) + 1); + if (!writebuf) + { + ereport(ERROR, + (errmsg("failed to allocate memory."))); + } + /* Add name and password pair to buffer. */ + strcat(writebuf, linebuf); } } - - fseek(passwd_fd, 0, SEEK_END); - - /* - * Not found the user name. Create a new entry. - */ - while ((c = *username++)) + if (!found) { - fputc(c, passwd_fd); + /* User name don't found. Append new user name and password. */ + snprintf(linebuf, sizeof(linebuf), "%s:%s\n", username, passwd); + if (writebuf == NULL) + { + writebuf = malloc(strlen(linebuf) + 1); + memset(writebuf, 0, strlen(linebuf) + 1); + } + else + writebuf = realloc(writebuf, strlen(linebuf) + strlen(writebuf) + 1); + + strcat(writebuf, linebuf); } - fputc(':', passwd_fd); - while ((c = *passwd++)) + + fclose(passwd_fd); + passwd_fd = fopen(saved_passwd_filename, "w+"); + if (!passwd_fd) { - fputc(c, passwd_fd); + ereport(ERROR, + (errmsg("pool_passwd reopen failed"))); } - fputc('\n', passwd_fd); + + /* write pool_passwd file. */ + fwrite(writebuf, 1, strlen(writebuf), passwd_fd); + pool_finish_pool_passwd(); + if(writebuf) + free(writebuf); return 0; + +#undef LINE_LEN } /* @@ -181,7 +228,7 @@ pool_get_passwd(char *username) { int c; char name[MAX_USER_NAME_LEN + 1]; - static char passwd[POOL_PASSWD_LEN + 1]; + static char passwd[MAX_POOL_PASSWD_LEN + 1]; char *p; int readlen; diff --git a/src/include/auth/pool_passwd.h b/src/include/auth/pool_passwd.h index c00cb96..bbcaaa8 100644 --- a/src/include/auth/pool_passwd.h +++ b/src/include/auth/pool_passwd.h @@ -29,6 +29,7 @@ #define POOL_PASSWD_FILENAME "pool_passwd" #define POOL_PASSWD_LEN 35 +#define MAX_POOL_PASSWD_LEN 132 /* In case of TEXT prefix(4byte) and plain text password(128byte)*/ #define MAX_USER_NAME_LEN 128 #define MAX_PGPASS_LEN 128