-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
433 lines (339 loc) · 12.4 KB
/
main.c
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//Alaya, webdav server
//Named after 'Alaya-vijnana', the Buddhist concept of 'storehouse mind' that
//'receives impressions from all functions of the other consciousnesses and retains them
//as potential energy for their further manifestations and activities.'
//Written by Colum Paget.
//Copyright 2011 Colum Paget.
/**** Gnu Public Licence ****/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "Authenticate.h"
#include "MimeType.h"
#include "ChrootHelper.h"
#include "server.h"
#include <sys/resource.h>
ListNode *MimeTypes=NULL;
ListNode *Connections=NULL;
STREAM *ParentProcessPipe=NULL;
void SigHandler(int sig)
{
if (sig==SIGHUP) Settings.Flags |= FLAG_SIGHUP_RECV;
//we do not want SIGPIPE to kill the parent process if a child process exits!
signal(SIGPIPE, SigHandler);
signal(SIGHUP, SigHandler);
//This is to stop select restarting if we get a SIGCHLD, if we get a signal
//we need to clean it up!
signal(SIGCHLD, SigHandler);
}
int ChildFunc(void *Data, int Flags)
{
HTTPSession *Session;
int i;
//go back to the default behavior for SIGPIPE. If a child process gets disconnected
//from anything we don't want to keep sending to it.
signal(SIGPIPE, SIG_DFL);
ParentProcessPipe=STREAMFromDualFD(0,1);
STREAMSetTimeout(ParentProcessPipe, 10);
Session=(HTTPSession *) Data;
Session->StartDir=CopyStr(Session->StartDir,Settings.DefaultDir);
STREAMSetFlushType(Session->S,FLUSH_FULL,0,4096);
STREAMSetTimeout(Session->S,Settings.ActivityTimeout);
/*
for (i=2; i < 1024; i++)
{
if (i != Session->S->in_fd) close(i);
}
*/
ListDestroy(Connections, STREAMClose);
ProcessSetTitle("alaya peer=%s", STREAMGetValue(Session->S, "PeerIP"));
HTTPServerHandleConnection(Session);
STREAMClose(Session->S);
HTTPSessionDestroy(Session);
STREAMClose(ParentProcessPipe);
//LogFileFlushAll(TRUE);
_exit(0);
}
void AcceptConnection(STREAM *Serv)
{
int fd, infd, outfd;
char *Tempstr=NULL;
HTTPSession *Session;
STREAM *S;
pid_t pid;
//Check if log file has gotten big and rotate if needs be
LogFileCheckRotate(Settings.LogPath);
Session=HTTPSessionCreate();
Session->S=STREAMServerAccept(Serv);
STREAMSetFlushType(Session->S, FLUSH_FULL,0,0);
pid=PipeSpawnFunction(&infd,&outfd,NULL, ChildFunc, Session, "");
Tempstr=FormatStr(Tempstr,"%d",pid);
S=STREAMFromDualFD(outfd, infd);
ListAddNamedItem(Connections,Tempstr,S);
//Close the *socket* connection, as this parent app no longer needs to speak to it.
//however, the pipe connection to the child process stays open in Connections list
STREAMClose(Session->S);
HTTPSessionDestroy(Session);
Destroy(Tempstr);
}
//There's two processes involved in a connection, the main service process and
//possibly a 'helper' process that performs out-of-chroot tasks for the service
//process. Either of these can signal closing the socket, the service task just
//by exiting, and the helper by exiting with status '0' (which we also get if
//it crashed rather than exiting)
void CollectChildProcesses()
{
int i, status;
#define NO_OF_PROCESSES 20
pid_t *PidsList, owner, helper;
int *StatusList;
ListNode *Curr, *Next;
STREAM *S;
PidsList=(int *) calloc(NO_OF_PROCESSES,sizeof(pid_t));
StatusList=(int *) calloc(NO_OF_PROCESSES,sizeof(int));
for (i=0; i < 20; i++)
{
owner=waitpid(-1,&status,WNOHANG);
if (owner < 1) break;
PidsList[i]=owner;
if (WIFEXITED(status)) StatusList[i]=WEXITSTATUS(status);
}
Curr=ListGetNext(Connections);
while (Curr)
{
Next=ListGetNext(Curr);
S=(STREAM *) Curr->Item;
if (StrValid(Curr->Tag))
{
owner=strtol(Curr->Tag, NULL, 10);
helper=(int) STREAMGetItem(S,"HelperPid");
for (i=0; i < NO_OF_PROCESSES; i++)
{
if (PidsList[i] < 1) break;
if (
(owner==PidsList[i]) ||
((helper==PidsList[i]) && (StatusList[i]==0))
)
{
STREAMClose(S);
ListDeleteNode(Curr);
break;
}
}
}
Curr=Next;
}
Destroy(PidsList);
Destroy(StatusList);
}
void SetGMT()
{
time_t Now;
setenv("TZ","GMT",TRUE);
time(&Now);
localtime(&Now);
}
void ReopenLogFile(char *Msg)
{
LogFileClose(Settings.LogPath);
LogFileFindSetValues(Settings.LogPath, LOGFILE_LOGPID|LOGFILE_LOCK|LOGFILE_TIMESTAMP|LOGFILE_MILLISECS, Settings.MaxLogSize, Settings.MaxLogRotate, 10);
LogToFile(Settings.LogPath, "%s",Msg);
}
void SetResourceLimits()
{
struct rlimit limit;
rlim_t val;
val= (rlim_t) FromMetric(Settings.AddressSpace, 0);
if (val > 0)
{
getrlimit(RLIMIT_AS, &limit);
limit.rlim_cur=val;
setrlimit(RLIMIT_AS, &limit);
}
val= (rlim_t) FromMetric(Settings.AddressSpace, 0);
if (val > 0)
{
getrlimit(RLIMIT_STACK, &limit);
limit.rlim_cur=val;
setrlimit(RLIMIT_STACK, &limit);
}
}
// This function sets up the TLS/SSL environment, logging some warnings if settings are
// inconsistent
void InitSSL()
{
const char *ptr;
#ifdef HAVE_LIBSSL
ptr=LibUsefulGetValue("SSL:PermittedCiphers");
if (! StrValid(ptr))
{
LibUsefulSetValue("SSLPermitted:Ciphers", "DH+AESGCM:DH+AES256:DH+CAMELLIA256:ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+AES:EDH-RSA-DES-CBC3-SHA:ECDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:CAMELLIA256:AES128-SHA256:CAMELLIA128:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:DES-CBC3-SHA:!ADH:!AECDH:!aNULL:!eNULL:!LOW:!EXPORT:!MD5");
}
ptr=LibUsefulGetValue("SSL_VERIFY_CERTFILE");
if (! StrValid(ptr)) ptr=LibUsefulGetValue("SSL_VERIFY_CERTDIR");
if ((Settings.AuthFlags & FLAG_AUTH_CERT_REQUIRED) && (! StrValid(ptr)))
{
HandleError(ERR_PRINT|ERR_LOG|ERR_EXIT, "ERROR: Client SSL Certificate required, but no certificate verify file/directory given.\nPlease Supply one with the -verify-path command-line-argument, or the SSLVerifyPath config file entry.");
}
if (Settings.Flags & FLAG_SSL_PFS)
{
ptr=LibUsefulGetValue("SSL:DHParamsFile");
if (! StrValid(ptr))
{
if (Settings.Flags & FLAG_PFS_GENERATE)
{
printf("Generating Diffie-Helman parameters\n");
OpenSSLGenerateDHParams();
printf("DONE Generating Diffie-Helman parameters\n");
}
else HandleError(ERR_PRINT|ERR_LOG, "WARNING: No DH parameters file given for SSL. Perfect Forward Secrecy can only be achieved with Eliptic Curve (ECDH) methods. ECDH depends on fixed values recomended by the U.S. National Institute of Standards and technology (NIST) and thus may not be trustworthy.\n\nCreate a DHParams file with 'openssl dhparam -2 -out dhparams.pem 4096' and give the path to it using the -dhparams command-line-argument or the DHParams config file entry, if you want DiffieHelman key exchange for Perfect Forward Secrecy.");
}
}
#endif
}
int ServiceSocketsInit(ListNode *Connections)
{
char *Tempstr=NULL, *Token=NULL, *Config=NULL;
const char *ptr;
int fd, BoundCount=0;
STREAM *S;
////Allow 5 secs for any previous instance of alaya to shutdown
//for (i=0; i < 5; i++)
ptr=GetToken(Settings.BindAddress,",",&Token,0);
while (ptr)
{
//if it starts with a '/' it's a unix Socket
if (*Token=='/')
{
Tempstr=MCopyStr(Tempstr,"unix:",Token,NULL);
Config=CopyStr(Config, "");
}
else
{
if (Settings.Flags & FLAG_USE_FASTOPEN) Config=CatStr(Config, "F");
if (Settings.Flags & FLAG_USE_REUSEPORT) Config=CatStr(Config, "P");
Tempstr=FormatStr(Tempstr, " listen=%d", Settings.ListenQueueLen);
Tempstr=FormatStr(Tempstr,"tcp:%s:%d",Token,Settings.Port);
}
S=STREAMServerNew(Tempstr, Config);
if (! S)
{
sleep(1);
S=STREAMServerNew(Tempstr, Config);
}
if (S)
{
ListAddItem(Connections,S);
BoundCount++;
}
else
{
LogToFile(Settings.LogPath, "Cannot bind to port %s",Tempstr);
printf("Cannot bind to port %s\n",Tempstr);
}
ptr=GetToken(ptr, ",",&Token,0);
}
if (BoundCount==0)
{
printf("Failed to bind any interface! Exiting.");
LogToFile(Settings.LogPath, "Failed to bind any interface! Exiting.");
exit(1);
}
Destroy(Tempstr);
Destroy(Config);
Destroy(Token);
}
int main(int argc, char *argv[])
{
STREAM *S;
int fd;
char *Tempstr=NULL;
int result, i;
pid_t pid;
ProcessTitleCaptureBuffer(argv);
SSLAvailable();
SetTimezoneEnv();
//Drop most capabilities
DropCapabilities(CAPS_LEVEL_STARTUP);
nice(10);
InitSettings();
//We should handle failed User/Group switches within alaya,
//so we don't need libUseful to handle them for us
LibUsefulSetValue("SwitchUserAllowFail","yes");
LibUsefulSetValue("SwitchGroupAllowFail","yes");
if (StrValid(Settings.Timezone)) setenv("TZ",Settings.Timezone,TRUE);
//LibUsefulMemFlags |= MEMORY_CLEAR_ONFREE;
openlog("alaya",LOG_PID, LOG_DAEMON);
Connections=ListCreate();
//do this initially to pick up -c <config file> option
SettingsParseCommandLine(argc,argv,&Settings);
ReadConfigFile(Settings.ConfigPath);
//do it again to override anything read from the config file
SettingsParseCommandLine(argc,argv,&Settings);
PostProcessSettings(&Settings);
SetResourceLimits();
Tempstr=FormatStr(Tempstr, "Alaya Version %s Starting up on %s:%d",Version,Settings.BindAddress,Settings.Port);
ReopenLogFile(Tempstr);
//Not only handles signals, but registers itself too, so we
//run it to set up signal handling
SigHandler(0);
if (Settings.Flags & FLAG_SSL) InitSSL();
LoadFileMagics("/etc/mime.types","/etc/magic");
ServiceSocketsInit(Connections);
//Do this before any logging!
if (! (Settings.Flags & FLAG_NODEMON)) demonize();
// must do this AFTER demonize
if (StrValid(Settings.PidFilePath)) WritePidFile(Settings.PidFilePath);
//We no longer need the 'bind port' capablity
DropCapabilities(CAPS_LEVEL_NETBOUND);
while (1)
{
S=STREAMSelect(Connections,NULL);
if (S)
{
switch (S->Type)
{
case STREAM_TYPE_UNIX_SERVER:
case STREAM_TYPE_TCP_SERVER:
AcceptConnection(S);
break;
default:
//This handles a request from a child process that is servicing a connection
//Often they are chrooted and need to call back to the parent process to
//perform tasks for them. HandleChildProcessRequest can either return '0',
//which means the request was dealt with or failed, and the connection can
//be closed, or a pid. We book the pid against the appropriate connection
//so when the process exits we can use its exit status to decide whether the
//connection has been closed, or is 'keep alive'
pid=HandleChildProcessRequest(S);
if (pid==STREAM_CLOSED)
{
ListDeleteItem(Connections,S);
STREAMClose(S);
}
else if (pid > 0) STREAMSetItem(S,"HelperPid",(void *) pid);
break;
}
}
if (Settings.Flags & FLAG_SIGHUP_RECV)
{
ReopenLogFile("Reopening Log File");
Settings.Flags &= ~FLAG_SIGHUP_RECV;
}
CollectChildProcesses();
}
LogFileFlushAll(TRUE);
Destroy(Tempstr);
return(0);
}