summaryrefslogtreecommitdiff
path: root/src/dnsmasq-dhcp-script.sh
blob: 167d22976e19a417a7ee4fd75c7507fac69bdd10 (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
#!/bin/sh
. samizdat-paths.sh
TFTP_ROOT=${samizdat_isolinux_dir}
[ "$1 $4" = "tftp ${TFTP_ROOT}/linux/vmlinuz" ] || exit
# $2 is the length of the file
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"
}

debug
(new_child || cleanup_after_fail) &