Lines 3-496
Link Here
|
3 |
# $FreeBSD: /tmp/pcvs/ports/sysutils/bsdstats/files/300.statistics.in,v 1.45 2011-09-25 21:35:26 eadler Exp $ |
3 |
# $FreeBSD: /tmp/pcvs/ports/sysutils/bsdstats/files/300.statistics.in,v 1.45 2011-09-25 21:35:26 eadler Exp $ |
4 |
# |
4 |
# |
5 |
|
5 |
|
|
|
6 |
# |
7 |
# options |
8 |
# |
9 |
CURR_VERSION="%%VERSION%%" |
10 |
USE_TOR=NO |
11 |
|
12 |
# |
13 |
# Standard commands used here |
14 |
# |
15 |
PCICONF=/usr/sbin/pciconf |
16 |
OPENSSL=/usr/bin/openssl |
17 |
CHOWN=/usr/sbin/chown |
18 |
UNAME=/usr/bin/uname |
19 |
UMASK=/usr/bin/umask |
20 |
SYSCTL=/sbin/sysctl |
21 |
AWK=/usr/bin/awk |
22 |
SED=/usr/bin/sed |
23 |
CUT=/usr/bin/cut |
24 |
JOT=/usr/bin/jot |
25 |
CHMOD=/bin/chmod |
26 |
NC=/usr/bin/nc |
27 |
WC=/usr/bin/wc |
28 |
MV=/bin/mv |
29 |
RM=/bin/rm |
30 |
|
31 |
# |
32 |
# constants |
33 |
# |
34 |
CR=$'\r' |
35 |
NL=$'\n' |
36 |
|
37 |
# |
6 |
# If there is a global system configuration file, suck it in. |
38 |
# If there is a global system configuration file, suck it in. |
7 |
# |
39 |
# |
8 |
if [ -r /etc/defaults/periodic.conf ] |
40 |
if [ -r /etc/defaults/periodic.conf ]; then |
9 |
then |
41 |
. /etc/defaults/periodic.conf |
10 |
. /etc/defaults/periodic.conf |
42 |
source_periodic_confs |
11 |
source_periodic_confs |
43 |
periodic_conf=/etc/periodic.conf |
12 |
periodic_conf=/etc/periodic.conf |
|
|
13 |
else |
44 |
else |
14 |
. /etc/rc.conf # For systems without periodic.conf, use rc.conf |
45 |
. /etc/rc.conf # For systems without periodic.conf, use rc.conf |
15 |
if [ -r /etc/rc.conf.local ] |
46 |
if [ -r /etc/rc.conf.local ]; then |
16 |
then |
47 |
. /etc/rc.conf.local |
17 |
. /etc/rc.conf.local |
48 |
fi |
18 |
fi |
49 |
periodic_conf=/etc/rc.conf.local |
19 |
periodic_conf=/etc/rc.conf.local |
|
|
20 |
fi |
50 |
fi |
21 |
|
51 |
|
22 |
oldmask=$(umask) |
52 |
# |
23 |
umask 066 |
53 |
# global values |
24 |
timeout=10 |
54 |
# |
25 |
|
|
|
26 |
version="5.5" |
27 |
checkin_server=${monthly_statistics_checkin_server:-"rpt.bsdstats.org"} |
55 |
checkin_server=${monthly_statistics_checkin_server:-"rpt.bsdstats.org"} |
28 |
bsdstats_log=${monthly_statistics_logfile:-"/var/log/bsdstats"} |
56 |
bsdstats_log=${monthly_statistics_logfile:-"/var/log/bsdstats"} |
29 |
id_token_file='/var/db/bsdstats' |
57 |
id_token_file='/var/db/bsdstats' |
|
|
58 |
checkin_server_description=${checkin_server} |
59 |
nc_host=${checkin_server} |
60 |
nc_port=80 |
61 |
http_header_proxy_auth="" |
30 |
|
62 |
|
31 |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:%%PREFIX%%/sbin:%%PREFIX%%/bin |
|
|
32 |
export PATH |
33 |
|
63 |
|
34 |
unset HTTP_USER_AGENT |
64 |
oldmask=$(${UMASK}) |
|
|
65 |
${UMASK} 066 |
66 |
timeout=10 |
35 |
|
67 |
|
36 |
IFS=" |
68 |
## |
37 |
" |
69 |
## Procedures |
|
|
70 |
## |
38 |
|
71 |
|
39 |
random () { |
72 |
echo_begin() { |
40 |
jot -r 1 0 900 |
73 |
echo -n "$1 ... " |
41 |
} |
74 |
} |
42 |
|
75 |
|
43 |
# RFC 2396 |
76 |
echo_end_success() { |
44 |
uri_escape () { |
77 |
echo "SUCCESS" |
45 |
echo ${1+$@} | sed -e ' |
|
|
46 |
s/%/%25/g |
47 |
s/;/%3b/g |
48 |
s,/,%2f,g |
49 |
s/?/%3f/g |
50 |
s/:/%3a/g |
51 |
s/@/%40/g |
52 |
s/&/%26/g |
53 |
s/=/%3d/g |
54 |
s/+/%2b/g |
55 |
s/\$/%24/g |
56 |
s/,/%2c/g |
57 |
s/ /%20/g |
58 |
' |
59 |
} |
60 |
|
61 |
do_fetch () { |
62 |
url="http://$checkin_server/scripts/$1" |
63 |
case $(uname) in |
64 |
FreeBSD ) |
65 |
/usr/bin/fetch -T "$timeout" -q -o - "$url" |
66 |
;; |
67 |
* ) |
68 |
/usr/bin/ftp -q "$timeout" -V -o - "$url" |
69 |
;; |
70 |
esac |
71 |
} |
78 |
} |
72 |
|
79 |
|
73 |
check_dns () { |
80 |
echo_err() { |
74 |
if [ `%%DIG%% bsdstats.org txt | grep TXT | grep UP | wc -l` = 0 ] |
81 |
echo "$1" >&2 |
75 |
then |
|
|
76 |
echo "DNS not reachable, Network Down?" |
77 |
exit |
78 |
fi |
79 |
} |
82 |
} |
80 |
|
83 |
|
81 |
send_devices () { |
84 |
log() { # log(categ,msg) |
82 |
case $(uname) in |
85 |
echo "[`date "+%Y-%m-%d %H:%M:%S"`] $1 $2" >> $bsdstats_log |
83 |
FreeBSD ) |
|
|
84 |
for line in `/usr/sbin/pciconf -l` |
85 |
do |
86 |
DRIVER=`echo $line | awk -F\@ '{print $1}'` |
87 |
DEV=`echo $line | awk '{print $4}' | cut -c8-15` |
88 |
CLASS=`echo $line | awk '{print $2}' | cut -c9-14` |
89 |
query_string=$query_string`echo \&dev[]=$DRIVER:$DEV:$CLASS` |
90 |
done |
91 |
|
92 |
report_devices |
93 |
;; |
94 |
* ) |
95 |
# Not supported |
96 |
;; |
97 |
esac |
98 |
} |
86 |
} |
99 |
|
87 |
|
100 |
send_ports () { |
88 |
fail() { # fail(msg): argument is simple user-level message, detailed log message is assumed to be printed before 'fail' invokation |
101 |
case $(uname) in |
89 |
# log error |
102 |
FreeBSD ) |
90 |
log "TERM" "$1 (failure)" |
103 |
|
91 |
# let user know |
104 |
############################################################################ |
92 |
echo_err "BSDstats failed: $1" |
105 |
# At the time of this writing (2012-07-24), FreeBSD is switching from |
93 |
# bail out |
106 |
# pkg_* tools to pkgng. Starting in FreeBSD 9.1, pkgng will become the |
94 |
${UMASK} $oldmask |
107 |
# default tool for package management. |
95 |
exit 1 |
108 |
# |
|
|
109 |
# Until pkg_* tools are officially declared unsupported, they will need |
110 |
# legacy support in ports like this one. When the need for this support is |
111 |
# officially discontinued, please do the following: |
112 |
# |
113 |
# 1. Delete the contents of this "LEGACY" section: |
114 |
# |
115 |
#-----BEGIN LEGACY----- |
116 |
# Detect pkgng |
117 |
if [ -e /var/db/pkg/local.sqlite ]; then |
118 |
|
119 |
# Use pkgng |
120 |
|
121 |
for line in `pkg info | awk '{ print $1 }'` |
122 |
do |
123 |
category=`pkg info -q -o ${line} | sed 's/\/.*//g'` |
124 |
line=$(uri_escape $line) |
125 |
category=$(uri_escape $category) |
126 |
query_string=$query_string`echo \&port[]=${category}:${line}` |
127 |
done |
128 |
|
129 |
else |
130 |
|
131 |
# Use pkg_* tools |
132 |
|
133 |
for line in `pkg_info | awk '{ print $1 }'` |
134 |
do |
135 |
category=`grep "@comment ORIGIN" /var/db/pkg/${line}/+CONTENTS | sed -E 's/^\@comment ORIGIN:(.+)\/.+/\1/g'` |
136 |
line=$(uri_escape $line) |
137 |
category=$(uri_escape $category) |
138 |
query_string=$query_string`echo \&port[]=${category}:${line}` |
139 |
done |
140 |
|
141 |
fi |
142 |
#-----END LEGACY----- |
143 |
# |
144 |
# 2. Uncomment the contents of this "PKGNG" section: |
145 |
# |
146 |
#-----BEGIN PKGNG----- |
147 |
# for line in `pkg info | awk '{ print $1 }'` |
148 |
# do |
149 |
# category=`pkg info -q -o ${line} | sed 's/\/.*//g'` |
150 |
# line=$(uri_escape $line) |
151 |
# category=$(uri_escape $category) |
152 |
# query_string=$query_string`echo \&port[]=${category}:${line}` |
153 |
# done |
154 |
#-----END PKGNG----- |
155 |
# |
156 |
# 3. Delete these comments. |
157 |
# |
158 |
# Thank you! |
159 |
############################################################################ |
160 |
|
161 |
report_ports |
162 |
;; |
163 |
* ) |
164 |
# Not supported |
165 |
;; |
166 |
esac |
167 |
} |
96 |
} |
168 |
|
97 |
|
169 |
report_ports () { |
98 |
random () { |
170 |
# Handle HTTP proxy services |
99 |
${JOT} -r 1 0 900 |
171 |
# |
100 |
} |
172 |
# HTTP_PROXY/http_proxy can take the following form: |
101 |
|
173 |
# [http://][username:password@]proxy[:port][/] |
102 |
# do_http_request: if success returns 0, and prints http.body, otherwise returns 1 |
174 |
# Authentication details may also be provided via HTTP_PROXY_AUTH: |
103 |
do_http_request () { |
175 |
# HTTP_PROXY_AUTH="basic:*:username:password" |
104 |
local meth="$1" |
176 |
# |
105 |
local url="$2" |
177 |
|
106 |
local body="$3" |
178 |
if [ -z "$HTTP_PROXY" -a -n "$http_proxy" ]; then |
107 |
local content_type="$4" |
179 |
HTTP_PROXY=$http_proxy |
108 |
local do_log="$5" |
180 |
fi |
109 |
|
181 |
if [ -n "$HTTP_PROXY" ]; then |
110 |
local resp |
182 |
# Attempt to resolve any HTTP authentication |
111 |
local lineno |
183 |
if [ -n "$HTTP_PROXY_AUTH" ]; then |
112 |
local in_header |
184 |
PROXY_AUTH_USER=`echo $HTTP_PROXY_AUTH | sed -E 's/^.+:\*:(.+):.+$/\1/g'` |
113 |
local result_count |
185 |
PROXY_AUTH_PASS=`echo $HTTP_PROXY_AUTH | sed -E 's/^.+:\*:.+:(.+)$/\1/g'` |
114 |
|
186 |
else |
115 |
if [ -n "${http_header_proxy_auth}" ]; then url="http://${checkin_server}${url}"; fi |
187 |
# Check for authentication within HTTP_PROXY |
116 |
|
188 |
HAS_HTTP_AUTH=`echo $HTTP_PROXY | sed -E 's/^(http:\/\/)?(.+:.+@)?.+/\2/'` |
117 |
local txt="${meth} ${url} HTTP/1.0" |
189 |
if [ -n "$HAS_HTTP_AUTH" ]; then |
118 |
local http_req="${txt}" |
190 |
# Found HTTP authentication details |
119 |
txt="${txt}${CR}${NL}Host: ${checkin_server}" |
191 |
PROXY_AUTH_USER=`echo $HAS_HTTP_AUTH | cut -d: -f1` |
120 |
if [ -n "${http_header_proxy_auth}" ]; then txt="${txt}${CR}${NL}Proxy-Authorization: ${http_header_proxy_auth}"; fi |
192 |
PROXY_AUTH_PASS=`echo $HAS_HTTP_AUTH | cut -d: -f2` |
121 |
txt="${txt}${CR}${NL}User-Agent: fetch"; # should be: "bsdstats-${CURR_VERSION}" |
193 |
fi |
122 |
txt="${txt}${CR}${NL}Connection: close" |
194 |
fi |
123 |
if [ -n "${content_type}" ]; then txt="${txt}${NL}Content-Type: ${content_type}"; fi |
195 |
|
124 |
if [ -n "${body}" ]; then txt="${txt}${CR}${NL}Content-Length: ${#body}"; fi |
196 |
# Determine the proxy components |
125 |
txt="${txt}${CR}${NL}${CR}${NL}${body}" |
197 |
PROXY_HOST=`echo $HTTP_PROXY | sed -E 's/^(http:\/\/)?(.+:.+@)?([^@:]+)(:.+)?/\3/'` |
126 |
|
198 |
PROXY_PORT=`echo $HTTP_PROXY | sed -E 's/^(http:\/\/)?(.+:.+@)?(.+):([0-9]+)/\4/' | sed -e 's/[^0-9]//g'` |
127 |
resp=$(echo "${txt}" | ${NC} ${nc_host} ${nc_port} 2>/dev/null) |
199 |
if [ -z "$PROXY_PORT" ]; then |
128 |
if [ $? -ne 0 ]; then |
200 |
# Use default proxy port |
129 |
if [ ${do_log} -ne 0 ]; then |
201 |
PROXY_PORT=3128 |
130 |
log "FAIL" "Failed to send data to the host ${nc_host}:${nc_port}, is network or host down?" |
202 |
fi |
131 |
fi |
203 |
fi |
132 |
return 1 |
204 |
|
133 |
fi |
205 |
# Determine the host/port netcat should connect to |
|
|
206 |
if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ]; then |
207 |
nc_host=$PROXY_HOST |
208 |
nc_port=$PROXY_PORT |
209 |
url_prefix="http://${checkin_server}" |
210 |
else |
211 |
nc_host=$checkin_server |
212 |
nc_port=80 |
213 |
fi |
214 |
|
215 |
# Proxy authentication, if required |
216 |
if [ -n "$PROXY_AUTH_USER" -a -n "$PROXY_AUTH_PASS" ]; then |
217 |
auth_base64=`echo "$PROXY_AUTH_USER:$PROXY_AUTH_PASS" | openssl base64` |
218 |
proxy_auth="Proxy-Authorization: Basic $auth_base64 |
219 |
" |
220 |
fi |
221 |
|
222 |
|
223 |
# Make the request |
224 |
string_length=`echo ${query_string} | wc -m` |
225 |
string_length=$((string_length - 1)) |
226 |
|
227 |
echo "POST ${url_prefix}/scripts/report_ports.php HTTP/1.0 |
228 |
Host: ${checkin_server} |
229 |
User-Agent: bsdstats ${version} |
230 |
Connection: close |
231 |
${proxy_auth}Content-Type: application/x-www-form-urlencoded |
232 |
Content-Length: ${string_length} |
233 |
|
234 |
token=${TOKEN}&key=${KEY}${query_string}" | \ |
235 |
nc $nc_host $nc_port | \ |
236 |
grep STATUS= | { |
237 |
local IFS |
238 |
IFS='= |
239 |
' |
240 |
|
241 |
while read var val |
242 |
do |
243 |
case $var in |
244 |
STATUS) |
245 |
if [ $val = "OK" ] |
246 |
then |
247 |
echo "[`date`] System Ports reported" |
248 |
else |
249 |
echo "[`date`] System Ports not reported, exiting" |
250 |
exit |
251 |
fi |
252 |
;; |
253 |
*) |
254 |
echo "[`date`] Error with fetch to server" |
255 |
exit |
256 |
;; |
257 |
esac |
258 |
done |
259 |
} >> $bsdstats_log |
260 |
|
134 |
|
|
|
135 |
local IFS=${NL}${CR} |
136 |
lineno=0 |
137 |
in_header=1 |
138 |
http_result="" |
139 |
for str in ${resp}; do |
140 |
if [ $lineno -eq 0 ] ; then |
141 |
if expr "${str}" : "^HTTP/1\.[01] 200 OK$" > /dev/null; then |
142 |
# ok |
143 |
true |
144 |
else |
145 |
if [ ${do_log} -ne 0 ]; then |
146 |
log "FAIL" "Failed HTTP query: request='${http_req}' -> response='${str}'" |
147 |
fi |
148 |
return 2 |
149 |
fi |
150 |
elif [ $lineno -ge 1 -a $in_header -eq 1 ] ; then |
151 |
if [ -z "${str}" ]; then |
152 |
in_header=0 |
153 |
result_count=0 |
154 |
fi |
155 |
else |
156 |
if [ $result_count -eq 0 ]; then |
157 |
http_result="${str}" |
158 |
else |
159 |
http_result="${http_result}${NL}${str}" |
160 |
fi |
161 |
result_count=$(($result_count+1)) |
162 |
fi |
163 |
lineno=$(($lineno+1)) |
164 |
done |
165 |
echo "${http_result}" |
166 |
return 0 |
167 |
} |
168 |
|
169 |
extract_field () { |
170 |
# charset of the value, besides alnum covers base64 encoding charset (/+), and single quote |
171 |
echo "$1" | grep "^${2}=" | tail -1 | sed -E -e "s/^${2}=([a-zA-Z0-9=/+']+).*/\1/g" |
172 |
} |
173 |
|
174 |
do_http_request_check_status () { |
175 |
local body |
176 |
local status |
177 |
local what="$5" |
178 |
# run request |
179 |
body=$(do_http_request "$1" "$2" "$3" "$4" 1) |
180 |
if [ $? -ne 0 ]; then |
181 |
fail "HTTP query failed during ${what}" |
182 |
fi |
183 |
# check status |
184 |
status=$(extract_field "${body}" "STATUS") |
185 |
case "${status}" in |
186 |
OK) |
187 |
# pass |
188 |
true |
189 |
;; |
190 |
FAIL) |
191 |
log "FAIL" "Got STATUS=FAIL from the server in during ${what}" |
192 |
fail "${what} request failed" |
193 |
;; |
194 |
*) |
195 |
fail "Server didn't return the status for ${what}" |
196 |
;; |
197 |
esac |
261 |
} |
198 |
} |
262 |
|
199 |
|
263 |
report_devices () { |
200 |
uri_escape () { |
264 |
do_fetch report_devices.php?token=$TOKEN\&key=$KEY$query_string | { |
201 |
# RFC 2396 |
265 |
local IFS |
202 |
echo "${1+$@}" | ${SED} -e ' |
266 |
IFS='= |
203 |
s/%/%25/g |
267 |
' |
204 |
s/;/%3b/g |
268 |
|
205 |
s,/,%2f,g |
269 |
while read var val |
206 |
s/?/%3f/g |
270 |
do |
207 |
s/:/%3a/g |
271 |
case $var in |
208 |
s/@/%40/g |
272 |
STATUS) |
209 |
s/&/%26/g |
273 |
if [ $val = "OK" ] |
210 |
s/=/%3d/g |
274 |
then |
211 |
s/+/%2b/g |
275 |
echo "[`date`] System Devices reported" |
212 |
s/\$/%24/g |
276 |
else |
213 |
s/,/%2c/g |
277 |
echo "[`date`] System Devices not reported, exiting" |
214 |
s/ /%20/g |
278 |
exit |
215 |
' |
279 |
fi |
|
|
280 |
;; |
281 |
*) |
282 |
echo "[`date`] Error with fetch to server" |
283 |
exit |
284 |
;; |
285 |
esac |
286 |
done |
287 |
} >> $bsdstats_log |
288 |
} |
216 |
} |
289 |
|
217 |
|
290 |
get_id_token () { |
218 |
parse_http_proxy_string () { |
291 |
if [ -f $id_token_file ] |
219 |
# Handle HTTP proxy services |
292 |
then |
220 |
# |
293 |
if [ `wc -l < $id_token_file` -lt 3 ] |
221 |
# HTTP_PROXY/http_proxy can take the following form: |
294 |
then |
222 |
# [http://][username:password@]proxy[:port][/] |
295 |
rm $id_token_file |
223 |
# Authentication details may also be provided via HTTP_PROXY_AUTH: |
|
|
224 |
# HTTP_PROXY_AUTH="basic:*:username:password" |
225 |
# |
226 |
# IN: * HTTP_PROXY or http_proxy |
227 |
# IN: * HTTP_PROXY_AUTH |
228 |
# OUT: * http_header_proxy_auth |
229 |
# OUT: * nc_host |
230 |
# OUT: * nc_port |
231 |
|
232 |
local PROXY_AUTH_USER |
233 |
local PROXY_AUTH_PASS |
234 |
local PROXY_HOST |
235 |
local PROXY_PORT |
236 |
|
237 |
if [ -z "$HTTP_PROXY" -a -n "$http_proxy" ]; then |
238 |
HTTP_PROXY=$http_proxy |
239 |
fi |
240 |
if [ -n "$HTTP_PROXY" ]; then |
241 |
# Attempt to resolve any HTTP authentication |
242 |
if [ -n "$HTTP_PROXY_AUTH" ]; then |
243 |
PROXY_AUTH_USER=$(echo $HTTP_PROXY_AUTH | ${SED} -E 's/^.+:\*:(.+):.+$/\1/g') |
244 |
PROXY_AUTH_PASS=$(echo $HTTP_PROXY_AUTH | ${SED} -E 's/^.+:\*:.+:(.+)$/\1/g') |
245 |
else |
246 |
# Check for authentication within HTTP_PROXY |
247 |
HAS_HTTP_AUTH=$(echo $HTTP_PROXY | ${SED} -E 's/^(http:\/\/)?((.+:.+)@)?.+/\3/') |
248 |
if [ -n "$HAS_HTTP_AUTH" ]; then |
249 |
# Found HTTP authentication details |
250 |
PROXY_AUTH_USER=$(echo $HAS_HTTP_AUTH | ${CUT} -d: -f1) |
251 |
PROXY_AUTH_PASS=$(echo $HAS_HTTP_AUTH | ${CUT} -d: -f2) |
296 |
fi |
252 |
fi |
297 |
fi |
253 |
fi |
298 |
|
254 |
|
299 |
if [ ! -f $id_token_file -o ! -s $id_token_file ] ; |
255 |
# Determine the proxy components |
300 |
then |
256 |
PROXY_HOST=$(echo $HTTP_PROXY | ${SED} -E 's/^(http:\/\/)?(.+:.+@)?([^@:]+)(:.+)?/\3/') |
301 |
IDTOKEN=$(uri_escape $( openssl rand -base64 32 ) ) |
257 |
PROXY_PORT=$(echo $HTTP_PROXY | ${SED} -E 's/^(http:\/\/)?(.+:.+@)?(.+):([0-9]+)/\4/' | ${SED} -e 's/[^0-9]//g') |
302 |
|
258 |
if [ -z "$PROXY_PORT" ]; then |
303 |
idf=$( mktemp "$id_token_file.XXXXXX" ) && \ |
259 |
# Use default proxy port |
304 |
chown root:wheel $idf && \ |
260 |
PROXY_PORT=3128 |
305 |
chmod 600 $idf |
261 |
fi |
306 |
|
262 |
fi |
307 |
do_fetch getid.php?key=$IDTOKEN | { |
263 |
|
308 |
local IFS |
264 |
# Determine the host/port netcat should connect to |
309 |
IFS='= |
265 |
if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ]; then |
310 |
' |
266 |
nc_host=$PROXY_HOST |
311 |
|
267 |
nc_port=$PROXY_PORT |
312 |
while read var val |
268 |
# Proxy authentication, if required |
313 |
do |
269 |
if [ -n "$PROXY_AUTH_USER" -a -n "$PROXY_AUTH_PASS" ]; then |
314 |
case $var in |
270 |
local auth_base64=$(echo -n "$PROXY_AUTH_USER:$PROXY_AUTH_PASS" | ${OPENSSL} base64) |
315 |
KEY) |
271 |
http_header_proxy_auth="Basic $auth_base64" |
316 |
echo "KEY=$val" |
272 |
fi |
317 |
;; |
273 |
return 0 |
318 |
TOKEN) |
274 |
else |
319 |
echo "TOKEN=$val" |
275 |
nc_host=$checkin_server |
320 |
;; |
276 |
nc_port=80 |
321 |
*) |
277 |
return 1 |
322 |
;; |
278 |
fi |
323 |
esac |
279 |
} |
324 |
done |
280 |
|
325 |
echo "VERSION=$version" |
281 |
test_connection () { |
326 |
} > $idf && \ |
282 |
local body |
327 |
|
283 |
body=$(do_http_request "HEAD" "/" "" "" 0) |
328 |
mv $idf $id_token_file |
284 |
if [ $? -ne 0 -a $? -ne 2 ]; then |
329 |
if [ ! -s $id_token_file ] ; |
285 |
log "FAIL" "Unable to connect to ${checkin_server_description}" |
330 |
then |
286 |
fail "Network or host is down?" |
331 |
echo "Nothing returned from $checkin_server" |
287 |
fi |
332 |
exit 1 |
288 |
} |
333 |
fi |
289 |
|
334 |
fi |
290 |
setup_proxies() { |
335 |
. $id_token_file |
291 |
# TOR |
336 |
KEY=$( uri_escape $KEY ) |
292 |
if [ "${USE_TOR}" = "YES" ]; then |
337 |
TOKEN=$( uri_escape $TOKEN ) |
293 |
if [ -n "${HTTP_PROXY}" -o -n "${http_proxy}" ]; then |
|
|
294 |
echo_err "Ignoring HTTP_PROXY since TOR is used" |
295 |
fi |
296 |
NC="${NC} -x localhost:9050 -X 5" |
297 |
checkin_server_description="${checkin_server_description} (through TOR)" |
298 |
return 0 |
299 |
fi |
300 |
|
301 |
# HTTP proxy |
302 |
if [ -n "${HTTP_PROXY}" -o -n "${http_proxy}" ]; then |
303 |
parse_http_proxy_string |
304 |
if [ $? -eq 0 ]; then |
305 |
checkin_server_description="${checkin_server_description} (through proxy)" |
306 |
return 0 |
307 |
fi |
308 |
fi |
309 |
|
310 |
# no proxy |
311 |
} |
312 |
|
313 |
report_devices () { |
314 |
case $(${UNAME}) in |
315 |
FreeBSD) |
316 |
local query_string="" |
317 |
local line |
318 |
for line in $(${PCICONF} -l); do |
319 |
local DRIVER=$(echo "${line}" | ${AWK} -F\@ '{print $1}') |
320 |
local DEV=$(echo "${line}" | ${AWK} '{print $4}' | ${CUT} -c8-15) |
321 |
local CLASS=$(echo "${line}" | ${AWK} '{print $2}' | ${CUT} -c9-14) |
322 |
query_string=$query_string`echo \&dev[]=${DRIVER}:${DEV}:${CLASS}` |
323 |
done |
324 |
|
325 |
echo_begin "Posting device statistics to ${checkin_server_description}" |
326 |
do_http_request_check_status "GET" "/scripts/report_devices.php?token=${TOKEN}&key=${KEY}$query_string" \ |
327 |
"" "" "system devices sumbission" |
328 |
echo_end_success |
329 |
log "INFO" "System devices reported to ${checkin_server_description}" |
330 |
;; |
331 |
*) |
332 |
# Not supported |
333 |
;; |
334 |
esac |
335 |
} |
336 |
|
337 |
report_ports () { |
338 |
case $(${UNAME}) in |
339 |
FreeBSD) |
340 |
local query_string="" |
341 |
# Detect pkgng |
342 |
if [ -e /var/db/pkg/local.sqlite ]; then |
343 |
# Use pkgng |
344 |
query_string="${query_string}$(pkg info -o "*" | ${SED} -E -e 's/\+/%2b/g' -e 's/,/%2c/g' -e 's/^([^ ]+) +([^\/]+)\/.+$/\&port[]=\2:\1/g' | tr -d '\n')" |
345 |
else |
346 |
#-----BEGIN LEGACY: to delete when FreeBSD with pkg_ tools is out of support period----- |
347 |
# Use obsolete pkg_* tools |
348 |
local line |
349 |
for line in `pkg_info | ${AWK} '{ print $1 }'`; do |
350 |
local category=`grep "@comment ORIGIN" /var/db/pkg/${line}/+CONTENTS | ${SED} -E 's/^\@comment ORIGIN:(.+)\/.+/\1/g'` |
351 |
line=$(uri_escape $line) |
352 |
category=$(uri_escape $category) |
353 |
query_string=$query_string`echo \&port[]=${category}:${line}` |
354 |
done |
355 |
#-----END LEGACY----- |
356 |
fi |
357 |
echo_begin "Posting port statistics to ${checkin_server_description}" |
358 |
do_http_request_check_status "POST" "/scripts/report_ports.php" \ |
359 |
"token=${TOKEN}&key=${KEY}${query_string}" "application/x-www-form-urlencoded" "ports sumbission" |
360 |
echo_end_success |
361 |
log "INFO" "Posted port statistics to ${checkin_server_description}" |
362 |
;; |
363 |
*) |
364 |
# Not supported |
365 |
;; |
366 |
esac |
338 |
} |
367 |
} |
339 |
|
368 |
|
|
|
369 |
get_id_token () { |
370 |
if [ -f $id_token_file ]; then |
371 |
if [ $(${WC} -l < $id_token_file) -lt 3 ]; then |
372 |
${RM} -f $id_token_file |
373 |
fi |
374 |
fi |
375 |
|
376 |
if [ ! -f $id_token_file -o ! -s $id_token_file ]; then |
377 |
# generate the token file |
378 |
echo "BSDstats runs on this system for the first time, generating registration ID" |
379 |
IDTOKEN=$(uri_escape $(${OPENSSL} rand -base64 32)) |
380 |
if [ $? -ne 0 ]; then |
381 |
fail "Failed to generate IDTOKEN" |
382 |
fi |
383 |
|
384 |
# receive KEY/TOKEN |
385 |
local body |
386 |
body=$(do_http_request "GET" "/scripts/getid.php?key=${IDTOKEN}" "" "" 1) |
387 |
if [ $? -ne 0 ]; then |
388 |
fail "HTTP query failed during key/token generation" |
389 |
fi |
390 |
KEY=$(extract_field "${body}" "KEY") |
391 |
TOKEN=$(extract_field "${body}" "TOKEN") |
392 |
# validate KEY/TOKEN |
393 |
if [ ${#KEY} -lt 10 -o ${#KEY} -gt 64 -o ${#TOKEN} -lt 10 -o ${#TOKEN} -gt 64 ]; then |
394 |
log "FAIL" "Invalid key/token received for IDTOKEN=${TOKEN}" |
395 |
fail "Invalid key/token combination received from the server" |
396 |
fi |
397 |
log "INFO" "Generated idtoken='${IDTOKEN}', received key=${KEY} and token=${TOKEN}" |
398 |
# save KEY/TOKEN |
399 |
(echo "# This file was auto-generated on $(date),"; \ |
400 |
echo "# and contains the BSDstats registration credentials"; \ |
401 |
echo "KEY=${KEY}"; echo "TOKEN=${TOKEN}"; echo "VERSION=${CURR_VERSION}") > $id_token_file && \ |
402 |
${CHOWN} root:wheel $id_token_file && \ |
403 |
${CHMOD} 600 $id_token_file |
404 |
if [ $? -ne 0 ]; then |
405 |
${RM} -f $id_token_file |
406 |
fail "Failed to create identification file $id_token_file" |
407 |
fi |
408 |
log "INFO" "Created identification file $id_token_file" |
409 |
fi |
410 |
# read the token file into the global variables |
411 |
. $id_token_file |
412 |
KEY=$(uri_escape $KEY) |
413 |
TOKEN=$(uri_escape $TOKEN) |
414 |
PREV_VERSION="${VERSION}" |
415 |
VERSION="" |
416 |
} |
340 |
|
417 |
|
341 |
enable_token () { |
418 |
enable_token () { |
342 |
do_fetch enable_token.php?key=$TOKEN\&token=$KEY | { |
419 |
# XXX key and token are swapped here, this has been like that from version 5.5, not sure if fixing will break it |
343 |
local IFS |
420 |
do_http_request_check_status "GET" "/scripts/enable_token.php?key=${TOKEN}&token=${KEY}" \ |
344 |
IFS='= |
421 |
"" "" "token enabling" |
345 |
' |
422 |
log "INFO" "System enabled" |
346 |
|
423 |
} |
347 |
while read var val |
424 |
|
348 |
do |
425 |
disable_token() { |
349 |
case $var in |
426 |
do_http_request_check_status "GET" "/scripts/disable_token.php?key=${TOKEN}&token=${KEY}" \ |
350 |
STATUS) |
427 |
"" "" "token disabling" |
351 |
if [ $val = "OK" ] |
428 |
log "INFO" "System disabled" |
352 |
then |
429 |
} |
353 |
echo "[`date`] System enabled" |
430 |
|
354 |
else |
431 |
report_system() { |
355 |
echo "[`date`] System not enabled, exiting" |
432 |
local REL=$(${UNAME} -r) |
356 |
exit |
433 |
local ARCH=$(${UNAME} -m) |
357 |
fi |
434 |
local OS=$(${UNAME} -s) |
358 |
;; |
435 |
echo_begin "Posting OS statistics to ${checkin_server_description}" |
359 |
*) |
436 |
do_http_request_check_status "GET" "/scripts/report_system.php?token=${TOKEN}&key=${KEY}&rel=${REL}&arch=${ARCH}&opsys=${OS}" \ |
360 |
echo "[`date`] Error with fetch to server" |
437 |
"" "" "OS statistics sumbission" |
361 |
exit |
438 |
echo_end_success |
362 |
;; |
439 |
log "INFO" "Posted OS statistics to ${checkin_server_description}" |
363 |
esac |
|
|
364 |
done |
365 |
} >> $bsdstats_log |
366 |
} |
367 |
|
368 |
disable_token () { |
369 |
do_fetch disable_token.php?key=$TOKEN\&token=$KEY | { |
370 |
local IFS |
371 |
IFS='= |
372 |
' |
373 |
|
374 |
while read var val |
375 |
do |
376 |
case $var in |
377 |
STATUS) |
378 |
if [ $val = "OK" ] |
379 |
then |
380 |
echo "[`date`] System disabled" |
381 |
else |
382 |
echo "[`date`] System not disabled, exiting" |
383 |
exit |
384 |
fi |
385 |
;; |
386 |
*) |
387 |
echo "[`date`] Error with fetch to server" |
388 |
exit |
389 |
;; |
390 |
esac |
391 |
done |
392 |
} >> $bsdstats_log |
393 |
} |
394 |
|
395 |
report_system () { |
396 |
do_fetch report_system.php?token=$TOKEN\&key=$KEY\&rel=$REL\&arch=$ARCH\&opsys=$OS | { |
397 |
local IFS |
398 |
IFS='= |
399 |
' |
400 |
|
401 |
while read var val |
402 |
do |
403 |
case $var in |
404 |
STATUS) |
405 |
if [ $val = "OK" ] |
406 |
then |
407 |
echo "[`date`] System reported" |
408 |
else |
409 |
echo "[`date`] System report failed, exiting" |
410 |
exit |
411 |
fi |
412 |
;; |
413 |
*) |
414 |
echo "[`date`] Error with fetch to server" |
415 |
exit |
416 |
;; |
417 |
esac |
418 |
done |
419 |
} >> $bsdstats_log |
420 |
} |
440 |
} |
421 |
|
441 |
|
422 |
report_cpu () { |
442 |
report_cpu () { |
423 |
do_fetch report_cpu.php?token=$TOKEN\&key=$KEY\&cpus=$count\&vendor=$VEN\&cpu_type=$DEV | { |
443 |
local line=$(${SYSCTL} -n hw.model) |
424 |
local IFS |
444 |
local VEN=$(echo $line | ${CUT} -d ' ' -f 1) |
425 |
IFS='= |
445 |
local DEV=$(uri_escape $(echo $line | ${CUT} -d ' ' -f 2-)) |
426 |
' |
446 |
local count=$(${SYSCTL} -n hw.ncpu) |
427 |
|
447 |
echo_begin "Posting CPU information to ${checkin_server_description}" |
428 |
while read var val |
448 |
do_http_request_check_status "GET" "/scripts/report_cpu.php?token=${TOKEN}&key=${KEY}&cpus=${count}&vendor=${VEN}&cpu_type=${DEV}" \ |
429 |
do |
449 |
"" "" "CPU information sumbission" |
430 |
case $var in |
450 |
echo_end_success |
431 |
STATUS) |
451 |
log "INFO" "Posted CPU information to ${checkin_server_description}" |
432 |
if [ $val = "OK" ] |
|
|
433 |
then |
434 |
echo "[`date`] System CPU reported" |
435 |
else |
436 |
echo "[`date`] System CPU report failed, exiting" |
437 |
exit |
438 |
fi |
439 |
;; |
440 |
*) |
441 |
echo "[`date`] Error with fetch to server" |
442 |
exit |
443 |
;; |
444 |
esac |
445 |
done |
446 |
} >> $bsdstats_log |
447 |
} |
452 |
} |
|
|
453 |
|
454 |
## |
455 |
## MAIN: processing begins here |
456 |
## |
457 |
|
448 |
case "$monthly_statistics_enable" in |
458 |
case "$monthly_statistics_enable" in |
449 |
[Yy][Ee][Ss]) |
459 |
[Yy][Ee][Ss]) |
450 |
check_dns |
460 |
# network setup |
451 |
REL=`/usr/bin/uname -r` |
461 |
setup_proxies |
452 |
ARCH=`/usr/bin/uname -m` |
462 |
test_connection |
453 |
OS=`/usr/bin/uname -s` |
463 |
log "INIT" "Connected to ${checkin_server_description}" |
454 |
get_id_token |
464 |
# sleep random number of seconds by default |
455 |
test X"$1" = X-nodelay || sleep `random` |
465 |
test X"$1" = X-nodelay || sleep $(random) |
456 |
enable_token |
466 |
# prepare |
457 |
report_system |
467 |
get_id_token |
458 |
echo "Posting monthly OS statistics to $checkin_server" |
468 |
# begin |
459 |
if [ X"$1" != X-nodelay ]; then |
469 |
enable_token |
460 |
case "$monthly_statistics_report_devices" in |
470 |
report_system |
461 |
[Yy][Ee][Ss]) |
471 |
# optional parts |
462 |
send_devices |
472 |
case "$monthly_statistics_report_devices" in |
463 |
echo "Posting monthly device statistics to $checkin_server" |
473 |
[Yy][Ee][Ss]) |
464 |
line=$( sysctl -n hw.model ) |
474 |
report_devices |
465 |
VEN=$( echo $line | cut -d ' ' -f 1 ) |
475 |
report_cpu |
466 |
DEV=$( uri_escape $( echo $line | cut -d ' ' -f 2- ) ) |
476 |
;; |
467 |
count=$( sysctl -n hw.ncpu ) |
477 |
*) |
468 |
report_cpu |
478 |
echo "Posting monthly device/CPU statistics disabled" |
469 |
echo "Posting monthly CPU statistics to $checkin_server" |
479 |
echo " set monthly_statistics_report_devices=\"YES\" in $periodic_conf" |
470 |
;; |
480 |
;; |
471 |
*) |
481 |
esac |
472 |
echo "Posting monthly device/CPU statistics disabled" |
482 |
case "$monthly_statistics_report_ports" in |
473 |
echo " set monthly_statistics_report_devices=\"YES\" in $periodic_conf" |
483 |
[Yy][Ee][Ss]) |
474 |
;; |
484 |
report_ports |
475 |
esac |
485 |
;; |
476 |
case "$monthly_statistics_report_ports" in |
486 |
*) |
477 |
[Yy][Ee][Ss]) |
487 |
echo "Posting monthly ports statistics disabled" |
478 |
send_ports |
488 |
echo " set monthly_statistics_report_ports=\"YES\" in $periodic_conf" |
479 |
echo "Posting monthly ports statistics to $checkin_server" |
489 |
;; |
480 |
;; |
490 |
esac |
481 |
*) |
491 |
# end |
482 |
echo "Posting monthly ports statistics disabled" |
492 |
disable_token |
483 |
echo " set monthly_statistics_report_ports=\"YES\" in $periodic_conf" |
493 |
;; |
484 |
;; |
494 |
*) |
485 |
esac |
495 |
echo "Posting monthly OS statistics disabled" |
486 |
fi |
496 |
echo " set monthly_statistics_enable=\"YES\" in $periodic_conf" |
487 |
disable_token |
|
|
488 |
;; |
489 |
*) |
490 |
echo "Posting monthly OS statistics disabled" |
491 |
echo " set monthly_statistics_enable=\"YES\" in $periodic_conf" |
492 |
;; |
497 |
;; |
493 |
esac |
498 |
esac |
494 |
|
499 |
|
495 |
umask $oldmask |
500 |
# success |
496 |
exit $rc |
501 |
log "SUCC" "Finished successfully" |
|
|
502 |
${UMASK} $oldmask |
503 |
exit 0 |