-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerCommands.cpp
349 lines (271 loc) · 9.43 KB
/
ServerCommands.cpp
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
347
348
349
#include <StdFuncs.h>
#include <File.h>
#include <FileUtils.h>
#include <StdSocket.h>
#include <Yggdrasil/Commands.h>
#include <string.h>
#ifdef __amigaos__
#include <sys/wait.h>
#endif /* ! __amigaos__ */
/**
* Deletes a file.
* Deletes the file specified by the client.
*
* @date Wednesday 08-Mar-2023 7:47 am, Code HQ Tokyo Tsukuda
*/
void CDelete::execute()
{
readPayload();
/* Extract the filename from the payload */
char *fileName = reinterpret_cast<char *>(m_payload);
printf("delete: Deleting file \"%s\"\n", fileName);
RFileUtils fileUtils;
TResponse response;
/* Delete the file and return the result to the client */
response.m_result = fileUtils.deleteFile(fileName);
SWAP(&response.m_result);
response.m_size = 0;
m_socket->write(&response, sizeof(response));
}
/**
* Sends a directory listing to the remote client.
* Lists the contents of the requested directory and returns the list of files to the client.
*
* @date Wednesday 11-Jan-2023 6:52 am, Code HQ Tokyo Tsukuda
*/
void CDir::execute()
{
readPayload();
/* Extract the filename from the payload */
m_directoryName = reinterpret_cast<char *>(m_payload);
printf("dir: Listing contents of directory \"%s\"\n", m_directoryName);
int result;
RDir dir;
TResponse response;
TEntryArray *entries;
/* Scan the specified directory and build a list of files that it contains */
if ((result = dir.open(m_directoryName)) == KErrNone)
{
if ((result = dir.read(entries, EDirSortNameAscending)) == KErrNone)
{
char *payload;
size_t nameLength, offset = 0;
uint32_t payloadSize = 1;
/* Iterate through the list of files and determine the amount of memory required to store the filenames */
/* and file metadata */
const TEntry *entry = entries->getHead();
while (entry != nullptr)
{
payloadSize += static_cast<uint32_t>(strlen(entry->iName) + 1 + sizeof(TEntry::iSize));
entry = entries->getSucc(entry);
}
/* Allocate a buffer large enough to hold the response payload and fill it with the file information */
payload = new char[payloadSize];
entry = entries->getHead();
while (entry != nullptr)
{
nameLength = strlen(entry->iName);
memcpy(payload + offset, entry->iName, nameLength + 1);
offset += nameLength + 1;
WRITE_INT_64((payload + offset), entry->iSize);
offset += sizeof(entry->iSize);
entry = entries->getSucc(entry);
}
*(payload + offset) = '\0';
response.m_result = result;
SWAP(&response.m_result);
response.m_size = payloadSize;
SWAP(&response.m_size);
m_socket->write(&response, sizeof(response));
m_socket->write(payload, payloadSize);
delete [] payload;
}
dir.close();
}
/* If the directory could not be read, just send a response with an empty payload */
if (result != KErrNone)
{
response.m_result = result;
response.m_size = 0;
SWAP(&response.m_result);
m_socket->write(&response, sizeof(response));
}
}
/**
* Executes a file.
* Runs the executable or script file specified by the client, displaying its stdout and stderr output
* on the console.
*
* @date Wednesday 29-Jan-2020 2:24 pm, Scoot flight TR 735 to Singapore
*/
void CExecute::execute()
{
readPayload();
printf("execute: Executing command \"%s\"\n", m_payload);
TResult result = launchCommand(reinterpret_cast<char*>(m_payload));
TResponse response{ result.m_result, result.m_subResult };
SWAP(&response.m_result);
SWAP(&response.m_subResult);
/* Regardless of whether the client was launched successfully then send two NULL terminators in a row. This */
/* is the signal to the client that the stdout output stream has ended. We then also send a response to signal */
/* the exit code of the client command in response.m_subResult */
char terminators[2] = { 0, 0 };
m_socket->write(terminators, sizeof(terminators));
m_socket->write(&response, sizeof(response));
if (result.m_result != KErrNone)
{
printf("execute: Unable to launch command (Error = %d)\n", result.m_result);
}
else if (result.m_subResult != 0)
{
printf("execute: Command \"%s\" was launched successfully but returned failure (Error = %d)\n", m_payload,
result.m_subResult);
}
}
/**
* Determines detailed file information and returns it to the client.
* Sends information about a filesystem object to the client, allowing it to determine such things
* as the size of the object, whether it is a file or a directory, its size etc.
*
* @date Thursday 23-Feb-2023 10:04 am, Code HQ Tokyo Tsukuda
*/
void CFileInfo::execute()
{
readPayload();
/* Extract the filename from the payload */
m_fileName = reinterpret_cast<char *>(m_payload);
printf("fileinfo: Querying information about file \"%s\"\n", m_payload);
int result;
SFileInfo *fileInfo;
TResponse response;
TEntry entry;
/* Determine if the file exists and send the result to the remote client */
result = response.m_result = getFileInformation(m_fileName, fileInfo);
SWAP(&response.m_result);
/* If the file exists then send a response and a payload, containing the file's timestamp */
if (result == KErrNone)
{
/* Include the size of the filename in the payload size */
int32_t payloadSize = static_cast<int32_t>(sizeof(SFileInfo) + strlen(entry.iName) + 1);
/* And send the response and its payload */
response.m_size = payloadSize;
SWAP(&response.m_size);
m_socket->write(&response, sizeof(response));
m_socket->write(fileInfo, payloadSize);
delete[] reinterpret_cast<unsigned char *>(fileInfo);
}
/* Otherwise just send a response with an empty payload */
else
{
response.m_size = 0;
m_socket->write(&response, sizeof(response));
}
}
/**
* Sends a file to the remote client.
* Transfers a file, if it exists, to the remote client. If the file does not exist then an error will be
* sent instead, to indicate this.
*
* @date Saturday 16-Jan-2021 11:54 am, Code HQ Bergmannstrasse
*/
void CGet::execute()
{
readPayload();
/* Extract the filename from the payload */
m_fileName = reinterpret_cast<char *>(m_payload);
int result;
TResponse response;
TEntry entry;
/* Determine if the file exists and send the result to the remote client */
result = response.m_result = Utils::GetFileInfo(m_fileName, &entry);
SWAP(&response.m_result);
/* If the file exists then send a response and a payload, containing the file's timestamp */
if (result == KErrNone)
{
/* Strip any path component from the file as we want it to be written to the current directory */
/* in the destination */
const char *fileName = Utils::filePart(m_fileName);
/* Include the size of just the filename in the payload size */
int32_t payloadSize = static_cast<int32_t>(sizeof(SFileInfo) + strlen(fileName) + 1);
/* Allocate an SFileInfo structure of a size large enough to hold the file's name */
SFileInfo *fileInfo = reinterpret_cast<SFileInfo *>(new unsigned char [payloadSize]);
/* Initialise it with the file's name and timestamp */
fileInfo->m_microseconds = entry.iModified.Int64();
SWAP64(&fileInfo->m_microseconds);
strcpy(fileInfo->m_fileName, fileName);
/* And finally send the response and its payload */
response.m_size = payloadSize;
SWAP(&response.m_size);
m_socket->write(&response, sizeof(response));
m_socket->write(fileInfo, payloadSize);
delete [] reinterpret_cast<unsigned char *>(fileInfo);
}
/* Otherwise display an error and just send a response with an empty payload */
else
{
Utils::Error("fileinfo: Unable to query information about file \"%s\"", m_fileName);
response.m_size = 0;
m_socket->write(&response, sizeof(response));
}
/* If the file exists then the remote client will be awaiting its transfer, so send it now */
if (response.m_result == KErrNone)
{
sendFile(m_fileName);
}
}
/**
* Renames a file.
* Renames the file specified by the client.
*
* @date Saturday 11-Mar-2023 6:47 am, Code HQ Tokyo Tsukuda
*/
void CRename::execute()
{
readPayload();
/* Extract the old and new filenames from the payload */
char *oldName = reinterpret_cast<char *>(m_payload);
char *newName = reinterpret_cast<char *>(m_payload + strlen(oldName) + 1);
printf("rename: Renaming file \"%s\" to \"%s\"\n", oldName, newName);
RFileUtils fileUtils;
TResponse response;
/* Rename the file and return the result to the client */
response.m_result = fileUtils.renameFile(oldName, newName);
SWAP(&response.m_result);
response.m_size = 0;
m_socket->write(&response, sizeof(response));
}
/**
* Receives a file from the remote client.
* Transfers a file from the remote client and writes it to a local file. The file will be written
* into the current directory.
*
* @date Wednesday 29-Jan-2020 12:38 pm, Scoot flight TR 735 to Singapore
*/
void CSend::execute()
{
readPayload();
/* Extract the file's information from the payload */
SFileInfo *fileInfo = reinterpret_cast<SFileInfo *>(m_payload);
SWAP64(&fileInfo->m_microseconds);
m_fileName = fileInfo->m_fileName;
/* Transfer the file from the remote client */
if (readFile(m_fileName) == KErrNone)
{
/* And set its datestamp and protection bits */
setFileInformation(*fileInfo);
}
}
/**
* Sends the server's protocol version.
* Sends the currently supported protocol version to the client. No validation checking is done as it is
* the client's responsibility to determine compatibility.
*
* @date Saturday 06-Feb-2021 7:02 am, Code HQ Bergmannstrasse
*/
void CVersion::execute()
{
readPayload();
uint32_t serverVersion = ((PROTOCOL_MAJOR << 16) | PROTOCOL_MINOR);
SWAP(&serverVersion);
m_socket->write(&serverVersion, sizeof(serverVersion));
}