1 /* 2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver 3 * Copyright (c) 1997-1998 University of Maryland at College Park 4 * Copyright (c) 2007-2013 Zmanda, Inc. All Rights Reserved. 5 * All Rights Reserved. 6 * 7 * Permission to use, copy, modify, distribute, and sell this software and its 8 * documentation for any purpose is hereby granted without fee, provided that 9 * the above copyright notice appear in all copies and that both that 10 * copyright notice and this permission notice appear in supporting 11 * documentation, and that the name of U.M. not be used in advertising or 12 * publicity pertaining to distribution of the software without specific, 13 * written prior permission. U.M. makes no representations about the 14 * suitability of this software for any purpose. It is provided "as is" 15 * without express or implied warranty. 16 * 17 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. 19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 23 * 24 * Author: AMANDA core development group. 25 */ 26 27 extern int mkpdir(char *file, mode_t mode, uid_t uid, gid_t gid); 28 extern int rmpdir(char *file, char *topdir); 29 30 /* Given a pathname, convert it to "canonical form" for this system. Currently, 31 * this means nothing on POSIX, but means substituting /cygdrive, etc. on Cygwin. 32 * 33 * @param pathname: the pathname to canonicalize 34 * @param result_buf (output): the canonicalize pathname; this should be a buffer of 35 * at least PATH_MAX bytes. 36 */ 37 void canonicalize_pathname(char *pathname, char *result_buf); 38 39 extern char *sanitise_filename(char *inp); 40 char *old_sanitise_filename(char *inp); 41 void safe_fd(int fd_start, int fd_count); 42 void safe_cd(void); 43 void save_core(void); 44 45 /* Get the uid of CLIENT_LOGIN, or -1 if it doesn't exist. Note that, if 46 * only running a server, CLIENT_LOGIN may legitimately not exist. 47 * 48 * @returns: userid, or -1 if invalid 49 */ 50 uid_t get_client_uid(void); 51 52 /* Get the gid of CLIENT_LOGIN, or -1 if it doesn't exist. Note that, if 53 * only running a server, CLIENT_LOGIN may legitimately not exist. 54 * 55 * @returns: groupid, or -1 if invalid 56 */ 57 gid_t get_client_gid(void); 58 59 extern /*@only@*/ /*@null@*/ char *debug_agets(const char *c, int l, FILE *file); 60 extern /*@only@*/ /*@null@*/ char *debug_areads(const char *c, int l, int fd); 61 #define agets(f) debug_agets(__FILE__,__LINE__,(f)) 62 #define areads(f) debug_areads(__FILE__,__LINE__,(f)) 63 64 ssize_t areads_dataready(int fd); 65 void areads_relbuf(int fd); 66 67 /* 68 * "Safe" close macros. Close the object then set it to a value that 69 * will cause an error if referenced. 70 * 71 * aclose(fd) -- close a file descriptor and set it to -1. 72 * afclose(f) -- close a stdio file and set it to NULL. 73 * apclose(p) -- close a stdio pipe file and set it to NULL. 74 * 75 * Note: be careful not to do the following: 76 * 77 * for(fd = low; fd < high; fd++) { 78 * aclose(fd); 79 * } 80 * 81 * Since aclose() sets the argument to -1, this will loop forever. 82 * Just copy fd to a temp variable and use that with aclose(). 83 * 84 * Aclose() interacts with areads() to inform it to release any buffer 85 * it has outstanding on the file descriptor. 86 */ 87 88 #define aclose(fd) do { \ 89 if((fd) >= 0) { \ 90 close(fd); \ 91 areads_relbuf(fd); \ 92 } \ 93 (fd) = -1; \ 94 } while(0) 95 96 #define afclose(f) do { \ 97 if((f) != NULL) { \ 98 fclose(f); \ 99 } \ 100 (f) = NULL; \ 101 } while(0) 102 103 #define apclose(p) do { \ 104 if((p) != NULL) { \ 105 pclose(p); \ 106 } \ 107 (p) = NULL; \ 108 } while(0) 109 110 111 /* Calls system open(), but takes care of interrupted system calls and 112 * clears the close-on-exec bit. In the failure case, errno is 113 * retained from the final call to open(). */ 114 extern int robust_open(const char * pathname, int flags, mode_t mode); 115 116 /* Same idea but for close. */ 117 extern int robust_close(int fd); 118 119 /* Get the original working directory, at application startup 120 * 121 * @returns: pointer to statically allocated string 122 */ 123 char *get_original_cwd(void); 124