File: prey.sh

package info (click to toggle)
prey 0.6.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,016 kB
  • ctags: 174
  • sloc: sh: 2,108; perl: 1,258; python: 628; makefile: 21
file content (189 lines) | stat: -rwxr-xr-x 5,146 bytes parent folder | download | duplicates (2)
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
#!/bin/bash
####################################################################
# Prey Bash Client - (c) Fork Ltd.
# http://preyproject.com
# License: GPLv3
####################################################################

PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
readonly base_path=$(dirname "$0")
readonly modules_path='/var/lib/prey/modules'

####################################################################
# base files inclusion
####################################################################

. "$base_path/version"
. /etc/prey/config
[ ! -f "lang/$lang" ] && lang='en' # fallback to english in case the lang is missing
. "$base_path/lang/$lang"
. "$base_path/core/base"
. "$base_path/platform/$os/functions"

# if [ `number_of_instances_of prey.sh` -gt 1 ]; then
# 	log ' -- Prey is already running!'
# 	exit 1
# fi

log "\n${cyan} ## $STRING_START\n ## $(uname -a)\n ## $(date)${color_end}\n"

####################################################################
# lets check if we're actually connected
# if we're not, lets try to connect to a wifi access point
####################################################################

check_net_status
if [ $connected == 0 ]; then

	if [ "$auto_connect" == "y" ]; then
		log "$STRING_TRY_TO_CONNECT"
		try_to_connect
	fi

	# ok, lets check again, after waiting a bit
	sleep 5
	check_net_status

	if [ $connected == 0 ]; then

		log "$STRING_NO_CONNECT_TO_WIFI"
		if [ -f "$last_response" ]; then # offline actions were enabled

			log ' -- Offline actions enabled!'
			offline_mode=1
			get_last_response
			process_module_config

		else
			exit 1
		fi

	fi
fi

####################################################################
# check API key and perform stuff accordingly
####################################################################

if [ $connected == 1 ]; then

	log ' -- Got network connection!'

	# we do have an API key but no device key, so let's try to add this device under the account
	if [[ -n "$api_key" && -z "$device_key" ]]; then

		log "\n${bold} >> Registering device under account!${bold_end}\n"
		self_setup

	fi

fi

####################################################################
# verify if installation and keys are correct, if requested
####################################################################

if [ -n "$check_mode" ]; then

	log "\n${bold} == Verifying Prey installation...${bold_end}\n"
	verify_installation

	if [ "$post_method" == "http" ]; then
		log "\n${bold} == Verifying API and Device keys...${bold_end}\n"
		verify_keys
	elif [ "$post_method" == "email" ]; then
		log "\n${bold} == Verifying SMTP settings...${bold_end}\n"
		verify_smtp_settings
	fi

	exit $?

fi

####################################################################
# wait a few seconds to make sure our request doesn't get dropped
# due to clashes with the other zillion requests to the CP
####################################################################

# only do this if Prey is being run from cron in Mac and Linux
if [[ "$os" != "windows" && -n "$(running_from_cron)" && "$post_method" == "http" ]]; then
	seconds_to_wait=$(get_random_number 59)
	log " -- Pausing for ${seconds_to_wait} seconds..."
	sleep $seconds_to_wait
fi

####################################################################
# if there's a URL in the config, lets see if it actually exists
# if it doesn't, the program will shut down gracefully
####################################################################

# create tmpdir for downloading stuff, storing files, etc
create_tmpdir

if [[ $connected == 1 && -n "$check_url" ]]; then

	log "$STRING_CHECK_URL"

	log "\n${bold} == Verifying status...${bold_end}\n"
	check_device_status

	if [ -z "$response_status" ]; then

		log_response_error "$check_url"

	else

		log " -- Got status code $response_status!"
		[ "$response_status" == "$missing_status_code" ] && device_missing=1
		process_config
		process_module_config

		if [ -n "$device_missing" ]; then

			log "$STRING_PROBLEM"

			####################################################################
			# initialize and fire off active modules
			####################################################################

			set +e # error mode off, just continue if a module fails
			log " -- Running active report modules..."
			run_active_modules

			####################################################################
			# lets send whatever we've gathered
			####################################################################

			log "\n${bold} == Sending report!${bold_end}\n"
			send_report

			log "\n$STRING_DONE"

		else

			log "$STRING_NO_PROBLEM"

		fi

	fi

fi

####################################################################
# if we have any pending actions, run them
####################################################################

check_running_actions

if [ "${#actions[*]}" -gt 0 ]; then
	run_pending_actions & disown -h
else
	cleanup
fi

# if on demand mode was activated, and we're not being by on demand mode itself
if [[ -z "$on_demand_call" && "$on_demand_mode" == "true" ]]; then
	enable_on_demand_mode
fi

# exit 0