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.0
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, ".") || !osStrcmp(dirEntry.name, ".."))
176  {
177  //Discard "." and ".." entries
178  }
179  else
180  {
181  //Retrieve the full path name
182  pathCombine(session->path, dirEntry.name, SCP_SERVER_MAX_PATH_LEN);
183  pathCanonicalize(session->path);
184 
185  //Retrieve permissions for the specified file
186  perm = scpServerGetFilePermissions(session, session->path);
187 
188  //Check access rights
189  if((perm & SCP_FILE_PERM_LIST) != 0)
190  {
191  //Check file type
192  if((dirEntry.attributes & FS_FILE_ATTR_DIRECTORY) != 0)
193  {
194  //Ensure the maximum recursion depth is not exceeded
195  if((session->dirLevel + 1) < SCP_SERVER_MAX_RECURSION_LEVEL)
196  {
197  //Increment recursion level
198  session->dirLevel++;
199 
200  //Process the directory recursively
201  error = scpServerOpenDir(session);
202 
203  //Failed to open directory?
204  if(error)
205  {
206  //Clean up side effects
207  session->dirLevel--;
208  }
209  }
210  else
211  {
212  //Maximum recursion depth exceeded
213  error = ERROR_OPEN_FAILED;
214  }
215  }
216  else
217  {
218  //Open the file for reading
219  error = scpServerOpenFileForReading(session);
220  }
221 
222  //Valid directory entry?
223  if(!error)
224  {
225  break;
226  }
227  }
228 
229  //Remove the file name from the path
230  pathRemoveFilename(session->path);
231  pathRemoveSlash(session->path);
232  }
233  }
234  else
235  {
236  //The end of the directory has been reached
237  break;
238  }
239  }
240 
241  //End of the directory?
242  if(error)
243  {
244  //Close directory
245  fsCloseDir(session->dir[session->dirLevel]);
246  session->dir[session->dirLevel] = NULL;
247 
248  //Change to the parent directory
249  if(session->dirLevel > 0)
250  {
251  pathRemoveFilename(session->path);
252  pathRemoveSlash(session->path);
253  }
254  }
255 
256  //The source side feeds the commands and the target side consumes them
257  session->state = SCP_SERVER_SESSION_STATE_READ_COMMAND;
258 }
259 
260 #endif
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ ERROR_ACCESS_DENIED
Definition: error.h:148
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_DIRECTORY_NOT_FOUND
Definition: error.h:164
@ FS_FILE_ATTR_DIRECTORY
Definition: fs_port.h:61
bool_t fsDirExists(const char_t *path)
Check whether a directory exists.
FsDir * fsOpenDir(const char_t *path)
Open a directory stream.
error_t fsCreateDir(const char_t *path)
Create a directory.
void fsCloseDir(FsDir *dir)
Close a directory stream.
error_t fsReadDir(FsDir *dir, FsDirEntry *dirEntry)
Read an entry from the specified directory stream.
#define osStrcmp(s1, s2)
Definition: os_port.h:171
void pathCombine(char_t *path, const char_t *more, size_t maxLen)
Concatenate two paths.
Definition: path.c:370
void pathCanonicalize(char_t *path)
Simplify a path.
Definition: path.c:150
void pathRemoveFilename(char_t *path)
Remove the trailing file name from the supplied path.
Definition: path.c:112
void pathRemoveSlash(char_t *path)
Remove the trailing slash from a given path.
Definition: path.c:340
Path manipulation helper functions.
char_t name[]
#define SCP_MODE_IRWXO
Definition: scp_common.h:41
#define SCP_MODE_IRWXU
Definition: scp_common.h:49
#define SCP_MODE_IRWXG
Definition: scp_common.h:45
SCP server.
@ SCP_SERVER_SESSION_STATE_READ_COMMAND
Definition: scp_server.h:158
@ SCP_FILE_PERM_READ
Definition: scp_server.h:139
@ SCP_FILE_PERM_LIST
Definition: scp_server.h:138
@ SCP_FILE_PERM_WRITE
Definition: scp_server.h:140
#define SCP_SERVER_MAX_RECURSION_LEVEL
Definition: scp_server.h:102
#define ScpServerSession
Definition: scp_server.h:113
#define SCP_SERVER_MAX_PATH_LEN
Definition: scp_server.h:95
error_t scpServerOpenDir(ScpServerSession *session)
Open a directory.
void scpServerGetNextDirEntry(ScpServerSession *session)
Fetch the next entry from the directory.
error_t scpServerCreateDir(ScpServerSession *session, const char_t *name)
Create a directory.
Directory operations.
error_t scpServerOpenFileForReading(ScpServerSession *session)
Open a file for reading.
File operations.
uint_t scpServerGetFilePermissions(ScpServerSession *session, const char_t *path)
Get permissions for the specified file or directory.
Helper functions for SCP server.
Secure Shell (SSH)
Directory entry.
Definition: fs_port.h:108
char_t name[FS_MAX_NAME_LEN+1]
Definition: fs_port.h:112
uint32_t attributes
Definition: fs_port.h:109