-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-credential-gnomekeyring.c
173 lines (151 loc) · 3.73 KB
/
git-credential-gnomekeyring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <gnome-keyring.h>
static GnomeKeyringPasswordSchema git_schema = {
GNOME_KEYRING_ITEM_GENERIC_SECRET,
{
{ "protocol", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ "host", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ "path", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ "username", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
{ NULL, 0 },
}
};
typedef struct git_credential {
gchar *protocol;
gchar *host;
gchar *path;
gchar *username;
gchar *password;
} git_credential_t;
static void
error(const char *err, ...)
{
char msg[4096];
va_list params;
va_start(params, err);
vsnprintf(msg, sizeof(msg), err, params);
fprintf(stderr, "%s\n", msg);
va_end(params);
}
static void
get_password(git_credential_t *cred)
{
GnomeKeyringResult keyres;
gchar *pass = NULL;
keyres = gnome_keyring_find_password_sync(&git_schema,
&pass,
"protocol", cred->protocol,
"host", cred->host,
"path", cred->path,
"username", cred->username,
NULL);
if (keyres != GNOME_KEYRING_RESULT_OK) {
return;
}
g_printf("password=%s\n", pass);
gnome_keyring_free_password(pass);
}
static void
store_password(git_credential_t *cred)
{
gchar desc[1024];
GnomeKeyringResult keyres;
/* Only store complete credentials */
if (!cred->protocol || !cred->host ||
!cred->username || !cred->password)
return;
g_snprintf(desc, sizeof(desc), "Git %s", cred->host);
keyres = gnome_keyring_store_password_sync(&git_schema,
GNOME_KEYRING_DEFAULT,
desc,
cred->password,
"protocol", cred->protocol,
"host", cred->host,
"path", cred->path,
"username", cred->username,
NULL);
if (keyres != GNOME_KEYRING_RESULT_OK) {
error("failed to store password");
return;
}
}
static void
erase_password(git_credential_t *cred)
{
GnomeKeyringResult keyres;
keyres = gnome_keyring_delete_password_sync(&git_schema,
"protocol", cred->protocol,
"host", cred->host,
"path", cred->path,
"username", cred->username,
NULL);
if (keyres != GNOME_KEYRING_RESULT_OK) {
error("failed to delete password");
return;
}
}
static int
read_credential(git_credential_t *cred)
{
char buf[1024];
while (fgets(buf, sizeof(buf), stdin)) {
char *v;
if (strcmp(buf, "\n") == 0)
break;
buf[strlen(buf) - 1] = '\0';
v = strchr(buf, '=');
if (!v) {
error("bad input: %s", buf);
return -1;
}
*v++ = '\0';
#define SET_CRED_ATTR(name) do { \
if (strcmp(buf, #name) == 0) { \
cred->name = g_strdup(v); \
} \
} while (0)
SET_CRED_ATTR(protocol);
SET_CRED_ATTR(host);
SET_CRED_ATTR(path);
SET_CRED_ATTR(username);
SET_CRED_ATTR(password);
}
return 0;
}
static void
clear_credential(git_credential_t *cred)
{
if (cred->protocol) g_free(cred->protocol);
if (cred->host) g_free(cred->host);
if (cred->path) g_free(cred->path);
if (cred->username) g_free(cred->username);
if (cred->password) gnome_keyring_free_password(cred->password);
}
int
main(int argc, const char **argv)
{
git_credential_t cred = {0};
if (argc < 2) {
error("Usage: git credential-gnomekeyring <get|store|erase>");
return 1;
}
if (read_credential(&cred)) {
clear_credential(&cred);
return 1;
}
if (strcmp(argv[1], "get") == 0) {
get_password(&cred);
}
else if (strcmp(argv[1], "store") == 0) {
store_password(&cred);
}
else if (strcmp(argv[1], "erase") == 0) {
erase_password(&cred);
}
clear_credential(&cred);
return 0;
}