Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 : #include "gittransport.h"
18 :
19 : #include "manager.h"
20 :
21 : #include <dhtnet/multiplexed_socket.h>
22 : #include <dhtnet/connectionmanager.h>
23 :
24 : using namespace std::string_view_literals;
25 :
26 : // NOTE: THIS MUST BE IN THE ROOT NAMESPACE FOR LIBGIT2
27 :
28 : /*
29 : * Create a git protocol request.
30 : *
31 : * For example: 0029git-upload-pack conversation\0host=device\0
32 : * @param buf The buffer to fill
33 : * @param cmd The wanted command
34 : * @param url The repository's URL
35 : * @return 0 on success, - 1 on error
36 : */
37 : int
38 1967 : generateRequest(git_buf* request, const std::string& cmd, std::string_view url)
39 : {
40 1967 : if (cmd.empty()) {
41 0 : giterr_set_str(GITERR_NET, "empty command");
42 0 : return -1;
43 : }
44 : // url format = deviceId/conversationId
45 1967 : auto delim = url.find('/');
46 1967 : if (delim == std::string::npos) {
47 0 : giterr_set_str(GITERR_NET, "malformed URL");
48 0 : return -1;
49 : }
50 :
51 1967 : auto deviceId = url.substr(0, delim);
52 1967 : auto conversationId = url.substr(delim, url.size());
53 :
54 1967 : constexpr auto nullSeparator = "\0"sv;
55 : auto total = 4 /* 4 bytes for the len len */
56 1967 : + cmd.size() /* followed by the command */
57 : + 1 /* space */
58 1967 : + conversationId.size() /* conversation */
59 : + 1 /* \0 */
60 1967 : + HOST_TAG.size() + deviceId.size() /* device */
61 1967 : + nullSeparator.size() /* \0 */;
62 :
63 1967 : std::string str;
64 1967 : str.reserve(total);
65 1967 : str.append("0000"sv);
66 1967 : fmt::format_to_n(str.begin(), 4, "{:04x}", total);
67 1967 : str.append(cmd)
68 1967 : .append(" "sv)
69 1967 : .append(conversationId)
70 1967 : .append(nullSeparator)
71 1966 : .append(HOST_TAG)
72 1966 : .append(deviceId)
73 1967 : .append(nullSeparator);
74 :
75 1966 : git_buf_set(request, str.data(), str.size());
76 1967 : return 0;
77 1967 : }
78 :
79 : int
80 1967 : sendCmd(P2PStream* s)
81 : {
82 1967 : auto res = 0;
83 1967 : git_buf request = {};
84 1967 : if ((res = generateRequest(&request, s->cmd, s->url)) < 0) {
85 0 : git_buf_dispose(&request);
86 0 : return res;
87 : }
88 :
89 1967 : std::error_code ec;
90 1966 : auto sock = s->socket.lock();
91 1966 : if (!sock) {
92 0 : git_buf_dispose(&request);
93 0 : return -1;
94 : }
95 1966 : if ((res = sock->write(reinterpret_cast<const unsigned char*>(request.ptr), request.size, ec))) {
96 1967 : s->sent_command = 1;
97 1967 : git_buf_dispose(&request);
98 1967 : return res;
99 : }
100 :
101 0 : s->sent_command = 1;
102 0 : git_buf_dispose(&request);
103 0 : return res;
104 1967 : }
105 :
106 : int
107 5409 : P2PStreamRead(git_smart_subtransport_stream* stream, char* buffer, size_t buflen, size_t* read)
108 : {
109 5409 : *read = 0;
110 5409 : auto* fs = reinterpret_cast<P2PStream*>(stream);
111 5409 : auto sock = fs->socket.lock();
112 5408 : if (!sock) {
113 0 : giterr_set_str(GITERR_NET, "unavailable socket");
114 0 : return -1;
115 : }
116 :
117 5408 : int res = 0;
118 : // If it's the first read, we need to send
119 : // the upload-pack command
120 5408 : if (!fs->sent_command && (res = sendCmd(fs)) < 0)
121 2 : return res;
122 :
123 5406 : std::error_code ec;
124 : // TODO ChannelSocket needs a blocking read operation
125 5406 : size_t datalen = sock->waitForData(std::chrono::milliseconds(3600 * 1000 * 24), ec);
126 5407 : if (datalen > 0)
127 5406 : *read = sock->read(reinterpret_cast<unsigned char*>(buffer), std::min(datalen, buflen), ec);
128 :
129 5407 : return res;
130 5409 : }
131 :
132 : int
133 3639 : P2PStreamWrite(git_smart_subtransport_stream* stream, const char* buffer, size_t len)
134 : {
135 3639 : auto* fs = reinterpret_cast<P2PStream*>(stream);
136 3639 : auto sock = fs->socket.lock();
137 3639 : if (!sock) {
138 1 : giterr_set_str(GITERR_NET, "unavailable socket");
139 1 : return -1;
140 : }
141 3638 : std::error_code ec;
142 3638 : sock->write(reinterpret_cast<const unsigned char*>(buffer), len, ec);
143 3639 : if (ec) {
144 0 : giterr_set_str(GITERR_NET, ec.message().c_str());
145 0 : return -1;
146 : }
147 3639 : return 0;
148 3640 : }
149 :
150 : void
151 1967 : P2PStreamFree(git_smart_subtransport_stream*)
152 1967 : {}
153 :
154 : int
155 5630 : P2PSubTransportAction(git_smart_subtransport_stream** out,
156 : git_smart_subtransport* transport,
157 : const char* url,
158 : git_smart_service_t action)
159 : {
160 5630 : auto* sub = reinterpret_cast<P2PSubTransport*>(transport);
161 5630 : if (!sub || !sub->remote) {
162 0 : JAMI_ERROR("Invalid subtransport");
163 0 : return -1;
164 : }
165 :
166 5630 : auto repo = git_remote_owner(sub->remote);
167 5630 : if (!repo) {
168 0 : JAMI_ERROR("No repository linked to the transport");
169 0 : return -1;
170 : }
171 :
172 5630 : const auto* workdir = git_repository_workdir(repo);
173 5630 : if (!workdir) {
174 0 : JAMI_ERROR("No working linked to the repository");
175 0 : return -1;
176 : }
177 5630 : std::string_view path = workdir;
178 5630 : auto delimConv = path.rfind("/conversations");
179 5630 : if (delimConv == std::string::npos) {
180 0 : JAMI_ERROR("No conversation id found");
181 0 : return -1;
182 : }
183 5630 : auto delimAccount = path.rfind('/', delimConv - 1);
184 5630 : if (delimAccount == std::string::npos && delimConv - 1 - delimAccount == 16) {
185 0 : JAMI_ERROR("No account id found");
186 0 : return -1;
187 : }
188 5630 : auto accountId = path.substr(delimAccount + 1, delimConv - 1 - delimAccount);
189 5630 : std::string_view gitUrl = url + ("git://"sv).size();
190 5629 : auto delim = gitUrl.find('/');
191 5630 : if (delim == std::string::npos) {
192 0 : JAMI_ERROR("Incorrect url {:s}", gitUrl);
193 0 : return -1;
194 : }
195 5630 : auto deviceId = gitUrl.substr(0, delim);
196 5630 : auto conversationId = gitUrl.substr(delim + 1, gitUrl.size());
197 :
198 5629 : if (action == GIT_SERVICE_UPLOADPACK_LS) {
199 1990 : auto gitSocket = jami::Manager::instance().gitSocket(accountId, deviceId, conversationId);
200 1990 : if (!gitSocket) {
201 92 : JAMI_ERROR("Unable to find related socket for {:s}, {:s}, {:s}", accountId, deviceId, conversationId);
202 23 : return -1;
203 : }
204 1967 : auto stream = std::make_unique<P2PStream>();
205 1967 : stream->socket = gitSocket;
206 1967 : stream->base.read = P2PStreamRead;
207 1967 : stream->base.write = P2PStreamWrite;
208 1967 : stream->base.free = P2PStreamFree;
209 1967 : stream->cmd = UPLOAD_PACK_CMD;
210 1967 : stream->url = gitUrl;
211 1967 : sub->stream = std::move(stream);
212 1967 : *out = &sub->stream->base;
213 1967 : return 0;
214 5629 : } else if (action == GIT_SERVICE_UPLOADPACK) {
215 3639 : if (sub->stream) {
216 3639 : *out = &sub->stream->base;
217 3639 : return 0;
218 : }
219 0 : return -1;
220 : }
221 0 : return 0;
222 : }
223 :
224 : int
225 5945 : P2PSubTransportClose(git_smart_subtransport*)
226 : {
227 5945 : return 0;
228 : }
229 :
230 : void
231 1990 : P2PSubTransportFree(git_smart_subtransport* transport)
232 : {
233 1990 : jami::Manager::instance().eraseGitTransport(transport);
234 1990 : }
235 :
236 : int
237 1990 : P2PSubTransportNew(P2PSubTransport** out, git_transport*, void* payload)
238 : {
239 1990 : auto sub = std::make_unique<P2PSubTransport>();
240 1990 : sub->remote = reinterpret_cast<git_remote*>(payload);
241 1990 : auto* base = &sub->base;
242 1990 : base->action = P2PSubTransportAction;
243 1990 : base->close = P2PSubTransportClose;
244 1990 : base->free = P2PSubTransportFree;
245 1990 : *out = sub.get();
246 1990 : jami::Manager::instance().insertGitTransport(base, std::move(sub));
247 1990 : return 0;
248 1990 : }
249 :
250 : int
251 1990 : p2p_subtransport_cb(git_smart_subtransport** out, git_transport* owner, void* payload)
252 : {
253 : P2PSubTransport* sub;
254 :
255 1990 : if (P2PSubTransportNew(&sub, owner, payload) < 0)
256 0 : return -1;
257 :
258 1990 : *out = &sub->base;
259 1990 : return 0;
260 : }
261 :
262 : int
263 1990 : p2p_transport_cb(git_transport** out, git_remote* owner, void*)
264 : {
265 1990 : git_smart_subtransport_definition def
266 : = {p2p_subtransport_cb,
267 : 0, /* Because we use an already existing channel socket, we use a permanent transport */
268 1990 : reinterpret_cast<void*>(owner)};
269 3980 : return git_transport_smart(out, owner, &def);
270 : }
|