ftp_server_misc.c
Go to the documentation of this file.
1 /**
2  * @file ftp_server_misc.c
3  * @brief Helper functions for FTP server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL FTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ftp/ftp_server.h"
37 #include "ftp/ftp_server_control.h"
38 #include "ftp/ftp_server_data.h"
39 #include "ftp/ftp_server_misc.h"
40 #include "path.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (FTP_SERVER_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Handle periodic operations
49  * @param[in] context Pointer to the FTP server context
50  **/
51 
53 {
54  uint_t i;
56  FtpClientConnection *connection;
57 
58  //Get current time
60 
61  //Loop through the connection table
62  for(i = 0; i < context->settings.maxConnections; i++)
63  {
64  //Point to the current entry
65  connection = &context->connections[i];
66 
67  //Check the state of the current connection
68  if(connection->controlChannel.state != FTP_CHANNEL_STATE_CLOSED)
69  {
70  //Disconnect inactive client after idle timeout
71  if(timeCompare(time, connection->timestamp + FTP_SERVER_TIMEOUT) >= 0)
72  {
73  //Debug message
74  TRACE_INFO("FTP server: Closing inactive connection...\r\n");
75  //Close connection with the client
76  ftpServerCloseConnection(connection);
77  }
78  }
79  }
80 }
81 
82 
83 /**
84  * @brief Get a passive port number
85  * @param[in] context Pointer to the FTP server context
86  * @return Passive port number
87  **/
88 
90 {
91  uint_t port;
92 
93  //Retrieve current passive port number
94  port = context->passivePort;
95 
96  //Invalid port number?
97  if(port < context->settings.passivePortMin ||
98  port > context->settings.passivePortMax)
99  {
100  //Generate a random port number
101  port = netGetRandRange(context->settings.passivePortMin,
102  context->settings.passivePortMax);
103  }
104 
105  //Next passive port to use
106  if(port < context->settings.passivePortMax)
107  {
108  //Increment port number
109  context->passivePort = port + 1;
110  }
111  else
112  {
113  //Wrap around if necessary
114  context->passivePort = context->settings.passivePortMin;
115  }
116 
117  //Return the passive port number
118  return port;
119 }
120 
121 
122 /**
123  * @brief Retrieve the full pathname
124  * @param[in] connection Pointer to the client connection
125  * @param[in] inputPath Relative or absolute path
126  * @param[out] outputPath Resulting full path
127  * @param[in] maxLen Maximum acceptable path length
128  * @return Error code
129  **/
130 
132  const char_t *inputPath, char_t *outputPath, size_t maxLen)
133 {
134  size_t n;
135 
136  //Relative or absolute path?
137  if(pathIsRelative(inputPath))
138  {
139  //Sanity check
140  if(osStrlen(connection->currentDir) > maxLen)
141  return ERROR_FAILURE;
142 
143  //Copy current directory
144  osStrcpy(outputPath, connection->currentDir);
145  //Append the specified path
146  pathCombine(outputPath, inputPath, maxLen);
147  }
148  else
149  {
150  //Sanity check
151  if(osStrlen(connection->homeDir) > maxLen)
152  return ERROR_FAILURE;
153 
154  //Copy home directory
155  osStrcpy(outputPath, connection->homeDir);
156  //Append the specified path
157  pathCombine(outputPath, inputPath, maxLen);
158  }
159 
160  //Clean the resulting path
161  pathCanonicalize(outputPath);
162  pathRemoveSlash(outputPath);
163 
164  //Calculate the length of the home directory
165  n = osStrlen(connection->homeDir);
166 
167  //Make sure the pathname is valid
168  if(osStrncmp(outputPath, connection->homeDir, n) != 0)
169  return ERROR_INVALID_PATH;
170 
171  //Successful processing
172  return NO_ERROR;
173 }
174 
175 
176 /**
177  * @brief Get permissions for the specified file or directory
178  * @param[in] connection Pointer to the client connection
179  * @param[in] path Canonical path of the file
180  * @return Access rights for the specified file
181  **/
182 
184  const char_t *path)
185 {
186  size_t n;
187  uint_t perm;
188  FtpServerContext *context;
189 
190  //Point to the FTP server context
191  context = connection->context;
192 
193  //Calculate the length of the home directory
194  n = osStrlen(connection->homeDir);
195 
196  //Make sure the pathname is valid
197  if(osStrncmp(path, connection->homeDir, n) == 0)
198  {
199  //Strip root directory from the pathname
200  path = ftpServerStripRootDir(context, path);
201 
202  //Invoke user-defined callback, if any
203  if(context->settings.getFilePermCallback != NULL)
204  {
205  //Retrieve access rights for the specified file
206  perm = context->settings.getFilePermCallback(connection,
207  connection->user, path);
208  }
209  else
210  {
211  //Use default access rights
213  }
214  }
215  else
216  {
217  //The specified pathname is not valid
218  perm = 0;
219  }
220 
221  //Return access rights
222  return perm;
223 }
224 
225 
226 /**
227  * @brief Format a directory entry in UNIX-style format
228  * @param[in] dirEntry Pointer to the directory entry
229  * @param[in] perm Access rights for the specified file
230  * @param[out] buffer Buffer where to format the directory entry
231  * @return Length of resulting string, in bytes
232  **/
233 
234 size_t ftpServerFormatDirEntry(const FsDirEntry *dirEntry, uint_t perm,
235  char_t *buffer)
236 {
237  size_t n;
238  time_t time;
239  time_t modified;
240 
241  //Abbreviated months
242  static const char_t months[13][4] =
243  {
244  " ",
245  "Jan",
246  "Feb",
247  "Mar",
248  "Apr",
249  "May",
250  "Jun",
251  "Jul",
252  "Aug",
253  "Sep",
254  "Oct",
255  "Nov",
256  "Dec"
257  };
258 
259  //Format links, owner, group and size fields
260  n = osSprintf(buffer, "---------- 1 owner group %10" PRIu32,
261  dirEntry->size);
262 
263  //Check whether the current entry is a directory
264  if((dirEntry->attributes & FS_FILE_ATTR_DIRECTORY) != 0)
265  {
266  buffer[0] = 'd';
267  }
268 
269  //Read access permitted?
270  if((perm & FTP_FILE_PERM_READ) != 0)
271  {
272  buffer[1] = 'r';
273  buffer[4] = 'r';
274  buffer[7] = 'r';
275  }
276 
277  //Write access permitted?
278  if((perm & FTP_FILE_PERM_WRITE) != 0)
279  {
280  //Make sure the file is not marked as read-only
281  if((dirEntry->attributes & FS_FILE_ATTR_READ_ONLY) == 0)
282  {
283  buffer[2] = 'w';
284  buffer[5] = 'w';
285  buffer[8] = 'w';
286  }
287  }
288 
289  //Get current time
291  //Get modification time
292  modified = convertDateToUnixTime(&dirEntry->modified);
293 
294  //Check whether the modification time is within the previous 180 days
295  if(time > modified && time < (modified + FTP_SERVER_180_DAYS))
296  {
297  //The format of the date/time field is Mmm dd hh:mm
298  n += osSprintf(buffer + n, " %s %02" PRIu8 " %02" PRIu8 ":%02" PRIu8,
299  months[MIN(dirEntry->modified.month, 12)], dirEntry->modified.day,
300  dirEntry->modified.hours, dirEntry->modified.minutes);
301  }
302  else
303  {
304  //The format of the date/time field is Mmm dd yyyy
305  n += osSprintf(buffer + n, " %s %02" PRIu8 " %04" PRIu16,
306  months[MIN(dirEntry->modified.month, 12)], dirEntry->modified.day,
307  dirEntry->modified.year);
308  }
309 
310  //Append filename
311  n += osSprintf(buffer + n, " %s\r\n", dirEntry->name);
312 
313  //Return the length of the resulting string, in bytes
314  return n;
315 }
316 
317 
318 /**
319  * @brief Strip root dir from specified pathname
320  * @param[in] context Pointer to the FTP server context
321  * @param[in] path input pathname
322  * @return Resulting pathname with root dir stripped
323  **/
324 
326  const char_t *path)
327 {
328  //Default directory
329  static const char_t defaultDir[] = "/";
330 
331  //Local variables
332  size_t m;
333  size_t n;
334 
335  //Retrieve the length of the root directory
336  n = osStrlen(context->settings.rootDir);
337  //Retrieve the length of the specified pathname
338  m = osStrlen(path);
339 
340  //Strip the root dir from the specified pathname
341  if(n <= 1)
342  {
343  return path;
344  }
345  else if(n < m)
346  {
347  return path + n;
348  }
349  else
350  {
351  return defaultDir;
352  }
353 }
354 
355 
356 /**
357  * @brief Strip home directory from specified pathname
358  * @param[in] connection Pointer to the client connection
359  * @param[in] path input pathname
360  * @return Resulting pathname with home directory stripped
361  **/
362 
364  const char_t *path)
365 {
366  //Default directory
367  static const char_t defaultDir[] = "/";
368 
369  //Local variables
370  size_t m;
371  size_t n;
372 
373  //Retrieve the length of the home directory
374  n = osStrlen(connection->homeDir);
375  //Retrieve the length of the specified pathname
376  m = osStrlen(path);
377 
378  //Strip the home directory from the specified pathname
379  if(n <= 1)
380  {
381  return path;
382  }
383  else if(n < m)
384  {
385  return path + n;
386  }
387  else
388  {
389  return defaultDir;
390  }
391 }
392 
393 
394 /**
395  * @brief Close client connection properly
396  * @param[in] connection Pointer to the client connection to be closed
397  **/
398 
400 {
401  //Close data connection
402  ftpServerCloseDataChannel(connection);
403  //Close control connection
404  ftpServerCloseControlChannel(connection);
405 
406  //Valid file pointer?
407  if(connection->file != NULL)
408  {
409  //Close file
410  fsCloseFile(connection->file);
411  connection->file = NULL;
412  }
413 
414  //Valid directory pointer?
415  if(connection->dir != NULL)
416  {
417  //Close directory
418  fsCloseDir(connection->dir);
419  connection->dir = NULL;
420  }
421 }
422 
423 #endif
#define FtpServerContext
Definition: ftp_server.h:208
Path manipulation helper functions.
const char_t * ftpServerStripRootDir(FtpServerContext *context, const char_t *path)
Strip root dir from specified pathname.
uint32_t netGetRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:417
const char_t * ftpServerStripHomeDir(FtpClientConnection *connection, const char_t *path)
Strip home directory from specified pathname.
FTP data connection.
size_t ftpServerFormatDirEntry(const FsDirEntry *dirEntry, uint_t perm, char_t *buffer)
Format a directory entry in UNIX-style format.
time_t convertDateToUnixTime(const DateTime *date)
Convert date to Unix timestamp.
Definition: date_time.c:266
uint16_t year
Definition: date_time.h:48
bool_t pathIsRelative(const char_t *path)
Test if the path is relative.
Definition: path.c:61
#define osStrlen(s)
Definition: os_port.h:168
#define timeCompare(t1, t2)
Definition: os_port.h:40
Helper functions for FTP server.
void ftpServerTick(FtpServerContext *context)
Handle periodic operations.
uint8_t day
Definition: date_time.h:50
@ FTP_CHANNEL_STATE_CLOSED
Definition: ftp_server.h:226
@ FS_FILE_ATTR_DIRECTORY
Definition: fs_port.h:61
void pathCanonicalize(char_t *path)
Simplify a path.
Definition: path.c:158
uint8_t minutes
Definition: date_time.h:53
error_t
Error codes.
Definition: error.h:43
#define osSprintf(dest,...)
Definition: os_port.h:234
void ftpServerCloseDataChannel(FtpClientConnection *connection)
Close data connection.
void fsCloseFile(FsFile *file)
Close a file.
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t ftpServerGetPath(FtpClientConnection *connection, const char_t *inputPath, char_t *outputPath, size_t maxLen)
Retrieve the full pathname.
void ftpServerCloseConnection(FtpClientConnection *connection)
Close client connection properly.
#define FTP_SERVER_TIMEOUT
Definition: ftp_server.h:74
uint8_t hours
Definition: date_time.h:52
#define TRACE_INFO(...)
Definition: debug.h:105
#define MIN(a, b)
Definition: os_port.h:63
#define FTP_SERVER_180_DAYS
@ ERROR_INVALID_PATH
Definition: error.h:147
@ FTP_FILE_PERM_LIST
Definition: ftp_server.h:279
uint32_t systime_t
System time.
uint16_t port
Definition: dns_common.h:267
char_t name[FS_MAX_NAME_LEN+1]
Definition: fs_port.h:112
uint8_t month
Definition: date_time.h:49
char char_t
Definition: compiler_port.h:55
uint32_t time
DateTime modified
Definition: fs_port.h:111
uint_t ftpServerGetFilePermissions(FtpClientConnection *connection, const char_t *path)
Get permissions for the specified file or directory.
uint8_t m
Definition: ndp.h:304
uint8_t n
Directory entry.
Definition: fs_port.h:108
uint32_t attributes
Definition: fs_port.h:109
uint32_t size
Definition: fs_port.h:110
void ftpServerCloseControlChannel(FtpClientConnection *connection)
Close control connection.
FTP server (File Transfer Protocol)
#define FtpClientConnection
Definition: ftp_server.h:212
#define osStrncmp(s1, s2, length)
Definition: os_port.h:180
void pathRemoveSlash(char_t *path)
Remove the trailing slash from a given path.
Definition: path.c:360
@ FTP_FILE_PERM_READ
Definition: ftp_server.h:280
void fsCloseDir(FsDir *dir)
Close a directory stream.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ FS_FILE_ATTR_READ_ONLY
Definition: fs_port.h:57
uint16_t ftpServerGetPassivePort(FtpServerContext *context)
Get a passive port number.
#define osStrcpy(s1, s2)
Definition: os_port.h:210
FTP control connection.
__weak_func time_t getCurrentUnixTime(void)
Get current time.
Definition: date_time.c:186
@ FTP_FILE_PERM_WRITE
Definition: ftp_server.h:281
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void pathCombine(char_t *path, const char_t *more, size_t maxLen)
Concatenate two paths.
Definition: path.c:394
systime_t osGetSystemTime(void)
Retrieve system time.