-
Notifications
You must be signed in to change notification settings - Fork 57
/
install_api.sh
executable file
·498 lines (411 loc) · 14.8 KB
/
install_api.sh
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env bash
# Copyright (C) 2015-2016 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.
# Installer for Wazuh API
# Wazuh Inc.
#
# Usage:
# ./install_api.sh [dependencies|download|dev]
# ./install_api.sh Install API from current path
# ./install_api.sh dependencies List dependencies
# ./install_api.sh download Download and install latest release (stable branch)
# ./install_api.sh dev Install API from current path in development mode
arg=$1 # empty, help, dependencies, download or dev
PREDEF_FILE=$(dirname $0)/configuration/preloaded_vars.conf
TRUE="true";
FALSE="false";
# Aux functions
isFile()
{
FILE=$1
ls ${FILE} >/dev/null 2>&1
if [ $? = 0 ]; then
echo "${TRUE}"
return 0;
fi
echo "${FALSE}"
return 1;
}
print() {
echo -e $1
}
error_and_exit() {
print "Error executing command: '$1'.\n"
if [ "X${API_BACKUP}" == "Xyes" ]; then
print "Backup directory: $API_PATH_BACKUP"
print "\t1. Save previous configuration: cp -rp $API_PATH_BACKUP/configuration $DEF_OSSDIR/tmp"
print "\t2. Install API $API_OLD_VERSION"
print "\t3. Restore configuration: mv $DEF_OSSDIR/tmp/configuration $API_PATH/configuration"
print "\t4. Restart API"
fi
if [ -d "/root/wazuh-api-tmp" ]; then
exec_cmd "rm -rf /root/wazuh-api-tmp"
fi
print "\nExiting."
exit 1
}
exec_cmd() {
eval $1 > /dev/null 2>&1 || error_and_exit "$1"
}
exec_cmd_bash() {
bash -c "$1" || error_and_exit "$1"
}
get_configuration_value () { # $1 setting
cat "$API_PATH/configuration/config.js" | grep -P "config.$1\s*=\s*\"" | grep -P '".*"' -o | tr -d '"'
}
edit_configuration() { # $1 -> setting, $2 -> value
sed -i "s/^config.$1\s=.*/config.$1 = \"$2\";/g" "$API_PATH/configuration/config.js" || error_and_exit "sed (editing configuration)"
}
get_type_service() {
if [ -n "$(ps -e | egrep ^\ *1\ .*systemd$)" ]; then
echo "systemctl"
else
echo "service"
fi
}
url_latest_release () {
LATEST_RELEASE=$(curl -L -s -H 'Accept: application/json' https://github.com/$1/$2/releases/latest)
#LATEST_VERSION=$(echo $LATEST_RELEASE | sed -e 's/.*"tag_name":"\(.*\)".*/\1/')
LATEST_VERSION=$(echo $LATEST_RELEASE | grep -P "\"tag_name\":\".+\"update_url" -o | grep -P "v\d+(?:\.\d+){0,2}\-*\w*" -o)
ARTIFACT_URL="https://github.com/$1/$2/archive/$LATEST_VERSION.tar.gz"
echo $ARTIFACT_URL
}
check_program_installed() {
hash $1 > /dev/null 2>&1
if [ "$?" != "0" ]; then
print "$1 not found. is it installed?."
print "Check the dependencies executing: ./install_api.sh dependencies"
exit 1
fi
}
# END Aux functions
show_info () {
https=$(get_configuration_value "https")
port=$(get_configuration_value "port")
basic_auth=$(get_configuration_value "basic_auth")
curl_msg="curl"
if [ "X${https}" == "Xyes" ]; then
proto="https"
else
proto="http"
fi
print "\nAPI URL: $proto://host_ip:$port/"
if [ "X${update}" != "Xyes" ]; then
print "user: 'foo'"
print "password: 'bar'"
curl_msg="$curl_msg -u foo:bar"
else
curl_msg="$curl_msg -u user:pass"
fi
if [ "X${basic_auth}" == "Xno" ]; then
print "Authentication disabled (not secure)."
curl_msg="curl"
fi
print "Configuration: $API_PATH/configuration"
print "Test: $curl_msg -k $proto://127.0.0.1:55000?pretty"
}
help() {
echo "./install_api.sh [dependencies|download|dev]"
echo "./install_api.sh <options> Install API from current path"
echo " Options:"
echo " --no-service Do not install API service"
echo "./install_api.sh dependencies List dependencies"
echo "./install_api.sh download Download and install latest release (stable branch)"
echo "./install_api.sh dev Install API from current path in development mode"
}
required_packages() {
print "\nDebian and Ubuntu based Linux distributions:"
print "\tNodeJS 4.x or newer:"
print "\t\tcurl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -"
print "\t\tsudo apt-get install -y nodejs"
print "\nRed Hat, CentOS and Fedora:"
print "\tNodeJS 4.x or newer:"
print "\t\tcurl --silent --location https://rpm.nodesource.com/setup_6.x | bash -"
print "\t\tsudo yum -y install nodejs"
}
previous_checks() {
# Arguments
if [ "X${arg}" == "Xdependencies" ]; then # dependencies argument
required_packages
exit 0
elif [ "X${arg}" == "Xhelp" ]; then # help argument
help
exit 0
elif [ "X${arg}" == "Xdev" ]; then # dev argument
API_SOURCES=`pwd`
elif [ "X${arg}" == "Xdownload" ]; then # download argument
API_SOURCES="/root"
DOWNLOAD_PATH=$(url_latest_release "wazuh" "wazuh-api")
else
API_SOURCES="." # empty argument
fi
if [ "X${arg}" == "X--no-service" ]; then # do not install api service
NO_SERVICE="1"
fi
# Test root permissions
if [ "$EUID" -ne 0 ]; then
print "Please run this script with root permissions.\nExiting."
exit 1
fi
# Paths
OSSEC_CONF="/etc/ossec-init.conf"
DEF_OSSDIR="/var/ossec"
if ! [ -f $OSSEC_CONF ]; then
print "Can't find $OSSEC_CONF. Is OSSEC installed?.\nExiting."
exit 1
fi
. $OSSEC_CONF
if [ -z "$DIRECTORY" ]; then
DIRECTORY=$DEF_OSSDIR
fi
API_PATH="${DIRECTORY}/api"
API_PATH_BACKUP="${DIRECTORY}/~api"
FRAMEWORK_PATH="${DIRECTORY}/framework"
API_BACKUP='no'
serv_type=$(get_type_service)
if ! [ -d $FRAMEWORK_PATH ]; then
print "Can't find $FRAMEWORK_PATH. Is Wazuh installed?.\nExiting."
exit 1
fi
# Dependencies
check_program_installed "tar"
check_program_installed "curl"
check_program_installed "gcc"
NODE_DIR=$(command -v nodejs 2> /dev/null)
if [ "X$NODE_DIR" = "X" ]; then
NODE_DIR=$(command -v node 2> /dev/null)
if [ "X$NODE_DIR" = "X" ]; then
echo "NodeJS binaries not found. Is NodeJS installed?"
exit 1
fi
fi
NODE_VERSION=`$NODE_DIR --version | grep -P '^v\d+' -o | grep -P '\d+' -o`
if [ $NODE_VERSION -lt 4 ]; then
print "The current version of NodeJS installed is not supported. Wazuh API requires NodeJS 4.x or newer."
print "Review the dependencies executing: ./install_api.sh dependencies"
exit 1
fi
check_program_installed "npm"
}
get_api () {
if [ "X$DOWNLOAD_PATH" != "X" ]; then
print "\nDownloading API from $DOWNLOAD_PATH"
if [ -d "$API_SOURCES/wazuh-api-tmp" ]; then
exec_cmd "rm -rf $API_SOURCES/wazuh-api-tmp"
fi
exec_cmd "mkdir -p $API_SOURCES/wazuh-api-tmp"
exec_cmd "curl -sL $DOWNLOAD_PATH | tar xvz -C $API_SOURCES/wazuh-api-tmp"
API_SOURCES="$API_SOURCES/wazuh-api-tmp/wazuh-api-*.*"
API_NEW_VERSION=`cat $API_SOURCES/package.json | grep "version\":" | grep -P "\d+(?:\.\d+){0,2}\-*\w*" -o`
print "\nInstalling Wazuh API $API_NEW_VERSION"
else
API_NEW_VERSION=`cat $API_SOURCES/package.json | grep "version\":" | grep -P "\d+(?:\.\d+){0,2}\-*\w*" -o`
if [ "X${arg}" == "Xdev" ]; then
print "\nInstalling Wazuh API $API_NEW_VERSION from current directory [DEV MODE]."
else
print "\nInstalling Wazuh API $API_NEW_VERSION from current directory."
fi
fi
}
backup_api () {
if [ -e $API_PATH_BACKUP ]; then
exec_cmd "rm -rf $API_PATH_BACKUP/*"
else
exec_cmd "mkdir $API_PATH_BACKUP"
exec_cmd "chown root:ossec $API_PATH_BACKUP"
fi
if [ -e $API_PATH/package.json ]; then
exec_cmd "cp -rLfp $API_PATH/package.json $API_PATH_BACKUP/package.json"
fi
if [ -d $API_PATH/configuration ]; then
exec_cmd "cp -rLfp $API_PATH/configuration $API_PATH_BACKUP/configuration"
fi
if [ -d $API_PATH/ssl ]; then
exec_cmd "cp -rLfp $API_PATH/ssl $API_PATH_BACKUP/ssl"
fi
API_BACKUP='yes'
API_OLD_VERSION=`cat $API_PATH_BACKUP/package.json | grep "version\":" | grep -P "\d+(?:\.\d+){0,2}\-*\w*" -o`
}
restore_configuration () {
if [[ ${API_OLD_VERSION} =~ ^3 ]] || [[ ${API_OLD_VERSION} =~ ^2 ]]; then
exec_cmd "rm -rf $API_PATH/configuration"
exec_cmd "cp -rfp $API_PATH_BACKUP/configuration $API_PATH/configuration"
# Assign the right permissions
setup_api_permissions
elif [[ ${API_OLD_VERSION} =~ ^1 ]]; then
exec_cmd "cp -rfp $API_PATH_BACKUP/ssl/htpasswd $API_PATH/configuration/auth/user"
exec_cmd "cp -p $API_PATH_BACKUP/ssl/*.key $API_PATH_BACKUP/ssl/*.crt $API_PATH/configuration/ssl/"
exec_cmd "chown -R root:root $API_PATH/configuration"
exec_cmd "chmod -R 750 $API_PATH/configuration"
exec_cmd "chmod u-x $API_PATH/configuration/ssl/*"
RESTORE_WARNING="1"
else
RESTORE_WARNING="2"
fi
}
setup_api_permissions () {
# General permissions
exec_cmd "chown -R root:ossec $API_PATH"
exec_cmd "chown -R root:root $API_PATH/scripts"
exec_cmd "chown -R root:root $API_PATH/configuration"
exec_cmd "chmod -R 750 $API_PATH"
# Remove execution permissions
exec_cmd "chmod ugo-x $API_PATH/package.json"
exec_cmd "chmod ugo-x $API_PATH/scripts/wazuh-api*"
# config.js
exec_cmd "chown root:ossec $API_PATH/configuration"
exec_cmd "chown root:ossec $API_PATH/configuration/config.js"
exec_cmd "chmod 740 $API_PATH/configuration/config.js"
# API users permissions
exec_cmd "chmod 660 $API_PATH/configuration/auth/user"
}
setup_api() {
# Check if API is installed
update="no"
if [ -d $API_PATH ]; then
backup_api
print ""
if [[ "X${REINSTALL}" != "X" ]]; then
case $REINSTALL in
[nN] ) update="no";;
[yY] ) update="yes";;
esac
else
while true; do
read -p "Wazuh API is already installed (version $API_OLD_VERSION). Do you want to update it? [y/n]: " yn
case $yn in
[Yy] ) update="yes"; break;;
[Nn] ) update="no"; break;;
esac
done
fi
if [ "X${update}" == "Xno" ]; then
if [[ "X${REMOVE}" != "X" ]]; then
case $REMOVE in
[nN] ) print "Not possible to install the API.\nExiting."; exit 1; break;;
esac
else
while true; do
print ""
read -p "The installation directory already exists. Should I delete it? [y/n]: " yn
case $yn in
[Yy] ) break;;
[Nn] ) print "Not possible to install the API.\nExiting."; exit 1; break;;
esac
done
fi
fi
exec_cmd "rm -rf $API_PATH"
fi
# Copy files
if [ "X${update}" == "Xyes" ]; then
print "\nUpdating API ['$API_PATH']."
e_msg="updated"
else
print "\nInstalling API ['$API_PATH']."
e_msg="installed"
fi
if [ "X${arg}" == "Xdev" ]; then
exec_cmd "ln -s $API_SOURCES $API_PATH"
else
exec_cmd "mkdir $API_PATH"
exec_cmd "cd $API_SOURCES"
exec_cmd "cp --parents -r app.js configuration controllers examples helpers models package.json scripts $API_PATH"
exec_cmd "cd -"
# Set up the right permissions for Wazuh API
setup_api_permissions
if [ -f "$API_PATH/configuration/ssl/.gitignore" ]; then
exec_cmd "rm -f $API_PATH/configuration/ssl/.gitignore"
fi
fi
if [ "X${update}" == "Xyes" ]; then
restore_configuration
# Remove framework
if [ -d "$API_PATH/framework" ]; then
exec_cmd "rm -rf $API_PATH/framework"
fi
fi
print "\nInstalling NodeJS modules."
if [ "X${arg}" == "Xdev" ]; then
exec_cmd "cd $API_PATH && npm install"
else
exec_cmd "cd $API_PATH && npm install --production"
fi
exec_cmd "chown -R root:ossec $API_PATH/node_modules"
exec_cmd "chmod -R 750 $API_PATH/node_modules"
exec_cmd "ln -fs $API_PATH/node_modules/htpasswd/bin/htpasswd $API_PATH/configuration/auth/htpasswd"
# Set OSSEC directory in API configuration
if [ "X${DIRECTORY}" != "X/var/ossec" ]; then
repl="\\\/"
escaped_ossec_path=`echo "$DIRECTORY" | sed -e "s#\/#$repl#g"`
edit_configuration "ossec_path" $escaped_ossec_path
fi
# Create/check api.log and /logs/api directory
APILOG_PATH="${DIRECTORY}/logs/api.log"
if [ ! -f $APILOG_PATH ]; then
touch $APILOG_PATH
fi
exec_cmd "chown ossec:ossec $APILOG_PATH"
exec_cmd "chmod 640 $APILOG_PATH"
APILOG_DIR="${DIRECTORY}/logs/api"
if [ ! -d $APILOG_DIR ]; then
mkdir $APILOG_DIR
fi
exec_cmd "chown ossec:ossec $APILOG_DIR"
exec_cmd "chmod 750 $APILOG_DIR"
# HTTPS never will be enabled if update=yes
if [ "X${update}" = "Xno" ]; then
if !([ "X${DISABLE_HTTPS}" = "Xy" ] || [ "X${DISABLE_HTTPS}" = "XY" ]); then
# enable HTTPS
configure_https
fi
fi
if [ -z "$NO_SERVICE" ]
then
print "\nInstalling service."
echo "----------------------------------------------------------------"
exec_cmd_bash "$API_PATH/scripts/install_daemon.sh"
echo "----------------------------------------------------------------"
else
print "\nSkipping service installation."
fi
}
configure_https() {
HTTPS="Y"
PASSWORD="wazuh"
COUNTRY="XX"
STATE="XX"
LOCALITY="XX"
ORG_NAME="XX"
ORG_UNIT="XX"
COMMON_NAME="XX"
. ./scripts/configure_api.sh
change_https
}
main() {
print "### Wazuh API ###"
# Reading pre-defined file
if [ ! `isFile ${PREDEF_FILE}` = "${FALSE}" ]; then
. ${PREDEF_FILE}
fi
previous_checks
get_api
setup_api
show_info
if [ "X${RESTORE_WARNING}" == "X1" ]; then
print "\nWarning: Some problems occurred when restoring your previous configuration ($API_PATH/configuration/config.js). Please, review it manually. Backup directory: $API_PATH_BACKUP."
elif [ "X${RESTORE_WARNING}" == "X2" ]; then
print "\nWarning: It was not possible to restore your previous configuration. Please, review it manually. Backup directory: $API_PATH_BACKUP."
else
exec_cmd "rm -rf $API_PATH_BACKUP"
fi
print "Note: You can configure the API executing $API_PATH/scripts/configure_api.sh"
print "\n### [API $e_msg successfully] ###"
exit 0
}
# Main
main