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.2
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->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->passivePortMin || port > context->passivePortMax)
98  {
99  //Generate a random port number
100  port = netGetRandRange(context->passivePortMin, context->passivePortMax);
101  }
102 
103  //Next passive port to use
104  if(port < context->passivePortMax)
105  {
106  //Increment port number
107  context->passivePort = port + 1;
108  }
109  else
110  {
111  //Wrap around if necessary
112  context->passivePort = context->passivePortMin;
113  }
114 
115  //Return the passive port number
116  return port;
117 }
118 
119 
120 /**
121  * @brief Retrieve the full pathname
122  * @param[in] connection Pointer to the client connection
123  * @param[in] inputPath Relative or absolute path
124  * @param[out] outputPath Resulting full path
125  * @param[in] maxLen Maximum acceptable path length
126  * @return Error code
127  **/
128 
130  const char_t *inputPath, char_t *outputPath, size_t maxLen)
131 {
132  size_t n;
133 
134  //Relative or absolute path?
135  if(pathIsRelative(inputPath))
136  {
137  //Sanity check
138  if(osStrlen(connection->currentDir) > maxLen)
139  return ERROR_FAILURE;
140 
141  //Copy current directory
142  osStrcpy(outputPath, connection->currentDir);
143  //Append the specified path
144  pathCombine(outputPath, inputPath, maxLen);
145  }
146  else
147  {
148  //Sanity check
149  if(osStrlen(connection->rootDir) > maxLen)
150  return ERROR_FAILURE;
151 
152  //Copy root directory
153  osStrcpy(outputPath, connection->rootDir);
154  //Append the specified path
155  pathCombine(outputPath, inputPath, maxLen);
156  }
157 
158  //Clean the resulting path
159  pathCanonicalize(outputPath);
160  pathRemoveSlash(outputPath);
161 
162  //Calculate the length of the root directory
163  n = osStrlen(connection->rootDir);
164 
165  //Make sure the pathname is valid
166  if(osStrncmp(outputPath, connection->rootDir, n) != 0)
167  return ERROR_INVALID_PATH;
168 
169  //Successful processing
170  return NO_ERROR;
171 }
172 
173 
174 /**
175  * @brief Get permissions for the specified file or directory
176  * @param[in] connection Pointer to the client connection
177  * @param[in] path Canonical path of the file
178  * @return Access rights for the specified file
179  **/
180 
182  const char_t *path)
183 {
184  size_t n;
185  uint_t perm;
186  FtpServerContext *context;
187 
188  //Point to the FTP server context
189  context = connection->context;
190 
191  //Calculate the length of the root directory
192  n = osStrlen(connection->rootDir);
193 
194  //Make sure the pathname is valid
195  if(osStrncmp(path, connection->rootDir, n) == 0)
196  {
197  //Strip root directory from the pathname
198  path = ftpServerStripRootDir(context, path);
199 
200  //Invoke user-defined callback, if any
201  if(context->getFilePermCallback != NULL)
202  {
203  //Retrieve access rights for the specified file
204  perm = context->getFilePermCallback(connection, connection->user,
205  path);
206  }
207  else
208  {
209  //Use default access rights
211  }
212  }
213  else
214  {
215  //The specified pathname is not valid
216  perm = 0;
217  }
218 
219  //Return access rights
220  return perm;
221 }
222 
223 
224 /**
225  * @brief Format a directory entry in UNIX-style format
226  * @param[in] dirEntry Pointer to the directory entry
227  * @param[in] perm Access rights for the specified file
228  * @param[out] buffer Buffer where to format the directory entry
229  * @return Length of resulting string, in bytes
230  **/
231 
232 size_t ftpServerFormatDirEntry(const FsDirEntry *dirEntry, uint_t perm,
233  char_t *buffer)
234 {
235  size_t n;
236  time_t time;
237  time_t modified;
238 
239  //Abbreviated months
240  static const char_t months[13][4] =
241  {
242  " ",
243  "Jan",
244  "Feb",
245  "Mar",
246  "Apr",
247  "May",
248  "Jun",
249  "Jul",
250  "Aug",
251  "Sep",
252  "Oct",
253  "Nov",
254  "Dec"
255  };
256 
257  //Format links, owner, group and size fields
258  n = osSprintf(buffer, "---------- 1 owner group %10" PRIu32,
259  dirEntry->size);
260 
261  //Check whether the current entry is a directory
262  if((dirEntry->attributes & FS_FILE_ATTR_DIRECTORY) != 0)
263  {
264  buffer[0] = 'd';
265  }
266 
267  //Read access permitted?
268  if((perm & FTP_FILE_PERM_READ) != 0)
269  {
270  buffer[1] = 'r';
271  buffer[4] = 'r';
272  buffer[7] = 'r';
273  }
274 
275  //Write access permitted?
276  if((perm & FTP_FILE_PERM_WRITE) != 0)
277  {
278  //Make sure the file is not marked as read-only
279  if((dirEntry->attributes & FS_FILE_ATTR_READ_ONLY) == 0)
280  {
281  buffer[2] = 'w';
282  buffer[5] = 'w';
283  buffer[8] = 'w';
284  }
285  }
286 
287  //Get current time
289  //Get modification time
290  modified = convertDateToUnixTime(&dirEntry->modified);
291 
292  //Check whether the modification time is within the previous 180 days
293  if(time > modified && time < (modified + FTP_SERVER_180_DAYS))
294  {
295  //The format of the date/time field is Mmm dd hh:mm
296  n += osSprintf(buffer + n, " %s %02" PRIu8 " %02" PRIu8 ":%02" PRIu8,
297  months[MIN(dirEntry->modified.month, 12)], dirEntry->modified.day,
298  dirEntry->modified.hours, dirEntry->modified.minutes);
299  }
300  else
301  {
302  //The format of the date/time field is Mmm dd yyyy
303  n += osSprintf(buffer + n, " %s %02" PRIu8 " %04" PRIu16,
304  months[MIN(dirEntry->modified.month, 12)], dirEntry->modified.day,
305  dirEntry->modified.year);
306  }
307 
308  //Append filename
309  n += osSprintf(buffer + n, " %s\r\n", dirEntry->name);
310 
311  //Return the length of the resulting string, in bytes
312  return n;
313 }
314 
315 
316 /**
317  * @brief Strip root directory from specified pathname
318  * @param[in] context Pointer to the FTP server context
319  * @param[in] path input pathname
320  * @return Resulting pathname with root directory stripped
321  **/
322 
324  const char_t *path)
325 {
326  //Default directory
327  static const char_t defaultDir[] = "/";
328 
329  //Local variables
330  size_t m;
331  size_t n;
332 
333  //Retrieve the length of the root directory
334  n = osStrlen(context->rootDir);
335  //Retrieve the length of the specified pathname
336  m = osStrlen(path);
337 
338  //Strip the root dir from the specified pathname
339  if(n <= 1)
340  {
341  return path;
342  }
343  else if(n < m)
344  {
345  return path + n;
346  }
347  else
348  {
349  return defaultDir;
350  }
351 }
352 
353 
354 /**
355  * @brief Strip user's root directory from specified pathname
356  * @param[in] connection Pointer to the client connection
357  * @param[in] path input pathname
358  * @return Resulting pathname with user's root directory stripped
359  **/
360 
362  const char_t *path)
363 {
364  //Default directory
365  static const char_t defaultDir[] = "/";
366 
367  //Local variables
368  size_t m;
369  size_t n;
370 
371  //Retrieve the length of the user's root directory
372  n = osStrlen(connection->rootDir);
373  //Retrieve the length of the specified pathname
374  m = osStrlen(path);
375 
376  //Strip the user's root directory from the specified pathname
377  if(n <= 1)
378  {
379  return path;
380  }
381  else if(n < m)
382  {
383  return path + n;
384  }
385  else
386  {
387  return defaultDir;
388  }
389 }
390 
391 
392 /**
393  * @brief Close client connection properly
394  * @param[in] connection Pointer to the client connection to be closed
395  **/
396 
398 {
399  //Close data connection
400  ftpServerCloseDataChannel(connection);
401  //Close control connection
402  ftpServerCloseControlChannel(connection);
403 
404  //Valid file pointer?
405  if(connection->file != NULL)
406  {
407  //Close file
408  fsCloseFile(connection->file);
409  connection->file = NULL;
410  }
411 
412  //Valid directory pointer?
413  if(connection->dir != NULL)
414  {
415  //Close directory
416  fsCloseDir(connection->dir);
417  connection->dir = NULL;
418  }
419 }
420 
421 #endif
#define FtpServerContext
Definition: ftp_server.h:201
Path manipulation helper functions.
const char_t * ftpServerStripRootDir(FtpServerContext *context, const char_t *path)
Strip root directory from specified pathname.
uint32_t netGetRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:417
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:219
@ FS_FILE_ATTR_DIRECTORY
Definition: fs_port.h:61
void pathCanonicalize(char_t *path)
Simplify a path.
Definition: path.c:162
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.
const char_t * ftpServerStripUserRootDir(FtpClientConnection *connection, const char_t *path)
Strip user's root directory from specified pathname.
#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:272
uint32_t systime_t
System time.
uint16_t port
Definition: dns_common.h:269
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:205
#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:376
@ FTP_FILE_PERM_READ
Definition: ftp_server.h:273
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:274
@ 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:414
systime_t osGetSystemTime(void)
Retrieve system time.