diff options
Diffstat (limited to 'src/dyndns-command.sh')
-rwxr-xr-x | src/dyndns-command.sh | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/dyndns-command.sh b/src/dyndns-command.sh new file mode 100755 index 0000000..375d50b --- /dev/null +++ b/src/dyndns-command.sh | |||
@@ -0,0 +1,92 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | die() { printf '%s\n' "$*" >&2; exit 1; } | ||
4 | |||
5 | sql_string() | ||
6 | { | ||
7 | printf '%s' "'${1//\'/\'\'}'" | ||
8 | } | ||
9 | |||
10 | powerdns_sqlite_add_replace_record() | ||
11 | { | ||
12 | local sql_new_domain="$(sql_string "$1.$3")" | ||
13 | local sql_ip_address="$(sql_string "$2")" | ||
14 | local sql_domain="$(sql_string "$3")" | ||
15 | local record_type="$(sql_string "$4")" | ||
16 | |||
17 | DBDIR=/etc/powerdns | ||
18 | DBNAME=powerdns.sqlite3 | ||
19 | DB=$DBDIR/$DBNAME | ||
20 | |||
21 | test -r $DB && test -w $DB || die "Wrong permissions on $DB" | ||
22 | test -r $DBDIR && test -w $DBDIR || die "Wrong permissions on $DBDIR" | ||
23 | |||
24 | sqlite3 $DB <<END | ||
25 | ${SQL_ECHO:+.echo on} | ||
26 | BEGIN; | ||
27 | DELETE FROM records WHERE type=$record_type AND name=$sql_new_domain; | ||
28 | |||
29 | INSERT INTO records | ||
30 | |||
31 | (domain_id, | ||
32 | name, | ||
33 | type, | ||
34 | content, | ||
35 | ttl, | ||
36 | prio) | ||
37 | |||
38 | SELECT | ||
39 | id, | ||
40 | $sql_new_domain, | ||
41 | $record_type, | ||
42 | $sql_ip_address, | ||
43 | 3600, | ||
44 | 0 | ||
45 | FROM domains | ||
46 | WHERE name=$sql_domain; | ||
47 | COMMIT; | ||
48 | END | ||
49 | } | ||
50 | |||
51 | add() | ||
52 | { | ||
53 | local record_type | ||
54 | case "$2" in | ||
55 | *.*.*.*) record_type=A ;; | ||
56 | *:*) record_type=AAAA ;; | ||
57 | *) exit 1 ;; | ||
58 | esac | ||
59 | |||
60 | powerdns_sqlite_add_replace_record "$1" "$2" "$domain" "$record_type" \ | ||
61 | && printf '%s %s\n' "$1.$domain $2" | ||
62 | } | ||
63 | |||
64 | main() | ||
65 | { | ||
66 | add "$subdomain" "$ip_address" | ||
67 | |||
68 | set -- $SSH_ORIGINAL_COMMAND | ||
69 | while [ $# -ge 2 ]; do | ||
70 | d=$1 | ||
71 | ip=$2 | ||
72 | shift 2 | ||
73 | |||
74 | case "$d" in | ||
75 | *.*) continue;; | ||
76 | esac | ||
77 | |||
78 | add "$d.$subdomain" "$ip" | ||
79 | done | ||
80 | } | ||
81 | |||
82 | PEM_DEST=$HOME/public_rsync | ||
83 | |||
84 | PATH=$HOME/bin:$PATH | ||
85 | |||
86 | eval "$(samizdat-ssh-uid --copy-pem "$PEM_DEST")" | ||
87 | |||
88 | domain=${SSH_CLIENT_DOMAIN} | ||
89 | subdomain=${SSH_CLIENT_FINGERPRINT} | ||
90 | ip_address=${SSH_CLIENT%% *} | ||
91 | |||
92 | main "$@" | ||