+/*
+ * 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) {
data=malloc(sbuf.st_size+1);
if (read(fd,data,sbuf.st_size)!=sbuf.st_size) {
close(fd);
+ free(data);
return;
}
close(fd);
*/
/*!
- \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;
}
/*!
- \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.
\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);
}
/*!
- \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.
\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
\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
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;
-
/* 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>
dup2(sock,0);
dup2(sock,1);
dup2(sock,2);
+ close(sock);
bzero(&addr, sizeof(addr));
addr.sun_family = AF_LOCAL;
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;
} else {
fdcmd = open("/dev/null",O_WRONLY);
}
+ if (fdout==-1 || fderr==-1 || fdcmd==-1)
+ return -1;
ourpid = getpid();
if ((pid = fork())==-1) {
perror("fork");
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;
usleep(500);
if (((x=poll(pfds,numfds,500))==-1)&&errno!=EINTR) {
perror("poll");
+ free(pfds);
+ if (procpath)
+ free(procpath);
return -1;
}
if (!reexec) {
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) {
}
}
}
+ free(buf);
} while ( bytesread==8192 );
}
y++;
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;
}
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);
}
#include <sys/signal.h>
#include <unistd.h>
+#include <stdlib.h>
int main() {
signal(SIGTERM, SIG_IGN);
#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
}
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);
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);
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] == '"') ||
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;
* 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.
*
*/