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