View | Details | Raw Unified | Return to bug 198799 | Differences between
and this patch

Collapse All | Expand All

(-)sysutils/bsdstats/Makefile (-13 / +24 lines)
Lines 2-36 Link Here
2
# $FreeBSD: head/sysutils/bsdstats/Makefile 339133 2014-01-08 13:53:59Z lme $
2
# $FreeBSD: head/sysutils/bsdstats/Makefile 339133 2014-01-08 13:53:59Z lme $
3
3
4
PORTNAME=	bsdstats
4
PORTNAME=	bsdstats
5
PORTVERSION=	5.5
5
PORTVERSION=	6.0
6
PORTREVISION=	5
7
CATEGORIES=	sysutils
6
CATEGORIES=	sysutils
8
DISTFILES=
7
DISTFILES=
9
8
10
MAINTAINER=	bsdstats@nanoman.ca
9
MAINTAINER=	yuri@rawbw.com
11
COMMENT=	Monthly script for reporting anonymous statistics about your machine
10
COMMENT=	Monthly script for reporting anonymous statistics about your machine
12
11
12
LICENSE=	BSD4CLAUSE
13
13
NO_BUILD=	yes
14
NO_BUILD=	yes
14
USE_RC_SUBR=	bsdstats
15
USE_RC_SUBR=	bsdstats
15
SUB_FILES=	300.statistics pkg-message
16
SUB_FILES=	300.statistics bsdstats-update pkg-message
16
SUB_LIST+=	DIG=${DIG}
17
SUB_LIST+=	VERSION=${PORTVERSION}
18
NO_ARCH=	YES
19
20
OPTIONS_DEFINE=	TOR
21
TOR_DESC=	Submit securely through Tor anonymity network
17
22
18
.include <bsd.port.pre.mk>
23
.include <bsd.port.pre.mk>
19
24
20
.if ${OSVERSION} >= 1000500
25
.if ${PORT_OPTIONS:MTOR}
21
DIG=		drill
26
RUN_DEPENDS+=	tor:${PORTSDIR}/security/tor
27
.endif
28
29
pre-install:
30
.if ${PORT_OPTIONS:MTOR}
31
	@${REINPLACE_CMD} -e 's|USE_TOR=NO|USE_TOR=YES|' ${WRKDIR}/300.statistics
32
	@${REINPLACE_CMD} -e 's|# REQUIRE: LOGIN|# REQUIRE: LOGIN tor|' ${WRKDIR}/bsdstats
33
	@${REINPLACE_CMD} -e 's|@@TOR_MESSAGE@@|Statistics will be submitted anonymously through the TOR network|' \
34
	    ${WRKDIR}/pkg-message
22
.else
35
.else
23
DIG=		dig
36
	@${REINPLACE_CMD} -e 's|@@TOR_MESSAGE@@|You can build bsdstats with TOR port option to submit anonymously|' \
37
	    ${WRKDIR}/pkg-message
24
.endif
38
.endif
25
39
26
do-install:
40
do-install:
27
	${MKDIR} ${STAGEDIR}${PREFIX}/etc/periodic/monthly
41
	${MKDIR} ${STAGEDIR}${PREFIX}/etc/periodic/monthly
28
	${INSTALL_SCRIPT} ${WRKDIR}/300.statistics \
42
	${INSTALL_SCRIPT} ${WRKDIR}/300.statistics \
29
		${STAGEDIR}${PREFIX}/etc/periodic/monthly
43
		${STAGEDIR}${PREFIX}/etc/periodic/monthly
30
44
	${INSTALL_SCRIPT} ${WRKDIR}/bsdstats-update \
31
post-install:
45
		${STAGEDIR}${PREFIX}/bin/bsdstats-update
32
.if ! defined(BATCH) && ! defined(PACKAGE_BUILDING)
33
	@PKG_PREFIX="${PREFIX}" ${SH} ${PKGINSTALL} ${PKGNAME} POST-INSTALL
34
.endif
35
46
36
.include <bsd.port.post.mk>
47
.include <bsd.port.post.mk>
(-)sysutils/bsdstats/files/300.statistics.in (-470 / +499 lines)
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=""
63
30
64
31
PATH=/sbin:/bin:/usr/sbin:/usr/bin:%%PREFIX%%/sbin:%%PREFIX%%/bin
65
oldmask=$(${UMASK})
32
export PATH
66
${UMASK} 066
67
timeout=10
33
68
34
unset HTTP_USER_AGENT
69
##
70
## Procedures
71
##
35
72
36
IFS="
73
echo_begin() {
37
"
74
  echo -n "$1 ... "
75
}
38
76
39
random () {
77
echo_end_success() {
40
  jot -r 1 0 900
78
  echo "SUCCESS"
41
}
79
}
42
80
43
# RFC 2396
81
echo_err() {
44
uri_escape () {
82
  echo "$1" >&2
45
    echo ${1+$@} | sed -e '
83
}
46
        s/%/%25/g
84
47
        s/;/%3b/g
85
log() { # log(categ,msg)
48
        s,/,%2f,g
86
  echo "[`date "+%Y-%m-%d %H:%M:%S"`] $1 $2" >> $bsdstats_log
49
        s/?/%3f/g
87
}
50
        s/:/%3a/g
88
51
        s/@/%40/g
89
fail() { # fail(msg): argument is simple user-level message, detailed log message is assumed to be printed before 'fail' invokation
52
        s/&/%26/g
90
  # log error
53
        s/=/%3d/g
91
  log "TERM" "$1 (failure)"
54
        s/+/%2b/g
92
  # let user know
55
        s/\$/%24/g
93
  echo_err "BSDstats failed: $1"
56
        s/,/%2c/g
94
  # bail out
57
        s/ /%20/g
95
  ${UMASK} $oldmask
58
        '
96
  exit 1
59
}
97
}
60
98
61
do_fetch () {
99
random() {
62
    url="http://$checkin_server/scripts/$1"
100
  ${JOT} -r 1 0 900
63
    case $(uname) in
101
}
64
	FreeBSD )	
102
65
		/usr/bin/fetch -T "$timeout" -q -o - "$url"
103
# do_http_request: if success returns 0, and prints http.body, otherwise returns 1
66
		;;
104
do_http_request() {
67
	* )
105
  local meth="$1"
68
		/usr/bin/ftp -q "$timeout" -V -o - "$url"
106
  local url="$2"
69
		;;
107
  local body="$3"
70
    esac
108
  local content_type="$4"
71
}
109
  local do_log="$5"
72
110
73
check_dns () {
111
  local resp
74
    if [ `%%DIG%% bsdstats.org txt | grep TXT | grep UP | wc -l` = 0 ] 
112
  local lineno
75
    then
113
  local in_header
76
      echo "DNS not reachable, Network Down?"
114
  local result_count
77
      exit
115
78
    fi
116
  if [ -n "${http_header_proxy_auth}" ]; then url="http://${checkin_server}${url}"; fi
79
}
117
80
118
  local txt="${meth} ${url} HTTP/1.0"
81
send_devices () {
119
  local http_req="${txt}"
82
    case $(uname) in
120
  txt="${txt}${CR}${NL}Host: ${checkin_server}"
83
	FreeBSD )
121
  if [ -n "${http_header_proxy_auth}" ]; then txt="${txt}${CR}${NL}Proxy-Authorization: ${http_header_proxy_auth}"; fi
84
	    for line in `/usr/sbin/pciconf -l`
122
  txt="${txt}${CR}${NL}User-Agent: bsdstats-${CURR_VERSION}"
85
	    do
123
  txt="${txt}${CR}${NL}Connection: close"
86
	      DRIVER=`echo $line | awk -F\@ '{print $1}'`
124
  if [ -n "${content_type}" ]; then txt="${txt}${NL}Content-Type: ${content_type}"; fi
87
	      DEV=`echo $line | awk '{print $4}' | cut -c8-15`
125
  if [ -n "${body}" ]; then txt="${txt}${CR}${NL}Content-Length: ${#body}"; fi
88
	      CLASS=`echo $line | awk '{print $2}' | cut -c9-14`
126
  txt="${txt}${CR}${NL}${CR}${NL}${body}"
89
	      query_string=$query_string`echo \&dev[]=$DRIVER:$DEV:$CLASS`
127
90
	    done
128
  resp=$(echo "${txt}" | ${NC} ${nc_host} ${nc_port} 2>/dev/null)
91
129
  if [ $? -ne 0 ]; then
92
	    report_devices
130
    if [ ${do_log} -ne 0 ]; then
93
	    ;;
131
      log "FAIL" "Failed to send data to the host ${nc_host}:${nc_port}, is network or host down?"
94
	* )
132
    fi
95
	    # Not supported
133
    return 1
96
	    ;;
134
  fi
97
    esac
135
98
}
136
  local IFS=${NL}${CR}
99
137
  lineno=0
100
send_ports () {
138
  in_header=1
101
    case $(uname) in
139
  http_result=""
102
	FreeBSD )
140
  for str in ${resp}; do
103
141
    if [ $lineno -eq 0 ] ; then
104
	############################################################################
142
      if expr "${str}" : "^HTTP/1\.[01] 200 OK$" > /dev/null; then
105
	# At the time of this writing (2012-07-24), FreeBSD is switching from
143
        # ok
106
	# pkg_* tools to pkgng.  Starting in FreeBSD 9.1, pkgng will become the
144
        true
107
	# default tool for package management.
145
      else
108
	#
146
        if [ ${do_log} -ne 0 ]; then
109
	# Until pkg_* tools are officially declared unsupported, they will need
147
          log "FAIL" "Failed HTTP query: request='${http_req}' -> response='${str}'"
110
	# legacy support in ports like this one.  When the need for this support is
148
        fi
111
	# officially discontinued, please do the following:
149
        return 2
112
	#
150
      fi
113
	# 1. Delete the contents of this "LEGACY" section:
151
    elif [ $lineno -ge 1 -a $in_header -eq 1 ] ; then
114
	#
152
      if [ -z "${str}" ]; then
115
	#-----BEGIN LEGACY-----
153
        in_header=0
116
	    # Detect pkgng
154
        result_count=0
117
	    if [ -e /var/db/pkg/local.sqlite ]; then
155
      fi
118
156
    else
119
	      # Use pkgng
157
      if [ $result_count -eq 0 ]; then
120
158
        http_result="${str}"
121
	      for line in `pkg info | awk '{ print $1 }'`
159
      else
122
	      do
160
        http_result="${http_result}${NL}${str}"
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
161
      fi
162
      result_count=$(($result_count+1))
297
    fi
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
199
}
298
200
299
    if [ ! -f $id_token_file -o ! -s $id_token_file ] ;
201
uri_escape() {
300
    then
202
  # RFC 2396
301
       IDTOKEN=$(uri_escape $( openssl rand -base64 32 ) )
203
  echo "${1+$@}" | ${SED} -e '
302
       
204
    s/%/%25/g
303
       idf=$( mktemp "$id_token_file.XXXXXX" )  && \
205
    s/;/%3b/g
304
       chown root:wheel $idf          && \
206
    s,/,%2f,g
305
       chmod 600 $idf
207
    s/?/%3f/g
306
208
    s/:/%3a/g
307
       do_fetch getid.php?key=$IDTOKEN | {
209
    s/@/%40/g
308
          local IFS
210
    s/&/%26/g
309
          IFS='=
211
    s/=/%3d/g
310
'
212
    s/+/%2b/g
311
213
    s/\$/%24/g
312
	   while read var val 
214
    s/,/%2c/g
313
	     do  
215
    s/ /%20/g
314
	     case $var in
216
    '
315
		 KEY)
316
                     echo "KEY=$val"
317
		 ;;
318
		 TOKEN)
319
                     echo "TOKEN=$val"
320
		 ;;
321
		 *)
322
                 ;;
323
		 esac
324
	   done
325
           echo "VERSION=$version"
326
       } > $idf                                  && \
327
328
       mv $idf $id_token_file
329
       if [ ! -s $id_token_file ] ;
330
       then
331
    	  echo "Nothing returned from $checkin_server"
332
	  exit 1
333
       fi
334
    fi
335
    . $id_token_file
336
    KEY=$( uri_escape $KEY )
337
    TOKEN=$( uri_escape $TOKEN )
338
}
339
340
341
enable_token () {
342
      do_fetch enable_token.php?key=$TOKEN\&token=$KEY | {
343
          local IFS
344
          IFS='=
345
'
346
347
           while read var val
348
             do
349
             case $var in
350
                 STATUS)
351
                     if [ $val = "OK" ]
352
                     then
353
                       echo "[`date`] System enabled"
354
                     else
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
}
217
}
448
case "$monthly_statistics_enable" in
218
449
    [Yy][Ee][Ss])
219
parse_http_proxy_string() {
450
      check_dns
220
# Handle HTTP proxy services
451
      REL=`/usr/bin/uname -r`
221
#
452
      ARCH=`/usr/bin/uname -m`
222
# HTTP_PROXY/http_proxy can take the following form:
453
      OS=`/usr/bin/uname -s`
223
#    [http://][username:password@]proxy[:port][/]
454
      get_id_token
224
# Authentication details may also be provided via HTTP_PROXY_AUTH:
455
      test X"$1" = X-nodelay || sleep `random`
225
#    HTTP_PROXY_AUTH="basic:*:username:password"
456
      enable_token
226
#
457
      report_system
227
# IN:   * HTTP_PROXY or http_proxy
458
      echo "Posting monthly OS statistics to $checkin_server"
228
# IN:   * HTTP_PROXY_AUTH
459
      if [ X"$1" != X-nodelay ]; then
229
# OUT:  * http_header_proxy_auth
460
          case "$monthly_statistics_report_devices" in
230
# OUT:  * nc_host
461
              [Yy][Ee][Ss])
231
# OUT:  * nc_port
462
                  send_devices
232
463
                  echo "Posting monthly device statistics to $checkin_server"
233
  local PROXY_AUTH_USER
464
                  line=$( sysctl -n hw.model )
234
  local PROXY_AUTH_PASS
465
                  VEN=$( echo $line | cut -d ' ' -f 1 )
235
  local PROXY_HOST
466
                  DEV=$( uri_escape $( echo $line | cut -d ' ' -f 2- ) )
236
  local PROXY_PORT
467
                  count=$( sysctl -n hw.ncpu )
237
468
                  report_cpu
238
  if [ -z "$HTTP_PROXY" -a -n "$http_proxy" ]; then
469
                  echo "Posting monthly CPU statistics to $checkin_server"
239
    HTTP_PROXY=$http_proxy
470
                 ;;
240
  fi
471
              *) 
241
  if [ -n "$HTTP_PROXY" ]; then
472
                 echo "Posting monthly device/CPU statistics disabled"
242
    # Attempt to resolve any HTTP authentication
473
                 echo "    set monthly_statistics_report_devices=\"YES\" in $periodic_conf"
243
    if [ -n "$HTTP_PROXY_AUTH" ]; then
474
                 ;;
244
      PROXY_AUTH_USER=$(echo $HTTP_PROXY_AUTH | ${SED} -E 's/^.+:\*:(.+):.+$/\1/g')
475
          esac
245
      PROXY_AUTH_PASS=$(echo $HTTP_PROXY_AUTH | ${SED} -E 's/^.+:\*:.+:(.+)$/\1/g')
476
          case "$monthly_statistics_report_ports" in
246
    else
477
              [Yy][Ee][Ss])
247
      # Check for authentication within HTTP_PROXY
478
                  send_ports
248
      HAS_HTTP_AUTH=$(echo $HTTP_PROXY | ${SED} -E 's/^(http:\/\/)?((.+:.+)@)?.+/\3/')
479
                  echo "Posting monthly ports statistics to $checkin_server"
249
      if [ -n "$HAS_HTTP_AUTH" ]; then
480
                 ;;
250
        # Found HTTP authentication details
481
              *) 
251
        PROXY_AUTH_USER=$(echo $HAS_HTTP_AUTH | ${CUT} -d: -f1)
482
                 echo "Posting monthly ports statistics disabled"
252
        PROXY_AUTH_PASS=$(echo $HAS_HTTP_AUTH | ${CUT} -d: -f2)
483
                 echo "    set monthly_statistics_report_ports=\"YES\" in $periodic_conf"
484
                 ;;
485
          esac
486
      fi
253
      fi
487
      disable_token
254
    fi
255
256
    # Determine the proxy components
257
    PROXY_HOST=$(echo $HTTP_PROXY | ${SED} -E 's/^(http:\/\/)?(.+:.+@)?([^@:]+)(:.+)?/\3/')
258
    PROXY_PORT=$(echo $HTTP_PROXY | ${SED} -E 's/^(http:\/\/)?(.+:.+@)?(.+):([0-9]+)/\4/' | ${SED} -e 's/[^0-9]//g')
259
    if [ -z "$PROXY_PORT" ]; then
260
      # Use default proxy port
261
      PROXY_PORT=3128
262
    fi
263
  fi
264
265
  # Determine the host/port netcat should connect to
266
  if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ]; then
267
    nc_host=$PROXY_HOST
268
    nc_port=$PROXY_PORT
269
    # Proxy authentication, if required
270
    if [ -n "$PROXY_AUTH_USER" -a -n "$PROXY_AUTH_PASS" ]; then
271
      local auth_base64=$(echo -n "$PROXY_AUTH_USER:$PROXY_AUTH_PASS" | ${OPENSSL} base64)
272
      http_header_proxy_auth="Basic $auth_base64"
273
    fi
274
    return 0
275
  else
276
    nc_host=$checkin_server
277
    nc_port=80
278
    return 1
279
  fi
280
}
281
282
test_connection() {
283
  local body
284
  body=$(do_http_request "HEAD" "/" "" "" 0)
285
  if [ $? -ne 0 -a $? -ne 2 ]; then
286
    log "FAIL" "Unable to connect to ${checkin_server_description}"
287
    fail "Network or host is down?"
288
  fi
289
}
290
291
setup_proxies() {
292
  # TOR
293
  if [ "${USE_TOR}" = "YES" ]; then
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}"
488
      ;;
363
      ;;
489
    *) 
364
    *)
490
      echo "Posting monthly OS statistics disabled"
365
      # Not supported
491
      echo "    set monthly_statistics_enable=\"YES\" in $periodic_conf"
366
      ;;
367
  esac
368
}
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
}
418
419
enable_token() {
420
  # XXX key and token are swapped here, this has been like that from version 5.5, not sure if fixing will break it
421
  do_http_request_check_status "GET" "/scripts/enable_token.php?key=${TOKEN}&token=${KEY}" \
422
    "" "" "token enabling"
423
  log "INFO" "System enabled"
424
}
425
426
disable_token() {
427
  do_http_request_check_status "GET" "/scripts/disable_token.php?key=${TOKEN}&token=${KEY}" \
428
    "" "" "token disabling"
429
  log "INFO" "System disabled"
430
}
431
432
report_system() {
433
  local REL=$(${UNAME} -r)
434
  local ARCH=$(${UNAME} -m)
435
  local OS=$(${UNAME} -s)
436
  echo_begin "Posting OS statistics to ${checkin_server_description}"
437
  do_http_request_check_status "GET" "/scripts/report_system.php?token=${TOKEN}&key=${KEY}&rel=${REL}&arch=${ARCH}&opsys=${OS}" \
438
    "" "" "OS statistics sumbission"
439
  echo_end_success
440
  log "INFO" "Posted OS statistics to ${checkin_server_description}"
441
}
442
443
report_cpu() {
444
  local line=$(${SYSCTL} -n hw.model)
445
  local VEN=$(echo $line | ${CUT} -d ' ' -f 1)
446
  local DEV=$(uri_escape $(echo $line | ${CUT} -d ' ' -f 2-))
447
  local count=$(${SYSCTL} -n hw.ncpu)
448
  echo_begin "Posting CPU information to ${checkin_server_description}"
449
  do_http_request_check_status "GET" "/scripts/report_cpu.php?token=${TOKEN}&key=${KEY}&cpus=${count}&vendor=${VEN}&cpu_type=${DEV}" \
450
      "" "" "CPU information sumbission"
451
  echo_end_success
452
  log "INFO" "Posted CPU information to ${checkin_server_description}"
453
}
454
455
report_all() {
456
  # network setup
457
  setup_proxies
458
  test_connection
459
  log "INIT" "Connected to ${checkin_server_description}"
460
  # sleep random number of seconds by default
461
  test X"$1" = X-nodelay || ${SLEEP} $(random)
462
  # prepare
463
  get_id_token
464
  # begin
465
  enable_token
466
  report_system
467
  # optional parts
468
  case "$monthly_statistics_report_devices" in
469
    [Yy][Ee][Ss])
470
       report_devices
471
       report_cpu
472
       ;;
473
    [Nn][Oo])
474
       echo "Posting monthly device/CPU statistics disabled"
475
       echo "    set monthly_statistics_report_devices=\"YES\" in $periodic_conf"
476
       ;;
477
    *)
478
       # opt-out for devices
479
       report_devices
480
       report_cpu
481
       ;;
482
  esac
483
  case "$monthly_statistics_report_ports" in
484
    [Yy][Ee][Ss])
485
      report_ports
486
      ;;
487
    [Nn][Oo])
488
      echo "Posting monthly ports statistics disabled"
489
      echo "    set monthly_statistics_report_ports=\"YES\" in $periodic_conf"
490
      ;;
491
    *)
492
      # opt-out for ports
493
      report_ports
494
      ;;
495
  esac
496
  # end
497
  disable_token
498
}
499
500
##
501
## MAIN: processing begins here
502
##
503
504
rc=0
505
506
case "$monthly_statistics_enable" in
507
  [Yy][Ee][Ss])
508
    # explicitly enabled: report it
509
    report_all "$1"
510
    ;;
511
  [Nn][Oo])
512
    echo "Posting monthly OS statistics disabled"
513
    echo "    set monthly_statistics_enable=\"YES\" in $periodic_conf"
514
    rc=1
515
    ;;
516
  *)
517
    # opt-out: we report when user have BSDstats installed, and user didn't say "NO"
518
    report_all "$1"
492
    ;;
519
    ;;
493
esac
520
esac
494
521
495
umask $oldmask
522
# success
496
exit $rc
523
log "SUCC" "Finished successfully"
524
${UMASK} $oldmask
525
exit ${rc}
(-)sysutils/bsdstats/files/bsdstats-update.in (+8 lines)
Line 0 Link Here
1
#!/bin/sh
2
3
if [ "$(id -u)" != "0" ]; then
4
  echo "Only root can run BSDstats update"
5
  exit 1
6
fi
7
8
%%PREFIX%%/etc/periodic/monthly/300.statistics -nodelay
(-)sysutils/bsdstats/files/pkg-message.in (-8 / +14 lines)
Lines 1-15 Link Here
1
********************
1
======================================================================
2
To enable monthly reporting, add these lines to /etc/periodic.conf:
2
You installed BSDstats: script reporting statistics about your machine
3
	monthly_statistics_enable="YES"
3
4
	monthly_statistics_report_devices="YES"
4
To disable monthly reporting, add this line to /etc/periodic.conf:
5
	monthly_statistics_report_ports="YES"
5
	monthly_statistics_enable="NO"
6
7
To disable parts of reporting, add these lines to /etc/periodic.conf:
8
	monthly_statistics_report_devices="NO"
9
	monthly_statistics_report_ports="NO"
6
10
7
To enable reporting on bootup, add this line to /etc/rc.conf:
11
To enable reporting on bootup, add this line to /etc/rc.conf:
8
	bsdstats_enable="YES"
12
	bsdstats_enable="YES"
9
13
10
To run it manually the first time:
14
To run it manually any time:
11
	%%PREFIX%%/etc/periodic/monthly/300.statistics -nodelay
15
	bsdstats-update
12
16
13
To view current statistics, go to:
17
To view current statistics, go to:
14
	http://www.bsdstats.org/
18
	http://www.bsdstats.org/
15
********************
19
20
@@TOR_MESSAGE@@
21
======================================================================
(-)sysutils/bsdstats/pkg-install (-23 / +3 lines)
Lines 2-27 Link Here
2
# pkg-install : based off ${PORTSDIR}/mail/courier/files/pkg-install.in
2
# pkg-install : based off ${PORTSDIR}/mail/courier/files/pkg-install.in
3
3
4
# The default answer to each of the installation questions is "yes".  You can
4
# The default answer to each of the installation questions is "yes".  You can
5
# override a questions's default by setting its environment variable to "no".
5
# override a question's default by setting its environment variable to "no".
6
# 
6
# 
7
# Environment Variable      | Question
7
# Environment Variable      | Question
8
# --------------------------+------------------------------------------------------------------
8
# --------------------------+------------------------------------------------------------------
9
# BSDSTATS_MONTHLY_ENABLE   | Would you like to enable monthly reporting in /etc/periodic.conf?
10
# BSDSTATS_MONTHLY_DEVICES  | Would you like to send a list of installed hardware as well?
11
# BSDSTATS_MONTHLY_PORTS    | Would you like to send a list of installed ports as well?
12
# BSDSTATS_MONTHLY_NOW      | Would you like to run it now?
9
# BSDSTATS_MONTHLY_NOW      | Would you like to run it now?
13
# BSDSTATS_REBOOT_REPORTING | Would you like to enable reporting on bootup in /etc/rc.conf?
10
# BSDSTATS_REBOOT_REPORTING | Would you like to enable reporting on bootup in /etc/rc.conf?
14
11
15
if [ -n "$PACKAGE_BUILDING" ]; then
12
if [ -n "$PACKAGE_BUILDING" ]; then
16
	BSDSTATS_MONTHLY_ENABLE=${BSDSTATS_MONTHLY_ENABLE:=no}
17
	BSDSTATS_MONTHLY_DEVICES=${BSDSTATS_MONTHLY_DEVICES:=no}
18
	BSDSTATS_MONTHLY_PORTS=${BSDSTATS_MONTHLY_PORTS:=no}
19
	BSDSTATS_MONTHLY_NOW=${BSDSTATS_MONTHLY_NOW:=no}
13
	BSDSTATS_MONTHLY_NOW=${BSDSTATS_MONTHLY_NOW:=no}
20
	BSDSTATS_REBOOT_REPORTING=${BSDSTATS_REBOOT_REPORTING:=no}
14
	BSDSTATS_REBOOT_REPORTING=${BSDSTATS_REBOOT_REPORTING:=no}
21
else
15
else
22
	BSDSTATS_MONTHLY_ENABLE=${BSDSTATS_MONTHLY_ENABLE:=yes}
23
	BSDSTATS_MONTHLY_DEVICES=${BSDSTATS_MONTHLY_DEVICES:=yes}
24
	BSDSTATS_MONTHLY_PORTS=${BSDSTATS_MONTHLY_PORTS:=yes}
25
	BSDSTATS_MONTHLY_NOW=${BSDSTATS_MONTHLY_NOW:=yes}
16
	BSDSTATS_MONTHLY_NOW=${BSDSTATS_MONTHLY_NOW:=yes}
26
	BSDSTATS_REBOOT_REPORTING=${BSDSTATS_REBOOT_REPORTING:=yes}
17
	BSDSTATS_REBOOT_REPORTING=${BSDSTATS_REBOOT_REPORTING:=yes}
27
fi
18
fi
Lines 56-74 Link Here
56
}
47
}
57
48
58
if [ "$2" = "POST-INSTALL" ]; then
49
if [ "$2" = "POST-INSTALL" ]; then
59
	if [ ! -f "/etc/periodic.conf" ] || [ -z "`grep monthly_statistics /etc/periodic.conf`" ]; then
50
	if yesno "Would you like to run BSDstats now" $BSDSTATS_MONTHLY_NOW; then
60
		if yesno "Would you like to enable monthly reporting in /etc/periodic.conf" $BSDSTATS_MONTHLY_ENABLE; then
51
		${INSTALL_PREFIX}${PKG_PREFIX}/etc/periodic/monthly/300.statistics -nodelay
61
			echo "monthly_statistics_enable=\"YES\"" >> /etc/periodic.conf
62
			if yesno "Would you like to send a list of installed hardware as well" $BSDSTATS_MONTHLY_DEVICES; then
63
				echo "monthly_statistics_report_devices=\"YES\"" >> /etc/periodic.conf
64
			fi
65
			if yesno "Would you like to send a list of installed ports as well" $BSDSTATS_MONTHLY_PORTS; then
66
				echo "monthly_statistics_report_ports=\"YES\"" >> /etc/periodic.conf
67
			fi
68
			if yesno "Would you like to run it now" $BSDSTATS_MONTHLY_NOW; then
69
				${PKG_PREFIX}/etc/periodic/monthly/300.statistics -nodelay
70
			fi
71
		fi
72
	fi
52
	fi
73
	if [ ! -f "/etc/rc.conf" ] || [ -z "`grep bsdstats_enable /etc/rc.conf`" ]; then
53
	if [ ! -f "/etc/rc.conf" ] || [ -z "`grep bsdstats_enable /etc/rc.conf`" ]; then
74
		echo
54
		echo
(-)sysutils/bsdstats/pkg-plist (+1 lines)
Line 1 Link Here
1
etc/periodic/monthly/300.statistics
1
etc/periodic/monthly/300.statistics
2
bin/bsdstats-update

Return to bug 198799