1/*
   2 * Copyright (C) 2011 John Szakmeister <john@szakmeister.net>
   3 *               2012 Philipp A. Hartmann <pah@qo.cx>
   4 *
   5 *  This program is free software; you can redistribute it and/or modify
   6 *  it under the terms of the GNU General Public License as published by
   7 *  the Free Software Foundation; either version 2 of the License, or
   8 *  (at your option) any later version.
   9 *
  10 *  This program is distributed in the hope that it will be useful,
  11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 *  GNU General Public License for more details.
  14 *
  15 *  You should have received a copy of the GNU General Public License
  16 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  17 */
  18/*
  20 * Credits:
  21 * - GNOME Keyring API handling originally written by John Szakmeister
  22 * - ported to credential helper API by Philipp A. Hartmann
  23 */
  24#include <stdio.h>
  26#include <string.h>
  27#include <stdlib.h>
  28#include <glib.h>
  29#include <gnome-keyring.h>
  30#ifdef GNOME_KEYRING_DEFAULT
  32   /* Modern gnome-keyring */
  34#include <gnome-keyring-memory.h>
  36#else
  38   /*
  40    * Support ancient gnome-keyring, circ. RHEL 5.X.
  41    * GNOME_KEYRING_DEFAULT seems to have been introduced with Gnome 2.22,
  42    * and the other features roughly around Gnome 2.20, 6 months before.
  43    * Ubuntu 8.04 used Gnome 2.22 (I think).  Not sure any distro used 2.20.
  44    * So the existence/non-existence of GNOME_KEYRING_DEFAULT seems like
  45    * a decent thing to use as an indicator.
  46    */
  47#define GNOME_KEYRING_DEFAULT NULL
  49/*
  51 * ancient gnome-keyring returns DENIED when an entry is not found.
  52 * Setting NO_MATCH to DENIED will prevent us from reporting DENIED
  53 * errors during get and erase operations, but we will still report
  54 * DENIED errors during a store.
  55 */
  56#define GNOME_KEYRING_RESULT_NO_MATCH GNOME_KEYRING_RESULT_DENIED
  57#define gnome_keyring_memory_alloc g_malloc
  59#define gnome_keyring_memory_free gnome_keyring_free_password
  60#define gnome_keyring_memory_strdup g_strdup
  61static const char *gnome_keyring_result_to_message(GnomeKeyringResult result)
  63{
  64        switch (result) {
  65        case GNOME_KEYRING_RESULT_OK:
  66                return "OK";
  67        case GNOME_KEYRING_RESULT_DENIED:
  68                return "Denied";
  69        case GNOME_KEYRING_RESULT_NO_KEYRING_DAEMON:
  70                return "No Keyring Daemon";
  71        case GNOME_KEYRING_RESULT_ALREADY_UNLOCKED:
  72                return "Already UnLocked";
  73        case GNOME_KEYRING_RESULT_NO_SUCH_KEYRING:
  74                return "No Such Keyring";
  75        case GNOME_KEYRING_RESULT_BAD_ARGUMENTS:
  76                return "Bad Arguments";
  77        case GNOME_KEYRING_RESULT_IO_ERROR:
  78                return "IO Error";
  79        case GNOME_KEYRING_RESULT_CANCELLED:
  80                return "Cancelled";
  81        case GNOME_KEYRING_RESULT_ALREADY_EXISTS:
  82                return "Already Exists";
  83        default:
  84                return "Unknown Error";
  85        }
  86}
  87/*
  89 * Support really ancient gnome-keyring, circ. RHEL 4.X.
  90 * Just a guess for the Glib version.  Glib 2.8 was roughly Gnome 2.12 ?
  91 * Which was released with gnome-keyring 0.4.3 ??
  92 */
  93#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 8
  94static void gnome_keyring_done_cb(GnomeKeyringResult result, gpointer user_data)
  96{
  97        gpointer *data = (gpointer *)user_data;
  98        int *done = (int *)data[0];
  99        GnomeKeyringResult *r = (GnomeKeyringResult *)data[1];
 100        *r = result;
 102        *done = 1;
 103}
 104static void wait_for_request_completion(int *done)
 106{
 107        GMainContext *mc = g_main_context_default();
 108        while (!*done)
 109                g_main_context_iteration(mc, TRUE);
 110}
 111static GnomeKeyringResult gnome_keyring_item_delete_sync(const char *keyring, guint32 id)
 113{
 114        int done = 0;
 115        GnomeKeyringResult result;
 116        gpointer data[] = { &done, &result };
 117        gnome_keyring_item_delete(keyring, id, gnome_keyring_done_cb, data,
 119                NULL);
 120        wait_for_request_completion(&done);
 122        return result;
 124}
 125#endif
 127#endif
 128/*
 130 * This credential struct and API is simplified from git's credential.{h,c}
 131 */
 132struct credential {
 133        char *protocol;
 134        char *host;
 135        unsigned short port;
 136        char *path;
 137        char *username;
 138        char *password;
 139};
 140#define CREDENTIAL_INIT { NULL, NULL, 0, NULL, NULL, NULL }
 142typedef int (*credential_op_cb)(struct credential *);
 144struct credential_operation {
 146        char *name;
 147        credential_op_cb op;
 148};
 149#define CREDENTIAL_OP_END { NULL, NULL }
 151/* ----------------- GNOME Keyring functions ----------------- */
 153/* create a special keyring option string, if path is given */
 155static char *keyring_object(struct credential *c)
 156{
 157        if (!c->path)
 158                return NULL;
 159        if (c->port)
 161                return g_strdup_printf("%s:%hd/%s", c->host, c->port, c->path);
 162        return g_strdup_printf("%s/%s", c->host, c->path);
 164}
 165static int keyring_get(struct credential *c)
 167{
 168        char *object = NULL;
 169        GList *entries;
 170        GnomeKeyringNetworkPasswordData *password_data;
 171        GnomeKeyringResult result;
 172        if (!c->protocol || !(c->host || c->path))
 174                return EXIT_FAILURE;
 175        object = keyring_object(c);
 177        result = gnome_keyring_find_network_password_sync(
 179                                c->username,
 180                                NULL /* domain */,
 181                                c->host,
 182                                object,
 183                                c->protocol,
 184                                NULL /* authtype */,
 185                                c->port,
 186                                &entries);
 187        g_free(object);
 189        if (result == GNOME_KEYRING_RESULT_NO_MATCH)
 191                return EXIT_SUCCESS;
 192        if (result == GNOME_KEYRING_RESULT_CANCELLED)
 194                return EXIT_SUCCESS;
 195        if (result != GNOME_KEYRING_RESULT_OK) {
 197                g_critical("%s", gnome_keyring_result_to_message(result));
 198                return EXIT_FAILURE;
 199        }
 200        /* pick the first one from the list */
 202        password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
 203        gnome_keyring_memory_free(c->password);
 205        c->password = gnome_keyring_memory_strdup(password_data->password);
 206        if (!c->username)
 208                c->username = g_strdup(password_data->user);
 209        gnome_keyring_network_password_list_free(entries);
 211        return EXIT_SUCCESS;
 213}
 214static int keyring_store(struct credential *c)
 217{
 218        guint32 item_id;
 219        char *object = NULL;
 220        GnomeKeyringResult result;
 221        /*
 223         * Sanity check that what we are storing is actually sensible.
 224         * In particular, we can't make a URL without a protocol field.
 225         * Without either a host or pathname (depending on the scheme),
 226         * we have no primary key. And without a username and password,
 227         * we are not actually storing a credential.
 228         */
 229        if (!c->protocol || !(c->host || c->path) ||
 230            !c->username || !c->password)
 231                return EXIT_FAILURE;
 232        object = keyring_object(c);
 234        result = gnome_keyring_set_network_password_sync(
 236                                GNOME_KEYRING_DEFAULT,
 237                                c->username,
 238                                NULL /* domain */,
 239                                c->host,
 240                                object,
 241                                c->protocol,
 242                                NULL /* authtype */,
 243                                c->port,
 244                                c->password,
 245                                &item_id);
 246        g_free(object);
 248        if (result != GNOME_KEYRING_RESULT_OK &&
 250            result != GNOME_KEYRING_RESULT_CANCELLED) {
 251                g_critical("%s", gnome_keyring_result_to_message(result));
 252                return EXIT_FAILURE;
 253        }
 254        return EXIT_SUCCESS;
 256}
 257static int keyring_erase(struct credential *c)
 259{
 260        char *object = NULL;
 261        GList *entries;
 262        GnomeKeyringNetworkPasswordData *password_data;
 263        GnomeKeyringResult result;
 264        /*
 266         * Sanity check that we actually have something to match
 267         * against. The input we get is a restrictive pattern,
 268         * so technically a blank credential means "erase everything".
 269         * But it is too easy to accidentally send this, since it is equivalent
 270         * to empty input. So explicitly disallow it, and require that the
 271         * pattern have some actual content to match.
 272         */
 273        if (!c->protocol && !c->host && !c->path && !c->username)
 274                return EXIT_FAILURE;
 275        object = keyring_object(c);
 277        result = gnome_keyring_find_network_password_sync(
 279                                c->username,
 280                                NULL /* domain */,
 281                                c->host,
 282                                object,
 283                                c->protocol,
 284                                NULL /* authtype */,
 285                                c->port,
 286                                &entries);
 287        g_free(object);
 289        if (result == GNOME_KEYRING_RESULT_NO_MATCH)
 291                return EXIT_SUCCESS;
 292        if (result == GNOME_KEYRING_RESULT_CANCELLED)
 294                return EXIT_SUCCESS;
 295        if (result != GNOME_KEYRING_RESULT_OK) {
 297                g_critical("%s", gnome_keyring_result_to_message(result));
 298                return EXIT_FAILURE;
 299        }
 300        /* pick the first one from the list (delete all matches?) */
 302        password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
 303        result = gnome_keyring_item_delete_sync(
 305                password_data->keyring, password_data->item_id);
 306        gnome_keyring_network_password_list_free(entries);
 308        if (result != GNOME_KEYRING_RESULT_OK) {
 310                g_critical("%s", gnome_keyring_result_to_message(result));
 311                return EXIT_FAILURE;
 312        }
 313        return EXIT_SUCCESS;
 315}
 316/*
 318 * Table with helper operation callbacks, used by generic
 319 * credential helper main function.
 320 */
 321static struct credential_operation const credential_helper_ops[] = {
 322        { "get",   keyring_get },
 323        { "store", keyring_store },
 324        { "erase", keyring_erase },
 325        CREDENTIAL_OP_END
 326};
 327/* ------------------ credential functions ------------------ */
 329static void credential_init(struct credential *c)
 331{
 332        memset(c, 0, sizeof(*c));
 333}
 334static void credential_clear(struct credential *c)
 336{
 337        g_free(c->protocol);
 338        g_free(c->host);
 339        g_free(c->path);
 340        g_free(c->username);
 341        gnome_keyring_memory_free(c->password);
 342        credential_init(c);
 344}
 345static int credential_read(struct credential *c)
 347{
 348        char *buf;
 349        size_t line_len;
 350        char *key;
 351        char *value;
 352        key = buf = gnome_keyring_memory_alloc(1024);
 354        while (fgets(buf, 1024, stdin)) {
 356                line_len = strlen(buf);
 357                if (line_len && buf[line_len-1] == '\n')
 359                        buf[--line_len] = '\0';
 360                if (!line_len)
 362                        break;
 363                value = strchr(buf, '=');
 365                if (!value) {
 366                        g_warning("invalid credential line: %s", key);
 367                        gnome_keyring_memory_free(buf);
 368                        return -1;
 369                }
 370                *value++ = '\0';
 371                if (!strcmp(key, "protocol")) {
 373                        g_free(c->protocol);
 374                        c->protocol = g_strdup(value);
 375                } else if (!strcmp(key, "host")) {
 376                        g_free(c->host);
 377                        c->host = g_strdup(value);
 378                        value = strrchr(c->host, ':');
 379                        if (value) {
 380                                *value++ = '\0';
 381                                c->port = atoi(value);
 382                        }
 383                } else if (!strcmp(key, "path")) {
 384                        g_free(c->path);
 385                        c->path = g_strdup(value);
 386                } else if (!strcmp(key, "username")) {
 387                        g_free(c->username);
 388                        c->username = g_strdup(value);
 389                } else if (!strcmp(key, "password")) {
 390                        gnome_keyring_memory_free(c->password);
 391                        c->password = gnome_keyring_memory_strdup(value);
 392                        while (*value)
 393                                *value++ = '\0';
 394                }
 395                /*
 396                 * Ignore other lines; we don't know what they mean, but
 397                 * this future-proofs us when later versions of git do
 398                 * learn new lines, and the helpers are updated to match.
 399                 */
 400        }
 401        gnome_keyring_memory_free(buf);
 403        return 0;
 405}
 406static void credential_write_item(FILE *fp, const char *key, const char *value)
 408{
 409        if (!value)
 410                return;
 411        fprintf(fp, "%s=%s\n", key, value);
 412}
 413static void credential_write(const struct credential *c)
 415{
 416        /* only write username/password, if set */
 417        credential_write_item(stdout, "username", c->username);
 418        credential_write_item(stdout, "password", c->password);
 419}
 420static void usage(const char *name)
 422{
 423        struct credential_operation const *try_op = credential_helper_ops;
 424        const char *basename = strrchr(name, '/');
 425        basename = (basename) ? basename + 1 : name;
 427        fprintf(stderr, "usage: %s <", basename);
 428        while (try_op->name) {
 429                fprintf(stderr, "%s", (try_op++)->name);
 430                if (try_op->name)
 431                        fprintf(stderr, "%s", "|");
 432        }
 433        fprintf(stderr, "%s", ">\n");
 434}
 435int main(int argc, char *argv[])
 437{
 438        int ret = EXIT_SUCCESS;
 439        struct credential_operation const *try_op = credential_helper_ops;
 441        struct credential cred = CREDENTIAL_INIT;
 442        if (!argv[1]) {
 444                usage(argv[0]);
 445                exit(EXIT_FAILURE);
 446        }
 447        g_set_application_name("Git Credential Helper");
 449        /* lookup operation callback */
 451        while (try_op->name && strcmp(argv[1], try_op->name))
 452                try_op++;
 453        /* unsupported operation given -- ignore silently */
 455        if (!try_op->name || !try_op->op)
 456                goto out;
 457        ret = credential_read(&cred);
 459        if (ret)
 460                goto out;
 461        /* perform credential operation */
 463        ret = (*try_op->op)(&cred);
 464        credential_write(&cred);
 466out:
 468        credential_clear(&cred);
 469        return ret;
 470}