]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/misc-progs/restartdhcp.c
Diverse Fixes.
[people/pmueller/ipfire-2.x.git] / src / misc-progs / restartdhcp.c
1 /* SmoothWall helper program - restartdhcp
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Lawrence Manning, 2001
7 * Simple program intended to be installed setuid(0) that can be used for
8 * restarting DHCPd.
9 *
10 * $Id: restartdhcp.c,v 1.5.2.1 2004/11/03 13:50:26 alanh Exp $
11 *
12 */
13
14 #include "libsmooth.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <signal.h>
23 #include "setuid.h"
24
25 int main(void)
26 {
27 int fd = -1;
28 int fdblue = -1;
29 char buffer[STRING_SIZE];
30 char blue_dev[STRING_SIZE] = "", green_dev[STRING_SIZE] = "";
31 int pid;
32 struct keyvalue *kv = NULL;
33
34 if (!(initsetuid()))
35 exit(1);
36
37 memset(buffer, 0, STRING_SIZE);
38
39 /* Init the keyvalue structure */
40 kv=initkeyvalues();
41
42 /* Read in the current values */
43 if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))
44 {
45 fprintf(stderr, "Cannot read ethernet settings\n");
46 exit(1);
47 }
48
49 if (!findkey(kv, "GREEN_DEV", green_dev))
50 {
51 fprintf(stderr, "Cannot read GREEN_DEV\n");
52 exit(1);
53 }
54
55 if (!VALID_DEVICE(green_dev))
56 {
57 fprintf(stderr, "Bad GREEN_DEV: %s\n", green_dev);
58 exit(1);
59 }
60
61 /* Get the BLUE interface details */
62 findkey(kv, "BLUE_DEV", blue_dev);
63
64 freekeyvalues(kv);
65
66 if ((fdblue = open(CONFIG_ROOT "/dhcp/enable_blue", O_RDONLY)) != -1)
67 {
68 close(fdblue);
69 if (!VALID_DEVICE(blue_dev))
70 {
71 fprintf(stderr, "Bad BLUE_DEV: %s\n", blue_dev);
72 exit(1);
73 }
74 }
75
76 if ((fd = open("/var/run/dhcpd.pid", O_RDONLY)) != -1)
77 {
78 if (read(fd, buffer, STRING_SIZE - 1) == -1)
79 fprintf(stderr, "Couldn't read from pid file\n");
80 else
81 {
82 pid = atoi(buffer);
83 if (pid <= 1)
84 fprintf(stderr, "Bad pid value\n");
85 else
86 {
87 if (kill(pid, SIGTERM) == -1)
88 fprintf(stderr, "Unable to send SIGTERM\n");
89 else
90 unlink("/var/run/dhcpd.pid");
91 }
92 }
93 safe_system("/bin/killall -KILL dhcpd");
94 close(fd);
95 }
96
97 safe_system("/sbin/iptables -F DHCPBLUEINPUT");
98
99 buffer[0] = '\0';
100
101 if ((fd = open(CONFIG_ROOT "/dhcp/enable_green", O_RDONLY)) != -1)
102 {
103 close(fd);
104 if ((fdblue = open(CONFIG_ROOT "/dhcp/enable_blue", O_RDONLY)) != -1)
105 {
106 close(fdblue);
107
108 snprintf(buffer, STRING_SIZE-1, "/sbin/iptables -A DHCPBLUEINPUT -p tcp --source-port 68 --destination-port 67 -i %s -j ACCEPT > /dev/null 2>&1", blue_dev);
109 safe_system(buffer);
110 snprintf(buffer, STRING_SIZE-1, "/sbin/iptables -A DHCPBLUEINPUT -p udp --source-port 68 --destination-port 67 -i %s -j ACCEPT > /dev/null 2>&1", blue_dev);
111 safe_system(buffer);
112 snprintf(buffer, STRING_SIZE-1, "/usr/sbin/dhcpd -q %s %s", green_dev, blue_dev);
113 } else {
114 snprintf(buffer, STRING_SIZE-1, "/usr/sbin/dhcpd -q %s", green_dev);
115 }
116 safe_system(buffer);
117 } else {
118 if ((fdblue = open(CONFIG_ROOT "/dhcp/enable_blue", O_RDONLY)) != -1)
119 {
120 close(fdblue);
121
122 snprintf(buffer, STRING_SIZE-1, "/sbin/iptables -A DHCPBLUEINPUT -p tcp --source-port 68 --destination-port 67 -i %s -j ACCEPT > /dev/null 2>&1", blue_dev);
123 safe_system(buffer);
124 snprintf(buffer, STRING_SIZE-1, "/sbin/iptables -A DHCPBLUEINPUT -p udp --source-port 68 --destination-port 67 -i %s -j ACCEPT > /dev/null 2>&1", blue_dev);
125 safe_system(buffer);
126 snprintf(buffer, STRING_SIZE-1, "/usr/sbin/dhcpd -q %s", blue_dev);
127 safe_system(buffer);
128 }
129 }
130
131 if (buffer[0] != '\0')
132 {
133 /* Silly dhcpd creates pids with mode 640 */
134 sleep (1);
135 if ((fd = open("/var/run/dhcpd.pid", 0)) == -1)
136 {
137 fprintf(stderr, "No pid file\n");
138 return 1;
139 }
140 fchmod(fd, 00644);
141 close(fd);
142 }
143
144 return 0;
145 }