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

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

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