summaryrefslogtreecommitdiff
path: root/src/libgit2/transports/credential.c
blob: 6e00b02824373f4f5166e49fa2ba34be31938fd8 (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"

#include "git2/credential.h"
#include "git2/sys/credential.h"
#include "git2/credential_helpers.h"

static int git_credential_ssh_key_type_new(
	git_credential **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase,
	git_credential_t credtype);

int git_credential_has_username(git_credential *cred)
{
	if (cred->credtype == GIT_CREDENTIAL_DEFAULT)
		return 0;

	return 1;
}

const char *git_credential_get_username(git_credential *cred)
{
	switch (cred->credtype) {
	case GIT_CREDENTIAL_USERNAME:
	{
		git_credential_username *c = (git_credential_username *) cred;
		return c->username;
	}
	case GIT_CREDENTIAL_USERPASS_PLAINTEXT:
	{
		git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *) cred;
		return c->username;
	}
	case GIT_CREDENTIAL_SSH_KEY:
	case GIT_CREDENTIAL_SSH_MEMORY:
	{
		git_credential_ssh_key *c = (git_credential_ssh_key *) cred;
		return c->username;
	}
	case GIT_CREDENTIAL_SSH_CUSTOM:
	{
		git_credential_ssh_custom *c = (git_credential_ssh_custom *) cred;
		return c->username;
	}
	case GIT_CREDENTIAL_SSH_INTERACTIVE:
	{
		git_credential_ssh_interactive *c = (git_credential_ssh_interactive *) cred;
		return c->username;
	}

	default:
		return NULL;
	}
}

static void plaintext_free(struct git_credential *cred)
{
	git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred;

	git__free(c->username);

	/* Zero the memory which previously held the password */
	if (c->password) {
		size_t pass_len = strlen(c->password);
		git__memzero(c->password, pass_len);
		git__free(c->password);
	}

	git__free(c);
}

int git_credential_userpass_plaintext_new(
	git_credential **cred,
	const char *username,
	const char *password)
{
	git_credential_userpass_plaintext *c;

	GIT_ASSERT_ARG(cred);
	GIT_ASSERT_ARG(username);
	GIT_ASSERT_ARG(password);

	c = git__malloc(sizeof(git_credential_userpass_plaintext));
	GIT_ERROR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDENTIAL_USERPASS_PLAINTEXT;
	c->parent.free = plaintext_free;
	c->username = git__strdup(username);

	if (!c->username) {
		git__free(c);
		return -1;
	}

	c->password = git__strdup(password);

	if (!c->password) {
		git__free(c->username);
		git__free(c);
		return -1;
	}

	*cred = &c->parent;
	return 0;
}

static void ssh_key_free(struct git_credential *cred)
{
	git_credential_ssh_key *c =
		(git_credential_ssh_key *)cred;

	git__free(c->username);

	if (c->privatekey) {
		/* Zero the memory which previously held the private key */
		size_t key_len = strlen(c->privatekey);
		git__memzero(c->privatekey, key_len);
		git__free(c->privatekey);
	}

	if (c->passphrase) {
		/* Zero the memory which previously held the passphrase */
		size_t pass_len = strlen(c->passphrase);
		git__memzero(c->passphrase, pass_len);
		git__free(c->passphrase);
	}

	if (c->publickey) {
		/* Zero the memory which previously held the public key */
		size_t key_len = strlen(c->publickey);
		git__memzero(c->publickey, key_len);
		git__free(c->publickey);
	}

	git__free(c);
}

static void ssh_interactive_free(struct git_credential *cred)
{
	git_credential_ssh_interactive *c = (git_credential_ssh_interactive *)cred;

	git__free(c->username);

	git__free(c);
}

static void ssh_custom_free(struct git_credential *cred)
{
	git_credential_ssh_custom *c = (git_credential_ssh_custom *)cred;

	git__free(c->username);

	if (c->publickey) {
		/* Zero the memory which previously held the publickey */
		size_t key_len = strlen(c->publickey);
		git__memzero(c->publickey, key_len);
		git__free(c->publickey);
	}

	git__free(c);
}

static void default_free(struct git_credential *cred)
{
	git_credential_default *c = (git_credential_default *)cred;

	git__free(c);
}

static void username_free(struct git_credential *cred)
{
	git__free(cred);
}

int git_credential_ssh_key_new(
	git_credential **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
	return git_credential_ssh_key_type_new(
		cred,
		username,
		publickey,
		privatekey,
		passphrase,
		GIT_CREDENTIAL_SSH_KEY);
}

int git_credential_ssh_key_memory_new(
	git_credential **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
#ifdef GIT_SSH_MEMORY_CREDENTIALS
	return git_credential_ssh_key_type_new(
		cred,
		username,
		publickey,
		privatekey,
		passphrase,
		GIT_CREDENTIAL_SSH_MEMORY);
#else
	GIT_UNUSED(cred);
	GIT_UNUSED(username);
	GIT_UNUSED(publickey);
	GIT_UNUSED(privatekey);
	GIT_UNUSED(passphrase);

	git_error_set(GIT_ERROR_INVALID,
		"this version of libgit2 was not built with ssh memory credentials.");
	return -1;
#endif
}

static int git_credential_ssh_key_type_new(
	git_credential **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase,
	git_credential_t credtype)
{
	git_credential_ssh_key *c;

	GIT_ASSERT_ARG(username);
	GIT_ASSERT_ARG(cred);
	GIT_ASSERT_ARG(privatekey);

	c = git__calloc(1, sizeof(git_credential_ssh_key));
	GIT_ERROR_CHECK_ALLOC(c);

	c->parent.credtype = credtype;
	c->parent.free = ssh_key_free;

	c->username = git__strdup(username);
	GIT_ERROR_CHECK_ALLOC(c->username);

	c->privatekey = git__strdup(privatekey);
	GIT_ERROR_CHECK_ALLOC(c->privatekey);

	if (publickey) {
		c->publickey = git__strdup(publickey);
		GIT_ERROR_CHECK_ALLOC(c->publickey);
	}

	if (passphrase) {
		c->passphrase = git__strdup(passphrase);
		GIT_ERROR_CHECK_ALLOC(c->passphrase);
	}

	*cred = &c->parent;
	return 0;
}

int git_credential_ssh_interactive_new(
	git_credential **out,
	const char *username,
	git_credential_ssh_interactive_cb prompt_callback,
	void *payload)
{
	git_credential_ssh_interactive *c;

	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(username);
	GIT_ASSERT_ARG(prompt_callback);

	c = git__calloc(1, sizeof(git_credential_ssh_interactive));
	GIT_ERROR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDENTIAL_SSH_INTERACTIVE;
	c->parent.free = ssh_interactive_free;

	c->username = git__strdup(username);
	GIT_ERROR_CHECK_ALLOC(c->username);

	c->prompt_callback = prompt_callback;
	c->payload = payload;

	*out = &c->parent;
	return 0;
}

int git_credential_ssh_key_from_agent(git_credential **cred, const char *username) {
	git_credential_ssh_key *c;

	GIT_ASSERT_ARG(username);
	GIT_ASSERT_ARG(cred);

	c = git__calloc(1, sizeof(git_credential_ssh_key));
	GIT_ERROR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDENTIAL_SSH_KEY;
	c->parent.free = ssh_key_free;

	c->username = git__strdup(username);
	GIT_ERROR_CHECK_ALLOC(c->username);

	c->privatekey = NULL;

	*cred = &c->parent;
	return 0;
}

int git_credential_ssh_custom_new(
	git_credential **cred,
	const char *username,
	const char *publickey,
	size_t publickey_len,
	git_credential_sign_cb sign_callback,
	void *payload)
{
	git_credential_ssh_custom *c;

	GIT_ASSERT_ARG(username);
	GIT_ASSERT_ARG(cred);

	c = git__calloc(1, sizeof(git_credential_ssh_custom));
	GIT_ERROR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDENTIAL_SSH_CUSTOM;
	c->parent.free = ssh_custom_free;

	c->username = git__strdup(username);
	GIT_ERROR_CHECK_ALLOC(c->username);

	if (publickey_len > 0) {
		c->publickey = git__malloc(publickey_len);
		GIT_ERROR_CHECK_ALLOC(c->publickey);

		memcpy(c->publickey, publickey, publickey_len);
	}

	c->publickey_len = publickey_len;
	c->sign_callback = sign_callback;
	c->payload = payload;

	*cred = &c->parent;
	return 0;
}

int git_credential_default_new(git_credential **cred)
{
	git_credential_default *c;

	GIT_ASSERT_ARG(cred);

	c = git__calloc(1, sizeof(git_credential_default));
	GIT_ERROR_CHECK_ALLOC(c);

	c->credtype = GIT_CREDENTIAL_DEFAULT;
	c->free = default_free;

	*cred = c;
	return 0;
}

int git_credential_username_new(git_credential **cred, const char *username)
{
	git_credential_username *c;
	size_t len, allocsize;

	GIT_ASSERT_ARG(cred);

	len = strlen(username);

	GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, sizeof(git_credential_username), len);
	GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, allocsize, 1);
	c = git__malloc(allocsize);
	GIT_ERROR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDENTIAL_USERNAME;
	c->parent.free = username_free;
	memcpy(c->username, username, len + 1);

	*cred = (git_credential *) c;
	return 0;
}

void git_credential_free(git_credential *cred)
{
	if (!cred)
		return;

	cred->free(cred);
}

/* Deprecated credential functions */

#ifndef GIT_DEPRECATE_HARD
int git_cred_has_username(git_credential *cred)
{
	return git_credential_has_username(cred);
}

const char *git_cred_get_username(git_credential *cred)
{
	return git_credential_get_username(cred);
}

int git_cred_userpass_plaintext_new(
	git_credential **out,
	const char *username,
	const char *password)
{
	return git_credential_userpass_plaintext_new(out,username, password);
}

int git_cred_default_new(git_credential **out)
{
	return git_credential_default_new(out);
}

int git_cred_username_new(git_credential **out, const char *username)
{
	return git_credential_username_new(out, username);
}

int git_cred_ssh_key_new(
	git_credential **out,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
	return git_credential_ssh_key_new(out, username,
		publickey, privatekey, passphrase);
}

int git_cred_ssh_key_memory_new(
	git_credential **out,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
	return git_credential_ssh_key_memory_new(out, username,
		publickey, privatekey, passphrase);
}

int git_cred_ssh_interactive_new(
	git_credential **out,
	const char *username,
	git_credential_ssh_interactive_cb prompt_callback,
	void *payload)
{
	return git_credential_ssh_interactive_new(out, username,
		prompt_callback, payload);
}

int git_cred_ssh_key_from_agent(
	git_credential **out,
	const char *username)
{
	return git_credential_ssh_key_from_agent(out, username);
}

int git_cred_ssh_custom_new(
	git_credential **out,
	const char *username,
	const char *publickey,
	size_t publickey_len,
	git_credential_sign_cb sign_callback,
	void *payload)
{
	return git_credential_ssh_custom_new(out, username,
		publickey, publickey_len, sign_callback, payload);
}

void git_cred_free(git_credential *cred)
{
	git_credential_free(cred);
}
#endif