Merge branch 'nd/diff-quiet-stat-dirty'
[gitweb.git] / contrib / credential / gnome-keyring / git-credential-gnome-keyring.c
index f2cdefee605aebe6c577129ae2c8cd76a3555a96..2a317fca442a51dd6f47bfeeaa19bf32ed6e670f 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
-#include <stdarg.h>
 #include <stdlib.h>
-#include <errno.h>
+#include <glib.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;
-};
+#ifdef GNOME_KEYRING_DEFAULT
 
-#define CREDENTIAL_INIT \
-  { NULL,NULL,0,NULL,NULL,NULL }
+   /* Modern gnome-keyring */
 
-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);
+#include <gnome-keyring-memory.h>
 
-typedef int (*credential_op_cb)(struct credential*);
+#else
 
-struct credential_operation
-{
-       char             *name;
-       credential_op_cb op;
-};
+   /*
+    * Support ancient gnome-keyring, circ. RHEL 5.X.
+    * GNOME_KEYRING_DEFAULT seems to have been introduced with Gnome 2.22,
+    * and the other features roughly around Gnome 2.20, 6 months before.
+    * Ubuntu 8.04 used Gnome 2.22 (I think).  Not sure any distro used 2.20.
+    * So the existence/non-existence of GNOME_KEYRING_DEFAULT seems like
+    * a decent thing to use as an indicator.
+    */
 
-#define CREDENTIAL_OP_END \
-  { NULL,NULL }
+#define GNOME_KEYRING_DEFAULT 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.
+ * ancient gnome-keyring returns DENIED when an entry is not found.
+ * Setting NO_MATCH to DENIED will prevent us from reporting DENIED
+ * errors during get and erase operations, but we will still report
+ * DENIED errors during a store.
  */
-struct credential_operation const credential_helper_ops[];
+#define GNOME_KEYRING_RESULT_NO_MATCH GNOME_KEYRING_RESULT_DENIED
 
-/* ---------------- common helper functions ----------------- */
+#define gnome_keyring_memory_alloc g_malloc
+#define gnome_keyring_memory_free gnome_keyring_free_password
+#define gnome_keyring_memory_strdup g_strdup
 
-static inline void free_password(char *password)
+static const char *gnome_keyring_result_to_message(GnomeKeyringResult result)
 {
-       char *c = password;
-       if (!password)
-               return;
-
-       while (*c) *c++ = '\0';
-       free(password);
+       switch (result) {
+       case GNOME_KEYRING_RESULT_OK:
+               return "OK";
+       case GNOME_KEYRING_RESULT_DENIED:
+               return "Denied";
+       case GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON:
+               return "No Keyring Daemon";
+       case GNOME_KEYRING_RESULT_ALREADY_UNLOCKED:
+               return "Already UnLocked";
+       case GNOME_KEYRING_RESULT_NO_SUCH_KEYRING:
+               return "No Such Keyring";
+       case GNOME_KEYRING_RESULT_BAD_ARGUMENTS:
+               return "Bad Arguments";
+       case GNOME_KEYRING_RESULT_IO_ERROR:
+               return "IO Error";
+       case GNOME_KEYRING_RESULT_CANCELLED:
+               return "Cancelled";
+       case GNOME_KEYRING_RESULT_ALREADY_EXISTS:
+               return "Already Exists";
+       default:
+               return "Unknown Error";
+       }
 }
 
-static inline void warning(const char *fmt, ...)
+/*
+ * Support really ancient gnome-keyring, circ. RHEL 4.X.
+ * Just a guess for the Glib version.  Glib 2.8 was roughly Gnome 2.12 ?
+ * Which was released with gnome-keyring 0.4.3 ??
+ */
+#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 8
+
+static void gnome_keyring_done_cb(GnomeKeyringResult result, gpointer user_data)
 {
-       va_list ap;
+       gpointer *data = (gpointer *)user_data;
+       int *done = (int *)data[0];
+       GnomeKeyringResult *r = (GnomeKeyringResult *)data[1];
 
-       va_start(ap, fmt);
-       fprintf(stderr, "warning: ");
-       vfprintf(stderr, fmt, ap);
-       fprintf(stderr, "\n" );
-       va_end(ap);
+       *r = result;
+       *done = 1;
 }
 
-static inline void error(const char *fmt, ...)
+static void wait_for_request_completion(int *done)
 {
-       va_list ap;
-
-       va_start(ap, fmt);
-       fprintf(stderr, "error: ");
-       vfprintf(stderr, fmt, ap);
-       fprintf(stderr, "\n" );
-       va_end(ap);
+       GMainContext *mc = g_main_context_default();
+       while (!*done)
+               g_main_context_iteration(mc, TRUE);
 }
 
-static inline void die(const char *fmt, ...)
+static GnomeKeyringResult gnome_keyring_item_delete_sync(const char *keyring, guint32 id)
 {
-       va_list ap;
+       int done = 0;
+       GnomeKeyringResult result;
+       gpointer data[] = { &done, &result };
 
-       va_start(ap,fmt);
-       error(fmt, ap);
-       va_end(ap);
-       exit(EXIT_FAILURE);
-}
+       gnome_keyring_item_delete(keyring, id, gnome_keyring_done_cb, data,
+               NULL);
 
-static inline void die_errno(int err)
-{
-       error("%s", strerror(err));
-       exit(EXIT_FAILURE);
+       wait_for_request_completion(&done);
+
+       return result;
 }
 
-static inline char *xstrdup(const char *str)
-{
-       char *ret = strdup(str);
-       if (!ret)
-               die_errno(errno);
+#endif
+#endif
 
-       return ret;
-}
+/*
+ * 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 }
+
+typedef int (*credential_op_cb)(struct credential *);
+
+struct credential_operation {
+       char *name;
+       credential_op_cb op;
+};
+
+#define CREDENTIAL_OP_END { NULL, NULL }
 
 /* ----------------- GNOME Keyring functions ----------------- */
 
 /* create a special keyring option string, if path is given */
-static charkeyring_object(struct credential *c)
+static char *keyring_object(struct credential *c)
 {
-       char* object = NULL;
-
        if (!c->path)
-               return object;
+               return NULL;
 
-       object = (char*) malloc(strlen(c->host)+strlen(c->path)+8);
-       if(!object)
-               die_errno(errno);
+       if (c->port)
+               return g_strdup_printf("%s:%hd/%s", c->host, c->port, c->path);
 
-       if(c->port)
-               sprintf(object,"%s:%hd/%s",c->host,c->port,c->path);
-       else
-               sprintf(object,"%s/%s",c->host,c->path);
-
-       return object;
+       return g_strdup_printf("%s/%s", c->host, c->path);
 }
 
-int keyring_get(struct credential *c)
+static int keyring_get(struct credential *c)
 {
-       charobject = NULL;
+       char *object = NULL;
        GList *entries;
        GnomeKeyringNetworkPasswordData *password_data;
        GnomeKeyringResult result;
@@ -173,7 +186,7 @@ int keyring_get(struct credential *c)
                                c->port,
                                &entries);
 
-       free(object);
+       g_free(object);
 
        if (result == GNOME_KEYRING_RESULT_NO_MATCH)
                return EXIT_SUCCESS;
@@ -182,18 +195,18 @@ int keyring_get(struct credential *c)
                return EXIT_SUCCESS;
 
        if (result != GNOME_KEYRING_RESULT_OK) {
-               error("%s",gnome_keyring_result_to_message(result));
+               g_critical("%s", gnome_keyring_result_to_message(result));
                return EXIT_FAILURE;
        }
 
        /* pick the first one from the list */
-       password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
+       password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
 
-       free_password(c->password);
-       c->password = xstrdup(password_data->password);
+       gnome_keyring_memory_free(c->password);
+       c->password = gnome_keyring_memory_strdup(password_data->password);
 
        if (!c->username)
-               c->username = xstrdup(password_data->user);
+               c->username = g_strdup(password_data->user);
 
        gnome_keyring_network_password_list_free(entries);
 
@@ -201,10 +214,11 @@ int keyring_get(struct credential *c)
 }
 
 
-int keyring_store(struct credential *c)
+static int keyring_store(struct credential *c)
 {
        guint32 item_id;
-       char  *object = NULL;
+       char *object = NULL;
+       GnomeKeyringResult result;
 
        /*
         * Sanity check that what we are storing is actually sensible.
@@ -219,7 +233,7 @@ int keyring_store(struct credential *c)
 
        object = keyring_object(c);
 
-       gnome_keyring_set_network_password_sync(
+       result = gnome_keyring_set_network_password_sync(
                                GNOME_KEYRING_DEFAULT,
                                c->username,
                                NULL /* domain */,
@@ -231,13 +245,20 @@ int keyring_store(struct credential *c)
                                c->password,
                                &item_id);
 
-       free(object);
+       g_free(object);
+
+       if (result != GNOME_KEYRING_RESULT_OK &&
+           result != GNOME_KEYRING_RESULT_CANCELLED) {
+               g_critical("%s", gnome_keyring_result_to_message(result));
+               return EXIT_FAILURE;
+       }
+
        return EXIT_SUCCESS;
 }
 
-int keyring_erase(struct credential *c)
+static int keyring_erase(struct credential *c)
 {
-       char  *object = NULL;
+       char *object = NULL;
        GList *entries;
        GnomeKeyringNetworkPasswordData *password_data;
        GnomeKeyringResult result;
@@ -265,7 +286,7 @@ int keyring_erase(struct credential *c)
                                c->port,
                                &entries);
 
-       free(object);
+       g_free(object);
 
        if (result == GNOME_KEYRING_RESULT_NO_MATCH)
                return EXIT_SUCCESS;
@@ -273,23 +294,21 @@ int keyring_erase(struct credential *c)
        if (result == GNOME_KEYRING_RESULT_CANCELLED)
                return EXIT_SUCCESS;
 
-       if (result != GNOME_KEYRING_RESULT_OK)
-       {
-               error("%s",gnome_keyring_result_to_message(result));
+       if (result != GNOME_KEYRING_RESULT_OK) {
+               g_critical("%s", gnome_keyring_result_to_message(result));
                return EXIT_FAILURE;
        }
 
        /* pick the first one from the list (delete all matches?) */
-       password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
+       password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
 
        result = gnome_keyring_item_delete_sync(
                password_data->keyring, password_data->item_id);
 
        gnome_keyring_network_password_list_free(entries);
 
-       if (result != GNOME_KEYRING_RESULT_OK)
-       {
-               error("%s",gnome_keyring_result_to_message(result));
+       if (result != GNOME_KEYRING_RESULT_OK) {
+               g_critical("%s", gnome_keyring_result_to_message(result));
                return EXIT_FAILURE;
        }
 
@@ -300,9 +319,8 @@ int keyring_erase(struct credential *c)
  * Table with helper operation callbacks, used by generic
  * credential helper main function.
  */
-struct credential_operation const credential_helper_ops[] =
-{
-       { "get",   keyring_get   },
+static struct credential_operation const credential_helper_ops[] = {
+       { "get",   keyring_get },
        { "store", keyring_store },
        { "erase", keyring_erase },
        CREDENTIAL_OP_END
@@ -310,67 +328,70 @@ struct credential_operation const credential_helper_ops[] =
 
 /* ------------------ credential functions ------------------ */
 
-void credential_init(struct credential *c)
+static void credential_init(struct credential *c)
 {
        memset(c, 0, sizeof(*c));
 }
 
-void credential_clear(struct credential *c)
+static void credential_clear(struct credential *c)
 {
-       free(c->protocol);
-       free(c->host);
-       free(c->path);
-       free(c->username);
-       free_password(c->password);
+       g_free(c->protocol);
+       g_free(c->host);
+       g_free(c->path);
+       g_free(c->username);
+       gnome_keyring_memory_free(c->password);
 
        credential_init(c);
 }
 
-int credential_read(struct credential *c)
+static int credential_read(struct credential *c)
 {
-       char    buf[1024];
-       ssize_t line_len = 0;
-       char   *key      = buf;
-       char   *value;
+       char *buf;
+       size_t line_len;
+       char *key;
+       char *value;
 
-       while (fgets(buf, sizeof(buf), stdin))
-       {
+       key = buf = gnome_keyring_memory_alloc(1024);
+
+       while (fgets(buf, 1024, stdin)) {
                line_len = strlen(buf);
 
-               if(buf[line_len-1]=='\n')
-                       buf[--line_len]='\0';
+               if (line_len && buf[line_len-1] == '\n')
+                       buf[--line_len] = '\0';
 
-               if(!line_len)
+               if (!line_len)
                        break;
 
-               value = strchr(buf,'=');
-               if(!value) {
-                       warning("invalid credential line: %s", key);
+               value = strchr(buf, '=');
+               if (!value) {
+                       g_warning("invalid credential line: %s", key);
+                       gnome_keyring_memory_free(buf);
                        return -1;
                }
                *value++ = '\0';
 
                if (!strcmp(key, "protocol")) {
-                       free(c->protocol);
-                       c->protocol = xstrdup(value);
+                       g_free(c->protocol);
+                       c->protocol = g_strdup(value);
                } else if (!strcmp(key, "host")) {
-                       free(c->host);
-                       c->host = xstrdup(value);
-                       value = strrchr(c->host,':');
+                       g_free(c->host);
+                       c->host = g_strdup(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);
+                       g_free(c->path);
+                       c->path = g_strdup(value);
                } else if (!strcmp(key, "username")) {
-                       free(c->username);
-                       c->username = xstrdup(value);
+                       g_free(c->username);
+                       c->username = g_strdup(value);
                } else if (!strcmp(key, "password")) {
-                       free_password(c->password);
-                       c->password = xstrdup(value);
-                       while (*value) *value++ = '\0';
+                       gnome_keyring_memory_free(c->password);
+                       c->password = gnome_keyring_memory_strdup(value);
+                       while (*value)
+                               *value++ = '\0';
                }
                /*
                 * Ignore other lines; we don't know what they mean, but
@@ -378,17 +399,20 @@ int credential_read(struct credential *c)
                 * learn new lines, and the helpers are updated to match.
                 */
        }
+
+       gnome_keyring_memory_free(buf);
+
        return 0;
 }
 
-void credential_write_item(FILE *fp, const char *key, const char *value)
+static 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)
+static void credential_write(const struct credential *c)
 {
        /* only write username/password, if set */
        credential_write_item(stdout, "username", c->username);
@@ -398,16 +422,16 @@ void credential_write(const struct credential *c)
 static void usage(const char *name)
 {
        struct credential_operation const *try_op = credential_helper_ops;
-       const char *basename = strrchr(name,'/');
+       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","|");
+       while (try_op->name) {
+               fprintf(stderr, "%s", (try_op++)->name);
+               if (try_op->name)
+                       fprintf(stderr, "%s", "|");
        }
-       fprintf(stderr,"%s",">\n");
+       fprintf(stderr, "%s", ">\n");
 }
 
 int main(int argc, char *argv[])
@@ -415,23 +439,25 @@ int main(int argc, char *argv[])
        int ret = EXIT_SUCCESS;
 
        struct credential_operation const *try_op = credential_helper_ops;
-       struct credential                  cred   = CREDENTIAL_INIT;
+       struct credential cred = CREDENTIAL_INIT;
 
        if (!argv[1]) {
                usage(argv[0]);
-               goto out;
+               exit(EXIT_FAILURE);
        }
 
+       g_set_application_name("Git Credential Helper");
+
        /* lookup operation callback */
-       while(try_op->name && strcmp(argv[1], try_op->name))
+       while (try_op->name && strcmp(argv[1], try_op->name))
                try_op++;
 
        /* unsupported operation given -- ignore silently */
-       if(!try_op->name || !try_op->op)
+       if (!try_op->name || !try_op->op)
                goto out;
 
        ret = credential_read(&cred);
-       if(ret)
+       if (ret)
                goto out;
 
        /* perform credential operation */