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