PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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