PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/amanda/tags/3_3_0beta1/common-src/util.h

#
C Header | 394 lines | 108 code | 59 blank | 227 comment | 4 complexity | ee6463ea367c48ab849345cf4669fc80 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. /* Function to get the GQuark for errors,
  41. * with error codes specified by AmUtilError
  42. *
  43. * @return The GQuark that's used for errors
  44. */
  45. GQuark am_util_error_quark(void);
  46. /* Error codes that may be returned by these functions */
  47. typedef enum {
  48. AM_UTIL_ERROR_HEXDECODEINVAL,
  49. } AmUtilError;
  50. int connect_portrange(sockaddr_union *, in_port_t, in_port_t, char *,
  51. sockaddr_union *, int);
  52. int bind_portrange(int, sockaddr_union *, in_port_t, in_port_t,
  53. char *);
  54. /* just like an accept() call, but periodically calling PROLONG(PROLONG_DATA) and
  55. * returning -1 with errno set to 0 if PROLONG returns false. Note that the socket
  56. * need not be configured as non-blocking.
  57. *
  58. * Other arguments are just like for accept(2).
  59. */
  60. int interruptible_accept(int sock, struct sockaddr *addr, socklen_t *addrlen,
  61. gboolean (*prolong)(gpointer data), gpointer prolong_data);
  62. ssize_t full_writev(int, struct iovec *, int);
  63. char * construct_datestamp(time_t *t);
  64. char * construct_timestamp(time_t *t);
  65. /* quote_string only adds "" if they're required; quote_string_always
  66. * always adds "" around the string */
  67. #define quote_string(str) quote_string_maybe((str), 0)
  68. #define quote_string_always(str) quote_string_maybe((str), 1)
  69. #define len_quote_string(str) len_quote_string_maybe((str), 0);
  70. /*@only@*//*@null@*/char *quote_string_maybe(const char *str, gboolean always);
  71. /*@only@*//*@null@*/char *unquote_string(const char *str);
  72. /*@only@*//*@null@*/int len_quote_string_maybe(const char *str, gboolean always);
  73. /* Split a string into space-delimited words, obeying quoting as created by
  74. * quote_string. To keep compatibility with the old split(), this has the
  75. * characteristic that multiple consecutive spaces are not collapsed into
  76. * a single space: "x y" parses as [ "x", "", "y", NULL ]. The strings are
  77. * unquoted before they are returned, unlike split(). An empty string is
  78. * split into [ "", NULL ].
  79. *
  80. * Returns a NULL-terminated array of strings, which should be freed with
  81. * g_strfreev.
  82. */
  83. gchar ** split_quoted_strings(const gchar *string);
  84. /* Like strtok_r, but consider a quoted string to be a single token. Caller
  85. * must begin parsing with strtok_r first, then pass the saveptr to this function.
  86. *
  87. * Returns NULL on unparseable strings (e.g., unterminated quotes, bad escapes)
  88. */
  89. char * strquotedstr(char **saveptr);
  90. char * sanitize_string(const char *str);
  91. /* Encode a string using URI-style hexadecimal encoding.
  92. * Non-alphanumeric characters will be replaced with "%xx"
  93. * where "xx" is the two-digit hexadecimal representation of the character.
  94. *
  95. * @param str The string to encode
  96. *
  97. * @return The encoded string. An empty string will be returned for NULL.
  98. */
  99. char * hexencode_string(const char *str);
  100. /* Decode a string using URI-style hexadecimal encoding.
  101. *
  102. * @param str The string to decode
  103. * @param err return location for a GError
  104. *
  105. * @return The decoded string. An empty string will be returned for NULL
  106. * or if an error occurs.
  107. */
  108. char * hexdecode_string(const char *str, GError **err);
  109. int copy_file(char *dst, char *src, char **errmsg);
  110. /* These two functions handle "braced alternates", which is a syntax borrowed,
  111. * partially, from shells. See perl/Amanda/Util.pod for a full description of
  112. * the syntax they support.
  113. */
  114. GPtrArray * expand_braced_alternates(char * source);
  115. char * collapse_braced_alternates(GPtrArray *source);
  116. /*
  117. * validate_email return 0 if the following characters are present
  118. * * ( ) < > [ ] , ; : ! $ \ / "
  119. * else returns 1
  120. */
  121. int validate_mailto(const char *mailto);
  122. /* This function is a portable reimplementation of readdir(). It
  123. * returns a newly-allocated string, that should be freed with
  124. * free(). Returns NULL on error or end of directory.
  125. * It is reentrant, with the following exceptions:
  126. * - This function cannot be run at the same time as readdir() or
  127. * readdir64().
  128. * - This function cannot be run simultaneously on the same directory
  129. * handle. */
  130. char * portable_readdir(DIR*);
  131. typedef gboolean (*SearchDirectoryFunctor)(const char * filename,
  132. gpointer user_data);
  133. /* This function will search the given directory handle for files
  134. matching the given POSIX extended regular expression.
  135. For each matching file, the functor will be called with the given
  136. user data. Stops when the functor returns FALSE, or all files have
  137. been searched. Returns the number of matching files. */
  138. int search_directory(DIR * handle, const char * regex,
  139. SearchDirectoryFunctor functor, gpointer user_data);
  140. /* This function extracts a substring match from a regular expression
  141. match result, and copies it into a newly allocated string. Example
  142. usage to get the first matched substring:
  143. substring = find_regmatch(whole_string, pmatch[1])
  144. Note that pmatch[0] yields the entire matching portion of the string. */
  145. char* find_regex_substring(const char* base_string, const regmatch_t match);
  146. void free_new_argv(int new_argc, char **new_argv);
  147. /* Like strcmp(a, b), except that NULL strings are sorted before non-NULL
  148. * strings, instead of segfaulting. */
  149. int compare_possibly_null_strings(const char * a, const char * b);
  150. /* Given a hostname, call getaddrinfo to resolve it. Optionally get the
  151. * entire set of results (if res is not NULL) and the canonical name of
  152. * the host (if canonname is not NULL). The canonical name might
  153. * expand e.g., www.domain.com to server3.webfarm.hosting.com.
  154. *
  155. * If not NULL, the caller is responsible for freeing res with freeaddrinfo().
  156. * Similarly, the caller is responsible for freeing canonname if it is
  157. * not NULL.
  158. *
  159. * @param hostname: the hostname to start with
  160. * @param socktype: the socket type (SOCK_DGRAM or SOCK_STREAM)
  161. * @param res: (result) if not NULL, the results from getaddrinfo()
  162. * @param canonname: (result) if not NULL, the newly-allocated canonical name of the host
  163. * @returns: 0 on success, otherwise a getaddrinfo result (for use with gai_strerror)
  164. */
  165. int resolve_hostname(const char *hostname, int socktype,
  166. struct addrinfo **res, char **canonname);
  167. /* Interpret a status (as returned from wait() and friends)
  168. * into a human-readable sentence.
  169. *
  170. * Caller is responsible for freeing the resulting string.
  171. * The resulting string has already been translated.
  172. *
  173. * The macro definition allows this to work even when amwait_t
  174. * is 'union wait' (4.3BSD). The cast is safe because the two
  175. * argument types are interchangeable.
  176. *
  177. * @param subject: subject of the sentence (program name, etc.)
  178. * @param status: the exit status
  179. * @returns: newly allocated string describing status
  180. */
  181. #define str_exit_status(subject, status) \
  182. _str_exit_status((subject), *(amwait_t *)&(status))
  183. char *_str_exit_status(char *subject, amwait_t status);
  184. /*
  185. * Userid manipulation
  186. */
  187. /* Check that the current uid and euid are set to a specific user,
  188. * calling error() if not. Does nothing if CHECK_USERID is not
  189. * defined.
  190. *
  191. * @param who: one of the RUNNING_AS_* constants, below.
  192. */
  193. typedef enum {
  194. /* doesn't matter */
  195. RUNNING_AS_ANY,
  196. /* userid is 0 */
  197. RUNNING_AS_ROOT,
  198. /* userid belongs to dumpuser (from config) */
  199. RUNNING_AS_DUMPUSER,
  200. /* prefer that userid belongs to dumpuser, but accept when userid belongs to
  201. * CLIENT_LOGIN with a debug-log message (needed because amandad always runs
  202. * as CLIENT_LOGIN, even on server) */
  203. RUNNING_AS_DUMPUSER_PREFERRED,
  204. /* userid belongs to CLIENT_LOGIN (from --with-user) */
  205. RUNNING_AS_CLIENT_LOGIN,
  206. RUNNING_AS_USER_MASK = (1 << 8) - 1,
  207. /* '|' this on to only check the uid, not the euid; use this for programs
  208. * that will call become_root() */
  209. RUNNING_AS_UID_ONLY = 1 << 8
  210. } running_as_flags;
  211. void check_running_as(running_as_flags who);
  212. /* Drop and regain root priviledges; used from setuid-root binaries which only
  213. * need to be root for certain operations. Does nothing if SINGLE_USERID is
  214. * defined.
  215. *
  216. * @param need_root: if 1, try to assume root priviledges; otherwise, drop
  217. * priviledges. If -1, drop them irreversibly.
  218. * @returns: true if the priviledge change succeeded
  219. */
  220. int set_root_privs(int need_root);
  221. /* Become root completely, by setting the uid to 0. This is used by setuid-root
  222. * apps which will exec subprocesses which will also need root priviledges. Does
  223. * nothing if SINGLE_USERID is defined.
  224. *
  225. * @returns: true if the priviledge change succeeded
  226. */
  227. int become_root(void);
  228. /*
  229. * Process parameters
  230. */
  231. /* The 'context' of a process gives a general description of how it is
  232. * used. This affects log output, among other things.
  233. */
  234. typedef enum {
  235. /* default context (logging to stderr, etc. -- not pretty) */
  236. CONTEXT_DEFAULT = 0,
  237. /* user-interfacing command-line utility like amadmin */
  238. CONTEXT_CMDLINE,
  239. /* daemon like amandad or sendbackup */
  240. CONTEXT_DAEMON,
  241. /* a utility used from shell scripts, and thus probably invoked
  242. * quite often */
  243. CONTEXT_SCRIPTUTIL,
  244. } pcontext_t;
  245. /* Set the name of the process. The parameter is copied, and remains
  246. * the responsibility of the caller on return. This value is used in log
  247. * messages and other output throughout Amanda.
  248. *
  249. * @param pname: the new process name
  250. */
  251. void set_pname(char *pname);
  252. /* Get the current process name; the result is in a static buffer, and
  253. * should *not* be free()d by the caller.
  254. *
  255. * @returns: process name
  256. */
  257. char *get_pname(void);
  258. /* Set the type of the process. The parameter is copied, and remains
  259. * the responsibility of the caller on return. This value dictates the
  260. * directory in which debug logs are stored.
  261. *
  262. * @param pname: the new process type
  263. */
  264. void set_ptype(char *ptype);
  265. /* Get the current process name; the result is in a static buffer, and
  266. * should *not* be free()d by the caller.
  267. *
  268. * @returns: process name
  269. */
  270. char *get_ptype(void);
  271. /* Set the process's context
  272. *
  273. * @param context: the new context
  274. */
  275. void set_pcontext(pcontext_t context);
  276. /* Get the process's context
  277. *
  278. * @returns: the context
  279. */
  280. pcontext_t get_pcontext(void);
  281. /*
  282. * Readline support
  283. *
  284. * This either includes the system readline header we found in configure,
  285. * or prototypes some simple stub functions that are used instead.
  286. */
  287. #ifdef HAVE_LIBREADLINE
  288. # if defined(HAVE_READLINE_READLINE_H)
  289. # include <readline/readline.h>
  290. # elif defined(HAVE_READLINE_H)
  291. # include <readline.h>
  292. # else /* !defined(HAVE_READLINE_H) */
  293. extern char *readline ();
  294. # endif /* !defined(HAVE_READLINE_H) */
  295. /* char *cmdline = NULL; */
  296. #else /* !defined(HAVE_LIBREADLINE) */
  297. /* use our own readline */
  298. char * readline(const char *prompt);
  299. #endif /* HAVE_LIBREADLINE */
  300. #ifdef HAVE_READLINE_HISTORY
  301. # if defined(HAVE_READLINE_HISTORY_H)
  302. # include <readline/history.h>
  303. # elif defined(HAVE_HISTORY_H)
  304. # include <history.h>
  305. # else /* !defined(HAVE_HISTORY_H) */
  306. extern void add_history ();
  307. extern int write_history ();
  308. extern int read_history ();
  309. # endif /* defined(HAVE_READLINE_HISTORY_H) */
  310. #else /* !defined(HAVE_READLINE_HISTORY) */
  311. /* use our own add_history */
  312. void add_history(const char *line);
  313. #endif /* HAVE_READLINE_HISTORY */
  314. char *base64_decode_alloc_string(char *);
  315. /* Inform the OpenBSD pthread library about the high-numbered file descriptors
  316. * that an amandad service inherits. This won't be necessary once the new
  317. * threading library is availble (OpenBSD 5.0?), but won't hurt anyway. See the
  318. * thread "Backup issues with OpenBSD 4.5 machines" from September 2009. */
  319. #ifdef __OpenBSD__
  320. void openbsd_fd_inform(void);
  321. #else
  322. #define openbsd_fd_inform()
  323. #endif
  324. /* Add all properties to an ARGV
  325. *
  326. * @param argvchild: Pointer to the ARGV.
  327. * @param proplist: The property list
  328. */
  329. void property_add_to_argv(GPtrArray *argv_ptr, GHashTable *proplist);
  330. /* Print the argv_ptr with g_debug()
  331. *
  332. * @param argv_ptr: GPtrArray of an array to print.
  333. */
  334. void debug_executing(GPtrArray *argv_ptr);
  335. #endif /* UTIL_H */