forked from prey/prey-bash-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse
139 lines (98 loc) · 3.93 KB
/
response
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
#!/bin/bash
####################################################################
# Prey Core Response Functions - (c) 2010 - Fork Ltd.
# URL: http://preyproject.com
# License: GPLv3
####################################################################
process_xml(){
# inmense thanks to this post, it saved my day
# http://edwardawebb.com/linux/scope-issue-bash-loops
STDOUT=$(echo -e "$1")
while read line; do
local key=$(get_key "$line")
local value=$(get_value "$line")
# write config value to file
[ `find_in "$line" 'save="true"'` ] && save_config_value "$key" "$value"
set_config "$key" "$value"
done <<< "$STDOUT"
}
process_config(){
local prey_config=`echo -e "$response_body" | awk -F"[<>]" ' /<configuration>/,/<\/configuration>/' | grep -v "configuration>"`
[ -z "$prey_config" ] && return 1
log "\n${bold} == Reading configuration...${bold_end}\n"
process_xml "$prey_config"
if [ "$offline_actions" == "true" ]; then # offline actions selected
echo -e "$response_body" > "$last_response" 2> /dev/null
chmod 600 "$last_response" 2> /dev/null
elif [ -f "$last_response" ]; then # otherwise, lets make sure there's nothing there
rm -f "$last_response" 2> /dev/null
fi
# check the current delay against the instruction from the server
new_delay=$(get_delay_for $delay)
if [ "`get_current_delay`" != "$new_delay" ]; then
log " -- Setting frequency to $delay!"
update_execution_delay "$new_delay"
else
log " -- Frequency in sync ($delay)."
fi
# check the current version against the one gotten from the server
if [[ -z "$on_demand_call" && "$auto_update" == "true" && `is_greater_than $current_release $version` == 1 ]]; then
log " -- New Prey version found! Auto-update selected so let's try to upgrade."
run_prey_updater
fi
}
# Prey expects a <configuration> and <modules> section in the xml
# and activates de modules as requested. format should be as follows:
# <device>
# <missing>true</missing>
# </device>
# <configuration>
# <delay>10</delay>
# </configuration>
# <modules>
# <module name="alert" active="true">
# <alert-message>Give it back!</alert-message>
# </module>
# <module name="network" active="true">
# <traceroute>n</traceroute>
# </module>
# </modules>
# it should also work with one-line module entries (with no configuration), such as
# <module name="location" active="true" />
process_module_config(){
local module_config=`echo -e "$response_body" | awk -F"[<>]" ' /<modules>/,/<\/modules>/' | grep -v "\/module" | sed '1d'`
[ -z "$module_config" ] && return 1
log "\n${bold} == Reading module configuration...${bold_end}\n"
STDOUT=$(echo -e "$module_config")
while read line; do
if [ `find_in "$line" 'name='` ]; then # we have a module node
local module_name=$(get_attribute 'name' "$line")
log " -- Got instructions for $module_name module."
[ "$auto_update" == "true" ] && local upstream_version=$(get_attribute 'version' "$line") # auto_update is enabled
setup_module $module_name $upstream_version
if [ $? == 1 ]; then # we got an error installing the new module
log " !! Couldn't install $module_name module from repository."
unset module_name
continue
else # lets see if its a report module or an action module
initialize_module $module_name
local module_type=$(get_attribute 'type' "$line")
if [[ "$module_type" == 'action' || "$module_type" == "tunnel" ]]; then
if [ `find_in "$line" 'function='` ]; then # a specific function was requested
local function_name=$(get_attribute 'function' "$line")
else
local function_name=''
fi
add_action $module_name $function_name
unset function_name
else # then its a report module
active_modules="$active_modules $module_name"
fi
fi
elif [ -n "$module_name" ]; then # config line for module $module
local key=$(get_key "$line")
local value=$(get_value "$line")
set_module_config "$module_name" "$key" "$value"
fi
done <<< "$STDOUT"
}