2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1999 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: util.h,v 1.17 2006/07/26 15:17:36 martinea Exp $
36 #include <glib-object.h>
39 #include "glib-util.h"
41 #define BIGINT INT_MAX
43 #define BSTRNCMP(a,b) strncmp(a, b, strlen(b))
45 /* internal types and variables */
48 ssize_t fullread(int, void *, size_t);
49 ssize_t fullwrite(int, const void *, size_t);
51 int connect_portrange(struct sockaddr_storage *, in_port_t, in_port_t, char *,
52 struct sockaddr_storage *, int);
53 int bind_portrange(int, struct sockaddr_storage *, in_port_t, in_port_t,
56 char * construct_datestamp(time_t *t);
57 char * construct_timestamp(time_t *t);
59 /*@only@*//*@null@*/char *quote_string(const char *str);
60 /*@only@*//*@null@*/char *unquote_string(const char *str);
61 int needs_quotes(const char * str);
63 char * sanitize_string(const char *str);
64 int copy_file(char *dst, char *src, char **errmsg);
67 * validate_email return 0 if the following characters are present
68 * * ( ) < > [ ] , ; : ! $ \ / "
71 int validate_mailto(const char *mailto);
73 /* This function is a portable reimplementation of readdir(). It
74 * returns a newly-allocated string, that should be freed with
75 * free(). Returns NULL on error or end of directory.
76 * It is reentrant, with the following exceptions:
77 * - This function cannot be run at the same time as readdir() or
79 * - This function cannot be run simultaneously on the same directory
81 char * portable_readdir(DIR*);
83 typedef gboolean (*SearchDirectoryFunctor)(const char * filename,
85 /* This function will search the given directory handle for files
86 matching the given POSIX extended regular expression.
87 For each matching file, the functor will be called with the given
88 user data. Stops when the functor returns FALSE, or all files have
89 been searched. Returns the number of matching files. */
90 int search_directory(DIR * handle, const char * regex,
91 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);
100 void free_new_argv(int new_argc, char **new_argv);
102 /* Like strcmp(a, b), except that NULL strings are sorted before non-NULL
103 * strings, instead of segfaulting. */
104 int compare_possibly_null_strings(const char * a, const char * b);
106 /* Does g_thread_init(), along with anything else that should be done
107 * before/after thread setup. It's OK to call this function more than once.
108 * Returns TRUE if threads are supported. */
109 gboolean amanda_thread_init(void);
111 /* Given a hostname, call getaddrinfo to resolve it. Optionally get the
112 * entire set of results (if res is not NULL) and the canonical name of
113 * the host (if canonname is not NULL). The canonical name might
114 * expand e.g., www.domain.com to server3.webfarm.hosting.com.
116 * If not NULL, the caller is responsible for freeing res with freeaddrinfo().
117 * Similarly, the caller is responsible for freeing canonname if it is
120 * @param hostname: the hostname to start with
121 * @param res: (result) if not NULL, the results from getaddrinfo()
122 * @param canonname: (result) if not NULL, the canonical name of the host
123 * @returns: newly allocated canonical hostname, or NULL if no
124 * canonical hostname was available.
126 int resolve_hostname(const char *hostname, int socktype,
127 struct addrinfo **res, char **canonname);
129 /* Interpret a status (as returned from wait() and friends)
130 * into a human-readable sentence.
132 * Caller is responsible for freeing the resulting string.
133 * The resulting string has already been translated.
135 * The macro definition allows this to work even when amwait_t
136 * is 'union wait' (4.3BSD). The cast is safe because the two
137 * argument types are interchangeable.
139 * @param subject: subject of the sentence (program name, etc.)
140 * @param status: the exit status
141 * @returns: newly allocated string describing status
143 #define str_exit_status(subject, status) \
144 _str_exit_status((subject), *(amwait_t *)&(status))
145 char *_str_exit_status(char *subject, amwait_t status);
148 * Userid manipulation
151 /* Check that the current uid and euid are set to a specific user,
152 * calling error() if not. Does nothing if CHECK_USERID is not
155 * @param who: one of the RUNNING_AS_* constants, below.
161 /* userid belongs to dumpuser (from config) */
164 /* prefer that userid belongs to dumpuser, but accept when userid belongs to
165 * CLIENT_LOGIN with a debug-log message (needed because amandad always runs
166 * as CLIENT_LOGIN, even on server) */
167 RUNNING_AS_DUMPUSER_PREFERRED,
169 /* userid belongs to CLIENT_LOGIN (from --with-user) */
170 RUNNING_AS_CLIENT_LOGIN,
172 RUNNING_AS_USER_MASK = (1 << 8) - 1,
173 /* '&' this on to only check the uid, not the euid; use this for programs
174 * that will call become_root() */
175 RUNNING_AS_UID_ONLY = 1 << 8
178 void check_running_as(running_as_flags who);
180 /* Drop and regain root priviledges; used from setuid-root binaries which only
181 * need to be root for certain operations. Does nothing if SINGLE_USERID is
184 * @param need_root: if true, try to assume root priviledges; otherwise, drop
186 * @returns: true if the priviledge change succeeded
188 int set_root_privs(int need_root);
190 /* Become root completely, by setting the uid to 0. This is used by setuid-root
191 * apps which will exec subprocesses which will also need root priviledges. Does
192 * nothing if SINGLE_USERID is defined.
194 * @returns: true if the priviledge change succeeded
196 int become_root(void);
202 /* Set the name of the process. The parameter is copied, and remains
203 * the responsibility of the caller on return. This value is used in log
204 * messages and other output throughout Amanda.
206 * @param pname: the new process name
208 void set_pname(char *pname);
210 /* Get the current process name; the result is in a static buffer, and
211 * should *not* be free()d by the caller.
213 * @returns: process name
215 char *get_pname(void);
220 * This either includes the system readline header we found in configure,
221 * or prototypes some simple stub functions that are used instead.
225 # ifdef HAVE_READLINE_READLINE_H
226 # include <readline/readline.h>
227 # ifdef HAVE_READLINE_HISTORY_H
228 # include <readline/history.h>
231 # ifdef HAVE_READLINE_H
232 # include <readline.h>
233 # ifdef HAVE_HISTORY_H
234 # include <history.h>
240 char * readline(const char *prompt);
241 void add_history(const char *line);