xref: /freebsd/crypto/openssh/contrib/aix/buildbff.sh (revision f1ed5c000c688cf9781b486134baf4ba25415efd)
1#!/bin/sh
2#
3# buildbff.sh: Create AIX SMIT-installable OpenSSH packages
4#
5# Author: Darren Tucker (dtucker at zip dot com dot au)
6# This file is placed in the public domain and comes with absolutely
7# no warranty.
8#
9# Based originally on Ben Lindstrom's buildpkg.sh for Solaris
10#
11
12#
13# Tunable configuration settings
14# 	create a "config.local" in your build directory or set
15#	environment variables to override these.
16#
17[ -z "$PERMIT_ROOT_LOGIN" ] && PERMIT_ROOT_LOGIN=no
18[ -z "$X11_FORWARDING" ] && X11_FORWARDING=no
19[ -z "$AIX_SRC" ] && AIX_SRC=no
20
21umask 022
22
23startdir=`pwd`
24
25perl -v >/dev/null || (echo perl required; exit 1)
26
27# Path to inventory.sh: same place as buildbff.sh
28if  echo $0 | egrep '^/'
29then
30	inventory=`dirname $0`/inventory.sh		# absolute path
31else
32	inventory=`pwd`/`dirname $0`/inventory.sh	# relative path
33fi
34
35#
36# We still support running from contrib/aix, but this is deprecated
37#
38if pwd | egrep 'contrib/aix$'
39then
40	echo "Changing directory to `pwd`/../.."
41	echo "Please run buildbff.sh from your build directory in future."
42	cd ../..
43	contribaix=1
44fi
45
46if [ ! -f Makefile ]
47then
48	echo "Makefile not found (did you run configure?)"
49	exit 1
50fi
51
52#
53# Directories used during build:
54# current dir = $objdir		directory you ran ./configure in.
55# $objdir/$PKGDIR/ 		directory package files are constructed in
56# $objdir/$PKGDIR/root/		package root ($FAKE_ROOT)
57#
58objdir=`pwd`
59PKGNAME=openssh
60PKGDIR=package
61
62#
63# Collect local configuration settings to override defaults
64#
65if [ -s ./config.local ]
66then
67	echo Reading local settings from config.local
68	. ./config.local
69fi
70
71#
72# Fill in some details from Makefile, like prefix and sysconfdir
73#	the eval also expands variables like sysconfdir=${prefix}/etc
74#	provided they are eval'ed in the correct order
75#
76for confvar in prefix exec_prefix bindir sbindir libexecdir datadir mandir mansubdir sysconfdir piddir srcdir
77do
78	eval $confvar=`grep "^$confvar=" $objdir/Makefile | cut -d = -f 2`
79done
80
81#
82# Collect values of privsep user and privsep path
83#	currently only found in config.h
84#
85for confvar in SSH_PRIVSEP_USER PRIVSEP_PATH
86do
87	eval $confvar=`awk '/#define[ \t]'$confvar'/{print $3}' $objdir/config.h`
88done
89
90# Set privsep defaults if not defined
91if [ -z "$SSH_PRIVSEP_USER" ]
92then
93	SSH_PRIVSEP_USER=sshd
94fi
95if [ -z "$PRIVSEP_PATH" ]
96then
97	PRIVSEP_PATH=/var/empty
98fi
99
100# Clean package build directory
101rm -rf $objdir/$PKGDIR
102FAKE_ROOT=$objdir/$PKGDIR/root
103mkdir -p $FAKE_ROOT
104
105# Start by faking root install
106echo "Faking root install..."
107cd $objdir
108make install-nokeys DESTDIR=$FAKE_ROOT
109
110if [ $? -gt 0 ]
111then
112	echo "Fake root install failed, stopping."
113	exit 1
114fi
115
116#
117# Copy informational files to include in package
118#
119cp $srcdir/LICENCE $objdir/$PKGDIR/
120cp $srcdir/README* $objdir/$PKGDIR/
121
122#
123# Extract common info requires for the 'info' part of the package.
124#	AIX requires 4-part version numbers
125#
126VERSION=`./ssh -V 2>&1 | cut -f 1 -d , | cut -f 2 -d _`
127MAJOR=`echo $VERSION | cut -f 1 -d p | cut -f 1 -d .`
128MINOR=`echo $VERSION | cut -f 1 -d p | cut -f 2 -d .`
129PATCH=`echo $VERSION | cut -f 1 -d p | cut -f 3 -d .`
130PORTABLE=`echo $VERSION | awk 'BEGIN{FS="p"}{print $2}'`
131[ "$PATCH" = "" ] && PATCH=0
132[ "$PORTABLE" = "" ] && PORTABLE=0
133BFFVERSION=`printf "%d.%d.%d.%d" $MAJOR $MINOR $PATCH $PORTABLE`
134
135echo "Building BFF for $PKGNAME $VERSION (package version $BFFVERSION)"
136
137#
138# Set ssh and sshd parameters as per config.local
139#
140if [ "${PERMIT_ROOT_LOGIN}" = no ]
141then
142	perl -p -i -e "s/#PermitRootLogin yes/PermitRootLogin no/" \
143		$FAKE_ROOT/${sysconfdir}/sshd_config
144fi
145if [ "${X11_FORWARDING}" = yes ]
146then
147	perl -p -i -e "s/#X11Forwarding no/X11Forwarding yes/" \
148		$FAKE_ROOT/${sysconfdir}/sshd_config
149fi
150
151
152# Rename config files; postinstall script will copy them if necessary
153for cfgfile in ssh_config sshd_config
154do
155	mv $FAKE_ROOT/$sysconfdir/$cfgfile $FAKE_ROOT/$sysconfdir/$cfgfile.default
156done
157
158#
159# Generate lpp control files.
160#	working dir is $FAKE_ROOT but files are generated in dir above
161#	and moved into place just before creation of .bff
162#
163cd $FAKE_ROOT
164echo Generating LPP control files
165find . ! -name . -print >../openssh.al
166$inventory >../openssh.inventory
167
168cat <<EOD >../openssh.copyright
169This software is distributed under a BSD-style license.
170For the full text of the license, see /usr/lpp/openssh/LICENCE
171EOD
172
173#
174# openssh.size file allows filesystem expansion as required
175# generate list of directories containing files
176# then calculate disk usage for each directory and store in openssh.size
177#
178files=`find . -type f -print`
179dirs=`for file in $files; do dirname $file; done | sort -u`
180for dir in $dirs
181do
182	du $dir
183done > ../openssh.size
184
185#
186# Create postinstall script
187#
188cat <<EOF >>../openssh.post_i
189#!/bin/sh
190
191echo Creating configs from defaults if necessary.
192for cfgfile in ssh_config sshd_config
193do
194	if [ ! -f $sysconfdir/\$cfgfile ]
195	then
196		echo "Creating \$cfgfile from default"
197		cp $sysconfdir/\$cfgfile.default $sysconfdir/\$cfgfile
198	else
199		echo "\$cfgfile already exists."
200	fi
201done
202echo
203
204# Create PrivilegeSeparation user and group if not present
205echo Checking for PrivilegeSeparation user and group.
206if cut -f1 -d: /etc/group | egrep '^'$SSH_PRIVSEP_USER'\$' >/dev/null
207then
208	echo "PrivSep group $SSH_PRIVSEP_USER already exists."
209else
210	echo "Creating PrivSep group $SSH_PRIVSEP_USER."
211	mkgroup -A $SSH_PRIVSEP_USER
212fi
213
214# Create user if required
215if lsuser "$SSH_PRIVSEP_USER" >/dev/null
216then
217	echo "PrivSep user $SSH_PRIVSEP_USER already exists."
218else
219	echo "Creating PrivSep user $SSH_PRIVSEP_USER."
220	mkuser gecos='SSHD PrivSep User' login=false rlogin=false account_locked=true pgrp=$SSH_PRIVSEP_USER $SSH_PRIVSEP_USER
221fi
222
223if egrep '^[ \t]*UsePrivilegeSeparation[ \t]+no' $sysconfdir/sshd_config >/dev/null
224then
225	echo UsePrivilegeSeparation not enabled, privsep directory not required.
226else
227	# create chroot directory if required
228	if [ -d $PRIVSEP_PATH ]
229	then
230		echo "PrivSep chroot directory $PRIVSEP_PATH already exists."
231	else
232		echo "Creating PrivSep chroot directory $PRIVSEP_PATH."
233		mkdir $PRIVSEP_PATH
234		chown 0 $PRIVSEP_PATH
235		chgrp 0 $PRIVSEP_PATH
236		chmod 755 $PRIVSEP_PATH
237	fi
238fi
239echo
240
241# Generate keys unless they already exist
242echo Creating host keys if required.
243if [ -f "$sysconfdir/ssh_host_key" ] ; then
244	echo "$sysconfdir/ssh_host_key already exists, skipping."
245else
246	$bindir/ssh-keygen -t rsa1 -f $sysconfdir/ssh_host_key -N ""
247fi
248if [ -f $sysconfdir/ssh_host_dsa_key ] ; then
249	echo "$sysconfdir/ssh_host_dsa_key already exists, skipping."
250else
251	$bindir/ssh-keygen -t dsa -f $sysconfdir/ssh_host_dsa_key -N ""
252fi
253if [ -f $sysconfdir/ssh_host_rsa_key ] ; then
254	echo "$sysconfdir/ssh_host_rsa_key already exists, skipping."
255else
256	$bindir/ssh-keygen -t rsa -f $sysconfdir/ssh_host_rsa_key -N ""
257fi
258echo
259
260# Set startup command depending on SRC support
261if [ "$AIX_SRC" = "yes" ]
262then
263	echo Creating SRC sshd subsystem.
264	rmssys -s sshd 2>&1 >/dev/null
265	mkssys -s sshd -p "$sbindir/sshd" -a '-D' -u 0 -S -n 15 -f 9 -R -G tcpip
266	startupcmd="start $sbindir/sshd \\\"\\\$src_running\\\""
267	oldstartcmd="$sbindir/sshd"
268else
269	startupcmd="$sbindir/sshd"
270	oldstartcmd="start $sbindir/sshd \\\"$src_running\\\""
271fi
272
273# If migrating to or from SRC, change previous startup command
274# otherwise add to rc.tcpip
275if egrep "^\$oldstartcmd" /etc/rc.tcpip >/dev/null
276then
277	if sed "s|^\$oldstartcmd|\$startupcmd|g" /etc/rc.tcpip >/etc/rc.tcpip.new
278	then
279		chmod 0755 /etc/rc.tcpip.new
280		mv /etc/rc.tcpip /etc/rc.tcpip.old && \
281		mv /etc/rc.tcpip.new /etc/rc.tcpip
282	else
283		echo "Updating /etc/rc.tcpip failed, please check."
284	fi
285else
286	# Add to system startup if required
287	if grep "^\$startupcmd" /etc/rc.tcpip >/dev/null
288	then
289		echo "sshd found in rc.tcpip, not adding."
290	else
291		echo "Adding sshd to rc.tcpip"
292		echo >>/etc/rc.tcpip
293		echo "# Start sshd" >>/etc/rc.tcpip
294		echo "\$startupcmd" >>/etc/rc.tcpip
295	fi
296fi
297EOF
298
299#
300# Create liblpp.a and move control files into it
301#
302echo Creating liblpp.a
303(
304	cd ..
305	for i in openssh.al openssh.copyright openssh.inventory openssh.post_i openssh.size LICENCE README*
306	do
307		ar -r liblpp.a $i
308		rm $i
309	done
310)
311
312#
313# Create lpp_name
314#
315# This will end up looking something like:
316# 4 R I OpenSSH {
317# OpenSSH 3.0.2.1 1 N U en_US OpenSSH 3.0.2p1 Portable for AIX
318# [
319# %
320# /usr/local/bin 8073
321# /usr/local/etc 189
322# /usr/local/libexec 185
323# /usr/local/man/man1 145
324# /usr/local/man/man8 83
325# /usr/local/sbin 2105
326# /usr/local/share 3
327# %
328# ]
329# }
330
331echo Creating lpp_name
332cat <<EOF >../lpp_name
3334 R I $PKGNAME {
334$PKGNAME $BFFVERSION 1 N U en_US OpenSSH $VERSION Portable for AIX
335[
336%
337EOF
338
339for i in $bindir $sysconfdir $libexecdir $mandir/${mansubdir}1 $mandir/${mansubdir}8 $sbindir $datadir /usr/lpp/openssh
340do
341	# get size in 512 byte blocks
342	if [ -d $FAKE_ROOT/$i ]
343	then
344		size=`du $FAKE_ROOT/$i | awk '{print $1}'`
345		echo "$i $size" >>../lpp_name
346	fi
347done
348
349echo '%' >>../lpp_name
350echo ']' >>../lpp_name
351echo '}' >>../lpp_name
352
353#
354# Move pieces into place
355#
356mkdir -p usr/lpp/openssh
357mv ../liblpp.a usr/lpp/openssh
358mv ../lpp_name .
359
360#
361# Now invoke backup to create .bff file
362#	note: lpp_name needs to be the first file so we generate the
363#	file list on the fly and feed it to backup using -i
364#
365echo Creating $PKGNAME-$VERSION.bff with backup...
366rm -f $PKGNAME-$VERSION.bff
367(
368	echo "./lpp_name"
369	find . ! -name lpp_name -a ! -name . -print
370) | backup  -i -q -f ../$PKGNAME-$VERSION.bff $filelist
371
372#
373# Move package into final location and clean up
374#
375mv ../$PKGNAME-$VERSION.bff $startdir
376cd $startdir
377rm -rf $objdir/$PKGDIR
378
379echo $0: done.
380
381