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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* ISC license. */
#include <sys/types.h>
#include <limits.h>
#include <skalibs/types.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr.h>
#include <skalibs/exec.h>
#include <s6/config.h>
#include <s6-networking/config.h>
#define USAGE "s6-tlsserver [ -e ] [ options ] ip port prog...\n" \
"s6-tcpserver options: [ -q | -Q | -v ] [ -4 | -6 ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gidlist ] [ -g gid ] [ -u uid ] [ -U ]\n" \
"s6-tcpserver-access options: [ -W | -w ] [ -D | -d ] [ -H | -h ] [ -R | -r ] [ -P | -p ] [ -l localname ] [ -B banner ] [ -t timeout ] [ -i rulesdir | -x rulesfile ]\n" \
"s6-tlsd options: [ -S | -s ] [ -Y | -y ] [ -K timeout ] [ -Z | -z ] [ -k snilevel ]"
#define dieusage() strerr_dieusage(100, USAGE)
typedef struct options_s options_t, *options_t_ref ;
struct options_s
{
char const *localname ;
char const *banner ;
char const *rules ;
gid_t gids[NGROUPS_MAX] ;
size_t gidn ;
uid_t uid ;
gid_t gid ;
unsigned int maxconn ;
unsigned int localmaxconn ;
unsigned int backlog ;
unsigned int timeout ;
unsigned int kimeout ;
unsigned int snilevel ;
unsigned int verbosity : 2 ;
unsigned int flag46 : 2 ;
unsigned int flag1 : 1 ;
unsigned int flagU : 1 ;
unsigned int flagw : 1 ;
unsigned int flagD : 1 ;
unsigned int flagH : 1 ;
unsigned int flagr : 1 ;
unsigned int flagp : 1 ;
unsigned int ruleswhat : 2 ;
unsigned int flagS : 1 ;
unsigned int flagy : 1 ;
unsigned int flagY : 1 ;
unsigned int flagZ : 1 ;
unsigned int onlyvars : 1 ;
unsigned int doaccess : 1 ;
unsigned int doapply : 1 ;
} ;
#define OPTIONS_ZERO \
{ \
.localname = 0, \
.banner = 0, \
.rules = 0, \
.backlog = (unsigned int)-1, \
.gidn = (size_t)-1, \
.uid = 0, \
.gid = 0, \
.maxconn = 0, \
.localmaxconn = 0, \
.timeout = 0, \
.kimeout = 0, \
.verbosity = 1, \
.snilevel = 0, \
.flag46 = 0, \
.flag1 = 0, \
.flagU = 0, \
.flagw = 0, \
.flagD = 0, \
.flagH = 0, \
.flagr = 0, \
.flagp = 0, \
.ruleswhat = 0, \
.flagS = 0, \
.flagy = 0, \
.flagY = 0, \
.flagZ = 0, \
.onlyvars = 0, \
.doaccess = 0, \
.doapply = 0 \
}
int main (int argc, char const *const *argv)
{
options_t o = OPTIONS_ZERO ;
PROG = "s6-tlsserver" ;
{
subgetopt l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "qQv461c:C:b:G:g:u:UWwDdHhRrPpl:eB:t:i:x:SsYyK:Zzk:", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'q' : o.verbosity = 0 ; break ;
case 'Q' : o.verbosity = 1 ; break ;
case 'v' : o.verbosity = 2 ; break ;
case '4' : o.flag46 = 1 ; break ;
case '6' : o.flag46 = 2 ; break ;
case '1' : o.flag1 = 1 ; break ;
case 'c' : if (!uint0_scan(l.arg, &o.maxconn)) dieusage() ; if (!o.maxconn) o.maxconn = 1 ; break ;
case 'C' : if (!uint0_scan(l.arg, &o.localmaxconn)) dieusage() ; if (!o.localmaxconn) o.localmaxconn = 1 ; break ;
case 'b' : if (!uint0_scan(l.arg, &o.backlog)) dieusage() ; break ;
case 'G' : if (!gid_scanlist(o.gids, NGROUPS_MAX, l.arg, &o.gidn) && *l.arg) dieusage() ; o.doapply = 1 ; break ;
case 'g' : if (!gid0_scan(l.arg, &o.gid)) dieusage() ; o.doapply = 1 ; break ;
case 'u' : if (!uid0_scan(l.arg, &o.uid)) dieusage() ; o.doapply = 1 ; break ;
case 'U' : o.flagU = 1 ; o.uid = 0 ; o.gid = 0 ; o.gidn = (size_t)-1 ; o.doapply = 1 ; break ;
case 'W' : o.flagw = 0 ; break ;
case 'w' : o.flagw = 1 ; break ;
case 'D' : o.flagD = 1 ; o.doaccess = 1 ; break ;
case 'd' : o.flagD = 0 ; break ;
case 'H' : o.flagH = 1 ; o.doaccess = 1 ; break ;
case 'h' : o.flagH = 0 ; break ;
case 'R' : o.flagr = 0 ; break ;
case 'r' : o.flagr = 1 ; o.doaccess = 1 ; break ;
case 'P' : o.flagp = 0 ; break ;
case 'p' : o.flagp = 1 ; o.doaccess = 1 ; break ;
case 'l' : o.localname = l.arg ; o.doaccess = 1 ; break ;
case 'e' : o.onlyvars = 1 ; o.doaccess = 1 ; break ;
case 'B' : o.banner = l.arg ; o.doaccess = 1 ; break ;
case 't' : if (!uint0_scan(l.arg, &o.timeout)) dieusage() ; break ;
case 'i' : o.rules = l.arg ; o.ruleswhat = 1 ; o.doaccess = 1 ; break ;
case 'x' : o.rules = l.arg ; o.ruleswhat = 2 ; o.doaccess = 1 ; break ;
case 'S' : o.flagS = 1 ; break ;
case 's' : o.flagS = 0 ; break ;
case 'Y' : o.flagY = 1 ; o.flagy = 0 ; break ;
case 'y' : o.flagy = 1 ; o.flagY = 0 ; break ;
case 'K' : if (!uint0_scan(l.arg, &o.kimeout)) dieusage() ; break ;
case 'Z' : o.flagZ = 1 ; break ;
case 'z' : o.flagZ = 0 ; break ;
case 'k' : if (!uint0_scan(l.arg, &o.snilevel)) dieusage() ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
if (argc < 3) dieusage() ;
}
{
size_t pos = 0 ;
unsigned int m = 0 ;
char fmt[UINT_FMT * 5 + UID_FMT + GID_FMT * (NGROUPS_MAX + 1)] ;
char const *newargv[46 + argc] ;
newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver" ;
if (o.verbosity != 1) newargv[m++] = o.verbosity ? "-v" : "-q" ;
if (o.flag46) newargv[m++] = o.flag46 == 1 ? "-4" : "-6" ;
if (o.flag1) newargv[m++] = "-1" ;
if (o.maxconn)
{
newargv[m++] = "-c" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, o.maxconn) ;
fmt[pos++] = 0 ;
}
if (o.localmaxconn)
{
newargv[m++] = "-C" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, o.localmaxconn) ;
fmt[pos++] = 0 ;
}
if (o.backlog != (unsigned int)-1)
{
newargv[m++] = "-b" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, o.backlog) ;
fmt[pos++] = 0 ;
}
newargv[m++] = "--" ;
newargv[m++] = *argv++ ;
newargv[m++] = *argv++ ;
if (o.doaccess)
{
newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver-access" ;
if (o.verbosity)
{
if (o.verbosity > 1 && (!o.onlyvars || o.ruleswhat))
newargv[m++] = "-v2" ;
}
else newargv[m++] = "-v0" ;
if (o.flagw) newargv[m++] = "-w" ;
if (o.flagD) newargv[m++] = "-D" ;
if (o.flagH) newargv[m++] = "-H" ;
if (o.flagr) newargv[m++] = "-r" ;
if (o.flagp) newargv[m++] = "-p" ;
if (o.localname)
{
newargv[m++] = "-l" ;
newargv[m++] = o.localname ;
}
if (o.banner)
{
newargv[m++] = "-B" ;
newargv[m++] = o.banner ;
}
if (o.timeout)
{
newargv[m++] = "-t" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, o.timeout) ;
fmt[pos++] = 0 ;
}
if (o.ruleswhat)
{
newargv[m++] = o.ruleswhat == 1 ? "-i" : "-x" ;
newargv[m++] = o.rules ;
}
newargv[m++] = "--" ;
}
newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsd" ;
if (o.verbosity != 1)
newargv[m++] = o.verbosity ? "-v2" : "-v0" ;
if (o.flagS) newargv[m++] = "-S" ;
if (o.flagy) newargv[m++] = "-y" ;
else if (o.flagY) newargv[m++] = "-Y" ;
if (o.kimeout)
{
newargv[m++] = "-K" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, o.kimeout) ;
fmt[pos++] = 0 ;
}
if (o.flagZ) newargv[m++] = "-Z" ;
if (o.snilevel >= 2) newargv[m++] = "-k2" ;
else if (o.snilevel) newargv[m++] = "-k1" ;
newargv[m++] = "--" ;
if (o.doapply)
{
newargv[m++] = S6_EXTBINPREFIX "s6-applyuidgid" ;
if (o.gidn != (size_t)-1)
{
newargv[m++] = "-G" ;
newargv[m++] = fmt + pos ;
pos += gid_fmtlist(fmt + pos, o.gids, o.gidn) ;
fmt[pos++] = 0 ;
}
if (o.gid)
{
newargv[m++] = "-g" ;
newargv[m++] = fmt + pos ;
pos += gid_fmt(fmt + pos, o.gid) ;
fmt[pos++] = 0 ;
}
if (o.uid)
{
newargv[m++] = "-u" ;
newargv[m++] = fmt + pos ;
pos += uid_fmt(fmt + pos, o.uid) ;
fmt[pos++] = 0 ;
}
if (o.flagU) newargv[m++] = "-Uz" ;
newargv[m++] = "--" ;
}
while (*argv) newargv[m++] = *argv++ ;
newargv[m++] = 0 ;
xexec(newargv) ;
}
}
|