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>
35 * This credential struct and API is simplified from git's credential.{h,c}
47 #define CREDENTIAL_INIT \
48 { NULL,NULL,0,NULL,NULL,NULL }
50 typedef int (*credential_op_cb)(struct credential*);
52 struct credential_operation
58 #define CREDENTIAL_OP_END \
61 /* ---------------- common helper functions ----------------- */
63 static inline void free_password(char *password)
69 while (*c) *c++ = '\0';
73 static inline void warning(const char *fmt, ...)
78 fprintf(stderr, "warning: ");
79 vfprintf(stderr, fmt, ap);
80 fprintf(stderr, "\n" );
84 static inline void error(const char *fmt, ...)
89 fprintf(stderr, "error: ");
90 vfprintf(stderr, fmt, ap);
91 fprintf(stderr, "\n" );
95 static inline void die_errno(int err)
97 error("%s", strerror(err));
101 static inline char *xstrdup(const char *str)
103 char *ret = strdup(str);
110 /* ----------------- GNOME Keyring functions ----------------- */
112 /* create a special keyring option string, if path is given */
113 static char* keyring_object(struct credential *c)
120 object = (char*) malloc(strlen(c->host)+strlen(c->path)+8);
125 sprintf(object,"%s:%hd/%s",c->host,c->port,c->path);
127 sprintf(object,"%s/%s",c->host,c->path);
132 static int keyring_get(struct credential *c)
136 GnomeKeyringNetworkPasswordData *password_data;
137 GnomeKeyringResult result;
139 if (!c->protocol || !(c->host || c->path))
142 object = keyring_object(c);
144 result = gnome_keyring_find_network_password_sync(
156 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
159 if (result == GNOME_KEYRING_RESULT_CANCELLED)
162 if (result != GNOME_KEYRING_RESULT_OK) {
163 error("%s",gnome_keyring_result_to_message(result));
167 /* pick the first one from the list */
168 password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
170 free_password(c->password);
171 c->password = xstrdup(password_data->password);
174 c->username = xstrdup(password_data->user);
176 gnome_keyring_network_password_list_free(entries);
182 static int keyring_store(struct credential *c)
188 * Sanity check that what we are storing is actually sensible.
189 * In particular, we can't make a URL without a protocol field.
190 * Without either a host or pathname (depending on the scheme),
191 * we have no primary key. And without a username and password,
192 * we are not actually storing a credential.
194 if (!c->protocol || !(c->host || c->path) ||
195 !c->username || !c->password)
198 object = keyring_object(c);
200 gnome_keyring_set_network_password_sync(
201 GNOME_KEYRING_DEFAULT,
216 static int keyring_erase(struct credential *c)
220 GnomeKeyringNetworkPasswordData *password_data;
221 GnomeKeyringResult result;
224 * Sanity check that we actually have something to match
225 * against. The input we get is a restrictive pattern,
226 * so technically a blank credential means "erase everything".
227 * But it is too easy to accidentally send this, since it is equivalent
228 * to empty input. So explicitly disallow it, and require that the
229 * pattern have some actual content to match.
231 if (!c->protocol && !c->host && !c->path && !c->username)
234 object = keyring_object(c);
236 result = gnome_keyring_find_network_password_sync(
248 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
251 if (result == GNOME_KEYRING_RESULT_CANCELLED)
254 if (result != GNOME_KEYRING_RESULT_OK)
256 error("%s",gnome_keyring_result_to_message(result));
260 /* pick the first one from the list (delete all matches?) */
261 password_data = (GnomeKeyringNetworkPasswordData *) entries->data;
263 result = gnome_keyring_item_delete_sync(
264 password_data->keyring, password_data->item_id);
266 gnome_keyring_network_password_list_free(entries);
268 if (result != GNOME_KEYRING_RESULT_OK)
270 error("%s",gnome_keyring_result_to_message(result));
278 * Table with helper operation callbacks, used by generic
279 * credential helper main function.
281 static struct credential_operation const credential_helper_ops[] =
283 { "get", keyring_get },
284 { "store", keyring_store },
285 { "erase", keyring_erase },
289 /* ------------------ credential functions ------------------ */
291 static void credential_init(struct credential *c)
293 memset(c, 0, sizeof(*c));
296 static void credential_clear(struct credential *c)
302 free_password(c->password);
307 static int credential_read(struct credential *c)
314 while (fgets(buf, sizeof(buf), stdin))
316 line_len = strlen(buf);
318 if (line_len && buf[line_len-1] == '\n')
319 buf[--line_len]='\0';
324 value = strchr(buf,'=');
326 warning("invalid credential line: %s", key);
331 if (!strcmp(key, "protocol")) {
333 c->protocol = xstrdup(value);
334 } else if (!strcmp(key, "host")) {
336 c->host = xstrdup(value);
337 value = strrchr(c->host,':');
340 c->port = atoi(value);
342 } else if (!strcmp(key, "path")) {
344 c->path = xstrdup(value);
345 } else if (!strcmp(key, "username")) {
347 c->username = xstrdup(value);
348 } else if (!strcmp(key, "password")) {
349 free_password(c->password);
350 c->password = xstrdup(value);
351 while (*value) *value++ = '\0';
354 * Ignore other lines; we don't know what they mean, but
355 * this future-proofs us when later versions of git do
356 * learn new lines, and the helpers are updated to match.
362 static void credential_write_item(FILE *fp, const char *key, const char *value)
366 fprintf(fp, "%s=%s\n", key, value);
369 static void credential_write(const struct credential *c)
371 /* only write username/password, if set */
372 credential_write_item(stdout, "username", c->username);
373 credential_write_item(stdout, "password", c->password);
376 static void usage(const char *name)
378 struct credential_operation const *try_op = credential_helper_ops;
379 const char *basename = strrchr(name,'/');
381 basename = (basename) ? basename + 1 : name;
382 fprintf(stderr, "usage: %s <", basename);
383 while (try_op->name) {
384 fprintf(stderr,"%s",(try_op++)->name);
386 fprintf(stderr,"%s","|");
388 fprintf(stderr,"%s",">\n");
391 int main(int argc, char *argv[])
393 int ret = EXIT_SUCCESS;
395 struct credential_operation const *try_op = credential_helper_ops;
396 struct credential cred = CREDENTIAL_INIT;
403 g_set_application_name("Git Credential Helper");
405 /* lookup operation callback */
406 while (try_op->name && strcmp(argv[1], try_op->name))
409 /* unsupported operation given -- ignore silently */
410 if (!try_op->name || !try_op->op)
413 ret = credential_read(&cred);
417 /* perform credential operation */
418 ret = (*try_op->op)(&cred);
420 credential_write(&cred);
423 credential_clear(&cred);