OpenVPN 3 Core Library
Loading...
Searching...
No Matches
sanitize.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// Sanitize certain kinds of strings before they are output to the log file.
13
14#ifndef OPENVPN_OPTIONS_SANITIZE_H
15#define OPENVPN_OPTIONS_SANITIZE_H
16
17#include <string>
18#include <cstring>
19
22
23namespace openvpn {
24
25inline std::string render_options_sanitized(const OptionList &opt, const unsigned int render_flags)
26{
27 std::ostringstream out;
28 for (size_t i = 0; i < opt.size(); i++)
29 {
30 const Option &o = opt[i];
31#ifndef OPENVPN_SHOW_SESSION_TOKEN
32 if (o.get_optional(0, 0) == "auth-token")
33 out << i << " [auth-token] ..." << std::endl;
34 else
35#endif
36 out << i << ' ' << o.render(render_flags) << std::endl;
37 }
38 return out.str();
39}
40
41// Remove security-sensitive strings from control message
42// so that they will not be output to log file.
43inline std::string sanitize_control_message(const std::string &src_str)
44{
45#ifdef OPENVPN_SHOW_SESSION_TOKEN
46 return src_str;
47#else
48 const char *src = src_str.c_str();
49 char *ret = new char[src_str.length() + 1];
50 char *dest = ret;
51 bool redact = false;
52 int skip = 0;
53
54 for (;;)
55 {
56 const char c = *src;
57 if (c == '\0')
58 break;
59 if (c == 'S' && !::strncmp(src, "SESS_ID_", 8))
60 {
61 skip = 7;
62 redact = true;
63 }
64 else if (c == 'e' && !::strncmp(src, "echo ", 5))
65 {
66 skip = 4;
67 redact = true;
68 }
69
70 if (c == ',') /* end of redacted item? */
71 {
72 skip = 0;
73 redact = false;
74 }
75
76 if (redact)
77 {
78 if (skip > 0)
79 {
80 --skip;
81 *dest++ = c;
82 }
83 }
84 else
85 *dest++ = c;
86
87 ++src;
88 }
89 *dest = '\0';
90
91 const std::string ret_str(ret);
92 delete[] ret;
93 return ret_str;
94#endif
95}
96
97} // namespace openvpn
98
99#endif
std::string get_optional(const size_t index, const size_t max_len) const
Definition options.hpp:194
std::string render(const unsigned int flags) const
Definition options.hpp:264
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
std::string sanitize_control_message(const std::string &src_str)
Definition sanitize.hpp:43
std::string render_options_sanitized(const OptionList &opt, const unsigned int render_flags)
Definition sanitize.hpp:25
std::string ret
static std::stringstream out
Definition test_path.cpp:10