]>
git.ipfire.org Git - people/pmueller/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_dev
[STRING_SIZE
];
43 char default_gateway
[STRING_SIZE
];
53 fprintf(stderr
, "Cannot run setuid\n");
59 /* Init the keyvalue structure */
62 /* Read in the current values */
63 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
65 fprintf(stderr
, "Cannot read ethernet settings\n");
69 /* Find the CONFIG_TYPE value */
70 if (!findkey(kv
, "CONFIG_TYPE", s
))
72 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
76 /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
77 * exit gracefully. This is not an error... */
78 if (!((strcmp(s
, "1")==0) || (strcmp(s
, "2")==0) || (strcmp(s
, "3")==0) || (strcmp(s
, "4")==0)))
81 /* Now check the RED_TYPE - aliases only work with STATIC.
82 * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */
84 /* Find the RED_TYPE value */
85 if (!findkey(kv
, "RED_TYPE", s
))
87 fprintf(stderr
, "Cannot read RED_TYPE\n");
91 /* Make sure it's the right type */
92 if (!(strcmp(s
, "STATIC")==0))
95 /* Get the RED interface details */
96 if((!findkey(kv
, "RED_NETMASK", red_netmask
)) ||
97 (!findkey(kv
, "RED_DEV", red_dev
)) || (!findkey(kv
, "DEFAULT_GATEWAY", default_gateway
)))
99 fprintf(stderr
, "Cannot read RED settings\n");
103 if (!VALID_DEVICE(red_dev
))
105 fprintf(stderr
, "Bad red_dev: %s\n", red_dev
);
109 if (!VALID_IP(red_netmask
))
111 fprintf(stderr
, "Bad red_netmask : %s\n", red_netmask
);
115 if (!VALID_IP(default_gateway
))
117 fprintf(stderr
, "Bad default_gateway : %s\n", default_gateway
);
121 /* down the aliases in turn until ifconfig complains */
125 memset(command
, 0, STRING_SIZE
);
126 snprintf(command
, STRING_SIZE
-1, "/sbin/ifconfig %s:%d down 2>/dev/null", red_dev
, alias
++);
127 } while (safe_system(command
)==0);
129 /* Now set up the new aliases from the config file */
130 if (!(file
= fopen(CONFIG_ROOT
"/ethernet/aliases", "r")))
132 fprintf(stderr
, "Unable to open aliases configuration file\n");
138 while (fgets(s
, STRING_SIZE
, file
) != NULL
)
141 if (s
[strlen(s
) - 1] == '\n')
142 s
[strlen(s
) - 1] = '\0';
147 sptr
= strtok(s
, ",");
157 sptr
= strtok(NULL
, ",");
160 if (!(aliasip
&& enabled
)) {
161 fprintf(stderr
, "Incomplete data line: in %s(%d)\n",
162 CONFIG_ROOT
"/ethernet/aliases",
166 if (!strcmp(enabled
, "on") == 0) /* disabled rule? */
169 if (!VALID_IP(aliasip
))
171 fprintf(stderr
, "Bad alias : %s in %s(%d)\n",
173 CONFIG_ROOT
"/ethernet/aliases",
178 memset(command
, 0, STRING_SIZE
);
179 snprintf(command
, STRING_SIZE
-1,
180 "/sbin/ifconfig %s:%d %s netmask %s up",
181 red_dev
, alias
, aliasip
, red_netmask
);
182 safe_system(command
);
183 memset(command
, 0, STRING_SIZE
);
184 snprintf(command
, STRING_SIZE
-1,
185 "/usr/sbin/arping -q -c 1 -w 1 -i %s -S %s %s",
186 red_dev
, aliasip
, default_gateway
);
187 safe_system(command
);