From 6c1ab87005c1219d05a8a6e48a08790feccabf65 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 23 Jul 2013 12:16:38 +0200 Subject: [PATCH] Stop using shared memory This breaks features like bandwidth limitation! Patch v3 against mysecureshell 1.31 --- SftpServer/SftpWho.c | 86 +++++++++------------------------------------------- SftpServer/SftpWho.h | 2 +- main.c | 4 +-- 3 files changed, 17 insertions(+), 75 deletions(-) diff --git a/SftpServer/SftpWho.c b/SftpServer/SftpWho.c index c68a738..862eb96 100644 --- a/SftpServer/SftpWho.c +++ b/SftpServer/SftpWho.c @@ -20,7 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../config.h" #include #include -#include #include "SftpWho.h" #include #include @@ -28,9 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include -static char *_shmfile = "/dev/null"; -static int _shmkey = 0x0787; - typedef struct s_shm { t_sftpglobal global; @@ -47,79 +43,25 @@ t_sftpwho *SftWhoGetAllStructs() int SftpWhoDeleteStructs() { - key_t key; - int shmid; - - if ((key = ftok(_shmfile, _shmkey)) != -1 - && (shmid = shmget(key, sizeof(t_shm), 0)) != -1) - { - if (shmctl(shmid, IPC_RMID, 0) == -1) - return (0); - } + free(_sftpwho_ptr); + _sftpwho_ptr = NULL; return (1); } t_sftpwho *SftpWhoGetStruct(int create) { - void *ptr; - key_t key; - int shmid; - int eraze = 0; - int i, try, tryshm = 3; - - try_shm: - if ((key = ftok(_shmfile, _shmkey)) != -1) - { - //try to join to existing shm - if ((shmid = shmget(key, sizeof(t_shm), 0)) == -1) - if (create == 1) - { - shmid = shmget(key, sizeof(t_shm), IPC_CREAT | IPC_EXCL | 0666); - eraze = 1; - } - if (shmid == -1 && (errno == EINVAL || errno == EEXIST)) - { - //huho we have a old shm memory - if (tryshm > 0) - { - tryshm--; - _shmkey++; - goto try_shm; - } + if (create == 1) { + if (_sftpglobal) { + free(_sftpglobal); + } + _sftpglobal = (t_sftpglobal *)malloc(1 * sizeof(t_sftpglobal)); + + if (_sftpwho_ptr) { + free(_sftpwho_ptr); + } + _sftpwho_ptr = (t_sftpwho *)malloc(SFTPWHO_MAXCLIENT * sizeof(t_sftpwho)); } - if (shmid != -1 && (ptr = shmat(shmid, 0, 0)) != (void *)-1) - { - t_sftpwho *who = NULL; - t_shm *shm = ptr; - - _sftpglobal = &shm->global; - who = shm->who; - _sftpwho_ptr = who; - if (eraze == 1) - memset(shm, 0, sizeof(t_shm)); - else //clean all sessions of bugged client (abnormally quit) - (void )SftpWhoCleanBuggedClient(); - if (create == -1) - return (who); - //search a empty place :) - //try to search 3 times to prevent infinite loop - for (try = 0; try < 3; try++) - for (i = 0; i < SFTPWHO_MAXCLIENT; i++) - if (who[i].status == SFTPWHO_EMPTY) - { - (void )usleep(100); - if (who[i].status == SFTPWHO_EMPTY) - { - //clean all old infos - memset(&who[i], 0, sizeof(*who)); - //marked structure as occuped :) - who[i].status = SFTPWHO_IDLE; - return (&who[i]); - } - } - } - } - return (NULL); + return (_sftpwho_ptr); } //return number of connected clients @@ -166,7 +108,7 @@ void SftpWhoReleaseStruct(/*@null@*/ t_sftpwho *currentSession) currentSession->status = SFTPWHO_EMPTY; if (_sftpglobal != NULL) { - (void )shmdt(_sftpglobal); + free(_sftpglobal); _sftpglobal = NULL; } } diff --git a/SftpServer/SftpWho.h b/SftpServer/SftpWho.h index bd99e18..ab118a3 100644 --- a/SftpServer/SftpWho.h +++ b/SftpServer/SftpWho.h @@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef __SFTPWHO_H__ #define __SFTPWHO_H__ -#define SFTPWHO_MAXCLIENT 128 //128 x 716o = 89.5 ko +#define SFTPWHO_MAXCLIENT 1 #define SFTPWHO_EMPTY 0 #define SFTPWHO_IDLE 1 diff --git a/main.c b/main.c index 7775c90..bf2e950 100644 --- a/main.c +++ b/main.c @@ -186,8 +186,8 @@ int main(int ac, char **av, char **env) if (params->who == NULL) { mylog_printf(MYLOG_ERROR, - "[%s]Server '%s' reached maximum connexion (%i clients)", - hash_get("User"), hash_get("SERVER_IP"), SFTPWHO_MAXCLIENT); + "[%s]Out of heap memory", + hash_get("User")); SftpWhoReleaseStruct(NULL); delete_hash(); mylog_close(); -- 1.8.2.1