由于日常工作的需要,单位使用Serv-U架设了一个FTP服务器,可是自从接手之后发现存在着一个非常严重的问题,这个FTP服务器是对外公开的,居然很多用户都没有设置密码。如果强制要求所有人设置密码又必须在服务器上设,这样岂不是要所有人都把自己的密码告诉管理员吗,毕竟很多人习惯于用同一个密码的。怎么办呢?最好的办法当然是能够提供一个Web页面来提供密码的修改功能。
      说干就干,在网上查了一下,有一种方法是使用Serv-U自身提供的ODBC功能,用数据库来存储密码,通过直接对数据库进行操作来实现密码的修改功能,但经过考试这种方法并不太可行。因为这个FTP服务器已经运行了一年之久,里面已有将近六十个用户,要将这些用户从Ini文件移植到数据库出现错误的几率还是比较高的,还不如直接对INI文件进行操作来得干脆。
       首先是要搞清楚Serv-U的用户信息在INI文件中是如何保存的,密码又是如何加密的。INI文件的结构比较简单,修改密码的话只要找到以[User=@UserID|1]节,并修改其下的Password键的值即可。@UserID指的是用户的登录ID。
 1为Serv-U提供在线修改密码功能[GLOBAL]
 2为Serv-U提供在线修改密码功能Version=6.1.0.5
 3为Serv-U提供在线修改密码功能PacketTimeOut=300
 4为Serv-U提供在线修改密码功能
 5为Serv-U提供在线修改密码功能为Serv-U提供在线修改密码功能
 6为Serv-U提供在线修改密码功能
 7为Serv-U提供在线修改密码功能[Domain1]
 8为Serv-U提供在线修改密码功能User1=
 9为Serv-U提供在线修改密码功能User2=
10为Serv-U提供在线修改密码功能User3=
11为Serv-U提供在线修改密码功能
12为Serv-U提供在线修改密码功能为Serv-U提供在线修改密码功能
13为Serv-U提供在线修改密码功能
14为Serv-U提供在线修改密码功能[USER=abc|1]
15为Serv-U提供在线修改密码功能Password=niE383DC3710266ECAE04A6B3A18A2966D
16为Serv-U提供在线修改密码功能HomeDir=D:\
17为Serv-U提供在线修改密码功能AlwaysAllowLogin=1
18为Serv-U提供在线修改密码功能ChangePassword=1
19为Serv-U提供在线修改密码功能TimeOut=600
20为Serv-U提供在线修改密码功能Note1="Wizard generated account"
21为Serv-U提供在线修改密码功能Access1=D:\
22为Serv-U提供在线修改密码功能
23为Serv-U提供在线修改密码功能为Serv-U提供在线修改密码功能

       用户密码的加密方法可以在Ser-U官方网站的知识库查到
       http://rhinosoft.com/KBArticle.asp?RefNo=1177&prod=su
为Serv-U提供在线修改密码功能Manually Entering Encrypted Passwords into the ServUDaemon.ini File 
为Serv-U提供在线修改密码功能To generate an encrypted password, first two random characters (the 'salt' - in the range a..z, A..Z) are added to the beginning of the clear-text password. This is then hashed using MD5 and the resulting hash is hex-encoded. The result of this is written as plain-text starting with the 2 salt characters followed by the hex-encoded hash.
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能For a user account in the .ini file, this will look like:
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能Password=cb644FB1F31184F8D3D169B54B3D46AB1A
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能The salt is the string "cb", the MD5 hash is "644FB1F31184F8D3D169B54B3D46AB1A".
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能When verifying a user's password, Serv-U will do the same. It parses the salt from the user's stored password (ie. "cb" in this case), prepends it the password the user sent to it by the client, MD5 hashes it, and compares the result with the stored hash. If the values are equal, then the entered password is correct.
为Serv-U提供在线修改密码功能 
为Serv-U提供在线修改密码功能
加密的方法也就是随机生成两个字母,然后将字母和密码进行拼接,再求它们的MD5值,最后将随机字母放在MD5值的前面便是加密后的密码。
       接下来就可以根据以上的分析编写程序来实现在线修改了。
  1        }
  
        以上代码中的_strServUDaemonPath变量用于保存ServUDaemon.ini文件所在的路径,该值可以在PageLoad事件中通过Web.Config设置取得。
       但事情并没有就此结束。经过测试,发现在存在一个严重的问题:修改密码后只有重启Serv-U才能使修改后的密码生效。那不是等于没什么用嘛,管理员总不可能没事老在那里重启服务器来使密码修改生效吧。
        再次回来Serv-U的官方知识库,查到了如下一条内容:
为Serv-U提供在线修改密码功能Manually Updating the ServUDaemon.ini File 
为Serv-U提供在线修改密码功能Whenever changes are made directly to the ServUDaemon.ini file, add the following line under the Global area in the INI file.
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能ReloadSettings=True
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能Serv-U regularly checks the INI file for this setting. If it is present, Serv-U will refresh all stored settings for every domain on the server. This allows Serv-U to recognize the changes without having to be restarted.
为Serv-U提供在线修改密码功能
为Serv-U提供在线修改密码功能After Serv-U loads the changes, it removes the "ReloadSettings=True" entry. This allows you to enter it again next time any changes are made.
也就是说,只要在INI文件的GLOBAL节添加键ReloadSettings并设置它的值为True便可以实现修改密码后自动更新了。于是只要修改原代码,在101行和102行之间插入如下一句代码即可:
为Serv-U提供在线修改密码功能ini.WriteString( "GLOBAL""ReloadSettings""True" );
        到这里,一个在线修改Serv-U密码的网页就全部完成了。
        程序中的IniFile是一个封装了API对INI文件操作的类,仅需要实现对字符串的读取和写入即可。

相关文章: