1# $MirOS: src/distrib/common/functions,v 1.6 2014/02/10 00:47:11 tg Exp $
2#-
3# Copyright (c) 2008, 2010, 2011, 2012, 2014
4#	Thorsten Glaser <tg@mirbsd.org>
5#
6# Provided that these terms and disclaimer and all copyright notices
7# are retained or reproduced in an accompanying document, permission
8# is granted to deal in this work without restriction, including un-
9# limited rights to use, publicly perform, distribute, sell, modify,
10# merge, give away, or sublicence.
11#
12# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
13# the utmost extent permitted by applicable law, neither express nor
14# implied; without malicious intent or gross negligence. In no event
15# may a licensor, author or contributor be held liable for indirect,
16# direct, other damage, loss, or other issues arising in any way out
17# of dealing in the work, even if advised of the possibility of such
18# damage or existence of a defect, except proven that it results out
19# of said person's immediate fault when using the work as intended.
20
21function uri_escape {
22	print -nr -- "$*" | sed -e '
23	    s.%.%25.g
24	    s.;.%3B.g
25	    s./.%2F.g
26	    s.?.%3F.g
27	    s.:.%3A.g
28	    s.@.%40.g
29	    s.&.%26.g
30	    s.=.%3D.g
31	    s.+.%2B.g
32	    s.\$.%24.g
33	    s.,.%2C.g
34	    s.	.%09.g
35	    s. .%20.g
36	    s.<.%3C.g
37	    s.>.%3E.g
38	    s.#.%23.g
39	    s.".%22.g
40	    s.{.%7B.g
41	    s.}.%7D.g
42	    s.|.%7C.g
43	    s.\\.%5C.g
44	    s.\^.%5E.g
45	    s.\[.%5B.g
46	    s.].%5D.g
47	    s.`.%60.g
48	    s.'\''.%27.g
49	'
50}
51
52_getrnd() {
53	local url=${2:-https}://call.mirbsd.org/rn.cgi?bsdrd${1:-manuell}
54	(ulimit -T 60; date; ftp -mvo /dev/arandom $url,os=$(uri_escape \
55	    $(sysctl -n kern.{ostype,ospatchlevel,osversion,hostname} \
56	    hw.machine)),seed=$(dd if=/dev/arandom bs=32 count=1 | \
57	    hd -ve '1/1 "%02X"'); date) >/dev/wrandom 2>&1
58}
59
60_ntp() {
61	ps ax 2>&- | fgrep ntp | fgrep -v grep | while read pid rest; do
62		kill $pid 2>&-
63	done
64	if [[ -x /usr/sbin/rdate ]]; then
65		print -n Adjusting the clock...
66		local nl='
67' x=$(for server in 10.0.2.2 ntp.mirbsd.org; do
68			rdate -nv $server
69		    done 2>&1)
70		print -r -- "$x" >/dev/wrandom
71		print -r " ${x//$nl/ }"
72	fi
73	if [[ -x /usr/sbin/ntpd ]]; then
74		print -n Starting NTP daemon...
75		/usr/sbin/ntpd
76		print done
77	fi
78}
79
80_vbox_check() {
81	grep '^.d[0-9].*<VBOX[, >]' /var/run/dmesg.boot >/dev/null 2>&1 && \
82	    return 0
83	# something with sysctl hw, hw.sensors, machdep here?
84	return 1
85}
86