forked from AGWA/titus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.hpp
191 lines (167 loc) · 6.44 KB
/
util.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (C) 2014 Andrew Ayer
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name(s) of the above copyright
* holders shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization.
*/
#ifndef UTIL_HPP
#define UTIL_HPP
#include <string>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/evp.h>
#include <openssl/dh.h>
#include <openssl/ssl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <unistd.h>
#include <memory>
#include <map>
#include <tuple>
#include <type_traits>
#include <stdlib.h>
#include "filedesc.hpp"
struct System_error {
std::string syscall;
std::string target;
int number;
System_error (const std::string& arg_syscall, const std::string& arg_target, int arg_number)
: syscall(arg_syscall), target(arg_target), number(arg_number) { }
};
struct Openssl_error {
unsigned long code;
explicit Openssl_error (unsigned long arg_code) : code(arg_code) { }
std::string message () const { return message(code); }
static std::string message (unsigned long code)
{
char buf[120];
ERR_error_string_n(code, buf, sizeof(buf));
return buf;
}
};
struct Configuration_error {
std::string message;
explicit Configuration_error (const std::string& arg_message) : message(arg_message) { }
};
struct Key_protocol_error {
std::string message;
explicit Key_protocol_error (const std::string& arg_message) : message(arg_message) { }
};
struct openssl_deleter {
void operator() (X509* p) const { if (p) X509_free(p); }
void operator() (RSA* p) const { if (p) RSA_free(p); }
void operator() (EVP_PKEY* p) const { if (p) EVP_PKEY_free(p); }
void operator() (SSL* p) const { if (p) SSL_free(p); }
void operator() (SSL_CTX* p) const { if (p) SSL_CTX_free(p); }
void operator() (DH* p) const { if (p) DH_free(p); }
void operator() (EC_KEY* p) const { if (p) EC_KEY_free(p); }
};
template<class T> using openssl_unique_ptr = std::unique_ptr<T, openssl_deleter>;
struct cstdio_deleter {
void operator() (std::FILE* p) const { if (p) fclose(p); }
};
template<class T> using cstdio_unique_ptr = std::unique_ptr<T, cstdio_deleter>;
void set_nonblocking (int fd, bool nonblocking);
void set_transparent (int sock_fd);
void set_not_v6only (int sock_fd);
void set_reuseaddr (int sock_fd);
void disable_tracing ();
void drop_privileges (const std::string& chroot_directory, uid_t drop_uid, gid_t drop_gid);
void restrict_file_descriptors ();
void write_all (int fd, const void* data, size_t len);
bool read_all (int fd, void* data, size_t len);
void resolve_address (struct sockaddr_in6* address, const std::string& host, const std::string& port);
uid_t resolve_user (const std::string&);
gid_t resolve_group (const std::string&);
void daemonize ();
inline bool parse_config_bool (const char* str)
{
if (strcasecmp(str, "yes") == 0 ||
strcasecmp(str, "true") == 0 ||
strcasecmp(str, "on") == 0 ||
(std::isdigit(str[0]) && std::atoi(str) > 0)) {
return true;
} else if (strcasecmp(str, "no") == 0 ||
strcasecmp(str, "false") == 0 ||
strcasecmp(str, "off") == 0 ||
(std::isdigit(str[0]) && std::atoi(str) == 0)) {
return false;
}
throw Configuration_error(std::string("Invalid yes/no value `") + str + "'");
}
inline bool parse_config_bool (const std::string& str) { return parse_config_bool(str.c_str()); }
enum Transparency {
TRANSPARENT_OFF = 0,
TRANSPARENT_ON = 1,
TRANSPARENT_BACKEND_ONLY = 2
};
inline Transparency parse_config_transparency (const char* str)
{
if (strcasecmp(str, "backend-only") == 0) {
return TRANSPARENT_BACKEND_ONLY;
} else {
return parse_config_bool(str) ? TRANSPARENT_ON : TRANSPARENT_OFF;
}
}
inline Transparency parse_config_transparency (const std::string& str) { return parse_config_transparency(str.c_str()); }
filedesc make_unix_socket (const std::string& path, struct sockaddr_un* addr, socklen_t* addr_len);
filedesc make_unix_socket (const std::string& path);
std::string make_temp_directory ();
template<class... Formal, class... Arg> pid_t spawn (int (*main_function)(Formal...), Arg&&... arg)
{
pid_t pid = fork();
if (pid == -1) {
throw System_error("fork", "", errno);
}
if (pid == 0) {
try {
// Don't pass the result of main_function() directly to _exit(),
// or else the destructors for main_function's arguments won't be invoked
// (since _exit never returns).
const int status = main_function(std::forward<Arg>(arg)...);
_exit(status);
} catch (...) {
std::terminate();
}
}
// Take ownership of every argument to ensure that their destructors are called in
// the parent process if they were moved into this function.
std::tuple<Formal...>(std::forward<Arg>(arg)...);
return pid;
}
void set_ssl_options (SSL_CTX* ctx, const std::map<long, bool>& options);
openssl_unique_ptr<EC_KEY> get_ecdhcurve (const std::string& name);
inline const std::string& coalesce (const std::string& first, const std::string& second) { return !first.empty() ? first : second; }
template<class T> T* coalesce (T* first, T* second) { return first ? first : second; }
inline void chomp (std::string& str) { str.erase(str.find_last_not_of(" \t\r\n") + 1); } // NB: std::string::npos+1==0
bool ascii_streqi (const char*, const char*); // case-insensitive string comparison
#endif