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) 2002-2022 OpenVPN Inc <sales@openvpn.net>
9  *  Copyright (C) 2014-2015 David Sommerseth <davids@redhat.com>
10  *  Copyright (C) 2016-2022 David Sommerseth <davids@openvpn.net>
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License version 2
14  *  as published by the Free Software Foundation.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #ifndef CONSOLE_H
27 #define CONSOLE_H
28 
29 #include "basic.h"
30 
31 /**
32  *  Configuration setup for declaring what kind of information to ask a user for
33  */
34 struct _query_user {
35     char *prompt;             /**< Prompt to present to the user */
36     size_t prompt_len;        /**< Length of the prompt string */
37     char *response;           /**< The user's response */
38     size_t response_len;      /**< Length the of the user response */
39     bool echo;                /**< True: The user should see what is being typed, otherwise mask it */
40 };
41 
42 #define QUERY_USER_NUMSLOTS 10
43 extern struct _query_user query_user[];  /**< Global variable, declared in console.c */
44 
45 /**
46  * Wipes all data put into all of the query_user structs
47  *
48  */
49 void query_user_clear(void);
50 
51 
52 /**
53  * Adds an item to ask the user for
54  *
55  * @param prompt     Prompt to display to the user
56  * @param prompt_len Length of the prompt string
57  * @param resp       String containing the user response
58  * @param resp_len   Length of the response string
59  * @param echo       Should the user input be echoed to the user?  If False, input will be masked
60  *
61  */
62 void query_user_add(char *prompt, size_t prompt_len,
63                     char *resp, size_t resp_len,
64                     bool echo);
65 
66 
67 /**
68  * Executes a configured setup, using the built-in method for querying the user.
69  * This method uses the console/TTY directly.
70  *
71  * @param setup    Pointer to the setup defining what to ask the user
72  *
73  * @return True if executing all the defined steps completed successfully
74  */
75 bool query_user_exec_builtin(void);
76 
77 
78 #if defined(ENABLE_SYSTEMD)
79 /**
80  * Executes a configured setup, using the compiled method for querying the user
81  *
82  * @param setup    Pointer to the setup defining what to ask the user
83  *
84  * @return True if executing all the defined steps completed successfully
85  */
86 bool query_user_exec(void);
87 
88 #else  /* ENABLE_SYSTEMD not defined*/
89 /**
90  * Wrapper function enabling query_user_exec() if no alternative methods have
91  * been enabled
92  *
93  */
94 static bool
query_user_exec(void)95 query_user_exec(void)
96 {
97     return query_user_exec_builtin();
98 }
99 #endif  /* defined(ENABLE_SYSTEMD) */
100 
101 
102 /**
103  * A plain "make Gert happy" wrapper.  Same arguments as @query_user_add
104  *
105  * FIXME/TODO: Remove this when refactoring the complete user query process
106  *             to be called at start-up initialization of OpenVPN.
107  *
108  */
109 static inline bool
query_user_SINGLE(char * prompt,size_t prompt_len,char * resp,size_t resp_len,bool echo)110 query_user_SINGLE(char *prompt, size_t prompt_len,
111                   char *resp, size_t resp_len,
112                   bool echo)
113 {
114     query_user_clear();
115     query_user_add(prompt, prompt_len, resp, resp_len, echo);
116     return query_user_exec();
117 }
118 
119 #endif /* ifndef CONSOLE_H */
120