OpenVPN
console_systemd.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2014-2015 David Sommerseth <davids@redhat.com>
9 * Copyright (C) 2016 David Sommerseth <dazo@privateinternetaccess.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
31#include "config.h"
32
33#ifdef ENABLE_SYSTEMD
34#include "syshead.h"
35#include "console.h"
36#include "misc.h"
37#include "run_command.h"
38
39#include <systemd/sd-daemon.h>
40
41/*
42 * is systemd running
43 */
44
45static bool
46check_systemd_running(void)
47{
48 struct stat c;
49
50 /* We simply test whether the systemd cgroup hierarchy is
51 * mounted, as well as the systemd-ask-password executable
52 * being available */
53
54 return (sd_booted() > 0)
55 && (stat(SYSTEMD_ASK_PASSWORD_PATH, &c) == 0);
56
57}
58
59static bool
60get_console_input_systemd(const char *prompt, const bool echo, char *input, const int capacity)
61{
62 int std_out;
63 bool ret = false;
64 struct argv argv = argv_new();
65
66 argv_printf(&argv, SYSTEMD_ASK_PASSWORD_PATH);
67#ifdef SYSTEMD_NEWER_THAN_216
68 /* the --echo support arrived in upstream systemd 217 */
69 if (echo)
70 {
71 argv_printf_cat(&argv, "--echo");
72 }
73#endif
74 argv_printf_cat(&argv, "--icon network-vpn");
75 argv_printf_cat(&argv, "--timeout=0");
76 argv_printf_cat(&argv, "%s", prompt);
77
78 if ((std_out = openvpn_popen(&argv, NULL)) < 0)
79 {
80 return false;
81 }
82 memset(input, 0, capacity);
83 if (read(std_out, input, capacity-1) != 0)
84 {
85 chomp(input);
86 ret = true;
87 }
88 close(std_out);
89
91
92 return ret;
93}
94
100bool
101query_user_exec_systemd(void)
102{
103 bool ret = true; /* Presume everything goes okay */
104 int i;
105
106 /* If systemd is not available, use the default built-in mechanism */
107 if (!check_systemd_running())
108 {
110 }
111
112 /* Loop through the complete query setup and when needed, collect the information */
113 for (i = 0; i < QUERY_USER_NUMSLOTS && query_user[i].response != NULL; i++)
114 {
115 if (!get_console_input_systemd(query_user[i].prompt, query_user[i].echo,
116 query_user[i].response, query_user[i].response_len) )
117 {
118 /* Force the final result state to failed on failure */
119 ret = false;
120 }
121 }
122
123 return ret;
124}
125
126#endif /* ENABLE_SYSTEMD */
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:102
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:440
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:464
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:88
void chomp(char *str)
Definition buffer.c:614
struct _query_user query_user[QUERY_USER_NUMSLOTS]
Global variable, declared in console.c.
Definition console.c:41
bool query_user_exec_builtin(void)
Loop through configured query_user slots, using the built-in method for querying the user.
#define QUERY_USER_NUMSLOTS
Definition console.h:42
@ read
int openvpn_popen(const struct argv *a, const struct env_set *es)
char * response
The user's response.
Definition console.h:37
Definition argv.h:35
size_t capacity
Definition argv.h:37