2 * Copyright (C) 2011 John Szakmeister <john@szakmeister.net>
3 * 2012 Philipp A. Hartmann <pah@qo.cx>
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.
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.
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/>.
21 * - GNOME Keyring API handling originally written by John Szakmeister
22 * - ported to credential helper API by Philipp A. Hartmann
29 #include <gnome-keyring.h>
31 #ifdef GNOME_KEYRING_DEFAULT
33 /* Modern gnome-keyring */
35 #include <gnome-keyring-memory.h>
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.
48 #define GNOME_KEYRING_DEFAULT NULL
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.
56 #define GNOME_KEYRING_RESULT_NO_MATCH GNOME_KEYRING_RESULT_DENIED
58 #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
62 static const char *gnome_keyring_result_to_message(GnomeKeyringResult result)
65 case GNOME_KEYRING_RESULT_OK:
67 case GNOME_KEYRING_RESULT_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:
79 case GNOME_KEYRING_RESULT_CANCELLED:
81 case GNOME_KEYRING_RESULT_ALREADY_EXISTS:
82 return "Already Exists";
84 return "Unknown Error";
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 ??
93 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 8
95 static void gnome_keyring_done_cb(GnomeKeyringResult result, gpointer user_data)
97 gpointer *data = (gpointer *)user_data;
98 int *done = (int *)data[0];
99 GnomeKeyringResult *r = (GnomeKeyringResult *)data[1];
105 static void wait_for_request_completion(int *done)
107 GMainContext *mc = g_main_context_default();
109 g_main_context_iteration(mc, TRUE);
112 static GnomeKeyringResult gnome_keyring_item_delete_sync(const char *keyring, guint32 id)
115 GnomeKeyringResult result;
116 gpointer data[] = { &done, &result };
118 gnome_keyring_item_delete(keyring, id, gnome_keyring_done_cb, data,
121 wait_for_request_completion(&done);
130 * This credential struct and API is simplified from git's credential.{h,c}
141 #define CREDENTIAL_INIT { NULL, NULL, 0, NULL, NULL, NULL }
143 typedef int (*credential_op_cb)(struct credential *);
145 struct credential_operation {
150 #define CREDENTIAL_OP_END { NULL, NULL }
152 /* ----------------- GNOME Keyring functions ----------------- */
154 /* create a special keyring option string, if path is given */
155 static char *keyring_object(struct credential *c)
161 return g_strdup_printf("%s:%hd/%s", c->host, c->port, c->path);
163 return g_strdup_printf("%s/%s", c->host, c->path);
166 static int keyring_get(struct credential *c)
170 GnomeKeyringNetworkPasswordData *password_data;
171 GnomeKeyringResult result;
173 if (!c->protocol || !(c->host || c->path))
176 object = keyring_object(c);
178 result = gnome_keyring_find_network_password_sync(
190 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
193 if (result == GNOME_KEYRING_RESULT_CANCELLED)
196 if (result != GNOME_KEYRING_RESULT_OK) {
197 g_critical("%s", gnome_keyring_result_to_message(result));
201 /* pick the first one from the list */
202 password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
204 gnome_keyring_memory_free(c->password);
205 c->password = gnome_keyring_memory_strdup(password_data->password);
208 c->username = g_strdup(password_data->user);
210 gnome_keyring_network_password_list_free(entries);
216 static int keyring_store(struct credential *c)
220 GnomeKeyringResult result;
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.
229 if (!c->protocol || !(c->host || c->path) ||
230 !c->username || !c->password)
233 object = keyring_object(c);
235 result = gnome_keyring_set_network_password_sync(
236 GNOME_KEYRING_DEFAULT,
249 if (result != GNOME_KEYRING_RESULT_OK &&
250 result != GNOME_KEYRING_RESULT_CANCELLED) {
251 g_critical("%s", gnome_keyring_result_to_message(result));
258 static int keyring_erase(struct credential *c)
262 GnomeKeyringNetworkPasswordData *password_data;
263 GnomeKeyringResult result;
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.
273 if (!c->protocol && !c->host && !c->path && !c->username)
276 object = keyring_object(c);
278 result = gnome_keyring_find_network_password_sync(
290 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
293 if (result == GNOME_KEYRING_RESULT_CANCELLED)
296 if (result != GNOME_KEYRING_RESULT_OK) {
297 g_critical("%s", gnome_keyring_result_to_message(result));
301 /* pick the first one from the list (delete all matches?) */
302 password_data = (GnomeKeyringNetworkPasswordData *)entries->data;
304 result = gnome_keyring_item_delete_sync(
305 password_data->keyring, password_data->item_id);
307 gnome_keyring_network_password_list_free(entries);
309 if (result != GNOME_KEYRING_RESULT_OK) {
310 g_critical("%s", gnome_keyring_result_to_message(result));
318 * Table with helper operation callbacks, used by generic
319 * credential helper main function.
321 static struct credential_operation const credential_helper_ops[] = {
322 { "get", keyring_get },
323 { "store", keyring_store },
324 { "erase", keyring_erase },
328 /* ------------------ credential functions ------------------ */
330 static void credential_init(struct credential *c)
332 memset(c, 0, sizeof(*c));
335 static void credential_clear(struct credential *c)
341 gnome_keyring_memory_free(c->password);
346 static int credential_read(struct credential *c)
353 key = buf = gnome_keyring_memory_alloc(1024);
355 while (fgets(buf, 1024, stdin)) {
356 line_len = strlen(buf);
358 if (line_len && buf[line_len-1] == '\n')
359 buf[--line_len] = '\0';
364 value = strchr(buf, '=');
366 g_warning("invalid credential line: %s", key);
367 gnome_keyring_memory_free(buf);
372 if (!strcmp(key, "protocol")) {
374 c->protocol = g_strdup(value);
375 } else if (!strcmp(key, "host")) {
377 c->host = g_strdup(value);
378 value = strrchr(c->host, ':');
381 c->port = atoi(value);
383 } else if (!strcmp(key, "path")) {
385 c->path = g_strdup(value);
386 } else if (!strcmp(key, "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);
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.
402 gnome_keyring_memory_free(buf);
407 static void credential_write_item(FILE *fp, const char *key, const char *value)
411 fprintf(fp, "%s=%s\n", key, value);
414 static void credential_write(const struct credential *c)
416 /* only write username/password, if set */
417 credential_write_item(stdout, "username", c->username);
418 credential_write_item(stdout, "password", c->password);
421 static void usage(const char *name)
423 struct credential_operation const *try_op = credential_helper_ops;
424 const char *basename = strrchr(name, '/');
426 basename = (basename) ? basename + 1 : name;
427 fprintf(stderr, "usage: %s <", basename);
428 while (try_op->name) {
429 fprintf(stderr, "%s", (try_op++)->name);
431 fprintf(stderr, "%s", "|");
433 fprintf(stderr, "%s", ">\n");
436 int main(int argc, char *argv[])
438 int ret = EXIT_SUCCESS;
440 struct credential_operation const *try_op = credential_helper_ops;
441 struct credential cred = CREDENTIAL_INIT;
448 g_set_application_name("Git Credential Helper");
450 /* lookup operation callback */
451 while (try_op->name && strcmp(argv[1], try_op->name))
454 /* unsupported operation given -- ignore silently */
455 if (!try_op->name || !try_op->op)
458 ret = credential_read(&cred);
462 /* perform credential operation */
463 ret = (*try_op->op)(&cred);
465 credential_write(&cred);
468 credential_clear(&cred);