]>
Commit | Line | Data |
---|---|---|
cd1a2927 MT |
1 | /*\r |
2 | * setaliases - configure red aliased interfaces\r | |
3 | *\r | |
4 | * This program is distributed under the terms of the GNU General Public\r | |
5 | * Licence. See the file COPYING for details.\r | |
6 | *\r | |
7 | * (c) Steve Bootes, 2002/04/15\r | |
8 | *\r | |
9 | * 21/04/03 Robert Kerr Changed to link directly to libsmooth rather than\r | |
10 | * using a copy & paste\r | |
11 | *\r | |
12 | * $Id: setaliases.c,v 1.2.2.4 2004/08/22 22:01:44 alanh Exp $\r | |
13 | *\r | |
14 | */\r | |
15 | \r | |
16 | #include "libsmooth.h"\r | |
17 | #include "setuid.h"\r | |
18 | #include <stdio.h>\r | |
19 | #include <stdlib.h>\r | |
20 | #include <string.h>\r | |
21 | #include <unistd.h>\r | |
22 | #include <sys/types.h>\r | |
23 | #include <sys/stat.h>\r | |
24 | #include <fcntl.h>\r | |
25 | \r | |
26 | struct keyvalue *kv = NULL;\r | |
27 | FILE *file = NULL;\r | |
28 | \r | |
29 | void exithandler(void)\r | |
30 | {\r | |
31 | if (kv) freekeyvalues(kv);\r | |
32 | if (file) fclose(file);\r | |
33 | }\r | |
34 | \r | |
35 | int main(void)\r | |
36 | {\r | |
37 | char s[STRING_SIZE];\r | |
38 | char command[STRING_SIZE];\r | |
39 | char red_netmask[STRING_SIZE];\r | |
40 | char red_broadcast[STRING_SIZE];\r | |
41 | char red_dev[STRING_SIZE];\r | |
42 | char default_gateway[STRING_SIZE];\r | |
43 | char *aliasip;\r | |
44 | char *enabled;\r | |
45 | char *sptr;\r | |
46 | char *comment;\r | |
47 | int alias;\r | |
48 | int count;\r | |
49 | \r | |
50 | if (!(initsetuid()))\r | |
51 | {\r | |
52 | fprintf(stderr, "Cannot run setuid\n");\r | |
53 | exit(1);\r | |
54 | }\r | |
55 | \r | |
56 | atexit(exithandler);\r | |
57 | \r | |
58 | /* Init the keyvalue structure */\r | |
59 | kv=initkeyvalues();\r | |
60 | \r | |
61 | /* Read in the current values */\r | |
62 | if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))\r | |
63 | {\r | |
64 | fprintf(stderr, "Cannot read ethernet settings\n");\r | |
65 | exit(1);\r | |
66 | }\r | |
67 | \r | |
68 | /* Find the CONFIG_TYPE value */\r | |
69 | if (!findkey(kv, "CONFIG_TYPE", s))\r | |
70 | {\r | |
71 | fprintf(stderr, "Cannot read CONFIG_TYPE\n");\r | |
72 | exit(1);\r | |
73 | }\r | |
74 | \r | |
75 | /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,\r | |
76 | * exit gracefully. This is not an error... */\r | |
77 | if (!((strcmp(s, "2")==0) || (strcmp(s, "3")==0) || (strcmp(s, "6")==0) || (strcmp(s, "7")==0)))\r | |
78 | exit(0);\r | |
79 | \r | |
80 | /* Now check the RED_TYPE - aliases only work with STATIC.\r | |
81 | * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */\r | |
82 | \r | |
83 | /* Find the RED_TYPE value */\r | |
84 | if (!findkey(kv, "RED_TYPE", s))\r | |
85 | {\r | |
86 | fprintf(stderr, "Cannot read RED_TYPE\n");\r | |
87 | exit(1);\r | |
88 | }\r | |
89 | \r | |
90 | /* Make sure it's the right type */\r | |
91 | if (!(strcmp(s, "STATIC")==0)) \r | |
92 | exit(0);\r | |
93 | \r | |
94 | /* Get the RED interface details */\r | |
95 | if((!findkey(kv, "RED_NETMASK", red_netmask)) || (!findkey(kv, "RED_BROADCAST", red_broadcast)) || \r | |
96 | (!findkey(kv, "RED_DEV", red_dev)) || (!findkey(kv, "DEFAULT_GATEWAY", default_gateway)))\r | |
97 | {\r | |
98 | fprintf(stderr, "Cannot read RED settings\n");\r | |
99 | exit(1);\r | |
100 | }\r | |
101 | \r | |
102 | if (!VALID_DEVICE(red_dev))\r | |
103 | {\r | |
104 | fprintf(stderr, "Bad red_dev: %s\n", red_dev);\r | |
105 | exit(1);\r | |
106 | }\r | |
107 | \r | |
108 | if (!VALID_IP(red_netmask))\r | |
109 | {\r | |
110 | fprintf(stderr, "Bad red_netmask : %s\n", red_netmask);\r | |
111 | exit(1);\r | |
112 | }\r | |
113 | \r | |
114 | if (!VALID_IP(red_broadcast))\r | |
115 | {\r | |
116 | fprintf(stderr, "Bad red_broadcast : %s\n", red_broadcast);\r | |
117 | exit(1);\r | |
118 | }\r | |
119 | \r | |
120 | if (!VALID_IP(default_gateway))\r | |
121 | {\r | |
122 | fprintf(stderr, "Bad default_gateway : %s\n", default_gateway);\r | |
123 | exit(1);\r | |
124 | }\r | |
125 | \r | |
126 | /* down the aliases in turn until ifconfig complains */\r | |
127 | alias=0;\r | |
128 | do\r | |
129 | {\r | |
130 | memset(command, 0, STRING_SIZE);\r | |
131 | snprintf(command, STRING_SIZE-1, "/sbin/ifconfig %s:%d down 2>/dev/null", red_dev, alias++);\r | |
132 | } while (safe_system(command)==0);\r | |
133 | \r | |
134 | /* Now set up the new aliases from the config file */\r | |
135 | if (!(file = fopen(CONFIG_ROOT "/ethernet/aliases", "r")))\r | |
136 | {\r | |
137 | fprintf(stderr, "Unable to open aliases configuration file\n");\r | |
138 | exit(1);\r | |
139 | }\r | |
140 | \r | |
141 | alias=0;\r | |
142 | while (fgets(s, STRING_SIZE, file) != NULL)\r | |
143 | {\r | |
144 | if (s[strlen(s) - 1] == '\n')\r | |
145 | s[strlen(s) - 1] = '\0';\r | |
146 | sptr = strtok(s, ",");\r | |
147 | count = 0;\r | |
148 | aliasip = NULL;\r | |
149 | enabled = NULL;\r | |
150 | comment = NULL;\r | |
151 | while (sptr)\r | |
152 | {\r | |
153 | if (count == 0)\r | |
154 | aliasip = sptr;\r | |
155 | if (count == 1)\r | |
156 | enabled = sptr;\r | |
157 | else\r | |
158 | comment = sptr;\r | |
159 | count++;\r | |
160 | sptr = strtok(NULL, ",");\r | |
161 | }\r | |
162 | \r | |
163 | if (!(aliasip && enabled))\r | |
164 | continue;\r | |
165 | \r | |
166 | if (!VALID_IP(aliasip))\r | |
167 | {\r | |
168 | fprintf(stderr, "Bad alias : %s\n", aliasip);\r | |
169 | exit(1);\r | |
170 | }\r | |
171 | \r | |
172 | if (strcmp(enabled, "on") == 0)\r | |
173 | {\r | |
174 | memset(command, 0, STRING_SIZE);\r | |
175 | snprintf(command, STRING_SIZE-1, "/sbin/ifconfig %s:%d %s netmask %s broadcast %s up", red_dev, alias, aliasip, red_netmask, red_broadcast);\r | |
176 | safe_system(command);\r | |
177 | memset(command, 0, STRING_SIZE);\r | |
178 | snprintf(command, STRING_SIZE-1, "/usr/sbin/arping -q -c 1 -w 1 -i %s -S %s %s", red_dev, aliasip, default_gateway);\r | |
179 | safe_system(command);\r | |
180 | alias++;\r | |
181 | }\r | |
182 | }\r | |
183 | \r | |
184 | return 0;\r | |
185 | }\r | |
186 | \r |