scp_server_directory.c
Go to the documentation of this file.
1 /**
2  * @file scp_server_directory.c
3  * @brief Directory operations
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH 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.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SCP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "scp/scp_server.h"
37 #include "scp/scp_server_file.h"
39 #include "scp/scp_server_misc.h"
40 #include "path.h"
41 #include "debug.h"
42 
43 //Check SSH stack configuration
44 #if (SCP_SERVER_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Create a directory
49  * @param[in] session Handle referencing an SCP session
50  * @param[in] name Directory name
51  * @return Error code
52  **/
53 
55 {
56  error_t error;
57  uint_t perm;
58 
59  //Change current directory
60  pathCombine(session->path, name, SCP_SERVER_MAX_PATH_LEN);
61  pathCanonicalize(session->path);
62  pathRemoveSlash(session->path);
63 
64  //Check whether the directory exists or not
65  if(!fsDirExists(session->path))
66  {
67  //Retrieve permissions for the specified directory
68  perm = scpServerGetFilePermissions(session, session->path);
69 
70  //Check access rights
71  if((perm & SCP_FILE_PERM_WRITE) != 0)
72  {
73  //Create a new directory
74  error = fsCreateDir(session->path);
75 
76  //Failed to create directory?
77  if(error)
78  {
79  //Report an error
81  }
82  }
83  else
84  {
85  //Insufficient access rights
86  error = ERROR_ACCESS_DENIED;
87  }
88  }
89  else
90  {
91  //The directory already exists
92  error = NO_ERROR;
93  }
94 
95  //Check status code
96  if(!error)
97  {
98  //Increment recursion level
99  session->dirLevel++;
100  }
101 
102  //Return status code
103  return error;
104 }
105 
106 
107 /**
108  * @brief Open a directory
109  * @param[in] session Handle referencing an SCP session
110  * @return Error code
111  **/
112 
114 {
115  error_t error;
116  uint_t perm;
117 
118  //Retrieve permissions for the specified directory
119  perm = scpServerGetFilePermissions(session, session->path);
120 
121  //Check access rights
122  if((perm & SCP_FILE_PERM_READ) != 0)
123  {
124  //Open the specified directory
125  session->dir[session->dirLevel] = fsOpenDir(session->path);
126 
127  //Valid directory pointer?
128  if(session->dir[session->dirLevel] != NULL)
129  {
130  //The mode bits determine what actions the owner of the file can
131  //perform on the file
132  session->fileMode = SCP_MODE_IRWXU | SCP_MODE_IRWXG | SCP_MODE_IRWXO;
133 
134  //Successful processing
135  error = NO_ERROR;
136  }
137  else
138  {
139  //Failed to open the directory
141  }
142  }
143  else
144  {
145  //Insufficient access rights
146  error = ERROR_ACCESS_DENIED;
147  }
148 
149  //Return status code
150  return error;
151 }
152 
153 
154 /**
155  * @brief Fetch the next entry from the directory
156  * @param[in] session Handle referencing an SCP session
157  **/
158 
160 {
161  error_t error;
162  uint_t perm;
163  FsDirEntry dirEntry;
164 
165  //Loop through the directory
166  while(1)
167  {
168  //Read a new entry from the directory
169  error = fsReadDir(session->dir[session->dirLevel], &dirEntry);
170 
171  //Check status code
172  if(!error)
173  {
174  //Check file name
175  if(osStrcmp(dirEntry.name, ".") == 0 ||
176  osStrcmp(dirEntry.name, "..") == 0)
177  {
178  //Discard "." and ".." entries
179  }
180  else
181  {
182  //Retrieve the full path name
183  pathCombine(session->path, dirEntry.name, SCP_SERVER_MAX_PATH_LEN);
184  pathCanonicalize(session->path);
185 
186  //Retrieve permissions for the specified file
187  perm = scpServerGetFilePermissions(session, session->path);
188 
189  //Check access rights
190  if((perm & SCP_FILE_PERM_LIST) != 0)
191  {
192  //Check file type
193  if((dirEntry.attributes & FS_FILE_ATTR_DIRECTORY) != 0)
194  {
195  //Ensure the maximum recursion depth is not exceeded
196  if((session->dirLevel + 1) < SCP_SERVER_MAX_RECURSION_LEVEL)
197  {
198  //Increment recursion level
199  session->dirLevel++;
200 
201  //Process the directory recursively
202  error = scpServerOpenDir(session);
203 
204  //Failed to open directory?
205  if(error)
206  {
207  //Clean up side effects
208  session->dirLevel--;
209  }
210  }
211  else
212  {
213  //Maximum recursion depth exceeded
214  error = ERROR_OPEN_FAILED;
215  }
216  }
217  else
218  {
219  //Open the file for reading
220  error = scpServerOpenFileForReading(session);
221  }
222 
223  //Valid directory entry?
224  if(!error)
225  {
226  break;
227  }
228  }
229 
230  //Remove the file name from the path
231  pathRemoveFilename(session->path);
232  pathRemoveSlash(session->path);
233  }
234  }
235  else
236  {
237  //The end of the directory has been reached
238  break;
239  }
240  }
241 
242  //End of the directory?
243  if(error)
244  {
245  //Close directory
246  fsCloseDir(session->dir[session->dirLevel]);
247  session->dir[session->dirLevel] = NULL;
248 
249  //Change to the parent directory
250  if(session->dirLevel > 0)
251  {
252  pathRemoveFilename(session->path);
253  pathRemoveSlash(session->path);
254  }
255  }
256 
257  //The source side feeds the commands and the target side consumes them
258  session->state = SCP_SERVER_SESSION_STATE_READ_COMMAND;
259 }
260 
261 #endif
Path manipulation helper functions.
error_t scpServerCreateDir(ScpServerSession *session, const char_t *name)
Create a directory.
Helper functions for SCP server.
#define SCP_SERVER_MAX_RECURSION_LEVEL
Definition: scp_server.h:102
char_t name[]
#define osStrcmp(s1, s2)
Definition: os_port.h:171
error_t scpServerOpenFileForReading(ScpServerSession *session)
Open a file for reading.
#define ScpServerSession
Definition: scp_server.h:113
#define SCP_MODE_IRWXO
Definition: scp_common.h:41
void scpServerGetNextDirEntry(ScpServerSession *session)
Fetch the next entry from the directory.
error_t fsCreateDir(const char_t *path)
Create a directory.
@ SCP_FILE_PERM_READ
Definition: scp_server.h:139
#define SCP_SERVER_MAX_PATH_LEN
Definition: scp_server.h:95
Directory operations.
FsDir * fsOpenDir(const char_t *path)
Open a directory stream.
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ FS_FILE_ATTR_DIRECTORY
Definition: fs_port.h:61
void pathCanonicalize(char_t *path)
Simplify a path.
Definition: path.c:158
error_t
Error codes.
Definition: error.h:43
error_t fsReadDir(FsDir *dir, FsDirEntry *dirEntry)
Read an entry from the specified directory stream.
error_t scpServerOpenDir(ScpServerSession *session)
Open a directory.
@ ERROR_ACCESS_DENIED
Definition: error.h:148
SCP server.
@ SCP_FILE_PERM_WRITE
Definition: scp_server.h:140
File operations.
@ ERROR_DIRECTORY_NOT_FOUND
Definition: error.h:164
@ SCP_SERVER_SESSION_STATE_READ_COMMAND
Definition: scp_server.h:158
char_t name[FS_MAX_NAME_LEN+1]
Definition: fs_port.h:112
char char_t
Definition: compiler_port.h:48
uint_t scpServerGetFilePermissions(ScpServerSession *session, const char_t *path)
Get permissions for the specified file or directory.
#define SCP_MODE_IRWXG
Definition: scp_common.h:45
@ SCP_FILE_PERM_LIST
Definition: scp_server.h:138
Directory entry.
Definition: fs_port.h:108
bool_t fsDirExists(const char_t *path)
Check whether a directory exists.
uint32_t attributes
Definition: fs_port.h:109
void pathRemoveSlash(char_t *path)
Remove the trailing slash from a given path.
Definition: path.c:360
void fsCloseDir(FsDir *dir)
Close a directory stream.
unsigned int uint_t
Definition: compiler_port.h:50
Secure Shell (SSH)
void pathRemoveFilename(char_t *path)
Remove the trailing file name from the supplied path.
Definition: path.c:120
@ 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
#define SCP_MODE_IRWXU
Definition: scp_common.h:49