cred.c 1.19 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9
 *
 * 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 "git2.h"
#include "smart.h"
10
#include "git2/cred_helpers.h"
11 12 13 14

static void plaintext_free(struct git_cred *cred)
{
	git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
15
	size_t pass_len = strlen(c->password);
16 17 18 19 20 21 22

	git__free(c->username);

	/* Zero the memory which previously held the password */
	memset(c->password, 0x0, pass_len);
	git__free(c->password);

23 24
	memset(c, 0, sizeof(*c));

25 26 27 28 29 30 31 32 33 34 35 36 37
	git__free(c);
}

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

	if (!cred)
		return -1;

38
	c = git__malloc(sizeof(git_cred_userpass_plaintext));
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	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;
60
}