summaryrefslogtreecommitdiff
path: root/src/dyndns-command.sh
blob: 375d50bdfa0cc0cc99f030fd40cca4e7c57ed7c0 (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
83
84
85
86
87
88
89
90
91
92
#!/bin/bash

die() { printf '%s\n' "$*" >&2; exit 1; }

sql_string()
{
	printf '%s' "'${1//\'/\'\'}'"
}

powerdns_sqlite_add_replace_record()
{
	local sql_new_domain="$(sql_string "$1.$3")"
	local sql_ip_address="$(sql_string "$2")"
	local sql_domain="$(sql_string "$3")"
	local record_type="$(sql_string "$4")"

	DBDIR=/etc/powerdns
	DBNAME=powerdns.sqlite3
	DB=$DBDIR/$DBNAME

	test -r $DB    && test -w $DB    || die "Wrong permissions on $DB"
	test -r $DBDIR && test -w $DBDIR || die "Wrong permissions on $DBDIR"

	sqlite3 $DB <<END
${SQL_ECHO:+.echo on}
BEGIN;
	DELETE FROM records WHERE type=$record_type AND name=$sql_new_domain;

	INSERT INTO records

		(domain_id,
		name,
		type,
		content,
		ttl,
		prio)

		SELECT
			id,
			$sql_new_domain,
			$record_type,
			$sql_ip_address,
			3600,
			0
		FROM domains
		WHERE name=$sql_domain;
COMMIT;
END
}

add()
{
	local record_type
	case "$2" in
		*.*.*.*) record_type=A ;;
		*:*) record_type=AAAA ;;
		*) exit 1 ;;
	esac

	powerdns_sqlite_add_replace_record "$1" "$2" "$domain" "$record_type" \
	&& printf '%s %s\n' "$1.$domain $2"
}

main()
{
	add "$subdomain" "$ip_address"

	set -- $SSH_ORIGINAL_COMMAND
	while [ $# -ge 2 ]; do
		d=$1
		ip=$2
		shift 2

		case "$d" in
			*.*) continue;;
		esac

		add "$d.$subdomain" "$ip"
	done
}

PEM_DEST=$HOME/public_rsync

PATH=$HOME/bin:$PATH

eval "$(samizdat-ssh-uid --copy-pem "$PEM_DEST")"

domain=${SSH_CLIENT_DOMAIN}
subdomain=${SSH_CLIENT_FINGERPRINT}
ip_address=${SSH_CLIENT%% *}

main "$@"