1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
#!/bin/sh -
#
# $FreeBSD: ports/sysutils/bsdstats/files/300.statistics,v 1.38 2006/12/05 13:49:45 scrappy Exp $
# $NetBSD: 300.statistics,v 1.1.1.1 2010/03/12 00:13:40 morr Exp $
#
# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
. /etc/defaults/periodic.conf
source_periodic_confs
periodic_conf=/etc/periodic.conf
else
. /etc/rc.conf # For systems without periodic.conf, use rc.conf
if [ -r /etc/rc.conf.local ]
then
. /etc/rc.conf.local
fi
periodic_conf=/etc/rc.conf.local
fi
oldmask=$(umask)
umask 066
version="5.3"
checkin_server=${monthly_statistics_checkin_server:-"rpt.bsdstats.org"}
bsdstats_log=${monthly_statistics_logfile:-"/var/log/bsdstats"}
id_token_file='/var/db/bsdstats'
PATH=/sbin:/bin:/usr/sbin:/usr/bin:@PREFIX@/sbin:@PREFIX@/bin
export PATH
unset HTTP_USER_AGENT
IFS="
"
random () {
jot -r 1 0 900
}
# RFC 2396
uri_escape () {
echo ${1+$@} | sed -e '
s/%/%25/g
s/;/%3b/g
s,/,%2f,g
s/?/%3f/g
s/:/%3a/g
s/@/%40/g
s/&/%26/g
s/=/%3d/g
s/+/%2b/g
s/\$/%24/g
s/,/%2c/g
s/ /%20/g
'
}
do_fetch () {
url="http://$checkin_server/scripts/$1"
case $(uname) in
FreeBSD )
/usr/bin/fetch -q -o - "$url"
;;
* )
/usr/bin/ftp -V -o - "$url"
;;
esac
}
check_dns () {
if [ `dig bsdstats.org txt | grep TXT | grep UP | wc -l` = 0 ]
then
echo "DNS not reachable, Network Down?"
exit
fi
}
send_devices () {
case $(uname) in
FreeBSD )
for line in `/usr/sbin/pciconf -l`
do
DRIVER=`echo $line | awk -F\@ '{print $1}'`
DEV=`echo $line | awk '{print $4}' | cut -c8-15`
CLASS=`echo $line | awk '{print $2}' | cut -c9-14`
query_string=$query_string`echo \&dev[]=$DRIVER:$DEV:$CLASS`
done
report_devices
;;
* )
# Not supported
;;
esac
}
send_ports () {
case $(uname) in
FreeBSD )
for line in `/usr/sbin/pkg_info | /usr/bin/awk '{print $1}' `
do
category=`grep "@comment ORIGIN" /var/db/pkg/${line}/+CONTENTS | sed -E 's/^\@comment ORIGIN:(.+)\/.+/\1/g'`
line=$(uri_escape $line)
category=$(uri_escape $category)
query_string=$query_string`echo \&port[]=${category}:${line}`
done
report_ports
;;
NetBSD|DragonFlyBSD )
for line in `/usr/sbin/pkg_info | /usr/bin/awk '{print $1}' `
do
category=`pkg_info -Q PKGPATH ${line}|cut -d/ -f1`
line=$(uri_escape $line)
category=$(uri_escape $category)
query_string=$query_string`echo \&port[]=${category}:${line}`
done
report_ports
;;
* )
# Not supported
;;
esac
}
report_ports () {
# Handle HTTP proxy services
#
# HTTP_PROXY/http_proxy can take the following form:
# [http://][username:password@]proxy[:port][/]
# Authentication details may also be provided via HTTP_PROXY_AUTH:
# HTTP_PROXY_AUTH="basic:*:username:password"
#
if [ -z "$HTTP_PROXY" -a -n "$http_proxy" ]; then
HTTP_PROXY=$http_proxy
fi
if [ -n "$HTTP_PROXY" ]; then
# Attempt to resolve any HTTP authentication
if [ -n "$HTTP_PROXY_AUTH" ]; then
PROXY_AUTH_USER=`echo $HTTP_PROXY_AUTH | sed -E 's/^.+:\*:(.+):.+$/\1/g'`
PROXY_AUTH_PASS=`echo $HTTP_PROXY_AUTH | sed -E 's/^.+:\*:.+:(.+)$/\1/g'`
else
# Check for authentication within HTTP_PROXY
HAS_HTTP_AUTH=`echo $HTTP_PROXY | sed -E 's/^(http:\/\/)?(.+:.+@)?.+/\2/'`
if [ -n "$HAS_HTTP_AUTH" ]; then
# Found HTTP authentication details
PROXY_AUTH_USER=`echo $HAS_HTTP_AUTH | cut -d: -f1`
PROXY_AUTH_PASS=`echo $HAS_HTTP_AUTH | cut -d: -f2`
fi
fi
# Determine the proxy components
PROXY_HOST=`echo $HTTP_PROXY | sed -E 's/^(http:\/\/)?(.+:.+@)?([^@:]+)(:.+)?/\3/'`
PROXY_PORT=`echo $HTTP_PROXY | sed -E 's/^(http:\/\/)?(.+:.+@)?(.+):([0-9]+)/\4/' | sed -e 's/[^0-9]//g'`
if [ -z "$PROXY_PORT" ]; then
# Use default proxy port
PROXY_PORT=3128
fi
fi
# Determine the host/port netcat should connect to
if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ]; then
nc_host=$PROXY_HOST
nc_port=$PROXY_PORT
url_prefix="http://${checkin_server}"
else
nc_host=$checkin_server
nc_port=80
fi
# Proxy authentication, if required
if [ -n "$PROXY_AUTH_USER" -a -n "$PROXY_AUTH_PASS" ]; then
auth_base64=`echo "$PROXY_AUTH_USER:$PROXY_AUTH_PASS" | openssl base64`
proxy_auth="Proxy-Authorization: Basic $auth_base64
"
fi
# Make the request
string_length=`echo ${query_string} | wc -m`
string_length=`expr ${string_length} - 1`
echo "POST ${url_prefix}/scripts/report_ports.php HTTP/1.0
Host: ${checkin_server}
User-Agent: bsdstats ${version}
Connection: close
${proxy_auth}Content-Type: application/x-www-form-urlencoded
Content-Length: ${string_length}
token=${TOKEN}&key=${KEY}${query_string}" | \
nc $nc_host $nc_port | \
grep STATUS= | {
local IFS
IFS='=
'
while read var val
do
case $var in
STATUS)
if [ $val = "OK" ]
then
echo "[`date`] System Ports reported"
else
echo "[`date`] System Ports not reported, exiting"
exit
fi
;;
*)
echo "[`date`] Error with fetch to server"
exit
;;
esac
done
} >> $bsdstats_log
}
report_devices () {
do_fetch report_devices.php?token=$TOKEN\&key=$KEY$query_string | {
local IFS
IFS='=
'
while read var val
do
case $var in
STATUS)
if [ $val = "OK" ]
then
echo "[`date`] System Devices reported"
else
echo "[`date`] System Devices not reported, exiting"
exit
fi
;;
*)
echo "[`date`] Error with fetch to server"
exit
;;
esac
done
} >> $bsdstats_log
}
get_id_token () {
if [ -f $id_token_file ]
then
if [ `cat /var/db/bsdstats | wc -l` -lt 3 ]
then
rm $id_token_file
fi
fi
if [ ! -f $id_token_file -o ! -s $id_token_file ] ;
then
IDTOKEN=$(uri_escape $( openssl rand -base64 32 ) )
idf=$( mktemp "$id_token_file.XXXXXX" ) && \
chown root:wheel $idf && \
chmod 600 $idf
do_fetch getid.php?key=$IDTOKEN | {
local IFS
IFS='=
'
while read var val
do
case $var in
KEY)
echo "KEY=$val"
;;
TOKEN)
echo "TOKEN=$val"
;;
*)
;;
esac
done
echo "VERSION=$version"
} > $idf && \
mv $idf $id_token_file
if [ ! -s $id_token_file ] ;
then
echo "Nothing returned from $checkin_server"
exit 1
fi
fi
. $id_token_file
KEY=$( uri_escape $KEY )
TOKEN=$( uri_escape $TOKEN )
}
enable_token () {
do_fetch enable_token.php?key=$TOKEN\&token=$KEY | {
local IFS
IFS='=
'
while read var val
do
case $var in
STATUS)
if [ $val = "OK" ]
then
echo "[`date`] System enabled"
else
echo "[`date`] System not enabled, exiting"
exit
fi
;;
*)
echo "[`date`] Error with fetch to server"
exit
;;
esac
done
} >> $bsdstats_log
}
disable_token () {
do_fetch disable_token.php?key=$TOKEN\&token=$KEY | {
local IFS
IFS='=
'
while read var val
do
case $var in
STATUS)
if [ $val = "OK" ]
then
echo "[`date`] System disabled"
else
echo "[`date`] System not disabled, exiting"
exit
fi
;;
*)
echo "[`date`] Error with fetch to server"
exit
;;
esac
done
} >> $bsdstats_log
}
report_system () {
do_fetch report_system.php?token=$TOKEN\&key=$KEY\&rel=$REL\&arch=$ARCH\&opsys=$OS | {
local IFS
IFS='=
'
while read var val
do
case $var in
STATUS)
if [ $val = "OK" ]
then
echo "[`date`] System reported"
else
echo "[`date`] System report failed, exiting"
exit
fi
;;
*)
echo "[`date`] Error with fetch to server"
exit
;;
esac
done
} >> $bsdstats_log
}
report_cpu () {
do_fetch report_cpu.php?token=$TOKEN\&key=$KEY\&cpus=$count\&vendor=$VEN\&cpu_type=$DEV | {
local IFS
IFS='=
'
while read var val
do
case $var in
STATUS)
if [ $val = "OK" ]
then
echo "[`date`] System CPU reported"
else
echo "[`date`] System CPU report failed, exiting"
exit
fi
;;
*)
echo "[`date`] Error with fetch to server"
exit
;;
esac
done
} >> $bsdstats_log
}
case "$monthly_statistics_enable" in
[Yy][Ee][Ss])
check_dns
HN=`/bin/hostname`
REL=`/usr/bin/uname -r`
ARCH=`/usr/bin/uname -m`
OS=`/usr/bin/uname -s`
get_id_token
test X"$1" = X-nodelay || sleep `random`
enable_token
report_system
echo "Posting monthly OS statistics to $checkin_server"
case "$monthly_statistics_report_devices" in
[Yy][Ee][Ss])
send_devices
echo "Posting monthly device statistics to $checkin_server"
line=$( sysctl -n hw.model )
VEN=$( echo $line | cut -d ' ' -f 1 )
DEV=$( uri_escape $( echo $line | cut -d ' ' -f 2- ) )
count=$( sysctl -n hw.ncpu )
report_cpu
echo "Posting monthly CPU statistics to $checkin_server"
;;
*)
echo "Posting monthly device/CPU statistics disabled"
echo " set monthly_statistics_report_devices=\"YES\" in $periodic_conf"
;;
esac
case "$monthly_statistics_report_ports" in
[Yy][Ee][Ss])
send_ports
echo "Posting monthly ports statistics to $checkin_server"
;;
*)
echo "Posting monthly ports statistics disabled"
echo " set monthly_statistics_report_ports=\"YES\" in $periodic_conf"
;;
esac
disable_token
;;
*)
echo "Posting monthly OS statistics disabled"
echo " set monthly_statistics_enable=\"YES\" in $periodic_conf"
;;
esac
umask $oldmask
exit $rc
|