git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Philipp A. Hartmann" <pah@qo.cx>
To: git@vger.kernel.org
Cc: Jeff King <peff@peff.net>,
	John Szakmeister <john@szakmeister.net>,
	"Philipp A. Hartmann" <pah@qo.cx>
Subject: [PATCH 3/4] gnome-keyring: port to generic helper implementation
Date: Thu, 23 Aug 2012 18:57:47 +0200	[thread overview]
Message-ID: <1345741068-11004-4-git-send-email-pah@qo.cx> (raw)
In-Reply-To: <1345741068-11004-1-git-send-email-pah@qo.cx>

From: "Philipp A. Hartmann" <pah@qo.cx>

Use generic credential helper implementation in the
GnomeKeyring credential helper.

The GnomeKeyring helper has been using the generic implementation
internally already and therefore only drops the duplicate code.

Signed-off-by: Philipp A. Hartmann <pah@qo.cx>
---
 contrib/credential/gnome-keyring/Makefile          |    6 +-
 .../gnome-keyring/git-credential-gnome-keyring.c   |  243 +-------------------
 2 files changed, 6 insertions(+), 243 deletions(-)

diff --git a/contrib/credential/gnome-keyring/Makefile b/contrib/credential/gnome-keyring/Makefile
index e6561d8..7f3ec11 100644
--- a/contrib/credential/gnome-keyring/Makefile
+++ b/contrib/credential/gnome-keyring/Makefile
@@ -11,11 +11,15 @@ CFLAGS = -g -O2 -Wall
 INCS:=$(shell pkg-config --cflags gnome-keyring-1)
 LIBS:=$(shell pkg-config --libs gnome-keyring-1)
 
+HELPER:=../helper
+VPATH +=$(HELPER)
+
 SRCS:=$(MAIN).c
+SRCS+=credential_helper.c
 OBJS:=$(SRCS:.c=.o)
 
 %.o: %.c
-	$(CC) $(CFLAGS) $(CPPFLAGS) $(INCS) -o $@ -c $<
+	$(CC) $(CFLAGS) $(CPPFLAGS) -I$(HELPER) $(INCS) -o $@ -c $<
 
 $(MAIN): $(OBJS)
 	$(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
diff --git a/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c b/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c
index 41f61c5..00244aa 100644
--- a/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c
+++ b/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c
@@ -23,114 +23,9 @@
  * - ported to credential helper API by Philipp A. Hartmann
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <errno.h>
+#include <credential_helper.h>
 #include <gnome-keyring.h>
 
-/*
- * This credential struct and API is simplified from git's credential.{h,c}
- */
-struct credential
-{
-	char          *protocol;
-	char          *host;
-	unsigned short port;
-	char          *path;
-	char          *username;
-	char          *password;
-};
-
-#define CREDENTIAL_INIT \
-  { NULL,NULL,0,NULL,NULL,NULL }
-
-void credential_init(struct credential *c);
-void credential_clear(struct credential *c);
-int  credential_read(struct credential *c);
-void credential_write(const struct credential *c);
-
-typedef int (*credential_op_cb)(struct credential*);
-
-struct credential_operation
-{
-	char             *name;
-	credential_op_cb op;
-};
-
-#define CREDENTIAL_OP_END \
-  { NULL,NULL }
-
-/*
- * Table with operation callbacks is defined in concrete
- * credential helper implementation and contains entries
- * like { "get", function_to_get_credential } terminated
- * by CREDENTIAL_OP_END.
- */
-struct credential_operation const credential_helper_ops[];
-
-/* ---------------- common helper functions ----------------- */
-
-static inline void free_password(char *password)
-{
-	char *c = password;
-	if (!password)
-		return;
-
-	while (*c) *c++ = '\0';
-	free(password);
-}
-
-static inline void warning(const char *fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap, fmt);
-	fprintf(stderr, "warning: ");
-	vfprintf(stderr, fmt, ap);
-	fprintf(stderr, "\n" );
-	va_end(ap);
-}
-
-static inline void error(const char *fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap, fmt);
-	fprintf(stderr, "error: ");
-	vfprintf(stderr, fmt, ap);
-	fprintf(stderr, "\n" );
-	va_end(ap);
-}
-
-static inline void die(const char *fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap,fmt);
-	error(fmt, ap);
-	va_end(ap);
-	exit(EXIT_FAILURE);
-}
-
-static inline void die_errno(int err)
-{
-	error("%s", strerror(err));
-	exit(EXIT_FAILURE);
-}
-
-static inline char *xstrdup(const char *str)
-{
-	char *ret = strdup(str);
-	if (!ret)
-		die_errno(errno);
-
-	return ret;
-}
-
-/* ----------------- GNOME Keyring functions ----------------- */
-
 /* create a special keyring option string, if path is given */
 static char* keyring_object(struct credential *c)
 {
@@ -307,139 +202,3 @@ struct credential_operation const credential_helper_ops[] =
 	{ "erase", keyring_erase },
 	CREDENTIAL_OP_END
 };
-
-/* ------------------ credential functions ------------------ */
-
-void credential_init(struct credential *c)
-{
-	memset(c, 0, sizeof(*c));
-}
-
-void credential_clear(struct credential *c)
-{
-	free(c->protocol);
-	free(c->host);
-	free(c->path);
-	free(c->username);
-	free_password(c->password);
-
-	credential_init(c);
-}
-
-int credential_read(struct credential *c)
-{
-	char    buf[1024];
-	ssize_t line_len = 0;
-	char   *key      = buf;
-	char   *value;
-
-	while (fgets(buf, sizeof(buf), stdin))
-	{
-		line_len = strlen(buf);
-
-		if(buf[line_len-1]=='\n')
-			buf[--line_len]='\0';
-
-		if(!line_len)
-			break;
-
-		value = strchr(buf,'=');
-		if(!value) {
-			warning("invalid credential line: %s", key);
-			return -1;
-		}
-		*value++ = '\0';
-
-		if (!strcmp(key, "protocol")) {
-			free(c->protocol);
-			c->protocol = xstrdup(value);
-		} else if (!strcmp(key, "host")) {
-			free(c->host);
-			c->host = xstrdup(value);
-			value = strrchr(c->host,':');
-			if (value) {
-				*value++ = '\0';
-				c->port = atoi(value);
-			}
-		} else if (!strcmp(key, "path")) {
-			free(c->path);
-			c->path = xstrdup(value);
-		} else if (!strcmp(key, "username")) {
-			free(c->username);
-			c->username = xstrdup(value);
-		} else if (!strcmp(key, "password")) {
-			free_password(c->password);
-			c->password = xstrdup(value);
-			while (*value) *value++ = '\0';
-		}
-		/*
-		 * Ignore other lines; we don't know what they mean, but
-		 * this future-proofs us when later versions of git do
-		 * learn new lines, and the helpers are updated to match.
-		 */
-	}
-	return 0;
-}
-
-void credential_write_item(FILE *fp, const char *key, const char *value)
-{
-	if (!value)
-		return;
-	fprintf(fp, "%s=%s\n", key, value);
-}
-
-void credential_write(const struct credential *c)
-{
-	/* only write username/password, if set */
-	credential_write_item(stdout, "username", c->username);
-	credential_write_item(stdout, "password", c->password);
-}
-
-static void usage(const char *name)
-{
-	struct credential_operation const *try_op = credential_helper_ops;
-	const char *basename = strrchr(name,'/');
-
-	basename = (basename) ? basename + 1 : name;
-	fprintf(stderr, "Usage: %s <", basename);
-	while(try_op->name) {
-		fprintf(stderr,"%s",(try_op++)->name);
-		if(try_op->name)
-			fprintf(stderr,"%s","|");
-	}
-	fprintf(stderr,"%s",">\n");
-}
-
-int main(int argc, char *argv[])
-{
-	int ret = EXIT_SUCCESS;
-
-	struct credential_operation const *try_op = credential_helper_ops;
-	struct credential                  cred   = CREDENTIAL_INIT;
-
-	if (!argv[1]) {
-		usage(argv[0]);
-		goto out;
-	}
-
-	/* lookup operation callback */
-	while(try_op->name && strcmp(argv[1], try_op->name))
-		try_op++;
-
-	/* unsupported operation given -- ignore silently */
-	if(!try_op->name || !try_op->op)
-		goto out;
-
-	ret = credential_read(&cred);
-	if(ret)
-		goto out;
-
-	/* perform credential operation */
-	ret = (*try_op->op)(&cred);
-
-	credential_write(&cred);
-
-out:
-	credential_clear(&cred);
-	return ret;
-}
-- 
1.7.10.4

  parent reply	other threads:[~2012-08-23 17:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-23 16:57 [PATCH] contrib: GnomeKeyring support + generic helper implementation Philipp A. Hartmann
2012-08-23 16:57 ` [PATCH 1/4] contrib: add credential helper for GnomeKeyring Philipp A. Hartmann
2012-08-23 16:57 ` [PATCH 2/4] contrib: add generic credential helper Philipp A. Hartmann
2012-08-23 16:57 ` Philipp A. Hartmann [this message]
2012-08-23 16:57 ` [PATCH 4/4] osxkeychain: port to generic credential helper implementation Philipp A. Hartmann
2012-08-24 18:15 ` [PATCH] contrib: GnomeKeyring support + generic " Junio C Hamano
2012-08-24 21:33   ` Jeff King
2012-08-24 21:46     ` Junio C Hamano
2012-08-26 17:46       ` Junio C Hamano
2012-08-26 18:16         ` Philipp A. Hartmann
2012-08-26 22:04           ` [PATCH 5/4] wincred: port to generic credential helper (UNTESTED) Philipp A. Hartmann
2012-08-26 22:45             ` [PATCH 5/4 v2] " Philipp A. Hartmann
2012-08-30 18:27             ` [PATCH 5/4] " Erik Faye-Lund
2012-08-30 20:11               ` Junio C Hamano
2012-08-31 15:44               ` Erik Faye-Lund
2012-09-01 12:42                 ` [PATCH 5/4 v3] wincred: port to generic credential helper Philipp A. Hartmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1345741068-11004-4-git-send-email-pah@qo.cx \
    --to=pah@qo.cx \
    --cc=git@vger.kernel.org \
    --cc=john@szakmeister.net \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).