From 0bc729e2de97755d1ad6707ed550239aa9653153 Mon Sep 17 00:00:00 2001 From: florian Date: Mon, 1 Mar 2021 15:56:31 +0000 Subject: [PATCH] Log adding and deleting of IP addresses as well as nameservers. deraadt@ pointed out that dhcpleased is too quiet. --- sbin/dhcpleased/engine.c | 76 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/sbin/dhcpleased/engine.c b/sbin/dhcpleased/engine.c index e2dbd31c191..ce13e1c0cb4 100644 --- a/sbin/dhcpleased/engine.c +++ b/sbin/dhcpleased/engine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: engine.c,v 1.5 2021/03/01 15:56:00 florian Exp $ */ +/* $OpenBSD: engine.c,v 1.6 2021/03/01 15:56:31 florian Exp $ */ /* * Copyright (c) 2017, 2021 Florian Obser @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -126,6 +127,8 @@ void state_transition(struct dhcpleased_iface *, enum void iface_timeout(int, short, void *); void request_dhcp_discover(struct dhcpleased_iface *); void request_dhcp_request(struct dhcpleased_iface *); +void log_lease(struct dhcpleased_iface *, int); +void log_rdns(struct dhcpleased_iface *, int); void send_configure_interface(struct dhcpleased_iface *); void send_rdns_proposal(struct dhcpleased_iface *); void send_deconfigure_interface(struct dhcpleased_iface *); @@ -1225,11 +1228,37 @@ request_dhcp_request(struct dhcpleased_iface *iface) sizeof(imsg_req_request)); } +void +log_lease(struct dhcpleased_iface *iface, int deconfigure) +{ + char hbuf_lease[INET_ADDRSTRLEN], hbuf_server[INET_ADDRSTRLEN]; + char if_name[IF_NAMESIZE]; + + memset(if_name, 0, sizeof(if_name)); + + if (if_indextoname(iface->if_index, if_name) == 0) + if_name[0] = '?'; + inet_ntop(AF_INET, &iface->requested_ip, hbuf_lease, + sizeof(hbuf_lease)); + inet_ntop(AF_INET, &iface->server_identifier, hbuf_server, + sizeof(hbuf_server)); + + + if (deconfigure) + log_info("deleting %s from %s (lease from %s)", hbuf_lease, + if_name, hbuf_server); + else + log_info("adding %s to %s (lease from %s)", hbuf_lease, + if_name, hbuf_server); +} + void send_configure_interface(struct dhcpleased_iface *iface) { struct imsg_configure_interface imsg; + log_lease(iface, 0); + imsg.if_index = iface->if_index; imsg.rdomain = iface->rdomain; imsg.addr.s_addr = iface->requested_ip.s_addr; @@ -1244,6 +1273,8 @@ send_deconfigure_interface(struct dhcpleased_iface *iface) { struct imsg_configure_interface imsg; + log_lease(iface, 1); + imsg.if_index = iface->if_index; imsg.rdomain = iface->rdomain; imsg.addr.s_addr = iface->requested_ip.s_addr; @@ -1259,11 +1290,52 @@ send_deconfigure_interface(struct dhcpleased_iface *iface) iface->router.s_addr = INADDR_ANY; } +void +log_rdns(struct dhcpleased_iface *iface, int withdraw) +{ + int i; + char hbuf_rdns[INET_ADDRSTRLEN], hbuf_server[INET_ADDRSTRLEN]; + char if_name[IF_NAMESIZE], *rdns_buf = NULL, *tmp_buf; + + memset(if_name, 0, sizeof(if_name)); + + if (if_indextoname(iface->if_index, if_name) == 0) + if_name[0] = '?'; + + inet_ntop(AF_INET, &iface->server_identifier, hbuf_server, + sizeof(hbuf_server)); + + for (i = 0; i < MAX_RDNS_COUNT && iface->nameservers[i].s_addr != + INADDR_ANY; i++) { + inet_ntop(AF_INET, &iface->nameservers[i], hbuf_rdns, + sizeof(hbuf_rdns)); + tmp_buf = rdns_buf; + if (asprintf(&rdns_buf, "%s %s", tmp_buf ? tmp_buf : "", + hbuf_rdns) < 0) { + rdns_buf = NULL; + break; + } + free(tmp_buf); + } + + if (rdns_buf != NULL) { + if (withdraw) + log_info("deleting nameservers%s (lease from %s on %s)", + rdns_buf, hbuf_server, if_name); + else + log_info("adding nameservers%s (lease from %s on %s)", + rdns_buf, hbuf_server, if_name); + free(rdns_buf); + } +} + void send_rdns_proposal(struct dhcpleased_iface *iface) { struct imsg_propose_rdns imsg; + log_rdns(iface, 0); + memset(&imsg, 0, sizeof(imsg)); imsg.if_index = iface->if_index; @@ -1281,6 +1353,8 @@ send_rdns_withdraw(struct dhcpleased_iface *iface) { struct imsg_propose_rdns imsg; + log_rdns(iface, 1); + memset(&imsg, 0, sizeof(imsg)); imsg.if_index = iface->if_index; -- 2.20.1