-/*\r
- * setaliases - configure red aliased interfaces\r
- *\r
- * This program is distributed under the terms of the GNU General Public\r
- * Licence. See the file COPYING for details.\r
- *\r
- * (c) Steve Bootes, 2002/04/15\r
- *\r
- * 21/04/03 Robert Kerr Changed to link directly to libsmooth rather than\r
- * using a copy & paste\r
- *\r
- * $Id: setaliases.c,v 1.2.2.5 2006/07/25 23:15:20 franck78 Exp $\r
- *\r
- */\r
-\r
-#include "libsmooth.h"\r
-#include "setuid.h"\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <unistd.h>\r
-#include <sys/types.h>\r
-#include <sys/stat.h>\r
-#include <fcntl.h>\r
-\r
-struct keyvalue *kv = NULL;\r
-FILE *file = NULL;\r
-\r
-void exithandler(void)\r
-{\r
- if (kv) freekeyvalues(kv);\r
- if (file) fclose(file);\r
-}\r
-\r
-int main(void)\r
-{\r
- char s[STRING_SIZE];\r
- char command[STRING_SIZE];\r
- char red_netmask[STRING_SIZE];\r
- char red_broadcast[STRING_SIZE];\r
- char red_dev[STRING_SIZE];\r
- char default_gateway[STRING_SIZE];\r
- char *aliasip;\r
- char *enabled;\r
- char *sptr;\r
- char *comment;\r
- int alias;\r
- int count;\r
-\r
- if (!(initsetuid()))\r
- {\r
- fprintf(stderr, "Cannot run setuid\n");\r
- exit(1);\r
- }\r
-\r
- atexit(exithandler);\r
-\r
- /* Init the keyvalue structure */\r
- kv=initkeyvalues();\r
-\r
- /* Read in the current values */\r
- if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))\r
- {\r
- fprintf(stderr, "Cannot read ethernet settings\n");\r
- exit(1);\r
- }\r
-\r
- /* Find the CONFIG_TYPE value */\r
- if (!findkey(kv, "CONFIG_TYPE", s))\r
- {\r
- fprintf(stderr, "Cannot read CONFIG_TYPE\n");\r
- exit(1);\r
- }\r
-\r
- /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,\r
- * exit gracefully. This is not an error... */\r
- if (!((strcmp(s, "1")==0) || (strcmp(s, "2")==0) || (strcmp(s, "3")==0) || (strcmp(s, "4")==0)))\r
- exit(0);\r
-\r
- /* Now check the RED_TYPE - aliases only work with STATIC.\r
- * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */\r
-\r
- /* Find the RED_TYPE value */\r
- if (!findkey(kv, "RED_TYPE", s))\r
- {\r
- fprintf(stderr, "Cannot read RED_TYPE\n");\r
- exit(1);\r
- }\r
- \r
- /* Make sure it's the right type */\r
- if (!(strcmp(s, "STATIC")==0)) \r
- exit(0);\r
-\r
- /* Get the RED interface details */\r
- if((!findkey(kv, "RED_NETMASK", red_netmask)) || (!findkey(kv, "RED_BROADCAST", red_broadcast)) || \r
- (!findkey(kv, "RED_DEV", red_dev)) || (!findkey(kv, "DEFAULT_GATEWAY", default_gateway)))\r
- {\r
- fprintf(stderr, "Cannot read RED settings\n");\r
- exit(1);\r
- }\r
- \r
- if (!VALID_DEVICE(red_dev))\r
- {\r
- fprintf(stderr, "Bad red_dev: %s\n", red_dev);\r
- exit(1);\r
- }\r
-\r
- if (!VALID_IP(red_netmask))\r
- {\r
- fprintf(stderr, "Bad red_netmask : %s\n", red_netmask);\r
- exit(1);\r
- }\r
-\r
- if (!VALID_IP(red_broadcast))\r
- {\r
- fprintf(stderr, "Bad red_broadcast : %s\n", red_broadcast);\r
- exit(1);\r
- }\r
-\r
- if (!VALID_IP(default_gateway))\r
- {\r
- fprintf(stderr, "Bad default_gateway : %s\n", default_gateway);\r
- exit(1);\r
- }\r
-\r
- /* down the aliases in turn until ifconfig complains */\r
- alias=0;\r
- do\r
- {\r
- memset(command, 0, STRING_SIZE);\r
- snprintf(command, STRING_SIZE-1, "/sbin/ifconfig %s:%d down 2>/dev/null", red_dev, alias++);\r
- } while (safe_system(command)==0);\r
-\r
- /* Now set up the new aliases from the config file */\r
- if (!(file = fopen(CONFIG_ROOT "/ethernet/aliases", "r")))\r
- {\r
- fprintf(stderr, "Unable to open aliases configuration file\n");\r
- exit(1);\r
- }\r
-\r
- alias=0;\r
- int linecounter = 0;\r
- while (fgets(s, STRING_SIZE, file) != NULL)\r
- {\r
- linecounter++;\r
- if (s[strlen(s) - 1] == '\n')\r
- s[strlen(s) - 1] = '\0';\r
- count = 0;\r
- aliasip = NULL;\r
- enabled = NULL;\r
- comment = NULL;\r
- sptr = strtok(s, ",");\r
- while (sptr)\r
- {\r
- if (count == 0)\r
- aliasip = sptr;\r
- if (count == 1)\r
- enabled = sptr;\r
- else\r
- comment = sptr;\r
- count++;\r
- sptr = strtok(NULL, ",");\r
- }\r
-\r
- if (!(aliasip && enabled)) {\r
- fprintf(stderr, "Incomplete data line: in %s(%d)\n",\r
- CONFIG_ROOT "/ethernet/aliases",\r
- linecounter);\r
- exit(1);\r
- }\r
- if (!strcmp(enabled, "on") == 0) /* disabled rule? */\r
- continue;\r
-\r
- if (!VALID_IP(aliasip))\r
- {\r
- fprintf(stderr, "Bad alias : %s in %s(%d)\n",\r
- aliasip,\r
- CONFIG_ROOT "/ethernet/aliases",\r
- linecounter);\r
- exit(1);\r
- }\r
-\r
- memset(command, 0, STRING_SIZE);\r
- snprintf(command, STRING_SIZE-1,\r
- "/sbin/ifconfig %s:%d %s netmask %s broadcast %s up",\r
- red_dev, alias, aliasip, red_netmask, red_broadcast);\r
- safe_system(command);\r
- memset(command, 0, STRING_SIZE);\r
- snprintf(command, STRING_SIZE-1,\r
- "/usr/sbin/arping -q -c 1 -w 1 -i %s -S %s %s",\r
- red_dev, aliasip, default_gateway);\r
- safe_system(command);\r
- alias++;\r
- }\r
- return 0;\r
-}\r
+/*
+ * setaliases - configure red aliased interfaces
+ *
+ * This program is distributed under the terms of the GNU General Public
+ * Licence. See the file COPYING for details.
+ *
+ * (c) Steve Bootes, 2002/04/15
+ *
+ * 21/04/03 Robert Kerr Changed to link directly to libsmooth rather than
+ * using a copy & paste
+ *
+ * $Id: setaliases.c,v 1.2.2.5 2006/07/25 23:15:20 franck78 Exp $
+ *
+ */
+
+#include "libsmooth.h"
+#include "setuid.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+struct keyvalue *kv = NULL;
+FILE *file = NULL;
+
+void exithandler(void)
+{
+ if (kv) freekeyvalues(kv);
+ if (file) fclose(file);
+}
+
+int main(void)
+{
+ char s[STRING_SIZE];
+ char command[STRING_SIZE];
+ char red_netmask[STRING_SIZE];
+ char red_broadcast[STRING_SIZE];
+ char red_dev[STRING_SIZE];
+ char default_gateway[STRING_SIZE];
+ char *aliasip;
+ char *enabled;
+ char *sptr;
+ char *comment;
+ int alias;
+ int count;
+
+ if (!(initsetuid()))
+ {
+ fprintf(stderr, "Cannot run setuid\n");
+ exit(1);
+ }
+
+ atexit(exithandler);
+
+ /* Init the keyvalue structure */
+ kv=initkeyvalues();
+
+ /* Read in the current values */
+ if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))
+ {
+ fprintf(stderr, "Cannot read ethernet settings\n");
+ exit(1);
+ }
+
+ /* Find the CONFIG_TYPE value */
+ if (!findkey(kv, "CONFIG_TYPE", s))
+ {
+ fprintf(stderr, "Cannot read CONFIG_TYPE\n");
+ exit(1);
+ }
+
+ /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
+ * exit gracefully. This is not an error... */
+ if (!((strcmp(s, "1")==0) || (strcmp(s, "2")==0) || (strcmp(s, "3")==0) || (strcmp(s, "4")==0)))
+ exit(0);
+
+ /* Now check the RED_TYPE - aliases only work with STATIC.
+ * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */
+
+ /* Find the RED_TYPE value */
+ if (!findkey(kv, "RED_TYPE", s))
+ {
+ fprintf(stderr, "Cannot read RED_TYPE\n");
+ exit(1);
+ }
+
+ /* Make sure it's the right type */
+ if (!(strcmp(s, "STATIC")==0))
+ exit(0);
+
+ /* Get the RED interface details */
+ if((!findkey(kv, "RED_NETMASK", red_netmask)) || (!findkey(kv, "RED_BROADCAST", red_broadcast)) ||
+ (!findkey(kv, "RED_DEV", red_dev)) || (!findkey(kv, "DEFAULT_GATEWAY", default_gateway)))
+ {
+ fprintf(stderr, "Cannot read RED settings\n");
+ exit(1);
+ }
+
+ if (!VALID_DEVICE(red_dev))
+ {
+ fprintf(stderr, "Bad red_dev: %s\n", red_dev);
+ exit(1);
+ }
+
+ if (!VALID_IP(red_netmask))
+ {
+ fprintf(stderr, "Bad red_netmask : %s\n", red_netmask);
+ exit(1);
+ }
+
+ if (!VALID_IP(red_broadcast))
+ {
+ fprintf(stderr, "Bad red_broadcast : %s\n", red_broadcast);
+ exit(1);
+ }
+
+ if (!VALID_IP(default_gateway))
+ {
+ fprintf(stderr, "Bad default_gateway : %s\n", default_gateway);
+ exit(1);
+ }
+
+ /* down the aliases in turn until ifconfig complains */
+ alias=0;
+ do
+ {
+ memset(command, 0, STRING_SIZE);
+ snprintf(command, STRING_SIZE-1, "/sbin/ifconfig %s:%d down 2>/dev/null", red_dev, alias++);
+ } while (safe_system(command)==0);
+
+ /* Now set up the new aliases from the config file */
+ if (!(file = fopen(CONFIG_ROOT "/ethernet/aliases", "r")))
+ {
+ fprintf(stderr, "Unable to open aliases configuration file\n");
+ exit(1);
+ }
+
+ alias=0;
+ int linecounter = 0;
+ while (fgets(s, STRING_SIZE, file) != NULL)
+ {
+ linecounter++;
+ if (s[strlen(s) - 1] == '\n')
+ s[strlen(s) - 1] = '\0';
+ count = 0;
+ aliasip = NULL;
+ enabled = NULL;
+ comment = NULL;
+ sptr = strtok(s, ",");
+ while (sptr)
+ {
+ if (count == 0)
+ aliasip = sptr;
+ if (count == 1)
+ enabled = sptr;
+ else
+ comment = sptr;
+ count++;
+ sptr = strtok(NULL, ",");
+ }
+
+ if (!(aliasip && enabled)) {
+ fprintf(stderr, "Incomplete data line: in %s(%d)\n",
+ CONFIG_ROOT "/ethernet/aliases",
+ linecounter);
+ exit(1);
+ }
+ if (!strcmp(enabled, "on") == 0) /* disabled rule? */
+ continue;
+
+ if (!VALID_IP(aliasip))
+ {
+ fprintf(stderr, "Bad alias : %s in %s(%d)\n",
+ aliasip,
+ CONFIG_ROOT "/ethernet/aliases",
+ linecounter);
+ exit(1);
+ }
+
+ memset(command, 0, STRING_SIZE);
+ snprintf(command, STRING_SIZE-1,
+ "/sbin/ifconfig %s:%d %s netmask %s broadcast %s up",
+ red_dev, alias, aliasip, red_netmask, red_broadcast);
+ safe_system(command);
+ memset(command, 0, STRING_SIZE);
+ snprintf(command, STRING_SIZE-1,
+ "/usr/sbin/arping -q -c 1 -w 1 -i %s -S %s %s",
+ red_dev, aliasip, default_gateway);
+ safe_system(command);
+ alias++;
+ }
+ return 0;
+}