- disallow non compressed tileset downloads when client IP not on same subnet as host

- bugfix for LAN server broadcasting
- bugfix for empty message on player disconnect (now properly shows that player switched to AI mode)
This commit is contained in:
Mark Vejvoda
2011-11-29 05:07:18 +00:00
parent 28ae161c25
commit 7cf9c189b2
13 changed files with 153 additions and 34 deletions

View File

@@ -139,6 +139,8 @@ extern void ftpCloseTransmission(int id);
extern int ftpGetActiveTransCnt(void);
extern int ftpFindAccount(const char* name);
extern const char * ftpFindAccountById(int userid);
extern int ftpCheckPassword(int userId, const char* passw);
extern int ftpCheckAccRights(int userId, int accRights);
extern const char* ftpGetRoot(int userId, int* len);

View File

@@ -38,7 +38,9 @@
extern "C" {
#endif
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4);
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2,
ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4,
ftpIsClientAllowedToGetFileType cb5);
int ftpCreateAccount(const char* name, const char* passw, const char* root, int accRights);
int ftpDeleteAccount(const char* name);
int ftpStart(int portNumber);

View File

@@ -70,11 +70,13 @@ typedef ip_t (*ftpFindExternalFTPServerIpType)(ip_t clientIp);
typedef void (*ftpAddUPNPPortForwardType)(int internalPort, int externalPort);
typedef void (*ftpRemoveUPNPPortForwardType)(int internalPort, int externalPort);
typedef int (*ftpIsValidClientType)(ip_t clientIp);
typedef int (*ftpIsClientAllowedToGetFileType)(ip_t clientIp, const char *username, const char *filename);
ftpFindExternalFTPServerIpType ftpFindExternalFTPServerIp;
ftpAddUPNPPortForwardType ftpAddUPNPPortForward;
ftpRemoveUPNPPortForwardType ftpRemoveUPNPPortForward;
ftpIsValidClientType ftpIsValidClient;
ftpIsClientAllowedToGetFileType ftpIsClientAllowedToGetFile;
#ifdef __cplusplus
}

View File

@@ -54,6 +54,7 @@ namespace Shared { namespace Platform {
class FTPClientValidationInterface {
public:
virtual int isValidClientType(uint32 clientIp) = 0;
virtual int isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename) = 0;
};
@@ -83,6 +84,7 @@ public:
Ip();
Ip(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned char byte3);
Ip(const string& ipString);
static void Inet_NtoA(uint32 addr, char * ipbuf);
unsigned char getByte(int byteIndex) {return bytes[byteIndex];}
string getString() const;

View File

@@ -129,6 +129,18 @@ int ftpFindAccount(const char* name)
return 0;
}
const char * ftpFindAccountById(int userid)
{
if(userid == 0) {
return 0;
}
else if(ftpUsers[userid - 1].name[0] == '\0') {
return 0;
}
return ftpUsers[userid - 1].name;
}
/**
* @brief Checks the password of a user account
*
@@ -143,12 +155,15 @@ int ftpFindAccount(const char* name)
*/
int ftpCheckPassword(int userId, const char* passw)
{
if(!userId)
if(userId == 0) {
return -1;
else if(ftpUsers[userId - 1].passw[0] == '\0')
}
else if(ftpUsers[userId - 1].passw[0] == '\0') {
return 0;
else
}
else {
return strncmp(ftpUsers[userId - 1].passw, passw, MAXLEN_PASSWORD);
}
}
/**

View File

@@ -524,6 +524,16 @@ LOCAL int ftpCmdRetr(int sessionId, const char* args, int len)
return 2;
}
if(ftpIsClientAllowedToGetFile != NULL) {
if(ftpIsClientAllowedToGetFile(ftpGetSession(sessionId)->remoteIp,ftpFindAccountById(ftpGetSession(sessionId)->userId),realPath) != 1) {
if(VERBOSE_MODE_ENABLED) printf("ERROR In ftpCmdRetr FILE DISALLOWED By MGserver [file not available] args [%s] realPath [%s]\n", args, realPath);
ftpSendMsg(MSG_NORMAL, sessionId, 550, ftpMsg032);
return 2;
}
}
if(ftpGetSession(sessionId)->passive == FALSE)
{
s = ftpEstablishDataConnection(FALSE, &ftpGetSession(sessionId)->remoteIp, &ftpGetSession(sessionId)->remoteDataPort,sessionId);

View File

@@ -39,11 +39,14 @@ LOCAL int serverListenPort;
LOCAL int serverPassiveListenPort;
//LOCAL socket_t serverPassivePort;
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2, ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4) {
void ftpInit(ftpFindExternalFTPServerIpType cb1, ftpAddUPNPPortForwardType cb2,
ftpRemoveUPNPPortForwardType cb3, ftpIsValidClientType cb4,
ftpIsClientAllowedToGetFileType cb5) {
ftpFindExternalFTPServerIp = cb1;
ftpAddUPNPPortForward = cb2;
ftpRemoveUPNPPortForward = cb3;
ftpIsValidClient = cb4;
ftpIsClientAllowedToGetFile = cb5;
}
int ftpGetListenPort()

View File

@@ -254,12 +254,17 @@ LOCAL int normalizePath(char* path)
*/
const char* ftpGetRealPath(int id, const char* path, int normalize)
{
const char* ftpRoot;
char ftpRoot[2048]="";
int ftpRootLen;
int len;
ftpRoot = ftpGetRoot(sessions[id].userId, &len);
strcpy(ftpRoot,ftpGetRoot(sessions[id].userId, &len));
ftpRootLen = 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);
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?

View File

@@ -46,6 +46,17 @@ int isValidClientType(ip_t clientIp) {
return result;
}
int isClientAllowedToGetFile(ip_t clientIp, const char *username, const char *filename) {
int result = 1;
//printf("In [%s::%s] Line: %d username [%s] file [%s]\n",__FILE__,__FUNCTION__,__LINE__,username,filename);
if(FTPServerThread::getFtpValidationIntf() != NULL) {
result = FTPServerThread::getFtpValidationIntf()->isClientAllowedToGetFile(clientIp,username,filename);
//printf("In [%s::%s] Line: %d username [%s] file [%s] result = %d\n",__FILE__,__FUNCTION__,__LINE__,username,filename,result);
}
return result;
}
FTPServerThread::FTPServerThread(std::pair<string,string> mapsPath,
std::pair<string,string> tilesetsPath, std::pair<string,string> techtreesPath,
bool internetEnabledFlag,
@@ -62,7 +73,8 @@ FTPServerThread::FTPServerThread(std::pair<string,string> mapsPath,
this->maxPlayers = maxPlayers;
this->ftpValidationIntf = ftpValidationIntf;
ftpInit(&FindExternalFTPServerIp,&UPNP_Tools::AddUPNPPortForward,&UPNP_Tools::RemoveUPNPPortForward, &isValidClientType);
ftpInit(&FindExternalFTPServerIp,&UPNP_Tools::AddUPNPPortForward,&UPNP_Tools::RemoveUPNPPortForward,
&isValidClientType, &isClientAllowedToGetFile);
VERBOSE_MODE_ENABLED = SystemFlags::VERBOSE_MODE_ENABLED;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("***FTP SERVER STARTED [%p]\n",this);

View File

@@ -342,7 +342,7 @@ static uint32 SockAddrToUint32(struct sockaddr * a) {
}
// convert a numeric IP address into its string representation
static void Inet_NtoA(uint32 addr, char * ipbuf)
void Ip::Inet_NtoA(uint32 addr, char * ipbuf)
{
sprintf(ipbuf, "%d.%d.%d.%d", (addr>>24)&0xFF, (addr>>16)&0xFF, (addr>>8)&0xFF, (addr>>0)&0xFF);
}
@@ -384,9 +384,9 @@ static void PrintNetworkInterfaceInfos()
uint32 dstAddr = SockAddrToUint32(p->ifa_dstaddr);
if (ifaAddr > 0)
{
char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr);
char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr);
char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr);
char ifaAddrStr[32]; Ip::Inet_NtoA(ifaAddr, ifaAddrStr);
char maskAddrStr[32]; Ip::Inet_NtoA(maskAddr, maskAddrStr);
char dstAddrStr[32]; Ip::Inet_NtoA(dstAddr, dstAddrStr);
printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
}
p = p->ifa_next;
@@ -485,9 +485,9 @@ static void PrintNetworkInterfaceInfos()
uint32 baddr = ipAddr & netmask;
if (row.dwBCastAddr) baddr |= ~netmask;
char ifaAddrStr[32]; Inet_NtoA(ipAddr, ifaAddrStr);
char maskAddrStr[32]; Inet_NtoA(netmask, maskAddrStr);
char dstAddrStr[32]; Inet_NtoA(baddr, dstAddrStr);
char ifaAddrStr[32]; Ip::Inet_NtoA(ipAddr, ifaAddrStr);
char maskAddrStr[32]; Ip::Inet_NtoA(netmask, maskAddrStr);
char dstAddrStr[32]; Ip::Inet_NtoA(baddr, dstAddrStr);
printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
}
@@ -522,9 +522,9 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress)
{
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr);
char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr);
char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr);
char ifaAddrStr[32]; Ip::Inet_NtoA(ifaAddr, ifaAddrStr);
char maskAddrStr[32]; Ip::Inet_NtoA(maskAddr, maskAddrStr);
char dstAddrStr[32]; Ip::Inet_NtoA(dstAddr, dstAddrStr);
//printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) {
broadCastAddress = dstAddrStr;
@@ -628,9 +628,9 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress)
uint32 baddr = ipAddr & netmask;
if (row.dwBCastAddr) baddr |= ~netmask;
char ifaAddrStr[32]; Inet_NtoA(ipAddr, ifaAddrStr);
char maskAddrStr[32]; Inet_NtoA(netmask, maskAddrStr);
char dstAddrStr[32]; Inet_NtoA(baddr, dstAddrStr);
char ifaAddrStr[32]; Ip::Inet_NtoA(ipAddr, ifaAddrStr);
char maskAddrStr[32]; Ip::Inet_NtoA(netmask, maskAddrStr);
char dstAddrStr[32]; Ip::Inet_NtoA(baddr, dstAddrStr);
//printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) {
broadCastAddress = dstAddrStr;
@@ -682,7 +682,7 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
//memcpy(&(SockAddr.sin_addr),&myhostent->h_addr[ipIdx],myhostent->h_length);
//SockAddr.sin_family = myhostent->h_addrtype;
//Inet_NtoA(SockAddrToUint32((sockaddr *)&SockAddr), myhostaddr);
Inet_NtoA(SockAddrToUint32((struct in_addr *)myhostent->h_addr_list[ipIdx]), myhostaddr);
Ip::Inet_NtoA(SockAddrToUint32((struct in_addr *)myhostent->h_addr_list[ipIdx]), myhostaddr);
//printf("ipIdx = %d [%s]\n",ipIdx,myhostaddr);
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr);
@@ -738,7 +738,7 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
struct sockaddr_in *pSockAddr = (struct sockaddr_in *)&ifr.ifr_addr;
if(pSockAddr != NULL) {
//sprintf(myhostaddr, "%s",inet_ntoa(pSockAddr->sin_addr));
Inet_NtoA(SockAddrToUint32(&pSockAddr->sin_addr), myhostaddr);
Ip::Inet_NtoA(SockAddrToUint32(&pSockAddr->sin_addr), myhostaddr);
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] szBuf [%s], myhostaddr = [%s], ifr.ifr_flags = %d, ifrA.ifr_flags = %d, ifr.ifr_name [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf,myhostaddr,ifr.ifr_flags,ifrA.ifr_flags,ifr.ifr_name);
// Now only include interfaces that are both UP and running
@@ -1855,8 +1855,8 @@ void BroadCastClientSocketThread::execute() {
struct sockaddr_in bcaddr; // The broadcast address for the receiver.
PLATFORM_SOCKET bcfd; // The file descriptor used for the broadcast.
//bool one = true; // Parameter for "setscokopt".
char buff[10024]; // Buffers the data to be broadcasted.
socklen_t alen;
char buff[10024]=""; // Buffers the data to be broadcasted.
socklen_t alen=0;
port = htons( Socket::getBroadCastPort() );
@@ -1892,16 +1892,18 @@ void BroadCastClientSocketThread::execute() {
// Keep getting packets forever.
for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 5; ) {
alen = sizeof(struct sockaddr);
int nb=0; // The number of bytes read.
int nb=0;// The number of bytes read.
bool gotData = (nb = recvfrom(bcfd, buff, 10024, 0, (struct sockaddr *) &bcSender, &alen)) > 0;
//printf("Broadcasting client nb = %d buff [%s] gotData = %d\n",nb,buff,gotData);
if(gotData == false) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"recvfrom failed: %s\n", getLastSocketErrorFormattedText().c_str());
}
else {
//string fromIP = inet_ntoa(bcSender.sin_addr);
char szHostFrom[100]="";
Inet_NtoA(SockAddrToUint32(&bcSender.sin_addr), szHostFrom);
Ip::Inet_NtoA(SockAddrToUint32(&bcSender.sin_addr), szHostFrom);
//printf("Client szHostFrom [%s]\n",szHostFrom);
string fromIP = szHostFrom;
@@ -2190,7 +2192,7 @@ Socket *ServerSocket::accept() {
}
else {
Inet_NtoA(SockAddrToUint32((struct sockaddr *)&cli_addr), client_host);
Ip::Inet_NtoA(SockAddrToUint32((struct sockaddr *)&cli_addr), client_host);
//printf("client_host [%s]\n",client_host);
//sprintf(client_host, "%s",inet_ntoa(cli_addr.sin_addr));
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got connection, newSock = %d client_host [%s]\n",__FILE__,__FUNCTION__,__LINE__,newSock,client_host);
@@ -2547,7 +2549,7 @@ void UPNP_Tools::NETremRedirects(int ext_port) {
//
BroadCastSocketThread::BroadCastSocketThread() : BaseThread() {
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
setPauseBroadcast(false);
//printf("new broadcast thread [%p]\n",this);
}
@@ -2666,16 +2668,21 @@ void BroadCastSocketThread::execute() {
if(difftime(time(NULL),elapsed) >= 1 && getQuitStatus() == false) {
elapsed = time(NULL);
if(getPauseBroadcast() == false) {
ssize_t send_res = 0;
bool pauseBroadCast = getPauseBroadcast();
if(pauseBroadCast == false) {
// Broadcast the packet to the subnet
//if( sendto( bcfd, buff, sizeof(buff) + 1, 0 , (struct sockaddr *)&bcaddr, sizeof(struct sockaddr_in) ) != sizeof(buff) + 1 )
if( sendto( bcfd[idx], buff, buffMaxSize, 0 , (struct sockaddr *)&bcLocal[idx], sizeof(struct sockaddr_in) ) != buffMaxSize ) {
send_res = sendto( bcfd[idx], buff, buffMaxSize, 0 , (struct sockaddr *)&bcLocal[idx], sizeof(struct sockaddr_in) );
if( send_res != buffMaxSize ) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Sendto error: %s\n", getLastSocketErrorFormattedText().c_str());
}
else {
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Broadcasting on port [%d] the message: [%s]\n",Socket::getBroadCastPort(),buff);
}
}
//printf("Broadcasting server send_res = %d buff [%s] ip [%s] getPauseBroadcast() = %d\n",send_res,buff,ipSubnetMaskList[idx].c_str(),pauseBroadCast);
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}