PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C++ Header | 330 lines | 88 code | 51 blank | 191 comment | 0 complexity | 569f200dd40baf1a773f3787a05f0a9d 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. int connect_portrange(sockaddr_union *, in_port_t, in_port_t, char *,
  41. sockaddr_union *, int);
  42. int bind_portrange(int, sockaddr_union *, in_port_t, in_port_t,
  43. char *);
  44. ssize_t full_writev(int, struct iovec *, int);
  45. char * construct_datestamp(time_t *t);
  46. char * construct_timestamp(time_t *t);
  47. /*@only@*//*@null@*/char *quote_string(const char *str);
  48. /*@only@*//*@null@*/char *unquote_string(const char *str);
  49. int needs_quotes(const char * str);
  50. /* Split a string into space-delimited words, obeying quoting as created by
  51. * quote_string. To keep compatibility with the old split(), this has the
  52. * characteristic that multiple consecutive spaces are not collapsed into
  53. * a single space: "x y" parses as [ "x", "", "y", NULL ]. The strings are
  54. * unquoted before they are returned, unlike split(). An empty string is
  55. * split into [ "", NULL ].
  56. *
  57. * Returns a NULL-terminated array of strings, which should be freed with
  58. * g_strfreev.
  59. */
  60. gchar ** split_quoted_strings(const gchar *string);
  61. /* Like strtok_r, but consider a quoted string to be a single token. Caller
  62. * must begin parsing with strtok_r first, then pass the saveptr to this function.
  63. *
  64. * Returns NULL on unparseable strings (e.g., unterminated quotes, bad escapes)
  65. */
  66. char * strquotedstr(char **saveptr);
  67. char * sanitize_string(const char *str);
  68. int copy_file(char *dst, char *src, char **errmsg);
  69. /*
  70. * validate_email return 0 if the following characters are present
  71. * * ( ) < > [ ] , ; : ! $ \ / "
  72. * else returns 1
  73. */
  74. int validate_mailto(const char *mailto);
  75. /* This function is a portable reimplementation of readdir(). It
  76. * returns a newly-allocated string, that should be freed with
  77. * free(). Returns NULL on error or end of directory.
  78. * It is reentrant, with the following exceptions:
  79. * - This function cannot be run at the same time as readdir() or
  80. * readdir64().
  81. * - This function cannot be run simultaneously on the same directory
  82. * handle. */
  83. char * portable_readdir(DIR*);
  84. typedef gboolean (*SearchDirectoryFunctor)(const char * filename,
  85. gpointer user_data);
  86. /* This function will search the given directory handle for files
  87. matching the given POSIX extended regular expression.
  88. For each matching file, the functor will be called with the given
  89. user data. Stops when the functor returns FALSE, or all files have
  90. been searched. Returns the number of matching files. */
  91. int search_directory(DIR * handle, const char * regex,
  92. SearchDirectoryFunctor functor, gpointer user_data);
  93. /* This function extracts a substring match from a regular expression
  94. match result, and copies it into a newly allocated string. Example
  95. usage to get the first matched substring:
  96. substring = find_regmatch(whole_string, pmatch[1])
  97. Note that pmatch[0] yields the entire matching portion of the string. */
  98. char* find_regex_substring(const char* base_string, const regmatch_t match);
  99. void free_new_argv(int new_argc, char **new_argv);
  100. /* Like strcmp(a, b), except that NULL strings are sorted before non-NULL
  101. * strings, instead of segfaulting. */
  102. int compare_possibly_null_strings(const char * a, const char * b);
  103. /* Given a hostname, call getaddrinfo to resolve it. Optionally get the
  104. * entire set of results (if res is not NULL) and the canonical name of
  105. * the host (if canonname is not NULL). The canonical name might
  106. * expand e.g., www.domain.com to server3.webfarm.hosting.com.
  107. *
  108. * If not NULL, the caller is responsible for freeing res with freeaddrinfo().
  109. * Similarly, the caller is responsible for freeing canonname if it is
  110. * not NULL.
  111. *
  112. * @param hostname: the hostname to start with
  113. * @param res: (result) if not NULL, the results from getaddrinfo()
  114. * @param canonname: (result) if not NULL, the canonical name of the host
  115. * @returns: newly allocated canonical hostname, or NULL if no
  116. * canonical hostname was available.
  117. */
  118. int resolve_hostname(const char *hostname, int socktype,
  119. struct addrinfo **res, char **canonname);
  120. /* Interpret a status (as returned from wait() and friends)
  121. * into a human-readable sentence.
  122. *
  123. * Caller is responsible for freeing the resulting string.
  124. * The resulting string has already been translated.
  125. *
  126. * The macro definition allows this to work even when amwait_t
  127. * is 'union wait' (4.3BSD). The cast is safe because the two
  128. * argument types are interchangeable.
  129. *
  130. * @param subject: subject of the sentence (program name, etc.)
  131. * @param status: the exit status
  132. * @returns: newly allocated string describing status
  133. */
  134. #define str_exit_status(subject, status) \
  135. _str_exit_status((subject), *(amwait_t *)&(status))
  136. char *_str_exit_status(char *subject, amwait_t status);
  137. /*
  138. * Userid manipulation
  139. */
  140. /* Check that the current uid and euid are set to a specific user,
  141. * calling error() if not. Does nothing if CHECK_USERID is not
  142. * defined.
  143. *
  144. * @param who: one of the RUNNING_AS_* constants, below.
  145. */
  146. typedef enum {
  147. /* doesn't matter */
  148. RUNNING_AS_ANY,
  149. /* userid is 0 */
  150. RUNNING_AS_ROOT,
  151. /* userid belongs to dumpuser (from config) */
  152. RUNNING_AS_DUMPUSER,
  153. /* prefer that userid belongs to dumpuser, but accept when userid belongs to
  154. * CLIENT_LOGIN with a debug-log message (needed because amandad always runs
  155. * as CLIENT_LOGIN, even on server) */
  156. RUNNING_AS_DUMPUSER_PREFERRED,
  157. /* userid belongs to CLIENT_LOGIN (from --with-user) */
  158. RUNNING_AS_CLIENT_LOGIN,
  159. RUNNING_AS_USER_MASK = (1 << 8) - 1,
  160. /* '&' this on to only check the uid, not the euid; use this for programs
  161. * that will call become_root() */
  162. RUNNING_AS_UID_ONLY = 1 << 8
  163. } running_as_flags;
  164. void check_running_as(running_as_flags who);
  165. /* Drop and regain root priviledges; used from setuid-root binaries which only
  166. * need to be root for certain operations. Does nothing if SINGLE_USERID is
  167. * defined.
  168. *
  169. * @param need_root: if true, try to assume root priviledges; otherwise, drop
  170. * priviledges.
  171. * @returns: true if the priviledge change succeeded
  172. */
  173. int set_root_privs(int need_root);
  174. /* Become root completely, by setting the uid to 0. This is used by setuid-root
  175. * apps which will exec subprocesses which will also need root priviledges. Does
  176. * nothing if SINGLE_USERID is defined.
  177. *
  178. * @returns: true if the priviledge change succeeded
  179. */
  180. int become_root(void);
  181. /*
  182. * Process parameters
  183. */
  184. /* The 'context' of a process gives a general description of how it is
  185. * used. This affects log output, among other things.
  186. */
  187. typedef enum {
  188. /* default context (logging to stderr, etc. -- not pretty) */
  189. CONTEXT_DEFAULT = 0,
  190. /* user-interfacing command-line utility like amadmin */
  191. CONTEXT_CMDLINE,
  192. /* daemon like amandad or sendbackup */
  193. CONTEXT_DAEMON,
  194. /* a utility used from shell scripts, and thus probably invoked
  195. * quite often */
  196. CONTEXT_SCRIPTUTIL,
  197. } pcontext_t;
  198. /* Set the name of the process. The parameter is copied, and remains
  199. * the responsibility of the caller on return. This value is used in log
  200. * messages and other output throughout Amanda.
  201. *
  202. * @param pname: the new process name
  203. */
  204. void set_pname(char *pname);
  205. /* Get the current process name; the result is in a static buffer, and
  206. * should *not* be free()d by the caller.
  207. *
  208. * @returns: process name
  209. */
  210. char *get_pname(void);
  211. /* Set the type of the process. The parameter is copied, and remains
  212. * the responsibility of the caller on return. This value dictates the
  213. * directory in which debug logs are stored.
  214. *
  215. * @param pname: the new process type
  216. */
  217. void set_ptype(char *ptype);
  218. /* Get the current process name; the result is in a static buffer, and
  219. * should *not* be free()d by the caller.
  220. *
  221. * @returns: process name
  222. */
  223. char *get_ptype(void);
  224. /* Set the process's context
  225. *
  226. * @param context: the new context
  227. */
  228. void set_pcontext(pcontext_t context);
  229. /* Get the process's context
  230. *
  231. * @returns: the context
  232. */
  233. pcontext_t get_pcontext(void);
  234. /*
  235. * Readline support
  236. *
  237. * This either includes the system readline header we found in configure,
  238. * or prototypes some simple stub functions that are used instead.
  239. */
  240. #ifdef HAVE_READLINE
  241. # ifdef HAVE_READLINE_READLINE_H
  242. # include <readline/readline.h>
  243. # ifdef HAVE_READLINE_HISTORY_H
  244. # include <readline/history.h>
  245. # endif
  246. # else
  247. # ifdef HAVE_READLINE_H
  248. # include <readline.h>
  249. # ifdef HAVE_HISTORY_H
  250. # include <history.h>
  251. # endif
  252. # endif
  253. # endif
  254. #else
  255. char * readline(const char *prompt);
  256. void add_history(const char *line);
  257. #endif
  258. char *base64_decode_alloc_string(char *);
  259. /* A GHFunc (callback for g_hash_table_foreach),
  260. * Count the number of properties.
  261. *
  262. * @param key_p: (char *) property name.
  263. * @param value_p: (GSList *) property values list.
  264. * @param user_data_p: (int *) count are added to that value.
  265. */
  266. void count_proplist(gpointer key_p,
  267. gpointer value_p,
  268. gpointer user_data_p);
  269. /* A GHFunc (callback for g_hash_table_foreach),
  270. * Store a property and it's value in an ARGV.
  271. *
  272. * @param key_p: (char *) property name.
  273. * @param value_p: (GSList *) property values list.
  274. * @param user_data_p: (char ***) pointer to ARGV.
  275. */
  276. void proplist_add_to_argv(gpointer key_p,
  277. gpointer value_p,
  278. gpointer user_data_p);
  279. #endif /* UTIL_H */