libgit2/src/transports/cred.c
2013-05-04 11:28:20 -04:00

138 lines
2.8 KiB
C

/*
* 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 "git2.h"
#include "smart.h"
#include "git2/cred_helpers.h"
static void plaintext_free(struct git_cred *cred)
{
git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
size_t pass_len = strlen(c->password);
git__free(c->username);
/* Zero the memory which previously held the password */
memset(c->password, 0x0, pass_len);
git__free(c->password);
memset(c, 0, sizeof(*c));
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;
c = git__malloc(sizeof(git_cred_userpass_plaintext));
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;
}
static void ssh_keyfile_passphrase_free(struct git_cred *cred)
{
git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred;
size_t pass_len = strlen(c->passphrase);
if (c->publickey) {
git__free(c->publickey);
}
git__free(c->privatekey);
if (c->passphrase) {
/* Zero the memory which previously held the passphrase */
memset(c->passphrase, 0x0, pass_len);
git__free(c->passphrase);
}
memset(c, 0, sizeof(*c));
git__free(c);
}
int git_cred_ssh_keyfile_passphrase_new(
git_cred **cred,
const char *publickey,
const char *privatekey,
const char *passphrase)
{
git_cred_ssh_keyfile_passphrase *c;
if (!cred)
return -1;
c = git__malloc(sizeof(git_cred_ssh_keyfile_passphrase));
GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE;
c->parent.free = ssh_keyfile_passphrase_free;
c->privatekey = git__strdup(privatekey);
if (!c->privatekey) {
git__free(c);
return -1;
}
if (publickey) {
c->publickey = git__strdup(publickey);
if (!c->publickey) {
git__free(c->privatekey);
git__free(c);
return -1;
}
} else {
c->publickey = NULL;
}
if (passphrase) {
c->passphrase = git__strdup(passphrase);
if (!c->passphrase) {
git__free(c->privatekey);
if (c->publickey) {
git__free(c->publickey);
}
git__free(c);
return -1;
}
} else {
c->passphrase = NULL;
}
*cred = &c->parent;
return 0;
}