]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/misc-progs/setaliases.c
a541a4fd20b418b0629b9515c9875455d5dac9e5
[people/pmueller/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.5 2006/07/25 23:15:20 franck78 Exp $
13 *
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23
24 #include "libsmooth.h"
25 #include "setuid.h"
26 #include "netutil.h"
27
28 struct keyvalue *kv = NULL;
29 FILE *file = NULL;
30
31 #define SCOPE 128
32
33 void exithandler(void)
34 {
35 if (kv) freekeyvalues(kv);
36 if (file) fclose(file);
37 }
38
39 int main(void)
40 {
41 char s[STRING_SIZE];
42 char command[STRING_SIZE];
43 char red_netmask[STRING_SIZE];
44 char red_dev[STRING_SIZE];
45 char default_gateway[STRING_SIZE];
46 char *aliasip;
47 char *enabled;
48 char *sptr;
49 char *comment;
50 char* intf = NULL;
51 int alias;
52 int count;
53
54 if (!(initsetuid()))
55 {
56 fprintf(stderr, "Cannot run setuid\n");
57 exit(1);
58 }
59
60 atexit(exithandler);
61
62 /* Init the keyvalue structure */
63 kv=initkeyvalues();
64
65 /* Read in the current values */
66 if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))
67 {
68 fprintf(stderr, "Cannot read ethernet settings\n");
69 exit(1);
70 }
71
72 /* Find the CONFIG_TYPE value */
73 if (!findkey(kv, "CONFIG_TYPE", s))
74 {
75 fprintf(stderr, "Cannot read CONFIG_TYPE\n");
76 exit(1);
77 }
78
79 /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
80 * exit gracefully. This is not an error... */
81 if (!((strcmp(s, "1")==0) || (strcmp(s, "2")==0) || (strcmp(s, "3")==0) || (strcmp(s, "4")==0)))
82 exit(0);
83
84 /* Now check the RED_TYPE - aliases only work with STATIC.
85 * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */
86
87 /* Find the RED_TYPE value */
88 if (!findkey(kv, "RED_TYPE", s))
89 {
90 fprintf(stderr, "Cannot read RED_TYPE\n");
91 exit(1);
92 }
93
94 /* Make sure it's the right type */
95 if (!(strcmp(s, "STATIC")==0))
96 exit(0);
97
98 /* Get the RED interface details */
99 if((!findkey(kv, "RED_NETMASK", red_netmask)) ||
100 (!findkey(kv, "RED_DEV", red_dev)) || (!findkey(kv, "DEFAULT_GATEWAY", default_gateway)))
101 {
102 fprintf(stderr, "Cannot read RED settings\n");
103 exit(1);
104 }
105
106 if (!VALID_DEVICE(red_dev))
107 {
108 fprintf(stderr, "Bad red_dev: %s\n", red_dev);
109 exit(1);
110 }
111
112 if (!VALID_IP(red_netmask))
113 {
114 fprintf(stderr, "Bad red_netmask : %s\n", red_netmask);
115 exit(1);
116 }
117
118 if (!VALID_IP(default_gateway))
119 {
120 fprintf(stderr, "Bad default_gateway : %s\n", default_gateway);
121 exit(1);
122 }
123
124 // Flush all previous aliases
125 alias = 0;
126 do {
127 snprintf(command, STRING_SIZE - 1,
128 "ip addr flush dev red%d scope %d 2>/dev/null", alias++, SCOPE);
129 } while (safe_system(command) == 0);
130
131 /* Now set up the new aliases from the config file */
132 if (!(file = fopen(CONFIG_ROOT "/ethernet/aliases", "r")))
133 {
134 fprintf(stderr, "Unable to open aliases configuration file\n");
135 exit(1);
136 }
137
138 alias=0;
139 int linecounter = 0;
140 while (fgets(s, STRING_SIZE, file) != NULL)
141 {
142 linecounter++;
143 if (s[strlen(s) - 1] == '\n')
144 s[strlen(s) - 1] = '\0';
145 count = 0;
146 aliasip = NULL;
147 enabled = NULL;
148 comment = NULL;
149 intf = NULL;
150 sptr = strtok(s, ",");
151 while (sptr)
152 {
153 if (count == 0)
154 aliasip = sptr;
155 else if (count == 1)
156 enabled = sptr;
157 else if (count == 2)
158 comment = sptr;
159 else if (count == 3)
160 intf = sptr;
161 count++;
162 sptr = strtok(NULL, ",");
163 }
164
165 if (!(aliasip && enabled)) {
166 fprintf(stderr, "Incomplete data line: in %s(%d)\n",
167 CONFIG_ROOT "/ethernet/aliases",
168 linecounter);
169 exit(1);
170 }
171 if (!strcmp(enabled, "on") == 0) /* disabled rule? */
172 continue;
173
174 if (!VALID_IP(aliasip))
175 {
176 fprintf(stderr, "Bad alias : %s in %s(%d)\n",
177 aliasip,
178 CONFIG_ROOT "/ethernet/aliases",
179 linecounter);
180 exit(1);
181 }
182
183 // Default to RED_DEV if intf isn't set
184 if (!intf)
185 intf = red_dev;
186
187 snprintf(command, STRING_SIZE - 1, "ip addr add %s/%s dev %s scope %d",
188 aliasip, red_netmask, intf, SCOPE);
189 safe_system(command);
190
191 alias++;
192 }
193 return 0;
194 }