TinyXml是一个轻量级的xml的解析库,用它可以很方便的对XML文件进行操作。FileZilla是一款开源的FTP服务器,相对于windows下的FTP服务器,很功能齐全和使用便捷等优势。今天使用TinyXml对FileZilla的用户进行密码重设,FileZilla的主要配置信息保存在安装目录的FileZilla Server.xml文件中,该文件的users节点下保存有FTP用户与密码信息,密码是MD5加密。结构摘要如下:
<Users>
<User Name="abc">
<Option Name="Pass">40be4e59b9a2a2b5dffb918c0e86b3d7</Option>
我们只要替换"40be4e59b9a2a2b5dffb918c0e86b3d7"就可以修改FTP密码,密码重设后需要通过启动"FileZilla.exe /reload-config",修改才可生效,代码如下:
/*
@param szFileZillaPath:FileZilla可执行文件全路径
@param szFileName:FileZilla Server.xml全路径
*/
BOOL ResetFileZillaServerPwd(LPCSTR szFileZillaPath,LPCSTR szFileName,LPCSTR szUser,LPCSTR szOldPwd,LPCSTR szNewPwd)
{
if (GetFileAttributes(szFileName) == -1 || GetFileAttributes(szFileZillaPath) == -1) //文件不存在
return FALSE;
BOOL bRet = FALSE;
TiXmlDocument *myDocument = new TiXmlDocument();
try
{
if (!myDocument->LoadFile(szFileName))
{
if (myDocument != NULL)
delete myDocument;
return bRet;
}
TiXmlElement* RootElement = myDocument->RootElement();
if (RootElement == NULL)
{
bRet = FALSE;
goto lbb;
}
TiXmlElement *UserElement,*TmpElement,*SubTmpElement; //直接定位到users
TmpElement = RootElement->FirstChildElement();
TmpElement = TmpElement->NextSiblingElement();
UserElement = TmpElement = TmpElement->NextSiblingElement();
if(TmpElement != NULL)
{
SubTmpElement = TmpElement->FirstChildElement();
while (SubTmpElement != NULL)
{
bool bOk = FALSE;
TiXmlAttribute *NameAttribute,*TmpAttribute;
TmpAttribute = SubTmpElement->FirstAttribute();
while(TmpAttribute != NULL)
{
if (stricmp(TmpAttribute->Name(),"name") == 0 && stricmp(TmpAttribute->Value(),szUser) == 0)
{
cout<<TmpAttribute->Value()<<endl;
bOk = TRUE;
break;
}
TmpAttribute = TmpAttribute->Next();
}
if (bOk)
{
TmpElement = SubTmpElement->FirstChildElement();
if (TmpElement != NULL)
{
TiXmlAttribute *PassAttribute,*TmpAttribute;
TmpAttribute = TmpElement->FirstAttribute();
while (TmpAttribute != NULL)
{
if (stricmp(TmpAttribute->Name(),"name")==0 && stricmp(TmpAttribute->Value(),"pass")==0)
{
cout<<TmpElement->FirstChild()->Value()<<endl;
if (stricmp(TmpElement->FirstChild()->Value(),szOldPwd) == 0)
{
TmpElement->FirstChild()->SetValue(szNewPwd);
bRet = true;
}
break;
}
TmpAttribute = TmpAttribute->Next();
}
}
break;
}
SubTmpElement = SubTmpElement->NextSiblingElement();
}
if (bRet)
{
myDocument->SaveFile(szFileName);
delete myDocument;
CString strCmdLine;
strCmdLine.Format("%s /reload-config",szFileZillaPath); //重载生效 md5
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
memset(&pi,0,sizeof(pi));
if (CreateProcess(NULL,(LPSTR)(LPCSTR)strCmdLine,NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi) != 0)
{
if (pi.hProcess != NULL)
CloseHandle(pi.hProcess);
if (pi.hThread != NULL)
CloseHandle(pi.hThread);
}
}
else
{
delete myDocument;
}
return bRet;
}
lbb:
if (myDocument != NULL)
delete myDocument;
return bRet;
}
catch(...)
{
if (myDocument != NULL)
delete myDocument;
return FALSE;
}
}