1168546Spjd#!/bin/sh
2168546Spjd#
3168546Spjd# Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4282114Sdelphij# Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
5168546Spjd# All rights reserved.
6168546Spjd#
7168546Spjd# Redistribution and use in source and binary forms, with or without
8168546Spjd# modification, are permitted provided that the following conditions
9168546Spjd# are met:
10168546Spjd# 1. Redistributions of source code must retain the above copyright
11168546Spjd#    notice, this list of conditions and the following disclaimer.
12168546Spjd# 2. Redistributions in binary form must reproduce the above copyright
13168546Spjd#    notice, this list of conditions and the following disclaimer in the
14168546Spjd#    documentation and/or other materials provided with the distribution.
15168546Spjd#
16168546Spjd# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17168546Spjd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18168546Spjd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19168546Spjd# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20168546Spjd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21168546Spjd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22168546Spjd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23168546Spjd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24168546Spjd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25168546Spjd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26168546Spjd# SUCH DAMAGE.
27168546Spjd#
28168546Spjd# $FreeBSD$
29168546Spjd#
30168546Spjd
31168546Spjd# PROVIDE: hostid
32208307Sdougb# REQUIRE: sysctl
33168546Spjd# KEYWORD: nojail
34168546Spjd
35168546Spjd. /etc/rc.subr
36168546Spjd
37168546Spjdname="hostid"
38298514Slmedesc="Generate a unique host ID"
39168546Spjdstart_cmd="hostid_start"
40168546Spjdstop_cmd=":"
41168546Spjdreset_cmd="hostid_reset"
42168546Spjdextra_commands="reset"
43168546Spjdrcvar="hostid_enable"
44168546Spjd
45168546Spjdhostid_set()
46168546Spjd{
47168546Spjd	uuid=$1
48168546Spjd	# Generate hostid based on hostuuid - take first four bytes from md5(uuid).
49178809Smtm	id=`echo -n $uuid | /sbin/md5`
50168546Spjd	id="0x${id%????????????????????????}"
51179945Smtm
52168546Spjd	# Set both kern.hostuuid and kern.hostid.
53179945Smtm	#
54197947Sdougb	check_startmsgs && echo "Setting hostuuid: ${uuid}."
55220153Semaste	${SYSCTL} kern.hostuuid="${uuid}" >/dev/null
56197947Sdougb	check_startmsgs && echo "Setting hostid: ${id}."
57220153Semaste	${SYSCTL} kern.hostid=${id} >/dev/null
58168546Spjd}
59168546Spjd
60282114Sdelphijvalid_hostid()
61168607Spjd{
62282745Sdelphij	uuid=$1
63282114Sdelphij
64169818Srse	x="[0-9a-f]"
65168607Spjd	y=$x$x$x$x
66282114Sdelphij
67282114Sdelphij	# Check against a blacklist before
68282114Sdelphij	# accepting the UUID.
69168607Spjd	case "${uuid}" in
70282114Sdelphij	00000000-0000-0000-0000-000000000000)
71282114Sdelphij		;;
72282114Sdelphij	00020003-0004-0005-0006-000700080009)
73282114Sdelphij		;;
74282114Sdelphij	03000200-0400-0500-0006-000700080009)
75282114Sdelphij		;;
76282114Sdelphij	07090201-0103-0301-0807-060504030201)
77282114Sdelphij		;;
78282114Sdelphij	11111111-1111-1111-1111-111111111111)
79282114Sdelphij		;;
80282114Sdelphij	11111111-2222-3333-4444-555555555555)
81282114Sdelphij		;;
82282114Sdelphij	4c4c4544-0000-2010-8020-80c04f202020)
83282114Sdelphij		;;
84282114Sdelphij	58585858-5858-5858-5858-585858585858)
85282114Sdelphij		;;
86282114Sdelphij	890e2d14-cacd-45d1-ae66-bc80e8bfeb0f)
87282114Sdelphij		;;
88282114Sdelphij	8e275844-178f-44a8-aceb-a7d7e5178c63)
89282114Sdelphij		;;
90282114Sdelphij	dc698397-fa54-4cf2-82c8-b1b5307a6a7f)
91282114Sdelphij		;;
92282114Sdelphij	fefefefe-fefe-fefe-fefe-fefefefefefe)
93282114Sdelphij		;;
94282114Sdelphij	*-ffff-ffff-ffff-ffffffffffff)
95282114Sdelphij		;;
96168607Spjd	$y$y-$y-$y-$y-$y$y$y)
97282114Sdelphij		return 0
98168607Spjd		;;
99168607Spjd	esac
100282114Sdelphij
101282114Sdelphij	return 1
102168607Spjd}
103168607Spjd
104282114Sdelphijhostid_hardware()
105282114Sdelphij{
106282114Sdelphij	uuid=`kenv -q smbios.system.uuid`
107282114Sdelphij
108282114Sdelphij	if valid_hostid $uuid; then
109282114Sdelphij		echo "${uuid}"
110282114Sdelphij	fi
111282114Sdelphij}
112282114Sdelphij
113195938Spjdhostid_generate()
114168546Spjd{
115168607Spjd	# First look for UUID in hardware.
116168607Spjd	uuid=`hostid_hardware`
117282114Sdelphij	if [ -z "${uuid}" ]; then
118282114Sdelphij		warn "hostid: unable to figure out a UUID from DMI data, generating a new one"
119282114Sdelphij		sleep 2
120168607Spjd		# If not found, fall back to software-generated UUID.
121168607Spjd		uuid=`uuidgen`
122168607Spjd	fi
123195938Spjd	hostid_set $uuid
124195938Spjd}
125195938Spjd
126195938Spjdhostid_reset()
127195938Spjd{
128195938Spjd	hostid_generate
129168546Spjd	# Store newly generated UUID in ${hostid_file}.
130168546Spjd	echo $uuid > ${hostid_file}
131168546Spjd	if [ $? -ne 0 ]; then
132168546Spjd		warn "could not store hostuuid in ${hostid_file}."
133168546Spjd	fi
134168546Spjd}
135168546Spjd
136168546Spjdhostid_start()
137168546Spjd{
138168546Spjd	# If ${hostid_file} already exists, we take UUID from there.
139168546Spjd	if [ -r ${hostid_file} ]; then
140282114Sdelphij		read saved_hostid < ${hostid_file}
141282114Sdelphij		if valid_hostid ${saved_hostid}; then
142282114Sdelphij			hostid_set `cat ${hostid_file}`
143282114Sdelphij			exit 0
144282114Sdelphij		fi
145168546Spjd	fi
146282114Sdelphij
147282114Sdelphij	# No hostid file, generate UUID.
148282114Sdelphij	hostid_generate
149168546Spjd}
150168546Spjd
151168546Spjdload_rc_config $name
152168546Spjdrun_rc_command "$1"
153