2020# CONFIGURATION VARIABLES
2121#
2222
23- ossec_version=' 3.6.0'
24- source_file=" ossec-hids-${ossec_version} .tar.gz"
23+ NAME=" ossec-hids"
24+ ossec_version=' 4.0.0'
25+ source_file=" ${NAME} -${ossec_version} .tar.gz"
2526# packages=(ossec-hids ossec-hids-agent) # only options available
26- packages=(ossec-hids-agent)
27+ packages=(ossec-hids ossec-hids -agent)
2728
2829# codenames=(sid jessie wheezy precise trusty utopic)
29- codenames=(bionic )
30+ codenames=(bookworm )
3031
31- # For Debian use: sid, jessie or wheezy (hardcoded in update_changelog function)
32- # For Ubuntu use: lucid, precise, trusty or utopic
33- codenames_ubuntu=(precise trusty xenial bionic focal)
34- codenames_debian=(sid jessie wheezy)
32+ # For Debian use: sid, jessie, wheezy, bookworm (hardcoded in update_changelog function)
33+ # For Ubuntu use: xenial, bionic, focal, jammy, noble (24.04)
34+ codenames_ubuntu=(xenial bionic focal jammy noble )
35+ codenames_debian=(sid jessie wheezy bookworm )
3536
3637# architectures=(amd64 i386) only options available
37- architectures=(arm64 )
38+ architectures=(amd64 )
3839
39- # Debian files
40- debian_files_path=" /home/ubuntu/debian_files "
40+ # Debian files path (set after scriptpath below; use contrib/debian-packages, no version subdir)
41+ debian_files_path=" "
4142
42- # Setting up logfile
43+ # Setting up paths
4344scriptpath=$( cd $( dirname $0 ) ; pwd -P )
45+ debian_files_path=" ${scriptpath} "
46+ repo_root=" $( cd " ${scriptpath} /../.." && pwd) "
47+ build_root=" ${scriptpath} /build"
4448logfile=$scriptpath /ossec_packages.log
4549
50+ # Optional env overrides for one-off tests (e.g. on Fedora: CODENAMES=noble ARCHITECTURES=amd64)
51+ [[ -n " ${CODENAMES:- } " ]] && codenames=(${CODENAMES} )
52+ [[ -n " ${ARCHITECTURES:- } " ]] && architectures=(${ARCHITECTURES} )
53+
4654
4755#
4856# Function to write to LOG_FILE
@@ -77,7 +85,7 @@ contains_element() {
7785show_help ()
7886{
7987 echo "
80- This tool can be used to generate OSSEC packages for Ubuntu and Debian .
88+ This tool builds OSSEC Debian/ Ubuntu packages with pbuilder .
8189
8290 CONFIGURATION: The script is currently configured with the following variables:
8391 * Packages: ${packages[*]} .
@@ -151,6 +159,8 @@ update_changelog()
151159 local debdist=" testing"
152160 elif [ $codename = " wheezy" ]; then
153161 local debdist=" stable"
162+ elif [ $codename = " bookworm" ]; then
163+ local debdist=" bookworm"
154164 fi
155165
156166 # For Ubuntu
@@ -196,7 +206,11 @@ update_chroots()
196206 do
197207 for arch in ${architectures[@]}
198208 do
199- if [ -f /var/cache/pbuilder/$codename -$arrch -base.tgz ]; then
209+ # Ensure pbuilder cache dirs exist (debootstrap fails if aptcache/ is missing)
210+ sudo mkdir -p /var/cache/pbuilder/${codename} -${arch} /aptcache \
211+ /var/cache/pbuilder/${codename} -${arch} /result \
212+ /var/cache/pbuilder/build
213+ if [ -f /var/cache/pbuilder/$codename -$arch -base.tgz ]; then
200214 echo " Updating chroot environment: ${codename} -${arch} " | write_log
201215 if sudo DIST=$codename ARCH=$arch pbuilder update --configfile $scriptpath /pbuilderrc ; then
202216 echo " Successfully updated chroot environment: ${codename} -${arch} " | write_log
@@ -219,57 +233,63 @@ update_chroots()
219233
220234
221235#
222- # Downloads packages and prepare source directories.
223- # This is needed before building the packages.
236+ # Prepare source directories for building. Uses a dedicated build/ dir so we never
237+ # overwrite the packaging dirs (contrib/debian-packages/ossec-hids, ossec-hids-agent).
238+ # Prefer building from the local tree when possible (repo with .git and src/).
224239#
225240download_source ()
226241{
227- cd ${scriptpath}
228-
229- # Checking that Debian files exist for this version
230- for package in ${packages[*]}
231- do
232- if [ ! -d ${debian_files_path} /${ossec_version} /$package /debian ]; then
233- echo " Error: Couldn't find debian files directory for $package , version ${ossec_version} " | write_log
242+ # Check that Debian packaging dirs exist (we only read from them)
243+ for package in ${packages[*]} ; do
244+ if [ ! -d " ${debian_files_path} /$package /debian" ]; then
245+ echo " Error: Couldn't find debian files directory for $package at ${debian_files_path} /$package /debian" | write_log
234246 exit 1
235247 fi
236248 done
237249
238- # Downloading file
239- if wget -O $scriptpath /${source_file} -U ossec https://github.com/ossec/ossec-hids/archive/${ossec_version} .tar.gz ; then
240- echo " Successfully downloaded source file ${source_file} from ossec.net" | write_log
241- else
242- echo " Error: File ${source_file} was could not be downloaded" | write_log
243- exit 1
244- fi
250+ mkdir -p " ${build_root} "
251+ cd " ${build_root} "
252+ tmp_directory=" ${NAME} -${ossec_version} "
245253
246- # Uncompressing files
247- tmp_directory=$( echo ${source_file} | sed -e ' s/.tar.gz$//' )
248- if [ -d ${scriptpath} /${tmp_directory} ]; then
249- echo " + Deleting previous directory ${scriptpath} /${tmp_directory} " | write_log
250- sudo rm -rf ${scriptpath} /${tmp_directory}
251- fi
252- tar -xvzf ${scriptpath} /${source_file}
253- if [ ! -d ${scriptpath} /${tmp_directory} ]; then
254- echo " Error: Couldn't find uncompressed directory, named ${tmp_directory} " | write_log
255- exit 1
254+ # Prefer local tree when we have a full repo (src/ present)
255+ if [ -d " ${repo_root} /src" ]; then
256+ echo " Building from local source tree at ${repo_root} " | write_log
257+ if [ -f " ${build_root} /${source_file} " ]; then
258+ rm -f " ${build_root} /${source_file} "
259+ fi
260+ (cd " ${repo_root} " && tar -czf " ${build_root} /${source_file} " --exclude=' .git' --exclude=" ${source_file} " \
261+ --transform " s,^\./,${tmp_directory} /," .)
262+ else
263+ # Download or use SOURCE_TARBALL
264+ if [ -n " ${SOURCE_TARBALL:- } " ] && [ -f " ${SOURCE_TARBALL} " ]; then
265+ cp -p " ${SOURCE_TARBALL} " " ${build_root} /${source_file} "
266+ echo " Using local source tarball ${SOURCE_TARBALL} " | write_log
267+ elif wget -O " ${build_root} /${source_file} " -U ossec " https://github.com/ossec/ossec-hids/archive/${ossec_version} .tar.gz" ; then
268+ echo " Successfully downloaded ${source_file} from GitHub" | write_log
269+ else
270+ echo " Error: Could not obtain ${source_file} . Set SOURCE_TARBALL or run from a git clone." | write_log
271+ exit 1
272+ fi
256273 fi
257274
258- # Organizing directories structure
259- for package in ${packages[*]}
260- do
261- if [ -d ${scriptpath} /$package ]; then
262- echo " + Deleting previous source directory ${scriptpath} /$package " | write_log
263- sudo rm -rf ${scriptpath} /$package
275+ # Unpack and prepare each package under build/ (never touch scriptpath/package)
276+ for package in ${packages[*]} ; do
277+ rm -rf " ${build_root} /${package} "
278+ mkdir -p " ${build_root} /${package} "
279+ tar -xzf " ${build_root} /${source_file} " -C " ${build_root} /${package} "
280+ if [ ! -d " ${build_root} /${package} /${tmp_directory} " ]; then
281+ echo " Error: Unpack did not create ${build_root} /${package} /${tmp_directory} " | write_log
282+ exit 1
264283 fi
265- mkdir $scriptpath /$package
266- cp -pr $scriptpath /${tmp_directory} $scriptpath /$package /$package -${ossec_version}
267- cp -p $scriptpath /${source_file} $scriptpath /$package /${package} _${ossec_version} .orig.tar.gz
268- cp -pr ${debian_files_path} /${ossec_version} /$package /debian $scriptpath /$package /${package} -${ossec_version} /debian
284+ target=" ${build_root} /${package} /${package} -${ossec_version} "
285+ if [ " ${build_root} /${package} /${tmp_directory} " != " ${target} " ]; then
286+ mv " ${build_root} /${package} /${tmp_directory} " " ${target} "
287+ fi
288+ cp -p " ${build_root} /${source_file} " " ${build_root} /${package} /${package} _${ossec_version} .orig.tar.gz"
289+ cp -pr " ${debian_files_path} /${package} /debian" " ${target} /debian"
269290 done
270- rm -rf $scriptpath /${tmp_directory}
271291
272- echo " The packages directories for ${packages[*]} version ${ossec_version} have been successfully prepared. " | write_log
292+ echo " Prepared source for ${packages[*]} version ${ossec_version} under ${build_root} " | write_log
273293}
274294
275295
288308
289309 echo " Building Debian package ${package} ${codename} -${arch} " | write_log
290310
291- local source_path=" $scriptpath /${package} /${package} -${ossec_version} "
292- local changelog_file=" ${source_path} /debian/changelog"
293- if [ ! -f ${changelog_file} ] ; then
294- echo " Error: Couldn't find changelog file for ${package} -${ossec_version} " | write_log
311+ # Prefer build/ (local or prepared source); fall back to scriptpath/package for backward compat
312+ local source_path=" "
313+ if [ -f " ${build_root} /${package} /${package} -${ossec_version} /debian/changelog" ]; then
314+ source_path=" ${build_root} /${package} /${package} -${ossec_version} "
315+ elif [ -f " $scriptpath /${package} /${package} -${ossec_version} /debian/changelog" ]; then
316+ source_path=" $scriptpath /${package} /${package} -${ossec_version} "
317+ fi
318+ if [ -z " ${source_path} " ] || [ ! -f " ${source_path} /debian/changelog" ]; then
319+ echo " Error: Couldn't find changelog for ${package} -${ossec_version} . Run -d first to prepare source." | write_log
295320 exit 1
296321 fi
322+ local changelog_file=" ${source_path} /debian/changelog"
297323
298324 # Updating changelog file with new codename, date and debdist.
299325 if update_changelog ${changelog_file} ${codename} ; then
317343 sudo mkdir -p ${results_dir}
318344 fi
319345
320- # Building the package
346+ # Building the package (configfile must be absolute so it works from any cwd)
347+ local build_log=" ${build_root} /pdebuild-${package} -${codename} -${arch} .log"
321348 cd ${source_path}
322- if sudo DIST=$codename ARCH=$arch /usr/bin/pdebuild --configfile $scriptpath /pbuilderrc --use-pdebuild-internal --architecture ${arch} --buildresult ${results_dir} -- --basetgz \
323- ${base_tgz} --distribution ${codename} --architecture ${arch} --aptcache ${cache_dir} --override-config ; then
349+ if sudo DIST=$codename ARCH=$arch /usr/bin/pdebuild --configfile " $scriptpath /pbuilderrc" --use-pdebuild-internal --architecture ${arch} --buildresult ${results_dir} -- --basetgz \
350+ ${base_tgz} --distribution ${codename} --architecture ${arch} --aptcache ${cache_dir} --override-config >> " ${build_log} " 2>&1 ; then
324351 echo " + Successfully built Debian package ${package} ${codename} -${arch} " | write_log
325352 else
326353 echo " Error: Could not build package $package ${codename} -${arch} " | write_log
354+ echo " Last 100 lines of build log (full log: ${build_log} ):" | write_log
355+ tail -100 " ${build_log} " | while read line; do echo " $line " | write_log; done
356+ echo " ---" | write_log
357+ echo " Build failed. Last 100 lines of build output:" 1>&2
358+ tail -100 " ${build_log} " 1>&2
327359 exit 1
328360 fi
329361
343375 echo " + Package ${results_dir} /${deb_file} ${codename} -${arch} contains ${files} files" | write_log
344376 fi
345377
378+ # Copy built artifacts into repo for easy access (build/result/<codename>-<arch>/)
379+ local out_dir=" ${build_root} /result/${codename} -${arch} "
380+ mkdir -p " ${out_dir} "
381+ sudo cp -p " ${results_dir} /${deb_file} " " ${results_dir} /${changes_file} " " ${out_dir} /" 2> /dev/null || true
382+ sudo cp -p " ${results_dir} " /* .buildinfo " ${out_dir} /" 2> /dev/null || true
383+ sudo chown " $( id -un) :$( id -gn) " " ${out_dir} " /* 2> /dev/null || true
384+
346385 echo " Successfully built Debian package ${package} ${codename} -${arch} " | write_log
347386
348387 done
349388 done
350389done
390+ echo " Built .deb and .changes are in ${build_root} /result/<codename>-<arch>/ and in /var/cache/pbuilder/<codename>-<arch>/result/<package>/" | write_log
351391}
352392
353393# Synchronizes with the external repository, uploading new packages and ubstituting old ones.
0 commit comments