]> git.pld-linux.org Git - projects/rc-scripts.git/commitdiff
Update helpers from Fedora initscripts.
authorArkadiusz Miśkiewicz <arekm@maven.pl>
Tue, 18 May 2004 22:58:54 +0000 (22:58 +0000)
committerArkadiusz Miśkiewicz <arekm@maven.pl>
Tue, 18 May 2004 22:58:54 +0000 (22:58 +0000)
svn-id: @4086

src/doexec.c
src/initlog.c
src/ipcalc.c
src/minilogd.c
src/process.c
src/testd.c
src/usernetctl.c
src/usleep.c

index 319f6c2948ee8c784484758b9a58666a6d2b7ec2..1b045326366af8f49534b49c6311c5f984fc8060 100644 (file)
@@ -1,3 +1,15 @@
+/*
+ * Copyright (c) 1997-1999 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 <unistd.h>
 
 int main(int argc, char ** argv) {
index 4320d9bcbf0c9db544b491f4869d6f5a92c326ee..13a376ec797a5dbc1dc10ef3beccf917d4fef58e 100644 (file)
@@ -63,6 +63,7 @@ void readConfiguration(char *fname) {
     data=malloc(sbuf.st_size+1);
     if (read(fd,data,sbuf.st_size)!=sbuf.st_size) {
            close(fd);
+           free(data);
            return;
     }
     close(fd);
index 468a037cc16fb7b39376e17157c4cc58d0d195e4..f133412ccf4670daf6a5bea8e4ded39dd7e0a3ad 100644 (file)
@@ -56,7 +56,7 @@
 */
 
 /*!
-  \fn unsigned long int prefix2mask(int bits)
+  \fn u_int32_t 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
   \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) {
+u_int32_t prefix2mask(int prefix) {
     return htonl(~((1 << (32 - prefix)) - 1));
 }
 
 /*!
-  \fn int mask2prefix(unsigned long int mask)
+  \fn int mask2prefix(u_int32_t 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.
+  \param mask is the netmask, specified as an u_int32_teger in network byte order.
   \return the number of significant bits.  */
-int mask2prefix(unsigned long int mask)
+int mask2prefix(u_int32_t mask)
 {
     int i;
     int count = IPBITS;
@@ -96,7 +96,7 @@ int mask2prefix(unsigned long int mask)
 }
 
 /*!
-  \fn unsigned long int default_netmask(unsigned long int addr)
+  \fn u_int32_t default_netmask(u_int32_t addr)
 
   \brief returns the default (canonical) netmask associated with specified IP
   address.
@@ -108,7 +108,7 @@ int mask2prefix(unsigned long int mask)
 
   \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)
+u_int32_t default_netmask(u_int32_t addr)
 {
     if (((ntohl(addr) & 0xFF000000) >> 24) <= 127)
        return htonl(0xFF000000);
@@ -119,7 +119,7 @@ unsigned long int default_netmask(unsigned long int addr)
 }
 
 /*!
-  \fn unsigned long int calc_broadcast(unsigned long int addr, int prefix)
+  \fn u_int32_t calc_broadcast(u_int32_t addr, int prefix)
 
   \brief calculate broadcast address given an IP address and a prefix length.
 
@@ -129,14 +129,14 @@ unsigned long int default_netmask(unsigned long int addr)
   \return the calculated broadcast address for the network, in network byte
   order.
 */
-unsigned long int calc_broadcast(unsigned long int addr,
+u_int32_t calc_broadcast(u_int32_t addr,
                                 int prefix)
 {  
     return (addr & prefix2mask(prefix)) | ~prefix2mask(prefix);
 }
 
 /*!
-  \fn unsigned long int calc_network(unsigned long int addr, int prefix)
+  \fn u_int32_t calc_network(u_int32_t addr, int prefix)
   \brief calculates the network address for a specified address and prefix.
 
   \param addr an IP address, in network byte order
@@ -144,13 +144,13 @@ unsigned long int calc_broadcast(unsigned long int addr,
   \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)
+u_int32_t calc_network(u_int32_t addr, int prefix)
 {
     return (addr & prefix2mask(prefix));
 }
 
 /*!
-  \fn const char *get_hostname(unsigned long int addr)
+  \fn const char *get_hostname(u_int32_t 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
@@ -159,7 +159,7 @@ unsigned long int calc_network(unsigned long int addr, int prefix)
   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)
+const char *get_hostname(u_int32_t addr)
 {
     struct hostent * hostinfo;
     int x;
index 90bce3f8960429c7f094de509fe069d8eda78cc1..b7cbc8c095348fec81a4299c6954a1e150a66f0d 100644 (file)
@@ -1,9 +1,17 @@
-
 /* minilogd.c
  * 
  * A pale imitation of syslogd. Most notably, doesn't write anything
  * anywhere except possibly back to syslogd.
- * 
+ *
+ * Copyright (c) 1999-2001 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>
@@ -147,6 +155,7 @@ int main(int argc, char **argv) {
    dup2(sock,0);
    dup2(sock,1);
    dup2(sock,2);
+   close(sock);
        
    bzero(&addr, sizeof(addr));
    addr.sun_family = AF_LOCAL;
index 5a1571e224a8e52e00c872941ba19562228754c2..c17680bfa3f1cda71b18227d08b54e1a16888063 100644 (file)
@@ -33,7 +33,7 @@ extern regex_t **regList;
 int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
    /* Fork command 'cmd', returning pid, and optionally pointer
     * to open file descriptor fd */
-    int fdout, fderr, fdcmd, pid;
+    int fdout=-1, fderr=-1, fdcmd=-1, pid;
     int outpipe[2], errpipe[2], fdpipe[2];
     int ourpid;
     
@@ -63,6 +63,8 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
     } else {
         fdcmd = open("/dev/null",O_WRONLY);
     }
+    if (fdout==-1 || fderr==-1 || fdcmd==-1)
+       return -1;
     ourpid = getpid();
     if ((pid = fork())==-1) {
        perror("fork");
@@ -139,7 +141,6 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
 
 int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, int debug) {
     struct pollfd *pfds;
-    char *buf=malloc(8192*sizeof(char));
     char *outbuf=NULL;
     char *tmpstr=NULL;
     int x,y,rc=-1;
@@ -165,6 +166,9 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
        usleep(500);
        if (((x=poll(pfds,numfds,500))==-1)&&errno!=EINTR) {
          perror("poll");
+          free(pfds);
+          if (procpath)
+             free(procpath);
          return -1;
        }
        if (!reexec) {
@@ -176,16 +180,22 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
           if (stat(procpath,&sbuf)&&!stat("/proc/cpuinfo",&sbuf))
             done=1;
        }
+       if (x<0)
+          continue;
        y=0;
        while (y<numfds) {
          if ( x && ((pfds[y].revents & (POLLIN | POLLPRI)) )) {
             int bytesread = 0;
             
             do {
-               buf=calloc(8192,sizeof(char));
+               char *buf=calloc(8192,sizeof(char));
                bytesread = read(pfds[y].fd,buf,8192);
                if (bytesread==-1) {
                   perror("read");
+                   free(pfds);
+                   if (procpath)
+                      free(procpath);
+                   free(buf);
                   return -1;
                }
                if (bytesread) {
@@ -245,6 +255,7 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
                      }
                  }
                }
+                free(buf);
             } while ( bytesread==8192 );
          }
          y++;
@@ -256,8 +267,18 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
       if (quiet && output) {
            write(1,outbuf,strlen(outbuf));
       }
+      free(pfds);
+      if (procpath)
+         free(procpath);
+      if(outbuf)
+         free(outbuf);
       return (rc);
    }
+   free(pfds);
+   if (procpath)
+      free(procpath);
+   if(outbuf)
+      free(outbuf);
    return 0;
 }
 
@@ -283,10 +304,14 @@ int runCommand(char *cmd, int reexec, int quiet, int debug) {
       cmdname+=3;
     if (!reexec) {
        pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet);
+       if (pid == -1)
+          return -1;
        x=monitor(cmdname,pid,2,fds,reexec,quiet,debug);
     } else {
        setenv("IN_INITLOG","yes",1);
        pid=forkCommand(args,NULL,NULL,&fds[0],quiet);
+       if (pid == -1)
+          return -1;
        unsetenv("IN_INITLOG");
        x=monitor(cmdname,pid,1,&fds[0],reexec,quiet,debug);
     }
index 7deb00eb953bfec16785ae7e8f81f4bf12a2f8d1..b2a5c7e60e4f7b799556f20a13ff2b19e81a0505 100644 (file)
@@ -1,5 +1,6 @@
 #include <sys/signal.h>
 #include <unistd.h>
+#include <stdlib.h>
 
 int main() {
     signal(SIGTERM, SIG_IGN);
index 556dec75f716dad170ec1fca17688f52319ae1fd..3b35f93119cadef51ea6f78e20ccbe6efe45524d 100644 (file)
@@ -22,7 +22,7 @@
 #include <limits.h>
 
 /* This will be running setuid root, so be careful! */
-static char * safeEnviron[] = {
+static const char * safeEnviron[] = {
        "PATH=/bin:/sbin:/usr/bin:/usr/sbin",
        "HOME=/root",
        NULL
@@ -43,11 +43,11 @@ usage(void) {
 }
 
 static size_t
-testSafe(char *ifaceConfig) {
+testSafe(char *ifaceConfig, int fd) {
     struct stat sb;
 
     /* These shouldn't be symbolic links -- anal, but that's fine w/ mkj. */
-    if (lstat(ifaceConfig, &sb)) {
+    if (fstat(fd, &sb)) {
        fprintf(stderr, "failed to stat %s: %s\n", ifaceConfig, 
                strerror(errno));
        exit(1);
@@ -82,15 +82,25 @@ userCtl(char *file) {
     int fd = -1, retval = NOT_FOUND;
     size_t size = 0;
 
-    size = testSafe(file);
-
-    buf = contents = malloc(size + 2);
-
+    /* Open the file and then test it to see if we like it. This way
+       we avoid switcheroo attacks. */
     if ((fd = open(file, O_RDONLY)) == -1) {
        fprintf(stderr, "failed to open %s: %s\n", file, strerror(errno));
        exit(1);
     }
 
+    size = testSafe(file, fd);
+    if (size > INT_MAX) {
+        fprintf(stderr, "file %s is too big\n", file);
+        exit(1);
+    }
+
+    buf = contents = malloc(size + 2);
+    if (contents == NULL) {
+        fprintf(stderr, "failed to allocate memory\n");
+        exit(1);
+    }
+
     if (read(fd, contents, size) != size) {
        perror("error reading device configuration");
        exit(1);
@@ -109,7 +119,7 @@ userCtl(char *file) {
        while (chptr >= contents && isspace(*chptr)) chptr--;
        *(++chptr) = '\0';
 
-       if (!strncmp(contents, "USERCTL=", 8)) {
+       if (!strncasecmp(contents, "USERCTL=", 8)) {
            contents += 8;
            if ((contents[0] == '"' &&
                 contents[strlen(contents) - 1] == '"') ||
@@ -120,7 +130,7 @@ userCtl(char *file) {
                contents[strlen(contents) - 1] = '\0';
            }
 
-           if (!strcmp(contents, "yes") || !strcmp(contents, "true")) 
+           if (!strcasecmp(contents, "yes") || !strcasecmp(contents, "true")) 
                retval = FOUND_TRUE;
            else 
                retval = FOUND_FALSE;
index 206d07b9db5b60f2dccf11ed71100814e6b2550a..f00b6f15010839ce82c76aa6d2a952f172a245f5 100644 (file)
@@ -2,6 +2,15 @@
  * usleep
  * 
  * Written by Donald Barnes <djb@redhat.com> for Red Hat, Inc.
+ * 
+ * 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.
  *
  */
 
This page took 0.197438 seconds and 4 git commands to generate.