Pure-FTPd and the Upload-Script Feature

Standard

Today I was busy figuring out, what Pure-FTPd’s upload-script feature is and how it works. As I mainly run Debian machines, this little howto will deal with “The Debian Way”, as I use Debian’s initscripts to activate the feature. On other distros YMMV.

Motivation: The uploadscript feature triggers a script after every upload that is made and hands over the path to the uploaded file as argument 1 (“$1”) to the script. You can use the script to do something the with file immediately after the upload: You dont have to wait for a cronjob or write a directory watcher daemon. For example you could convert an uploaded picture to a differnt size and format and put it to a image galery…

Here is a little step-by-step guide:

Step 1: Run Pure-FTPd in standalone mode (not inetd mode). Change the option “STANDALONE_OR_INETD” to “standalone” in /etc/default/pure-ftpd.

Step 2: Enable the option itself:

echo "yes" > /etc/pure-ftpd/conf/CallUploadScript

我这边的实际操作是:

vi /etc/pure-ftpd/conf/pure-ftpd.conf
CallUploadScript yes

Step 3: Tell Pure-FTPd the user ID under which the script should be run. Pure doesn’t like usernames, so you have to provide the numeric UID. In case of www-data it is UID 33.

# if set, pure-uploadscript will spawn $UPLOADSCRIPT running as the # given uid and gid 
UPLOADUID=33 
UPLOADGID=33

Step 4: Tell pure-ftpd where the script is located: Change the option UPLOADSCRIPT in /etc/default/pure-ftpd.

# example: UPLOADSCRIPT=/usr/local/sbin/uploadhandler.pl 
UPLOADSCRIPT=/usr/local/sbin/uploadhandler.pl

Step 5: Start or Restart Pure-ftpd

# /etc/init.d/pure-ftpd restart 
Running: /usr/sbin/pure-ftpd -l pam -u 1000 -E -o -O clf:/var/log/pure-ftpd/transfer.log -B Restarting ftp upload handler: pure-uploadscript.

Step 6: Check if the daemon really run with the new options.

root 18623 0.0 0.0 12120 868 ? Ss 19:39 0:00 pure-ftpd (SERVER) 
www-data 18671 0.0 0.0 11912 672 ? Ss 19:40 0:00 /usr/sbin/pure-uploadscript -r /usr/local/sbin/uploadhandler.pl -B -u 33 -g 33

Step 7: Create a test script. E.g. this one will write the filename to a file called /tmp/pure-was-here.

#!/bin/sh 
echo "$1" > /tmp/pure-was-here$(date +%Y%m%d%H%M%S)

Step 8: Test the script, log in via ftp and upload a file:

ftp> put test.txt
local: test.txt 
remote: test.txt 
200 PORT command successful 
150 Connecting to port 41252 
226-File successfully transferred 
226 0.000 seconds (measured here), 50.72 Kbytes per second 
5 bytes sent in 0.00 secs (187.8 kB/s)

Then check if tmp/pure-was-here was written.

A side note: In case you want to trigger a PHP Script (many of my customers like PHP, I dunno why), you can set UPLOADSCRIPT=/usr/local/sbin/uploadhandler.php

That script can contain any php script. So if you dont know Perl or bash, an skeleton of an uploadsceript can look like that:

# cat /usr/local/sbin/uploadhandler.php 
#!/usr/bin/php5 
<?php 
echo ("foo"); 
?>

Dont forget to ‘chmod +x /usr/local/sbin/uploadhandler.php’. Test the script by executing it on the shell:

# /usr/local/sbin/uploadhandler.php
foo

转载 Author:Andreas John

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.