Miscellaneous Hooks

all_(pre|post).sh

Scripts that are called after all POST requests.

Heavy form checking is expected in your script as users can pass anything they'd like to try and fool your scripts with unexpected environmental variable values.

Be very very careful with this feature. It's very "raw" and can bite you if you're not careful.

Environment variables

Environment variables are set up according to this order:

  • $username: DA user
  • $command: for example, CMD_COMMAND_NAME
  • $caller_ip: from what IP the script was called
  • $SESSION_ID: session ID for current session during command call
  • Request body: body of request
  • URL query variables: any query variables included in the command request

Variables set higher in the list will override variables set lower in the list.


public_html_link_set_(pre|post).sh

Hook scripts, triggered when the link /home/user/public_html is created or set (happens fairly often).

Note that when this process is skipped using public_html_link_set_pre.sh you'd be on your own for handling any issues that may arise from not having the link set (untested, unknown).

Environment variables

  • username: owner of domain
  • domain: value link is being set to.
  • main_domain: main domain which is set in the user.conf
  • old_public_html_link (1|0): value from the directadmin.conf, will usually be set to 1, where the link would be http://ip/~username. The 0 method is a directory, e.g., http://ip/~username/domain.com.
  • apache_public_html (1|0): Usually will be 0. Old value of 1 was from when the public_html was chgrp to apache.

rotate_log_post.sh

Script is called after the logs are rotated into the User logs path, but before the web logs are actually deleted/truncated.

As this script runs as root, it's not recommended to work on any data in the User's home path, or anywhere that a User has write access.

If you're using this to save a copy, we recommend copying it direct from the /var/log/httpd/domains/ path to another non-User-writable path.

Use any su trickery to run as the User at your own risk 😃

Environment variables

  • USER: owner of the domain
  • dest: path to the folder in the user logs, where rotated logs are stored
  • log_dir: path to the domain log directory
  • compress_rotated_logs (0/1): whether DirectAdmin is set to compress rotated logs
  • tarfile: path to created log tarfile
  • files: space separated list of paths to domain weblogs, used during tar call
  • file: path to the active domain web access log
  • access_log: path to the rotated domain access log in /var/log/httpd/domains
  • error_log: path to rotated domain error log /var/log/httpd/domains

sendmail_pre.sh

All emails generated by DirectAdmin (e.g., Message System notices, etc.,) are pushed through /usr/sbin/sendmail.

This script will allow you to override the sendmail call and handle the message yourself.
For example, if you want to use a remote SMTP server directly for all DA messages, you could set up some sort of wrapper in the sendmail_pre.sh to deliver them to some port 25.

Environment variables

  • Always passed variables
    • fullmessage: all headers and message contents
    • message: just the body of the message
    • to: the "To:" header. Could be normal email, or a utf-8 encoded email if utf8_encode_from_to=1 is set in directadmin.conf.
    • subject: the "Subject:" header. Could be utf-8 encoded if utf8_encode_from_to=1 is set in the directadmin.conf.
  • Optional variables (possibly not passed)
    • from: the "From:" header. Could be normal email, or utf-8 encoded email if utf8_encode_from_to=1 is set in directadmin.conf.
    • reply_to: the "From:" header...could be normal email, or utf-8 encoded email if utf8_encode_from_to=1 is set in directadmin.conf.
    • headers: url-encoded string of possible extra headers, e.g., MIME-Version=1.0&Content-Type=text/html (for the <html> as start of body case), for example.

update_post.sh

This hooks runs after a DirectAdmin update has finished.

Environment variables

No variables are passed.


Examples

chmod files uploaded through the file manager

The following example utilizes the all_post.sh hook script to chmod all files uploaded through the File Manager:

#!/bin/sh

CHMODVAL=700
ULPATH=/home/${username}${path}

setfile() {
  if [ "$1" = "" ]; then
    return;
  fi

  F=`echo $1 | cut -d/ -f4 | awk '{ print substr($1,1,length($1)-6) }'`

  chmod ${CHMODVAL} ${ULPATH}${F}
}

TMP=/tmp/txt.txt
if [ "$command" = "/CMD_FILE_MANAGER/" ] || [ "$command" = "/CMD_FILE_MANAGER" ]; then
  if [ "$action" = "upload" ]; then

    setfile $file1
    setfile $file2
    setfile $file3
    setfile $file4
    setfile $file5
    setfile $file6
    setfile $file7
    setfile $file8
  fi
fi

exit 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
I wish to keep domain logs in a separate location for long-term safe-keeping

Full article here

After several iterations of log ownership in ~/domains/domain.com/logs, the current state (as of August 2019) stores the logs as the User.

Users do have sufficient privileges to accidentally delete their logs.

Managing root-owned data under a User's home directory is very difficult to do securely, so keeping your long-term root logs in a non-User-writable area is recommended.

Create the file /usr/local/directadmin/scripts/custom/rotate_log_post.sh with 700 permissions, diradmin ownership, and the following code:

#!/bin/sh
DAYS_TO_KEEP=365

DOMAIN=`basename $file`
LONG_TERM=/var/log/httpd/long_term
DOMAIN_DIR=${LONG_TERM}/$DOMAIN
DATE=`date +%F`
DEST=${DOMAIN_DIR}/${DATE}
if [ ! -d ${DEST} ]; then
       mkdir -p ${DEST}
fi
/bin/cp -n ${access_log} ${DEST}
if [ "${error_log}" != "" ]; then
       /bin/cp -n ${error_log} ${DEST}
fi
find ${LONG_TERM}/* -mtime +${DAYS_TO_KEEP} -type d -exec rm -rf "{}" \;
exit 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
How to prevent users from deleting certain files using the File Manager

Since Users often delete things they shouldn't, it may be wise to prevent them from deleting things they shouldn't.

In this example, we'll block them from deleting their /public_html symbolic link using the File Manager.

First, edit the following script which will be doing the work for us:

/usr/local/directadmin/scripts/custom/all_pre.sh
1

Inside this new script, add the code:

#!/bin/sh
if [ "${button}" = "delete" ]; then
    if env|grep -m1 -q '=/public_html#39;; then
        echo "You cannot delete your public_html link!"
        exit 1
    fi
fi
exit 0
1
2
3
4
5
6
7
8

Then, fix the ownership and permissions:

chmod 700 /usr/local/directadmin/scripts/custom/all_pre.sh
chown diradmin. /usr/local/directadmin/scripts/custom/all_pre.sh
1
2
Last Updated: 6/23/2021, 9:36:08 PM