Skip to contentSkip to conversation

git-imap-send: use libcurl for implementation

PATCH/RFC Bernhard Reiter 4 participants 7 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

Use libcurl's high-level API functions to implement git-imap-send instead of the previous low-level OpenSSL-based functions.

---
Since version 7.30.0, libcurl's API has been able to communicate with IMAP servers. Using those high-level functions instead of the current ones reduces imap-send.c by some 1200 lines of code.

As I don't have access to that many IMAP servers, I haven't been able to test a variety of parameter combinations. I did test both secure and insecure (imaps:// and imap://) connections -- this e-mail was generated that way -- but could e.g. neither test the authMethod nor the tunnel parameter.

As git-imap-send is one of the two instances OpenSSL is currently used by git -- the other one being SHA1 -- it might be worthwhile considering dropping it altogether as there's already a SHA1 library built into git available as an alternative.

Kind regards
Bernhard
PS: Please CC!

 INSTALL     |   14 +-
 Makefile    |    4 +-
 git.spec.in |    5 +-
 imap-send.c | 1288
+++++------------------------------------------------------
 4 files changed, 111 insertions(+), 1200 deletions(-)
INSTALL +6 -8
index 6ec7a24..2cd3a42 100644
@@ -108,18 +108,16 @@ Issues of note:
 	  so you might need to install additional packages other than Perl
 	  itself, e.g. Time::HiRes.
 
-	- "openssl" library is used by git-imap-send to use IMAP over SSL.
-	  If you don't need it, use NO_OPENSSL.
-
-	  By default, git uses OpenSSL for SHA1 but it will use its own
+	- By default, git uses OpenSSL for SHA1 but it will use its own
 	  library (inspired by Mozilla's) with either NO_OPENSSL or
 	  BLK_SHA1.  Also included is a version optimized for PowerPC
 	  (PPC_SHA1).
 
-	- "libcurl" library is used by git-http-fetch and git-fetch.  You
-	  might also want the "curl" executable for debugging purposes.
-	  If you do not use http:// or https:// repositories, you do not
-	  have to have them (use NO_CURL).
+	- "libcurl" library is used by git-http-fetch, git-fetch, and
+	  git-impap-send.  You might also want the "curl" executable for
+	  debugging purposes. If you do not use http:// or https://
+	  repositories, and do not want to put patches into an IMAP
+	  mailbox, you do not have to have them (use NO_CURL).
 
 	- "expat" library; git-http-push uses it for remote lock
 	  management over DAV.  Similar to "curl" above, this is optional
Makefile +2 -2
index 2320de5..7805603 100644
@@ -2067,9 +2067,9 @@ endif
 git-%$X: %.o GIT-LDFLAGS $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
 
-git-imap-send$X: imap-send.o GIT-LDFLAGS $(GITLIBS)
+git-imap-send$X: imap-send.o http.o GIT-LDFLAGS $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
-		$(LIBS) $(OPENSSL_LINK) $(OPENSSL_LIBSSL) $(LIB_4_CRYPTO)
+		$(LIBS) $(CURL_LIBCURL)
 
 git-http-fetch$X: http.o http-walker.o http-fetch.o GIT-LDFLAGS $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
git.spec.in +4 -1
index d61d537..7d9230f 100644
@@ -8,7 +8,7 @@ License: 	GPL
 Group: 		Development/Tools
 URL: 		http://kernel.org/pub/software/scm/git/
 Source: 	http://kernel.org/pub/software/scm/git/%{name}-%{version}.tar.gz
-BuildRequires:	zlib-devel >= 1.2, openssl-devel, curl-devel, expat-devel, gettext  %{!?_without_docs:, xmlto, asciidoc > 6.0.3}
+BuildRequires:	zlib-devel >= 1.2, openssl-devel, curl-devel >= 7.30.0, expat-devel, gettext  %{!?_without_docs:, xmlto, asciidoc > 6.0.3}
 BuildRoot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
 
 Requires:	perl-Git = %{version}-%{release}
@@ -214,6 +214,9 @@ rm -rf $RPM_BUILD_ROOT
 # No files for you!
 
 %changelog
+* Mon Aug 11 2014 Bernhard Reiter <[email protected]>
+- Require version >= 7.30.0 of curl-devel for IMAP functions.
+
 * Sun Sep 18 2011 Jakub Narebski <[email protected]>
 - Add gitweb manpages to 'gitweb' subpackage
 
imap-send.c +99 -1189

1,339 lines not on this page · raw message

#1 lore raw
Quoted 2 lines · Bernhard Reiter

Use libcurl's high-level API functions to implement git-imap-send instead of the previous low-level OpenSSL-based functions.

Wow! This sounds lovely. Thanks for working on this.

[...]

Quoted 9 lines

Since version 7.30.0, libcurl's API has been able to communicate with IMAP servers. Using those high-level functions instead of the current ones reduces imap-send.c by some 1200 lines of code.

As I don't have access to that many IMAP servers, I haven't been able to test a variety of parameter combinations. I did test both secure and insecure (imaps:// and imap://) connections -- this e-mail was generated that way -- but could e.g. neither test the authMethod nor the tunnel parameter.

The above two paragraphs belong in the commit message, too, since they'll be just as useful for someone looking back through the history as for someone reading the patch on-list today.

[...]

Quoted 2 lines
INSTALL +0 -0

[...]

INSTALL Issues of note: · line 117

Quoted 3 lines
INSTALL +2 -1
-	- "libcurl" library is used by git-http-fetch and git-fetch.  You
+	- "libcurl" library is used by git-http-fetch, git-fetch, and
+	  git-impap-send.  You might also want the "curl" executable for

Typo: s/impap-send/imap-send/

Makefile endif · line 2072

Quoted 11 lines
Makefile +2 -2
@@ -2067,9 +2067,9 @@ endif
 git-%$X: %.o GIT-LDFLAGS $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
 
-git-imap-send$X: imap-send.o GIT-LDFLAGS $(GITLIBS)
+git-imap-send$X: imap-send.o http.o GIT-LDFLAGS $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
-		$(LIBS) $(OPENSSL_LINK) $(OPENSSL_LIBSSL) $(LIB_4_CRYPTO)
+		$(LIBS) $(CURL_LIBCURL)

7.30.0 is only ~1 year old. Does this mean users would need to update curl in order to build imap-send?

For example, Debian 7.6 ships curl 7.26.0 and CentOS 7 has curl 7.29.0.

Ideally this could be done as an optional feature:

1. Introduce a USE_CURL_FOR_IMAP_SEND makefile variable to take advantage of the nice new API.

2. (optional) Use the curl_check makefile variable to turn on USE_CURL_FOR_IMAP_SEND automatically when appropriate.

3. In a few years, when everyone has upgraded, we could simplify by getting rid of the USE_OPENSSL_FOR_IMAP_SEND code path.

What do you think?

[...]

imap-send.c line 25

Quoted 7 lines
imap-send.c +0 -1
@@ -22,47 +22,13 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "cache.h"

Usual style is to start with a #include of "cache.h" or "git-compat-util.h". "http.h" including cache.h for itself was an old mistake. (I'll reply with a patch to fix that.)

[...]

imap-send.c line 29

Quoted 1 line
imap-send.c +1 -0
+#include <curl/curl.h>

http.h already #includes this. Do you use other helpers from http.h/http.c or do you use libcurl directly? (Just curious.)

Some style nits:

[...]

imap-send.c static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs) · line 156

Quoted 2 lines
imap-send.c +2 -0
+static curl_socket_t opensocket(void *clientp, curlsocktype purpose,
+								struct curl_sockaddr *address)

Long line. Do you have ts=4? (Git uses 8-space tabs. There's some emacs magic in Documentation/CodingGuidelines. It should be possible to add similar hints there for other editors if they don't do the right thing by default.)

imap-send.c static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs) · line 160

Quoted 4 lines
imap-send.c +4 -0
+{
+	curl_socket_t sockfd;
+	(void)purpose;
+	(void)address;

Elsewhere git lets unused parameters be. The unused parameter warning is too noisy in callback-heavy code (e.g., for_each_ref) so we don't turn it on.

imap-send.c static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs) · line 163

Quoted 3 lines
imap-send.c +3 -0
+	sockfd = *(curl_socket_t *)clientp;
+	/* the actual externally set socket is passed in via the OPENSOCKETDATA
+	   option */

(style nit) Comments in git look like this:

	/*
	 * The actual, externally set socket is passed in via the
	 * OPENSOCKETDATA option.
	 */
	return sockfd;

[...]

imap-send.c static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs) · line 174

Quoted 5 lines
imap-send.c +5 -0
+static int sockopt_callback(void *clientp, curl_socket_t curlfd,
+							curlsocktype purpose)
+{
+	/* This return code was added in libcurl 7.21.5 */
+	return CURL_SOCKOPT_ALREADY_CONNECTED;

I'd drop the comment, unless there's some subtlety involved. (E.g., is there some other return code that would be more appropriate but was introudced later or something?)

[...]

imap-send.c static int git_imap_config(const char *key, const char *val, void *cb) · line 221

Quoted 6 lines
imap-send.c +1 -1
@@ -1368,12 +218,14 @@ static int git_imap_config(const char *key, const char *val, void *cb)
 int main(int argc, char **argv)
 {
 	struct strbuf all_msgs = STRBUF_INIT;
-	struct strbuf msg = STRBUF_INIT;
+	struct buffer msg = { STRBUF_INIT, 0 };

Ah, ok --- we do use http.c stuff.

[...]

imap-send.c static int git_imap_config(const char *key, const char *val, void *cb) · line 226

Quoted 2 lines
imap-send.c +2 -0
+	char path[8192];
+	int pathlen;

I realize the old code only had 8192 for the IMAP command buffer, but could this be a strbuf now, or is there some underlying limit somewhere else?

[...]

imap-send.c int main(int argc, char **argv) · line 272

Quoted 5 lines
imap-send.c +1 -0
@@ -1417,31 +269,89 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
+	curl_global_init(CURL_GLOBAL_ALL);

http.c seems to make the same mistake, but should the return value from this be checked?

imap-send.c int main(int argc, char **argv) · line 277

Quoted 9 lines
imap-send.c +4 -4
-	/* write it to the imap server */
-	ctx = imap_open_store(&server);
-	if (!ctx) {
-		fprintf(stderr, "failed to open store\n");
+	curl = curl_easy_init();
+
+	if (!curl) {
+		fprintf(stderr, "failed to init curl\n");
 		return 1;

Could do

		die("failed to init curl");

for more consistent message format and exit codes (128 for internal errors, with an error message starting with 'fatal: ').

[...]

imap-send.c int main(int argc, char **argv) · line 291

Quoted 9 lines
imap-send.c +9 -0
+	if (ends_with(server.host, "/"))
+		pathlen = snprintf (path, sizeof(path), "%s%s", server.host, imap_folder);
+	else
+		pathlen = snprintf (path, sizeof(path), "%s/%s", server.host, imap_folder);
+
+	if (pathlen < 0)
+		die("Fatal: Out of memory");
+	if (pathlen >= sizeof(path))
+		die("imap URL overflow!");

With a strbuf, this could be something like

	strbuf_addstr(&path, server.host);
	if (!path.len || path.buf[path.len - 1] != '/')
		strbuf_addch(&path, '/');
	strbuf_addstr(&path, imap_folder);

or

	if (ends_with(...))
		strbuf_addf(&path, "%s%s", ...);
	else
		strbuf_addf(...);

Killing the unused ctx->prefix handling is nice. :)

[...]

imap-send.c int main(int argc, char **argv) · line 299

Quoted 3 lines
imap-send.c +3 -0
+	if (server.tunnel) {
+		const char *argv[] = { server.tunnel, NULL };
+		struct child_process tunnel = {NULL};

(not about this patch) Could use the child_proccess's internal argv_array:

		struct child_process tunnel = {NULL};
		argv_array_push(&tunnel.args, server.tunnel);

(about this patch) Would there be a way to make this part reuse the existing code? The only difference I see is that *srvc has been renamed to server, which doesn't seem to be related to the change of transport API from OpenSSL to libcurl.

[...]

imap-send.c int main(int argc, char **argv) · line 310

Quoted 1 line
imap-send.c +1 -0
+		curl_socket_t sockfd = tunnel.out; // what about tunnel.in ?

Hmm. curl expects to get a socket it can send(), recv(), setsockopt(), etc on instead of a pair of fds to read() and write().

I wonder why someone would want to use SSL through a tunnel, though. Currently it's impossible to get to the SSL codepath when a tunnel is active (it's in the 'else' block an 'if (srvc->tunnel)'). If that property is preserved, then we should be safe.

To summarize:

* I like this idea a lot! Using libcurl's imaps:// support directly means one less dependency to worry about, and using alternate SSL libraries like gnutls or nss becomes much easier (e.g., see
http://fedoraproject.org/wiki/FedoraCryptoConsolidation for how that makes configuring certificate trust simpler).

* This would be easier to take if guarded by an #ifdef, so people stuck on ancient libcurl would still be able to use git (and ideally still use imap over SSL).

* This shouldn't have to touch the imap.tunnel support. imap-send's imap.tunnel configuration expects the tunnel to take care of securing the channel (e.g. by using 'openssl s_client').

* Any potential cleanups noticed along the way are very welcome, as separate patches.

* As soon as you're ready to roll this out to a wider audience of testers, let me know, and we can try to get it into shape for Junio's "next" branch (and hence Debian experimental).

Thanks and hope that helps,
Jonathan

#2 lore raw
Quoted 4 lines · Jonathan Nieder

imap-send.c int main(int argc, char **argv) · line 310

Quoted 1 line
imap-send.c +1 -0
+		curl_socket_t sockfd = tunnel.out; // what about tunnel.in ?

Hmm. curl expects to get a socket it can send(), recv(), setsockopt(), etc on instead of a pair of fds to read() and write().

I wonder if we could teach run_command to optionally use socketpair() instead of pipe(). I'm not sure if that would cause problems on Windows, though.

Quoted 4 lines

I wonder why someone would want to use SSL through a tunnel, though. Currently it's impossible to get to the SSL codepath when a tunnel is active (it's in the 'else' block an 'if (srvc->tunnel)'). If that property is preserved, then we should be safe.

I'm not sure I understand this comment. Even if SSL is not in use, wouldn't we be passing a regular pipe to curl, which would break?

-Peff

#3 lore raw

Am 2014-08-17 um 10:30 schrieb Jeff King:

Quoted 9 lines · Jonathan Nieder
Quoted 5 lines

imap-send.c int main(int argc, char **argv) · line 310

Quoted 2 lines
imap-send.c +1 -0
+		curl_socket_t sockfd = tunnel.out; // what about tunnel.in ?

Hmm. curl expects to get a socket it can send(), recv(), setsockopt(), etc on instead of a pair of fds to read() and write().

I wonder if we could teach run_command to optionally use socketpair() instead of pipe().

That sounds like a good idea to me.

Quoted 2 lines

I'm not sure if that would cause problems on Windows, though.

Apparently socketpair is not available there. Googling "socketpair windows" yields, among a lot of other useful resources, the following relatively actively maintained ~150 LOC, BSD-3-clause-licensed, implementation:

https://github.com/ncm/selectable-socketpair

That license is GPL compatible, so should we consider including that implementation with git? That's the kind of stuff that goes to compat/win32, right?

One thing to consider: seems like socketpair() gives AF_LOCAL sockets, so I've asked [1] on the curl ML if that would work or if libcurl needs an AF_INET one.

Quoted 7 lines
Quoted 4 lines

I wonder why someone would want to use SSL through a tunnel, though. Currently it's impossible to get to the SSL codepath when a tunnel is active (it's in the 'else' block an 'if (srvc->tunnel)'). If that property is preserved, then we should be safe.

I'm not sure I understand this comment. Even if SSL is not in use, wouldn't we be passing a regular pipe to curl, which would break?

Yeah, we can't do that, and thus would have to keep the handwritten IMAP implementation just for the tunnel case (allowing to drop only the OpenSSL specific stuff), see my other email:
http://www.mail-archive.com/[email protected]/msg56791.html (the relevant part is pretty far down at the bottom).

Bernhard

[1] http://curl.haxx.se/mail/lib-2014-08/0131.html

#4 lore raw
Quoted 8 lines · Bernhard Reiter
Quoted 2 lines

I'm not sure if that would cause problems on Windows, though.

Apparently socketpair is not available there. Googling "socketpair windows" yields, among a lot of other useful resources, the following relatively actively maintained ~150 LOC, BSD-3-clause-licensed, implementation:

Quoted 5 lines

https://github.com/ncm/selectable-socketpair

That license is GPL compatible, so should we consider including that implementation with git? That's the kind of stuff that goes to compat/win32, right?

Thanks for researching. Sticking that in compat/ would be our usual strategy, yes.

Quoted 8 lines
Quoted 2 lines

I'm not sure I understand this comment. Even if SSL is not in use, wouldn't we be passing a regular pipe to curl, which would break?

Yeah, we can't do that, and thus would have to keep the handwritten IMAP implementation just for the tunnel case (allowing to drop only the OpenSSL specific stuff), see my other email:
http://www.mail-archive.com/[email protected]/msg56791.html (the relevant part is pretty far down at the bottom).

I'd really love it if we could make this work with tunnels and eventually get rid of the hand-written imap code entirely. I agree with Jonathan that we probably need to keep it around a bit for people on older curl, but dropping it is a good goal in the long run. That code was forked from the isync project, but mangled enough that we could not take bug fixes from upstream. As not many people use imap-send, I suspect it is largely unmaintained and the source of many lurking bugs[1]. Replacing it with curl's maintained implementation is probably a good step.

-Peff

[1] That's my somewhat subjective opinion, but I feel like I have seen several bugs reported in imap-send that had literally been there for years. And having worked on IMAP implementations in a past life, I know there are many dark corners of the protocol that vary from server to server.

#5 lore raw

Am 2014-08-17 um 20:42 schrieb Jeff King:

Quoted 11 lines

[...]

Quoted 8 lines
Quoted 2 lines

I'm not sure I understand this comment. Even if SSL is not in use, wouldn't we be passing a regular pipe to curl, which would break?

Yeah, we can't do that, and thus would have to keep the handwritten IMAP implementation just for the tunnel case (allowing to drop only the OpenSSL specific stuff), see my other email:
http://www.mail-archive.com/[email protected]/msg56791.html (the relevant part is pretty far down at the bottom).

Quoted 9 lines

I'd really love it if we could make this work with tunnels and eventually get rid of the hand-written imap code entirely. I agree with Jonathan that we probably need to keep it around a bit for people on older curl, but dropping it is a good goal in the long run. That code was forked from the isync project, but mangled enough that we could not take bug fixes from upstream. As not many people use imap-send, I suspect it is largely unmaintained and the source of many lurking bugs[1]. Replacing it with curl's maintained implementation is probably a good step.

I'll work on this as soon as I find some time, but as that will include changes to run-command.c (and possibly other files?), I'd like to cover that in a commit of its own. Do you guys think the current patch [1] is good enough for "official" submission already? If so, do I need some sort of official review? Documentation/SubmittingPatches says I'm only supposed to direct it to Junio after the list "reaches consensus", so I'm wondering how to get there... :-)

Bernhard

#6 lore raw
Quoted 21 lines · Bernhard Reiter

Am 2014-08-17 um 20:42 schrieb Jeff King:

Quoted 20 lines

[...]

Quoted 8 lines
Quoted 2 lines

I'm not sure I understand this comment. Even if SSL is not in use, wouldn't we be passing a regular pipe to curl, which would break?

Yeah, we can't do that, and thus would have to keep the handwritten IMAP implementation just for the tunnel case (allowing to drop only the OpenSSL specific stuff), see my other email:
http://www.mail-archive.com/[email protected]/msg56791.html (the relevant part is pretty far down at the bottom).

I'd really love it if we could make this work with tunnels and eventually get rid of the hand-written imap code entirely. I agree with Jonathan that we probably need to keep it around a bit for people on older curl, but dropping it is a good goal in the long run. That code was forked from the isync project, but mangled enough that we could not take bug fixes from upstream. As not many people use imap-send, I suspect it is largely unmaintained and the source of many lurking bugs[1]. Replacing it with curl's maintained implementation is probably a good step.

I would agree with s/a good step/a good goal/ ;-)

Quoted 4 lines

I'll work on this as soon as I find some time, but as that will include changes to run-command.c (and possibly other files?), I'd like to cover that in a commit of its own. Do you guys think the current patch [1] is good enough for "official" submission already?

My impression from reading the discussion in this thread has been that the patch that started this thread would break the tunneling code, i.e. is not there yet. Or did you mean some other patch?

The other patch $gmane/255403 from you looked good and I think I already have a copy queued on 'pu' as f9dc5d65 (git-imap-send:
simplify tunnel construction, 2014-08-13).

Thanks.

[References]

*$gmane/255403*

    http://thread.gmane.org/gmane.comp.version-control.git/255220/focus=255403