Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | STATUS_FILE="/tmp/dnsmasq-ip-mac.status" |
| 4 | |
| 5 | # Script for dnsmasq lease-change hook. |
Ville Skyttä | faaf306 | 2018-01-14 17:32:52 +0000 | [diff] [blame^] | 6 | # Maintains the above file with an IP address/MAC address pairs, |
Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 7 | # one lease per line. Works with IPv4 and IPv6 leases, file is |
| 8 | # atomically updated, so no races for users of the data. |
| 9 | |
| 10 | action="$1" |
| 11 | mac="$2" # IPv4 |
| 12 | ip="$3" |
| 13 | |
Simon Kelley | 10bd292 | 2013-09-27 21:07:30 +0100 | [diff] [blame] | 14 | # ensure it always exists. |
| 15 | |
| 16 | if [ ! -f "$STATUS_FILE" ]; then |
| 17 | touch "$STATUS_FILE" |
Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 18 | fi |
| 19 | |
Simon Kelley | 10bd292 | 2013-09-27 21:07:30 +0100 | [diff] [blame] | 20 | if [ -n "$DNSMASQ_IAID" ]; then |
| 21 | mac="$DNSMASQ_MAC" # IPv6 |
| 22 | fi |
| 23 | |
| 24 | # worry about an add or old action when the MAC address is not known: |
| 25 | # leave any old one in place in that case. |
| 26 | |
Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 27 | if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then |
Simon Kelley | 10bd292 | 2013-09-27 21:07:30 +0100 | [diff] [blame] | 28 | if [ -n "$mac" -o "$action" = "del" ]; then |
Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 29 | sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new |
Simon Kelley | 10bd292 | 2013-09-27 21:07:30 +0100 | [diff] [blame] | 30 | |
| 31 | if [ "$action" = "add" -o "$action" = "old" ]; then |
| 32 | echo "$ip $mac" >> "$STATUS_FILE".new |
| 33 | fi |
| 34 | mv "$STATUS_FILE".new "$STATUS_FILE" # atomic update. |
Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 35 | fi |
Simon Kelley | 42b44a5 | 2013-09-27 14:38:45 +0100 | [diff] [blame] | 36 | fi |