/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to dracut-module/password-agent.c

  • Committer: teddy at recompile
  • Date: 2020-07-04 13:39:36 UTC
  • mfrom: (237.4.132 release)
  • Revision ID: teddy@recompile.se-20200704133936-lfk4sy1d48m53iqu
Merge from release branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos password agent - Simple password agent to run Mandos client
4
4
 *
5
 
 * Copyright © 2019 Teddy Hogeborn
6
 
 * Copyright © 2019 Björn Påhlsson
 
5
 * Copyright © 2019-2020 Teddy Hogeborn
 
6
 * Copyright © 2019-2020 Björn Påhlsson
7
7
 * 
8
8
 * This file is part of Mandos.
9
9
 * 
48
48
#include <error.h>              /* error() */
49
49
#include <sysexits.h>           /* EX_USAGE, EX_OSERR, EX_OSFILE */
50
50
#include <errno.h>              /* errno, error_t, EACCES,
51
 
                                   ENAMETOOLONG, ENOENT, EEXIST,
52
 
                                   ECHILD, EPERM, ENOMEM, EAGAIN,
53
 
                                   EINTR, ENOBUFS, EADDRINUSE,
 
51
                                   ENAMETOOLONG, ENOENT, ENOTDIR,
 
52
                                   ENOMEM, EEXIST, ECHILD, EPERM,
 
53
                                   EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
54
54
                                   ECONNREFUSED, ECONNRESET,
55
55
                                   ETOOMANYREFS, EMSGSIZE, EBADF,
56
56
                                   EINVAL */
73
73
                                   ARGP_ERR_UNKNOWN, ARGP_KEY_ARGS,
74
74
                                   struct argp, argp_parse(),
75
75
                                   ARGP_NO_EXIT */
 
76
#include <stdint.h>             /* SIZE_MAX */
76
77
#include <unistd.h>             /* uid_t, gid_t, close(), pipe2(),
77
78
                                   fork(), _exit(), dup2(),
78
79
                                   STDOUT_FILENO, setresgid(),
84
85
#include <fcntl.h>              /* O_CLOEXEC, O_NONBLOCK, fcntl(),
85
86
                                   F_GETFD, F_GETFL, FD_CLOEXEC,
86
87
                                   open(), O_WRONLY, O_NOCTTY,
87
 
                                   O_RDONLY */
 
88
                                   O_RDONLY, O_NOFOLLOW */
88
89
#include <sys/wait.h>           /* waitpid(), WNOHANG, WIFEXITED(),
89
90
                                   WEXITSTATUS() */
90
91
#include <limits.h>             /* PIPE_BUF, NAME_MAX, INT_MAX */
91
92
#include <sys/inotify.h>        /* inotify_init1(), IN_NONBLOCK,
92
93
                                   IN_CLOEXEC, inotify_add_watch(),
93
94
                                   IN_CLOSE_WRITE, IN_MOVED_TO,
94
 
                                   IN_DELETE, struct inotify_event */
 
95
                                   IN_MOVED_FROM, IN_DELETE,
 
96
                                   IN_EXCL_UNLINK, IN_ONLYDIR,
 
97
                                   struct inotify_event */
95
98
#include <fnmatch.h>            /* fnmatch(), FNM_FILE_NAME */
96
 
#include <stdio.h>              /* asprintf(), FILE, fopen(),
97
 
                                   getline(), sscanf(), feof(),
98
 
                                   ferror(), fclose(), stderr,
99
 
                                   rename(), fdopen(), fprintf(),
100
 
                                   fscanf() */
 
99
#include <stdio.h>              /* asprintf(), FILE, stderr, fopen(),
 
100
                                   fclose(), getline(), sscanf(),
 
101
                                   feof(), ferror(), rename(),
 
102
                                   fdopen(), fprintf(), fscanf() */
101
103
#include <glib.h>    /* GKeyFile, g_key_file_free(), g_key_file_new(),
102
104
                        GError, g_key_file_load_from_file(),
103
105
                        G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
146
148
  mono_microsecs next_run;
147
149
} __attribute__((designated_init)) task_queue;
148
150
 
149
 
/* "func_type" - A function type for task functions
 
151
/* "task_func" - A function type for task functions
150
152
 
151
153
   I.e. functions for the code which runs when a task is run, all have
152
154
   this type */
649
651
 
650
652
__attribute__((nonnull, warn_unused_result))
651
653
bool add_to_queue(task_queue *const queue, const task_context task){
 
654
  if((queue->length + 1) > (SIZE_MAX / sizeof(task_context))){
 
655
    /* overflow */
 
656
    error(0, ENOMEM, "Failed to allocate %" PRIuMAX
 
657
          " tasks for queue->tasks", (uintmax_t)(queue->length + 1));
 
658
    errno = ENOMEM;
 
659
    return false;
 
660
  }
652
661
  const size_t needed_size = sizeof(task_context)*(queue->length + 1);
653
662
  if(needed_size > (queue->allocated)){
654
663
    task_context *const new_tasks = realloc(queue->tasks,
859
868
  }
860
869
  close(pipefds[1]);
861
870
 
 
871
  if(pid == -1){
 
872
    error(0, errno, "Failed to fork()");
 
873
    close(pipefds[0]);
 
874
    return false;
 
875
  }
 
876
 
862
877
  if(not add_to_queue(queue, (task_context){
863
878
        .func=wait_for_mandos_client_exit,
864
879
        .pid=pid,
1882
1897
  g_assert_true(queue->tasks[0].func == dummy_func);
1883
1898
}
1884
1899
 
 
1900
static void test_add_to_queue_overflow(__attribute__((unused))
 
1901
                                       test_fixture *fixture,
 
1902
                                       __attribute__((unused))
 
1903
                                       gconstpointer user_data){
 
1904
  __attribute__((cleanup(cleanup_queue)))
 
1905
    task_queue *queue = create_queue();
 
1906
  g_assert_nonnull(queue);
 
1907
  g_assert_true(queue->length == 0);
 
1908
  queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
 
1909
 
 
1910
  FILE *real_stderr = stderr;
 
1911
  FILE *devnull = fopen("/dev/null", "we");
 
1912
  g_assert_nonnull(devnull);
 
1913
  stderr = devnull;
 
1914
  const bool ret = add_to_queue(queue,
 
1915
                                (task_context){ .func=dummy_func });
 
1916
  g_assert_true(errno == ENOMEM);
 
1917
  g_assert_false(ret);
 
1918
  stderr = real_stderr;
 
1919
  g_assert_cmpint(fclose(devnull), ==, 0);
 
1920
  queue->length = 0;            /* Restore real size */
 
1921
}
 
1922
 
1885
1923
static void dummy_func(__attribute__((unused))
1886
1924
                       const task_context task,
1887
1925
                       __attribute__((unused))
2158
2196
    }
2159
2197
    exit(EXIT_SUCCESS);
2160
2198
  }
 
2199
  if(pid == -1){
 
2200
    error(EXIT_FAILURE, errno, "Failed to fork()");
 
2201
  }
 
2202
 
2161
2203
  int status;
2162
2204
  waitpid(pid, &status, 0);
2163
2205
  if(WIFEXITED(status) and (WEXITSTATUS(status) == EXIT_SUCCESS)){
5916
5958
                                           test_fixture *fixture,
5917
5959
                                           __attribute__((unused))
5918
5960
                                           gconstpointer user_data){
 
5961
#ifndef __amd64__
 
5962
  g_test_skip("Skipping EMSGSIZE test on non-AMD64 platform");
 
5963
#else
5919
5964
  __attribute__((cleanup(cleanup_close)))
5920
5965
    const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
5921
5966
  g_assert_cmpint(epoll_fd, >=, 0);
5974
6019
  g_assert_cmpuint((unsigned int)queue->length, ==, 0);
5975
6020
  g_assert_true(string_set_contains(cancelled_filenames,
5976
6021
                                    question_filename));
 
6022
#endif
5977
6023
}
5978
6024
 
5979
6025
static void test_send_password_to_socket_retry(__attribute__((unused))
7860
7906
  test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7861
7907
  test_add("/queue/create", test_create_queue);
7862
7908
  test_add("/queue/add", test_add_to_queue);
 
7909
  test_add("/queue/add/overflow", test_add_to_queue_overflow);
7863
7910
  test_add("/queue/has_question/empty",
7864
7911
           test_queue_has_question_empty);
7865
7912
  test_add("/queue/has_question/false",
8090
8137
  g_option_context_set_help_enabled(context, FALSE);
8091
8138
  g_option_context_set_ignore_unknown_options(context, TRUE);
8092
8139
 
8093
 
  gboolean run_tests = FALSE;
 
8140
  gboolean should_run_tests = FALSE;
8094
8141
  GOptionEntry entries[] = {
8095
8142
    { "test", 0, 0, G_OPTION_ARG_NONE,
8096
 
      &run_tests, "Run tests", NULL },
 
8143
      &should_run_tests, "Run tests", NULL },
8097
8144
    { NULL }
8098
8145
  };
8099
8146
  g_option_context_add_main_entries(context, entries, NULL);
8106
8153
  }
8107
8154
 
8108
8155
  g_option_context_free(context);
8109
 
  return run_tests != FALSE;
 
8156
  return should_run_tests != FALSE;
8110
8157
}