{"thread":{"id":"22907","subject":"[PATCH v5 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","startedAt":"2010-03-04T14:36:04Z","lastAt":"2010-03-05T02:31:18Z","messageCount":4,"participants":["Frank Li","Junio C Hamano","Johannes Sixt"],"isPatch":true,"patchVersion":5,"patchTotal":3},"messages":[{"id":"136152","messageId":"1267713364-2976-1-git-send-email-lznuaa@gmail.com","threadId":"22907","inReplyTo":null,"subject":"[PATCH v5 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","fromName":"Frank Li","fromEmail":"lznuaa@gmail.com","sentAt":"2010-03-04T14:36:04Z","receivedAt":"2010-03-04T14:36:04Z","isPatch":true,"sender":{"key":"lznuaa@gmail.com","avatar":"https://avatars.githubusercontent.com/u/40642?v=4"},"body":"imap-send and authority http connect reads passwords from an interactive\nterminal. This behavious cause GUIs to hang waiting for git complete.\n\nFix this problem by allowing a password-retrieving command\nto be specified in GIT_ASKPASS\n\nSigned-off-by: Frank Li <lznuaa@gmail.com>\n---\n cache.h     |    1 +\n connect.c   |   45 +++++++++++++++++++++++++++++++++++++++++++++\n http.c      |    4 ++--\n imap-send.c |    2 +-\n 4 files changed, 49 insertions(+), 3 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex d478eff..a25d269 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -877,6 +877,7 @@ struct ref {\n extern struct ref *find_ref_by_name(const struct ref *list, const char *name);\n \n #define CONNECT_VERBOSE       (1u << 0)\n+extern char *git_getpass(const char *prompt);\n extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);\n extern int finish_connect(struct child_process *conn);\n extern int path_match(const char *path, int nr, char **match);\ndiff --git a/connect.c b/connect.c\nindex a37cf6a..57d549c 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -647,3 +647,48 @@ int finish_connect(struct child_process *conn)\n \tfree(conn);\n \treturn code;\n }\n+\n+char *git_getpass(const char *prompt)\n+{\n+\tchar *askpass;\n+\tstruct child_process pass;\n+\tconst char *args[3];\n+\tstruct strbuf buffer = STRBUF_INIT;\n+\tstatic char *pd;\n+\n+\taskpass = getenv(\"GIT_ASKPASS\");\n+\n+\tif (!askpass || !(*askpass))\n+\t\treturn getpass(prompt);\n+\n+\targs[0] = askpass;\n+\targs[1]\t= prompt;\n+\targs[2] = NULL;\n+\n+\tmemset(&pass, 0, sizeof(pass));\n+\tpass.argv = args;\n+\tpass.out = -1;\n+\n+\tif (start_command(&pass))\n+\t\texit(1);\n+\n+\tif (strbuf_read(&buffer, pass.out, 20) < 0)\n+\t\tdie(\"fail get password from %s\\n\", askpass);\n+\n+\tclose(pass.out);\n+\n+\tif (finish_command(&pass))\n+\t\texit(1);\n+\n+\tstrbuf_setlen(&buffer, strcspn(buffer.buf, \"\\r\\n\"));\n+\n+\tif (pd) {\n+\t\tfree(pd);\n+\t\tpd = NULL;\n+\t}\n+\t/*it maybe memory leak because getpass return a static buffer*/\n+\t/*Memory will be free at next call*/\n+\tpd = strbuf_detach(&buffer, NULL);\n+\treturn pd;\n+}\n+\ndiff --git a/http.c b/http.c\nindex deab595..4814217 100644\n--- a/http.c\n+++ b/http.c\n@@ -204,7 +204,7 @@ static void init_curl_http_auth(CURL *result)\n \tif (user_name) {\n \t\tstruct strbuf up = STRBUF_INIT;\n \t\tif (!user_pass)\n-\t\t\tuser_pass = xstrdup(getpass(\"Password: \"));\n+\t\t\tuser_pass = xstrdup(git_getpass(\"Password: \"));\n \t\tstrbuf_addf(&up, \"%s:%s\", user_name, user_pass);\n \t\tcurl_easy_setopt(result, CURLOPT_USERPWD,\n \t\t\t\t strbuf_detach(&up, NULL));\n@@ -219,7 +219,7 @@ static int has_cert_password(void)\n \t\treturn 0;\n \t/* Only prompt the user once. */\n \tssl_cert_password_required = -1;\n-\tssl_cert_password = getpass(\"Certificate Password: \");\n+\tssl_cert_password = git_getpass(\"Certificate Password: \");\n \tif (ssl_cert_password != NULL) {\n \t\tssl_cert_password = xstrdup(ssl_cert_password);\n \t\treturn 1;\ndiff --git a/imap-send.c b/imap-send.c\nindex 5631930..5254b2a 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -1107,7 +1107,7 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)\n \t\tif (!srvc->pass) {\n \t\t\tchar prompt[80];\n \t\t\tsprintf(prompt, \"Password (%s@%s): \", srvc->user, srvc->host);\n-\t\t\targ = getpass(prompt);\n+\t\t\targ = git_getpass(prompt);\n \t\t\tif (!arg) {\n \t\t\t\tperror(\"getpass\");\n \t\t\t\texit(1);\n-- \n1.7.0.86.g84fa0\n"},{"id":"136162","messageId":"7vd3zk6j01.fsf@alter.siamese.dyndns.org","threadId":"22907","inReplyTo":"1267713364-2976-1-git-send-email-lznuaa@gmail.com","subject":"Re: [PATCH v5 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-03-04T18:26:22Z","receivedAt":"2010-03-04T18:26:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Frank Li <lznuaa@gmail.com> writes:\n\n> imap-send and authority http connect reads passwords from an interactive\n> terminal. This behavious cause GUIs to hang waiting for git complete.\n\n\"authority http\"?  Perhaps...\n\n    git tries to read a password from the terminal in imap-send and\n    talking to http server that requires authentication.\n\n    When a GUI is driving git, however, the end user is not paying\n    attention to the terminal (there may not even be a terminal).  GUI\n    would appear to hang forever.\n\n> Fix this problem by allowing a password-retrieving command\n> to be specified in GIT_ASKPASS\n>\n> Signed-off-by: Frank Li <lznuaa@gmail.com>\n> ---\n\nHere after three-dash lines, could you describe the changes (what changed\nand why) since the last round, when sending a revised version?  Saying\n\"This round addressed such and such issues raised and I think it is ready\nfor inclusion\" (when you think it is ready) would also help.\n\n> +\tif (start_command(&pass))\n> +\t\texit(1);\n> +\n> +\tif (strbuf_read(&buffer, pass.out, 20) < 0)\n> +\t\tdie(\"fail get password from %s\\n\", askpass);\n\n\"failed to get...\"\n\n> +\tclose(pass.out);\n> +\n> +\tif (finish_command(&pass))\n> +\t\texit(1);\n> +\n> +\tstrbuf_setlen(&buffer, strcspn(buffer.buf, \"\\r\\n\"));\n> +\n> +\tif (pd) {\n> +\t\tfree(pd);\n> +\t\tpd = NULL;\n> +\t}\n> +\t/*it maybe memory leak because getpass return a static buffer*/\n\nHmph, why not do this at the beginning?  Is the \"maybe leak\" comment still\nvalid?\n"},{"id":"136167","messageId":"201003042059.51516.j6t@kdbg.org","threadId":"22907","inReplyTo":"7vd3zk6j01.fsf@alter.siamese.dyndns.org","subject":"[PATCH v5-j6t 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2010-03-04T19:59:50Z","receivedAt":"2010-03-04T19:59:50Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"From: Frank Li <lznuaa@gmail.com>\n\ngit tries to read a password from the terminal in imap-send and\nwhen talking to a http server that requires authentication.\n\nWhen a GUI is driving git, however, the end user is not paying\nattention to the terminal (there may not even be a terminal).\nGUI would appear to hang forever.\n\nFix this problem by allowing a password-retrieving command\nto be specified in GIT_ASKPASS\n\nSigned-off-by: Frank Li <lznuaa@gmail.com>\nSigned-off-by: Johannes Sixt <j6t@kdbg.org>\n---\n Here is a cleaned-up version.\n\n I changed the code to make struct strbuf buffer itself static and fixed\n the error message. I removed the comment about the memory leak again\n because now the buffer is conceptually static even though behind the\n scenes it points to allocated memory.\n\n Frank, I did not test it beyond the test suite. Please test and say\n no or go.\n\n -- Hannes\n\n cache.h     |    1 +\n connect.c   |   37 +++++++++++++++++++++++++++++++++++++\n http.c      |    4 ++--\n imap-send.c |    2 +-\n 4 files changed, 41 insertions(+), 3 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex d454b7e..06f3b73 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -877,6 +877,7 @@ struct ref {\n extern struct ref *find_ref_by_name(const struct ref *list, const char *name);\n \n #define CONNECT_VERBOSE       (1u << 0)\n+extern char *git_getpass(const char *prompt);\n extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);\n extern int finish_connect(struct child_process *conn);\n extern int path_match(const char *path, int nr, char **match);\ndiff --git a/connect.c b/connect.c\nindex 4748caf..5e9d29c 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -624,3 +624,40 @@ int finish_connect(struct child_process *conn)\n \tfree(conn);\n \treturn code;\n }\n+\n+char *git_getpass(const char *prompt)\n+{\n+\tchar *askpass;\n+\tstruct child_process pass;\n+\tconst char *args[3];\n+\tstatic struct strbuf buffer = STRBUF_INIT;\n+\n+\taskpass = getenv(\"GIT_ASKPASS\");\n+\n+\tif (!askpass || !(*askpass))\n+\t\treturn getpass(prompt);\n+\n+\targs[0] = askpass;\n+\targs[1]\t= prompt;\n+\targs[2] = NULL;\n+\n+\tmemset(&pass, 0, sizeof(pass));\n+\tpass.argv = args;\n+\tpass.out = -1;\n+\n+\tif (start_command(&pass))\n+\t\texit(1);\n+\n+\tstrbuf_reset(&buffer);\n+\tif (strbuf_read(&buffer, pass.out, 20) < 0)\n+\t\tdie(\"failed to read password from %s\\n\", askpass);\n+\n+\tclose(pass.out);\n+\n+\tif (finish_command(&pass))\n+\t\texit(1);\n+\n+\tstrbuf_setlen(&buffer, strcspn(buffer.buf, \"\\r\\n\"));\n+\n+\treturn buffer.buf;\n+}\ndiff --git a/http.c b/http.c\nindex deab595..4814217 100644\n--- a/http.c\n+++ b/http.c\n@@ -204,7 +204,7 @@ static void init_curl_http_auth(CURL *result)\n \tif (user_name) {\n \t\tstruct strbuf up = STRBUF_INIT;\n \t\tif (!user_pass)\n-\t\t\tuser_pass = xstrdup(getpass(\"Password: \"));\n+\t\t\tuser_pass = xstrdup(git_getpass(\"Password: \"));\n \t\tstrbuf_addf(&up, \"%s:%s\", user_name, user_pass);\n \t\tcurl_easy_setopt(result, CURLOPT_USERPWD,\n \t\t\t\t strbuf_detach(&up, NULL));\n@@ -219,7 +219,7 @@ static int has_cert_password(void)\n \t\treturn 0;\n \t/* Only prompt the user once. */\n \tssl_cert_password_required = -1;\n-\tssl_cert_password = getpass(\"Certificate Password: \");\n+\tssl_cert_password = git_getpass(\"Certificate Password: \");\n \tif (ssl_cert_password != NULL) {\n \t\tssl_cert_password = xstrdup(ssl_cert_password);\n \t\treturn 1;\ndiff --git a/imap-send.c b/imap-send.c\nindex fa70383..aeb2985 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -1207,7 +1207,7 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)\n \t\tif (!srvc->pass) {\n \t\t\tchar prompt[80];\n \t\t\tsprintf(prompt, \"Password (%s@%s): \", srvc->user, srvc->host);\n-\t\t\targ = getpass(prompt);\n+\t\t\targ = git_getpass(prompt);\n \t\t\tif (!arg) {\n \t\t\t\tperror(\"getpass\");\n \t\t\t\texit(1);\n-- \n1.7.0.rc2.65.g7b13a\n"},{"id":"136179","messageId":"1976ea661003041831n2573bda2xd0f528e03a1444de@mail.gmail.com","threadId":"22907","inReplyTo":"201003042059.51516.j6t@kdbg.org","subject":"Re: [PATCH v5-j6t 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","fromName":"Frank Li","fromEmail":"lznuaa@gmail.com","sentAt":"2010-03-05T02:31:18Z","receivedAt":"2010-03-05T02:31:18Z","isPatch":true,"sender":{"key":"lznuaa@gmail.com","avatar":"https://avatars.githubusercontent.com/u/40642?v=4"},"body":">\n>  I changed the code to make struct strbuf buffer itself static and fixed\n>  the error message. I removed the comment about the memory leak again\n>  because now the buffer is conceptually static even though behind the\n>  scenes it points to allocated memory.\n>\n>  Frank, I did not test it beyond the test suite. Please test and say\n>  no or go.\n>\n\n        Thanks.\n        I test it. It work rightly with prompted password\nappliction(ssh-askpass).\n\n\nbest regards\nFrank Li\n"}]}