xref: /openssh-portable/contrib/aix/buildbff.sh (revision 7703ae5f)
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.
243$bindir/ssh-keygen -A
244echo
245
246# Set startup command depending on SRC support
247if [ "$AIX_SRC" = "yes" ]
248then
249	echo Creating SRC sshd subsystem.
250	rmssys -s sshd 2>&1 >/dev/null
251	mkssys -s sshd -p "$sbindir/sshd" -a '-D' -u 0 -S -n 15 -f 9 -R -G tcpip
252	startupcmd="start $sbindir/sshd \\\"\\\$src_running\\\""
253	oldstartcmd="$sbindir/sshd"
254else
255	startupcmd="$sbindir/sshd"
256	oldstartcmd="start $sbindir/sshd \\\"$src_running\\\""
257fi
258
259# If migrating to or from SRC, change previous startup command
260# otherwise add to rc.tcpip
261if egrep "^\$oldstartcmd" /etc/rc.tcpip >/dev/null
262then
263	if sed "s|^\$oldstartcmd|\$startupcmd|g" /etc/rc.tcpip >/etc/rc.tcpip.new
264	then
265		chmod 0755 /etc/rc.tcpip.new
266		mv /etc/rc.tcpip /etc/rc.tcpip.old && \
267		mv /etc/rc.tcpip.new /etc/rc.tcpip
268	else
269		echo "Updating /etc/rc.tcpip failed, please check."
270	fi
271else
272	# Add to system startup if required
273	if grep "^\$startupcmd" /etc/rc.tcpip >/dev/null
274	then
275		echo "sshd found in rc.tcpip, not adding."
276	else
277		echo "Adding sshd to rc.tcpip"
278		echo >>/etc/rc.tcpip
279		echo "# Start sshd" >>/etc/rc.tcpip
280		echo "\$startupcmd" >>/etc/rc.tcpip
281	fi
282fi
283EOF
284
285#
286# Create liblpp.a and move control files into it
287#
288echo Creating liblpp.a
289(
290	cd ..
291	for i in openssh.al openssh.copyright openssh.inventory openssh.post_i openssh.size LICENCE README*
292	do
293		ar -r liblpp.a $i
294		rm $i
295	done
296)
297
298#
299# Create lpp_name
300#
301# This will end up looking something like:
302# 4 R I OpenSSH {
303# OpenSSH 3.0.2.1 1 N U en_US OpenSSH 3.0.2p1 Portable for AIX
304# [
305# %
306# /usr/local/bin 8073
307# /usr/local/etc 189
308# /usr/local/libexec 185
309# /usr/local/man/man1 145
310# /usr/local/man/man8 83
311# /usr/local/sbin 2105
312# /usr/local/share 3
313# %
314# ]
315# }
316
317echo Creating lpp_name
318cat <<EOF >../lpp_name
3194 R I $PKGNAME {
320$PKGNAME $BFFVERSION 1 N U en_US OpenSSH $VERSION Portable for AIX
321[
322%
323EOF
324
325for i in $bindir $sysconfdir $libexecdir $mandir/${mansubdir}1 $mandir/${mansubdir}8 $sbindir $datadir /usr/lpp/openssh
326do
327	# get size in 512 byte blocks
328	if [ -d $FAKE_ROOT/$i ]
329	then
330		size=`du $FAKE_ROOT/$i | awk '{print $1}'`
331		echo "$i $size" >>../lpp_name
332	fi
333done
334
335echo '%' >>../lpp_name
336echo ']' >>../lpp_name
337echo '}' >>../lpp_name
338
339#
340# Move pieces into place
341#
342mkdir -p usr/lpp/openssh
343mv ../liblpp.a usr/lpp/openssh
344mv ../lpp_name .
345
346#
347# Now invoke backup to create .bff file
348#	note: lpp_name needs to be the first file so we generate the
349#	file list on the fly and feed it to backup using -i
350#
351echo Creating $PKGNAME-$VERSION.bff with backup...
352rm -f $PKGNAME-$VERSION.bff
353(
354	echo "./lpp_name"
355	find . ! -name lpp_name -a ! -name . -print
356) | backup  -i -q -f ../$PKGNAME-$VERSION.bff $filelist
357
358#
359# Move package into final location and clean up
360#
361mv ../$PKGNAME-$VERSION.bff $startdir
362cd $startdir
363rm -rf $objdir/$PKGDIR
364
365echo $0: done.
366
367