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 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #elif defined(_MSC_VER)
29 #include "config-msvc.h"
30 #endif
31 
32 #include "syshead.h"
33 #include "console.h"
34 #include "error.h"
35 #include "buffer.h"
36 #include "misc.h"
37 
38 #ifdef ENABLE_SYSTEMD
39 #include <systemd/sd-daemon.h>
40 #endif
41 
42 
43 struct _query_user query_user[QUERY_USER_NUMSLOTS];  /* GLOBAL */
44 
45 
46 void
query_user_clear(void)47 query_user_clear(void)
48 {
49     int i;
50 
51     for (i = 0; i < QUERY_USER_NUMSLOTS; i++)
52     {
53         CLEAR(query_user[i]);
54     }
55 }
56 
57 
58 void
query_user_add(char * prompt,size_t prompt_len,char * resp,size_t resp_len,bool echo)59 query_user_add(char *prompt, size_t prompt_len,
60                char *resp, size_t resp_len,
61                bool echo)
62 {
63     int i;
64 
65     /* Ensure input is sane.  All these must be present otherwise it is
66      * a programming error.
67      */
68     ASSERT( prompt_len > 0 && prompt != NULL && resp_len > 0 && resp != NULL );
69 
70     /* Seek to the last unused slot */
71     for (i = 0; i < QUERY_USER_NUMSLOTS; i++)
72     {
73         if (query_user[i].prompt == NULL)
74         {
75             break;
76         }
77     }
78     ASSERT( i < QUERY_USER_NUMSLOTS );  /* Unlikely, but we want to panic if it happens */
79 
80     /* Save the information needed for the user interaction */
81     query_user[i].prompt = prompt;
82     query_user[i].prompt_len = prompt_len;
83     query_user[i].response = resp;
84     query_user[i].response_len = resp_len;
85     query_user[i].echo = echo;
86 }
87