2 * Copyright (C) 2011 John Szakmeister <john@szakmeister.net>
3 * 2012 Philipp A. Hartmann <pah@qo.cx>
4 * 2016 Mantas Mikulėnas <grawity@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * - GNOME Keyring API handling originally written by John Szakmeister
24 * - ported to credential helper API by Philipp A. Hartmann
31 #include <libsecret/secret.h>
34 * This credential struct and API is simplified from git's credential.{h,c}
45 #define CREDENTIAL_INIT { NULL, NULL, 0, NULL, NULL, NULL }
47 typedef int (*credential_op_cb)(struct credential *);
49 struct credential_operation {
54 #define CREDENTIAL_OP_END { NULL, NULL }
56 /* ----------------- Secret Service functions ----------------- */
58 static char *make_label(struct credential *c)
61 return g_strdup_printf("Git: %s://%s:%hu/%s",
62 c->protocol, c->host, c->port, c->path ? c->path : "");
64 return g_strdup_printf("Git: %s://%s/%s",
65 c->protocol, c->host, c->path ? c->path : "");
68 static GHashTable *make_attr_list(struct credential *c)
70 GHashTable *al = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
73 g_hash_table_insert(al, "user", g_strdup(c->username));
75 g_hash_table_insert(al, "protocol", g_strdup(c->protocol));
77 g_hash_table_insert(al, "server", g_strdup(c->host));
79 g_hash_table_insert(al, "port", g_strdup_printf("%hu", c->port));
81 g_hash_table_insert(al, "object", g_strdup(c->path));
86 static int keyring_get(struct credential *c)
88 SecretService *service = NULL;
89 GHashTable *attributes = NULL;
93 if (!c->protocol || !(c->host || c->path))
96 service = secret_service_get_sync(0, NULL, &error);
98 g_critical("could not connect to Secret Service: %s", error->message);
103 attributes = make_attr_list(c);
104 items = secret_service_search_sync(service,
105 SECRET_SCHEMA_COMPAT_NETWORK,
107 SECRET_SEARCH_LOAD_SECRETS,
110 g_hash_table_unref(attributes);
112 g_critical("lookup failed: %s", error->message);
123 secret = secret_item_get_secret(item);
124 attributes = secret_item_get_attributes(item);
126 s = g_hash_table_lookup(attributes, "user");
129 c->username = g_strdup(s);
132 s = secret_value_get_text(secret);
135 c->password = g_strdup(s);
138 g_hash_table_unref(attributes);
139 secret_value_unref(secret);
140 g_list_free_full(items, g_object_unref);
147 static int keyring_store(struct credential *c)
150 GHashTable *attributes = NULL;
151 GError *error = NULL;
154 * Sanity check that what we are storing is actually sensible.
155 * In particular, we can't make a URL without a protocol field.
156 * Without either a host or pathname (depending on the scheme),
157 * we have no primary key. And without a username and password,
158 * we are not actually storing a credential.
160 if (!c->protocol || !(c->host || c->path) ||
161 !c->username || !c->password)
164 label = make_label(c);
165 attributes = make_attr_list(c);
166 secret_password_storev_sync(SECRET_SCHEMA_COMPAT_NETWORK,
174 g_hash_table_unref(attributes);
177 g_critical("store failed: %s", error->message);
185 static int keyring_erase(struct credential *c)
187 GHashTable *attributes = NULL;
188 GError *error = NULL;
191 * Sanity check that we actually have something to match
192 * against. The input we get is a restrictive pattern,
193 * so technically a blank credential means "erase everything".
194 * But it is too easy to accidentally send this, since it is equivalent
195 * to empty input. So explicitly disallow it, and require that the
196 * pattern have some actual content to match.
198 if (!c->protocol && !c->host && !c->path && !c->username)
201 attributes = make_attr_list(c);
202 secret_password_clearv_sync(SECRET_SCHEMA_COMPAT_NETWORK,
206 g_hash_table_unref(attributes);
209 g_critical("erase failed: %s", error->message);
218 * Table with helper operation callbacks, used by generic
219 * credential helper main function.
221 static struct credential_operation const credential_helper_ops[] = {
222 { "get", keyring_get },
223 { "store", keyring_store },
224 { "erase", keyring_erase },
228 /* ------------------ credential functions ------------------ */
230 static void credential_init(struct credential *c)
232 memset(c, 0, sizeof(*c));
235 static void credential_clear(struct credential *c)
246 static int credential_read(struct credential *c)
253 key = buf = g_malloc(1024);
255 while (fgets(buf, 1024, stdin)) {
256 line_len = strlen(buf);
258 if (line_len && buf[line_len-1] == '\n')
259 buf[--line_len] = '\0';
264 value = strchr(buf, '=');
266 g_warning("invalid credential line: %s", key);
272 if (!strcmp(key, "protocol")) {
274 c->protocol = g_strdup(value);
275 } else if (!strcmp(key, "host")) {
277 c->host = g_strdup(value);
278 value = strrchr(c->host, ':');
281 c->port = atoi(value);
283 } else if (!strcmp(key, "path")) {
285 c->path = g_strdup(value);
286 } else if (!strcmp(key, "username")) {
288 c->username = g_strdup(value);
289 } else if (!strcmp(key, "password")) {
291 c->password = g_strdup(value);
296 * Ignore other lines; we don't know what they mean, but
297 * this future-proofs us when later versions of git do
298 * learn new lines, and the helpers are updated to match.
307 static void credential_write_item(FILE *fp, const char *key, const char *value)
311 fprintf(fp, "%s=%s\n", key, value);
314 static void credential_write(const struct credential *c)
316 /* only write username/password, if set */
317 credential_write_item(stdout, "username", c->username);
318 credential_write_item(stdout, "password", c->password);
321 static void usage(const char *name)
323 struct credential_operation const *try_op = credential_helper_ops;
324 const char *basename = strrchr(name, '/');
326 basename = (basename) ? basename + 1 : name;
327 fprintf(stderr, "usage: %s <", basename);
328 while (try_op->name) {
329 fprintf(stderr, "%s", (try_op++)->name);
331 fprintf(stderr, "%s", "|");
333 fprintf(stderr, "%s", ">\n");
336 int main(int argc, char *argv[])
338 int ret = EXIT_SUCCESS;
340 struct credential_operation const *try_op = credential_helper_ops;
341 struct credential cred = CREDENTIAL_INIT;
348 g_set_application_name("Git Credential Helper");
350 /* lookup operation callback */
351 while (try_op->name && strcmp(argv[1], try_op->name))
354 /* unsupported operation given -- ignore silently */
355 if (!try_op->name || !try_op->op)
358 ret = credential_read(&cred);
362 /* perform credential operation */
363 ret = (*try_op->op)(&cred);
365 credential_write(&cred);
368 credential_clear(&cred);