summaryrefslogtreecommitdiff
path: root/src/dnsmasq-dhcp-script.sh
blob: 7469447859e82807a677da50a163e4f0e14cf0de (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
[ "$1" = tftp ] || exit 0
. samizdat-paths.sh
TFTP_ROOT=${samizdat_isolinux_dir}
TFTP_FILE=$4
TFTP_FILE_SIZE=$3
CLIENT_IP=$3

# dnsmasq clears the environment.  kiki needs at least $HOME
export USER=root
export MAIL=/var/mail/root
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export LANG=en_US.UTF-8
export HOME=/root
export LANGUAGE=en_US:en
export LOGNAME=root

debug()
{
    LOGDIR=/var/log/dnsmasq-dhcp-script
    mkdir -p "$LOGDIR"
    exec > "$LOGDIR/$$.log" 2>&1
    set -x
}

new_child()
{
    child_dir=$samizdat_child_dir/child.$$
    if [ -d "$child_dir" ]; then
        rmdir "$child_dir" || exit 1
    fi

    sh -x "$(which keygen.sh)" "$child_dir" || return
    tar --exclude '*~' -zcf "$child_dir"/gnupghome.tar -C "$child_dir"/root/.gnupg . || return

    tftp_dir="${TFTP_ROOT}"/"$CLIENT_IP"
    if [ ! -d "$tftp_dir" ]; then
        ATOMIC=y
        dest_dir="$tftp_dir"~

        # DNSMASQ DOCUMENTATION IS WRONG OMFG
        # Otherwise this symlink farm would be unnecessary

        mkdir -p "$dest_dir"
        (cd "$dest_dir" && ln -sf ../* . && rm "$CLIENT_IP"~) || return
    else
        dest_dir="$tftp_dir"
    fi

    chown dnsmasq "$child_dir" "$child_dir"/gnupghome.tar
    ln -sf "$child_dir"/gnupghome.tar "$dest_dir"/

    if [ "$ATOMIC" ]; then
        mv -T "$dest_dir" "$tftp_dir"
    fi
}

cleanup_after_fail()
{
    umount "$child_dir"
    rmdir "$child_dir"
}

exit_unless_needed()
{

    case "$TFTP_FILE" in
        "${TFTP_ROOT}/linux/vmlinuz")
        ;;
        "${TFTP_ROOT}/${CLIENT_IP}/linux/vmlinuz")
            [ -e "${TFTP_ROOT}/${CLIENT_IP}/gnupghome.tar" ] && exit
        ;;
        *) exit
        ;;
    esac
}

exit_unless_needed

debug

(new_child || cleanup_after_fail) &