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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/**
* Feathery FTP-Server <https://sourceforge.net/projects/feathery>
* Copyright (C) 2005-2010 Andreas Martin (andreas.martin@linuxmail.org)
*
* ftpSession.c - Session handling of connected clients
*
* A ftp session is a TCP connection between the server and a ftp client.
* Each session has its own unique session id. The session id refers
* (index in session[]) to a struct ftpSession_S which holds all
* relevant data. The virtual chroot environment is also part of the
* session management.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include "ftpTypes.h"
#include "ftpConfig.h"
#include "ftp.h"
/**
* @brief array that holds the data of all ftp sessions
*
* The index represents the session ID.
*/
LOCAL ftpSession_S sessions[MAX_CONNECTIONS];
/**
* @brief number of active file or directory transmission
*/
LOCAL int actTransCnt;
/**
* @brief Scratch-buffer for path manipulation
*/
LOCAL char pathScratchBuf[MAX_PATH_LEN];
/**
* @brief Opens a ftp session
*
* Searches in 'sessions' for an empty entry and inits the session data.
* Call this function after accepting a tcp connection from a ftp client.
*
* @param ctrlSocket control socket which initiated the the ftp session
* @param remoteIp IP address of the client
* @param remotePort TCP-Port of the client
*
* @return session id or -1 if session limit is reached
*/
int ftpOpenSession(socket_t ctrlSocket, ip_t remoteIp, port_t remotePort)
{
int n;
for(n = 0; n < MAX_CONNECTIONS; n++)
{
if(!sessions[n].open)
{
sessions[n].open = TRUE;
sessions[n].authenticated = FALSE;
sessions[n].userId = 0;
sessions[n].workingDir[0] = '\0';
sessions[n].remoteIp = remoteIp;
sessions[n].remotePort = remotePort;
sessions[n].remoteDataPort = 0;
sessions[n].passive = FALSE;
sessions[n].binary = TRUE;
sessions[n].timeLastCmd = ftpGetUnixTime();
sessions[n].ctrlSocket = ctrlSocket;
sessions[n].passiveDataSocket = -1;
sessions[n].rxBufWriteIdx = 0;
sessions[n].activeTrans.op = OP_NOP;
sessions[n].activeTrans.fsHandle = NULL;
sessions[n].activeTrans.dataSocket = -1;
sessions[n].activeTrans.fileSize = 0;
if(VERBOSE_MODE_ENABLED) printf("ftpOpenSession started for ctrlSocket: %d\n",ctrlSocket);
return n;
}
}
return -1;
}
/**
* @brief Marks the current ftp session as 'authenticated'
*
* @param id Session id
*
* @return 0
*/
int ftpAuthSession(int id)
{
sessions[id].authenticated = TRUE;
strcpy(sessions[id].workingDir, "/");
return 0;
}
/**
* @brief Closes a ftp session
*
* Closes the TCP control connection and releases the session struct.
*
* @param id Session id
*
* @return 0
*/
int ftpCloseSession(int id)
{
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = %u, port = %d, ctrlSocket = %d\n",
id, sessions[id].remoteIp, sessions[id].remoteFTPServerPassivePort,sessions[id].ctrlSocket);
if(ftpFindExternalFTPServerIp != NULL && ftpFindExternalFTPServerIp(sessions[id].remoteIp) != 0)
{
//if(ftpRemoveUPNPPortForward)
//{
//if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort);
//ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort);
sessions[id].remoteFTPServerPassivePort = 0;
//}
}
//if(sessions[id].open) {
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession about to Close socket = %d, dataSocket = %d, activeDataSocket = %d, for sessionId = %d\n",sessions[id].ctrlSocket,sessions[id].passiveDataSocket,sessions[id].activeTrans.dataSocket,id);
ftpUntrackSocket(sessions[id].ctrlSocket);
ftpCloseSocket(&sessions[id].ctrlSocket);
ftpCloseTransmission(id);
ftpUntrackSocket(sessions[id].passiveDataSocket);
ftpCloseSocket(&sessions[id].passiveDataSocket);
//}
sessions[id].remoteIp = 0;
sessions[id].ctrlSocket = 0;
sessions[id].passiveDataSocket = 0;
sessions[id].passiveIp = 0;
sessions[id].passivePort = 0;
sessions[id].activeTrans.dataSocket = 0;
sessions[id].activeTrans.op = OP_NOP;
sessions[id].activeTrans.fileSize = 0;
sessions[id].open = FALSE;
if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id);
return 0;
}
/**
* @brief Returns pointer to session struct
*
* @param id Session id
*
* @return pointer to session data
*/
ftpSession_S* ftpGetSession(int id)
{
return &sessions[id];
}
/**
* @brief Normalizes a path string
*
* The function resolves all dot and doubledot directory entries in the
* passed path. If the normalized path refers beyond root, root is returned.
*
* @param path string of an absolute path
*
* @return 0
*/
LOCAL int normalizePath(char* path)
{
char *in;
char *r = path;
in = path;
while((in = strchr(in, '/')))
{
if(!strncmp(in, "/./", 3))
{
ftpStrcpy(in, &in[2]);
continue;
}
if(!strcmp(in, "/."))
{
in[1] = '\0';
continue;
}
if(!strncmp(in, "/../", 4))
{
*in = '\0';
r = strrchr(path, '/');
if(r == NULL)
r = path;
ftpStrcpy(r, &in[3]);
in = r;
continue;
}
if(!strcmp(in, "/.."))
{
*in = '\0';
r = strrchr(path, '/');
if(r)
{
if(r == path)
r[1] = '\0';
else
*r = '\0';
}
else
{
in[0] = '/';
in[1] = '\0';
}
continue;
}
in++;
}
return 0;
}
/**
* @brief Translate a ftp session path to server path
*
* The translated path depends on the current working directory of the
* session and the root path of the session. In addition the path will
* be normalized.
* The server path is generated as followed:
* passed path is relative => server path = ftp user root + session working dir + path
* passed path is absolute => server path = ftp user root + path
*
* @param id Session id
* @param path ftp session path (can be relative or absolute)
* @param if != 0 dot and doubledot directories will be resolved
*
* @return translated absolute server path
*/
const char* ftpGetRealPath(int id, const char* path, int normalize)
{
char ftpRoot[2048]="";
int ftpRootLen;
int len;
const char *ftp_rootget = ftpGetRoot(sessions[id].userId, &len);
snprintf(ftpRoot,2047,"%s",ftp_rootget);
ftpRootLen = (int)strlen(ftpRoot);
if(ftpRootLen > 0 && ftpRoot[ftpRootLen-1] != '/') {
strcat(ftpRoot,"/");
}
if(VERBOSE_MODE_ENABLED) printf("#1 ftpGetRealPath id = %d path [%s] ftpRoot [%s] sessions[id].workingDir [%s] normalize = %d\n", id, path, ftpRoot, sessions[id].workingDir,normalize);
pathScratchBuf[0]='\0';
if(path[0] == '/' || strcmp(path,sessions[id].workingDir) == 0) // absolute path?
{
ftpMergePaths(pathScratchBuf, ftpRoot, path, NULL);
}
else
{
ftpMergePaths(pathScratchBuf, ftpRoot, sessions[id].workingDir, "/", path, NULL);
//ftpMergePaths(pathScratchBuf, ftpRoot, path, NULL);
}
if(VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathScratchBuf [%s]\n", path, ftpRoot, pathScratchBuf);
ftpRemoveDoubleSlash(pathScratchBuf);
if(normalize) {
normalizePath(pathScratchBuf);
}
ftpRemoveTrailingSlash(pathScratchBuf);
if(VERBOSE_MODE_ENABLED) printf("#2 ftpGetRealPath path [%s] ftpRoot [%s] pathScratchBuf [%s]\n", path, ftpRoot, pathScratchBuf);
return pathScratchBuf;
}
/**
* @brief Change the current working directory of the session
*
* @param id Session id
* @param path destination ftp path (can be relative or absolute)
*
* @return 0 on success; -2 if the directory doesn't exist
*/
int ftpChangeDir(int id, const char* path)
{
ftpPathInfo_S fileInfo;
const char* realPath = ftpGetRealPath(id, path, TRUE);
int len;
ftpGetRoot(sessions[id].userId, &len); // determine len of root-path
if(len == 1) // if len == 1 root-path == '/'
len = 0;
if(VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] sessions[id].workingDir [%s]\n", path, realPath, sessions[id].workingDir);
if(ftpStat(realPath, &fileInfo) || (fileInfo.type != TYPE_DIR)) // directory accessible?
return -2;
strncpy(sessions[id].workingDir, &realPath[len], MAX_PATH_LEN-1); // apply path
if(sessions[id].workingDir[0] == '\0')
strcpy(sessions[id].workingDir, "/");
if(VERBOSE_MODE_ENABLED) printf("ftpChangeDir path [%s] realPath [%s] NEW sessions[id].workingDir [%s]\n", path, realPath, sessions[id].workingDir);
return 0;
}
/**
* Open an ftp transmission
*/
void ftpOpenTransmission(int id, operation_E op, void* fsHandle, socket_t dataSocket, uint32_t fileSize)
{
actTransCnt++;
sessions[id].activeTrans.op = op;
sessions[id].activeTrans.fsHandle = fsHandle;
sessions[id].activeTrans.dataSocket = dataSocket;
sessions[id].activeTrans.fileSize = fileSize;
}
/**
* Close an ftp transmission
*/
void ftpCloseTransmission(int id)
{
if(VERBOSE_MODE_ENABLED) printf("In ftpCloseTransmission about to Close socket = %d, for sessionId = %d, fsHandle [%p] op = %d\n",
sessions[id].activeTrans.dataSocket, id,sessions[id].activeTrans.fsHandle,sessions[id].activeTrans.op);
if(sessions[id].activeTrans.dataSocket > 0)
{
ftpUntrackSocket(sessions[id].activeTrans.dataSocket);
ftpCloseSocket(&sessions[id].activeTrans.dataSocket);
}
if(sessions[id].activeTrans.op != OP_NOP) // is thera an active transmission?
{
if(sessions[id].activeTrans.op == OP_LIST)
{
ftpCloseDir(sessions[id].activeTrans.fsHandle);
}
else
{
ftpCloseFile(sessions[id].activeTrans.fsHandle);
}
sessions[id].activeTrans.fsHandle = NULL;
sessions[id].activeTrans.op = OP_NOP;
sessions[id].activeTrans.dataSocket = 0;
actTransCnt--;
}
}
/**
* Get the active transaction count
*/
int ftpGetActiveTransCnt(void)
{
return actTransCnt;
}
|