cred.c 3.99 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
int git_cred_has_username(git_cred *cred)
{
	int ret = 0;

	switch (cred->credtype) {
	case GIT_CREDTYPE_USERPASS_PLAINTEXT: {
		git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
		ret = !!c->username;
		break;
	}
	case GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE: {
		git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred;
		ret = !!c->username;
		break;
	}
	case GIT_CREDTYPE_SSH_PUBLICKEY: {
		git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;
		ret = !!c->username;
		break;
	}
	}

	return ret;
}

37 38 39 40 41 42 43
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 */
44 45 46 47 48
	if (c->password) {
		size_t pass_len = strlen(c->password);
		git__memzero(c->password, pass_len);
		git__free(c->password);
	}
49

50
	git__memzero(c, sizeof(*c));
51 52 53 54 55 56 57 58 59 60
	git__free(c);
}

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

61
	assert(cred);
62

63
	c = git__malloc(sizeof(git_cred_userpass_plaintext));
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	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;
85
}
Brad Morgan committed
86 87 88

static void ssh_keyfile_passphrase_free(struct git_cred *cred)
{
89 90
	git_cred_ssh_keyfile_passphrase *c =
		(git_cred_ssh_keyfile_passphrase *)cred;
91

92
	git__free(c->username);
93
	git__free(c->publickey);
Brad Morgan committed
94 95
	git__free(c->privatekey);

Russell Belfer committed
96 97
	if (c->passphrase) {
		/* Zero the memory which previously held the passphrase */
98
		size_t pass_len = strlen(c->passphrase);
Russell Belfer committed
99 100 101
		git__memzero(c->passphrase, pass_len);
		git__free(c->passphrase);
	}
Brad Morgan committed
102

103
	git__memzero(c, sizeof(*c));
Brad Morgan committed
104 105 106
	git__free(c);
}

107 108 109 110
static void ssh_publickey_free(struct git_cred *cred)
{
	git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;

111
	git__free(c->username);
Russell Belfer committed
112
	git__free(c->publickey);
113

114
	git__memzero(c, sizeof(*c));
115 116 117
	git__free(c);
}

Brad Morgan committed
118 119
int git_cred_ssh_keyfile_passphrase_new(
	git_cred **cred,
120
	const char *username,
Brad Morgan committed
121 122 123 124 125 126
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
	git_cred_ssh_keyfile_passphrase *c;

127
	assert(cred && privatekey);
Brad Morgan committed
128

129
	c = git__calloc(1, sizeof(git_cred_ssh_keyfile_passphrase));
Brad Morgan committed
130 131 132 133
	GITERR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE;
	c->parent.free = ssh_keyfile_passphrase_free;
134

135 136 137 138 139
	if (username) {
		c->username = git__strdup(username);
		GITERR_CHECK_ALLOC(c->username);
	}

Russell Belfer committed
140
	c->privatekey = git__strdup(privatekey);
141
	GITERR_CHECK_ALLOC(c->privatekey);
142

Russell Belfer committed
143
	if (publickey) {
144 145 146
		c->publickey = git__strdup(publickey);
		GITERR_CHECK_ALLOC(c->publickey);
	}
147

148 149 150 151
	if (passphrase) {
		c->passphrase = git__strdup(passphrase);
		GITERR_CHECK_ALLOC(c->passphrase);
	}
Brad Morgan committed
152 153 154

	*cred = &c->parent;
	return 0;
155 156 157 158
}

int git_cred_ssh_publickey_new(
	git_cred **cred,
159
	const char *username,
160
	const char *publickey,
Russell Belfer committed
161
	size_t publickey_len,
162
	git_cred_sign_callback sign_callback,
Russell Belfer committed
163
	void *sign_data)
164 165 166
{
	git_cred_ssh_publickey *c;

167
	assert(cred);
168

169
	c = git__calloc(1, sizeof(git_cred_ssh_publickey));
170 171 172 173
	GITERR_CHECK_ALLOC(c);

	c->parent.credtype = GIT_CREDTYPE_SSH_PUBLICKEY;
	c->parent.free = ssh_publickey_free;
174

175 176 177 178 179
	if (username) {
		c->username = git__strdup(username);
		GITERR_CHECK_ALLOC(c->username);
	}

180 181 182
	if (publickey_len > 0) {
		c->publickey = git__malloc(publickey_len);
		GITERR_CHECK_ALLOC(c->publickey);
183

184 185
		memcpy(c->publickey, publickey, publickey_len);
	}
186

Russell Belfer committed
187 188 189
	c->publickey_len = publickey_len;
	c->sign_callback = sign_callback;
	c->sign_data = sign_data;
190 191 192

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