pgpool2.git / summary / log / commit / refs

commit    68d66a66f55099d7ef2fdf0e7fb48ba6577e98c3
Author:   Bo Peng <pengbo@sraoss.co.jp>
Date:     Sun Apr 05 18:28:15 2020 +0000

    Feature: Add support for an user/password input file to pg_md5.
    
    Allow to use an input file containing multiple username/password pairs.
    
    Patch created by Umar Hayat, and Japanese document added by Bo Peng.
    Discussion: https://www.pgpool.net/pipermail/pgpool-hackers/2020-March/003527.html


doc.ja/src/sgml/ref/pg_md5.sgml | 25 +++++++++ doc/src/sgml/ref/pg_md5.sgml | 28 ++++++++++ src/tools/pgmd5/pg_md5.c | 111 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 162 insertions(+), 2 deletions(-) diff --git a/doc.ja/src/sgml/ref/pg_md5.sgml b/doc.ja/src/sgml/ref/pg_md5.sgml index 5e5d66116..4f2bc31db 100644 --- a/doc.ja/src/sgml/ref/pg_md5.sgml +++ b/doc.ja/src/sgml/ref/pg_md5.sgml @@ -110,6 +110,16 @@ Pgpool-II documentation </listitem> </varlistentry> + <varlistentry> + <term><option>-i <replaceable class="parameter">INPUT_FILE</replaceable></option></term> + <term><option>--input-file=<replaceable class="parameter">INPUT_FILE</replaceable></option></term> + <listitem> + <para> + ユーザ名とパスワードのペアを含むファイルを指定します。 + </para> + </listitem> + </varlistentry> + </variablelist> </para> </refsect1> @@ -153,6 +163,21 @@ Pgpool-II documentation username:md55a231fcdb710d73268c4f44283487ba2 </programlisting> + パスワードの入力が促されたりコマンド引数の中でパスワードを指定するのを避けたい場合は<literal>pg_md5</literal>は名前:パスワードのペアをファイルから読むことができます。 + すべてのユーザ名と暗号化されたパスワードが<xref linkend="guc-pool-passwd">認証ファイルに追加されます。 + <programlisting> + $ cat users.txt + username1:secretpassword1 + username2:secretpassword1 + + $ pg_md5 -m -f /path/to/pgpool.conf -i users.txt + trying to read username:password pairs from file users.txt + + $ cat /path/to/pool_passwd + username1:md533314126ba0b187df1e37f5ce6a489a8 + username2:md58ae92c6e1d6a48d80e2583fe715e2b36 + </programlisting> + <xref linkend="guc-pool-passwd"> にエントリを登録せず、md5ハッシュされた文字列だけを表示させるには、引数としてパスワードとユーザ名を連結した文字列を渡します。 例として、パスワードに"password"、ユーザ名に"user"を渡す例を示します。 <programlisting> diff --git a/doc/src/sgml/ref/pg_md5.sgml b/doc/src/sgml/ref/pg_md5.sgml index b02c24f9f..ef8eac2b0 100644 --- a/doc/src/sgml/ref/pg_md5.sgml +++ b/doc/src/sgml/ref/pg_md5.sgml @@ -86,6 +86,16 @@ Pgpool-II documentation </listitem> </varlistentry> + <varlistentry> + <term><option>-i <replaceable class="parameter">input_file</replaceable></option></term> + <term><option>--input-file=<replaceable class="parameter">input_file</replaceable></option></term> + <listitem> + <para> + Specifies file containing user name and password pairs. + </para> + </listitem> + </varlistentry> + </variablelist> </para> </refsect1> @@ -118,6 +128,24 @@ Pgpool-II documentation $ cat /path/to/pool_passwd username:md55a231fcdb710d73268c4f44283487ba2 </programlisting> + + To avoid password prompt or password in command parameter, + <literal>pg_md5</literal> can read user name:password pairs from file. + It will add all user names and <literal>md5</literal> hashed password to + <xref linkend="guc-pool-passwd"> authentication file. + <programlisting> + $ cat users.txt + username1:secretpassword1 + username2:secretpassword1 + + $ pg_md5 -m -f /path/to/pgpool.conf -i users.txt + trying to read username:password pairs from file users.txt + + $ cat /path/to/pool_passwd + username1:md533314126ba0b187df1e37f5ce6a489a8 + username2:md58ae92c6e1d6a48d80e2583fe715e2b36 + </programlisting> + To just display the md5 hashed string, not adding an entry to <xref linkend="guc-pool-passwd">, pass a string concatenating password and user name. For example, if password is "password" and user name is "user", the output would be: <programlisting> diff --git a/src/tools/pgmd5/pg_md5.c b/src/tools/pgmd5/pg_md5.c index cf37e890a..2fe3f03ea 100644 --- a/src/tools/pgmd5/pg_md5.c +++ b/src/tools/pgmd5/pg_md5.c @@ -39,13 +39,16 @@ #endif #include <pwd.h> #include <libgen.h> +#include <sys/stat.h> /* Maximum number of characters allowed for input. */ #define MAX_INPUT_SIZE MAX_USER_NAME_LEN +#define MAX_BUFFER_SIZE MAX_INPUT_SIZE + 1 + MAX_INPUT_SIZE static void print_usage(const char prog[], int exit_code); static void set_tio_attr(int enable); static void update_pool_passwd(char *conf_file, char *username, char *password); +static void update_pool_passwd_from_file(char *conf_file, char *input_file, bool md5auth); int main(int argc, char *argv[]) @@ -53,11 +56,13 @@ main(int argc, char *argv[]) #define PRINT_USAGE(exit_code) print_usage(argv[0], exit_code) char conf_file[POOLMAXPATHLEN + 1]; + char input_file[POOLMAXPATHLEN + 1]; char username[MAX_INPUT_SIZE + 1]; int opt; int optindex; bool md5auth = false; bool prompt = false; + bool use_input_file = false; static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, @@ -65,6 +70,7 @@ main(int argc, char *argv[]) {"md5auth", no_argument, NULL, 'm'}, {"username", required_argument, NULL, 'u'}, {"config-file", required_argument, NULL, 'f'}, + {"input-file", required_argument, NULL, 'i'}, {NULL, 0, NULL, 0} }; @@ -76,7 +82,7 @@ main(int argc, char *argv[]) */ memset(username, 0, MAX_INPUT_SIZE + 1); - while ((opt = getopt_long(argc, argv, "hpmf:u:", long_options, &optindex)) != -1) + while ((opt = getopt_long(argc, argv, "hpmf:i:u:", long_options, &optindex)) != -1) { switch (opt) { @@ -96,6 +102,15 @@ main(int argc, char *argv[]) strlcpy(conf_file, optarg, sizeof(conf_file)); break; + case 'i': /* specify users password input file */ + if (!optarg) + { + PRINT_USAGE(EXIT_SUCCESS); + } + strlcpy(input_file, optarg, sizeof(input_file)); + use_input_file = true; + break; + case 'u': if (!optarg) { @@ -115,6 +130,12 @@ main(int argc, char *argv[]) break; } } + /* Use input file */ + if (use_input_file) + { + update_pool_passwd_from_file(conf_file, input_file, md5auth); + return EXIT_SUCCESS; + } /* Prompt for password. */ if (prompt) @@ -189,6 +210,91 @@ main(int argc, char *argv[]) return EXIT_SUCCESS; } +static void +update_pool_passwd_from_file(char *conf_file, char *input_file, bool md5auth) +{ + FILE *fp; + struct stat stat_buf; + + char buf[MAX_BUFFER_SIZE + 1]; + char username[MAX_INPUT_SIZE + 1]; + char password[MAX_INPUT_SIZE + 1]; + char md5[MD5_PASSWD_LEN + 1]; + char *pch; + int len; + int line_count; + + + fprintf(stdout, "trying to read username:password pairs from file %s\n", input_file); + fp = fopen(input_file, "r"); + if (fp == NULL) + { + fprintf(stderr, "failed to open input_file \"%s\" (%m)\n\n", input_file); + exit(EXIT_FAILURE); + } + + line_count = 0; + while (!feof(fp) && !ferror(fp)) + { + line_count++; + + memset(buf, 0, MAX_BUFFER_SIZE + 1); + memset(username, 0, MAX_INPUT_SIZE + 1); + memset(password, 0, MAX_INPUT_SIZE + 1); + + if (fgets(buf, sizeof(buf), fp) == NULL) + break; + fprintf(stdout, "LINE#%02d: ", line_count); + len = strlen(buf); + if (len == 0 || *buf == '\n') + { + fprintf(stdout, "BLANK LINE\n"); + continue; + } + + /* Remove trailing newline */ + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + + /* Split username and passwords */ + pch = buf; + while( pch && pch != buf + len && *pch != ':') + pch++; + if (*pch == ':') + pch++; + + if (!strlen(pch)) + { + fprintf(stdout, "Invalid username:password pair\n"); + continue; + } + + if( (pch-buf) > sizeof(username)) + { + fprintf(stdout, "input exceeds maximum username length %d\n\n", MAX_USER_NAME_LEN); + continue; + } + strncpy(username, buf, pch-buf-1); + + if (strlen(pch) >= sizeof(password)) + { + fprintf(stdout, "input exceeds maximum password length %d\n", MAX_PGPASS_LEN); + continue; + } + strncpy(password, pch, strlen(pch)); + + fprintf(stdout, "USER: <%s>\n", username); + if(md5auth) + update_pool_passwd(conf_file, username, password); + else + { + pool_md5_hash(password, strlen(password), md5); + printf("%s\n", md5); + } + } + fclose(fp); +} + static void update_pool_passwd(char *conf_file, char *username, char *password) { @@ -250,12 +356,13 @@ print_usage(const char prog[], int exit_code) --username, -u USER When producing a md5 authentication password,\n\ create the pool_passwd entry for USER.\n\ --config-file, -f CONFIG-FILE Specify pgpool.conf.\n\ + --input-file, -i INPUT-FILE Specify file containing username and password pairs.\n\ --help, -h This help menu.\n\ \n\ Warning: At most %d characters are allowed for input.\n\ Warning: Plain password argument is deprecated for security concerns\n\ and kept for compatibility. Please prefer using password\n\ - prompt.\n", + prompt or input file method.\n", prog, prog, MAX_INPUT_SIZE); exit(exit_code); [parent: c810e252baa5]