cred.c 5.3 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
int git_cred_has_username(git_cred *cred)
{
14 15
	if (cred->credtype == GIT_CREDTYPE_DEFAULT)
		return 0;
16

17
	return 1;
18 19
}

20 21 22 23 24 25 26
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 */
27 28 29 30 31
	if (c->password) {
		size_t pass_len = strlen(c->password);
		git__memzero(c->password, pass_len);
		git__free(c->password);
	}
32

33 34 35 36 37 38 39 40 41 42
	git__free(c);
}

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

43
	assert(cred && username && password);
44

45
	c = git__malloc(sizeof(git_cred_userpass_plaintext));
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	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;
67
}
Brad Morgan committed
68

69
static void ssh_key_free(struct git_cred *cred)
Brad Morgan committed
70
{
71 72
	git_cred_ssh_key *c =
		(git_cred_ssh_key *)cred;
73

74
	git__free(c->username);
75 76 77 78 79 80 81

	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
82

Russell Belfer committed
83 84
	if (c->passphrase) {
		/* Zero the memory which previously held the passphrase */
85
		size_t pass_len = strlen(c->passphrase);
Russell Belfer committed
86 87 88
		git__memzero(c->passphrase, pass_len);
		git__free(c->passphrase);
	}
Brad Morgan committed
89

90 91 92 93 94 95 96
	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
97 98 99
	git__free(c);
}

100 101 102 103 104 105 106 107 108
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);
}

109
static void ssh_custom_free(struct git_cred *cred)
110
{
111
	git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
112

113
	git__free(c->username);
114

115 116 117 118 119 120 121
	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);
	}

122 123 124
	git__free(c);
}

125 126 127 128 129 130 131
static void default_free(struct git_cred *cred)
{
	git_cred_default *c = (git_cred_default *)cred;

	git__free(c);
}

132
int git_cred_ssh_key_new(
Brad Morgan committed
133
	git_cred **cred,
134
	const char *username,
Brad Morgan committed
135 136 137 138
	const char *publickey,
	const char *privatekey,
	const char *passphrase)
{
139
	git_cred_ssh_key *c;
Brad Morgan committed
140

141
	assert(username && cred && privatekey);
Brad Morgan committed
142

143
	c = git__calloc(1, sizeof(git_cred_ssh_key));
Brad Morgan committed
144 145
	GITERR_CHECK_ALLOC(c);

146 147
	c->parent.credtype = GIT_CREDTYPE_SSH_KEY;
	c->parent.free = ssh_key_free;
148

149 150
	c->username = git__strdup(username);
	GITERR_CHECK_ALLOC(c->username);
151

Russell Belfer committed
152
	c->privatekey = git__strdup(privatekey);
153
	GITERR_CHECK_ALLOC(c->privatekey);
154

Russell Belfer committed
155
	if (publickey) {
156 157 158
		c->publickey = git__strdup(publickey);
		GITERR_CHECK_ALLOC(c->publickey);
	}
159

160 161 162 163
	if (passphrase) {
		c->passphrase = git__strdup(passphrase);
		GITERR_CHECK_ALLOC(c->passphrase);
	}
Brad Morgan committed
164 165

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

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
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;
}

195 196 197
int git_cred_ssh_key_from_agent(git_cred **cred, const char *username) {
	git_cred_ssh_key *c;

198
	assert(username && cred);
199 200 201 202 203 204 205

	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;

206 207
	c->username = git__strdup(username);
	GITERR_CHECK_ALLOC(c->username);
208 209 210 211

	c->privatekey = NULL;

	*cred = &c->parent;
Brad Morgan committed
212
	return 0;
213 214
}

215
int git_cred_ssh_custom_new(
216
	git_cred **cred,
217
	const char *username,
218
	const char *publickey,
Russell Belfer committed
219
	size_t publickey_len,
220
	git_cred_sign_callback sign_callback,
221
	void *payload)
222
{
223
	git_cred_ssh_custom *c;
224

225
	assert(username && cred);
226

227
	c = git__calloc(1, sizeof(git_cred_ssh_custom));
228 229
	GITERR_CHECK_ALLOC(c);

230 231
	c->parent.credtype = GIT_CREDTYPE_SSH_CUSTOM;
	c->parent.free = ssh_custom_free;
232

233 234
	c->username = git__strdup(username);
	GITERR_CHECK_ALLOC(c->username);
235

236 237 238
	if (publickey_len > 0) {
		c->publickey = git__malloc(publickey_len);
		GITERR_CHECK_ALLOC(c->publickey);
239

240 241
		memcpy(c->publickey, publickey, publickey_len);
	}
242

Russell Belfer committed
243 244
	c->publickey_len = publickey_len;
	c->sign_callback = sign_callback;
245
	c->payload = payload;
246 247 248

	*cred = &c->parent;
	return 0;
249
}
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

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;
}