2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1997-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: AMANDA core development group.
26 * $Id: file.c,v 1.14.4.6.4.2.2.5 2003/01/01 23:28:52 martinea Exp $
28 * file and directory bashing routines
34 uid_t client_uid = (uid_t) -1;
35 gid_t client_gid = (gid_t) -1;
37 /* Make a directory (internal function).
38 ** If the directory already exists then we pretend we created it.
39 ** XXX - I'm not sure about the use of the chown() stuff. On most systems
40 ** it will do nothing - only root is permitted to change the owner
43 int mk1dir(dir, mode, uid, gid)
44 char *dir; /* directory to create */
45 int mode; /* mode for new directory */
46 uid_t uid; /* uid for new directory */
47 gid_t gid; /* gid for new directory */
49 int rc; /* return code */
51 rc = 0; /* assume the best */
53 if(mkdir(dir, mode) == 0) {
54 chmod(dir, mode); /* mkdir() is affected by the umask */
55 chown(dir, uid, gid); /* XXX - no-op on most systems? */
56 } else { /* maybe someone beat us to it */
60 if(access(dir, F_OK) != 0) rc = -1;
61 errno = serrno; /* pass back the real error */
69 * Make a directory hierarchy given an entry to be created (by the caller)
70 * in the new target. In other words, create all the directories down to
71 * the last element, but not the last element. So a (potential) file name
72 * may be passed to mkpdir and all the parents of that file will be created.
74 int mkpdir(file, mode, uid, gid)
75 char *file; /* file to create parent directories for */
76 int mode; /* mode for new directories */
77 uid_t uid; /* uid for new directories */
78 gid_t gid; /* gid for new directories */
81 int rc; /* return code */
85 dir = stralloc(file); /* make a copy we can play with */
87 p = strrchr(dir, '/');
88 if(p != dir && p != NULL) { /* got a '/' or a simple name */
91 if(access(dir, F_OK) != 0) { /* doesn't exist */
92 if(mkpdir(dir, mode, uid, gid) != 0 ||
93 mk1dir(dir, mode, uid, gid) != 0) rc = -1; /* create failed */
102 /* Remove as much of a directory hierarchy as possible.
104 ** - assumes that rmdir() on a non-empty directory will fail!
105 ** - stops deleting before topdir, ie: topdir will not be removed
106 ** - if file is not under topdir this routine will not notice
108 int rmpdir(file, topdir)
109 char *file; /* directory hierarchy to remove */
110 char *topdir; /* where to stop removing */
113 char *p, *dir = NULL;
115 if(strcmp(file, topdir) == 0) return 0; /* all done */
118 if (rc != 0) switch(errno) {
120 #if ENOTEMPTY != EEXIST /* AIX makes these the same */
124 case EEXIST: /* directory not empty */
125 return 0; /* cant do much more */
126 case ENOENT: /* it has already gone */
129 case ENOTDIR: /* it was a file */
134 if(rc != 0) return -1; /* unexpected error */
136 dir = stralloc(file);
138 p = strrchr(dir, '/');
139 if(p == dir) rc = 0; /* no /'s */
143 rc = rmpdir(dir, topdir);
153 *=====================================================================
154 * Do Amanda setup for all programs.
156 * void amanda_setup (int argc, char **argv, int setup_flags)
158 * entry: setup_flags (see AMANDA_SETUP_FLAG_xxx)
160 *=====================================================================
164 amanda_setup (argc, argv, setup_flags)
172 *=====================================================================
173 * Change directory to a "safe" location and set some base environment.
175 * void safe_cd (void)
177 * entry: client_uid and client_gid set to CLIENT_LOGIN information
180 * Set a default umask of 0077.
182 * Create the Amada debug directory (if defined) and the Amanda temp
185 * Try to chdir to the Amanda debug directory first, but it must be owned
186 * by the Amanda user and not allow rwx to group or other. Otherwise,
187 * try the same thing to the Amanda temp directory.
189 * If that is all OK, call save_core().
191 * Otherwise, cd to "/" so if we take a signal we cannot drop core
192 * unless the system administrator has made special arrangements (e.g.
193 * pre-created a core file with the right ownership and permissions).
194 *=====================================================================
202 struct passwd *pwent;
205 if(client_uid == (uid_t) -1 && (pwent = getpwnam(CLIENT_LOGIN)) != NULL) {
206 client_uid = pwent->pw_uid;
207 client_gid = pwent->pw_gid;
213 if (client_uid != (uid_t) -1) {
214 #if defined(AMANDA_DBGDIR)
215 d = stralloc2(AMANDA_DBGDIR, "/.");
216 (void) mkpdir(d, 02700, client_uid, client_gid);
219 d = stralloc2(AMANDA_TMPDIR, "/.");
220 (void) mkpdir(d, 02700, client_uid, client_gid);
224 #if defined(AMANDA_DBGDIR)
225 if (chdir(AMANDA_DBGDIR) != -1
226 && stat(".", &sbuf) != -1
227 && (sbuf.st_mode & 0777) == 0700 /* drwx------ */
228 && sbuf.st_uid == client_uid) { /* owned by Amanda user */
229 cd_ok = 1; /* this is a good place to be */
233 && chdir(AMANDA_TMPDIR) != -1
234 && stat(".", &sbuf) != -1
235 && (sbuf.st_mode & 0777) == 0700 /* drwx------ */
236 && sbuf.st_uid == client_uid) { /* owned by Amanda user */
237 cd_ok = 1; /* this is a good place to be */
240 save_core(); /* save any old core file */
242 (void) chdir("/"); /* assume this works */
247 *=====================================================================
248 * Save an existing core file.
250 * void save_core (void)
257 * "core" to "coreYYYYMMDD",
258 * "coreYYYYMMDD" to "coreYYYYMMDDa",
259 * "coreYYYYMMDDa" to "coreYYYYMMDDb",
262 * ... where YYYYMMDD is the modification time of the original file.
263 * If it gets that far, an old "coreYYYYMMDDz" is thrown away.
264 *=====================================================================
272 if(stat("core", &sbuf) != -1) {
277 ts = construct_datestamp((time_t *)&sbuf.st_mtime);
280 old = vstralloc("core", ts, suffix, NULL);
282 while(ts[0] != '\0') {
285 if(suffix[0] == 'a') {
287 } else if(suffix[0] == '\0') {
292 old = vstralloc("core", ts, suffix, NULL);
293 (void)rename(old, new); /* it either works ... */
302 ** Sanitise a file name.
304 ** Convert all funny characters to '_' so that we can use,
305 ** for example, disk names as part of file names.
307 ** - there is a many-to-one mapping between input and output
308 ** XXX - We only look for '/' and ' ' at the moment. May
309 ** XXX - be we should also do all unprintables.
311 char *sanitise_filename(inp)
319 buf_size = 2 * strlen(inp) + 1; /* worst case */
320 buf = alloc(buf_size);
323 while((ch = *s++) != '\0') {
325 if(d >= buf + buf_size) {
326 return NULL; /* cannot happen */
328 *d++ = '_'; /* convert _ to __ to try */
329 /* and ensure unique output */
330 } else if(ch == '/' || isspace(ch)) {
331 ch = '_'; /* convert "bad" to "_" */
333 if(d >= buf + buf_size) {
334 return NULL; /* cannot happen */
338 if(d >= buf + buf_size) {
339 return NULL; /* cannot happen */
347 *=====================================================================
348 * Get the next line of input from a stdio file.
350 * char *agets (FILE *f)
352 * entry: f = stdio stream to read
353 * exit: returns a pointer to an alloc'd string or NULL at EOF
354 * or error (errno will be zero on EOF).
356 * Notes: the newline, if read, is removed from the string
357 * the caller is responsible for free'ing the string
358 *=====================================================================
362 debug_agets(s, l, file)
367 char *line = NULL, *line_ptr;
368 size_t line_size, size_save;
369 int line_free, line_len;
373 malloc_enter(dbmalloc_caller_loc(s, l));
375 #define AGETS_LINE_INCR 128
377 line_size = AGETS_LINE_INCR;
378 line = debug_alloc (s, l, line_size);
379 line_free = line_size;
383 while ((f = fgets(line_ptr, line_free, file)) != NULL) {
385 * Note that we only have to search what we just read, not
388 if ((cp = strchr (line_ptr, '\n')) != NULL) {
389 line_len += cp - line_ptr;
390 *cp = '\0'; /* zap the newline */
391 break; /* got to end of line */
393 line_len += line_free - 1; /* bytes read minus '\0' */
394 size_save = line_size;
395 if (line_size < 256 * AGETS_LINE_INCR) {
398 line_size += 256 * AGETS_LINE_INCR;
400 cp = debug_alloc (s, l, line_size); /* get more space */
401 memcpy (cp, line, size_save); /* copy old to new */
402 free (line); /* and release the old */
404 line_ptr = line + size_save - 1; /* start at the null byte */
405 line_free = line_size - line_len; /* and we get to use it */
408 * Return what we got even if there was not a newline. Only
409 * report done (NULL) when no data was processed.
411 if (f == NULL && line_len == 0) {
413 line = NULL; /* redundant, but clear */
415 errno = 0; /* flag EOF vs error */
418 malloc_leave(dbmalloc_caller_loc(s, l));
423 *=====================================================================
424 * Find/create a buffer for a particular file descriptor for use with
427 * void areads_getbuf (char *file, int line, int fd)
429 * entry: file, line = caller source location
430 * fd = file descriptor to look up
431 * exit: returns a pointer to the buffer, possibly new
432 *=====================================================================
435 static struct areads_buffer {
439 } *areads_buffer = NULL;
440 static int areads_bufcount = 0;
441 static ssize_t areads_bufsize = BUFSIZ; /* for the test program */
444 areads_getbuf(s, l, fd)
449 struct areads_buffer *new;
453 if(fd >= areads_bufcount) {
454 size = (fd + 1) * sizeof(*areads_buffer);
455 new = (struct areads_buffer *) debug_alloc(s, l, size);
456 memset((char *)new, 0, size);
458 size = areads_bufcount * sizeof(*areads_buffer);
459 memcpy(new, areads_buffer, size);
461 amfree(areads_buffer);
463 areads_bufcount = fd + 1;
465 if(areads_buffer[fd].buffer == NULL) {
466 areads_buffer[fd].bufsize = areads_bufsize;
467 areads_buffer[fd].buffer = debug_alloc(s, l,
468 areads_buffer[fd].bufsize + 1);
469 areads_buffer[fd].buffer[0] = '\0';
470 areads_buffer[fd].endptr = areads_buffer[fd].buffer;
475 *=====================================================================
476 * Return the amount of data still in an areads buffer.
478 * ssize_t areads_dataready (int fd)
480 * entry: fd = file descriptor to release buffer for
481 * exit: returns number of bytes of data ready to process
482 *=====================================================================
491 if(fd >= 0 && fd < areads_bufcount && areads_buffer[fd].buffer != NULL) {
492 r = (ssize_t) (areads_buffer[fd].endptr - areads_buffer[fd].buffer);
498 *=====================================================================
499 * Release a buffer for a particular file descriptor used by areads().
501 * void areads_relbuf (int fd)
503 * entry: fd = file descriptor to release buffer for
505 *=====================================================================
512 if(fd >= 0 && fd < areads_bufcount) {
513 amfree(areads_buffer[fd].buffer);
514 areads_buffer[fd].endptr = NULL;
515 areads_buffer[fd].bufsize = 0;
520 *=====================================================================
521 * Get the next line of input from a file descriptor.
523 * char *areads (int fd)
525 * entry: fd = file descriptor to read
526 * exit: returns a pointer to an alloc'd string or NULL at EOF
527 * or error (errno will be zero on EOF).
529 * Notes: the newline, if read, is removed from the string
530 * the caller is responsible for free'ing the string
531 *=====================================================================
535 debug_areads (s, l, fd)
549 malloc_enter(dbmalloc_caller_loc(s, l));
555 areads_getbuf(s, l, fd);
556 buffer = areads_buffer[fd].buffer;
557 endptr = areads_buffer[fd].endptr;
558 buflen = areads_buffer[fd].bufsize - (endptr - buffer);
559 while((nl = strchr(buffer, '\n')) == NULL) {
561 * No newline yet, so get more data.
564 if ((size = areads_buffer[fd].bufsize) < 256 * areads_bufsize) {
567 size += 256 * areads_bufsize;
569 newbuf = debug_alloc(s, l, size + 1);
570 memcpy (newbuf, buffer, areads_buffer[fd].bufsize + 1);
571 amfree(areads_buffer[fd].buffer);
573 areads_buffer[fd].buffer = newbuf;
574 areads_buffer[fd].endptr = newbuf + areads_buffer[fd].bufsize;
575 areads_buffer[fd].bufsize = size;
576 buffer = areads_buffer[fd].buffer;
577 endptr = areads_buffer[fd].endptr;
578 buflen = areads_buffer[fd].bufsize - (endptr - buffer);
580 if ((r = read(fd, endptr, buflen)) <= 0) {
582 errno = 0; /* flag EOF instead of error */
584 malloc_leave(dbmalloc_caller_loc(s, l));
587 endptr[r] = '\0'; /* we always leave room for this */
592 line = stralloc(buffer);
593 size = endptr - nl; /* data still left in buffer */
594 memmove(buffer, nl, size);
595 areads_buffer[fd].endptr = buffer + size;
596 areads_buffer[fd].endptr[0] = '\0';
597 malloc_leave(dbmalloc_caller_loc(s, l));
614 for(fd = 3; fd < FD_SETSIZE; fd++) {
616 * Make sure nobody spoofs us with a lot of extra open files
617 * that would cause an open we do to get a very high file
618 * descriptor, which in turn might be used as an index into
619 * an array (e.g. an fd_set).
624 set_pname("file test");
626 name = "/tmp/a/b/c/d/e";
627 if (argc > 2 && argv[1][0] != '\0') {
631 if (argc > 3 && argv[2][0] != '\0') {
635 if (argc > 4 && argv[3][0] != '\0') {
639 fprintf(stderr, "Create parent directories of %s ...", name);
640 rc = mkpdir(name, 02777, (uid_t)-1, (gid_t)-1);
642 fprintf(stderr, " done\n");
648 fprintf(stderr, "Delete %s back to %s ...", name, top);
649 rc = rmpdir(name, top);
651 fprintf(stderr, " done\n");
657 fprintf(stderr, "areads dump of %s ...", file);
658 if ((fd = open (file, 0)) < 0) {
662 areads_bufsize = 1; /* force buffer overflow */
663 while ((line = areads(fd)) != NULL) {
668 fprintf(stderr, " done.\n");
670 fprintf(stderr, "Finished.\n");