6.6. Executing Server Scripts
This chapter concludes with a discussion of a small but widely useful set of parameters. The preexec and postexec options allow you to define a command to be executed on the server whenever a user connects or disconnects from a share. The preexec and postexec commands are executed in the user's own context, and the root preexec and root postexec variants are executed under the context of the root account.
Scripts can be specified with arguments to pass, which are the standard Samba % variables. You are limited only by your own imagination. Some clients might connect and disconnect to shares more often that you think, so it is best not to assume that a user will connect to a share only once per session.
For an example, we'll create a share for the server's anonymous FTP directory. This step allows a user to publish files via FTP without having to resort to using an FTP client. The root preexec command creates an individual folder for the user to upload files. The share definition appears as:
[ftp] path = /var/ftp/pub read only = no force create mode = 0004 force directory mode = 0005 root preexec = /etc/samba/scripts/mkftpdir.sh %u
The contents of the mkftpdir.sh script are:
#!/bin/bash if [ "x$1" == "x" ]; then exit 1 fi if [ ! -d /var/ftp/pub/$1 ]; then mkdir /var/ftp/pub/$1 chmod 755 /var/ftp/pub/$1 chown $1 /var/ftp/pub/$1 fi
If desired, Samba can choose to honor or reject connections based on the return code from the preexec and root preexec commands. The preexec close (and root preexec close) can instruct smbd to evaluate the return code from the respective preexec command and reject the connection if the result is non-zero.
The pair of postexec commands are invoked when a user disconnects from a share. For example, when a user disconnects from a share containing removable media (such as a CD-ROM), the filesystem should be unmounted so that it can be removed. postexec commands are prime candidates for solving this category of problems.
Execution options are summarized in Table 6-13.