Convert to using shared ncmlib.
This commit is contained in:
@ -3,16 +3,14 @@ project (ndhc)
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
|
||||
set(NDHC_SRCS
|
||||
nstrl.c
|
||||
log.c
|
||||
options.c
|
||||
socket.c
|
||||
packet.c
|
||||
rootcap.c
|
||||
script.c
|
||||
clientpacket.c
|
||||
rootcap.c
|
||||
dhcpc.c
|
||||
)
|
||||
|
||||
add_executable(ndhc ${NDHC_SRCS})
|
||||
#target_link_libraries(ndhc ncmlib)
|
||||
target_link_libraries(ndhc ncmlib)
|
||||
|
@ -50,8 +50,8 @@ unsigned long random_xid(void)
|
||||
|
||||
fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd == -1 || read(fd, &seed, sizeof(seed)) < 0) {
|
||||
log_line(LOG_WARNING, "Could not load seed from /dev/urandom: %s\n",
|
||||
strerror(errno));
|
||||
log_warning("Could not load seed from /dev/urandom: %s",
|
||||
strerror(errno));
|
||||
seed = time(0);
|
||||
}
|
||||
if (fd != -1)
|
||||
@ -111,7 +111,7 @@ int send_discover(unsigned long xid, unsigned long requested)
|
||||
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
|
||||
|
||||
add_requests(&packet);
|
||||
log_line(LOG_DEBUG, "Sending discover...\n");
|
||||
log_line("Sending discover...");
|
||||
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
||||
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
||||
}
|
||||
@ -132,7 +132,7 @@ int send_selecting(unsigned long xid, unsigned long server,
|
||||
|
||||
add_requests(&packet);
|
||||
addr.s_addr = requested;
|
||||
log_line(LOG_DEBUG, "Sending select for %s...\n", inet_ntoa(addr));
|
||||
log_line("Sending select for %s...", inet_ntoa(addr));
|
||||
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
||||
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
||||
}
|
||||
@ -149,7 +149,7 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
|
||||
packet.ciaddr = ciaddr;
|
||||
|
||||
add_requests(&packet);
|
||||
log_line(LOG_DEBUG, "Sending renew...\n");
|
||||
log_line("Sending renew...");
|
||||
if (server)
|
||||
ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
||||
else
|
||||
@ -171,7 +171,7 @@ int send_release(unsigned long server, unsigned long ciaddr)
|
||||
add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
|
||||
add_simple_option(packet.options, DHCP_SERVER_ID, server);
|
||||
|
||||
log_line(LOG_DEBUG, "Sending release...\n");
|
||||
log_line("Sending release...");
|
||||
return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
||||
}
|
||||
|
||||
@ -188,18 +188,18 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
|
||||
memset(&packet, 0, sizeof(struct udp_dhcp_packet));
|
||||
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
|
||||
if (bytes < 0) {
|
||||
debug(LOG_INFO, "couldn't read on raw listening socket -- ignoring\n");
|
||||
log_line("couldn't read on raw listening socket -- ignoring");
|
||||
usleep(500000); /* possible down interface, looping condition */
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
|
||||
debug(LOG_INFO, "message too short, ignoring\n");
|
||||
log_line("message too short, ignoring");
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (bytes < ntohs(packet.ip.tot_len)) {
|
||||
debug(LOG_INFO, "Truncated packet\n");
|
||||
log_line("Truncated packet");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -214,7 +214,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
|
||||
|| packet.udp.dest != htons(CLIENT_PORT)
|
||||
|| bytes > (int)sizeof(struct udp_dhcp_packet)
|
||||
|| ntohs(packet.udp.len) != (short)(bytes - sizeof(packet.ip))) {
|
||||
debug(LOG_INFO, "unrelated/bogus packet\n");
|
||||
log_line("unrelated/bogus packet");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -222,7 +222,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
|
||||
check = packet.ip.check;
|
||||
packet.ip.check = 0;
|
||||
if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
|
||||
debug(LOG_INFO, "bad IP header checksum, ignoring\n");
|
||||
log_line("bad IP header checksum, ignoring");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -238,7 +238,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
|
||||
packet.ip.daddr = dest;
|
||||
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
|
||||
if (check && check != checksum(&packet, bytes)) {
|
||||
debug(LOG_ERR, "packet with bad UDP checksum received, ignoring\n");
|
||||
log_error("packet with bad UDP checksum received, ignoring");
|
||||
return -2;
|
||||
}
|
||||
|
||||
@ -246,10 +246,10 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
|
||||
bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
|
||||
|
||||
if (ntohl(payload->cookie) != DHCP_MAGIC) {
|
||||
log_line(LOG_ERR, "received bogus message (bad magic) -- ignoring\n");
|
||||
log_error("received bogus message (bad magic) -- ignoring");
|
||||
return -2;
|
||||
}
|
||||
debug(LOG_INFO, "oooooh!!! got some!\n");
|
||||
log_line("oooooh!!! got some!");
|
||||
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
|
||||
|
||||
}
|
||||
|
68
ndhc/dhcpc.c
68
ndhc/dhcpc.c
@ -49,8 +49,9 @@
|
||||
#include "script.h"
|
||||
#include "socket.h"
|
||||
#include "log.h"
|
||||
#include "chroot.h"
|
||||
#include "rootcap.h"
|
||||
#include "nstrl.h"
|
||||
#include "strl.h"
|
||||
|
||||
#define VERSION "1.0"
|
||||
|
||||
@ -104,8 +105,8 @@ static void show_usage(void)
|
||||
/* just a little helper */
|
||||
static void change_mode(int new_mode)
|
||||
{
|
||||
debug(LOG_INFO, "entering %s listen mode",
|
||||
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
|
||||
log_line("entering %s listen mode",
|
||||
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
|
||||
close(fd);
|
||||
fd = -1;
|
||||
listen_mode = new_mode;
|
||||
@ -114,7 +115,7 @@ static void change_mode(int new_mode)
|
||||
/* perform a renew */
|
||||
static void perform_renew(void)
|
||||
{
|
||||
log_line(LOG_INFO, "Performing a DHCP renew...\n");
|
||||
log_line("Performing a DHCP renew...");
|
||||
switch (state) {
|
||||
case BOUND:
|
||||
change_mode(LISTEN_KERNEL);
|
||||
@ -154,12 +155,12 @@ static void perform_release(void)
|
||||
temp_addr.s_addr = server_addr;
|
||||
snprintf(buf, sizeof buf, "%s", inet_ntoa(temp_addr));
|
||||
temp_addr.s_addr = requested_ip;
|
||||
log_line(LOG_INFO, "Unicasting a release of %s to %s.\n",
|
||||
inet_ntoa(temp_addr), buf);
|
||||
log_line("Unicasting a release of %s to %s.",
|
||||
inet_ntoa(temp_addr), buf);
|
||||
send_release(server_addr, requested_ip); /* unicast */
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
}
|
||||
log_line(LOG_INFO, "Entering released state.\n");
|
||||
log_line("Entering released state.");
|
||||
|
||||
change_mode(LISTEN_NONE);
|
||||
state = RELEASED;
|
||||
@ -208,10 +209,10 @@ static void handle_timeout(void)
|
||||
packet_num++;
|
||||
} else {
|
||||
if (client_config.background_if_no_lease) {
|
||||
log_line(LOG_INFO, "No lease, going to background.\n");
|
||||
log_line("No lease, going to background.");
|
||||
background();
|
||||
} else if (client_config.abort_if_no_lease) {
|
||||
log_line(LOG_INFO, "No lease, failing.\n");
|
||||
log_line("No lease, failing.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* wait to try again */
|
||||
@ -245,7 +246,7 @@ static void handle_timeout(void)
|
||||
/* Lease is starting to run out, time to enter renewing state */
|
||||
state = RENEWING;
|
||||
change_mode(LISTEN_KERNEL);
|
||||
debug(LOG_INFO, "Entering renew state.\n");
|
||||
log_line("Entering renew state.");
|
||||
/* fall right through */
|
||||
case RENEWING:
|
||||
/* Either set a new T1, or enter REBINDING state */
|
||||
@ -253,7 +254,7 @@ static void handle_timeout(void)
|
||||
/* timed out, enter rebinding state */
|
||||
state = REBINDING;
|
||||
timeout = now + (t2 - t1);
|
||||
debug(LOG_INFO, "Entering rebinding state.\n");
|
||||
log_line("Entering rebinding state.");
|
||||
} else {
|
||||
/* send a request packet */
|
||||
send_renew(xid, server_addr, requested_ip); /* unicast */
|
||||
@ -267,7 +268,7 @@ static void handle_timeout(void)
|
||||
if ((lease - t2) <= (lease / 14400 + 1)) {
|
||||
/* timed out, enter init state */
|
||||
state = INIT_SELECTING;
|
||||
log_line(LOG_INFO, "Lease lost, entering init state.\n");
|
||||
log_line("Lease lost, entering init state.");
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
timeout = now;
|
||||
packet_num = 0;
|
||||
@ -295,7 +296,7 @@ static void handle_packet(void)
|
||||
struct in_addr temp_addr;
|
||||
struct dhcpMessage packet;
|
||||
|
||||
debug(LOG_INFO, "got a packet\n");
|
||||
log_line("got a packet");
|
||||
|
||||
if (listen_mode == LISTEN_KERNEL)
|
||||
len = get_packet(&packet, fd);
|
||||
@ -303,8 +304,8 @@ static void handle_packet(void)
|
||||
len = get_raw_packet(&packet, fd);
|
||||
|
||||
if (len == -1 && errno != EINTR) {
|
||||
debug(LOG_INFO, "error on read, %s, reopening socket.\n",
|
||||
strerror(errno));
|
||||
log_error("error on read, %s, reopening socket.",
|
||||
strerror(errno));
|
||||
change_mode(listen_mode); /* just close and reopen */
|
||||
}
|
||||
|
||||
@ -312,13 +313,13 @@ static void handle_packet(void)
|
||||
return;
|
||||
|
||||
if (packet.xid != xid) {
|
||||
debug(LOG_INFO, "Ignoring XID %lx (our xid is %lx).\n",
|
||||
(unsigned long) packet.xid, xid);
|
||||
log_line("Ignoring XID %lx (our xid is %lx).",
|
||||
(unsigned long) packet.xid, xid);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
|
||||
debug(LOG_ERR, "couldnt get option from packet -- ignoring\n");
|
||||
log_line("couldnt get option from packet -- ignoring");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -336,7 +337,7 @@ static void handle_packet(void)
|
||||
timeout = now;
|
||||
packet_num = 0;
|
||||
} else {
|
||||
debug(LOG_ERR, "No server ID in message\n");
|
||||
log_line("No server ID in message");
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -346,8 +347,7 @@ static void handle_packet(void)
|
||||
case REBINDING:
|
||||
if (*message == DHCPACK) {
|
||||
if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
|
||||
log_line(LOG_ERR,
|
||||
"No lease time received, assuming 1h.\n");
|
||||
log_line("No lease time received, assuming 1h.");
|
||||
lease = 60 * 60;
|
||||
} else {
|
||||
memcpy(&lease, temp, 4);
|
||||
@ -360,9 +360,8 @@ static void handle_packet(void)
|
||||
/* little fixed point for n * .875 */
|
||||
t2 = (lease * 0x7) >> 3;
|
||||
temp_addr.s_addr = packet.yiaddr;
|
||||
log_line(LOG_INFO,
|
||||
"Lease of %s obtained, lease time %ld.\n",
|
||||
inet_ntoa(temp_addr), lease);
|
||||
log_line("Lease of %s obtained, lease time %ld.",
|
||||
inet_ntoa(temp_addr), lease);
|
||||
start = now;
|
||||
timeout = t1 + start;
|
||||
requested_ip = packet.yiaddr;
|
||||
@ -379,7 +378,7 @@ static void handle_packet(void)
|
||||
|
||||
} else if (*message == DHCPNAK) {
|
||||
/* return to init state */
|
||||
log_line(LOG_INFO, "Received DHCP NAK.\n");
|
||||
log_line("Received DHCP NAK.");
|
||||
run_script(&packet, SCRIPT_NAK);
|
||||
if (state != REQUESTING)
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
@ -410,7 +409,7 @@ static int do_work(void)
|
||||
if (pending_release)
|
||||
perform_release();
|
||||
if (pending_exit) {
|
||||
log_line(LOG_INFO, "Received SIGTERM. Exiting gracefully.\n");
|
||||
log_line("Received SIGTERM. Exiting gracefully.");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@ -425,8 +424,8 @@ static int do_work(void)
|
||||
fd = raw_socket(client_config.ifindex);
|
||||
|
||||
if (fd < 0) {
|
||||
log_line(LOG_ERR, "FATAL: couldn't listen on socket: %s.\n",
|
||||
strerror(errno));
|
||||
log_error("FATAL: couldn't listen on socket: %s.",
|
||||
strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@ -439,19 +438,17 @@ static int do_work(void)
|
||||
FD_ZERO(&rfds);
|
||||
if (fd >= 0)
|
||||
FD_SET(fd, &rfds);
|
||||
debug(LOG_INFO, "Waiting on select...\n");
|
||||
if (select(fd + 1, &rfds, NULL, NULL, &tv) == -1) {
|
||||
switch (errno) {
|
||||
case EBADF:
|
||||
fd = -1;
|
||||
default:
|
||||
debug(LOG_ERR, "Error: \"%s\" on select!\n",
|
||||
strerror(errno));
|
||||
log_error("Error: \"%s\" on select!",
|
||||
strerror(errno));
|
||||
case EINTR: /* Signal received, go back to top. */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
debug(LOG_INFO, "select suceeded\n");
|
||||
|
||||
if (listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds))
|
||||
handle_packet();
|
||||
@ -553,7 +550,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
log_line(LOG_INFO, "ndhc client " VERSION " started.\n");
|
||||
log_line("ndhc client " VERSION " started.");
|
||||
|
||||
if (read_interface(client_config.interface, &client_config.ifindex,
|
||||
NULL, client_config.arp) < 0)
|
||||
@ -581,9 +578,10 @@ int main(int argc, char **argv)
|
||||
printf("Failed to chroot(%s)!\n", chroot_dir);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
drop_root(uid, gid,
|
||||
|
||||
set_cap(uid, gid,
|
||||
"cap_net_bind_service,cap_net_broadcast,cap_net_raw=ep");
|
||||
drop_root(uid, gid);
|
||||
|
||||
state = INIT_SELECTING;
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
|
20
ndhc/log.c
20
ndhc/log.c
@ -1,20 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include <stdarg.h>
|
||||
#include <syslog.h>
|
||||
|
||||
void log_line(int level, char *format, ...) {
|
||||
va_list argp;
|
||||
|
||||
if (format == NULL) return;
|
||||
|
||||
va_start(argp, format);
|
||||
vfprintf(stderr, format, argp);
|
||||
va_end(argp);
|
||||
openlog("ndhc", 0, 0);
|
||||
va_start(argp, format);
|
||||
vsyslog(level, format, argp);
|
||||
va_end(argp);
|
||||
closelog();
|
||||
}
|
||||
|
11
ndhc/log.h
11
ndhc/log.h
@ -1,11 +0,0 @@
|
||||
#ifndef H_LOG_H__
|
||||
#define H_LOG_H__
|
||||
#include <syslog.h>
|
||||
void log_line(int level, char *format, ...);
|
||||
#ifdef DEBUG
|
||||
#define debug log_line
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
#endif
|
||||
|
48
ndhc/nstrl.c
48
ndhc/nstrl.c
@ -1,48 +0,0 @@
|
||||
/* nstrl.c - strlcpy/strlcat implementation
|
||||
Time-stamp: <2003-05-28 02:35:13 njk>
|
||||
|
||||
(C) 2003 Nicholas Jay Kain <njk@aerifal.cx>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef HAVE_STRLCPY
|
||||
|
||||
size_t strlcpy (char *dest, char *src, size_t size)
|
||||
{
|
||||
register char *d = dest, *s = src;
|
||||
|
||||
for (; *s != '\0' && size > 0; size--, d++, s++)
|
||||
*d = *s;
|
||||
|
||||
*d = '\0';
|
||||
return (d - dest) + (s - src);
|
||||
}
|
||||
|
||||
size_t strlcat (char *dest, char *src, size_t size)
|
||||
{
|
||||
register char *d = dest, *s = src;
|
||||
|
||||
for (; size > 0 && *d != '\0'; size--, d++);
|
||||
|
||||
for (; *s != '\0' && size > 0; size--, d++, s++)
|
||||
*d = *s;
|
||||
|
||||
*d = '\0';
|
||||
return (d - dest) + (s - src);
|
||||
}
|
||||
|
||||
#endif
|
25
ndhc/nstrl.h
25
ndhc/nstrl.h
@ -1,25 +0,0 @@
|
||||
/* nstrl.h - header file for strlcpy/strlcat implementation
|
||||
Time-stamp: <2003-05-28 02:34:47 njk>
|
||||
|
||||
(C) 2003 Nicholas Jay Kain <njk@aerifal.cx>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
#ifndef NJK_HAVE_STRL_
|
||||
size_t strlcpy (char *dest, char *src, size_t size);
|
||||
size_t strlcat (char *dest, char *src, size_t size);
|
||||
#endif
|
||||
|
||||
#define NJK_HAVE_STRL_ 1
|
@ -70,12 +70,12 @@ unsigned char *get_option(struct dhcpMessage *packet, int code)
|
||||
optionptr = packet->options;
|
||||
while (!done) {
|
||||
if (i >= length) {
|
||||
log_line(LOG_WARNING, "bogus packet, option fields too long.\n");
|
||||
log_warning("bogus packet, option fields too long.");
|
||||
return NULL;
|
||||
}
|
||||
if (optionptr[i + OPT_CODE] == code) {
|
||||
if (i + 1 + optionptr[i + OPT_LEN] >= length) {
|
||||
log_line(LOG_WARNING, "bogus packet, option fields too long.\n");
|
||||
log_warning("bogus packet, option fields too long.");
|
||||
return NULL;
|
||||
}
|
||||
return optionptr + i + 2;
|
||||
@ -86,8 +86,7 @@ unsigned char *get_option(struct dhcpMessage *packet, int code)
|
||||
break;
|
||||
case DHCP_OPTION_OVER:
|
||||
if (i + 1 + optionptr[i + OPT_LEN] >= length) {
|
||||
log_line(LOG_WARNING,
|
||||
"bogus packet, option fields too long.\n");
|
||||
log_warning("bogus packet, option fields too long.");
|
||||
return NULL;
|
||||
}
|
||||
over = optionptr[i + 3];
|
||||
@ -137,11 +136,11 @@ int add_option_string(unsigned char *optionptr, unsigned char *string)
|
||||
|
||||
/* end position + string length + option code/length + end option */
|
||||
if (end + string[OPT_LEN] + 2 + 1 >= 308) {
|
||||
log_line(LOG_ERR, "Option 0x%02x did not fit into the packet!\n",
|
||||
log_error("Option 0x%02x did not fit into the packet!",
|
||||
string[OPT_CODE]);
|
||||
return 0;
|
||||
}
|
||||
debug(LOG_INFO, "adding option 0x%02x\n", string[OPT_CODE]);
|
||||
log_line("adding option 0x%02x", string[OPT_CODE]);
|
||||
memcpy(optionptr + end, string, string[OPT_LEN] + 2);
|
||||
optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
|
||||
return string[OPT_LEN] + 2;
|
||||
@ -162,7 +161,7 @@ int add_simple_option(unsigned char *optionptr, unsigned char code,
|
||||
option[OPT_LEN] = (unsigned char)length;
|
||||
|
||||
if (!length) {
|
||||
debug(LOG_ERR, "Could not add option 0x%02x\n", code);
|
||||
log_error("Could not add option 0x%02x", code);
|
||||
return 0;
|
||||
} else if (length == 1) {
|
||||
uint8_t t = (uint8_t)data;
|
||||
@ -196,8 +195,8 @@ void attach_option(struct option_set **opt_list, struct dhcp_option *option,
|
||||
|
||||
/* add it to an existing option */
|
||||
if ((existing = find_option(*opt_list, option->code))) {
|
||||
debug(LOG_INFO, "Attaching option %s to existing member of list\n",
|
||||
option->name);
|
||||
log_line("Attaching option %s to existing member of list",
|
||||
option->name);
|
||||
if (option->flags & OPTION_LIST) {
|
||||
if (existing->data[OPT_LEN] + length <= 255) {
|
||||
existing->data = realloc(existing->data,
|
||||
@ -209,7 +208,7 @@ void attach_option(struct option_set **opt_list, struct dhcp_option *option,
|
||||
in the future */
|
||||
} /* else, ignore the new data */
|
||||
} else {
|
||||
debug(LOG_INFO, "Attaching option %s to list\n", option->name);
|
||||
log_line("Attaching option %s to list", option->name);
|
||||
|
||||
/* make a new option */
|
||||
new = malloc(sizeof(struct option_set));
|
||||
|
@ -51,15 +51,15 @@ int get_packet(struct dhcpMessage *packet, int fd)
|
||||
memset(packet, 0, sizeof(struct dhcpMessage));
|
||||
bytes = read(fd, packet, sizeof(struct dhcpMessage));
|
||||
if (bytes < 0) {
|
||||
debug(LOG_INFO, "couldn't read on listening socket, ignoring\n");
|
||||
log_line("couldn't read on listening socket, ignoring");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ntohl(packet->cookie) != DHCP_MAGIC) {
|
||||
log_line(LOG_ERR, "received bogus message, ignoring.\n");
|
||||
log_error("received bogus message, ignoring.");
|
||||
return -2;
|
||||
}
|
||||
debug(LOG_INFO, "Received a packet\n");
|
||||
log_line("Received a packet");
|
||||
|
||||
if (packet->op == BOOTREQUEST
|
||||
&& (vendor = get_option(packet, DHCP_VENDOR)))
|
||||
@ -69,7 +69,7 @@ int get_packet(struct dhcpMessage *packet, int fd)
|
||||
&& !strncmp((char *)vendor, broken_vendors[i],
|
||||
vendor[OPT_LEN - 2]))
|
||||
{
|
||||
debug(LOG_INFO, "broken client (%s), forcing broadcast\n",
|
||||
log_line("broken client (%s), forcing broadcast",
|
||||
broken_vendors[i]);
|
||||
packet->flags |= htons(BROADCAST_FLAG);
|
||||
}
|
||||
@ -118,7 +118,7 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
||||
struct udp_dhcp_packet packet;
|
||||
|
||||
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
|
||||
debug(LOG_ERR, "socket call failed: %s\n", strerror(errno));
|
||||
log_error("socket call failed: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
||||
dest.sll_halen = 6;
|
||||
memcpy(dest.sll_addr, dest_arp, 6);
|
||||
if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
|
||||
debug(LOG_ERR, "bind call failed: %s\n", strerror(errno));
|
||||
log_error("bind call failed: %s", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
@ -155,8 +155,8 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
||||
result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0,
|
||||
(struct sockaddr *)&dest, sizeof dest);
|
||||
if (result <= 0) {
|
||||
debug(LOG_ERR, "write on socket failed: %s\n",
|
||||
strerror(errno));
|
||||
log_error("write on socket failed: %s",
|
||||
strerror(errno));
|
||||
}
|
||||
out_fd:
|
||||
close(fd);
|
||||
|
@ -7,61 +7,40 @@
|
||||
|
||||
#include "log.h"
|
||||
|
||||
static void set_cap(uid_t uid, gid_t gid, char *captxt)
|
||||
void set_cap(uid_t uid, gid_t gid, char *captxt)
|
||||
{
|
||||
cap_t caps;
|
||||
|
||||
if (!captxt) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: captxt == NULL\n");
|
||||
log_error("FATAL - set_cap: captxt == NULL");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
if (prctl(PR_SET_KEEPCAPS, 1)) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: prctl() failed\n");
|
||||
log_error("FATAL - set_cap: prctl() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (setgroups(0, NULL) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: setgroups() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
log_error("FATAL - set_cap: setgroups() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (setegid(gid) == -1 || seteuid(uid) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: seteuid() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
log_error("FATAL - set_cap: seteuid() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
caps = cap_from_text(captxt);
|
||||
if (!caps) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: cap_from_text() failed\n");
|
||||
log_error("FATAL - set_cap: cap_from_text() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
if (cap_set_proc(caps) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: cap_set_proc() failed\n");
|
||||
log_error("FATAL - set_cap: cap_set_proc() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
cap_free(caps);
|
||||
}
|
||||
|
||||
void drop_root(uid_t uid, gid_t gid, char *captxt)
|
||||
{
|
||||
if (!captxt) {
|
||||
log_line(LOG_ERR, "FATAL - drop_root: captxt == NULL\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (uid == 0 || gid == 0) {
|
||||
log_line(LOG_ERR, "FATAL - drop_root: attempt to drop root to root?\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
set_cap(uid, gid, captxt);
|
||||
|
||||
if (setregid(gid, gid) == -1 || setreuid(uid, uid) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - drop_root: failed to drop root!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,2 +1,6 @@
|
||||
void drop_root(uid_t uid, gid_t gid, char *captxt);
|
||||
#ifndef ROOTCAP_H_
|
||||
#define ROOTCAP_H_
|
||||
|
||||
void set_cap(uid_t uid, gid_t gid, char *captxt);
|
||||
|
||||
#endif /* ROOTCAP_H_ */
|
||||
|
@ -123,7 +123,7 @@ static int open_ifch(void) {
|
||||
ret = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
|
||||
|
||||
if (ret == -1) {
|
||||
log_line(LOG_ERR, "unable to connect to ifchd!\n");
|
||||
log_error("unable to connect to ifchd!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@ -139,14 +139,14 @@ sockwrite_again:
|
||||
if (ret == -1) {
|
||||
if (errno == EAGAIN)
|
||||
goto sockwrite_again;
|
||||
log_line(LOG_ERR, "error while writing to unix socket!\n");
|
||||
log_error("error while writing to unix socket!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (ret < 0) ret = 0;
|
||||
if ((unsigned int)ret < strlen(buf)) {
|
||||
log_line(LOG_ERR, "incomplete write!\n");
|
||||
log_error("incomplete write!");
|
||||
}
|
||||
debug(LOG_INFO, "writing: %s\n", (char *)buf);
|
||||
log_line("writing: %s", (char *)buf);
|
||||
}
|
||||
|
||||
static void deconfig_if(void)
|
||||
@ -251,7 +251,7 @@ void run_script(struct dhcpMessage *packet, int mode)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log_line(LOG_ERR, "invalid script mode: %d\n", mode);
|
||||
log_error("invalid script mode: %d", mode);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +37,7 @@
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include "log.h"
|
||||
#include "nstrl.h"
|
||||
#include "strl.h"
|
||||
|
||||
int read_interface(char *interface, int *ifindex, uint32_t *addr,
|
||||
unsigned char *arp)
|
||||
@ -48,39 +48,39 @@ int read_interface(char *interface, int *ifindex, uint32_t *addr,
|
||||
|
||||
memset(&ifr, 0, sizeof(struct ifreq));
|
||||
if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
|
||||
log_line(LOG_ERR, "socket failed!: %s\n", strerror(errno));
|
||||
log_error("socket failed!: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
|
||||
|
||||
if (addr) {
|
||||
if (addr) {
|
||||
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
|
||||
log_line(LOG_ERR, "Couldn't get IP for %s.\n", strerror(errno));
|
||||
log_error("Couldn't get IP for %s.", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
|
||||
*addr = our_ip->sin_addr.s_addr;
|
||||
debug(LOG_INFO, "%s (our ip) = %s\n", ifr.ifr_name,
|
||||
inet_ntoa(our_ip->sin_addr));
|
||||
log_line("%s (our ip) = %s", ifr.ifr_name,
|
||||
inet_ntoa(our_ip->sin_addr));
|
||||
}
|
||||
|
||||
if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
|
||||
log_line(LOG_ERR, "SIOCGIFINDEX failed!: %s\n", strerror(errno));
|
||||
log_error("SIOCGIFINDEX failed!: %s", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
debug(LOG_INFO, "adapter index %d\n", ifr.ifr_ifindex);
|
||||
log_line("adapter index %d", ifr.ifr_ifindex);
|
||||
*ifindex = ifr.ifr_ifindex;
|
||||
|
||||
if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
|
||||
log_line(LOG_ERR, "Couldn't get MAC for %s\n", strerror(errno));
|
||||
log_error("Couldn't get MAC for %s", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
|
||||
debug(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
log_line("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
|
||||
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
|
||||
ret = 0;
|
||||
out_fd:
|
||||
@ -96,9 +96,9 @@ int listen_socket(unsigned int ip, int port, char *inf)
|
||||
struct sockaddr_in addr;
|
||||
int n = 1;
|
||||
|
||||
debug(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
|
||||
log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
|
||||
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
|
||||
debug(LOG_ERR, "socket call failed: %s\n", strerror(errno));
|
||||
log_error("socket call failed: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -132,9 +132,9 @@ int raw_socket(int ifindex)
|
||||
int fd;
|
||||
struct sockaddr_ll sock;
|
||||
|
||||
debug(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
|
||||
log_line("Opening raw socket on ifindex %d", ifindex);
|
||||
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
|
||||
debug(LOG_ERR, "socket call failed: %s\n", strerror(errno));
|
||||
log_error("socket call failed: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ int raw_socket(int ifindex)
|
||||
sock.sll_protocol = htons(ETH_P_IP);
|
||||
sock.sll_ifindex = ifindex;
|
||||
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
|
||||
debug(LOG_ERR, "bind call failed: %s\n", strerror(errno));
|
||||
log_error("bind call failed: %s", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user