-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-credential-password-store.c
213 lines (179 loc) · 5.22 KB
/
git-credential-password-store.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ftw.h>
#include <ctype.h>
#define BUF_SIZE 1024
static char buffer[BUF_SIZE];
static char *store_root;
static void die(const char *err)
{
fprintf(stderr, "%s\n", err);
exit(1);
}
static void *xstrdup(const char *str)
{
void *ret = strdup(str);
if (!ret) die("Out of memory");
return ret;
}
static void *xstrconcat(const char *str1, const char *str2)
{
void *ret = malloc(strlen(str1) + strlen(str2));
if (!ret) die("Out of memory");
strcpy(ret, str1);
strcpy(ret + strlen(str1), str2);
return ret;
}
static char *xstrstrip(char *str)
{
int len = strlen(str);
char *ret = str;
while(*ret && isspace(*ret)) { ++ret; --len; }
if(ret!=str) memmove(str, ret, len + 1);
while(len && isspace(str[len-1])) str[--len]='\0';
return str;
}
//-----------------------------------------------------------------------------
//
// CREDENTIALS
//
// These are passed around as key=value lines in stdin/stdout.
//
//-----------------------------------------------------------------------------
struct Credentials {
char *protocol;
char *username;
char *host;
char *path;
char *password;
};
static struct Credentials* git_credentials;
static struct Credentials* credentials_new()
{
void *ret = calloc(1, sizeof(struct Credentials));
if(!ret) die("Out of memory.");
return ret;
}
static void credentials_free(struct Credentials* creds)
{
if(!creds) return;
free(creds->protocol);
free(creds->username);
free(creds->host);
free(creds->path);
free(creds->password);
free(creds);
}
static void credentials_set(struct Credentials *creds,
char *prop,
char *value)
{
if(!strcasecmp("host", prop)) {
free(creds->host);
char *sep = strchr(value, ':');
if(sep) *sep = '\0';
creds->host = xstrdup(value);
return;
}
char **dest = NULL;
if(!strcasecmp("protocol",prop)) dest = &(creds->protocol);
else if(!strcasecmp("password",prop)) dest = &(creds->password);
else if(!strcasecmp("path",prop)) dest = &(creds->path);
else if(!strcasecmp("username",prop)
|| !strcasecmp("user",prop)
|| !strcasecmp("login",prop))
dest = &(creds->username);
if(dest) {
free(*dest);
*dest = xstrstrip(xstrdup(value));
}
}
static void credentials_line(
struct Credentials *creds,
char *line,
char delim)
{
//
// This is not very robust parsing, but it is not any worse than
// the parsing in the osxkeychain credentials helper.
//
if(!line || *line == '\n') return;
line[strlen(line)-1] = '\0';
char *sep = strchr(line, delim);
if(!sep) return;
*sep++ = '\0';
credentials_set(creds, line, sep);
}
static void credentials_git_read()
{
while(fgets(buffer, sizeof(buffer), stdin))
credentials_line(git_credentials, buffer, '=');
}
static void credentials_pass_read(struct Credentials *creds,
const char *entry)
{
char *cmd = xstrconcat("pass ", entry);
FILE* stream = popen(cmd, "r");
if(!stream) goto finish;
char line[BUF_SIZE];
for(int first=1; fgets(line, sizeof(line), stream); first=0) {
if(first) credentials_set(creds, "password", line);
else credentials_line(creds, line, ':');
}
finish:
if(stream) pclose(stream);
free(cmd);
}
//-----------------------------------------------------------------------------
//
// GET
//
//-----------------------------------------------------------------------------
static int got_one = 0;
int do_get(const char *filepath,
const struct stat *info,
const int ftw_type,
struct FTW *pathinfo)
{
if(got_one || ftw_type != FTW_F) return 0;
int len = strlen(filepath);
if(len < 5) return 0;
if(strcmp(filepath + len - 4, ".gpg")) return 0;
if(strncmp(store_root, filepath, strlen(store_root))) return 0;
char *key = xstrdup(filepath);
key[strlen(key)-4] = '\0';
memmove(key, key+strlen(store_root)+1, strlen(key) - strlen(store_root));
if(!strcasestr(key, git_credentials->host)) return 0;
struct Credentials *candidate = credentials_new();
credentials_pass_read(candidate, key);
if(candidate->password
&& candidate->username
&& (!git_credentials->username
|| !strcmp(candidate->username, git_credentials->username)))
{
printf("password=%s\n", candidate->password);
if(!git_credentials->username) printf("username=%s\n", candidate->username);
got_one = 1;
}
free(key);
credentials_free(candidate);
return 0;
}
int main(int argc, char **argv)
{
if(!argv[1])
die("usage: git credential-password-store get");
char *env;
if((env = getenv("PASSWORD_STORE_DIR"))) store_root = xstrdup(env);
else store_root = xstrconcat(getenv("HOME"), "/.password-store");
git_credentials = credentials_new();
if(!strcmp(argv[1],"get")) {
credentials_git_read();
if(git_credentials->host)
nftw(store_root, do_get, 1, FTW_PHYS);
}
credentials_free(git_credentials);
free(store_root);
}