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