447e9aa551619892eb53f6a8b8762d4fb1232784
   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, write to the Free Software
  17 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 */
  19
  20/*
  21 * Credits:
  22 * - GNOME Keyring API handling originally written by John Szakmeister
  23 * - ported to credential helper API by Philipp A. Hartmann
  24 */
  25
  26#include <stdio.h>
  27#include <string.h>
  28#include <stdlib.h>
  29#include <glib.h>
  30#include <gnome-keyring.h>
  31#include <gnome-keyring-memory.h>
  32
  33/*
  34 * This credential struct and API is simplified from git's credential.{h,c}
  35 */
  36struct credential
  37{
  38        char          *protocol;
  39        char          *host;
  40        unsigned short port;
  41        char          *path;
  42        char          *username;
  43        char          *password;
  44};
  45
  46#define CREDENTIAL_INIT \
  47  { NULL,NULL,0,NULL,NULL,NULL }
  48
  49typedef int (*credential_op_cb)(struct credential*);
  50
  51struct credential_operation
  52{
  53        char             *name;
  54        credential_op_cb op;
  55};
  56
  57#define CREDENTIAL_OP_END \
  58  { NULL,NULL }
  59
  60/* ----------------- GNOME Keyring functions ----------------- */
  61
  62/* create a special keyring option string, if path is given */
  63static char* keyring_object(struct credential *c)
  64{
  65        if (!c->path)
  66                return NULL;
  67
  68        if (c->port)
  69                return g_strdup_printf("%s:%hd/%s", c->host, c->port, c->path);
  70
  71        return g_strdup_printf("%s/%s", c->host, c->path);
  72}
  73
  74static int keyring_get(struct credential *c)
  75{
  76        char* object = NULL;
  77        GList *entries;
  78        GnomeKeyringNetworkPasswordData *password_data;
  79        GnomeKeyringResult result;
  80
  81        if (!c->protocol || !(c->host || c->path))
  82                return EXIT_FAILURE;
  83
  84        object = keyring_object(c);
  85
  86        result = gnome_keyring_find_network_password_sync(
  87                                c->username,
  88                                NULL /* domain */,
  89                                c->host,
  90                                object,
  91                                c->protocol,
  92                                NULL /* authtype */,
  93                                c->port,
  94                                &entries);
  95
  96        g_free(object);
  97
  98        if (result == GNOME_KEYRING_RESULT_NO_MATCH)
  99                return EXIT_SUCCESS;
 100
 101        if (result == GNOME_KEYRING_RESULT_CANCELLED)
 102                return EXIT_SUCCESS;
 103
 104        if (result != GNOME_KEYRING_RESULT_OK) {
 105                g_critical("%s", gnome_keyring_result_to_message(result));
 106                return EXIT_FAILURE;
 107        }
 108
 109        /* pick the first one from the list */
 110        password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
 111
 112        gnome_keyring_memory_free(c->password);
 113        c->password = gnome_keyring_memory_strdup(password_data->password);
 114
 115        if (!c->username)
 116                c->username = g_strdup(password_data->user);
 117
 118        gnome_keyring_network_password_list_free(entries);
 119
 120        return EXIT_SUCCESS;
 121}
 122
 123
 124static int keyring_store(struct credential *c)
 125{
 126        guint32 item_id;
 127        char  *object = NULL;
 128        GnomeKeyringResult result;
 129
 130        /*
 131         * Sanity check that what we are storing is actually sensible.
 132         * In particular, we can't make a URL without a protocol field.
 133         * Without either a host or pathname (depending on the scheme),
 134         * we have no primary key. And without a username and password,
 135         * we are not actually storing a credential.
 136         */
 137        if (!c->protocol || !(c->host || c->path) ||
 138            !c->username || !c->password)
 139                return EXIT_FAILURE;
 140
 141        object = keyring_object(c);
 142
 143        result = gnome_keyring_set_network_password_sync(
 144                                GNOME_KEYRING_DEFAULT,
 145                                c->username,
 146                                NULL /* domain */,
 147                                c->host,
 148                                object,
 149                                c->protocol,
 150                                NULL /* authtype */,
 151                                c->port,
 152                                c->password,
 153                                &item_id);
 154
 155        g_free(object);
 156
 157        if (result != GNOME_KEYRING_RESULT_OK &&
 158            result != GNOME_KEYRING_RESULT_CANCELLED) {
 159                g_critical("%s", gnome_keyring_result_to_message(result));
 160                return EXIT_FAILURE;
 161        }
 162
 163        return EXIT_SUCCESS;
 164}
 165
 166static int keyring_erase(struct credential *c)
 167{
 168        char  *object = NULL;
 169        GList *entries;
 170        GnomeKeyringNetworkPasswordData *password_data;
 171        GnomeKeyringResult result;
 172
 173        /*
 174         * Sanity check that we actually have something to match
 175         * against. The input we get is a restrictive pattern,
 176         * so technically a blank credential means "erase everything".
 177         * But it is too easy to accidentally send this, since it is equivalent
 178         * to empty input. So explicitly disallow it, and require that the
 179         * pattern have some actual content to match.
 180         */
 181        if (!c->protocol && !c->host && !c->path && !c->username)
 182                return EXIT_FAILURE;
 183
 184        object = keyring_object(c);
 185
 186        result = gnome_keyring_find_network_password_sync(
 187                                c->username,
 188                                NULL /* domain */,
 189                                c->host,
 190                                object,
 191                                c->protocol,
 192                                NULL /* authtype */,
 193                                c->port,
 194                                &entries);
 195
 196        g_free(object);
 197
 198        if (result == GNOME_KEYRING_RESULT_NO_MATCH)
 199                return EXIT_SUCCESS;
 200
 201        if (result == GNOME_KEYRING_RESULT_CANCELLED)
 202                return EXIT_SUCCESS;
 203
 204        if (result != GNOME_KEYRING_RESULT_OK)
 205        {
 206                g_critical("%s", gnome_keyring_result_to_message(result));
 207                return EXIT_FAILURE;
 208        }
 209
 210        /* pick the first one from the list (delete all matches?) */
 211        password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
 212
 213        result = gnome_keyring_item_delete_sync(
 214                password_data->keyring, password_data->item_id);
 215
 216        gnome_keyring_network_password_list_free(entries);
 217
 218        if (result != GNOME_KEYRING_RESULT_OK)
 219        {
 220                g_critical("%s", gnome_keyring_result_to_message(result));
 221                return EXIT_FAILURE;
 222        }
 223
 224        return EXIT_SUCCESS;
 225}
 226
 227/*
 228 * Table with helper operation callbacks, used by generic
 229 * credential helper main function.
 230 */
 231static struct credential_operation const credential_helper_ops[] =
 232{
 233        { "get",   keyring_get   },
 234        { "store", keyring_store },
 235        { "erase", keyring_erase },
 236        CREDENTIAL_OP_END
 237};
 238
 239/* ------------------ credential functions ------------------ */
 240
 241static void credential_init(struct credential *c)
 242{
 243        memset(c, 0, sizeof(*c));
 244}
 245
 246static void credential_clear(struct credential *c)
 247{
 248        g_free(c->protocol);
 249        g_free(c->host);
 250        g_free(c->path);
 251        g_free(c->username);
 252        gnome_keyring_memory_free(c->password);
 253
 254        credential_init(c);
 255}
 256
 257static int credential_read(struct credential *c)
 258{
 259        char    *buf;
 260        size_t line_len;
 261        char   *key;
 262        char   *value;
 263
 264        key = buf = gnome_keyring_memory_alloc(1024);
 265
 266        while (fgets(buf, 1024, stdin))
 267        {
 268                line_len = strlen(buf);
 269
 270                if (line_len && buf[line_len-1] == '\n')
 271                        buf[--line_len]='\0';
 272
 273                if (!line_len)
 274                        break;
 275
 276                value = strchr(buf,'=');
 277                if (!value) {
 278                        g_warning("invalid credential line: %s", key);
 279                        gnome_keyring_memory_free(buf);
 280                        return -1;
 281                }
 282                *value++ = '\0';
 283
 284                if (!strcmp(key, "protocol")) {
 285                        g_free(c->protocol);
 286                        c->protocol = g_strdup(value);
 287                } else if (!strcmp(key, "host")) {
 288                        g_free(c->host);
 289                        c->host = g_strdup(value);
 290                        value = strrchr(c->host,':');
 291                        if (value) {
 292                                *value++ = '\0';
 293                                c->port = atoi(value);
 294                        }
 295                } else if (!strcmp(key, "path")) {
 296                        g_free(c->path);
 297                        c->path = g_strdup(value);
 298                } else if (!strcmp(key, "username")) {
 299                        g_free(c->username);
 300                        c->username = g_strdup(value);
 301                } else if (!strcmp(key, "password")) {
 302                        gnome_keyring_memory_free(c->password);
 303                        c->password = gnome_keyring_memory_strdup(value);
 304                        while (*value) *value++ = '\0';
 305                }
 306                /*
 307                 * Ignore other lines; we don't know what they mean, but
 308                 * this future-proofs us when later versions of git do
 309                 * learn new lines, and the helpers are updated to match.
 310                 */
 311        }
 312
 313        gnome_keyring_memory_free(buf);
 314
 315        return 0;
 316}
 317
 318static void credential_write_item(FILE *fp, const char *key, const char *value)
 319{
 320        if (!value)
 321                return;
 322        fprintf(fp, "%s=%s\n", key, value);
 323}
 324
 325static void credential_write(const struct credential *c)
 326{
 327        /* only write username/password, if set */
 328        credential_write_item(stdout, "username", c->username);
 329        credential_write_item(stdout, "password", c->password);
 330}
 331
 332static void usage(const char *name)
 333{
 334        struct credential_operation const *try_op = credential_helper_ops;
 335        const char *basename = strrchr(name,'/');
 336
 337        basename = (basename) ? basename + 1 : name;
 338        fprintf(stderr, "usage: %s <", basename);
 339        while (try_op->name) {
 340                fprintf(stderr,"%s",(try_op++)->name);
 341                if (try_op->name)
 342                        fprintf(stderr,"%s","|");
 343        }
 344        fprintf(stderr,"%s",">\n");
 345}
 346
 347int main(int argc, char *argv[])
 348{
 349        int ret = EXIT_SUCCESS;
 350
 351        struct credential_operation const *try_op = credential_helper_ops;
 352        struct credential                  cred   = CREDENTIAL_INIT;
 353
 354        if (!argv[1]) {
 355                usage(argv[0]);
 356                exit(EXIT_FAILURE);
 357        }
 358
 359        g_set_application_name("Git Credential Helper");
 360
 361        /* lookup operation callback */
 362        while (try_op->name && strcmp(argv[1], try_op->name))
 363                try_op++;
 364
 365        /* unsupported operation given -- ignore silently */
 366        if (!try_op->name || !try_op->op)
 367                goto out;
 368
 369        ret = credential_read(&cred);
 370        if (ret)
 371                goto out;
 372
 373        /* perform credential operation */
 374        ret = (*try_op->op)(&cred);
 375
 376        credential_write(&cred);
 377
 378out:
 379        credential_clear(&cred);
 380        return ret;
 381}