[workitem:225] Could not create FTP account in a vanilla fresh installation of FileZilla

This commit is contained in:
ptsurbeleu 2012-04-26 00:17:15 -07:00
parent d1b07ef269
commit 0ef2f7b808

View file

@ -40,361 +40,366 @@ using WebsitePanel.Server.Utils;
namespace WebsitePanel.Providers.FTP namespace WebsitePanel.Providers.FTP
{ {
public class FileZilla : HostingServiceProviderBase, IFtpServer public class FileZilla : HostingServiceProviderBase, IFtpServer
{ {
#region Constants #region Constants
private const string FILEZILLA_REG = @"SOFTWARE\FileZilla Server"; private const string FILEZILLA_REG = @"SOFTWARE\FileZilla Server";
private const string FILEZILLA_REG_X64 = @"SOFTWARE\Wow6432Node\FileZilla Server"; private const string FILEZILLA_REG_X64 = @"SOFTWARE\Wow6432Node\FileZilla Server";
private const string FILEZILLA_SERVER_FILE = "FileZilla Server.xml"; private const string FILEZILLA_SERVER_FILE = "FileZilla Server.xml";
#endregion #endregion
#region Properties #region Properties
protected virtual string FileZillaFolder protected virtual string FileZillaFolder
{ {
get get
{ {
RegistryKey fzKey = Registry.LocalMachine.OpenSubKey(FILEZILLA_REG) ?? RegistryKey fzKey = Registry.LocalMachine.OpenSubKey(FILEZILLA_REG) ??
Registry.LocalMachine.OpenSubKey(FILEZILLA_REG_X64); Registry.LocalMachine.OpenSubKey(FILEZILLA_REG_X64);
if (fzKey == null) if (fzKey == null)
throw new Exception("FileZilla registry key was not found: " + FILEZILLA_REG); throw new Exception("FileZilla registry key was not found: " + FILEZILLA_REG);
return (string)fzKey.GetValue("Install_Dir"); return (string)fzKey.GetValue("Install_Dir");
} }
} }
#endregion #endregion
#region Sites #region Sites
public virtual void ChangeSiteState(string siteId, ServerState state) public virtual void ChangeSiteState(string siteId, ServerState state)
{ {
// not implemented // not implemented
} }
public virtual string CreateSite(FtpSite site) public virtual string CreateSite(FtpSite site)
{ {
// not implemented // not implemented
return null; return null;
} }
public virtual void DeleteSite(string siteId) public virtual void DeleteSite(string siteId)
{ {
// not implemented // not implemented
} }
public virtual FtpSite GetSite(string siteId) public virtual FtpSite GetSite(string siteId)
{ {
// not implemented // not implemented
return null; return null;
} }
public virtual FtpSite[] GetSites() public virtual FtpSite[] GetSites()
{ {
// not implemented // not implemented
return null; return null;
} }
public virtual bool SiteExists(string siteId) public virtual bool SiteExists(string siteId)
{ {
// not implemented // not implemented
return false; return false;
} }
public virtual ServerState GetSiteState(string siteId) public virtual ServerState GetSiteState(string siteId)
{ {
// not implemented // not implemented
return ServerState.Started; return ServerState.Started;
} }
public virtual void UpdateSite(FtpSite site) public virtual void UpdateSite(FtpSite site)
{ {
// not implemented // not implemented
} }
#endregion #endregion
#region Accounts #region Accounts
public virtual bool AccountExists(string accountName) public virtual bool AccountExists(string accountName)
{ {
XmlDocument doc = GetFileZillaConfig(); XmlDocument doc = GetFileZillaConfig();
XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + accountName + "']"); XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + accountName + "']");
return (nodeUser != null); return (nodeUser != null);
} }
public virtual FtpAccount GetAccount(string accountName) public virtual FtpAccount GetAccount(string accountName)
{ {
XmlDocument doc = GetFileZillaConfig(); XmlDocument doc = GetFileZillaConfig();
XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + accountName + "']"); XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + accountName + "']");
if (nodeUser == null) if (nodeUser == null)
return null; return null;
return CreateAccountFromXmlNode(nodeUser, false); return CreateAccountFromXmlNode(nodeUser, false);
} }
public virtual FtpAccount[] GetAccounts() public virtual FtpAccount[] GetAccounts()
{ {
XmlDocument doc = GetFileZillaConfig(); XmlDocument doc = GetFileZillaConfig();
XmlNodeList nodeUsers = doc.SelectNodes("/FileZillaServer/Users/User"); XmlNodeList nodeUsers = doc.SelectNodes("/FileZillaServer/Users/User");
List<FtpAccount> accounts = new List<FtpAccount>(); List<FtpAccount> accounts = new List<FtpAccount>();
foreach (XmlNode nodeUser in nodeUsers) foreach (XmlNode nodeUser in nodeUsers)
accounts.Add(CreateAccountFromXmlNode(nodeUser, true)); accounts.Add(CreateAccountFromXmlNode(nodeUser, true));
return accounts.ToArray(); return accounts.ToArray();
} }
public virtual void CreateAccount(FtpAccount account) public virtual void CreateAccount(FtpAccount account)
{ {
Log.WriteInfo("GetFileZillaConfig"); Log.WriteInfo("GetFileZillaConfig");
XmlDocument doc = GetFileZillaConfig(); XmlDocument doc = GetFileZillaConfig();
Log.WriteInfo("Find users nodes"); Log.WriteInfo("Find users nodes");
// find users node // find users node
XmlNode nodeUsers = doc.SelectSingleNode("/FileZillaServer/Users"); XmlNode fzServerNode = doc.SelectSingleNode("/FileZillaServer");
XmlNode fzAccountsNode = fzServerNode.SelectSingleNode("/Users");
XmlElement nodeUser = doc.CreateElement("User"); if (fzAccountsNode == null)
if (nodeUsers != null) nodeUsers.AppendChild(nodeUser); {
fzAccountsNode = doc.CreateElement("Users");
fzServerNode.AppendChild(fzAccountsNode);
}
// set properties XmlElement fzAccountNode = doc.CreateElement("User");
nodeUser.SetAttribute("Name", account.Name); fzAccountsNode.AppendChild(fzAccountNode);
SetOption(nodeUser, "Pass", MD5(account.Password)); // set properties
SetOption(nodeUser, "Group", ""); fzAccountNode.SetAttribute("Name", account.Name);
SetOption(nodeUser, "Bypass server userlimit", "0"); SetOption(fzAccountNode, "Pass", MD5(account.Password));
SetOption(nodeUser, "User Limit", "0"); SetOption(fzAccountNode, "Group", "");
SetOption(nodeUser, "IP Limit", "0"); SetOption(fzAccountNode, "Bypass server userlimit", "0");
SetOption(nodeUser, "Enabled", BoolToString(account.Enabled)); SetOption(fzAccountNode, "User Limit", "0");
SetOption(nodeUser, "Comments", ""); SetOption(fzAccountNode, "IP Limit", "0");
SetOption(nodeUser, "ForceSsl", "0"); SetOption(fzAccountNode, "Enabled", BoolToString(account.Enabled));
SetOption(fzAccountNode, "Comments", "");
SetOption(fzAccountNode, "ForceSsl", "0");
// IP filter // IP filter
XmlElement nodeIPFilter = doc.CreateElement("IpFilter"); XmlElement nodeIPFilter = doc.CreateElement("IpFilter");
nodeUser.AppendChild(nodeIPFilter); fzAccountNode.AppendChild(nodeIPFilter);
XmlElement nodeDisallowed = doc.CreateElement("Disallowed"); XmlElement nodeDisallowed = doc.CreateElement("Disallowed");
nodeIPFilter.AppendChild(nodeDisallowed); nodeIPFilter.AppendChild(nodeDisallowed);
XmlElement nodeAllowed = doc.CreateElement("Allowed"); XmlElement nodeAllowed = doc.CreateElement("Allowed");
nodeIPFilter.AppendChild(nodeAllowed); nodeIPFilter.AppendChild(nodeAllowed);
// folder // folder
XmlElement nodePermissions = doc.CreateElement("Permissions"); XmlElement nodePermissions = doc.CreateElement("Permissions");
nodeUser.AppendChild(nodePermissions); fzAccountNode.AppendChild(nodePermissions);
XmlElement nodePermission = doc.CreateElement("Permission"); XmlElement nodePermission = doc.CreateElement("Permission");
nodePermissions.AppendChild(nodePermission); nodePermissions.AppendChild(nodePermission);
// folder settings // folder settings
nodePermission.SetAttribute("Dir", account.Folder); nodePermission.SetAttribute("Dir", account.Folder);
SetOption(nodePermission, "FileRead", BoolToString(account.CanRead)); SetOption(nodePermission, "FileRead", BoolToString(account.CanRead));
SetOption(nodePermission, "FileWrite", BoolToString(account.CanWrite)); SetOption(nodePermission, "FileWrite", BoolToString(account.CanWrite));
SetOption(nodePermission, "FileDelete", BoolToString(account.CanWrite)); SetOption(nodePermission, "FileDelete", BoolToString(account.CanWrite));
SetOption(nodePermission, "DirCreate", BoolToString(account.CanWrite)); SetOption(nodePermission, "DirCreate", BoolToString(account.CanWrite));
SetOption(nodePermission, "DirDelete", BoolToString(account.CanWrite)); SetOption(nodePermission, "DirDelete", BoolToString(account.CanWrite));
SetOption(nodePermission, "DirList", BoolToString(account.CanRead)); SetOption(nodePermission, "DirList", BoolToString(account.CanRead));
SetOption(nodePermission, "DirSubdirs", BoolToString(account.CanRead)); SetOption(nodePermission, "DirSubdirs", BoolToString(account.CanRead));
SetOption(nodePermission, "IsHome", "1"); SetOption(nodePermission, "IsHome", "1");
SetOption(nodePermission, "AutoCreate", "0"); SetOption(nodePermission, "AutoCreate", "0");
// speed limits // speed limits
XmlElement nodeSpeedLimits = doc.CreateElement("SpeedLimits"); XmlElement nodeSpeedLimits = doc.CreateElement("SpeedLimits");
nodeUser.AppendChild(nodeSpeedLimits); fzAccountNode.AppendChild(nodeSpeedLimits);
nodeSpeedLimits.SetAttribute("DlType", "0"); nodeSpeedLimits.SetAttribute("DlType", "0");
nodeSpeedLimits.SetAttribute("DlLimit", "10"); nodeSpeedLimits.SetAttribute("DlLimit", "10");
nodeSpeedLimits.SetAttribute("ServerDlLimitBypass", "0"); nodeSpeedLimits.SetAttribute("ServerDlLimitBypass", "0");
nodeSpeedLimits.SetAttribute("UlType", "0"); nodeSpeedLimits.SetAttribute("UlType", "0");
nodeSpeedLimits.SetAttribute("UlLimit", "10"); nodeSpeedLimits.SetAttribute("UlLimit", "10");
nodeSpeedLimits.SetAttribute("ServerUlLimitBypass", "0"); nodeSpeedLimits.SetAttribute("ServerUlLimitBypass", "0");
XmlElement nodeDownload = doc.CreateElement("Download"); XmlElement nodeDownload = doc.CreateElement("Download");
nodeSpeedLimits.AppendChild(nodeDownload); nodeSpeedLimits.AppendChild(nodeDownload);
XmlElement nodeUpload = doc.CreateElement("Upload"); XmlElement nodeUpload = doc.CreateElement("Upload");
nodeSpeedLimits.AppendChild(nodeUpload); nodeSpeedLimits.AppendChild(nodeUpload);
// save document // save document
doc.Save(GetFileZillaConfigPath()); doc.Save(GetFileZillaConfigPath());
// reload config // reload config
ReloadFileZillaConfig(); ReloadFileZillaConfig();
} }
public virtual void UpdateAccount(FtpAccount account) public virtual void UpdateAccount(FtpAccount account)
{ {
XmlDocument doc = GetFileZillaConfig(); XmlDocument doc = GetFileZillaConfig();
XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + account.Name + "']"); XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + account.Name + "']");
if (nodeUser == null) if (nodeUser == null)
return; return;
// update user // update user
if(!String.IsNullOrEmpty(account.Password)) if(!String.IsNullOrEmpty(account.Password))
SetOption(nodeUser, "Pass", MD5(account.Password)); SetOption(nodeUser, "Pass", MD5(account.Password));
SetOption(nodeUser, "Enabled", BoolToString(account.Enabled)); SetOption(nodeUser, "Enabled", BoolToString(account.Enabled));
// update folder // update folder
XmlNode nodePermission = nodeUser.SelectSingleNode("Permissions/Permission"); XmlNode nodePermission = nodeUser.SelectSingleNode("Permissions/Permission");
if (nodePermission != null) if (nodePermission != null)
{ {
((XmlElement)nodePermission).SetAttribute("Dir", account.Folder); ((XmlElement)nodePermission).SetAttribute("Dir", account.Folder);
SetOption(nodePermission, "FileRead", BoolToString(account.CanRead)); SetOption(nodePermission, "FileRead", BoolToString(account.CanRead));
SetOption(nodePermission, "FileWrite", BoolToString(account.CanWrite)); SetOption(nodePermission, "FileWrite", BoolToString(account.CanWrite));
SetOption(nodePermission, "FileDelete", BoolToString(account.CanWrite)); SetOption(nodePermission, "FileDelete", BoolToString(account.CanWrite));
SetOption(nodePermission, "DirCreate", BoolToString(account.CanWrite)); SetOption(nodePermission, "DirCreate", BoolToString(account.CanWrite));
SetOption(nodePermission, "DirDelete", BoolToString(account.CanWrite)); SetOption(nodePermission, "DirDelete", BoolToString(account.CanWrite));
SetOption(nodePermission, "DirList", BoolToString(account.CanRead)); SetOption(nodePermission, "DirList", BoolToString(account.CanRead));
SetOption(nodePermission, "DirSubdirs", BoolToString(account.CanRead)); SetOption(nodePermission, "DirSubdirs", BoolToString(account.CanRead));
} }
// save document // save document
doc.Save(GetFileZillaConfigPath()); doc.Save(GetFileZillaConfigPath());
// reload config // reload config
ReloadFileZillaConfig(); ReloadFileZillaConfig();
} }
public virtual void DeleteAccount(string accountName) public virtual void DeleteAccount(string accountName)
{ {
XmlDocument doc = GetFileZillaConfig(); XmlDocument doc = GetFileZillaConfig();
XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + accountName + "']"); XmlNode nodeUser = doc.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + accountName + "']");
if (nodeUser == null) if (nodeUser == null)
return; return;
// delete account // delete account
nodeUser.ParentNode.RemoveChild(nodeUser); nodeUser.ParentNode.RemoveChild(nodeUser);
// save document // save document
doc.Save(GetFileZillaConfigPath()); doc.Save(GetFileZillaConfigPath());
// reload config // reload config
ReloadFileZillaConfig(); ReloadFileZillaConfig();
} }
#endregion #endregion
public override void ChangeServiceItemsState(ServiceProviderItem[] items, bool enabled) public override void ChangeServiceItemsState(ServiceProviderItem[] items, bool enabled)
{ {
foreach (ServiceProviderItem item in items) foreach (ServiceProviderItem item in items)
{ {
if (item is FtpAccount) if (item is FtpAccount)
{ {
try try
{ {
// change FTP account state // change FTP account state
FtpAccount account = GetAccount(item.Name); FtpAccount account = GetAccount(item.Name);
account.Enabled = enabled; account.Enabled = enabled;
UpdateAccount(account); UpdateAccount(account);
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.WriteError(String.Format("Error switching '{0}' {1}", item.Name, item.GetType().Name), ex); Log.WriteError(String.Format("Error switching '{0}' {1}", item.Name, item.GetType().Name), ex);
} }
} }
} }
} }
public override void DeleteServiceItems(ServiceProviderItem[] items) public override void DeleteServiceItems(ServiceProviderItem[] items)
{ {
foreach (ServiceProviderItem item in items) foreach (ServiceProviderItem item in items)
{ {
if (item is FtpAccount) if (item is FtpAccount)
{ {
try try
{ {
// delete FTP account // delete FTP account
DeleteAccount(item.Name); DeleteAccount(item.Name);
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.WriteError(String.Format("Error deleting '{0}' {1}", item.Name, item.GetType().Name), ex); Log.WriteError(String.Format("Error deleting '{0}' {1}", item.Name, item.GetType().Name), ex);
} }
} }
} }
} }
#region Private Helpers #region Private Helpers
private string BoolToString(bool val) private string BoolToString(bool val)
{ {
return val ? "1" : "0"; return val ? "1" : "0";
} }
private void SetOption(XmlNode parentNode, string name, string val) private void SetOption(XmlNode parentNode, string name, string val)
{ {
XmlNode option = parentNode.SelectSingleNode("Option[@Name='" + name + "']"); XmlNode option = parentNode.SelectSingleNode("Option[@Name='" + name + "']");
if (option == null) if (option == null)
{ {
option = parentNode.OwnerDocument.CreateElement("Option"); option = parentNode.OwnerDocument.CreateElement("Option");
parentNode.AppendChild(option); parentNode.AppendChild(option);
((XmlElement)option).SetAttribute("Name", name); ((XmlElement)option).SetAttribute("Name", name);
} }
option.InnerText = val; option.InnerText = val;
} }
private FtpAccount CreateAccountFromXmlNode(XmlNode nodeUser, bool excludeDetails) private FtpAccount CreateAccountFromXmlNode(XmlNode nodeUser, bool excludeDetails)
{ {
FtpAccount account = new FtpAccount(); FtpAccount account = new FtpAccount();
account.Name = nodeUser.Attributes["Name"].Value; account.Name = nodeUser.Attributes["Name"].Value;
if (!excludeDetails) if (!excludeDetails)
{ {
account.Password = nodeUser.SelectSingleNode("Option[@Name='Pass']").InnerText; account.Password = nodeUser.SelectSingleNode("Option[@Name='Pass']").InnerText;
account.Enabled = (nodeUser.SelectSingleNode("Option[@Name='Enabled']").InnerText == "1"); account.Enabled = (nodeUser.SelectSingleNode("Option[@Name='Enabled']").InnerText == "1");
XmlNode nodeFolder = nodeUser.SelectSingleNode("Permissions/Permission"); XmlNode nodeFolder = nodeUser.SelectSingleNode("Permissions/Permission");
if (nodeFolder != null) if (nodeFolder != null)
{ {
account.Folder = nodeFolder.Attributes["Dir"].Value; account.Folder = nodeFolder.Attributes["Dir"].Value;
account.CanRead = (nodeFolder.SelectSingleNode("Option[@Name='FileRead']").InnerText == "1"); account.CanRead = (nodeFolder.SelectSingleNode("Option[@Name='FileRead']").InnerText == "1");
account.CanWrite = (nodeFolder.SelectSingleNode("Option[@Name='FileWrite']").InnerText == "1"); account.CanWrite = (nodeFolder.SelectSingleNode("Option[@Name='FileWrite']").InnerText == "1");
} }
} }
return account; return account;
} }
private XmlDocument GetFileZillaConfig() private XmlDocument GetFileZillaConfig()
{ {
string path = GetFileZillaConfigPath(); string path = GetFileZillaConfigPath();
if (!File.Exists(path)) if (!File.Exists(path))
throw new Exception("FileZilla configuration file was not found: " + path); throw new Exception("FileZilla configuration file was not found: " + path);
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
doc.Load(path); doc.Load(path);
return doc; return doc;
} }
private string GetFileZillaConfigPath() private string GetFileZillaConfigPath()
{ {
return Path.Combine(FileZillaFolder, FILEZILLA_SERVER_FILE); return Path.Combine(FileZillaFolder, FILEZILLA_SERVER_FILE);
} }
private string MD5(string str) private string MD5(string str)
{ {
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding(); System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(str); byte[] bytes = ue.GetBytes(str);
// encrypt bytes // encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes); byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16) // Convert the encrypted bytes back to a string (base 16)
string hashString = ""; string hashString = "";
for (int i = 0; i < hashBytes.Length; i++) for (int i = 0; i < hashBytes.Length; i++)
hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0'); hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
return hashString.PadLeft(32, '0'); return hashString.PadLeft(32, '0');
} }
private void ReloadFileZillaConfig() private void ReloadFileZillaConfig()
{ {
FileUtils.ExecuteSystemCommand( FileUtils.ExecuteSystemCommand(
Path.Combine(FileZillaFolder, "FileZilla Server.exe"), Path.Combine(FileZillaFolder, "FileZilla Server.exe"),
"/reload-config"); "/reload-config");
} }
#endregion #endregion
public override bool IsInstalled() public override bool IsInstalled()
{ {
@ -423,5 +428,5 @@ namespace WebsitePanel.Providers.FTP
} }
} }
} }
} }