]>
git.ipfire.org Git - people/teissler/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 $
16 #include "libsmooth.h"
22 #include <sys/types.h>
26 struct keyvalue
*kv
= NULL
;
29 void exithandler(void)
31 if (kv
) freekeyvalues(kv
);
32 if (file
) fclose(file
);
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
];
52 fprintf(stderr
, "Cannot run setuid\n");
58 /* Init the keyvalue structure */
61 /* Read in the current values */
62 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
64 fprintf(stderr
, "Cannot read ethernet settings\n");
68 /* Find the CONFIG_TYPE value */
69 if (!findkey(kv
, "CONFIG_TYPE", s
))
71 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
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
, "1")==0) || (strcmp(s
, "2")==0) || (strcmp(s
, "3")==0) || (strcmp(s
, "4")==0)))
80 /* Now check the RED_TYPE - aliases only work with STATIC.
81 * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */
83 /* Find the RED_TYPE value */
84 if (!findkey(kv
, "RED_TYPE", s
))
86 fprintf(stderr
, "Cannot read RED_TYPE\n");
90 /* Make sure it's the right type */
91 if (!(strcmp(s
, "STATIC")==0))
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
)))
98 fprintf(stderr
, "Cannot read RED settings\n");
102 if (!VALID_DEVICE(red_dev
))
104 fprintf(stderr
, "Bad red_dev: %s\n", red_dev
);
108 if (!VALID_IP(red_netmask
))
110 fprintf(stderr
, "Bad red_netmask : %s\n", red_netmask
);
114 if (!VALID_IP(red_broadcast
))
116 fprintf(stderr
, "Bad red_broadcast : %s\n", red_broadcast
);
120 if (!VALID_IP(default_gateway
))
122 fprintf(stderr
, "Bad default_gateway : %s\n", default_gateway
);
126 /* down the aliases in turn until ifconfig complains */
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);
134 /* Now set up the new aliases from the config file */
135 if (!(file
= fopen(CONFIG_ROOT
"/ethernet/aliases", "r")))
137 fprintf(stderr
, "Unable to open aliases configuration file\n");
143 while (fgets(s
, STRING_SIZE
, file
) != NULL
)
146 if (s
[strlen(s
) - 1] == '\n')
147 s
[strlen(s
) - 1] = '\0';
152 sptr
= strtok(s
, ",");
162 sptr
= strtok(NULL
, ",");
165 if (!(aliasip
&& enabled
)) {
166 fprintf(stderr
, "Incomplete data line: in %s(%d)\n",
167 CONFIG_ROOT
"/ethernet/aliases",
171 if (!strcmp(enabled
, "on") == 0) /* disabled rule? */
174 if (!VALID_IP(aliasip
))
176 fprintf(stderr
, "Bad alias : %s in %s(%d)\n",
178 CONFIG_ROOT
"/ethernet/aliases",
183 memset(command
, 0, STRING_SIZE
);
184 snprintf(command
, STRING_SIZE
-1,
185 "/sbin/ifconfig %s:%d %s netmask %s broadcast %s up",
186 red_dev
, alias
, aliasip
, red_netmask
, red_broadcast
);
187 safe_system(command
);
188 memset(command
, 0, STRING_SIZE
);
189 snprintf(command
, STRING_SIZE
-1,
190 "/usr/sbin/arping -q -c 1 -w 1 -i %s -S %s %s",
191 red_dev
, aliasip
, default_gateway
);
192 safe_system(command
);