]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/setaliases.c
2 * setaliases - configure red aliased interfaces
4 * This program is distributed under the terms of the GNU General Public
5 * Licence. See the file COPYING for details.
7 * (c) Steve Bootes, 2002/04/15
9 * 21/04/03 Robert Kerr Changed to link directly to libsmooth rather than
10 * using a copy & paste
12 * $Id: setaliases.c,v 1.2.2.5 2006/07/25 23:15:20 franck78 Exp $
20 #include <sys/types.h>
24 #include "libsmooth.h"
28 struct keyvalue
*kv
= NULL
;
31 void exithandler(void)
33 if (kv
) freekeyvalues(kv
);
34 if (file
) fclose(file
);
40 char command
[STRING_SIZE
];
41 char red_netmask
[STRING_SIZE
];
42 char red_broadcast
[STRING_SIZE
];
43 char red_dev
[STRING_SIZE
];
44 char default_gateway
[STRING_SIZE
];
54 fprintf(stderr
, "Cannot run setuid\n");
60 /* Init the keyvalue structure */
63 /* Read in the current values */
64 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
66 fprintf(stderr
, "Cannot read ethernet settings\n");
70 /* Find the CONFIG_TYPE value */
71 if (!findkey(kv
, "CONFIG_TYPE", s
))
73 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
77 /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
78 * exit gracefully. This is not an error... */
79 if (!((strcmp(s
, "1")==0) || (strcmp(s
, "2")==0) || (strcmp(s
, "3")==0) || (strcmp(s
, "4")==0)))
82 /* Now check the RED_TYPE - aliases only work with STATIC.
83 * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */
85 /* Find the RED_TYPE value */
86 if (!findkey(kv
, "RED_TYPE", s
))
88 fprintf(stderr
, "Cannot read RED_TYPE\n");
92 /* Make sure it's the right type */
93 if (!(strcmp(s
, "STATIC")==0))
96 /* Get the RED interface details */
97 if((!findkey(kv
, "RED_NETMASK", red_netmask
)) || (!findkey(kv
, "RED_BROADCAST", red_broadcast
)) ||
98 (!findkey(kv
, "RED_DEV", red_dev
)) || (!findkey(kv
, "DEFAULT_GATEWAY", default_gateway
)))
100 fprintf(stderr
, "Cannot read RED settings\n");
104 if (!VALID_DEVICE(red_dev
))
106 fprintf(stderr
, "Bad red_dev: %s\n", red_dev
);
110 if (!VALID_IP(red_netmask
))
112 fprintf(stderr
, "Bad red_netmask : %s\n", red_netmask
);
116 if (!VALID_IP(red_broadcast
))
118 fprintf(stderr
, "Bad red_broadcast : %s\n", red_broadcast
);
122 if (!VALID_IP(default_gateway
))
124 fprintf(stderr
, "Bad default_gateway : %s\n", default_gateway
);
128 /* down the aliases in turn until ifconfig complains */
132 memset(command
, 0, STRING_SIZE
);
133 snprintf(command
, STRING_SIZE
-1, "/sbin/ifconfig %s:%d down 2>/dev/null", red_dev
, alias
++);
134 } while (safe_system(command
)==0);
136 /* Now set up the new aliases from the config file */
137 if (!(file
= fopen(CONFIG_ROOT
"/ethernet/aliases", "r")))
139 fprintf(stderr
, "Unable to open aliases configuration file\n");
145 while (fgets(s
, STRING_SIZE
, file
) != NULL
)
148 if (s
[strlen(s
) - 1] == '\n')
149 s
[strlen(s
) - 1] = '\0';
154 sptr
= strtok(s
, ",");
164 sptr
= strtok(NULL
, ",");
167 if (!(aliasip
&& enabled
)) {
168 fprintf(stderr
, "Incomplete data line: in %s(%d)\n",
169 CONFIG_ROOT
"/ethernet/aliases",
173 if (!strcmp(enabled
, "on") == 0) /* disabled rule? */
176 if (!VALID_IP(aliasip
))
178 fprintf(stderr
, "Bad alias : %s in %s(%d)\n",
180 CONFIG_ROOT
"/ethernet/aliases",
185 memset(command
, 0, STRING_SIZE
);
186 snprintf(command
, STRING_SIZE
-1,
187 "/sbin/ifconfig %s:%d %s netmask %s broadcast %s up",
188 red_dev
, alias
, aliasip
, red_netmask
, red_broadcast
);
189 safe_system(command
);
190 memset(command
, 0, STRING_SIZE
);
191 snprintf(command
, STRING_SIZE
-1,
192 "/usr/sbin/arping -q -c 1 -w 1 -i %s -S %s %s",
193 red_dev
, aliasip
, default_gateway
);
194 safe_system(command
);