buildbff.sh 8.9 KB

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