-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
163 lines (124 loc) · 3.82 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <errno.h>
#include <stdlib.h>
#include <QtGlobal>
#include <QDebug>
#ifndef Q_OS_WIN32
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#endif
#include <QCoreApplication>
#include <QTextStream>
#include <QStringList>
#include <QFileInfo>
#include "httpdaemon.h"
#include "logging.h"
#include "configuration.h"
int main(int argc, char *argv[])
{
#ifndef Q_OS_WIN32
openlog("http-daemon", LOG_ODELAY, LOG_DAEMON);
atexit(closelog);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qInstallMessageHandler(qtOutputToLog);
#else
qInstallMsgHandler(qtOutputToLog);
#endif
QCoreApplication a(argc, argv);
a.setObjectName("main app");
QTextStream out(stdout);
QTextStream err(stderr);
#ifndef CONFIG_DIR
#define CONFIG_DIR "";
#endif
Configuration c(CONFIG_DIR CONFIG_FILE_NAME);
if(!c.read()){
QByteArray msg;
msg = c.getSettingsPath().toLocal8Bit();
syslog(LOG_ERR, "Cannot read configuration file at: %s", msg.constData());
return 1;
}
if(!c.check()){
QByteArray msg;
msg = c.getSettingsPath().toLocal8Bit();
syslog(LOG_ERR, "Invalid configuration file at: %s", msg.constData());
return 1;
}
#ifndef Q_OS_WIN32
pid_t pid = fork();
if(-1 == pid){
err << strerror(errno);
syslog(LOG_ERR, strerror(errno));
return 1;
}
if(pid > 0){
out << "Successfully forked child with PID: " << pid << endl;
syslog(LOG_INFO, "Successfully forked child with PID: %i", pid);
return 0;
}
umask(0);
pid_t sid = setsid();
if(-1 == sid){
syslog(LOG_ERR, "setsid() failed: %m");
return 1;
}
if(-1 == chdir("/")){
syslog(LOG_ERR, "chdir() failed: %m");
return 1;
}
if(0 != getuid()){
qDebug () << "Current UID:" << getuid();
syslog(LOG_ERR, "Must be root to switch user");
return 1;
}
QByteArray msg;
msg = Configuration::get("user", "root").toString().toLocal8Bit();
const char *user = msg.constData();
struct passwd *pw = getpwnam(user);
if(NULL == pw){
syslog(LOG_ERR, "Cannot find user %s", user);
return 1;
}
if(0 != setgid(pw->pw_gid)){
syslog(LOG_ERR, "Cannot set gid to %i, error: %m", pw->pw_gid);
return 1;
}
if(0 != setuid(pw->pw_uid)){
syslog(LOG_ERR, "Cannot set uid to %i, error: %m", pw->pw_uid);
return 1;
}
syslog(LOG_INFO, "Set uid to %d and gid to %d", pw->pw_uid, pw->pw_gid);
#endif
bool ok;
int port = Configuration::get("port", 80).toInt(&ok);
if(!ok){
port = 80;
}
HTTPDaemon s;
s.setObjectName("HTTPDaemon");
//TODO: some ports (like 80) can be bound to only as root, so do this before changing users
if(!s.listen(QHostAddress(Configuration::get("address").toString()), port)){
QByteArray msg;
msg = s.errorString().toLocal8Bit();
syslog(LOG_ERR, "Cannot start the server: %s", msg.constData());
return 1;
}
else{
QByteArray msg, msg1;
msg = s.serverAddress().toString().toLocal8Bit();
syslog(LOG_NOTICE, "Listening on %s:%i", msg.constData(), s.serverPort());
msg = Configuration::get("documentroot").toString().toLocal8Bit();
msg1 = Configuration::get("pluginroot").toString().toLocal8Bit();
syslog(LOG_INFO, "Document root is in: %s\nPlugin root is in: %s", msg.constData(), msg1.constData());
}
#ifndef Q_OS_WIN32
//for some reason if I do this before starting listening the listen() function will fail
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
#endif
qRegisterMetaType<HTTPResponse>("HTTPResponse");
return a.exec();
}