{"thread":{"id":"22891","subject":"[PATCH v4 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","startedAt":"2010-03-03T11:36:48Z","lastAt":"2010-03-03T19:56:20Z","messageCount":2,"participants":["Frank Li","Junio C Hamano"],"isPatch":true,"patchVersion":4,"patchTotal":3},"messages":[{"id":"136068","messageId":"1267616208-3844-1-git-send-email-lznuaa@gmail.com","threadId":"22891","inReplyTo":null,"subject":"[PATCH v4 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","fromName":"Frank Li","fromEmail":"lznuaa@gmail.com","sentAt":"2010-03-03T11:36:48Z","receivedAt":"2010-03-03T11:36:48Z","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   |   39 +++++++++++++++++++++++++++++++++++++++\n http.c      |    4 ++--\n imap-send.c |    2 +-\n 4 files changed, 43 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..8d45ab9 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -647,3 +647,42 @@ 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+\tint i = 0;\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\texit(1);\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+\t/*it maybe memory leak because getpass return a static buffer*/\n+\treturn strbuf_detach(&buffer, NULL);\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.g013a4.dirty\n"},{"id":"136088","messageId":"7vd3zlqivv.fsf@alter.siamese.dyndns.org","threadId":"22891","inReplyTo":"1267616208-3844-1-git-send-email-lznuaa@gmail.com","subject":"Re: [PATCH v4 2/3] git-core: Support retrieving passwords with GIT_ASKPASS","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-03-03T19:56:20Z","receivedAt":"2010-03-03T19:56:20Z","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> 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\nIt appears that between v3 and v4 you incorporated all the changes J6t\nsuggested.  I would have preferred to see you Cc him, so that he can\nsimply respond with \"Acked-by:\" (or another round of review cycle).\n\n> +char *git_getpass(const char *prompt)\n> +{\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\nI understand that you exit silently here because start-command would have\ngiven diagnostic messages already.\n\n> +\tif (strbuf_read(&buffer, pass.out, 20) < 0)\n> +\t\texit(1);\n\nWhat about this one?  Running out of memory inside strbuf_grow() may have\ndied with message but other error conditions that do give control back to\nthe caller seem pretty quiet to my cursory look...\n\n> +\tclose(pass.out);\n> +\n> +\tif (finish_command(&pass))\n> +\t\texit(1);\n\nThis silent exit is correct, I think; wait_or_whine() called by\nfinish_command() would have reported errors.\n\n> +\tstrbuf_setlen(&buffer, strcspn(buffer.buf, \"\\r\\n\"));\n> +\n> +\t/*it maybe memory leak because getpass return a static buffer*/\n> +\treturn strbuf_detach(&buffer, NULL);\n\nPerhaps keep this value in a \"static char *\" in the function and free it\nupon entry on the second call?\n"}]}