-
Notifications
You must be signed in to change notification settings - Fork 23
/
acceptFactory.cc
407 lines (348 loc) · 11.3 KB
/
acceptFactory.cc
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Process server for soft ioc
// David H. Thompson 8/29/2003
// Ralph Lange <[email protected]> 2007-2016
// Freddie Akeroyd 2016
// Michael Davidsaver 2017
// GNU Public License (GPLv3) applies - see www.gnu.org
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <sstream>
#include "procServ.h"
struct acceptItem : public connectionItem
{
acceptItem(bool readonly) :connectionItem(-1, readonly) {}
virtual ~acceptItem();
void readFromFd(void);
int Send(const char *, int);
virtual void remakeConnection()=0;
};
struct acceptItemTCP : public acceptItem
{
acceptItemTCP(const sockaddr_in& addr, bool readonly);
virtual ~acceptItemTCP() {}
sockaddr_in addr;
virtual void remakeConnection();
virtual void writeAddress(std::ostream& fp) {
char buf[40] = "";
inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf));
buf[sizeof(buf)-1] = '\0';
fp<<"tcp:"<<buf<<":"<<ntohs(addr.sin_port)<<"\n";
}
virtual void writeAddressEnv(std::ostringstream& env_var) {
char buf[40] = "";
inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf));
buf[sizeof(buf)-1] = '\0';
if(_readonly) {
env_var<<"LOG=";
} else {
env_var<<"CTL=";
}
env_var<<"tcp:"<<buf<<":"<<ntohs(addr.sin_port)<<";";
}
};
#ifdef USOCKS
struct acceptItemUNIX : public acceptItem
{
acceptItemUNIX(const char* path, bool readonly);
virtual ~acceptItemUNIX();
sockaddr_un addr;
socklen_t addrlen;
uid_t uid;
gid_t gid;
unsigned perms;
bool abstract;
virtual void remakeConnection();
virtual void writeAddress(std::ostream& fp) {
if(abstract) {
fp<<"unix:@"<<&addr.sun_path[1]<<"\n";
} else {
fp<<"unix:"<<addr.sun_path<<"\n";
}
}
virtual void writeAddressEnv(std::ostringstream& env_var) {
if(_readonly) {
env_var<<"LOG=";
} else {
env_var<<"CTL=";
}
if(abstract) {
env_var<<"unix:@"<<&addr.sun_path[1]<<";";
} else {
env_var<<"unix:"<<addr.sun_path<<";";
}
}
};
#endif
// service and calls clientFactory when clients are accepted
connectionItem * acceptFactory (const char *spec, bool local, bool readonly)
{
char junk;
unsigned port = 0;
unsigned A[4];
sockaddr_in inet_addr;
memset(&inet_addr, 0, sizeof(inet_addr));
if(sscanf(spec, "%u %c", &port, &junk)==1) {
// simple integer is TCP port number
inet_addr.sin_family = AF_INET;
inet_addr.sin_addr.s_addr = htonl(local ? INADDR_LOOPBACK : INADDR_ANY);
inet_addr.sin_port = htons(port);
if(port>0xffff) {
fprintf( stderr, "%s: invalid control port %d (must be <65536)\n",
procservName, port );
exit(1);
}
connectionItem *ci = new acceptItemTCP(inet_addr, readonly);
return ci;
} else if(sscanf(spec, "%u . %u . %u . %u : %u %c",
&A[0], &A[1], &A[2], &A[3], &port, &junk)==5) {
// bind to specific interface and port
inet_addr.sin_family = AF_INET;
inet_addr.sin_addr.s_addr = htonl(local ? INADDR_LOOPBACK : (A[0]<<24 | A[1]<<16 | A[2]<<8 | A[3]));
inet_addr.sin_port = htons(port);
if(port>0xffff) {
fprintf( stderr, "%s: invalid control port %d (must be <65536)\n",
procservName, port );
exit(1);
}
connectionItem *ci = new acceptItemTCP(inet_addr, readonly);
return ci;
} else if(strncmp(spec, "unix:", 5)==0) {
#ifdef USOCKS
connectionItem *ci = new acceptItemUNIX(spec+5, readonly);
return ci;
#else
fprintf(stderr, "Unix sockets not supported on this host\n");
exit(1);
#endif
} else {
fprintf(stderr, "Invalid socket spec '%s'\n", spec);
exit(1);
}
}
acceptItem::~acceptItem()
{
if (_fd >= 0) close(_fd);
PRINTF("~acceptItem()\n");
}
// Accept item constructor
// This opens a socket, binds it to the decided port,
// and sets it to listen mode
acceptItemTCP::acceptItemTCP(const sockaddr_in &addr, bool readonly)
:acceptItem(readonly)
,addr(addr)
{
char myname[128] = "<unknown>\0";
inet_ntop(AF_INET, &addr.sin_addr, myname, sizeof(myname)-1);
myname[sizeof(myname)-1] = '\0';
remakeConnection();
PRINTF("Created new telnet TCP listener (acceptItem %p) at %s:%d (read%s)\n",
this, myname, ntohs(addr.sin_port), readonly?"only":"/write");
}
void acceptItemTCP::remakeConnection()
{
int optval = 1;
int bindStatus;
_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (_fd < 0) {
PRINTF("Socket error: %s\n", strerror(errno));
throw errno;
}
int setsockoptStatus = setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
if (setsockoptStatus < 0)
PRINTF("Setsockopt error while trying to set SO_REUSEADDR: %s\n",
strerror(errno));
#ifdef SOLARIS
int setsockoptStatus = setsockopt(_fd, SOL_SOCKET, SO_EXCLBIND, &optval,
sizeof(optval));
if (setsockoptStatus < 0)
PRINTF("Setsockopt error while trying to set SO_EXCLBIND: %s\n",
strerror(errno));
#endif
#ifdef _WIN32
int setsockoptStatus = setsockopt(_fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
&optval, sizeof(optval));
if (setsockoptStatus < 0)
PRINTF("Setsockopt error while trying to set SO_EXCLUSIVEADDRUSE: %s\n",
strerror(errno));
#endif
bindStatus = bind(_fd, (struct sockaddr *) &addr, sizeof(addr));
if (bindStatus < 0)
{
PRINTF("Bind error: %s\n", strerror(errno));
throw errno;
}
else
PRINTF("Bind returned %d\n", bindStatus);
if (listen(_fd, 5) < 0) {
PRINTF("Listen error: %s\n", strerror(errno));
throw errno;
}
socklen_t slen = sizeof(addr);
getsockname(_fd, (struct sockaddr *) &addr, &slen);
PRINTF("Listening on fd %d\n", _fd);
return;
}
#ifdef USOCKS
acceptItemUNIX::acceptItemUNIX(const char *path, bool readonly)
:acceptItem(readonly)
,uid(getuid())
,gid(getgid())
,perms(0666) // default permissions equivalent to tcp bind to localhost
,abstract(false)
{
/* must be one of
* "/path/sock"
* "user:grp:perm:/path/sock"
* "@/path/of/sock" # abstract socket
*/
std::string spec(path);
size_t sep(spec.find_first_of(':'));
if(sep!=spec.npos) {
size_t sep2(spec.find_first_of(':', sep+1)),
sep3(spec.find_first_of(':', sep2+1));
if(sep2==spec.npos || sep3==spec.npos) {
fprintf(stderr, "Unix path+permissions spec unparsable '%s'\n", path);
exit(1);
}
std::string user(spec.substr(0,sep)),
grp (spec.substr(sep+1, sep2-sep-1)),
perm(spec.substr(sep2+1, sep3-sep2-1));
if(!user.empty()) {
struct passwd *uinfo = getpwnam(user.c_str());
if(!uinfo) {
fprintf(stderr, "Unknown user '%s'\n", user.c_str());
exit(1);
}
uid = uinfo->pw_uid;
gid = uinfo->pw_gid;
}
if(!grp.empty()) {
struct group *ginfo = getgrnam(grp.c_str());
if(!ginfo) {
fprintf(stderr, "Unknown group '%s'\n", grp.c_str());
exit(1);
}
gid =ginfo->gr_gid;
}
if(!perm.empty()) {
perms = strtoul(perm.c_str(), NULL, 8);
}
spec = spec.substr(sep3+1);
}
if(spec.empty()) {
fprintf(stderr, "expected unix socket path spec.\n");
exit(1);
}
/* Abstract unix sockets are a linux specific feature whereby
* the socket has a "path" name associated with it, but no presence
* in the filesystem (and no associated permission check).
* Analogous to IP bound to localhost with a name string
* instead of a port number.
* We denote an abstract socket with a leading '@'.
*/
abstract = spec[0]=='@';
#ifndef __linux__
if(abstract) {
fprintf(stderr, "Abstract unix sockets not supported by this host\n");
exit(1);
}
#endif
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if(spec.size()>=sizeof(addr.sun_path)) {
fprintf(stderr, "Unix path is too long (must be <%zu)\n", sizeof(addr.sun_path));
exit(1);
}
// see unix(7) man-page
addrlen = offsetof(struct sockaddr_un, sun_path) + spec.size() + 1;
memcpy(addr.sun_path, spec.c_str(), spec.size()+1);
PRINTF("Created new telnet UNIX listener (acceptItem %p) at '%s' (read%s)\n",
this, addr.sun_path, readonly?"only":"/write");
/* signal an abstract socket with a *leading* nil.
* We replace the '@'
* and reduce addrlen to *not* include the trailing nil.
* To quote unix(7) manpage
* "Null bytes in the name have no special significance."
*/
if(abstract) {
addr.sun_path[0] = '\0';
addrlen--;
}
remakeConnection();
}
acceptItemUNIX::~acceptItemUNIX()
{
if(!abstract)
unlink(addr.sun_path);
}
void acceptItemUNIX::remakeConnection()
{
int bindStatus;
if(!abstract && unlink(addr.sun_path) < 0) {
if(errno!=ENOENT) {
PRINTF("Failed to remove unix socket at '%s' : %s\n", addr.sun_path, strerror(errno));
throw errno; // Nooooo!
}
}
_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (_fd < 0) {
PRINTF("Socket error: %s\n", strerror(errno));
throw errno;
}
bindStatus = bind(_fd, (struct sockaddr *) &addr, addrlen);
if (bindStatus < 0)
{
PRINTF("Bind error: %s\n", strerror(errno));
throw errno;
}
else
PRINTF("Bind returned %d\n", bindStatus);
if (listen(_fd, 5) < 0) {
PRINTF("Listen error: %s\n", strerror(errno));
throw errno;
}
if(!abstract) {
if(chmod(addr.sun_path, 0)<0)
PRINTF("Can't chmod %u unix socket : %s\n", 0, strerror(errno));
if(chown(addr.sun_path, uid, gid))
PRINTF("Can't chown %u:%u unix socket : %s\n", uid, gid, strerror(errno));
if(chmod(addr.sun_path, perms)<0)
PRINTF("Can't chmod %u unix socket : %s\n", perms, strerror(errno));
}
PRINTF("Listening on fd %d\n", _fd);
return;
}
#endif
// Accept connection and create a new connectionItem for it.
void acceptItem::readFromFd(void)
{
int newFd;
struct sockaddr addr;
socklen_t len = sizeof(addr);
newFd = accept( _fd, &addr, &len );
if (newFd >= 0) {
PRINTF("acceptItem: Accepted connection on handle %d\n", newFd);
AddConnection(clientFactory(newFd, _readonly));
} else {
PRINTF("Accept error: %s\n", strerror(errno)); // on Cygwin got error EINVAL
remakeConnection();
}
}
// Send characters to client
int acceptItem::Send (const char * buf, int count)
{
// Makes no sense to send to the listening socket
return true;
}