blob: 9712d095420e31dd7d7d29ad2012ed2e0c4c826e [file] [log] [blame]
Simon Kelley42b44a52013-09-27 14:38:45 +01001#!/bin/bash
2
3STATUS_FILE="/tmp/dnsmasq-ip-mac.status"
4
5# Script for dnsmasq lease-change hook.
Ville Skyttäfaaf3062018-01-14 17:32:52 +00006# Maintains the above file with an IP address/MAC address pairs,
Simon Kelley42b44a52013-09-27 14:38:45 +01007# one lease per line. Works with IPv4 and IPv6 leases, file is
8# atomically updated, so no races for users of the data.
9
10action="$1"
11mac="$2" # IPv4
12ip="$3"
13
Simon Kelley10bd2922013-09-27 21:07:30 +010014# ensure it always exists.
15
16if [ ! -f "$STATUS_FILE" ]; then
17 touch "$STATUS_FILE"
Simon Kelley42b44a52013-09-27 14:38:45 +010018fi
19
Simon Kelley10bd2922013-09-27 21:07:30 +010020if [ -n "$DNSMASQ_IAID" ]; then
21 mac="$DNSMASQ_MAC" # IPv6
22fi
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 Kelley42b44a52013-09-27 14:38:45 +010027if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then
Simon Kelley10bd2922013-09-27 21:07:30 +010028 if [ -n "$mac" -o "$action" = "del" ]; then
Simon Kelley42b44a52013-09-27 14:38:45 +010029 sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new
Simon Kelley10bd2922013-09-27 21:07:30 +010030
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 Kelley42b44a52013-09-27 14:38:45 +010035 fi
Simon Kelley42b44a52013-09-27 14:38:45 +010036fi