-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacquire-method.cpp
140 lines (116 loc) · 2.99 KB
/
acquire-method.cpp
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
/*
* More information about the APT helpers:
* http://www.fifi.org/doc/libapt-pkg-doc/method.html/ch2.html
*/
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include "acquire-method.h"
#include "stanza.h"
static void print_capabilities()
{
std::cout << "100 Capabilities" << std::endl;
std::cout << "Version: 1.0" << std::endl;
std::cout << "Single-Instance: true" << std::endl;
std::cout << std::endl;
}
/*
* Checking of the messages is based on the code used in the official APT helpers:
* https://salsa.debian.org/apt-team/apt/-/blob/main/apt-pkg/acquire-method.cc#L89
*/
void AcquireMethod::send_message(const std::string &message,
const std::map<std::string, std::string> &fields)
{
auto checkKey = [](const std::string &s) {
return std::all_of(s.begin(), s.end(), [](unsigned char c) -> bool {
/* alphanumeric, hyphen or a space */
return std::isalnum(c) || c == '-' || c == ' ';
});
};
auto checkValue = [](const std::string &s) {
return std::all_of(s.begin(), s.end(), [](unsigned char c) -> bool {
/* unicode characters, printable characters or a tab */
return c > 127 || (c > 31 && c < 127) || c == '\t';
});
};
for (const auto &[key, val] : fields) {
if (!checkKey(key) || !checkValue(val)) {
send_message("400 URI Failure", {
{"URI", "<UNKNOWN>"},
{"Message", "SECURITY: Message contains control characters, rejecting."}
});
abort();
}
}
std::cout << message << std::endl;
for (const auto &[key, val] : fields) {
if (val.length() == 0)
continue;
std::cout << key << ": " << val << std::endl;
}
std::cout << std::endl;
}
void AcquireMethod::report_general_failure(const std::string &message)
{
send_message("401 General Failure", {{"Message", message}});
}
void AcquireMethod::report_uri_failure(const uri_exception &e)
{
send_message("400 URI Failure", {
{"URI", e.uri()},
{"FailReason", e.reason()},
{"Message", e.what()}
});
}
int AcquireMethod::acquire(std::istream &in)
{
Stanza stanza(in);
try {
fetch_file(stanza);
} catch (const uri_exception &e) {
report_uri_failure(e);
return 100;
}
send_message("201 URI Done", {
{"URI", stanza["URI"]}, {"Filename", stanza["Filename"]}
});
return 0;
}
int AcquireMethod::loop()
{
std::string line;
unsigned int code;
int retval = 0, ret;
print_capabilities();
config.load_from_file("/etc/apt/private.conf");
while (std::cin.good()) {
try {
getline(std::cin, line);
if (line.length() == 0) {
break;
}
code = std::stoi(line);
switch (code) {
case 600:
ret = acquire(std::cin);
if (ret != 0) {
retval = ret;
}
break;
default:
throw std::invalid_argument("Unexpected command.");
break;
}
} catch (const std::runtime_error &e) {
report_general_failure(e.what());
return -1;
} catch (const std::invalid_argument &e) {
report_general_failure(e.what());
return -1;
} catch (const std::exception &e) {
report_general_failure("Unexpected error.");
return -1;
}
}
return retval;
}