-
Notifications
You must be signed in to change notification settings - Fork 57
/
app.js
executable file
·272 lines (229 loc) · 8.1 KB
/
app.js
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
/**
* Wazuh API RESTful
* Copyright (C) 2015-2020 Wazuh, Inc. All rights reserved.
* Wazuh.com
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/
if (process.getuid() !== 0){
console.log('A root user is required to start the API.');
process.exit(1);
}
/********************************************/
/* Root actions
/********************************************/
try {
var auth = require("http-auth");
} catch (e) {
console.log("Dependencies not found. Try 'npm install' in /var/ossec/api. Exiting...");
process.exit(1);
}
const check = require('./helpers/check');
// Get configuration
config = require('./configuration/config');
if (check.configuration_file() < 0) {
setTimeout(function(){ process.exit(1); }, 500);
return;
}
// Get credentials
if (config.basic_auth.toLowerCase() == "yes"){
var auth_secure = auth.basic({
realm: "OSSEC API",
file: __dirname + "/configuration/auth/user"
});
}
// Get Certs
var options = {};
var fs = require('fs');
if (config.https.toLowerCase() == "yes"){
var api_route = config.ossec_path + '/api/';
var option_paths = {};
// Cert and key
option_paths.key = config.https_key || 'configuration/ssl/server.key';
if (option_paths.key.charAt(0) != '/'){
option_paths.key = api_route + option_paths.key;
}
option_paths.cert = config.https_cert || 'configuration/ssl/server.crt';
if (option_paths.cert.charAt(0) != '/'){
option_paths.cert = api_route + option_paths.cert;
}
options.key = fs.readFileSync(option_paths.key);
options.cert = fs.readFileSync(option_paths.cert);
// CA
var use_ca = config.https_use_ca || 'no';
if (use_ca.toLowerCase() == "yes"){
option_paths.ca = config.https_ca || 'configuration/ssl/ca.crt';
if (option_paths.ca.charAt(0) != '/'){
option_paths.ca = api_route + option_paths.ca;
}
options.ca = fs.readFileSync(option_paths.ca);
}
if (config.secureProtocol && config.secureProtocol != "") options.secureProtocol = config.secureProtocol;
if (config.ciphers && config.ciphers != "") options.ciphers = config.ciphers;
if (config.honorCipherOrder) options.honorCipherOrder = config.honorCipherOrder;
if (config.secureOptions) options.secureOptions = config.secureOptions;
}
/********************************************/
/* Drop privileges
/********************************************/
if (config.drop_privileges || config.drop_privileges == undefined) {
try {
process.setgid('ossec');
process.setuid('ossec');
} catch(err) {
console.log('Drop privileges failed: ' + err.message);
process.exit(1);
}
}
/********************************************/
/* Modules, vars and global vars
/********************************************/
try {
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors')
var moment = require('moment');
res_h = require('./helpers/response_handler');
logger = require('./helpers/logger');
} catch (e) {
console.log("Dependencies not found. Try 'npm install' in /var/ossec/api. Exiting...");
process.exit(1);
}
api_path = __dirname;
python_bin = config.ossec_path + '/framework/python/bin/python3';
/********************************************/
/* Config APP
/********************************************/
info_package = require('./package.json');
var version_mmp = info_package.version.split('.'); // major.minor.patch
var current_mm_version = version_mmp[0] + '.' + version_mmp[1]; // major.minor
if (process.argv.length == 3 && process.argv[2] == "-f")
logger.set_foreground();
if (check.wazuh(logger) < 0) {
setTimeout(function(){ process.exit(1); }, 500);
return;
}
var port = process.env.PORT || config.port;
if (config.host != "0.0.0.0")
var host = config.host;
var app = express();
// CORS
if (config.cors.toLowerCase() == "yes"){
app.use(cors());
}
// Basic authentication
if (config.basic_auth.toLowerCase() == "yes"){
app.use(auth.connect(auth_secure));
auth_secure.on('fail', (result, req) => {
logger.set_user(result.user);
var log_msg = "[" + req.connection.remoteAddress + "] " + "User: \"" + result.user + "\" - Authentication failed.";
logger.log(log_msg);
});
auth_secure.on('error', (error, req) => {
logger.set_user("");
var log_msg = "[" + req.connection.remoteAddress + "] Authentication error: " + error.code + " - " + error.message;
logger.log(log_msg);
});
}
// temporary
if (config.ld_library_path) {
logger.warning("LD library path configuration is deprecated.");
}
if (config.python) {
logger.warning("Python configuration is deprecated. Using " + python_bin);
}
// Body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.text({type:"application/xml", limit:"10mb"}));
app.use(bodyParser.raw({type:"application/octet-stream", limit:"10mb"}));
/**
* Check Wazuh app version
* Using: Header: "wazuh-app-version: X.Y.Z"
*/
app.use(function(req, res, next) {
var go_next = true;
var app_version_header = req.get('wazuh-app-version');
var regex_version = /^\d+\.\d+\.\d+$/i;
if (typeof app_version_header != 'undefined'){
if (!regex_version.test(app_version_header)){
go_next = false;
res_h.bad_request(req, res, 801);
}
else{
var app_version_mmp = app_version_header.split('.'); // major.minor.patch
var app_mm_version = app_version_mmp[0] + '.' + app_version_mmp[1]; // major.minor
if (app_mm_version != current_mm_version){
go_next = false;
res_h.bad_request(req, res, 802, "Expected version '" + current_mm_version + ".x', and found '" + app_mm_version + ".x'.");
}
}
}
if (go_next)
next();
});
// Controllers
app.use("/", require('./controllers'));
// APP Errors
app.use (function (err, req, res, next){
if ( err == "Error: invalid json" ){
logger.debug(req.connection.remoteAddress + " " + req.method + " " + req.path);
res_h.bad_request(req, res, 607);
}
else if ('status' in err && err.status == 400){
var msg = "";
if ('body' in err)
msg = "Body is not correct.";
res_h.bad_request(req, res, 614, msg);
}
else if (err == "PayloadTooLargeError: request entity too large"){
res_h.bad_request(req, res, 701);
}
else{
logger.log("Internal Error");
if(err.stack)
logger.log(err.stack);
logger.log("Exiting...");
setTimeout(function(){ process.exit(1); }, 500);
}
});
/********************************************/
/* Create server
/********************************************/
if (config.https.toLowerCase() == "yes"){
var https = require('https');
var server = https.createServer(options, app).listen(port, host, function(){
logger.log("Listening on: https://" + server.address().address + ":" + port);
});
}
else{
var http = require('http');
var server = http.createServer(app).listen(port, host, function(){
logger.log("Listening on: http://" + server.address().address + ":" + port);
});
}
/********************************************/
/* Event handler
/********************************************/
process.on('uncaughtException', function(err) {
if (err.errno == "EADDRINUSE")
logger.log("Error: Address in use (port " + port + "): Close the program using that port or change the port.")
else {
logger.log("Internal Error: uncaughtException");
if(err.stack)
logger.log(err.stack);
}
logger.log("Exiting...");
setTimeout(function(){ process.exit(1); }, 500);
});
process.on('SIGTERM', function() {
logger.log("Exiting... (SIGTERM)");
setTimeout(function(){ process.exit(1); }, 500);
});
process.on('SIGINT', function() {
logger.log("Exiting... (SIGINT)");
setTimeout(function(){ process.exit(1); }, 500);
});