PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/amanda/tags/amanda260/common-src/util.h

#
C Header | 245 lines | 70 code | 38 blank | 137 comment | 0 complexity | 30ec9ecd875e5abe5c9a041c7c4a0691 MD5 | raw file
  1. /*
  2. * Amanda, The Advanced Maryland Automatic Network Disk Archiver
  3. * Copyright (c) 1999 University of Maryland at College Park
  4. * All Rights Reserved.
  5. *
  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.
  15. *
  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.
  22. *
  23. * Authors: the Amanda Development Team. Its members are listed in a
  24. * file named AUTHORS, in the root directory of this distribution.
  25. */
  26. /*
  27. * $Id: util.h,v 1.17 2006/07/26 15:17:36 martinea Exp $
  28. */
  29. #ifndef UTIL_H
  30. #define UTIL_H
  31. #include "amanda.h"
  32. #include "sl.h"
  33. #include <glib.h>
  34. #include <glib-object.h>
  35. #include <regex.h>
  36. #include "glib-util.h"
  37. #define BIGINT INT_MAX
  38. #define BSTRNCMP(a,b) strncmp(a, b, strlen(b))
  39. /* internal types and variables */
  40. ssize_t fullread(int, void *, size_t);
  41. ssize_t fullwrite(int, const void *, size_t);
  42. int connect_portrange(struct sockaddr_storage *, in_port_t, in_port_t, char *,
  43. struct sockaddr_storage *, int);
  44. int bind_portrange(int, struct sockaddr_storage *, in_port_t, in_port_t,
  45. char *);
  46. char * construct_datestamp(time_t *t);
  47. char * construct_timestamp(time_t *t);
  48. /*@only@*//*@null@*/char *quote_string(const char *str);
  49. /*@only@*//*@null@*/char *unquote_string(const char *str);
  50. int needs_quotes(const char * str);
  51. char * sanitize_string(const char *str);
  52. int copy_file(char *dst, char *src, char **errmsg);
  53. /*
  54. * validate_email return 0 if the following characters are present
  55. * * ( ) < > [ ] , ; : ! $ \ / "
  56. * else returns 1
  57. */
  58. int validate_mailto(const char *mailto);
  59. /* This function is a portable reimplementation of readdir(). It
  60. * returns a newly-allocated string, that should be freed with
  61. * free(). Returns NULL on error or end of directory.
  62. * It is reentrant, with the following exceptions:
  63. * - This function cannot be run at the same time as readdir() or
  64. * readdir64().
  65. * - This function cannot be run simultaneously on the same directory
  66. * handle. */
  67. char * portable_readdir(DIR*);
  68. typedef gboolean (*SearchDirectoryFunctor)(const char * filename,
  69. gpointer user_data);
  70. /* This function will search the given directory handle for files
  71. matching the given POSIX extended regular expression.
  72. For each matching file, the functor will be called with the given
  73. user data. Stops when the functor returns FALSE, or all files have
  74. been searched. Returns the number of matching files. */
  75. int search_directory(DIR * handle, const char * regex,
  76. SearchDirectoryFunctor functor, gpointer user_data);
  77. /* This function extracts a substring match from a regular expression
  78. match result, and copies it into a newly allocated string. Example
  79. usage to get the first matched substring:
  80. substring = find_regmatch(whole_string, pmatch[1])
  81. Note that pmatch[0] yields the entire matching portion of the string. */
  82. char* find_regex_substring(const char* base_string, const regmatch_t match);
  83. void free_new_argv(int new_argc, char **new_argv);
  84. /* Like strcmp(a, b), except that NULL strings are sorted before non-NULL
  85. * strings, instead of segfaulting. */
  86. int compare_possibly_null_strings(const char * a, const char * b);
  87. /* Does g_thread_init(), along with anything else that should be done
  88. * before/after thread setup. It's OK to call this function more than once.
  89. * Returns TRUE if threads are supported. */
  90. gboolean amanda_thread_init(void);
  91. /* Given a hostname, call getaddrinfo to resolve it. Optionally get the
  92. * entire set of results (if res is not NULL) and the canonical name of
  93. * the host (if canonname is not NULL). The canonical name might
  94. * expand e.g., www.domain.com to server3.webfarm.hosting.com.
  95. *
  96. * If not NULL, the caller is responsible for freeing res with freeaddrinfo().
  97. * Similarly, the caller is responsible for freeing canonname if it is
  98. * not NULL.
  99. *
  100. * @param hostname: the hostname to start with
  101. * @param res: (result) if not NULL, the results from getaddrinfo()
  102. * @param canonname: (result) if not NULL, the canonical name of the host
  103. * @returns: newly allocated canonical hostname, or NULL if no
  104. * canonical hostname was available.
  105. */
  106. int resolve_hostname(const char *hostname, int socktype,
  107. struct addrinfo **res, char **canonname);
  108. /* Interpret a status (as returned from wait() and friends)
  109. * into a human-readable sentence.
  110. *
  111. * Caller is responsible for freeing the resulting string.
  112. * The resulting string has already been translated.
  113. *
  114. * The macro definition allows this to work even when amwait_t
  115. * is 'union wait' (4.3BSD). The cast is safe because the two
  116. * argument types are interchangeable.
  117. *
  118. * @param subject: subject of the sentence (program name, etc.)
  119. * @param status: the exit status
  120. * @returns: newly allocated string describing status
  121. */
  122. #define str_exit_status(subject, status) \
  123. _str_exit_status((subject), *(amwait_t *)&(status))
  124. char *_str_exit_status(char *subject, amwait_t status);
  125. /*
  126. * Userid manipulation
  127. */
  128. /* Check that the current uid and euid are set to a specific user,
  129. * calling error() if not. Does nothing if CHECK_USERID is not
  130. * defined.
  131. *
  132. * @param who: one of the RUNNING_AS_* constants, below.
  133. */
  134. typedef enum {
  135. /* userid is 0 */
  136. RUNNING_AS_ROOT,
  137. /* userid belongs to dumpuser (from config) */
  138. RUNNING_AS_DUMPUSER,
  139. /* prefer that userid belongs to dumpuser, but accept when userid belongs to
  140. * CLIENT_LOGIN with a debug-log message (needed because amandad always runs
  141. * as CLIENT_LOGIN, even on server) */
  142. RUNNING_AS_DUMPUSER_PREFERRED,
  143. /* userid belongs to CLIENT_LOGIN (from --with-user) */
  144. RUNNING_AS_CLIENT_LOGIN,
  145. RUNNING_AS_USER_MASK = (1 << 8) - 1,
  146. /* '&' this on to only check the uid, not the euid; use this for programs
  147. * that will call become_root() */
  148. RUNNING_AS_UID_ONLY = 1 << 8
  149. } running_as_flags;
  150. void check_running_as(running_as_flags who);
  151. /* Drop and regain root priviledges; used from setuid-root binaries which only
  152. * need to be root for certain operations. Does nothing if SINGLE_USERID is
  153. * defined.
  154. *
  155. * @param need_root: if true, try to assume root priviledges; otherwise, drop
  156. * priviledges.
  157. * @returns: true if the priviledge change succeeded
  158. */
  159. int set_root_privs(int need_root);
  160. /* Become root completely, by setting the uid to 0. This is used by setuid-root
  161. * apps which will exec subprocesses which will also need root priviledges. Does
  162. * nothing if SINGLE_USERID is defined.
  163. *
  164. * @returns: true if the priviledge change succeeded
  165. */
  166. int become_root(void);
  167. /*
  168. * Process parameters
  169. */
  170. /* Set the name of the process. The parameter is copied, and remains
  171. * the responsibility of the caller on return. This value is used in log
  172. * messages and other output throughout Amanda.
  173. *
  174. * @param pname: the new process name
  175. */
  176. void set_pname(char *pname);
  177. /* Get the current process name; the result is in a static buffer, and
  178. * should *not* be free()d by the caller.
  179. *
  180. * @returns: process name
  181. */
  182. char *get_pname(void);
  183. /*
  184. * Readline support
  185. *
  186. * This either includes the system readline header we found in configure,
  187. * or prototypes some simple stub functions that are used instead.
  188. */
  189. #ifdef HAVE_READLINE
  190. # ifdef HAVE_READLINE_READLINE_H
  191. # include <readline/readline.h>
  192. # ifdef HAVE_READLINE_HISTORY_H
  193. # include <readline/history.h>
  194. # endif
  195. # else
  196. # ifdef HAVE_READLINE_H
  197. # include <readline.h>
  198. # ifdef HAVE_HISTORY_H
  199. # include <history.h>
  200. # endif
  201. # endif
  202. # endif
  203. #else
  204. char * readline(const char *prompt);
  205. void add_history(const char *line);
  206. #endif
  207. #endif /* UTIL_H */