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