cred.c 7.67 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "cred.h"

10 11
#include "git2.h"
#include "smart.h"
12
#include "git2/cred_helpers.h"
13

14 15 16 17 18 19 20 21
static int git_cred_ssh_key_type_new(
	git_cred **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase,
	git_credtype_t credtype);

22 23
int git_cred_has_username(git_cred *cred)
{
24 25
	if (cred->credtype == GIT_CREDTYPE_DEFAULT)
		return 0;
26

27
	return 1;
28 29
}

30 31 32 33 34 35 36 37 38 39 40 41 42 43
const char *git_cred__username(git_cred *cred)
{
	switch (cred->credtype) {
	case GIT_CREDTYPE_USERNAME:
	{
		git_cred_username *c = (git_cred_username *) cred;
		return c->username;
	}
	case GIT_CREDTYPE_USERPASS_PLAINTEXT:
	{
		git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *) cred;
		return c->username;
	}
	case GIT_CREDTYPE_SSH_KEY:
44
	case GIT_CREDTYPE_SSH_MEMORY:
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	{
		git_cred_ssh_key *c = (git_cred_ssh_key *) cred;
		return c->username;
	}
	case GIT_CREDTYPE_SSH_CUSTOM:
	{
		git_cred_ssh_custom *c = (git_cred_ssh_custom *) cred;
		return c->username;
	}
	case GIT_CREDTYPE_SSH_INTERACTIVE:
	{
		git_cred_ssh_interactive *c = (git_cred_ssh_interactive *) cred;
		return c->username;
	}

	default:
		return NULL;
	}
}

65 66 67 68 69 70 71
static void plaintext_free(struct git_cred *cred)
{
	git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;

	git__free(c->username);

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

78 79 80 81 82 83 84 85 86 87
	git__free(c);
}

int git_cred_userpass_plaintext_new(
	git_cred **cred,
	const char *username,
	const char *password)
{
	git_cred_userpass_plaintext *c;

88
	assert(cred && username && password);
89

90
	c = git__malloc(sizeof(git_cred_userpass_plaintext));
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	GITERR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDTYPE_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;
112
}
Brad Morgan committed
113

114
static void ssh_key_free(struct git_cred *cred)
Brad Morgan committed
115
{
116 117
	git_cred_ssh_key *c =
		(git_cred_ssh_key *)cred;
118

119
	git__free(c->username);
120 121 122 123 124 125 126

	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);
	}
Brad Morgan committed
127

Russell Belfer committed
128 129
	if (c->passphrase) {
		/* Zero the memory which previously held the passphrase */
130
		size_t pass_len = strlen(c->passphrase);
Russell Belfer committed
131 132 133
		git__memzero(c->passphrase, pass_len);
		git__free(c->passphrase);
	}
Brad Morgan committed
134

135 136 137 138 139 140 141
	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);
	}

Brad Morgan committed
142 143 144
	git__free(c);
}

145 146 147 148 149 150 151 152 153
static void ssh_interactive_free(struct git_cred *cred)
{
	git_cred_ssh_interactive *c = (git_cred_ssh_interactive *)cred;

	git__free(c->username);

	git__free(c);
}

154
static void ssh_custom_free(struct git_cred *cred)
155
{
156
	git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
157

158
	git__free(c->username);
159

160 161 162 163 164 165 166
	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);
	}

167 168 169
	git__free(c);
}

170 171 172 173 174 175 176
static void default_free(struct git_cred *cred)
{
	git_cred_default *c = (git_cred_default *)cred;

	git__free(c);
}

177 178 179 180 181
static void username_free(struct git_cred *cred)
{
	git__free(cred);
}

182
int git_cred_ssh_key_new(
Brad Morgan committed
183
	git_cred **cred,
184
	const char *username,
Brad Morgan committed
185 186 187 188
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	return git_cred_ssh_key_type_new(
		cred,
		username,
		publickey,
		privatekey,
		passphrase,
		GIT_CREDTYPE_SSH_KEY);
}

int git_cred_ssh_key_memory_new(
	git_cred **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
205
#ifdef GIT_SSH_MEMORY_CREDENTIALS
206 207 208 209 210 211 212
	return git_cred_ssh_key_type_new(
		cred,
		username,
		publickey,
		privatekey,
		passphrase,
		GIT_CREDTYPE_SSH_MEMORY);
213
#else
214 215 216 217 218 219
	GIT_UNUSED(cred);
	GIT_UNUSED(username);
	GIT_UNUSED(publickey);
	GIT_UNUSED(privatekey);
	GIT_UNUSED(passphrase);

220
	giterr_set(GITERR_INVALID,
221
		"this version of libgit2 was not built with ssh memory credentials.");
222
	return -1;
223
#endif
224
}
225 226 227 228 229 230 231 232 233

static int git_cred_ssh_key_type_new(
	git_cred **cred,
	const char *username,
	const char *publickey,
	const char *privatekey,
	const char *passphrase,
	git_credtype_t credtype)
{
234
	git_cred_ssh_key *c;
Brad Morgan committed
235

236
	assert(username && cred && privatekey);
Brad Morgan committed
237

238
	c = git__calloc(1, sizeof(git_cred_ssh_key));
Brad Morgan committed
239 240
	GITERR_CHECK_ALLOC(c);

241
	c->parent.credtype = credtype;
242
	c->parent.free = ssh_key_free;
243

244 245
	c->username = git__strdup(username);
	GITERR_CHECK_ALLOC(c->username);
246

Russell Belfer committed
247
	c->privatekey = git__strdup(privatekey);
248
	GITERR_CHECK_ALLOC(c->privatekey);
249

Russell Belfer committed
250
	if (publickey) {
251 252 253
		c->publickey = git__strdup(publickey);
		GITERR_CHECK_ALLOC(c->publickey);
	}
254

255 256 257 258
	if (passphrase) {
		c->passphrase = git__strdup(passphrase);
		GITERR_CHECK_ALLOC(c->passphrase);
	}
Brad Morgan committed
259 260

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

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
int git_cred_ssh_interactive_new(
	git_cred **out,
	const char *username,
	git_cred_ssh_interactive_callback prompt_callback,
	void *payload)
{
	git_cred_ssh_interactive *c;

	assert(out && username && prompt_callback);

	c = git__calloc(1, sizeof(git_cred_ssh_interactive));
	GITERR_CHECK_ALLOC(c);

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

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

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

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

290 291 292
int git_cred_ssh_key_from_agent(git_cred **cred, const char *username) {
	git_cred_ssh_key *c;

293
	assert(username && cred);
294 295 296 297 298 299 300

	c = git__calloc(1, sizeof(git_cred_ssh_key));
	GITERR_CHECK_ALLOC(c);

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

301 302
	c->username = git__strdup(username);
	GITERR_CHECK_ALLOC(c->username);
303 304 305 306

	c->privatekey = NULL;

	*cred = &c->parent;
Brad Morgan committed
307
	return 0;
308 309
}

310
int git_cred_ssh_custom_new(
311
	git_cred **cred,
312
	const char *username,
313
	const char *publickey,
Russell Belfer committed
314
	size_t publickey_len,
315
	git_cred_sign_callback sign_callback,
316
	void *payload)
317
{
318
	git_cred_ssh_custom *c;
319

320
	assert(username && cred);
321

322
	c = git__calloc(1, sizeof(git_cred_ssh_custom));
323 324
	GITERR_CHECK_ALLOC(c);

325 326
	c->parent.credtype = GIT_CREDTYPE_SSH_CUSTOM;
	c->parent.free = ssh_custom_free;
327

328 329
	c->username = git__strdup(username);
	GITERR_CHECK_ALLOC(c->username);
330

331 332 333
	if (publickey_len > 0) {
		c->publickey = git__malloc(publickey_len);
		GITERR_CHECK_ALLOC(c->publickey);
334

335 336
		memcpy(c->publickey, publickey, publickey_len);
	}
337

Russell Belfer committed
338 339
	c->publickey_len = publickey_len;
	c->sign_callback = sign_callback;
340
	c->payload = payload;
341 342 343

	*cred = &c->parent;
	return 0;
344
}
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

int git_cred_default_new(git_cred **cred)
{
	git_cred_default *c;

	assert(cred);

	c = git__calloc(1, sizeof(git_cred_default));
	GITERR_CHECK_ALLOC(c);

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

	*cred = c;
	return 0;
}
361 362 363 364

int git_cred_username_new(git_cred **cred, const char *username)
{
	git_cred_username *c;
365
	size_t len, allocsize;
366 367 368 369

	assert(cred);

	len = strlen(username);
370

371 372 373
	GITERR_CHECK_ALLOC_ADD(&allocsize, sizeof(git_cred_username), len);
	GITERR_CHECK_ALLOC_ADD(&allocsize, allocsize, 1);
	c = git__malloc(allocsize);
374 375 376 377 378 379
	GITERR_CHECK_ALLOC(c);

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

380
	*cred = (git_cred *) c;
381 382
	return 0;
}
383 384 385 386 387 388 389 390

void git_cred_free(git_cred *cred)
{
	if (!cred)
		return;

	cred->free(cred);
}