xref: /freebsd/libexec/rc/rc.d/hostid (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1#!/bin/sh
2#
3# Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4# Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28
29# PROVIDE: hostid
30# REQUIRE: sysctl
31# KEYWORD: nojail
32
33. /etc/rc.subr
34
35name="hostid"
36desc="Generate a unique host ID"
37start_cmd="hostid_start"
38stop_cmd=":"
39reset_cmd="hostid_reset"
40extra_commands="reset"
41rcvar="hostid_enable"
42
43hostid_set()
44{
45	uuid=$1
46	# Generate hostid based on hostuuid - take first four bytes from md5(uuid).
47	id=`echo -n $uuid | /sbin/md5`
48	id="0x${id%????????????????????????}"
49
50	# Set both kern.hostuuid and kern.hostid.
51	#
52	startmsg "Setting hostuuid: ${uuid}."
53	${SYSCTL} kern.hostuuid="${uuid}" >/dev/null
54	startmsg "Setting hostid: ${id}."
55	${SYSCTL} kern.hostid=${id} >/dev/null
56}
57
58valid_hostid()
59{
60	uuid=$1
61
62	x="[0-9a-f]"
63	y=$x$x$x$x
64
65	# Check against a blacklist before
66	# accepting the UUID.
67	case "${uuid}" in
68	00000000-0000-0000-0000-000000000000)
69		;;
70	00020003-0004-0005-0006-000700080009)
71		;;
72	03000200-0400-0500-0006-000700080009)
73		;;
74	07090201-0103-0301-0807-060504030201)
75		;;
76	11111111-1111-1111-1111-111111111111)
77		;;
78	11111111-2222-3333-4444-555555555555)
79		;;
80	12345678-1234-5678-90ab-cddeefaabbcc)
81		;;
82	4c4c4544-0000-2010-8020-80c04f202020)
83		;;
84	58585858-5858-5858-5858-585858585858)
85		;;
86	890e2d14-cacd-45d1-ae66-bc80e8bfeb0f)
87		;;
88	8e275844-178f-44a8-aceb-a7d7e5178c63)
89		;;
90	dc698397-fa54-4cf2-82c8-b1b5307a6a7f)
91		;;
92	fefefefe-fefe-fefe-fefe-fefefefefefe)
93		;;
94	*-ffff-ffff-ffff-ffffffffffff)
95		;;
96	$y$y-$y-$y-$y-$y$y$y)
97		return 0
98		;;
99	esac
100
101	return 1
102}
103
104hostid_hardware()
105{
106	uuid=`kenv -q smbios.system.uuid`
107
108	if valid_hostid $uuid; then
109		echo "${uuid}"
110	elif [ "$uuid" ]; then
111		echo "INVALID"
112	fi
113}
114
115hostid_generate()
116{
117	# First look for UUID in hardware.
118	uuid=`hostid_hardware`
119
120	# Warn about invalid UUIDs
121	if [ "${uuid}" = "INVALID" ]; then
122		warn "hostid: unable to figure out a UUID from DMI data, generating a new one"
123		sleep 2
124		uuid=""
125	fi
126
127	# Generate a random UUID if invalid or not found
128	if [ -z "${uuid}" ]; then
129		# If not found, fall back to software-generated UUID.
130		uuid=`uuidgen ${hostid_uuidgen_flags}`
131	fi
132	hostid_set $uuid
133}
134
135hostid_reset()
136{
137	hostid_generate
138	# Store newly generated UUID in ${hostid_file}.
139	echo $uuid > ${hostid_file}
140	if [ $? -ne 0 ]; then
141		warn "could not store hostuuid in ${hostid_file}."
142	fi
143}
144
145hostid_start()
146{
147	# If ${hostid_file} already exists, we take UUID from there.
148	if [ -r ${hostid_file} ]; then
149		read saved_hostid < ${hostid_file}
150		if valid_hostid ${saved_hostid}; then
151			hostid_set ${saved_hostid}
152			exit 0
153		fi
154	fi
155
156	# No hostid file, generate UUID.
157	hostid_generate
158}
159
160load_rc_config $name
161
162# doesn't make sense to run in a svcj: config setting
163hostid_svcj="NO"
164
165run_rc_command "$1"
166