All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] imap-send: escape backslash in password
@ 2017-08-04 16:16 Nicolas Morey-Chaisemartin
  2017-08-04 19:09 ` Junio C Hamano
  2017-08-04 20:06 ` brian m. carlson
  0 siblings, 2 replies; 15+ messages in thread
From: Nicolas Morey-Chaisemartin @ 2017-08-04 16:16 UTC (permalink / raw)
  To: git

Password containing backslashes need to have them doubled to have them properly interpreted by the imap server.

A password terminating with a blackslash used to trigger this error:
IMAP command 'LOGIN <user> <pass>' returned response (BAD) - Missing '"'

Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com>
---
 imap-send.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/imap-send.c b/imap-send.c
index b2d0b849b..7fde6ec96 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -926,6 +926,32 @@ static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const cha
 	return 0;
 }
 
+static char* imap_escape_password(const char *passwd)
+{
+	const unsigned passwd_len = strlen(passwd);
+	char *escaped = xmalloc(2 * passwd_len + 1);
+	const char *passwd_cur = passwd;
+	char *escaped_cur = escaped;
+
+	do {
+		char *next = strchr(passwd_cur, '\\');
+
+		if (!next) {
+			strcpy(escaped_cur, passwd_cur);
+		} else {
+			int len = next - passwd_cur + 1;
+
+			memcpy(escaped_cur, passwd_cur, len);
+			escaped_cur += len;
+			next++;
+			*(escaped_cur++) = '\\';
+		}
+		passwd_cur = next;
+	} while(passwd_cur);
+
+	return escaped;
+}
+
 static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *folder)
 {
 	struct credential cred = CREDENTIAL_INIT;
@@ -1090,7 +1116,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, char *f
 			if (!srvc->user)
 				srvc->user = xstrdup(cred.username);
 			if (!srvc->pass)
-				srvc->pass = xstrdup(cred.password);
+				srvc->pass = imap_escape_password(cred.password);
 		}
 
 		if (srvc->auth_method) {

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2017-08-09 12:04 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-04 16:16 [RFC] imap-send: escape backslash in password Nicolas Morey-Chaisemartin
2017-08-04 19:09 ` Junio C Hamano
2017-08-04 19:32   ` Nicolas Morey-Chaisemartin
2017-08-04 19:46   ` Andreas Schwab
2017-08-04 20:22     ` Jeff King
2017-08-04 21:18       ` Junio C Hamano
2017-08-04 21:22         ` Jeff King
2017-08-06 19:12           ` Nicolas Morey-Chaisemartin
2017-08-07 20:58             ` Jeff King
2017-08-07  1:34           ` Junio C Hamano
2017-08-08  7:25             ` Jeff King
2017-08-08 16:54               ` Junio C Hamano
2017-08-09 12:04                 ` Jeff King
2017-08-04 20:06 ` brian m. carlson
2017-08-04 20:18   ` Jeff King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.