credential.con commit credential: let helpers tell us to quit (59b3865)
   1#include "cache.h"
   2#include "credential.h"
   3#include "string-list.h"
   4#include "run-command.h"
   5#include "url.h"
   6#include "prompt.h"
   7
   8void credential_init(struct credential *c)
   9{
  10        memset(c, 0, sizeof(*c));
  11        c->helpers.strdup_strings = 1;
  12}
  13
  14void credential_clear(struct credential *c)
  15{
  16        free(c->protocol);
  17        free(c->host);
  18        free(c->path);
  19        free(c->username);
  20        free(c->password);
  21        string_list_clear(&c->helpers, 0);
  22
  23        credential_init(c);
  24}
  25
  26int credential_match(const struct credential *want,
  27                     const struct credential *have)
  28{
  29#define CHECK(x) (!want->x || (have->x && !strcmp(want->x, have->x)))
  30        return CHECK(protocol) &&
  31               CHECK(host) &&
  32               CHECK(path) &&
  33               CHECK(username);
  34#undef CHECK
  35}
  36
  37static int credential_config_callback(const char *var, const char *value,
  38                                      void *data)
  39{
  40        struct credential *c = data;
  41        const char *key, *dot;
  42
  43        if (!skip_prefix(var, "credential.", &key))
  44                return 0;
  45
  46        if (!value)
  47                return config_error_nonbool(var);
  48
  49        dot = strrchr(key, '.');
  50        if (dot) {
  51                struct credential want = CREDENTIAL_INIT;
  52                char *url = xmemdupz(key, dot - key);
  53                int matched;
  54
  55                credential_from_url(&want, url);
  56                matched = credential_match(&want, c);
  57
  58                credential_clear(&want);
  59                free(url);
  60
  61                if (!matched)
  62                        return 0;
  63                key = dot + 1;
  64        }
  65
  66        if (!strcmp(key, "helper"))
  67                string_list_append(&c->helpers, value);
  68        else if (!strcmp(key, "username")) {
  69                if (!c->username)
  70                        c->username = xstrdup(value);
  71        }
  72        else if (!strcmp(key, "usehttppath"))
  73                c->use_http_path = git_config_bool(var, value);
  74
  75        return 0;
  76}
  77
  78static int proto_is_http(const char *s)
  79{
  80        if (!s)
  81                return 0;
  82        return !strcmp(s, "https") || !strcmp(s, "http");
  83}
  84
  85static void credential_apply_config(struct credential *c)
  86{
  87        if (c->configured)
  88                return;
  89        git_config(credential_config_callback, c);
  90        c->configured = 1;
  91
  92        if (!c->use_http_path && proto_is_http(c->protocol)) {
  93                free(c->path);
  94                c->path = NULL;
  95        }
  96}
  97
  98static void credential_describe(struct credential *c, struct strbuf *out)
  99{
 100        if (!c->protocol)
 101                return;
 102        strbuf_addf(out, "%s://", c->protocol);
 103        if (c->username && *c->username)
 104                strbuf_addf(out, "%s@", c->username);
 105        if (c->host)
 106                strbuf_addstr(out, c->host);
 107        if (c->path)
 108                strbuf_addf(out, "/%s", c->path);
 109}
 110
 111static char *credential_ask_one(const char *what, struct credential *c,
 112                                int flags)
 113{
 114        struct strbuf desc = STRBUF_INIT;
 115        struct strbuf prompt = STRBUF_INIT;
 116        char *r;
 117
 118        credential_describe(c, &desc);
 119        if (desc.len)
 120                strbuf_addf(&prompt, "%s for '%s': ", what, desc.buf);
 121        else
 122                strbuf_addf(&prompt, "%s: ", what);
 123
 124        r = git_prompt(prompt.buf, flags);
 125
 126        strbuf_release(&desc);
 127        strbuf_release(&prompt);
 128        return xstrdup(r);
 129}
 130
 131static void credential_getpass(struct credential *c)
 132{
 133        if (!c->username)
 134                c->username = credential_ask_one("Username", c,
 135                                                 PROMPT_ASKPASS|PROMPT_ECHO);
 136        if (!c->password)
 137                c->password = credential_ask_one("Password", c,
 138                                                 PROMPT_ASKPASS);
 139}
 140
 141int credential_read(struct credential *c, FILE *fp)
 142{
 143        struct strbuf line = STRBUF_INIT;
 144
 145        while (strbuf_getline(&line, fp, '\n') != EOF) {
 146                char *key = line.buf;
 147                char *value = strchr(key, '=');
 148
 149                if (!line.len)
 150                        break;
 151
 152                if (!value) {
 153                        warning("invalid credential line: %s", key);
 154                        strbuf_release(&line);
 155                        return -1;
 156                }
 157                *value++ = '\0';
 158
 159                if (!strcmp(key, "username")) {
 160                        free(c->username);
 161                        c->username = xstrdup(value);
 162                } else if (!strcmp(key, "password")) {
 163                        free(c->password);
 164                        c->password = xstrdup(value);
 165                } else if (!strcmp(key, "protocol")) {
 166                        free(c->protocol);
 167                        c->protocol = xstrdup(value);
 168                } else if (!strcmp(key, "host")) {
 169                        free(c->host);
 170                        c->host = xstrdup(value);
 171                } else if (!strcmp(key, "path")) {
 172                        free(c->path);
 173                        c->path = xstrdup(value);
 174                } else if (!strcmp(key, "url")) {
 175                        credential_from_url(c, value);
 176                } else if (!strcmp(key, "quit")) {
 177                        c->quit = !!git_config_bool("quit", value);
 178                }
 179                /*
 180                 * Ignore other lines; we don't know what they mean, but
 181                 * this future-proofs us when later versions of git do
 182                 * learn new lines, and the helpers are updated to match.
 183                 */
 184        }
 185
 186        strbuf_release(&line);
 187        return 0;
 188}
 189
 190static void credential_write_item(FILE *fp, const char *key, const char *value)
 191{
 192        if (!value)
 193                return;
 194        fprintf(fp, "%s=%s\n", key, value);
 195}
 196
 197void credential_write(const struct credential *c, FILE *fp)
 198{
 199        credential_write_item(fp, "protocol", c->protocol);
 200        credential_write_item(fp, "host", c->host);
 201        credential_write_item(fp, "path", c->path);
 202        credential_write_item(fp, "username", c->username);
 203        credential_write_item(fp, "password", c->password);
 204}
 205
 206static int run_credential_helper(struct credential *c,
 207                                 const char *cmd,
 208                                 int want_output)
 209{
 210        struct child_process helper;
 211        const char *argv[] = { NULL, NULL };
 212        FILE *fp;
 213
 214        memset(&helper, 0, sizeof(helper));
 215        argv[0] = cmd;
 216        helper.argv = argv;
 217        helper.use_shell = 1;
 218        helper.in = -1;
 219        if (want_output)
 220                helper.out = -1;
 221        else
 222                helper.no_stdout = 1;
 223
 224        if (start_command(&helper) < 0)
 225                return -1;
 226
 227        fp = xfdopen(helper.in, "w");
 228        credential_write(c, fp);
 229        fclose(fp);
 230
 231        if (want_output) {
 232                int r;
 233                fp = xfdopen(helper.out, "r");
 234                r = credential_read(c, fp);
 235                fclose(fp);
 236                if (r < 0) {
 237                        finish_command(&helper);
 238                        return -1;
 239                }
 240        }
 241
 242        if (finish_command(&helper))
 243                return -1;
 244        return 0;
 245}
 246
 247static int credential_do(struct credential *c, const char *helper,
 248                         const char *operation)
 249{
 250        struct strbuf cmd = STRBUF_INIT;
 251        int r;
 252
 253        if (helper[0] == '!')
 254                strbuf_addstr(&cmd, helper + 1);
 255        else if (is_absolute_path(helper))
 256                strbuf_addstr(&cmd, helper);
 257        else
 258                strbuf_addf(&cmd, "git credential-%s", helper);
 259
 260        strbuf_addf(&cmd, " %s", operation);
 261        r = run_credential_helper(c, cmd.buf, !strcmp(operation, "get"));
 262
 263        strbuf_release(&cmd);
 264        return r;
 265}
 266
 267void credential_fill(struct credential *c)
 268{
 269        int i;
 270
 271        if (c->username && c->password)
 272                return;
 273
 274        credential_apply_config(c);
 275
 276        for (i = 0; i < c->helpers.nr; i++) {
 277                credential_do(c, c->helpers.items[i].string, "get");
 278                if (c->username && c->password)
 279                        return;
 280                if (c->quit)
 281                        die("credential helper '%s' told us to quit",
 282                            c->helpers.items[i].string);
 283        }
 284
 285        credential_getpass(c);
 286        if (!c->username && !c->password)
 287                die("unable to get password from user");
 288}
 289
 290void credential_approve(struct credential *c)
 291{
 292        int i;
 293
 294        if (c->approved)
 295                return;
 296        if (!c->username || !c->password)
 297                return;
 298
 299        credential_apply_config(c);
 300
 301        for (i = 0; i < c->helpers.nr; i++)
 302                credential_do(c, c->helpers.items[i].string, "store");
 303        c->approved = 1;
 304}
 305
 306void credential_reject(struct credential *c)
 307{
 308        int i;
 309
 310        credential_apply_config(c);
 311
 312        for (i = 0; i < c->helpers.nr; i++)
 313                credential_do(c, c->helpers.items[i].string, "erase");
 314
 315        free(c->username);
 316        c->username = NULL;
 317        free(c->password);
 318        c->password = NULL;
 319        c->approved = 0;
 320}
 321
 322void credential_from_url(struct credential *c, const char *url)
 323{
 324        const char *at, *colon, *cp, *slash, *host, *proto_end;
 325
 326        credential_clear(c);
 327
 328        /*
 329         * Match one of:
 330         *   (1) proto://<host>/...
 331         *   (2) proto://<user>@<host>/...
 332         *   (3) proto://<user>:<pass>@<host>/...
 333         */
 334        proto_end = strstr(url, "://");
 335        if (!proto_end)
 336                return;
 337        cp = proto_end + 3;
 338        at = strchr(cp, '@');
 339        colon = strchr(cp, ':');
 340        slash = strchrnul(cp, '/');
 341
 342        if (!at || slash <= at) {
 343                /* Case (1) */
 344                host = cp;
 345        }
 346        else if (!colon || at <= colon) {
 347                /* Case (2) */
 348                c->username = url_decode_mem(cp, at - cp);
 349                host = at + 1;
 350        } else {
 351                /* Case (3) */
 352                c->username = url_decode_mem(cp, colon - cp);
 353                c->password = url_decode_mem(colon + 1, at - (colon + 1));
 354                host = at + 1;
 355        }
 356
 357        if (proto_end - url > 0)
 358                c->protocol = xmemdupz(url, proto_end - url);
 359        if (slash - host > 0)
 360                c->host = url_decode_mem(host, slash - host);
 361        /* Trim leading and trailing slashes from path */
 362        while (*slash == '/')
 363                slash++;
 364        if (*slash) {
 365                char *p;
 366                c->path = url_decode(slash);
 367                p = c->path + strlen(c->path) - 1;
 368                while (p > c->path && *p == '/')
 369                        *p-- = '\0';
 370        }
 371}