In the first part of the howto I just explained how to get a proftpd deamon to run with ssl encryption. Doing this brings up a new problem for dyndns users with 24h reconnects and dynamic wan IPs. Proftpd masquarades the IP-address when it is started and not everytime a connection is made (like some other deamons) so after getting a new IP, via for example dyndns service, the deamon is still running with the old address and no encrypted connection is possible to (home)servers which get a new ip adress after reconnecting to the internet.
As there exists no implemented solution for this problem we need to find a solution by ourselves ;) Thinking about it I had two ideas.
1. restart the proftpd deamon with a cronjob at a specific time (normally a few minutes after the 24h reconnect) which should be made at the same time every day. This solution is quite poor in my opinion because just restarting the router or disconnecting the connection to the internet would lead to an unuseable ftp server.
2. using a script which checks whether the IP has changed till the last check. Putting such a script in a cron with the instruction to restart the proftpd deamon if the IP has changed should just work. The great advantage would be that it automatically restarts the deamon when the IP changes and its completely independent of any user interaction or time (like a 24h reconnect of course).
Thinking about realising it lead to a problem: I know perfectly well how to set a cron but I didn’t know how to write a skript (which would be the more ergonomic solution) but had the idea and could also think of something like sending a single ping to the hostname of the server to find out its IP-adress and checking if it changed till the last ping. After that a simple if/else should be enough to restart the server or leaving it as it is if the IP hasn’t changed.
Luckily I told SIYB about my idea and as it seemed to be quite easy to him he just coded it and it seems to work :
here is the code (change the filenames/locations as you wish or however the default locations of proftpd and your cron deamon are, the ones here are the defaults used by gentoo):
first create a new file with (all actions explained here are done with root privileges)
# nano -w /usr/local/sbin/proftpd_masquarade
and then copy the code into it (change the hostname to yours):
#!/usr/bin/tclsh # this script will restart the proftp server if the ip of the host changes ### config ### # your dyndns hostname set config(host) "yourhostname.dyndns.org" # the temporary file to store the old ip set config(file) "/var/run/proftpdip" ### code ### # getting ip from dyndns host set newip [string trim [lindex [split [exec ping -c 1 $config(host)]] 2] ()] # getting old ip or enter ip if there is no record if {![file exists $config(file)]} { set fl [open $config(file) a+]; puts $fl $newip; close $fl set data $newip } else { # get data from file set fl [open $config(file) r]; set data [gets $fl]; close $fl # erase file content and enter new ip set fl [open $config(file) w]; puts $fl $newip; close $fl } # check newip vs oldip if {$newip == $data} { puts "super" } else { puts "restart ftpd" exec /etc/init.d/proftpd restart >> /dev/null }
Save it. As it is written in TCL you need to have TCL installed to run it with:
# tclsh /usr/local/sbin/proftpd_masquarade
Now you just have to create a cronjob to run the script every minute so just put the following in your crontab by running:
# echo “*/1 * * * * root tclsh /usr/local/sbin/proftpd_masquarade” >> /etc/crontab
Finally your proftpd deamon should work fine behind NAT with encryption and the 24h reconnect. Not so bad, heh!?
This was just planned as a short tip so its not really part 2 but 1.5. More is on its way, especially things like vhosts and performance tuning. There is also a POSIX script now, written by zhenech, to avoid the TCL dependency. It will be blogged soon.
big thx to siyb for writing the script, to zhenech and craven for correction and to teranetworks for wasting my spare-time






