11.1Sriastrad#!/bin/sh
21.1Sriastrad#
31.3Skre# $NetBSD: certctl_init,v 1.3 2023/12/25 07:46:12 kre Exp $
41.1Sriastrad#
51.1Sriastrad# PROVIDE: certctl_init
61.1Sriastrad# REQUIRE: mountcritremote
71.1Sriastrad#
81.1Sriastrad# This script ensures that we run `certctl rehash' on first boot of a
91.1Sriastrad# live image to configure TLS trust anchors for OpenSSL in
101.1Sriastrad# /etc/openssl/certs.  We do this only on first boot by testing whether
111.1Sriastrad# /etc/openssl/certs is an empty directory.
121.1Sriastrad#
131.1Sriastrad# Requires mountcritremote for /usr/sbin/certctl.
141.1Sriastrad#
151.1Sriastrad# This is a stop-gap measure to ensure we get TLS trust anchors with
161.1Sriastrad# live images, which we can't prepare at build time because the
171.1Sriastrad# preparation requires running openssl(1) as a tool.  This stop-gap
181.1Sriastrad# measure should perhaps be replaced by a more general-purpose way to
191.1Sriastrad# run postinstall on first boot of the image, but that's a riskier
201.1Sriastrad# proposition to implement on short notice for netbsd-10.
211.1Sriastrad
221.1Sriastrad$_rc_subr_loaded . /etc/rc.subr
231.1Sriastrad
241.1Sriastradname="certctl_init"
251.1Sriastradrcvar=${name}
261.1Sriastradstart_cmd="certctl_init"
271.1Sriastradstop_cmd=":"
281.1Sriastrad
291.1Sriastradcertctl_init()
301.1Sriastrad{
311.1Sriastrad	local certsdir
321.1Sriastrad
331.1Sriastrad	certsdir=/etc/openssl/certs
341.1Sriastrad
351.1Sriastrad	# If /etc/openssl/certs is a symlink, or exists but is not a
361.1Sriastrad	# directory, or is a directory but is nonempty, then we're not
371.1Sriastrad	# in the first boot's initial configuration.  So do nothing.
381.1Sriastrad	if [ -h "$certsdir" ] ||
391.2Skre	    { [ -e "$certsdir" ] && ! [ -d "$certsdir" ] ; } ||
401.2Skre	    ( [ -d "$certsdir" ] &&
411.2Skre		find -f "$certsdir" -- -maxdepth 0 -type d -empty -exit 1 )
421.3Skre	then
431.1Sriastrad		return
441.1Sriastrad	fi
451.1Sriastrad
461.1Sriastrad	# Otherwise, if /etc/openssl/certs is nonexistent or is an
471.1Sriastrad	# empty directory, run `certctl rehash'.
481.1Sriastrad	echo "Configuring TLS trust anchors."
491.1Sriastrad	certctl rehash
501.1Sriastrad}
511.1Sriastrad
521.1Sriastradload_rc_config $name
531.1Sriastradrun_rc_command "$1"
54