Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 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 1962 : generateRequest(git_buf* request, const std::string& cmd, std::string_view url)
39 : {
40 1962 : if (cmd.empty()) {
41 0 : giterr_set_str(GITERR_NET, "empty command");
42 0 : return -1;
43 : }
44 : // url format = deviceId/conversationId
45 1962 : auto delim = url.find('/');
46 1962 : if (delim == std::string::npos) {
47 0 : giterr_set_str(GITERR_NET, "malformed URL");
48 0 : return -1;
49 : }
50 :
51 1962 : auto deviceId = url.substr(0, delim);
52 1962 : auto conversationId = url.substr(delim, url.size());
53 :
54 1962 : constexpr auto nullSeparator = "\0"sv;
55 : auto total = 4 /* 4 bytes for the len len */
56 1962 : + cmd.size() /* followed by the command */
57 : + 1 /* space */
58 1962 : + conversationId.size() /* conversation */
59 : + 1 /* \0 */
60 1962 : + HOST_TAG.size() + deviceId.size() /* device */
61 1962 : + nullSeparator.size() /* \0 */;
62 :
63 1962 : std::string str;
64 1962 : str.reserve(total);
65 1962 : str.append("0000"sv);
66 1962 : fmt::format_to_n(str.begin(), 4, "{:04x}", total);
67 1962 : str.append(cmd)
68 1962 : .append(" "sv)
69 1962 : .append(conversationId)
70 1962 : .append(nullSeparator)
71 1962 : .append(HOST_TAG)
72 1962 : .append(deviceId)
73 1962 : .append(nullSeparator);
74 :
75 1962 : git_buf_set(request, str.data(), str.size());
76 1962 : return 0;
77 1962 : }
78 :
79 : int
80 1962 : sendCmd(P2PStream* s)
81 : {
82 1962 : auto res = 0;
83 1962 : git_buf request = {};
84 1962 : if ((res = generateRequest(&request, s->cmd, s->url)) < 0) {
85 0 : git_buf_dispose(&request);
86 0 : return res;
87 : }
88 :
89 1962 : std::error_code ec;
90 1962 : auto sock = s->socket.lock();
91 1962 : if (!sock) {
92 0 : git_buf_dispose(&request);
93 0 : return -1;
94 : }
95 1962 : if ((res = sock->write(reinterpret_cast<const unsigned char*>(request.ptr), request.size, ec))) {
96 1962 : s->sent_command = 1;
97 1962 : git_buf_dispose(&request);
98 1961 : return res;
99 : }
100 :
101 0 : s->sent_command = 1;
102 0 : git_buf_dispose(&request);
103 0 : return res;
104 1961 : }
105 :
106 : int
107 5407 : P2PStreamRead(git_smart_subtransport_stream* stream, char* buffer, size_t buflen, size_t* read)
108 : {
109 5407 : *read = 0;
110 5407 : auto* fs = reinterpret_cast<P2PStream*>(stream);
111 5407 : auto sock = fs->socket.lock();
112 5407 : if (!sock) {
113 0 : giterr_set_str(GITERR_NET, "unavailable socket");
114 0 : return -1;
115 : }
116 :
117 5407 : int res = 0;
118 : // If it's the first read, we need to send
119 : // the upload-pack command
120 5407 : if (!fs->sent_command && (res = sendCmd(fs)) < 0)
121 1 : 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 5406 : if (datalen > 0)
127 5405 : *read = sock->read(reinterpret_cast<unsigned char*>(buffer), std::min(datalen, buflen), ec);
128 :
129 5405 : return res;
130 5406 : }
131 :
132 : int
133 3634 : P2PStreamWrite(git_smart_subtransport_stream* stream, const char* buffer, size_t len)
134 : {
135 3634 : auto* fs = reinterpret_cast<P2PStream*>(stream);
136 3634 : auto sock = fs->socket.lock();
137 3634 : if (!sock) {
138 2 : giterr_set_str(GITERR_NET, "unavailable socket");
139 2 : return -1;
140 : }
141 3632 : std::error_code ec;
142 3632 : sock->write(reinterpret_cast<const unsigned char*>(buffer), len, ec);
143 3633 : if (ec) {
144 1 : giterr_set_str(GITERR_NET, ec.message().c_str());
145 0 : return -1;
146 : }
147 3632 : return 0;
148 3634 : }
149 :
150 : void
151 1962 : P2PStreamFree(git_smart_subtransport_stream*)
152 1962 : {}
153 :
154 : int
155 5620 : P2PSubTransportAction(git_smart_subtransport_stream** out,
156 : git_smart_subtransport* transport,
157 : const char* url,
158 : git_smart_service_t action)
159 : {
160 5620 : auto* sub = reinterpret_cast<P2PSubTransport*>(transport);
161 5620 : if (!sub || !sub->remote) {
162 0 : JAMI_ERROR("Invalid subtransport");
163 0 : return -1;
164 : }
165 :
166 5620 : auto repo = git_remote_owner(sub->remote);
167 5620 : if (!repo) {
168 0 : JAMI_ERROR("No repository linked to the transport");
169 0 : return -1;
170 : }
171 :
172 5620 : const auto* workdir = git_repository_workdir(repo);
173 5620 : if (!workdir) {
174 0 : JAMI_ERROR("No working linked to the repository");
175 0 : return -1;
176 : }
177 5620 : std::string_view path = workdir;
178 5620 : auto delimConv = path.rfind("/conversations");
179 5620 : if (delimConv == std::string::npos) {
180 0 : JAMI_ERROR("No conversation id found");
181 0 : return -1;
182 : }
183 5620 : auto delimAccount = path.rfind('/', delimConv - 1);
184 5619 : if (delimAccount == std::string::npos && delimConv - 1 - delimAccount == 16) {
185 0 : JAMI_ERROR("No account id found");
186 0 : return -1;
187 : }
188 5619 : auto accountId = path.substr(delimAccount + 1, delimConv - 1 - delimAccount);
189 5619 : std::string_view gitUrl = url + ("git://"sv).size();
190 5619 : auto delim = gitUrl.find('/');
191 5618 : if (delim == std::string::npos) {
192 0 : JAMI_ERROR("Incorrect url {:s}", gitUrl);
193 0 : return -1;
194 : }
195 5618 : auto deviceId = gitUrl.substr(0, delim);
196 5619 : auto conversationId = gitUrl.substr(delim + 1, gitUrl.size());
197 :
198 5619 : if (action == GIT_SERVICE_UPLOADPACK_LS) {
199 1985 : auto gitSocket = jami::Manager::instance().gitSocket(accountId, deviceId, conversationId);
200 1985 : if (!gitSocket) {
201 92 : JAMI_ERROR("Unable to find related socket for {:s}, {:s}, {:s}", accountId, deviceId, conversationId);
202 23 : return -1;
203 : }
204 1962 : auto stream = std::make_unique<P2PStream>();
205 1962 : stream->socket = gitSocket;
206 1962 : stream->base.read = P2PStreamRead;
207 1962 : stream->base.write = P2PStreamWrite;
208 1962 : stream->base.free = P2PStreamFree;
209 1962 : stream->cmd = UPLOAD_PACK_CMD;
210 1962 : stream->url = gitUrl;
211 1962 : sub->stream = std::move(stream);
212 1962 : *out = &sub->stream->base;
213 1962 : return 0;
214 5619 : } else if (action == GIT_SERVICE_UPLOADPACK) {
215 3634 : if (sub->stream) {
216 3634 : *out = &sub->stream->base;
217 3634 : return 0;
218 : }
219 0 : return -1;
220 : }
221 0 : return 0;
222 : }
223 :
224 : int
225 5931 : P2PSubTransportClose(git_smart_subtransport*)
226 : {
227 5931 : return 0;
228 : }
229 :
230 : void
231 1985 : P2PSubTransportFree(git_smart_subtransport* transport)
232 : {
233 1985 : jami::Manager::instance().eraseGitTransport(transport);
234 1985 : }
235 :
236 : int
237 1985 : P2PSubTransportNew(P2PSubTransport** out, git_transport*, void* payload)
238 : {
239 1985 : auto sub = std::make_unique<P2PSubTransport>();
240 1985 : sub->remote = reinterpret_cast<git_remote*>(payload);
241 1985 : auto* base = &sub->base;
242 1985 : base->action = P2PSubTransportAction;
243 1985 : base->close = P2PSubTransportClose;
244 1985 : base->free = P2PSubTransportFree;
245 1985 : *out = sub.get();
246 1985 : jami::Manager::instance().insertGitTransport(base, std::move(sub));
247 1985 : return 0;
248 1985 : }
249 :
250 : int
251 1985 : p2p_subtransport_cb(git_smart_subtransport** out, git_transport* owner, void* payload)
252 : {
253 : P2PSubTransport* sub;
254 :
255 1985 : if (P2PSubTransportNew(&sub, owner, payload) < 0)
256 0 : return -1;
257 :
258 1985 : *out = &sub->base;
259 1985 : return 0;
260 : }
261 :
262 : int
263 1985 : p2p_transport_cb(git_transport** out, git_remote* owner, void*)
264 : {
265 1985 : 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 1985 : reinterpret_cast<void*>(owner)};
269 3970 : return git_transport_smart(out, owner, &def);
270 : }
|