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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * - GNOME Keyring API handling originally written by John Szakmeister
23 * - ported to credential helper API by Philipp A. Hartmann
32 #include <gnome-keyring.h>
33 #include <gnome-keyring-memory.h>
36 * This credential struct and API is simplified from git's credential.{h,c}
48 #define CREDENTIAL_INIT \
49 { NULL,NULL,0,NULL,NULL,NULL }
51 typedef int (*credential_op_cb)(struct credential*);
53 struct credential_operation
59 #define CREDENTIAL_OP_END \
62 /* ---------------- common helper functions ----------------- */
64 static inline void warning(const char *fmt, ...)
69 fprintf(stderr, "warning: ");
70 vfprintf(stderr, fmt, ap);
71 fprintf(stderr, "\n" );
75 static inline void error(const char *fmt, ...)
80 fprintf(stderr, "error: ");
81 vfprintf(stderr, fmt, ap);
82 fprintf(stderr, "\n" );
86 static inline void die_errno(int err)
88 error("%s", strerror(err));
92 static inline char *xstrdup(const char *str)
94 char *ret = strdup(str);
101 /* ----------------- GNOME Keyring functions ----------------- */
103 /* create a special keyring option string, if path is given */
104 static char* keyring_object(struct credential *c)
110 return g_strdup_printf("%s:%hd/%s", c->host, c->port, c->path);
112 return g_strdup_printf("%s/%s", c->host, c->path);
115 static int keyring_get(struct credential *c)
119 GnomeKeyringNetworkPasswordData *password_data;
120 GnomeKeyringResult result;
122 if (!c->protocol || !(c->host || c->path))
125 object = keyring_object(c);
127 result = gnome_keyring_find_network_password_sync(
139 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
142 if (result == GNOME_KEYRING_RESULT_CANCELLED)
145 if (result != GNOME_KEYRING_RESULT_OK) {
146 error("%s",gnome_keyring_result_to_message(result));
150 /* pick the first one from the list */
151 password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
153 gnome_keyring_memory_free(c->password);
154 c->password = gnome_keyring_memory_strdup(password_data->password);
157 c->username = xstrdup(password_data->user);
159 gnome_keyring_network_password_list_free(entries);
165 static int keyring_store(struct credential *c)
171 * Sanity check that what we are storing is actually sensible.
172 * In particular, we can't make a URL without a protocol field.
173 * Without either a host or pathname (depending on the scheme),
174 * we have no primary key. And without a username and password,
175 * we are not actually storing a credential.
177 if (!c->protocol || !(c->host || c->path) ||
178 !c->username || !c->password)
181 object = keyring_object(c);
183 gnome_keyring_set_network_password_sync(
184 GNOME_KEYRING_DEFAULT,
199 static int keyring_erase(struct credential *c)
203 GnomeKeyringNetworkPasswordData *password_data;
204 GnomeKeyringResult result;
207 * Sanity check that we actually have something to match
208 * against. The input we get is a restrictive pattern,
209 * so technically a blank credential means "erase everything".
210 * But it is too easy to accidentally send this, since it is equivalent
211 * to empty input. So explicitly disallow it, and require that the
212 * pattern have some actual content to match.
214 if (!c->protocol && !c->host && !c->path && !c->username)
217 object = keyring_object(c);
219 result = gnome_keyring_find_network_password_sync(
231 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
234 if (result == GNOME_KEYRING_RESULT_CANCELLED)
237 if (result != GNOME_KEYRING_RESULT_OK)
239 error("%s",gnome_keyring_result_to_message(result));
243 /* pick the first one from the list (delete all matches?) */
244 password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
246 result = gnome_keyring_item_delete_sync(
247 password_data->keyring, password_data->item_id);
249 gnome_keyring_network_password_list_free(entries);
251 if (result != GNOME_KEYRING_RESULT_OK)
253 error("%s",gnome_keyring_result_to_message(result));
261 * Table with helper operation callbacks, used by generic
262 * credential helper main function.
264 static struct credential_operation const credential_helper_ops[] =
266 { "get", keyring_get },
267 { "store", keyring_store },
268 { "erase", keyring_erase },
272 /* ------------------ credential functions ------------------ */
274 static void credential_init(struct credential *c)
276 memset(c, 0, sizeof(*c));
279 static void credential_clear(struct credential *c)
285 gnome_keyring_memory_free(c->password);
290 static int credential_read(struct credential *c)
297 while (fgets(buf, sizeof(buf), stdin))
299 line_len = strlen(buf);
301 if (line_len && buf[line_len-1] == '\n')
302 buf[--line_len]='\0';
307 value = strchr(buf,'=');
309 warning("invalid credential line: %s", key);
314 if (!strcmp(key, "protocol")) {
316 c->protocol = xstrdup(value);
317 } else if (!strcmp(key, "host")) {
319 c->host = xstrdup(value);
320 value = strrchr(c->host,':');
323 c->port = atoi(value);
325 } else if (!strcmp(key, "path")) {
327 c->path = xstrdup(value);
328 } else if (!strcmp(key, "username")) {
330 c->username = xstrdup(value);
331 } else if (!strcmp(key, "password")) {
332 gnome_keyring_memory_free(c->password);
333 c->password = gnome_keyring_memory_strdup(value);
334 while (*value) *value++ = '\0';
337 * Ignore other lines; we don't know what they mean, but
338 * this future-proofs us when later versions of git do
339 * learn new lines, and the helpers are updated to match.
345 static void credential_write_item(FILE *fp, const char *key, const char *value)
349 fprintf(fp, "%s=%s\n", key, value);
352 static void credential_write(const struct credential *c)
354 /* only write username/password, if set */
355 credential_write_item(stdout, "username", c->username);
356 credential_write_item(stdout, "password", c->password);
359 static void usage(const char *name)
361 struct credential_operation const *try_op = credential_helper_ops;
362 const char *basename = strrchr(name,'/');
364 basename = (basename) ? basename + 1 : name;
365 fprintf(stderr, "usage: %s <", basename);
366 while (try_op->name) {
367 fprintf(stderr,"%s",(try_op++)->name);
369 fprintf(stderr,"%s","|");
371 fprintf(stderr,"%s",">\n");
374 int main(int argc, char *argv[])
376 int ret = EXIT_SUCCESS;
378 struct credential_operation const *try_op = credential_helper_ops;
379 struct credential cred = CREDENTIAL_INIT;
386 g_set_application_name("Git Credential Helper");
388 /* lookup operation callback */
389 while (try_op->name && strcmp(argv[1], try_op->name))
392 /* unsupported operation given -- ignore silently */
393 if (!try_op->name || !try_op->op)
396 ret = credential_read(&cred);
400 /* perform credential operation */
401 ret = (*try_op->op)(&cred);
403 credential_write(&cred);
406 credential_clear(&cred);