* Author: AMANDA core development group.
*/
/*
- * $Id: file.c,v 1.14.4.6.4.2.2.5 2003/01/01 23:28:52 martinea Exp $
+ * $Id: file.c,v 1.35 2006/03/09 16:51:41 martinea Exp $
*
* file and directory bashing routines
*/
#include "amanda.h"
#include "util.h"
+static int mk1dir P((const char *, int, uid_t, gid_t));
+
uid_t client_uid = (uid_t) -1;
gid_t client_gid = (gid_t) -1;
** it will do nothing - only root is permitted to change the owner
** of a file.
*/
-int mk1dir(dir, mode, uid, gid)
-char *dir; /* directory to create */
+static int mk1dir(dir, mode, uid, gid)
+const char *dir; /* directory to create */
int mode; /* mode for new directory */
uid_t uid; /* uid for new directory */
gid_t gid; /* gid for new directory */
}
}
+/*
+ *=====================================================================
+ * Close all file descriptors except stdin, stdout and stderr. Make
+ * sure they are open.
+ *
+ * void safe_fd (fd_start, fd_count)
+ *
+ * entry: fd_start - start of fd-s to leave alone (or -1)
+ * fd_count - count of fd-s to leave alone
+ * exit: none
+ *
+ * On exit, all three standard file descriptors will be open and pointing
+ * someplace (either what we were handed or /dev/null) and all other
+ * file descriptors (up to FD_SETSIZE) will be closed.
+ *=====================================================================
+ */
+
+void
+safe_fd(fd_start, fd_count)
+ int fd_start;
+ int fd_count;
+{
+ int fd;
+
+ for(fd = 0; fd < FD_SETSIZE; fd++) {
+ if (fd < 3) {
+ /*
+ * Open three file descriptors. If one of the standard
+ * descriptors is not open it will be pointed to /dev/null...
+ *
+ * This avoids, for instance, someone running us with stderr
+ * closed so that when we open some other file, messages
+ * sent to stderr do not accidentally get written to the
+ * wrong file.
+ */
+ if (fcntl(fd, F_GETFD) == -1) {
+ if (open("/dev/null", O_RDWR) == -1) {
+ fprintf(stderr, "/dev/null is inaccessable: %s\n",
+ strerror(errno));
+ exit(1);
+ }
+ }
+ } else {
+ /*
+ * Make sure nobody spoofs us with a lot of extra open files
+ * that would cause an open we do to get a very high file
+ * descriptor, which in turn might be used as an index into
+ * an array (e.g. an fd_set).
+ */
+ if (fd < fd_start || fd >= fd_start + fd_count) {
+ close(fd);
+ }
+ }
+ }
+
+}
+
/*
*=====================================================================
* Save an existing core file.
while((ch = *s++) != '\0') {
if(ch == '_') {
if(d >= buf + buf_size) {
+ amfree(buf);
return NULL; /* cannot happen */
}
*d++ = '_'; /* convert _ to __ to try */
ch = '_'; /* convert "bad" to "_" */
}
if(d >= buf + buf_size) {
+ amfree(buf);
return NULL; /* cannot happen */
}
*d++ = ch;
}
if(d >= buf + buf_size) {
+ amfree(buf);
return NULL; /* cannot happen */
}
*d = '\0';
char *
debug_agets(s, l, file)
- char *s;
+ const char *s;
int l;
FILE *file;
{
* Find/create a buffer for a particular file descriptor for use with
* areads().
*
- * void areads_getbuf (char *file, int line, int fd)
+ * void areads_getbuf (const char *file, int line, int fd)
*
* entry: file, line = caller source location
* fd = file descriptor to look up
static void
areads_getbuf(s, l, fd)
- char *s;
+ const char *s;
int l;
int fd;
{
char *
debug_areads (s, l, fd)
- char *s;
+ const char *s;
int l;
int fd;
{
char *file;
char *line;
- for(fd = 3; fd < FD_SETSIZE; fd++) {
- /*
- * Make sure nobody spoofs us with a lot of extra open files
- * that would cause an open we do to get a very high file
- * descriptor, which in turn might be used as an index into
- * an array (e.g. an fd_set).
- */
- close(fd);
- }
+ safe_fd(-1, 0);
set_pname("file test");
+ /* Don't die when child closes pipe */
+ signal(SIGPIPE, SIG_IGN);
+
name = "/tmp/a/b/c/d/e";
if (argc > 2 && argv[1][0] != '\0') {
name = argv[1];