home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / Accounts.cpp next >
Encoding:
C/C++ Source or Header  |  2005-08-29  |  12.7 KB  |  574 lines

  1. #include "stdafx.h"
  2. #include "accounts.h"
  3. #include "iputils.h"
  4.  
  5. t_directory::t_directory()
  6. {
  7.     bAutoCreate = FALSE;
  8. }
  9.  
  10. t_group::t_group()
  11. {
  12.     pOwner = NULL;
  13.  
  14.     for (int i = 0; i < 2; i++)
  15.     {
  16.         nSpeedLimitType[i] = 0;
  17.         nSpeedLimit[i] = 10;
  18.         nBypassServerSpeedLimit[i] = 0;
  19.     }
  20.     nEnabled = 1;
  21.     forceSsl = 0;
  22. }
  23.  
  24. t_group& t_group::operator=(const t_group &a)
  25. {
  26.     group = a.group;
  27.     nBypassUserLimit = a.nBypassUserLimit;
  28.     nUserLimit = a.nUserLimit;
  29.     nIpLimit = a.nIpLimit;
  30.     permissions = a.permissions;
  31.     nEnabled = a.nEnabled;
  32.     disallowedIPs = a.disallowedIPs;
  33.     allowedIPs = a.allowedIPs;
  34.     comment = a.comment;
  35.     forceSsl = a.forceSsl;
  36.  
  37.     for (int i = 0; i < 2; i++)
  38.     {
  39.         nBypassServerSpeedLimit[i] = a.nBypassServerSpeedLimit[i];
  40.         nSpeedLimit[i] = a.nSpeedLimit[i];
  41.         nSpeedLimitType[i] = a.nSpeedLimitType[i];
  42.         SpeedLimits[i] = a.SpeedLimits[i];
  43.     }
  44.  
  45.     return *this;
  46. }
  47.  
  48. bool t_group::BypassUserLimit() const
  49. {
  50.     if (!nBypassUserLimit)
  51.         return false;
  52.     if (nBypassUserLimit == 2 && pOwner)
  53.         return pOwner->BypassUserLimit();
  54.     return true;
  55. }
  56.  
  57. int t_group::GetIpLimit() const
  58. {
  59.     if (nIpLimit)
  60.         return nIpLimit;
  61.     if (pOwner)
  62.         return pOwner->GetIpLimit();
  63.     return 0;
  64. }
  65.  
  66. int t_group::GetUserLimit() const
  67. {
  68.     if (nUserLimit)
  69.         return nUserLimit;
  70.     if (pOwner)
  71.         return pOwner->GetUserLimit();
  72.     return 0;
  73. }
  74.  
  75. t_user::t_user()
  76. {
  77. }
  78.  
  79. t_user& t_user::operator=(const t_user &a)
  80. {
  81.     group = a.group;
  82.     pOwner = a.pOwner;
  83.     user=a.user;
  84.     password=a.password;
  85.     nBypassUserLimit = a.nBypassUserLimit;
  86.     nUserLimit = a.nUserLimit;
  87.     nIpLimit = a.nIpLimit;
  88.     permissions = a.permissions;
  89.     nEnabled = a.nEnabled;
  90.     disallowedIPs = a.disallowedIPs;
  91.     allowedIPs = a.allowedIPs;
  92.     comment = a.comment;
  93.     forceSsl = a.forceSsl;
  94.  
  95.     for (int i = 0; i < 2; i++)
  96.     {
  97.         nBypassServerSpeedLimit[i] = a.nBypassServerSpeedLimit[i];
  98.         nSpeedLimit[i] = a.nSpeedLimit[i];
  99.         nSpeedLimitType[i] = a.nSpeedLimitType[i];
  100.         SpeedLimits[i] = a.SpeedLimits[i];
  101.     }
  102.  
  103.     return *this;
  104. }
  105.  
  106. unsigned char * t_group::ParseBuffer(unsigned char *pBuffer, int length)
  107. {
  108.     unsigned char *p = pBuffer;
  109.     unsigned char *endMarker = pBuffer + length;
  110.  
  111.     if (!ParseString(endMarker, p, group))
  112.         return 0;
  113.  
  114.     if ((endMarker - p) < 11)
  115.         return NULL;
  116.  
  117.     memcpy(&nIpLimit, p, 4);
  118.     p += 4;
  119.     memcpy(&nUserLimit, p, 4);
  120.     p += 4;
  121.  
  122.     int options = *p++;
  123.  
  124.     nBypassUserLimit    = options & 0x03;
  125.     nEnabled            = (options >> 2) & 0x03;
  126.  
  127.     // Parse IP filter rules.
  128.     int numDisallowedIPs = (int(*p) << 8) + p[1];
  129.     p += 2;
  130.     while (numDisallowedIPs--)
  131.     {
  132.         CString ip;
  133.         if (!ParseString(endMarker, p, ip))
  134.             return 0;
  135.  
  136.         if (IsValidAddressFilter(ip) || ip == "*")
  137.             disallowedIPs.push_back(ip);
  138.     }
  139.  
  140.     if ((endMarker - p) < 2)
  141.         return NULL;
  142.  
  143.     int numAllowedIPs = (int(*p) << 8) + p[1];
  144.     p += 2;
  145.     while (numAllowedIPs--)
  146.     {
  147.         CString ip;
  148.         if (!ParseString(endMarker, p, ip))
  149.             return 0;
  150.  
  151.         if (IsValidAddressFilter(ip) || ip == "*")
  152.             allowedIPs.push_back(ip);
  153.     }
  154.  
  155.     if ((endMarker - p) < 2)
  156.         return NULL;
  157.  
  158.     int dircount = (int(*p) << 8) + p[1];
  159.     p += 2;
  160.  
  161.     BOOL bGotHome = FALSE;
  162.  
  163.     for (int j = 0; j < dircount; j++)
  164.     {
  165.         t_directory dir;
  166.  
  167.         if (!ParseString(endMarker, p, dir.dir))
  168.             return 0;
  169.  
  170.         dir.dir.TrimRight("\\");
  171.         
  172.         if (dir.dir == "")
  173.             return 0;
  174.  
  175.         // Get directory aliases.
  176.         if ((endMarker - p) < 2)
  177.             return NULL;
  178.  
  179.         int aliascount = (int(*p) << 8) + p[1];
  180.         p += 2;
  181.  
  182.         for (int i = 0; i < aliascount; i++)
  183.         {
  184.             CString alias;
  185.             if (!ParseString(endMarker, p, alias))
  186.                 return 0;
  187.  
  188.             alias.TrimRight("\\");
  189.  
  190.             if (alias == "")
  191.                 return 0;
  192.  
  193.             dir.aliases.push_back(alias);
  194.         }
  195.  
  196.         if ((endMarker - p) < 2)
  197.             return NULL;
  198.  
  199.         int rights = (int(*p) << 8) + p[1];
  200.         p += 2;
  201.  
  202.         dir.bDirCreate    = rights & 0x0001 ? 1:0;
  203.         dir.bDirDelete    = rights & 0x0002 ? 1:0;
  204.         dir.bDirList    = rights & 0x0004 ? 1:0;
  205.         dir.bDirSubdirs    = rights & 0x0008 ? 1:0;
  206.         dir.bFileAppend    = rights & 0x0010 ? 1:0;
  207.         dir.bFileDelete    = rights & 0x0020 ? 1:0;
  208.         dir.bFileRead    = rights & 0x0040 ? 1:0;
  209.         dir.bFileWrite    = rights & 0x0080 ? 1:0;
  210.         dir.bIsHome        = rights & 0x0100 ? 1:0;
  211.         dir.bAutoCreate    = rights & 0x0200 ? 1:0;
  212.  
  213.         // Avoid multiple home directories.
  214.         if (dir.bIsHome)
  215.             if (!bGotHome)
  216.                 bGotHome = TRUE;
  217.             else
  218.                 dir.bIsHome = FALSE;
  219.  
  220.         permissions.push_back(dir);
  221.     }
  222.  
  223.     for (int i = 0; i < 2; i++)
  224.     {
  225.         if ((endMarker - p) < 5)
  226.             return NULL;
  227.  
  228.         nSpeedLimitType[i] = *p & 3;
  229.         nBypassServerSpeedLimit[i] = (*p++ >> 2) & 3;
  230.  
  231.         nSpeedLimit[i] = int(*p++) << 8;
  232.         nSpeedLimit[i] |= *p++;
  233.     
  234.         if (!nSpeedLimit[i])
  235.             nSpeedLimit[i] = 10;
  236.  
  237.         int num = (int(*p) << 8) + p[1];
  238.         p += 2;
  239.         while (num--)
  240.         {
  241.             CSpeedLimit sl;
  242.             p = sl.ParseBuffer(p, length-(int)(p-pBuffer));
  243.             if (!p)
  244.                 return NULL;
  245.             SpeedLimits[i].push_back(sl);
  246.         }
  247.     }
  248.  
  249.     if (!ParseString(endMarker, p, comment))
  250.         return 0;
  251.  
  252.     if (p >= endMarker)
  253.         return 0;
  254.  
  255.     forceSsl = *p++;
  256.  
  257.     return p;
  258. }
  259.  
  260. char * t_group::FillBuffer(char *p) const
  261. {
  262.     *p++ = group.GetLength() >> 8;
  263.     *p++ = group.GetLength() & 0xff;
  264.     memcpy(p, group, group.GetLength());
  265.     p += group.GetLength();
  266.  
  267.     memcpy(p, &nIpLimit, 4);
  268.     p += 4;
  269.     memcpy(p, &nUserLimit, 4);
  270.     p += 4;
  271.  
  272.     int options = nBypassUserLimit & 3;
  273.     options |= (nEnabled & 3) << 2;
  274.  
  275.     *p++ = options & 0xff;
  276.  
  277.     std::list<CString>::const_iterator ipLimitIter;
  278.  
  279.     *p++ = (char)(disallowedIPs.size() >> 8);
  280.     *p++ = (char)(disallowedIPs.size() & 0xff);
  281.     for (ipLimitIter = disallowedIPs.begin(); ipLimitIter != disallowedIPs.end(); ipLimitIter++)
  282.     {
  283.         *p++ = (char)(ipLimitIter->GetLength() >> 8);
  284.         *p++ = (char)(ipLimitIter->GetLength() & 0xff);
  285.         memcpy(p, *ipLimitIter, ipLimitIter->GetLength());
  286.         p += ipLimitIter->GetLength();
  287.     }
  288.  
  289.     *p++ = (char)(allowedIPs.size() >> 8);
  290.     *p++ = (char)(allowedIPs.size() & 0xff);
  291.     for (ipLimitIter = allowedIPs.begin(); ipLimitIter != allowedIPs.end(); ipLimitIter++)
  292.     {
  293.         *p++ = (char)(ipLimitIter->GetLength() >> 8);
  294.         *p++ = (char)(ipLimitIter->GetLength() & 0xff);
  295.         memcpy(p, *ipLimitIter, ipLimitIter->GetLength());
  296.         p += ipLimitIter->GetLength();
  297.     }
  298.  
  299.     *p++ = (char)(permissions.size() >> 8);
  300.     *p++ = (char)(permissions.size() & 0xff);
  301.     for (std::vector<t_directory>::const_iterator permissioniter = permissions.begin(); permissioniter!=permissions.end(); permissioniter++)
  302.     {
  303.         *p++ = (char)(permissioniter->dir.GetLength() >> 8);
  304.         *p++ = (char)(permissioniter->dir.GetLength() & 0xff);
  305.         memcpy(p, permissioniter->dir, permissioniter->dir.GetLength());
  306.         p += permissioniter->dir.GetLength();
  307.  
  308.         *p++ = (char)(permissioniter->aliases.size() >> 8);
  309.         *p++ = (char)(permissioniter->aliases.size() & 0xff);
  310.         for (std::list<CString>::const_iterator aliasiter = permissioniter->aliases.begin(); aliasiter != permissioniter->aliases.end(); aliasiter++)
  311.         {
  312.             *p++ = (char)(aliasiter->GetLength() >> 8);
  313.             *p++ = (char)(aliasiter->GetLength() & 0xff);
  314.             memcpy(p, *aliasiter, aliasiter->GetLength());
  315.             p += aliasiter->GetLength();
  316.         }
  317.  
  318.         int rights = 0;
  319.         rights |= permissioniter->bDirCreate    ? 0x0001:0;
  320.         rights |= permissioniter->bDirDelete    ? 0x0002:0;
  321.         rights |= permissioniter->bDirList        ? 0x0004:0;
  322.         rights |= permissioniter->bDirSubdirs    ? 0x0008:0;
  323.         rights |= permissioniter->bFileAppend    ? 0x0010:0;
  324.         rights |= permissioniter->bFileDelete    ? 0x0020:0;
  325.         rights |= permissioniter->bFileRead        ? 0x0040:0;
  326.         rights |= permissioniter->bFileWrite    ? 0x0080:0;
  327.         rights |= permissioniter->bIsHome        ? 0x0100:0;
  328.         rights |= permissioniter->bAutoCreate    ? 0x0200:0;
  329.         *p++ = (char)(rights >> 8);
  330.         *p++ = (char)(rights & 0xff);
  331.     }
  332.  
  333.     for (int i = 0; i < 2; i++)
  334.     {
  335.         *p++ = (char)((nSpeedLimitType[i] & 3) + ((nBypassServerSpeedLimit[i] & 3) << 2));
  336.         *p++ = (char)(nSpeedLimit[i] >> 8);
  337.         *p++ = (char)(nSpeedLimit[i] & 0xff);
  338.  
  339.         SPEEDLIMITSLIST::const_iterator iter;
  340.  
  341.         *p++ = (char)(SpeedLimits[i].size() >> 8);
  342.         *p++ = (char)(SpeedLimits[i].size() & 0xff);
  343.         for (iter = SpeedLimits[i].begin(); (iter != SpeedLimits[i].end()) && p; iter++)
  344.             p = iter->FillBuffer(p);
  345.         if (!p)
  346.             return NULL;
  347.     }
  348.  
  349.     *p++ = (char)(comment.GetLength() >> 8);
  350.     *p++ = (char)(comment.GetLength() & 0xff);
  351.     memcpy(p, comment, comment.GetLength());
  352.     p += comment.GetLength();
  353.  
  354.     *p++ = (char)forceSsl;
  355.  
  356.     return p;
  357. }
  358.  
  359. int t_group::GetRequiredBufferLen() const
  360. {
  361.     int len = 9;
  362.     len += group.GetLength() + 2;
  363.  
  364.     len += 4;
  365.     std::list<CString>::const_iterator ipLimitIter;
  366.     for (ipLimitIter = disallowedIPs.begin(); ipLimitIter != disallowedIPs.end(); ipLimitIter++)
  367.         len += ipLimitIter->GetLength() + 2;
  368.     for (ipLimitIter = allowedIPs.begin(); ipLimitIter != allowedIPs.end(); ipLimitIter++)
  369.         len += ipLimitIter->GetLength() + 2;
  370.  
  371.     len += 2;
  372.     for (std::vector<t_directory>::const_iterator permissioniter = permissions.begin(); permissioniter!=permissions.end(); permissioniter++)
  373.     {
  374.         t_directory directory = *permissioniter;
  375.         len += 2;
  376.         len += directory.dir.GetLength() + 2;
  377.  
  378.         len += 2;
  379.         for (std::list<CString>::const_iterator aliasiter = permissioniter->aliases.begin(); aliasiter != permissioniter->aliases.end(); aliasiter++)
  380.             len += aliasiter->GetLength() + 2;
  381.     }
  382.  
  383.     // Speed limits.
  384.     len += 6; // Basic limits.
  385.     len += 4; // Number of rules.
  386.     for (int i = 0; i < 2; i++)
  387.     {
  388.         SPEEDLIMITSLIST::const_iterator iter;
  389.         for (iter = SpeedLimits[i].begin(); iter != SpeedLimits[i].end(); iter++)
  390.             len += iter->GetRequiredBufferLen();
  391.     }
  392.  
  393.     len += 2 + comment.GetLength();
  394.  
  395.     len++; //forceSsl
  396.  
  397.     return len;
  398. }
  399.  
  400. int t_group::GetCurrentSpeedLimit(sltype type) const
  401. {
  402.     switch (nSpeedLimitType[type])
  403.     {
  404.     case 0:
  405.         if (pOwner)
  406.             return pOwner->GetCurrentSpeedLimit(type);
  407.         else
  408.             return 0;
  409.     case 1:
  410.         return 0;
  411.     case 2:
  412.         return nSpeedLimit[type];
  413.     case 3:
  414.         {
  415.             SYSTEMTIME st;
  416.             GetSystemTime(&st);
  417.             for (SPEEDLIMITSLIST::const_iterator iter = SpeedLimits[type].begin(); iter != SpeedLimits[type].end(); iter++)
  418.                 if (iter->IsItActive(st))
  419.                     return iter->m_Speed;
  420.         }
  421.         if (pOwner)
  422.             return pOwner->GetCurrentSpeedLimit(type);
  423.         else
  424.             return 0;
  425.     }
  426.     return 0;
  427. }
  428.  
  429. bool t_group::BypassServerSpeedLimit(sltype type) const
  430. {
  431.     if (nBypassServerSpeedLimit[type] == 1)
  432.         return true;
  433.     else if (!nBypassServerSpeedLimit[type])
  434.         return false;
  435.     else if (pOwner)
  436.         return pOwner->BypassServerSpeedLimit(type);
  437.     else
  438.         return false;
  439. }
  440.  
  441. bool t_group::IsEnabled() const
  442. {
  443.     switch (nEnabled)
  444.     {
  445.     default:
  446.     case 0:
  447.         return false;
  448.     case 1:
  449.         return true;
  450.     case 2:
  451.         if (!pOwner)
  452.             return false;
  453.  
  454.         return pOwner->IsEnabled();
  455.     }
  456. }
  457.  
  458. bool t_group::AccessAllowed(SOCKADDR_IN sockAddr) const
  459. {
  460.     unsigned int ip = htonl(sockAddr.sin_addr.s_addr);
  461.     const char *pIp = inet_ntoa(sockAddr.sin_addr);
  462.  
  463.     bool disallowed = false;
  464.  
  465.     std::list<CString>::const_iterator iter;
  466.     for (iter = disallowedIPs.begin(); iter != disallowedIPs.end(); iter++)
  467.     {
  468.         if (disallowed = MatchesFilter(*iter, ip, pIp))
  469.             break;
  470.     }
  471.  
  472.     if (!disallowed)
  473.     {
  474.         if (!pOwner)
  475.             return true;
  476.  
  477.         if (pOwner->AccessAllowed(sockAddr))
  478.             return true;
  479.     }
  480.  
  481.     for (iter = allowedIPs.begin(); iter != allowedIPs.end(); iter++)
  482.     {
  483.         if (MatchesFilter(*iter, ip, pIp))
  484.             return true;
  485.     }
  486.  
  487.     if (pOwner && !disallowed)
  488.         return pOwner->AccessAllowed(sockAddr);
  489.  
  490.     return false;
  491. }
  492.  
  493. unsigned char * t_user::ParseBuffer(unsigned char *pBuffer, int length)
  494. {
  495.     unsigned char *p = pBuffer;
  496.     unsigned char *endMarker = pBuffer + length;
  497.  
  498.     p = t_group::ParseBuffer(p, length);
  499.     if (!p)
  500.         return NULL;
  501.  
  502.     if (!ParseString(endMarker, p, user))
  503.         return 0;
  504.  
  505.     if (!ParseString(endMarker, p, password))
  506.         return 0;
  507.  
  508.     return p;
  509. }
  510.  
  511. char * t_user::FillBuffer(char *p) const
  512. {
  513.     p = t_group::FillBuffer(p);
  514.     if (!p)
  515.         return NULL;
  516.  
  517.     *p++ = user.GetLength() >> 8;
  518.     *p++ = user.GetLength() & 0xff;
  519.     memcpy(p, user, user.GetLength());
  520.     p += user.GetLength();
  521.  
  522.     *p++ = password.GetLength() >> 8;
  523.     *p++ = password.GetLength() & 0xff;
  524.     memcpy(p, password, password.GetLength());
  525.     p += password.GetLength();
  526.  
  527.     return p;
  528. }
  529.  
  530. int t_user::GetRequiredBufferLen() const
  531. {
  532.     int len = t_group::GetRequiredBufferLen();
  533.     len += user.GetLength() + 2;
  534.     len += password.GetLength() + 2;
  535.     return len;
  536. }
  537.  
  538. bool t_group::ParseString(const unsigned char* endMarker, unsigned char *&p, CString &string)
  539. {
  540.     if ((endMarker - p) < 2)
  541.         return false;
  542.  
  543.     int len = *p * 256 + p[1];
  544.     p += 2;
  545.  
  546.     if ((endMarker - p) < len)
  547.         return false;
  548.     char *pStr = string.GetBuffer(len);
  549.     if (!pStr)
  550.         return false;
  551.     memcpy(pStr, p, len);
  552.     string.ReleaseBuffer(len);
  553.     p += len;
  554.  
  555.     return true;
  556. }
  557.  
  558. bool t_group::ForceSsl() const
  559. {
  560.     switch (forceSsl)
  561.     {
  562.     default:
  563.     case 0:
  564.         return false;
  565.     case 1:
  566.         return true;
  567.     case 2:
  568.         if (!pOwner)
  569.             return false;
  570.  
  571.         return pOwner->ForceSsl();
  572.     }
  573. }
  574.