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
192
193
194
195
196
197
198
199
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "af-list.h"
#include "extract-word.h"
#include "ip-protocol-list.h"
#include "log.h"
#include "parse-helpers.h"
#include "parse-util.h"
#include "path-util.h"
#include "utf8.h"
int path_simplify_and_warn(
char *path,
unsigned flag,
const char *unit,
const char *filename,
unsigned line,
const char *lvalue) {
bool fatal = flag & PATH_CHECK_FATAL;
assert(!FLAGS_SET(flag, PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
if (!utf8_is_valid(path))
return log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path);
if (flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) {
bool absolute;
absolute = path_is_absolute(path);
if (!absolute && (flag & PATH_CHECK_ABSOLUTE))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
"%s= path is not absolute%s: %s",
lvalue, fatal ? "" : ", ignoring", path);
if (absolute && (flag & PATH_CHECK_RELATIVE))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
"%s= path is absolute%s: %s",
lvalue, fatal ? "" : ", ignoring", path);
}
path_simplify(path);
if (!path_is_valid(path))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
"%s= path has invalid length (%zu bytes)%s.",
lvalue, strlen(path), fatal ? "" : ", ignoring");
if (!path_is_normalized(path))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
"%s= path is not normalized%s: %s",
lvalue, fatal ? "" : ", ignoring", path);
return 0;
}
static int parse_af_token(
const char *token,
int *family,
int *ip_protocol,
uint16_t *nr_ports,
uint16_t *port_min) {
int af;
assert(token);
assert(family);
af = af_from_ipv4_ipv6(token);
if (af == AF_UNSPEC)
return -EINVAL;
*family = af;
return 0;
}
static int parse_ip_protocol_token(
const char *token,
int *family,
int *ip_protocol,
uint16_t *nr_ports,
uint16_t *port_min) {
int proto;
assert(token);
assert(ip_protocol);
proto = ip_protocol_from_tcp_udp(token);
if (proto < 0)
return -EINVAL;
*ip_protocol = proto;
return 0;
}
static int parse_ip_ports_token(
const char *token,
int *family,
int *ip_protocol,
uint16_t *nr_ports,
uint16_t *port_min) {
assert(token);
assert(nr_ports);
assert(port_min);
if (streq(token, "any"))
*nr_ports = *port_min = 0;
else {
uint16_t mn = 0, mx = 0;
int r = parse_ip_port_range(token, &mn, &mx);
if (r < 0)
return r;
*nr_ports = mx - mn + 1;
*port_min = mn;
}
return 0;
}
typedef int (*parse_token_f)(
const char *,
int *,
int *,
uint16_t *,
uint16_t *);
int parse_socket_bind_item(
const char *str,
int *address_family,
int *ip_protocol,
uint16_t *nr_ports,
uint16_t *port_min) {
/* Order of token parsers is important. */
const parse_token_f parsers[] = {
&parse_af_token,
&parse_ip_protocol_token,
&parse_ip_ports_token,
};
parse_token_f const *parser_ptr = parsers;
int af = AF_UNSPEC, proto = 0, r;
uint16_t nr = 0, mn = 0;
const char *p = str;
assert(str);
assert(address_family);
assert(ip_protocol);
assert(nr_ports);
assert(port_min);
if (isempty(p))
return -EINVAL;
for (;;) {
_cleanup_free_ char *token = NULL;
r = extract_first_word(&p, &token, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
if (r == 0)
break;
if (r < 0)
return r;
if (isempty(token))
return -EINVAL;
while (parser_ptr != parsers + ELEMENTSOF(parsers)) {
r = (*parser_ptr)(token, &af, &proto, &nr, &mn);
if (r == -ENOMEM)
return r;
++parser_ptr;
/* Continue to next token if parsing succeeded,
* otherwise apply next parser to the same token.
*/
if (r >= 0)
break;
}
if (parser_ptr == parsers + ELEMENTSOF(parsers))
break;
}
/* Failed to parse a token. */
if (r < 0)
return r;
/* Parsers applied successfully, but end of the string not reached. */
if (p)
return -EINVAL;
*address_family = af;
*ip_protocol = proto;
*nr_ports = nr;
*port_min = mn;
return 0;
}
|