+#ifdef USE_READDIR64
+ entry_p = readdir64(handle);
+#endif
+
+ g_static_mutex_unlock(&mutex);
+
+ if (entry_p == NULL)
+ return NULL;
+
+ /* FIXME: According to glibc documentation, d_name may not be
+ null-terminated in some cases on some very old platforms. Not
+ sure what to do about that case. */
+ return strdup(entry_p->d_name);
+}
+#if (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 31))
+# pragma GCC diagnostic pop
+#endif
+
+int search_directory(DIR * handle, const char * regex,
+ SearchDirectoryFunctor functor, gpointer user_data) {
+ int rval = 0;
+ regex_t compiled_regex;
+ gboolean done = FALSE;
+
+ if (regcomp(&compiled_regex, regex, REG_EXTENDED | REG_NOSUB) != 0) {
+ regfree(&compiled_regex);
+ return -1;
+ }
+
+ rewinddir(handle);
+
+ while (!done) {
+ char * read_name;
+ int result;
+ read_name = portable_readdir(handle);
+ if (read_name == NULL) {
+ regfree(&compiled_regex);
+ return rval;
+ }
+ result = regexec(&compiled_regex, read_name, 0, NULL, 0);
+ if (result == 0) {
+ rval ++;
+ done = !functor(read_name, user_data);
+ }
+ amfree(read_name);
+ }
+ regfree(&compiled_regex);
+ return rval;
+}
+
+char* find_regex_substring(const char* base_string, const regmatch_t match) {
+ char * rval;
+ int size;
+
+ size = match.rm_eo - match.rm_so;
+ rval = malloc(size+1);
+ memcpy(rval, base_string + match.rm_so, size);
+ rval[size] = '\0';
+
+ return rval;
+}
+
+int compare_possibly_null_strings(const char * a, const char * b) {
+ if (a == b) {
+ /* NULL or otherwise, they're the same. */
+ return 0;
+ } else if (a == NULL) {
+ /* b != NULL */
+ return -1;
+ } else if (b == NULL) {
+ /* a != NULL */
+ return 1;
+ } else {
+ /* a != NULL != b */
+ return strcmp(a, b);
+ }
+}
+
+int
+resolve_hostname(const char *hostname,
+ int socktype,
+ struct addrinfo **res,
+ char **canonname)
+{
+ struct addrinfo hints;
+ struct addrinfo *myres;
+ int flags = 0;
+ int result;
+
+ if (res) *res = NULL;
+ if (canonname) {
+ *canonname = NULL;
+ flags = AI_CANONNAME;
+ }
+
+#ifdef AI_ADDRCONFIG
+ flags |= AI_ADDRCONFIG;
+#endif
+
+ memset(&hints, 0, sizeof(hints));
+#ifdef WORKING_IPV6
+ /* get any kind of addresss */
+ hints.ai_family = AF_UNSPEC;
+#else
+ /* even if getaddrinfo supports IPv6, don't let it return
+ * such an address */
+ hints.ai_family = AF_INET;
+#endif
+ hints.ai_flags = flags;
+ hints.ai_socktype = socktype;
+ result = getaddrinfo(hostname, NULL, &hints, &myres);
+ if (result != 0) {
+ return result;
+ }
+
+ if (canonname && myres && myres->ai_canonname) {
+ *canonname = stralloc(myres->ai_canonname);
+ }
+
+ if (res) {
+ *res = myres;
+ } else {
+ freeaddrinfo(myres);
+ }
+
+ return result;
+}
+
+char *
+_str_exit_status(
+ char *subject,
+ amwait_t status)
+{
+ if (WIFEXITED(status)) {
+ int exitstatus = WEXITSTATUS(status);
+ if (exitstatus == 0)
+ return vstrallocf(_("%s exited normally"), subject);
+ else
+ return vstrallocf(_("%s exited with status %d"), subject, exitstatus);
+ }
+
+ if (WIFSIGNALED(status)) {
+ int signal = WTERMSIG(status);
+#ifdef WCOREDUMP
+ if (WCOREDUMP(status))
+ return vstrallocf(_("%s exited after receiving signal %d (core dumped)"),
+ subject, signal);
+ else
+#endif
+ return vstrallocf(_("%s exited after receiving signal %d"),
+ subject, signal);
+ }
+
+ if (WIFSTOPPED(status)) {
+ int signal = WSTOPSIG(status);
+ return vstrallocf(_("%s stopped temporarily after receiving signal %d"),
+ subject, signal);
+ }
+
+#ifdef WIFCONTINUED
+ if (WIFCONTINUED(status)) {
+ return vstrallocf(_("%s was resumed"), subject);
+ }
+#endif
+
+ return vstrallocf(_("%s exited in unknown circumstances"), subject);
+}
+
+void
+check_running_as(running_as_flags who)
+{
+#ifdef CHECK_USERID
+ struct passwd *pw;
+ uid_t uid_me;
+ uid_t uid_target;
+ char *uname_me = NULL;
+ char *uname_target = NULL;
+ char *dumpuser;
+
+ uid_me = getuid();
+ if ((pw = getpwuid(uid_me)) == NULL) {
+ error(_("current userid %ld not found in password database"), (long)uid_me);
+ /* NOTREACHED */
+ }
+ uname_me = stralloc(pw->pw_name);
+
+#ifndef SINGLE_USERID
+ if (!(who & RUNNING_AS_UID_ONLY) && uid_me != geteuid()) {
+ error(_("euid (%lld) does not match uid (%lld); is this program setuid-root when it shouldn't be?"),
+ (long long int)geteuid(), (long long int)uid_me);
+ /* NOTREACHED */
+ }
+#endif
+
+ switch (who & RUNNING_AS_USER_MASK) {
+ case RUNNING_AS_ANY:
+ uid_target = uid_me;
+ uname_target = uname_me;
+ amfree(uname_me);
+ return;
+
+ case RUNNING_AS_ROOT:
+ uid_target = 0;
+ uname_target = "root";
+ break;
+
+ case RUNNING_AS_DUMPUSER_PREFERRED:
+ dumpuser = getconf_str(CNF_DUMPUSER);
+ if ((pw = getpwnam(dumpuser)) != NULL &&
+ uid_me != pw->pw_uid) {
+ if ((pw = getpwnam(CLIENT_LOGIN)) != NULL &&
+ uid_me == pw->pw_uid) {
+ /* uid == CLIENT_LOGIN: not ideal, but OK */
+ dbprintf(_("NOTE: running as '%s', which is the client"
+ " user, not the dumpuser ('%s'); forging"
+ " on anyway\n"),
+ CLIENT_LOGIN, dumpuser);
+ uid_target = uid_me; /* force success below */
+ break;
+ }
+ }
+ /* FALLTHROUGH */
+
+ case RUNNING_AS_DUMPUSER:
+ uname_target = getconf_str(CNF_DUMPUSER);
+ if ((pw = getpwnam(uname_target)) == NULL) {
+ error(_("cannot look up dumpuser \"%s\""), uname_target);
+ /*NOTREACHED*/
+ }
+ uid_target = pw->pw_uid;
+ break;
+
+ case RUNNING_AS_CLIENT_LOGIN:
+ uname_target = CLIENT_LOGIN;
+ if ((pw = getpwnam(uname_target)) == NULL) {
+ error(_("cannot look up client user \"%s\""), uname_target);
+ /*NOTREACHED*/
+ }
+ uid_target = pw->pw_uid;
+ break;
+
+ default:
+ error(_("Unknown check_running_as() call"));
+ /* NOTREACHED */
+ }
+
+ if (uid_me != uid_target) {
+ error(_("running as user \"%s\" instead of \"%s\""), uname_me, uname_target);
+ /*NOTREACHED*/
+ }
+ amfree(uname_me);
+
+#else
+ /* Quiet unused variable warning */
+ (void)who;
+#endif
+}
+
+int
+set_root_privs(int need_root)
+{
+#ifndef SINGLE_USERID
+ static gboolean first_call = TRUE;
+ static uid_t unpriv = 1;
+
+ if (first_call) {
+ /* save the original real userid (that of our invoker) */
+ unpriv = getuid();
+
+ /* and set all of our userids (including, importantly, the saved
+ * userid) to 0 */
+ setuid(0);
+
+ /* don't need to do this next time */
+ first_call = FALSE;
+ }
+
+ if (need_root == 1) {
+ if (geteuid() == 0) return 1; /* already done */
+
+ if (seteuid(0) == -1) return 0;
+ /* (we don't switch the group back) */
+ } else if (need_root == -1) {
+ /* make sure the euid is 0 so that we can set the uid */
+ if (geteuid() != 0) {
+ if (seteuid(0) == -1) return 0;
+ }
+
+ /* now set the uid to the unprivileged userid */
+ if (setuid(unpriv) == -1) return 0;
+ } else {
+ if (geteuid() != 0) return 1; /* already done */
+
+ /* set the *effective* userid only */
+ if (seteuid(unpriv) == -1) return 0;
+ if (setegid(getgid()) == -1) return 0;
+ }
+#else
+ (void)need_root; /* Quiet unused variable warning */
+#endif
+ return 1;
+}
+
+int
+become_root(void)
+{
+#ifndef SINGLE_USERID
+ /* first, set the effective userid to 0 */
+ if (seteuid(0) == -1) return 0;
+
+ /* then, set all of the userids to 0 */
+ if (setuid(0) == -1) return 0;
+#endif
+ return 1;
+}
+
+char *
+base64_decode_alloc_string(
+ char *in)
+{
+ char *out;
+ size_t in_len = strlen(in);
+ size_t out_len = 3 * (in_len / 4) + 3;
+
+ out = malloc(out_len);
+ if (!base64_decode(in, in_len, out, &out_len)) {
+ amfree(out);
+ return NULL;
+ }
+ out[out_len] = '\0';
+
+ return out;
+}
+
+
+/* A GHFunc (callback for g_hash_table_foreach),
+ * Store a property and it's value in an ARGV.
+ *
+ * @param key_p: (char *) property name.
+ * @param value_p: (GSList *) property values list.
+ * @param user_data_p: (char ***) pointer to ARGV.
+ */
+static void
+proplist_add_to_argv(
+ gpointer key_p,
+ gpointer value_p,
+ gpointer user_data_p)
+{
+ char *property_s = key_p;
+ property_t *value_s = value_p;
+ GPtrArray *argv_ptr = user_data_p;
+ GSList *value;
+ char *q, *w, *qprop;
+
+ q = stralloc(property_s);
+ /* convert to lower case */
+ for (w=q; *w != '\0'; w++) {
+ *w = tolower(*w);
+ if (*w == '_')
+ *w = '-';
+ }
+ qprop = stralloc2("--", q);
+ amfree(q);
+ for(value=value_s->values; value != NULL; value = value->next) {
+ g_ptr_array_add(argv_ptr, stralloc(qprop));
+ g_ptr_array_add(argv_ptr, stralloc((char *)value->data));
+ }
+ amfree(qprop);
+}
+
+void
+property_add_to_argv(
+ GPtrArray *argv_ptr,
+ GHashTable *proplist)
+{
+ g_hash_table_foreach(proplist, &proplist_add_to_argv, argv_ptr);
+}
+
+
+/*
+ * Process parameters
+ */
+
+static char *pname = NULL;
+static char *ptype = NULL;
+static pcontext_t pcontext = CONTEXT_DEFAULT;
+
+void
+set_pname(char *p)
+{
+ pname = newstralloc(pname, p);
+}
+
+char *
+get_pname(void)
+{
+ if (!pname) pname = stralloc("unknown");
+ return pname;
+}
+
+void
+set_ptype(char *p)
+{
+ ptype = newstralloc(ptype, p);
+}
+
+char *
+get_ptype(void)
+{
+ if (!ptype) ptype = stralloc("unknown");
+ return ptype;
+}
+
+void
+set_pcontext(pcontext_t pc)
+{
+ pcontext = pc;
+}
+
+pcontext_t
+get_pcontext(void)
+{
+ return pcontext;
+}
+
+#ifdef __OpenBSD__
+void
+openbsd_fd_inform(void)
+{
+ int i;
+ for (i = DATA_FD_OFFSET; i < DATA_FD_OFFSET + DATA_FD_COUNT*2; i++) {
+ /* a simple fcntl() will cause the library to "look" at this file
+ * descriptor, which is good enough */
+ (void)fcntl(i, F_GETFL);
+ }
+}
+#endif
+
+void
+debug_executing(
+ GPtrArray *argv_ptr)
+{
+ guint i;
+ char *cmdline = stralloc((char *)g_ptr_array_index(argv_ptr, 0));
+
+ for (i = 1; i < argv_ptr->len-1; i++) {
+ char *arg = g_shell_quote((char *)g_ptr_array_index(argv_ptr, i));
+ cmdline = vstrextend(&cmdline, " ", arg, NULL);
+ amfree(arg);
+ }
+ g_debug("Executing: %s\n", cmdline);
+ amfree(cmdline);
+}
+
+char *
+get_first_line(
+ GPtrArray *argv_ptr)
+{
+ char *output_string = NULL;
+ int inpipe[2], outpipe[2], errpipe[2];
+ int pid;
+ FILE *out, *err;
+
+ assert(argv_ptr != NULL);
+ assert(argv_ptr->pdata != NULL);
+ assert(argv_ptr->len >= 1);
+
+ if (pipe(inpipe) == -1) {
+ error(_("error [open pipe: %s]"), strerror(errno));
+ /*NOTREACHED*/
+ }
+ if (pipe(outpipe) == -1) {
+ error(_("error [open pipe: %s]"), strerror(errno));
+ /*NOTREACHED*/
+ }
+ if (pipe(errpipe) == -1) {
+ error(_("error [open pipe: %s]"), strerror(errno));
+ /*NOTREACHED*/
+ }
+
+ fflush(stdout);
+ switch(pid = fork()) {
+ case -1:
+ error(_("error [fork: %s]"), strerror(errno));
+ /*NOTREACHED*/
+
+ default: /* parent process */
+ aclose(inpipe[0]);
+ aclose(outpipe[1]);
+ aclose(errpipe[1]);
+ break;
+
+ case 0: /* child process */
+ aclose(inpipe[1]);
+ aclose(outpipe[0]);
+ aclose(errpipe[0]);
+
+ dup2(inpipe[0], 0);
+ dup2(outpipe[1], 1);
+ dup2(errpipe[1], 2);
+
+ debug_executing(argv_ptr);
+ g_fprintf(stdout, "unknown\n");
+ execv((char *)*argv_ptr->pdata, (char **)argv_ptr->pdata);
+ error(_("error [exec %s: %s]"), (char *)*argv_ptr->pdata, strerror(errno));
+ }
+
+ aclose(inpipe[1]);
+
+ out = fdopen(outpipe[0],"r");
+ err = fdopen(errpipe[0],"r");
+
+ if (out) {
+ output_string = agets(out);
+ fclose(out);
+ }
+
+ if (err) {
+ if (!output_string)
+ output_string = agets(err);
+ fclose(err);
+ }
+
+ waitpid(pid, NULL, 0);
+
+ return output_string;
+}
+
+gboolean
+make_amanda_tmpdir(void)
+{
+ struct stat stat_buf;
+
+ if (stat(AMANDA_TMPDIR, &stat_buf) != 0) {
+ if (errno != ENOENT) {
+ g_debug("Error doing a stat of AMANDA_TMPDIR (%s): %s", AMANDA_TMPDIR, strerror(errno));
+ return FALSE;
+ }
+ /* create it */
+ if (mkdir(AMANDA_TMPDIR,0700) != 0) {
+ g_debug("Error mkdir of AMANDA_TMPDIR (%s): %s", AMANDA_TMPDIR, strerror(errno));
+ return FALSE;
+ }
+ if (chown(AMANDA_TMPDIR, (int)get_client_uid(), (int)get_client_gid()) < 0) {
+ g_debug("Error chown of AMANDA_TMPDIR (%s): %s", AMANDA_TMPDIR, strerror(errno));
+ return FALSE;
+ }
+ return TRUE;
+ } else {
+ /* check permission */
+ return TRUE;
+ }
+}