1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
|
/*
* 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"
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_KEY: {
git_cred_ssh_key *c = (git_cred_ssh_key *)cred;
ret = !!c->username;
break;
}
case GIT_CREDTYPE_SSH_CUSTOM: {
git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
ret = !!c->username;
break;
}
}
return ret;
}
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 */
if (c->password) {
size_t pass_len = strlen(c->password);
git__memzero(c->password, pass_len);
git__free(c->password);
}
git__memzero(c, 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;
assert(cred && username && password);
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_key_free(struct git_cred *cred)
{
git_cred_ssh_key *c =
(git_cred_ssh_key *)cred;
git__free(c->username);
git__free(c->publickey);
git__free(c->privatekey);
if (c->passphrase) {
/* Zero the memory which previously held the passphrase */
size_t pass_len = strlen(c->passphrase);
git__memzero(c->passphrase, pass_len);
git__free(c->passphrase);
}
git__memzero(c, sizeof(*c));
git__free(c);
}
static void ssh_custom_free(struct git_cred *cred)
{
git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
git__free(c->username);
git__free(c->publickey);
git__memzero(c, sizeof(*c));
git__free(c);
}
int git_cred_ssh_key_new(
git_cred **cred,
const char *username,
const char *publickey,
const char *privatekey,
const char *passphrase)
{
git_cred_ssh_key *c;
assert(cred && privatekey);
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;
if (username) {
c->username = git__strdup(username);
GITERR_CHECK_ALLOC(c->username);
}
c->privatekey = git__strdup(privatekey);
GITERR_CHECK_ALLOC(c->privatekey);
if (publickey) {
c->publickey = git__strdup(publickey);
GITERR_CHECK_ALLOC(c->publickey);
}
if (passphrase) {
c->passphrase = git__strdup(passphrase);
GITERR_CHECK_ALLOC(c->passphrase);
}
*cred = &c->parent;
return 0;
}
int git_cred_ssh_custom_new(
git_cred **cred,
const char *username,
const char *publickey,
size_t publickey_len,
git_cred_sign_callback sign_callback,
void *sign_data)
{
git_cred_ssh_custom *c;
assert(cred);
c = git__calloc(1, sizeof(git_cred_ssh_custom));
GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_CUSTOM;
c->parent.free = ssh_custom_free;
if (username) {
c->username = git__strdup(username);
GITERR_CHECK_ALLOC(c->username);
}
if (publickey_len > 0) {
c->publickey = git__malloc(publickey_len);
GITERR_CHECK_ALLOC(c->publickey);
memcpy(c->publickey, publickey, publickey_len);
}
c->publickey_len = publickey_len;
c->sign_callback = sign_callback;
c->sign_data = sign_data;
*cred = &c->parent;
return 0;
}
|