home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
- #include "accounts.h"
- #include "iputils.h"
-
- t_directory::t_directory()
- {
- bAutoCreate = FALSE;
- }
-
- t_group::t_group()
- {
- pOwner = NULL;
-
- for (int i = 0; i < 2; i++)
- {
- nSpeedLimitType[i] = 0;
- nSpeedLimit[i] = 10;
- nBypassServerSpeedLimit[i] = 0;
- }
- nEnabled = 1;
- forceSsl = 0;
- }
-
- t_group& t_group::operator=(const t_group &a)
- {
- group = a.group;
- nBypassUserLimit = a.nBypassUserLimit;
- nUserLimit = a.nUserLimit;
- nIpLimit = a.nIpLimit;
- permissions = a.permissions;
- nEnabled = a.nEnabled;
- disallowedIPs = a.disallowedIPs;
- allowedIPs = a.allowedIPs;
- comment = a.comment;
- forceSsl = a.forceSsl;
-
- for (int i = 0; i < 2; i++)
- {
- nBypassServerSpeedLimit[i] = a.nBypassServerSpeedLimit[i];
- nSpeedLimit[i] = a.nSpeedLimit[i];
- nSpeedLimitType[i] = a.nSpeedLimitType[i];
- SpeedLimits[i] = a.SpeedLimits[i];
- }
-
- return *this;
- }
-
- bool t_group::BypassUserLimit() const
- {
- if (!nBypassUserLimit)
- return false;
- if (nBypassUserLimit == 2 && pOwner)
- return pOwner->BypassUserLimit();
- return true;
- }
-
- int t_group::GetIpLimit() const
- {
- if (nIpLimit)
- return nIpLimit;
- if (pOwner)
- return pOwner->GetIpLimit();
- return 0;
- }
-
- int t_group::GetUserLimit() const
- {
- if (nUserLimit)
- return nUserLimit;
- if (pOwner)
- return pOwner->GetUserLimit();
- return 0;
- }
-
- t_user::t_user()
- {
- }
-
- t_user& t_user::operator=(const t_user &a)
- {
- group = a.group;
- pOwner = a.pOwner;
- user=a.user;
- password=a.password;
- nBypassUserLimit = a.nBypassUserLimit;
- nUserLimit = a.nUserLimit;
- nIpLimit = a.nIpLimit;
- permissions = a.permissions;
- nEnabled = a.nEnabled;
- disallowedIPs = a.disallowedIPs;
- allowedIPs = a.allowedIPs;
- comment = a.comment;
- forceSsl = a.forceSsl;
-
- for (int i = 0; i < 2; i++)
- {
- nBypassServerSpeedLimit[i] = a.nBypassServerSpeedLimit[i];
- nSpeedLimit[i] = a.nSpeedLimit[i];
- nSpeedLimitType[i] = a.nSpeedLimitType[i];
- SpeedLimits[i] = a.SpeedLimits[i];
- }
-
- return *this;
- }
-
- unsigned char * t_group::ParseBuffer(unsigned char *pBuffer, int length)
- {
- unsigned char *p = pBuffer;
- unsigned char *endMarker = pBuffer + length;
-
- if (!ParseString(endMarker, p, group))
- return 0;
-
- if ((endMarker - p) < 11)
- return NULL;
-
- memcpy(&nIpLimit, p, 4);
- p += 4;
- memcpy(&nUserLimit, p, 4);
- p += 4;
-
- int options = *p++;
-
- nBypassUserLimit = options & 0x03;
- nEnabled = (options >> 2) & 0x03;
-
- // Parse IP filter rules.
- int numDisallowedIPs = (int(*p) << 8) + p[1];
- p += 2;
- while (numDisallowedIPs--)
- {
- CString ip;
- if (!ParseString(endMarker, p, ip))
- return 0;
-
- if (IsValidAddressFilter(ip) || ip == "*")
- disallowedIPs.push_back(ip);
- }
-
- if ((endMarker - p) < 2)
- return NULL;
-
- int numAllowedIPs = (int(*p) << 8) + p[1];
- p += 2;
- while (numAllowedIPs--)
- {
- CString ip;
- if (!ParseString(endMarker, p, ip))
- return 0;
-
- if (IsValidAddressFilter(ip) || ip == "*")
- allowedIPs.push_back(ip);
- }
-
- if ((endMarker - p) < 2)
- return NULL;
-
- int dircount = (int(*p) << 8) + p[1];
- p += 2;
-
- BOOL bGotHome = FALSE;
-
- for (int j = 0; j < dircount; j++)
- {
- t_directory dir;
-
- if (!ParseString(endMarker, p, dir.dir))
- return 0;
-
- dir.dir.TrimRight("\\");
-
- if (dir.dir == "")
- return 0;
-
- // Get directory aliases.
- if ((endMarker - p) < 2)
- return NULL;
-
- int aliascount = (int(*p) << 8) + p[1];
- p += 2;
-
- for (int i = 0; i < aliascount; i++)
- {
- CString alias;
- if (!ParseString(endMarker, p, alias))
- return 0;
-
- alias.TrimRight("\\");
-
- if (alias == "")
- return 0;
-
- dir.aliases.push_back(alias);
- }
-
- if ((endMarker - p) < 2)
- return NULL;
-
- int rights = (int(*p) << 8) + p[1];
- p += 2;
-
- dir.bDirCreate = rights & 0x0001 ? 1:0;
- dir.bDirDelete = rights & 0x0002 ? 1:0;
- dir.bDirList = rights & 0x0004 ? 1:0;
- dir.bDirSubdirs = rights & 0x0008 ? 1:0;
- dir.bFileAppend = rights & 0x0010 ? 1:0;
- dir.bFileDelete = rights & 0x0020 ? 1:0;
- dir.bFileRead = rights & 0x0040 ? 1:0;
- dir.bFileWrite = rights & 0x0080 ? 1:0;
- dir.bIsHome = rights & 0x0100 ? 1:0;
- dir.bAutoCreate = rights & 0x0200 ? 1:0;
-
- // Avoid multiple home directories.
- if (dir.bIsHome)
- if (!bGotHome)
- bGotHome = TRUE;
- else
- dir.bIsHome = FALSE;
-
- permissions.push_back(dir);
- }
-
- for (int i = 0; i < 2; i++)
- {
- if ((endMarker - p) < 5)
- return NULL;
-
- nSpeedLimitType[i] = *p & 3;
- nBypassServerSpeedLimit[i] = (*p++ >> 2) & 3;
-
- nSpeedLimit[i] = int(*p++) << 8;
- nSpeedLimit[i] |= *p++;
-
- if (!nSpeedLimit[i])
- nSpeedLimit[i] = 10;
-
- int num = (int(*p) << 8) + p[1];
- p += 2;
- while (num--)
- {
- CSpeedLimit sl;
- p = sl.ParseBuffer(p, length-(int)(p-pBuffer));
- if (!p)
- return NULL;
- SpeedLimits[i].push_back(sl);
- }
- }
-
- if (!ParseString(endMarker, p, comment))
- return 0;
-
- if (p >= endMarker)
- return 0;
-
- forceSsl = *p++;
-
- return p;
- }
-
- char * t_group::FillBuffer(char *p) const
- {
- *p++ = group.GetLength() >> 8;
- *p++ = group.GetLength() & 0xff;
- memcpy(p, group, group.GetLength());
- p += group.GetLength();
-
- memcpy(p, &nIpLimit, 4);
- p += 4;
- memcpy(p, &nUserLimit, 4);
- p += 4;
-
- int options = nBypassUserLimit & 3;
- options |= (nEnabled & 3) << 2;
-
- *p++ = options & 0xff;
-
- std::list<CString>::const_iterator ipLimitIter;
-
- *p++ = (char)(disallowedIPs.size() >> 8);
- *p++ = (char)(disallowedIPs.size() & 0xff);
- for (ipLimitIter = disallowedIPs.begin(); ipLimitIter != disallowedIPs.end(); ipLimitIter++)
- {
- *p++ = (char)(ipLimitIter->GetLength() >> 8);
- *p++ = (char)(ipLimitIter->GetLength() & 0xff);
- memcpy(p, *ipLimitIter, ipLimitIter->GetLength());
- p += ipLimitIter->GetLength();
- }
-
- *p++ = (char)(allowedIPs.size() >> 8);
- *p++ = (char)(allowedIPs.size() & 0xff);
- for (ipLimitIter = allowedIPs.begin(); ipLimitIter != allowedIPs.end(); ipLimitIter++)
- {
- *p++ = (char)(ipLimitIter->GetLength() >> 8);
- *p++ = (char)(ipLimitIter->GetLength() & 0xff);
- memcpy(p, *ipLimitIter, ipLimitIter->GetLength());
- p += ipLimitIter->GetLength();
- }
-
- *p++ = (char)(permissions.size() >> 8);
- *p++ = (char)(permissions.size() & 0xff);
- for (std::vector<t_directory>::const_iterator permissioniter = permissions.begin(); permissioniter!=permissions.end(); permissioniter++)
- {
- *p++ = (char)(permissioniter->dir.GetLength() >> 8);
- *p++ = (char)(permissioniter->dir.GetLength() & 0xff);
- memcpy(p, permissioniter->dir, permissioniter->dir.GetLength());
- p += permissioniter->dir.GetLength();
-
- *p++ = (char)(permissioniter->aliases.size() >> 8);
- *p++ = (char)(permissioniter->aliases.size() & 0xff);
- for (std::list<CString>::const_iterator aliasiter = permissioniter->aliases.begin(); aliasiter != permissioniter->aliases.end(); aliasiter++)
- {
- *p++ = (char)(aliasiter->GetLength() >> 8);
- *p++ = (char)(aliasiter->GetLength() & 0xff);
- memcpy(p, *aliasiter, aliasiter->GetLength());
- p += aliasiter->GetLength();
- }
-
- int rights = 0;
- rights |= permissioniter->bDirCreate ? 0x0001:0;
- rights |= permissioniter->bDirDelete ? 0x0002:0;
- rights |= permissioniter->bDirList ? 0x0004:0;
- rights |= permissioniter->bDirSubdirs ? 0x0008:0;
- rights |= permissioniter->bFileAppend ? 0x0010:0;
- rights |= permissioniter->bFileDelete ? 0x0020:0;
- rights |= permissioniter->bFileRead ? 0x0040:0;
- rights |= permissioniter->bFileWrite ? 0x0080:0;
- rights |= permissioniter->bIsHome ? 0x0100:0;
- rights |= permissioniter->bAutoCreate ? 0x0200:0;
- *p++ = (char)(rights >> 8);
- *p++ = (char)(rights & 0xff);
- }
-
- for (int i = 0; i < 2; i++)
- {
- *p++ = (char)((nSpeedLimitType[i] & 3) + ((nBypassServerSpeedLimit[i] & 3) << 2));
- *p++ = (char)(nSpeedLimit[i] >> 8);
- *p++ = (char)(nSpeedLimit[i] & 0xff);
-
- SPEEDLIMITSLIST::const_iterator iter;
-
- *p++ = (char)(SpeedLimits[i].size() >> 8);
- *p++ = (char)(SpeedLimits[i].size() & 0xff);
- for (iter = SpeedLimits[i].begin(); (iter != SpeedLimits[i].end()) && p; iter++)
- p = iter->FillBuffer(p);
- if (!p)
- return NULL;
- }
-
- *p++ = (char)(comment.GetLength() >> 8);
- *p++ = (char)(comment.GetLength() & 0xff);
- memcpy(p, comment, comment.GetLength());
- p += comment.GetLength();
-
- *p++ = (char)forceSsl;
-
- return p;
- }
-
- int t_group::GetRequiredBufferLen() const
- {
- int len = 9;
- len += group.GetLength() + 2;
-
- len += 4;
- std::list<CString>::const_iterator ipLimitIter;
- for (ipLimitIter = disallowedIPs.begin(); ipLimitIter != disallowedIPs.end(); ipLimitIter++)
- len += ipLimitIter->GetLength() + 2;
- for (ipLimitIter = allowedIPs.begin(); ipLimitIter != allowedIPs.end(); ipLimitIter++)
- len += ipLimitIter->GetLength() + 2;
-
- len += 2;
- for (std::vector<t_directory>::const_iterator permissioniter = permissions.begin(); permissioniter!=permissions.end(); permissioniter++)
- {
- t_directory directory = *permissioniter;
- len += 2;
- len += directory.dir.GetLength() + 2;
-
- len += 2;
- for (std::list<CString>::const_iterator aliasiter = permissioniter->aliases.begin(); aliasiter != permissioniter->aliases.end(); aliasiter++)
- len += aliasiter->GetLength() + 2;
- }
-
- // Speed limits.
- len += 6; // Basic limits.
- len += 4; // Number of rules.
- for (int i = 0; i < 2; i++)
- {
- SPEEDLIMITSLIST::const_iterator iter;
- for (iter = SpeedLimits[i].begin(); iter != SpeedLimits[i].end(); iter++)
- len += iter->GetRequiredBufferLen();
- }
-
- len += 2 + comment.GetLength();
-
- len++; //forceSsl
-
- return len;
- }
-
- int t_group::GetCurrentSpeedLimit(sltype type) const
- {
- switch (nSpeedLimitType[type])
- {
- case 0:
- if (pOwner)
- return pOwner->GetCurrentSpeedLimit(type);
- else
- return 0;
- case 1:
- return 0;
- case 2:
- return nSpeedLimit[type];
- case 3:
- {
- SYSTEMTIME st;
- GetSystemTime(&st);
- for (SPEEDLIMITSLIST::const_iterator iter = SpeedLimits[type].begin(); iter != SpeedLimits[type].end(); iter++)
- if (iter->IsItActive(st))
- return iter->m_Speed;
- }
- if (pOwner)
- return pOwner->GetCurrentSpeedLimit(type);
- else
- return 0;
- }
- return 0;
- }
-
- bool t_group::BypassServerSpeedLimit(sltype type) const
- {
- if (nBypassServerSpeedLimit[type] == 1)
- return true;
- else if (!nBypassServerSpeedLimit[type])
- return false;
- else if (pOwner)
- return pOwner->BypassServerSpeedLimit(type);
- else
- return false;
- }
-
- bool t_group::IsEnabled() const
- {
- switch (nEnabled)
- {
- default:
- case 0:
- return false;
- case 1:
- return true;
- case 2:
- if (!pOwner)
- return false;
-
- return pOwner->IsEnabled();
- }
- }
-
- bool t_group::AccessAllowed(SOCKADDR_IN sockAddr) const
- {
- unsigned int ip = htonl(sockAddr.sin_addr.s_addr);
- const char *pIp = inet_ntoa(sockAddr.sin_addr);
-
- bool disallowed = false;
-
- std::list<CString>::const_iterator iter;
- for (iter = disallowedIPs.begin(); iter != disallowedIPs.end(); iter++)
- {
- if (disallowed = MatchesFilter(*iter, ip, pIp))
- break;
- }
-
- if (!disallowed)
- {
- if (!pOwner)
- return true;
-
- if (pOwner->AccessAllowed(sockAddr))
- return true;
- }
-
- for (iter = allowedIPs.begin(); iter != allowedIPs.end(); iter++)
- {
- if (MatchesFilter(*iter, ip, pIp))
- return true;
- }
-
- if (pOwner && !disallowed)
- return pOwner->AccessAllowed(sockAddr);
-
- return false;
- }
-
- unsigned char * t_user::ParseBuffer(unsigned char *pBuffer, int length)
- {
- unsigned char *p = pBuffer;
- unsigned char *endMarker = pBuffer + length;
-
- p = t_group::ParseBuffer(p, length);
- if (!p)
- return NULL;
-
- if (!ParseString(endMarker, p, user))
- return 0;
-
- if (!ParseString(endMarker, p, password))
- return 0;
-
- return p;
- }
-
- char * t_user::FillBuffer(char *p) const
- {
- p = t_group::FillBuffer(p);
- if (!p)
- return NULL;
-
- *p++ = user.GetLength() >> 8;
- *p++ = user.GetLength() & 0xff;
- memcpy(p, user, user.GetLength());
- p += user.GetLength();
-
- *p++ = password.GetLength() >> 8;
- *p++ = password.GetLength() & 0xff;
- memcpy(p, password, password.GetLength());
- p += password.GetLength();
-
- return p;
- }
-
- int t_user::GetRequiredBufferLen() const
- {
- int len = t_group::GetRequiredBufferLen();
- len += user.GetLength() + 2;
- len += password.GetLength() + 2;
- return len;
- }
-
- bool t_group::ParseString(const unsigned char* endMarker, unsigned char *&p, CString &string)
- {
- if ((endMarker - p) < 2)
- return false;
-
- int len = *p * 256 + p[1];
- p += 2;
-
- if ((endMarker - p) < len)
- return false;
- char *pStr = string.GetBuffer(len);
- if (!pStr)
- return false;
- memcpy(pStr, p, len);
- string.ReleaseBuffer(len);
- p += len;
-
- return true;
- }
-
- bool t_group::ForceSsl() const
- {
- switch (forceSsl)
- {
- default:
- case 0:
- return false;
- case 1:
- return true;
- case 2:
- if (!pOwner)
- return false;
-
- return pOwner->ForceSsl();
- }
- }
-