]> git.pld-linux.org Git - projects/rc-scripts.git/commitdiff
Update bunch of utils in src/. New getnhostid utility. ipcalc has enhanced PREFIX...
authorArkadiusz Miśkiewicz <arekm@maven.pl>
Thu, 30 Oct 2003 23:58:03 +0000 (23:58 +0000)
committerArkadiusz Miśkiewicz <arekm@maven.pl>
Thu, 30 Oct 2003 23:58:03 +0000 (23:58 +0000)
svn-id: @1463

src/Makefile.am
src/consoletype.c
src/genhostid.c [new file with mode: 0644]
src/getkey.c
src/initlog.c
src/ipcalc.c
src/netreport.c
src/ppp-watch.c
src/process.c
src/usernetctl.c

index 4eadc0ac221c4fdd0c81acd243a60c1983ec4726..90df79d28c7363eece60cdf72fbfda3b052cb60c 100644 (file)
@@ -12,6 +12,7 @@ bin_PROGRAMS = \
        resolvesymlink
        
 sbin_PROGRAMS =        \
+       genhostid \
        netreport \
        usernetctl \
        initlog \
@@ -32,6 +33,8 @@ resolvesymlink_SOURCE = resolvesymlink.c
 ipcalc_SOURCES = ipcalc.c
 ipcalc_LDADD = -lpopt
 
+genhostid_SOURCES = genhostid.c
+
 netreport_SOURCES = netreport.c
 
 testd_SOURCES = testd.c
@@ -47,6 +50,7 @@ initlog_SOURCES = initlog.c initlog.h process.c process.h
 initlog_LDADD = -lpopt
 
 getkey_SOURCES = getkey.c
+getkey_LDADD = -lpopt
 
 ppp_watch_SOURCES = ppp-watch.c shvar.c shvar.h
 ppp_watch_LDADD = /usr/lib/libglib.a
index 6bfda4bd437260baeda1ff5177ae17b87c53fc46..ab7e7534e3699d7f45bcb571c3e3dc0e122a3e96 100644 (file)
@@ -1,5 +1,19 @@
+/*
+ * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/sysmacros.h>
 int main(int argc, char **argv)
 {
     unsigned char twelve = 12;
-    int maj;
+    char *type;
+    int maj, min, ret = 0, fg = -1;
     struct stat sb;
-
+       
     fstat(0, &sb);
     maj = major(sb.st_rdev);
+    min = minor(sb.st_rdev);
     if (maj != 3 && (maj < 136 || maj > 143)) {
-       if (ioctl (0, TIOCLINUX, &twelve) < 0) {
-           printf ("serial\n");
-           return 1;
+       if ((fg = ioctl (0, TIOCLINUX, &twelve)) < 0) {
+           type = "serial";
+           ret = 1;
        } else {
-           printf ("vt\n");
-           return 0;
+#ifdef __powerpc__
+           int fd;
+           char buf[65536];
+           
+           fd = open("/proc/tty/drivers",O_RDONLY);
+           read(fd, buf, 65535);
+           if (strstr(buf,"vioconsole           /dev/tty")) {
+                   type = "vio";
+                   ret = 3;
+           } else {
+                   type = "vt";
+                   ret = 0;
+           }
+#else
+           type = "vt";
+           ret = 0;
+#endif
        }
     } else {
-       printf ("pty\n");
-       return 2;
+       type = "pty";
+       ret = 2;
+    }
+    if (argc > 1 && !strcmp(argv[1],"fg")) {
+           if (fg < 0 || fg != (min-1))
+                   return 1;
+           return 0;
+    } else {
+           printf("%s\n",type);
+           return ret;
     }
 } 
diff --git a/src/genhostid.c b/src/genhostid.c
new file mode 100644 (file)
index 0000000..a56cad8
--- /dev/null
@@ -0,0 +1,32 @@
+/* Copyright (C) 2003 Red Hat, Inc.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *         
+ */
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/stat.h>
+int
+main (void)
+{
+  struct stat st;
+  long int n;
+  if (stat ("/etc/hostid", &st) == 0 && S_ISREG (st.st_mode)
+      && st.st_size >= sizeof (n))
+    return 0;
+  int fd = open ("/dev/random", O_RDONLY);
+  if (fd == -1 || read (fd, &n, sizeof (n)) != sizeof (n))
+    {
+      srand48 ((long int) time (NULL) ^ (long int) getpid ());
+      n = lrand48 ();
+    }
+  return sethostid (n);
+}
index 8f1cc1f6572a264e913e5f8fef00679349ef6e41..132ea3ff8d37f00990c7f894476a5b7eb4e62604 100644 (file)
-
-#include <ctype.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-
-/* A very simple keygrabber. */
-
-struct termios tp;
-
-void reset_term(int x) {
-       tcsetattr(0,TCSANOW,&tp);
-       exit(x);
-}
-
-int main(int argc, char **argv) {
-       char foo[2];
-       char *list;
-       int alarmlen=0;
-       int tp_if,tp_of,tp_lf;
-       int x;
-       
-       if (argc>1) {
-               if (argv[1][0]=='-' && isdigit(argv[1][1])) {
-                       alarmlen = atoi(argv[1]+1);
-                       list = argv[2];
-               } else {
-                       list = argv[1];
-               }
-               for (x=0;list[x];x++) list[x]=toupper(list[x]);
-       }
-       foo[0]=foo[1]='\0';
-       signal(SIGTERM,reset_term);
-       alarm(alarmlen);
-       signal(SIGALRM,reset_term);
-       tcgetattr(0,&tp);
-       tp_if=tp.c_iflag;
-       tp_of=tp.c_oflag;
-       tp_lf=tp.c_lflag;
-       tp.c_iflag=0;
-       tp.c_oflag &= ~OPOST;
-       tp.c_lflag &= ~(ISIG | ICANON);
-       tcsetattr(0,TCSANOW,&tp);
-       tp.c_iflag=tp_if;
-       tp.c_oflag=tp_of;
-       tp.c_lflag=tp_lf;
-       while (1) {
-               read(0,foo,1);
-               foo[0]=toupper(foo[0]);
-               /* Die if we get a control-c or control-d */
-               if (foo[0]==3 || foo[0]==4) reset_term(1);
-               if ((!list) || strstr(list,foo)) {
-                       reset_term(0);
-               }
-       }
-}
+/*\r
+ * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.\r
+ *\r
+ * This software may be freely redistributed under the terms of the GNU\r
+ * public license.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
+ *\r
+ * getkey\r
+ *\r
+ * A very simple keygrabber.\r
+ *\r
+ */\r
+#include <ctype.h>\r
+#include <errno.h>\r
+#include <signal.h>\r
+#include <stdlib.h>\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <termios.h>\r
+#include <unistd.h>\r
+#include <sys/poll.h>\r
+#include "popt.h"\r
+\r
+struct termios tp;\r
+\r
+void reset_term(int x) {\r
+       tcsetattr(0,TCSANOW,&tp);\r
+       exit(x);\r
+}\r
+\r
+int main(int argc, char **argv) {\r
+       char foo[2];\r
+       char list[100]; /* should be enough */\r
+       char *waitmessage = NULL;\r
+       char *waitprint, *waitsprint;\r
+       const char *fooptr;\r
+       int waitseconds=0;\r
+       int alarmlen=0;\r
+       int ignore_control=0;\r
+       int tp_if,tp_of,tp_lf;\r
+       int x, r;\r
+       struct pollfd ufds; /* only one, no need for an array... */\r
+        poptContext context;\r
+       struct poptOption options[] = {\r
+            { "wait", 'c', POPT_ARG_INT, &waitseconds, 0, "Number of seconds to wait for keypress", NULL },\r
+           /*            { "message", 'm', POPT_ARG_STRING, &waitmessage, 0, "Message to print out while waiting for string", "NOTE: argument must have a \"%d\" in it so the number of seconds\nleft until getkey times out can be printed" },*/\r
+            { "message", 'm', POPT_ARG_STRING, &waitmessage, 0, "Message to print out while waiting for string\nNOTE: message must have a \"%d\" in it, to hold the number of seconds left to wait", NULL },\r
+            { "ignore-control-chars", 'i', POPT_ARG_NONE, &ignore_control, 0, "Ignore Control-C and Control-D", NULL },\r
+            POPT_AUTOHELP\r
+            POPT_TABLEEND\r
+        };\r
+\r
+       strcpy(list, "");\r
+        context = poptGetContext("getkey", argc, argv, options, \r
+                                POPT_CONTEXT_POSIXMEHARDER);\r
+        poptSetOtherOptionHelp(context, "[keys]");\r
+\r
+        r = poptGetNextOpt(context);\r
+        if (r < -1) {\r
+            fprintf(stderr, "%s: %s\n", \r
+                   poptBadOption(context, POPT_BADOPTION_NOALIAS),\r
+                   poptStrerror(r));\r
+\r
+            return -1;\r
+        }\r
+        fooptr = poptGetArg(context);\r
+       if (fooptr != NULL) {\r
+           strncpy(list, fooptr, sizeof(list) - 1);\r
+           list[99] = '\0';\r
+           for (x=0;list[x];x++) list[x]=toupper(list[x]);\r
+       }\r
+       if (waitseconds) {\r
+           alarmlen = waitseconds;\r
+       }\r
+       foo[0]=foo[1]='\0';\r
+\r
+       signal(SIGTERM,reset_term);\r
+       alarm(alarmlen);\r
+       signal(SIGALRM,reset_term);\r
+\r
+       tcgetattr(0,&tp);\r
+       tp_if=tp.c_iflag;\r
+       tp_of=tp.c_oflag;\r
+       tp_lf=tp.c_lflag;\r
+       tp.c_iflag=0;\r
+       tp.c_oflag &= ~OPOST;\r
+       tp.c_lflag &= ~(ISIG | ICANON);\r
+       tcsetattr(0,TCSANOW,&tp);\r
+       tp.c_iflag=tp_if;\r
+       tp.c_oflag=tp_of;\r
+       tp.c_lflag=tp_lf;\r
+\r
+       ufds.events = POLLIN;\r
+       ufds.fd = 0;\r
+\r
+       if (waitseconds && waitmessage) {\r
+           waitprint = alloca (strlen(waitmessage)+15); /* long enough */\r
+           waitprint[0] = '\r';\r
+           waitsprint = waitprint + 1;\r
+       }\r
+\r
+       while (1) {\r
+           if (waitseconds && waitmessage) {\r
+               sprintf (waitsprint, waitmessage, waitseconds);\r
+               write (1, waitprint, strlen(waitprint));\r
+           }\r
+           r = poll(&ufds, 1, alarmlen ? 1000 : -1);\r
+           if (r == 0) {\r
+               /* we have waited a whole second with no keystroke... */\r
+               waitseconds--;\r
+           }\r
+           if (r > 0) {\r
+               read(0,foo,1);\r
+               foo[0]=toupper(foo[0]);\r
+               /* Die if we get a control-c or control-d */\r
+                if (ignore_control == 0) {\r
+                   if (foo[0]==3 || foo[0]==4) reset_term(1);\r
+                }\r
+               /* Don't let a null character be interpreted as a match\r
+                  by strstr */\r
+               if (foo[0] != 0) {\r
+                   if (strcmp(list, "") == 0 || strstr(list,foo)) {\r
+                     reset_term(0);\r
+                   }\r
+               }\r
+           }\r
+       }\r
+}\r
index 5905e16c09b2f7e39b33f008b05c95c6ec600544..4320d9bcbf0c9db544b491f4869d6f5a92c326ee 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
 
 #include <ctype.h>
 #include <errno.h>
@@ -272,8 +283,9 @@ int logEvent(char *cmd, int eventtype,char *string) {
     
     if (cmd) {
        logentry.cmd = strdup(basename(cmd));
-       if ((logentry.cmd[0] =='K' || logentry.cmd[0] == 'S') && ( 30 <= logentry.cmd[1] <= 39 )
-           && ( 30 <= logentry.cmd[2] <= 39 ) )
+       if ((logentry.cmd[0] =='K' || logentry.cmd[0] == 'S') &&
+           ( logentry.cmd[1] >= '0' && logentry.cmd[1] <= '9' ) &&
+           ( logentry.cmd[2] >= '0' && logentry.cmd[2] <= '9' ) )
          logentry.cmd+=3;
     } else
       logentry.cmd = strdup(_("(none)"));
@@ -298,8 +310,9 @@ int logString(char *cmd, char *string) {
     
     if (cmd) {
        logentry.cmd = strdup(basename(cmd));
-       if ((logentry.cmd[0] =='K' || logentry.cmd[0] == 'S') && ( 30 <= logentry.cmd[1] <= 39 )
-           && ( 30 <= logentry.cmd[2] <= 39 ) )
+       if ((logentry.cmd[0] =='K' || logentry.cmd[0] == 'S') && 
+           ( logentry.cmd[1] >= '0' && logentry.cmd[1] <= 0x39 ) &&
+           ( logentry.cmd[2] >= '0' && logentry.cmd[2] <= 0x39 ) )
          logentry.cmd+=3;
     } else
       logentry.cmd = strdup(_(""));
@@ -413,6 +426,11 @@ int processArgs(int argc, char **argv, int silent) {
         fprintf(stderr, _("--name requires one of --event or --string\n"));
        return -1;
     }
+    if (cmdevent && cmd) {
+           if (!silent)
+             fprintf(stderr, _("--cmd and --run are incompatible with --event\n"));
+           return -1;
+    }
     if (conffile) {
        readConfiguration(conffile);
     } else {
index 9b26a79a1bc60f743f85dd1c89d1734a79b66bf2..468a037cc16fb7b39376e17157c4cc58d0d195e4 100644 (file)
-/* $Id$ */
+/*
+ * Copyright (c) 1997-2003 Red Hat, Inc. All rights reserved.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Authors:
+ *   Erik Troan <ewt@redhat.com>
+ *   Preston Brown <pbrown@redhat.com>
+ */
+     
 
+#include <ctype.h>
 #include <popt.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
-#include <libintl.h>
-#include <locale.h>
 
-typedef unsigned int int32;
+/*!
+  \def IPBITS
+  \brief the number of bits in an IP address.
+*/
+#define IPBITS (sizeof(u_int32_t) * 8)
+/*!
+  \def IPBYTES
+  \brief the number of bytes in an IP address.
+*/
+#define IPBYTES (sizeof(u_int32_t))
 
-int main(int argc, char ** argv) {
-    int showBroadcast = 0, showNetwork = 0, showHostname = 0, showNetmask = 0, showPrefix = 0;
+
+/*!
+  \file ipcalc.c
+  \brief provides utilities for manipulating IP addresses.
+
+  ipcalc provides utilities and a front-end command line interface for
+  manipulating IP addresses, and calculating various aspects of an ip
+  address/netmask/network address/prefix/etc.
+
+  Functionality can be accessed from other languages from the library
+  interface, documented here.  To use ipcalc from the shell, read the
+  ipcalc(1) manual page.
+
+  When passing parameters to the various functions, take note of whether they
+  take host byte order or network byte order.  Most take host byte order, and
+  return host byte order, but there are some exceptions.
+  
+*/
+
+/*!
+  \fn unsigned long int prefix2mask(int bits)
+  \brief creates a netmask from a specified number of bits
+  
+  This function converts a prefix length to a netmask.  As CIDR (classless
+  internet domain internet domain routing) has taken off, more an more IP
+  addresses are being specified in the format address/prefix
+  (i.e. 192.168.2.3/24, with a corresponding netmask 255.255.255.0).  If you
+  need to see what netmask corresponds to the prefix part of the address, this
+  is the function.  See also \ref mask2prefix.
+  
+  \param prefix is the number of bits to create a mask for.
+  \return a network mask, in network byte order.
+*/
+unsigned long int prefix2mask(int prefix) {
+    return htonl(~((1 << (32 - prefix)) - 1));
+}
+
+/*!
+  \fn int mask2prefix(unsigned long int mask)
+  \brief calculates the number of bits masked off by a netmask.
+
+  This function calculates the significant bits in an IP address as specified by
+  a netmask.  See also \ref prefix2mask.
+
+  \param mask is the netmask, specified as an unsigned long integer in network byte order.
+  \return the number of significant bits.  */
+int mask2prefix(unsigned long int mask)
+{
+    int i;
+    int count = IPBITS;
+       
+    for (i = 0; i < IPBITS; i++) {
+       if (!(ntohl(mask) & ((2 << i) - 1)))
+           count--;
+    }
+
+    return count;
+}
+
+/*!
+  \fn unsigned long int default_netmask(unsigned long int addr)
+
+  \brief returns the default (canonical) netmask associated with specified IP
+  address.
+
+  When the Internet was originally set up, various ranges of IP addresses were
+  segmented into three network classes: A, B, and C.  This function will return
+  a netmask that is associated with the IP address specified defining where it
+  falls in the predefined classes.
+
+  \param addr an IP address in network byte order.
+  \return a netmask in network byte order.  */
+unsigned long int default_netmask(unsigned long int addr)
+{
+    if (((ntohl(addr) & 0xFF000000) >> 24) <= 127)
+       return htonl(0xFF000000);
+    else if (((ntohl(addr) & 0xFF000000) >> 24) <= 191)
+       return htonl(0xFFFF0000);
+    else
+       return htonl(0xFFFFFF00);
+}
+
+/*!
+  \fn unsigned long int calc_broadcast(unsigned long int addr, int prefix)
+
+  \brief calculate broadcast address given an IP address and a prefix length.
+
+  \param addr an IP address in network byte order.
+  \param prefix a prefix length.
+  
+  \return the calculated broadcast address for the network, in network byte
+  order.
+*/
+unsigned long int calc_broadcast(unsigned long int addr,
+                                int prefix)
+{  
+    return (addr & prefix2mask(prefix)) | ~prefix2mask(prefix);
+}
+
+/*!
+  \fn unsigned long int calc_network(unsigned long int addr, int prefix)
+  \brief calculates the network address for a specified address and prefix.
+
+  \param addr an IP address, in network byte order
+  \param prefix the network prefix
+  \return the base address of the network that addr is associated with, in
+  network byte order.
+*/
+unsigned long int calc_network(unsigned long int addr, int prefix)
+{
+    return (addr & prefix2mask(prefix));
+}
+
+/*!
+  \fn const char *get_hostname(unsigned long int addr)
+  \brief returns the hostname associated with the specified IP address
+
+  \param addr an IP address to find a hostname for, in network byte order
+
+  \return a hostname, or NULL if one cannot be determined.  Hostname is stored
+  in a static buffer that may disappear at any time, the caller should copy the
+  data if it needs permanent storage.
+*/
+const char *get_hostname(unsigned long int addr)
+{
+    struct hostent * hostinfo;
+    int x;
+    
+    hostinfo = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
+    if (!hostinfo)
+       return NULL;
+
+    for (x=0; hostinfo->h_name[x]; x++) {
+       hostinfo->h_name[x] = tolower(hostinfo->h_name[x]);
+    }
+    return hostinfo->h_name;
+}
+
+/*!
+  \fn main(int argc, const char **argv)
+  \brief wrapper program for ipcalc functions.
+  
+  This is a wrapper program for the functions that the ipcalc library provides.
+  It can be used from shell scripts or directly from the command line.
+  
+  For more information, please see the ipcalc(1) man page.
+*/
+int main(int argc, const char **argv) {
+    int showBroadcast = 0, showPrefix = 0, showNetwork = 0;
+    int showHostname = 0, showNetmask = 0;
     int beSilent = 0;
     int rc;
     poptContext optCon;
-    char * ipStr, * netmaskStr, * chptr;
-    int32 ip, netmask, network, broadcast;
-    struct hostent * hostinfo;
+    char *ipStr, *prefixStr, *netmaskStr, *hostName, *chptr;
+    struct in_addr ip, netmask, network, broadcast;
+    int prefix = 0;
     char errBuf[250];
     struct poptOption optionsTable[] = {
-           { "broadcast", '\0', 0, &showBroadcast, 0 },
-           { "hostname", '\0', 0, &showHostname, 0 },
-           { "netmask", '\0', 0, &showNetmask, 0 },
-           { "network", '\0', 0, &showNetwork, 0 },
-           { "silent", '\0', 0, &beSilent, 0 },
-           { "prefix", '\0', 0, &showPrefix, 0 },
-           { NULL, '\0', 0, 0, 0 },
+           { "broadcast", 'b', 0, &showBroadcast, 0,
+               "Display calculated broadcast address", },
+           { "hostname", 'h', 0, &showHostname, 0,
+               "Show hostname determined via DNS" },
+           { "netmask", 'm', 0, &showNetmask, 0,
+               "Display default netmask for IP (class A, B, or C)" },
+           { "network", 'n', 0, &showNetwork, 0,
+               "Display network address", },
+           { "prefix", 'p', 0, &showPrefix, 0,
+             "Display network prefix", },
+           { "silent", 's', 0, &beSilent, 0,
+               "Don't ever display error messages " },
+           POPT_AUTOHELP
+           { NULL, '\0', 0, 0, 0, NULL, NULL }
     };
 
-    optCon = poptGetContext("ipcalc", argc, argv, optionsTable,0);
+    optCon = poptGetContext("ipcalc", argc, argv, optionsTable, 0);
     poptReadDefaultConfig(optCon, 1);
 
     if ((rc = poptGetNextOpt(optCon)) < -1) {
-       if (!beSilent)
+       if (!beSilent) {
            fprintf(stderr, "ipcalc: bad argument %s: %s\n", 
                    poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
                    poptStrerror(rc));
+           poptPrintHelp(optCon, stderr, 0);
+       }
        return 1;
     }
 
-    if (!(ipStr = poptGetArg(optCon))) {
-       if (!beSilent)
+    if (!(ipStr = (char *) poptGetArg(optCon))) {
+       if (!beSilent) {
            fprintf(stderr, "ipcalc: ip address expected\n");
+           poptPrintHelp(optCon, stderr, 0);
+       }
        return 1;
     }
 
-    if (showBroadcast || showNetwork) {
-       if (!(netmaskStr = poptGetArg(optCon))) {
+    if (strchr(ipStr,'/') != NULL) {
+       prefixStr = strchr(ipStr, '/') + 1;
+       prefixStr--;
+       *prefixStr = '\0';  /* fix up ipStr */
+       prefixStr++;
+    } else
+       prefixStr = NULL;
+    
+    if (prefixStr != NULL) {
+       prefix = atoi(prefixStr);
+       if (prefix == 0) {
            if (!beSilent)
-               fprintf(stderr, "ipcalc: netmask expected\n");
+               fprintf(stderr, "ipcalc: bad prefix: %s\n",
+                       prefixStr);
            return 1;
        }
-
-       if (!inet_aton(netmaskStr, (struct in_addr *) &netmask)) {
-           if (!beSilent)
-               fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr);
+    }
+       
+    if (showBroadcast || showNetwork || showPrefix) {
+       if (!(netmaskStr = (char *) poptGetArg(optCon)) &&
+           (prefix == 0)) {
+           if (!beSilent) {
+               fprintf(stderr, "ipcalc: netmask or prefix expected\n");
+               poptPrintHelp(optCon, stderr, 0);
+           }
+           return 1;
+       } else if (netmaskStr && prefix != 0) {
+           if (!beSilent) {
+                   fprintf(stderr, "ipcalc: both netmask and prefix specified\n");
+                   poptPrintHelp(optCon, stderr, 0);
+           }
            return 1;
+       } else if (netmaskStr) {
+           if (!inet_aton(netmaskStr, &netmask)) {
+               if (!beSilent)
+                   fprintf(stderr, "ipcalc: bad netmask: %s\n",
+                           netmaskStr);
+               return 1;
+           }
+           prefix = mask2prefix(netmask.s_addr);
        }
     }
 
-    if ((chptr = poptGetArg(optCon))) {
-       if (!beSilent)
+    if ((chptr = (char *) poptGetArg(optCon))) {
+       if (!beSilent) {
            fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
+           poptPrintHelp(optCon, stderr, 0);
+       }
        return 1;
     }
 
-    poptFreeContext(optCon);
+    /* Handle CIDR entries such as 172/8 */
+    if (prefix) {
+       char *tmp = ipStr;
+       int i;
+           
+       for(i=3; i> 0; i--) {
+               tmp = strchr(tmp,'.');
+               if (!tmp) 
+                       break;
+               else
+                       tmp++;
+       }
+       tmp = NULL;
+       for (; i>0; i--) {
+          tmp = malloc(strlen(ipStr) + 3);
+          sprintf(tmp,"%s.0",ipStr);
+          ipStr = tmp;
+       }
+    }
 
     if (!inet_aton(ipStr, (struct in_addr *) &ip)) {
        if (!beSilent)
@@ -75,57 +308,54 @@ int main(int argc, char ** argv) {
        return 1;
     }
 
-    if (showPrefix)
-           showNetmask = 1;
+    
+    if (!(showNetmask|showPrefix|showBroadcast|showNetwork|showHostname)) {
+           poptPrintHelp(optCon, stderr, 0);
+           return 1;
+    }
+
+    poptFreeContext(optCon);
+
+    /* we know what we want to display now, so display it. */
 
     if (showNetmask) {
-       if (showPrefix) {
-               if (((ntohl(ip) & 0xFF000000) >> 24) <= 127)
-                   chptr = "8";
-               else if (((ntohl(ip) & 0xFF000000) >> 24) <= 191)
-                   chptr = "16";
-               else 
-                   chptr = "24";
-
-               printf("PREFIX=%s\n", chptr);
-               
+       if (prefix) {
+           netmask.s_addr = prefix2mask(prefix);
        } else {
-                if (((ntohl(ip) & 0xFF000000) >> 24) <= 127)
-                    chptr = "255.0.0.0";
-                else if (((ntohl(ip) & 0xFF000000) >> 24) <= 191)
-                    chptr = "255.255.0.0";
-                else
-                    chptr = "255.255.255.0";
-
-               printf("NETMASK=%s\n", chptr);
+           netmask.s_addr = default_netmask(ip.s_addr);
+           prefix = mask2prefix(netmask.s_addr);
        }
 
+       printf("NETMASK=%s\n", inet_ntoa(netmask));
     }
 
+    if (showPrefix) {
+       if (!prefix)
+           prefix = mask2prefix(ip.s_addr);
+       printf("PREFIX=%d\n", prefix);
+    }
+           
     if (showBroadcast) {
-       broadcast = (ip & netmask) | ~netmask;
-       printf("BROADCAST=%s\n", inet_ntoa(*((struct in_addr *) &broadcast)));
+       broadcast.s_addr = calc_broadcast(ip.s_addr, prefix);
+       printf("BROADCAST=%s\n", inet_ntoa(broadcast));
     }
 
     if (showNetwork) {
-       network = ip & netmask;
-       printf("NETWORK=%s\n", inet_ntoa(*((struct in_addr *) &network)));
+       network.s_addr = calc_network(ip.s_addr, prefix);
+       printf("NETWORK=%s\n", inet_ntoa(network));
     }
-
-    if (showHostname) {
-       hostinfo = gethostbyaddr((char *) &ip, sizeof(ip), AF_INET);
-       if (!hostinfo) {
+    
+    if (showHostname) {        
+       if ((hostName = (char *) get_hostname(ip.s_addr)) == NULL) {
            if (!beSilent) {
                sprintf(errBuf, "ipcalc: cannot find hostname for %s", ipStr);
                herror(errBuf);
            }
-
            return 1;
        }
-
-       printf("HOSTNAME=%s\n", hostinfo->h_name);
+       
+       printf("HOSTNAME=%s\n", hostName);
     }
        
-
     return 0;
 }
index cceedc3a8576cc1315529d1c2c9ffc2a2727f934..6eb220c60736602cd027b19d6e4f0d02807878cb 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * Copyright (c) 1997-2002 Red Hat, Inc. All rights reserved.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -26,7 +37,7 @@ int main(int argc, char ** argv) {
     }
 
     if (argc > 1) {
-         if (strcmp(argv[1], "-r") == 0) {
+         if (argc == 2 && strcmp(argv[1], "-r") == 0) {
                  action = DEL;
          } else {
                  usage();
index ed2e8aea57b61082c4201614a7a0d7abcd663397..8f7d642ad5c350de79d5938adaea888754c21ebf 100644 (file)
@@ -6,7 +6,7 @@
  * one argument: the logical name of the device to bring up.  Does not
  * detach until the interface is up or has permanently failed to come up.
  *
- * Copyright 1999-2001 Red Hat, Inc.
+ * Copyright 1999-2003 Red Hat, Inc.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
@@ -162,6 +162,9 @@ detach(char *device) {
      * of the pppd process to its parent (i.e., it reads nothing). */
     close (pipeArray[0]);
 
+    /* Don't leak this into programs we call. */
+    fcntl(pipeArray[1], F_SETFD, FD_CLOEXEC);
+
     /* Redirect stdio to /dev/null. */
     fd = open("/dev/null", O_RDONLY);
     dup2(fd, STDIN_FILENO);
@@ -433,7 +436,7 @@ interfaceIsUp(char *device) {
     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
 
-    /* We return TRUE if the ioctl succeeded and the interface is UP. */
+    /* We return TRUE iff the ioctl succeeded and the interface is UP. */
     if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
         retcode = FALSE;
     } else if (ifr.ifr_flags & IFF_UP) {
@@ -490,7 +493,7 @@ main(int argc, char **argv) {
     int maxfail = 0;           // MAXFAIL Patch <ssharkey@linux-no-limits.com>
 
     if (argc < 2) {
-       fprintf (stderr, "usage: ppp-watch [ifcfg-]<logical-name> [boot]\n");
+       fprintf (stderr, "usage: ppp-watch <interface-name> [boot]\n");
        exit(30);
     }
 
@@ -606,7 +609,7 @@ main(int argc, char **argv) {
            dying = TRUE;
 
            /* Get the pid of our child pppd. */
-           pppLogicalToPhysical(&pppdPid, real_device, NULL);
+           pppLogicalToPhysical(&pppdPid, device, NULL);
 
            /* We don't know what our child pid is.  This is very confusing. */
            if (!pppdPid) {
@@ -636,7 +639,7 @@ main(int argc, char **argv) {
            ifcfg = shvarfilesGet(device);
 
            /* Get the PID of our child pppd. */
-           pppLogicalToPhysical(&pppdPid, real_device, NULL);
+           pppLogicalToPhysical(&pppdPid, device, NULL);
            kill(pppdPid, SIGTERM);
 
            /* We'll redial when the SIGCHLD arrives, even if PERSIST is
@@ -654,7 +657,7 @@ main(int argc, char **argv) {
        if (theSigio) {
            theSigio = 0;
 
-           pppLogicalToPhysical(NULL, real_device, &physicalDevice);
+           pppLogicalToPhysical(NULL, device, &physicalDevice);
            if (physicalDevice) {
                if (interfaceIsUp(physicalDevice)) {
                    /* The interface is up, so report a success to a parent if
@@ -711,10 +714,11 @@ main(int argc, char **argv) {
                break;
            }
 
-            /* We default to retrying the connect phase for backward
-            * compatibility, unless RETRYCONNECT is false. */
+            /* PGB 08/20/02: We no longer retry connecting MAXFAIL
+              times on a failed connect script unless RETRYCONNECT is
+              true. */
            if ((WEXITSTATUS(status) == 8) &&
-               !svTrueValue(ifcfg, "RETRYCONNECT", TRUE)) {
+               !svTrueValue(ifcfg, "RETRYCONNECT", FALSE)) {
                 failureExit(WEXITSTATUS(status));
             }
 
@@ -740,6 +744,7 @@ main(int argc, char **argv) {
                sleep(timeout);
                sigprocmask(SIG_BLOCK, &blockedsigs, NULL);
                if (!theSigterm &&
+                   !theSigint &&
                    !theSighup &&
                    !theSigio &&
                    !theSigchld &&
@@ -765,13 +770,8 @@ main(int argc, char **argv) {
                }
                if ( maxfail != 0 ) {
                    dialCount++;
-                   if ( dialCount < maxfail ) {
-                       fork_exec(FALSE, IFUP_PPP, "daemon", device, boot);
-                   } else {
+                   if ( dialCount >= maxfail )
                        failureExit(WEXITSTATUS(status));
-                   }   
-               } else {        
-                   fork_exec(FALSE, IFUP_PPP, "daemon", device, boot);
                } 
            } else {
                failureExit(WEXITSTATUS(status));
index 4ff805ef9bfe65f64a812d9e1017c397df129cf6..5a1571e224a8e52e00c872941ba19562228754c2 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
 
 #include <errno.h>
 #include <fcntl.h>
@@ -137,7 +148,7 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
     char **cmdargs=NULL;
     char **tmpargs=NULL;
     int cmdargc;
-    char *procpath;
+    char *procpath = NULL;
     
     if (reexec) {
        procpath=malloc(20*sizeof(char));
@@ -266,8 +277,9 @@ int runCommand(char *cmd, int reexec, int quiet, int debug) {
       cmdname = basename(args[0]);
     else
       cmdname = basename(args[1]);
-    if ((cmdname[0] =='K' || cmdname[0] == 'S') && ( '0' <= cmdname[1] <= '9' )
-       && ( '0' <= cmdname[2] <= '9' ) )
+    if ((cmdname[0] =='K' || cmdname[0] == 'S') && 
+        ( cmdname[1] >= '0' && cmdname[1] <= '9' ) &&
+        ( cmdname[2] >= '0' && cmdname[2] <= '9' ) )
       cmdname+=3;
     if (!reexec) {
        pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet);
index 7eeda3acb54a5692382f1bb12a5dd69015d3c002..2574d725a074fa374b1e57ad34e049fec5c750ce 100644 (file)
@@ -1,3 +1,15 @@
+/*
+ * Copyright (c) 1997-2003 Red Hat, Inc. All rights reserved.
+ *
+ * This software may be freely redistributed under the terms of the GNU
+ * public license.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
 #include <alloca.h>
 #include <ctype.h>
 #include <errno.h>
@@ -7,6 +19,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <limits.h>
 
 /* This will be running setuid root, so be careful! */
 static char * safeEnviron[] = {
@@ -58,6 +71,7 @@ testSafe(char *ifaceConfig) {
 
 static int
 userCtl(char *file) {
+    char *buf;
     char *contents = NULL;
     char *chptr = NULL;
     char *next = NULL;
@@ -66,7 +80,7 @@ userCtl(char *file) {
 
     size = testSafe(file);
 
-    contents = malloc(size + 2);
+    buf = contents = malloc(size + 2);
 
     if ((fd = open(file, O_RDONLY)) == -1) {
        fprintf(stderr, "failed to open %s: %s\n", file, strerror(errno));
@@ -93,8 +107,11 @@ userCtl(char *file) {
 
        if (!strncmp(contents, "USERCTL=", 8)) {
            contents += 8;
-           if (contents[0] == '"' &&
-               contents[strlen(contents) - 1] == '"') {
+           if ((contents[0] == '"' &&
+                contents[strlen(contents) - 1] == '"') ||
+               (contents[0] == '\'' &&
+                contents[strlen(contents) - 1] == '\''))
+               {
                contents++;
                contents[strlen(contents) - 1] = '\0';
            }
@@ -110,7 +127,7 @@ userCtl(char *file) {
        contents = next;
     }
 
-    free(contents);
+    free(buf);
 
     return retval;
 }
@@ -119,24 +136,24 @@ int
 main(int argc, char ** argv) {
     char * ifaceConfig;
     char * chptr;
-    char * cmd;
+    char * cmd = NULL;
     int report = 0;
     char tmp;
 
     if (argc != 3) usage();
 
     if (!strcmp(argv[2], "up")) {
-       cmd = "/sbin/ifup";
+       cmd = "./ifup";
     } else if (!strcmp(argv[2], "down")) {
-       cmd = "/sbin/ifdown";
+       cmd = "./ifdown";
     } else if (!strcmp(argv[2], "report")) {
        report = 1;
     } else {
        usage();
     }
 
-    if (chdir("/etc/sysconfig/interfaces")) {
-       fprintf(stderr, "error switching to /etc/sysconfig/interfaces: "
+    if (chdir("/etc/sysconfig/network-scripts")) {
+       fprintf(stderr, "error switching to /etc/sysconfig/network-scripts: "
                "%s\n", strerror(errno));
        exit(1);
     }
@@ -152,8 +169,15 @@ main(int argc, char ** argv) {
     /* automatically prepend "ifcfg-" if it is not specified */
     if (strncmp(ifaceConfig, "ifcfg-", 6)) {
        char *temp;
+        size_t len = strlen(ifaceConfig);
+
+       /* Make sure a wise guys hasn't tried an integer wrap-around or
+          stack overflow attack. There's no way it could refer to anything 
+          bigger than the largest filename, so cut 'em off there. */
+        if (len > PATH_MAX)
+               exit(1);
 
-       temp = (char *) alloca(strlen(ifaceConfig) + 7);
+       temp = (char *) alloca(len + 7);
        strcpy(temp, "ifcfg-");
        /* strcat is safe because we got the length from strlen */
        strcat(temp, ifaceConfig);
This page took 0.312611 seconds and 4 git commands to generate.