OpenVPN 3 Core Library
Loading...
Searching...
No Matches
userpass.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_COMMON_USERPASS_H
13#define OPENVPN_COMMON_USERPASS_H
14
15#include <string>
16#include <vector>
17#include <utility>
18
24
26
27OPENVPN_EXCEPTION(creds_error);
28
30{
31 OPT_REQUIRED = (1 << 0),
32 OPT_OPTIONAL = (1 << 1),
33 USERNAME_REQUIRED = (1 << 2),
34 PASSWORD_REQUIRED = (1 << 3),
35 TRY_FILE = (1 << 4),
36};
37
74inline bool parse(const OptionList &options,
75 const std::string &opt_name,
76 const unsigned int flags,
77 std::vector<std::string> *user_pass)
78{
79 const Option *auth_user_pass = options.get_ptr(opt_name);
80 if (!auth_user_pass)
81 {
82 if (flags & OPT_REQUIRED)
83 throw creds_error(opt_name + " : credentials option missing");
84 return false;
85 }
86 if (auth_user_pass->size() == 1 && !(flags & OPT_REQUIRED))
87 return true;
88 if (auth_user_pass->size() != 2)
89 throw creds_error(opt_name + " : credentials option incorrectly specified");
90
91 std::string str = auth_user_pass->get(1, 1024 | Option::MULTILINE);
94 SplitLines in(str, 1024);
95 for (int i = 0; in(true) && i < 2; ++i)
96 {
97 if (user_pass)
98 user_pass->push_back(in.line_move());
99 }
100 return true;
101}
102
144inline void parse(const OptionList &options,
145 const std::string &opt_name,
146 const unsigned int flags,
147 std::string &user,
148 std::string &pass)
149{
150 user.clear();
151 pass.clear();
152 std::vector<std::string> up;
153 up.reserve(2);
154 if (!parse(options, opt_name, flags, &up) && (flags & OPT_OPTIONAL))
155 return;
156 if (up.size() >= 1)
157 {
158 user = std::move(up[0]);
159 if (up.size() >= 2)
160 pass = std::move(up[1]);
161 }
163 throw creds_error(opt_name + " : username empty");
165 throw creds_error(opt_name + " : password empty");
166}
167
186inline void parse_file(const std::string &path,
187 const unsigned int flags,
188 std::string &user,
189 std::string &pass)
190{
191 user.clear();
192 pass.clear();
193 const std::string str = read_text_utf8(path);
194 SplitLines in(str, 1024);
195 if (in(true))
196 {
197 user = in.line_move();
198 if (in(true))
199 pass = in.line_move();
200 }
202 throw creds_error(path + " : username empty");
204 throw creds_error(path + " : password empty");
205}
206
207} // namespace openvpn::UserPass
208
209#endif
const Option * get_ptr(const std::string &name) const
Definition options.hpp:1186
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:187
size_t size() const
Definition options.hpp:327
#define OPENVPN_EXCEPTION(C)
@ OPT_REQUIRED
option must be present
Definition userpass.hpp:31
@ PASSWORD_REQUIRED
password must be present
Definition userpass.hpp:34
@ USERNAME_REQUIRED
username must be present
Definition userpass.hpp:33
@ OPT_OPTIONAL
if option is not present, USERNAME_REQUIRED and PASSWORD_REQUIRED are ignored
Definition userpass.hpp:32
@ TRY_FILE
option argument might be a filename, try to load creds from it
Definition userpass.hpp:35
bool parse(const OptionList &options, const std::string &opt_name, const unsigned int flags, std::vector< std::string > *user_pass)
interpret user-pass option
Definition userpass.hpp:74
void parse_file(const std::string &path, const unsigned int flags, std::string &user, std::string &pass)
read username/password from file
Definition userpass.hpp:186
bool is_multiline(const std::string &str)
Definition string.hpp:255
bool is_empty(const std::string &str)
Definition string.hpp:448
std::string read_text_utf8(const std::string &filename, const std::uint64_t max_size=0)
Definition file.hpp:136
reroute_gw flags
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
const std::vector< std::string > user_pass