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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/*
* 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 "ssh_exec.h"
#ifdef GIT_SSH_EXEC
#include "common.h"
#include "config.h"
#include "net.h"
#include "path.h"
#include "futils.h"
#include "process.h"
#include "transports/smart.h"
typedef struct {
git_smart_subtransport_stream parent;
} ssh_exec_subtransport_stream;
typedef struct {
git_smart_subtransport parent;
git_transport *owner;
ssh_exec_subtransport_stream *current_stream;
char *cmd_uploadpack;
char *cmd_receivepack;
git_smart_service_t action;
git_process *process;
} ssh_exec_subtransport;
static int ssh_exec_subtransport_stream_read(
git_smart_subtransport_stream *s,
char *buffer,
size_t buf_size,
size_t *bytes_read)
{
ssh_exec_subtransport *transport;
ssh_exec_subtransport_stream *stream = (ssh_exec_subtransport_stream *)s;
ssize_t ret;
GIT_ASSERT_ARG(stream);
GIT_ASSERT(stream->parent.subtransport);
transport = (ssh_exec_subtransport *)stream->parent.subtransport;
if ((ret = git_process_read(transport->process, buffer, buf_size)) < 0) {
return (int)ret;
}
*bytes_read = (size_t)ret;
return 0;
}
static int ssh_exec_subtransport_stream_write(
git_smart_subtransport_stream *s,
const char *buffer,
size_t len)
{
ssh_exec_subtransport *transport;
ssh_exec_subtransport_stream *stream = (ssh_exec_subtransport_stream *)s;
ssize_t ret;
GIT_ASSERT(stream && stream->parent.subtransport);
transport = (ssh_exec_subtransport *)stream->parent.subtransport;
while (len > 0) {
if ((ret = git_process_write(transport->process, buffer, len)) < 0)
return (int)ret;
len -= ret;
}
return 0;
}
static void ssh_exec_subtransport_stream_free(git_smart_subtransport_stream *s)
{
ssh_exec_subtransport_stream *stream = (ssh_exec_subtransport_stream *)s;
git__free(stream);
}
static int ssh_exec_subtransport_stream_init(
ssh_exec_subtransport_stream **out,
ssh_exec_subtransport *transport)
{
GIT_ASSERT_ARG(out);
*out = git__calloc(sizeof(ssh_exec_subtransport_stream), 1);
GIT_ERROR_CHECK_ALLOC(*out);
(*out)->parent.subtransport = &transport->parent;
(*out)->parent.read = ssh_exec_subtransport_stream_read;
(*out)->parent.write = ssh_exec_subtransport_stream_write;
(*out)->parent.free = ssh_exec_subtransport_stream_free;
return 0;
}
GIT_INLINE(int) ensure_transport_state(
ssh_exec_subtransport *transport,
git_smart_service_t expected,
git_smart_service_t next)
{
if (transport->action != expected && transport->action != next) {
git_error_set(GIT_ERROR_NET, "invalid transport state");
return -1;
}
return 0;
}
static int get_ssh_cmdline(
git_str *out,
ssh_exec_subtransport *transport,
git_net_url *url,
const char *command)
{
git_remote *remote = ((transport_smart *)transport->owner)->owner;
git_repository *repo = remote->repo;
git_config *cfg;
git_str ssh_cmd = GIT_STR_INIT;
const char *default_ssh_cmd = "ssh";
int error;
/*
* Safety check: like git, we forbid paths that look like an
* option as that could lead to injection to ssh that can make
* us do unexpected things
*/
if (git_process__is_cmdline_option(url->username)) {
git_error_set(GIT_ERROR_NET, "cannot ssh: username '%s' is ambiguous with command-line option", url->username);
return -1;
} else if (git_process__is_cmdline_option(url->host)) {
git_error_set(GIT_ERROR_NET, "cannot ssh: host '%s' is ambiguous with command-line option", url->host);
return -1;
} else if (git_process__is_cmdline_option(url->path)) {
git_error_set(GIT_ERROR_NET, "cannot ssh: path '%s' is ambiguous with command-line option", url->path);
return -1;
}
if ((error = git_repository_config_snapshot(&cfg, repo)) < 0)
return error;
if ((error = git__getenv(&ssh_cmd, "GIT_SSH")) == 0)
;
else if (error != GIT_ENOTFOUND)
goto done;
else if ((error = git_config__get_string_buf(&ssh_cmd, cfg, "core.sshcommand")) < 0 && error != GIT_ENOTFOUND)
goto done;
error = git_str_printf(out, "%s -p %s \"%s%s%s\" \"%s\" \"%s\"",
ssh_cmd.size > 0 ? ssh_cmd.ptr : default_ssh_cmd,
url->port,
url->username ? url->username : "",
url->username ? "@" : "",
url->host,
command,
url->path);
done:
git_str_dispose(&ssh_cmd);
git_config_free(cfg);
return error;
}
static int start_ssh(
ssh_exec_subtransport *transport,
git_smart_service_t action,
const char *sshpath)
{
const char *env[] = { "GIT_DIR=" };
git_process_options process_opts = GIT_PROCESS_OPTIONS_INIT;
git_net_url url = GIT_NET_URL_INIT;
git_str ssh_cmdline = GIT_STR_INIT;
const char *command;
int error;
process_opts.capture_in = 1;
process_opts.capture_out = 1;
process_opts.capture_err = 0;
switch (action) {
case GIT_SERVICE_UPLOADPACK_LS:
command = transport->cmd_uploadpack ?
transport->cmd_uploadpack : "git-upload-pack";
break;
case GIT_SERVICE_RECEIVEPACK_LS:
command = transport->cmd_receivepack ?
transport->cmd_receivepack : "git-receive-pack";
break;
default:
git_error_set(GIT_ERROR_NET, "invalid action");
error = -1;
goto done;
}
if (git_net_str_is_url(sshpath))
error = git_net_url_parse(&url, sshpath);
else
error = git_net_url_parse_scp(&url, sshpath);
if (error < 0)
goto done;
if ((error = get_ssh_cmdline(&ssh_cmdline, transport, &url, command)) < 0)
goto done;
if ((error = git_process_new_from_cmdline(&transport->process,
ssh_cmdline.ptr, env, ARRAY_SIZE(env), &process_opts)) < 0 ||
(error = git_process_start(transport->process)) < 0) {
git_process_free(transport->process);
transport->process = NULL;
goto done;
}
done:
git_str_dispose(&ssh_cmdline);
git_net_url_dispose(&url);
return error;
}
static int ssh_exec_subtransport_action(
git_smart_subtransport_stream **out,
git_smart_subtransport *t,
const char *sshpath,
git_smart_service_t action)
{
ssh_exec_subtransport *transport = (ssh_exec_subtransport *)t;
ssh_exec_subtransport_stream *stream = NULL;
git_smart_service_t expected;
int error;
switch (action) {
case GIT_SERVICE_UPLOADPACK_LS:
case GIT_SERVICE_RECEIVEPACK_LS:
if ((error = ensure_transport_state(transport, 0, 0)) < 0 ||
(error = ssh_exec_subtransport_stream_init(&stream, transport)) < 0 ||
(error = start_ssh(transport, action, sshpath)) < 0)
goto on_error;
transport->current_stream = stream;
break;
case GIT_SERVICE_UPLOADPACK:
case GIT_SERVICE_RECEIVEPACK:
expected = (action == GIT_SERVICE_UPLOADPACK) ?
GIT_SERVICE_UPLOADPACK_LS : GIT_SERVICE_RECEIVEPACK_LS;
if ((error = ensure_transport_state(transport, expected, action)) < 0)
goto on_error;
break;
default:
git_error_set(GIT_ERROR_INVALID, "invalid service request");
goto on_error;
}
transport->action = action;
*out = &transport->current_stream->parent;
return 0;
on_error:
if (stream != NULL)
ssh_exec_subtransport_stream_free(&stream->parent);
return -1;
}
static int ssh_exec_subtransport_close(git_smart_subtransport *t)
{
ssh_exec_subtransport *transport = (ssh_exec_subtransport *)t;
if (transport->process) {
git_process_close(transport->process);
git_process_free(transport->process);
transport->process = NULL;
}
transport->action = 0;
return 0;
}
static void ssh_exec_subtransport_free(git_smart_subtransport *t)
{
ssh_exec_subtransport *transport = (ssh_exec_subtransport *)t;
git__free(transport->cmd_uploadpack);
git__free(transport->cmd_receivepack);
git__free(transport);
}
int git_smart_subtransport_ssh_exec(
git_smart_subtransport **out,
git_transport *owner,
void *payload)
{
ssh_exec_subtransport *transport;
GIT_UNUSED(payload);
transport = git__calloc(sizeof(ssh_exec_subtransport), 1);
GIT_ERROR_CHECK_ALLOC(transport);
transport->owner = owner;
transport->parent.action = ssh_exec_subtransport_action;
transport->parent.close = ssh_exec_subtransport_close;
transport->parent.free = ssh_exec_subtransport_free;
*out = (git_smart_subtransport *) transport;
return 0;
}
int git_smart_subtransport_ssh_exec_set_paths(
git_smart_subtransport *subtransport,
const char *cmd_uploadpack,
const char *cmd_receivepack)
{
ssh_exec_subtransport *t = (ssh_exec_subtransport *)subtransport;
git__free(t->cmd_uploadpack);
git__free(t->cmd_receivepack);
t->cmd_uploadpack = git__strdup(cmd_uploadpack);
GIT_ERROR_CHECK_ALLOC(t->cmd_uploadpack);
t->cmd_receivepack = git__strdup(cmd_receivepack);
GIT_ERROR_CHECK_ALLOC(t->cmd_receivepack);
return 0;
}
#endif
|