#!/bin/sh # ===================================================================== # Projet : DARKCONTAINER # Fichier : lib/chroot # Auteur : Nicolas "DarkHack" Chapuis (c) 2026 # ================== DARKHACK ================== # [ darkweb.fr - system online ] # Powered by Perplexity, your AI assistant # https://www.perplexity.ai # ============================================== # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ===================================================================== if ! type "shInclude" > /dev/null; then racineSh="${racineSh:-/etc/darksh}" rootDarkContainer="${rootDarkContainer:-/etc/darkcontainer}" . "$racineSh/lib/shTools" shLog="${shLog:-"no-parent"}-chroot-init" shDebugEnable="${shDebugEnable:-1}" shDebugHidden "racineSh:${racineSh} rootDarkContainer:${rootDarkContainer}" >&2 fi [ ! -d "$rootDarkContainer" ] && shDebug 0 "exit cause rootDarkContainer is not dir:$rootDarkContainer" >&2 && exit 0 #|------------------------------| #| base "which" tools | #| cp | rm | echo | head | tail | #| grep | rev | tr | cut | sh | #| date | cat | touch | #|------------------------------| requiresCmd="cp rm echo head tail grep rev tr cut sh date cat touch" shWhichCmd $requiresCmd >/dev/null #shWarnOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) # &&&&& #|-----------------------------------------------------------------------| #| which "directory" tools : mkdir | chmod | chgrp | mount | umount | #|-----------------------------------------------------------------------| requiresCmd="mkdir chmod chgrp mount umount" shWhichCmd $requiresCmd >/dev/null #shWarnOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) # &&&&& #|----------------------------------| #| user right "which" tools | #| chmod | chown | chgrp | groupadd | #| useradd | chroot | getent | #|----------------------------------| requiresCmd="chmod chown chgrp groupadd useradd chroot getent" shWhichCmd $requiresCmd >/dev/null #shWarnOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) #$(shInclude $rootDarkContainer/which/lib/base) && $(shInclude $rootDarkContainer/which/lib/directory) && $(shInclude $rootDarkContainer/which/lib/userRight) cmdWhich="${cmdWhich:-$(which which)}" cmdWhich="${cmdWhich:-/usr/bin/which}" cmdMknode="${cmdMknode:-$($cmdWhich mknode 2>/dev/null)}" if [ -z "$cmdMknode" ];then cmdMknode="${cmdMknode:-$($cmdWhich mknod)}" fi cmdStartStopDaemon="${cmdStartStopDaemon:-$($cmdWhich start-stop-daemon 2>/dev/null)}" #pChrootDir="$pChrootDir" pName="$pName" pUser="$pUser" pGroup="$pGroup"\ #pCacheDir="$pCacheDir" pServiceInfo="$pServiceInfo" cmdDaemon="$cmdDaemon"\ #pLibFiles="$pLibFiles" pLibFolders="$pLibFolders" pCopyLib="$pCopyLib" pDirStruct="$pDirStruct"\ # chroot_init_install chroot_init_install() { local log="chroot_init_install:" shErrorOnMissing "pChrootDir pName pUser pGroup" shErrorOnMissing "pCacheDir pServiceInfo cmdDaemon" shWarnOnMissing "pLibFiles pLibFolders pCopyLib pDirStruct" racineSh="$racineSh" "$rootDarkContainer/bin/container" force-stop "$(echo $pChrootDir | base64)" #chroot init_clear $pChrootDir $pName $pUser $pGroup $pBaseDir $pUserComment $pExtra chroot_init_clear "$pChrootDir" "$pName" "$pUser" "$pGroup" "$pCacheDir" "$pServiceInfo" shDebug 1 ""$log" :chroot_install $cmdStartStopDaemon /etc/nsswitch.conf" for file in $cmdStartStopDaemon /etc/nsswitch.conf;do chroot_install $pChrootDir $file done # for cmd in $cmdGrep $cmdTr;do # chroot_install $pChrootDir $cmd # done shDebug 1 ""$log" rsyslog_init" rsyslog_init "$pName" "$pChrootDir" shDebug 1 ""$log" chroot_install $cmdDaemon" chroot_install "$pChrootDir" "$cmdDaemon" shDebug 1 ""$log" chroot_install pLibFiles:$(echo $pLibFiles | cut -c 1-15)" for file in $pLibFiles;do chroot_install "$pChrootDir" "$file" done shDebug 1 ""$log" chroot_install pLibFolders:$(echo $pLibFolders | cut -c 1-15)" for dir in $pLibFolders;do chroot_install_debug=1 chroot_install "$pChrootDir" "$dir" done shDebug 1 ""$log" chroot_install pCopyLib:$(echo $pCopyLib | cut -c 1-15)" for instruction in $pCopyLib;do # chroot_install $pChrootDir $dir source="$($cmdEcho $instruction | $cmdCut -f1 -d';')" dest="$($cmdEcho $instruction | $cmdCut -f2 -d';')" shDebug 1 ""$log" cp -ar $pChrootDir/$source/* $pChrootDir/$dest" $cmdCp -ar $pChrootDir/$source/* $pChrootDir/$dest done # pDirStruct : directory squelton of programme shDebug 1 ""$log" chroot_install pDirStruct:$(echo $pDirStruct | cut -c 1-15)" for dir in $pDirStruct;do chroot_root_execute $pChrootDir $cmdMkdir -p "$dir"; chroot_root_execute $pChrootDir $cmdChown -R "$pUser:$pGroup" "$dir" # chroot_root_execute $pChrootDir $cmdChgrp $pGroup "$dir" chroot_root_execute $pChrootDir $cmdChmod a-rwx -R "$dir" chroot_root_execute $pChrootDir $cmdChmod u+rwx -R "$dir" done } #chroot_init_dpkg $pChrootDir $pName $pUser $pGroup $pBaseDir $pUserComment $pExtra chroot_init_dpkg() { local shLog="chroot_init_dpkg" local pChrootDir="${pChrootDir:-$1}" local pName="${pName:-$2}" local pUser="${pUser:-$3}" local pGroup="${pGroup:-$4}" local pBaseDir="${pBaseDir:-$5}" local pUserComment="${pUserComment:-$6}" local pExtra="${pExtra:-$7}" #/bin/sh fonctionnel shDebug 1 "chroot_init_clear" chroot_init_clear "${pChrootDir}" "${pName}" "${pUser}" "${pGroup}" "${pBaseDir}" "${pUserComment}" "${pExtra}" local libArch="" if [ -d "/usr/lib/aarch64-linux-gnu" ];then local libArch="aarch64-linux-gnu" elif [ -d "/usr/lib/x86_64-linux-gnu" ];then local libArch="x86_64-linux-gnu" elif [ -d "/usr/lib/i386-linux-gnu" ];then local libArch="i386-linux-gnu" fi if [ -z "$libArch" ];then if [ -d "/lib/aarch64-linux-gnu" ];then local libArch="aarch64-linux-gnu" elif [ -d "/lib/x86_64-linux-gnu" ];then local libArch="x86_64-linux-gnu" elif [ -d "/lib/i386-linux-gnu" ];then local libArch="i386-linux-gnu" fi fi local basicLoader="libz.so.1 libm.so.6 liblzma.so.5 libzstd.so.1 libmount.so.1 libblkid.so.1" for file in $basicLoader;do # chroot_install_debug=1 chroot_install $pChrootDir $file if [ -f "/lib/${libArch}/$file" ];then # chroot_install $pChrootDir /usr/lib/${libArch}/$file cp "/lib/${libArch}/$file" "$pChrootDir/usr/lib/${libArch}/$file" elif [ -f "/lib/${libArch}/$file" ];then # chroot_install $pChrootDir /usr/lib/${libArch}/$file cp "/lib/${libArch}/$file" "$pChrootDir/usr/lib/${libArch}/$file" fi done $cmdMkdir -p "$pChrootDir/var/lock" $cmdMkdir -p "$pChrootDir/var/lib/dpkg/info" $cmdMkdir -p "$pChrootDir/var/lib/dpkg/updates" $cmdMkdir -p "$pChrootDir/var/lib/dpkg/parts" $cmdMkdir -p "$pChrootDir/var/lib/dpkg/triggers" # shDebug 1 $cmdMkdir -p "$pChrootDir/var/lib/dpkg/tmp.ci" $cmdMkdir -p "$pChrootDir/var/lib/dpkg/tmp.ci" $cmdMkdir -p "$pChrootDir/var/lib/dpkg/alternatives" $cmdTouch "$pChrootDir/var/lib/dpkg/status" $cmdTouch "$pChrootDir/var/lib/dpkg/available" $cmdMkdir -p "$pChrootDir/var/cache/debconf" $cmdTouch "$pChrootDir/var/cache/debconf/config.dat" listCmd="" listCmd="$listCmd uname basename" listCmd="$listCmd mount mountpoint perl" listCmd="$listCmd ldconfig" shWhichCmd $listCmd >/dev/null shErrorOnMissing $( shWhichCmd $requiresCmd | $cmdTr '_' ' ' ) for file in $( shWhichCmd $listCmd | $cmdTr '_' ' ' );do local cmdPath="$(eval $cmdEcho \$$file )" shDebug 1 "shWhichCmd $file:$cmdPath" shErrorOnMissing cmdPath [ ! -f "${pChrootDir}/${cmdPath}" ] && chroot_install "${pChrootDir}" "${cmdPath}" done chroot_install "${pChrootDir}" "$( which start-stop-daemon )" shDebug 1 "install perl-base" # [ -d "/usr/lib/${libArch}/perl-base" ] && chroot_install_debug=1 chroot_install "${pChrootDir}" "/usr/lib/${libArch}/perl-base" [ -d "/usr/lib/${libArch}/perl-base" ] && chroot_install "${pChrootDir}" "/usr/lib/${libArch}/perl-base" [ -d "/lib/${libArch}/perl-base" ] && chroot_install "${pChrootDir}" "/lib/${libArch}/perl-base" shDebug 1 "chroot install" #chroot_install $pChrootDir "/usr/bin/sh" #chroot_install $pChrootDir "$( which infocmp )" binDpkg="ldconfig od $cmdMknode uniq head dpkg" # binDpkg="$binDpkg cd" binDpkg="$binDpkg dpkg-deb dpkg-query dpkg-maintscript-helper dpkg-split dpkg-query dpkg-divert" binDpkg="$binDpkg dpkg-maintscript-helper dpkg-trigger dpkg-reconfigure" binDpkg="$binDpkg pkg-config update-alternatives" for file in $binDpkg;do cmdFound="$(which $file)" cmdFound="${cmdFound:-$(PATH="$PATH:/usr/sbin" which $file)}" shDebug 1 "chroot install $file: $cmdFound" shErrorOnMissing cmdFound # chroot_install_debug=1 chroot_install $pChrootDir "$cmdFound" chroot_install "$pChrootDir" "$cmdFound" done extraBinDpkg="ldconfig.real dpkg-realpath" for file in $extraBinDpkg;do cmdFound="$(which $file)" cmdFound="${cmdFound:-$(PATH="$PATH:/usr/sbin" which $file)}" # chroot_install_debug=1 chroot_install $pChrootDir "$cmdFound" [ -n "$cmdFound" ] && chroot_install "$pChrootDir" "$cmdFound" && shDebug 1 "chroot install $file:$cmdFound" done chroot_install "$pChrootDir" "/usr/share/dpkg/sh/dpkg-error.sh" chroot_install "$pChrootDir" "/usr/share/debconf/frontend" # chroot_install "$pChrootDir" "/usr/share/perl5/Debconf" chroot_install "$pChrootDir" "/usr/share/perl" chroot_install "$pChrootDir" "/usr/share/perl5" chroot_install "$pChrootDir" "/etc/debconf.conf" chroot_install "$pChrootDir" "/usr/share/debconf/debconf.conf" local perlVersion="$( apt-cache policy perl-base | grep Install | cut -d: -f2 |tr -d ' ' | cut -d. -f1,2 )" local basicPerlLoader="perl/$perlVersion" # local basicPerlLoader="${basicLoader} libbz2.so.1.0" for file in $basicPerlLoader;do # echo file:$file # chroot_install_debug=1 chroot_install $pChrootDir $file if [ -d "/usr/lib/${libArch}/$file" ];then # echo chroot_install $pChrootDir /usr/lib/${libArch}/$file chroot_install "$pChrootDir" "/usr/lib/${libArch}/$file" # rm "$pChrootDir/usr/lib/${libArch}/$file" # cp "/usr/lib/${libArch}/$file" "$pChrootDir/usr/lib/${libArch}/$file" elif [ -d "/lib/${libArch}/$file" ];then # echo chroot_install $pChrootDir /lib/${libArch}/$file chroot_install "$pChrootDir" "/lib/${libArch}/$file" # rm "$pChrootDir/usr/lib/${libArch}/$file" # cp "/lib/${libArch}/$file" "$pChrootDir/usr/lib/${libArch}/$file" fi done } #chroot_init_clear $pChrootDir $pName $pUser $pGroup $pBaseDir $pUserComment $pExtra chroot_init_clear() { local shLog="chroot_init_clear" local pChrootDir="${pChrootDir:-$1}" local pName="${pName:-$2}" local pUser="${pUser:-$3}" local pGroup="${pGroup:-$4}" local pBaseDir="${pBaseDir:-$5}" local pUserComment="${pUserComment:-$6}" local pExtra="${pExtra:-$7}" if [ -d "$pChrootDir" ];then shDebug 1 "clean an init $pChrootDir" chroot_umount "${pChrootDir}" || ( echo "FATAL UMOUNT"; exit 1; ) $cmdRm -R "$pChrootDir" fi shDebug 1 "$cmdMkdir $pChrootDir" $cmdMkdir "$pChrootDir" $cmdMkdir "$pChrootDir/etc" $cmdMkdir "$pChrootDir/etc/alternatives" $cmdTouch "$pChrootDir/etc/group" $cmdMkdir "$pChrootDir/tmp" $cmdMkdir -p "$pChrootDir/var/log" $cmdMkdir -p "$pChrootDir/run" $cmdMkdir -p "$pChrootDir/usr" if [ -d "/usr/lib64" ];then # if true;then $cmdMkdir -p "$pChrootDir/usr/lib" $cmdMkdir -p "$pChrootDir/usr/lib32" $cmdMkdir -p "$pChrootDir/usr/lib64" $cmdMkdir -p "$pChrootDir/usr/bin" $cmdMkdir -p "$pChrootDir/usr/bin64" $cmdMkdir -p "$pChrootDir/usr/sbin" local oldPwd="$(pwd)" cd "$pChrootDir" $cmdLn -s "usr/lib" "lib" $cmdLn -s "usr/lib32" "lib32" $cmdLn -s "usr/lib64" "lib64" $cmdLn -s "usr/bin" "bin" $cmdLn -s "usr/bin64" "bin64" $cmdLn -s "usr/sbin" "sbin" cd $oldPwd else $cmdMkdir -p "$pChrootDir/lib" $cmdMkdir -p "$pChrootDir/lib32" # $cmdMkdir -p "$pChrootDir/lib64" $cmdMkdir -p "$pChrootDir/bin" # $cmdMkdir -p "$pChrootDir/bin64" $cmdMkdir -p "$pChrootDir/sbin" # local oldPwd="$(pwd)" # cd "$pChrootDir" # $cmdLn -s "/lib" "usr/lib" # $cmdLn -s "/lib32" "usr/lib32" # $cmdLn -s "/lib64" "usr/lib64" # $cmdLn -s "/bin" "usr/bin" # $cmdLn -s "/bin64" "usr/bin64" # $cmdLn -s "/sbin" "usr/sbin" # cd $oldPwd fi # find "$pChrootDir" for file in hosts services rpc protocols networks netgroup default/nss nsswitch.conf locale.gen localtime login.defs default/locale;do [ ! -f "$pChrootDir/etc/$file" ] && chroot_install $pChrootDir /etc/$file done local basicLoader="/dev/urandom /dev/random /dev/null /dev/kmsg" [ -f "/lib/ld-linux.so.2" ] && local basicLoader="${basicLoader} /lib/ld-linux.so.2" [ -f "/lib/ld-linux-aarch64.so.1" ] && local basicLoader="${basicLoader} /lib/ld-linux-aarch64.so.1" [ -f "/usr/lib/ld-linux.so.2" ] && local basicLoader="${basicLoader} /usr/lib/ld-linux.so.2" [ -f "/usr/lib/ld-linux-aarch64.so.1" ] && local basicLoader="${basicLoader} /usr/lib/ld-linux-aarch64.so.1" [ -f "/usr/lib64/ld-linux-x86-64.so.2" ] && local basicLoader="${basicLoader} /usr/lib64/ld-linux-x86-64.so.2" for file in $basicLoader;do # chroot_install_debug=1 chroot_install $pChrootDir $file chroot_install $pChrootDir $file done local libArch="" if [ -d "/usr/lib/aarch64-linux-gnu" ];then local libArch="aarch64-linux-gnu" elif [ -d "/usr/lib/x86_64-linux-gnu" ];then local libArch="x86_64-linux-gnu" elif [ -d "/usr/lib/i386-linux-gnu" ];then local libArch="i386-linux-gnu" fi if [ -z "$libArch" ];then if [ -d "/lib/aarch64-linux-gnu" ];then local libArch="aarch64-linux-gnu" elif [ -d "/lib/x86_64-linux-gnu" ];then local libArch="x86_64-linux-gnu" elif [ -d "/lib/i386-linux-gnu" ];then local libArch="i386-linux-gnu" fi fi local basicLoader="libselinux.so.1 libaudit.so.1 libbsd.so.0 libsemanage.so.2 libacl.so.1 libcap-ng.so.0 libpcre2-8.so.0 libattr.so.1 libmd.so.0 libsepol.so.2" local basicLoader="${basicLoader} libbz2.so.1.0" for file in $basicLoader;do # chroot_install_debug=1 chroot_install $pChrootDir $file if [ -f "/usr/lib/${libArch}/$file" ];then chroot_install "$pChrootDir" "/usr/lib/${libArch}/$file" # rm "$pChrootDir/usr/lib/${libArch}/$file" # cp "/usr/lib/${libArch}/$file" "$pChrootDir/usr/lib/${libArch}/$file" elif [ -f "/lib/${libArch}/$file" ];then chroot_install "$pChrootDir" "/lib/${libArch}/$file" # rm "$pChrootDir/lib/${libArch}/$file" # cp "/lib/${libArch}/$file" "$pChrootDir/lib/${libArch}/$file" fi done shDebug 1 "host and protocole file initied" listCmd="false echo cut tr expr mkdir cat grep egrep cut ps rev ls md5sum cp sh env rm date chown chmod echo printf ln sed" listCmd="$listCmd uname basename sleep mktemp dirname sync gzip mv" listCmd="$listCmd tempfile ldconfig tee stat ldd tar diff find readlink free useradd groupadd" listCmd="$listCmd sort awk xargs tail getopt nologin ls rmdir" shWhichCmd $listCmd >/dev/null for file in $( shWhichCmd $listCmd | $cmdTr '_' ' ' );do local cmdPath="$(eval $cmdEcho \$$file )" shDebug 1 "shWhichCmd $file:$cmdPath" shErrorOnMissing cmdPath # echo chroot_install $pChrootDir $cmdPath # [ ! -f "${pChrootDir}/${cmdPath}" ] && chroot_install $pChrootDir $cmdPath # chroot_install_debug=1 chroot_install "$pChrootDir" "$cmdPath" chroot_install "$pChrootDir" "$cmdPath" done # cp "/bin/which" "$pChrootDir/usr/bin/which" # cp "/bin/touch" "$pChrootDir/usr/bin/touch" for binCmd in "true" "false" "bash" "sh" "which" "touch";do [ ! -f "${pChrootDir}/bin/${binCmd}" ] && chroot_install $pChrootDir /bin/${binCmd} [ ! -f "${pChrootDir}/usr/bin/${binCmd}" ] && chroot_install $pChrootDir /usr/bin/${binCmd} done chroot_root_execute $pChrootDir "$cmdGroupadd -g 0 root" chroot_root_execute $pChrootDir "$cmdUseradd -u 0 -g 0 -c \"root account\" -m -d /root -e \"\" -s /usr/bin/bash root" # chroot $pChrootDir chown -R _apt:root "/var/cache/apt/archives" # for file in group gshadow passwd shadow ;do # chroot_root_execute $pChrootDir "$cmdTouch /etc/$file" ## chroot_install $pChrootDir /etc/$file # done # [ ! -f "${pChrootDir}/${cmdUseradd}" ] && chroot_install $pChrootDir $cmdUseradd # [ ! -f "${pChrootDir}/${cmdGroupadd}" ] && chroot_install $pChrootDir $cmdGroupadd shDebug 1 "base tools initied" shDebug 1 ""$log" :chroot_install create $pUser $pGroup extra:$pExtra" chroot_mount "${pChrootDir}" "${racineSh}" if [ -z "$pExtra" ];then chroot_groupadd "$pChrootDir" "$pGroup" #chroot_useradd "$pChrootDir" "$pUser" "$pGroup" "$pBaseDir" "$pUserComment" "$pExtra" chroot_useradd "${pChrootDir}" "${pUser}" "${pGroup}" "${pBaseDir}" "${pUserComment}" shDebug 1 ""$log" :chroot user created" else chroot_root_execute "${pChrootDir}" "$cmdUseradd -c \"$pUserComment\" -d $pBaseDir -e \"\" -s /bin/false $pExtra $pUser " fi chroot_umount "${pChrootDir}" } # chroot_mount $pChrootDir $pMountFolder chroot_mount() { local log="chroot_mount:" local pChrootDir="$1" local pMountFolder="$2" shDebug 1 ""$log" check :$cmdMount $pChrootDir" for folder in $pMountFolder;do shDebug 1 ""$log" mount $folder ${pChrootDir}$folder" if [ -d "$folder" ];then #chroot_root_execute "$pChrootDir" "if [ ! -d \"$folder\" ]; then \"$cmdMkdir\" -p \"$folder\"; fi" [ ! -d "$pChrootDir/$folder" ] && $cmdMkdir -p "$pChrootDir/$folder" fi local srcFolder=$folder if [ "$folder" = "/proc" ];then local mountParam="-t proc" local srcFolder="none" elif [ "$folder" = "/sys" ];then local mountParam="-t sysfs" #local mountParam="--rbind" elif [ "$folder" = "/dev" ];then local mountParam="--bind" #local mountParam="--rbind" else local mountParam="--bind" if [ -f "$folder" ];then $cmdTouch "${pChrootDir}$folder" fi fi $cmdMount $mountParam $srcFolder ${pChrootDir}$folder # if [ -d "$folder" ] || [ -f "$folder" ] || [ -x "$folder" ];then ## chroot_root_execute $pChrootDir "$cmdChown -R $pUser:$pGroup ${pChrootDir}$folder" ## chroot_root_execute $pChrootDir "$cmdChmod a-rwx -R ${pChrootDir}$folder" ## chroot_root_execute $pChrootDir "$cmdChmod a+rw -R ${pChrootDir}$folder" ## chroot_root_execute $pChrootDir "$cmdChmod u+x -R ${pChrootDir}$folder" # chroot_root_execute $pChrootDir "$cmdChown -R $pUser:$pGroup $folder" # chroot_root_execute $pChrootDir "$cmdChmod a-rwx -R $folder" # chroot_root_execute $pChrootDir "$cmdChmod a+rw -R $folder" # chroot_root_execute $pChrootDir "$cmdChmod u+x -R $folder" # fi done } # chroot_umount $pChrootDir chroot_umount() { local log="chroot_umount:" local pChrootDir="$1" for mountPath in $($cmdMount -l | $cmdCut -d' ' -f3 | $cmdGrep "^$pChrootDir");do shDebug 1 ""$log" umount:$mountPath" if [ $($cmdUmount -l $mountPath) ];then exit 1;fi done } #chroot_groupadd "$pChrootDir" "$pGroup" chroot_groupadd() { local pChrootDir="$1" local pGroup="$2" chroot_install $pChrootDir $cmdGroupadd chroot_install $pChrootDir $cmdGetent local run="#!/bin/sh\n\ racineSh=\"$racineSh\" \n\ . \$racineSh/lib/shTools \n\ \n\ shLog=\"$pName-groupeadd:\" \n\ shDebugEnable=\"1\" \n\ shDebug \"\$*\" \n\ if [ ! -f /etc/group ];then $cmdTouch /etc/group;fi \n\ if ! shGroupExist $pGroup ;then \n\ shDebug \"group not exist, we will create now. $pGroup\" \n\ $cmdGroupadd $pGroup \n\ if [ \$($cmdGetent group $pGroup) ];then \n\ shDebug 1 \"[ succes ] create group:$pGroup\" \n\ else \n\ shDebug 1 \"create group:$pGroup\" \n\ fi \n\ fi \n\ " if [ ! -d $pChrootDir/proc ];then $cmdMkdir $pChrootDir/proc;fi $cmdMount --bind /proc $pChrootDir/proc chroot_root_execute $pChrootDir "$(echo $run | tr '\n' '¤')" 2>&1 $cmdUmount $pChrootDir/proc } #chroot_useradd "$pChrootDir" "$pUser" "$pGroup" "$pBaseDir" "$pUserComment" "$pExtra" chroot_useradd() { local pChrootDir="$1" local pUser="$2" local pGroup="$3" local pBaseDir="$4" local pUserComment="$5" local pExtra="$6" chroot_install $pChrootDir $cmdUseradd local run="#!/bin/sh\n\ racineSh=\"$racineSh\" \n\ . \$racineSh/lib/shTools \n\ \n\ shLog=\"$pName-useradd:\" \n\ shDebugEnable=\"1\" \n\ shDebug \"\$*\" \n\ [ ! -f /etc/passwd ] && $cmdTouch /etc/passwd \n\ if ! shUserExist $pUser ;then \n\ shDebug 1 \"user not exist, we will create now. $pUser\" \n\ $cmdUseradd -g $pGroup -b $pBaseDir -c \"$pUserComment\" -d $pBaseDir -e \"\" -s /bin/false $pUser $pExtra \n\ shDebug 1 \"[ succes ] create user:$pUser\" \n\ fi \n\ " [ ! -d $pChrootDir/proc ] && $cmdMkdir $pChrootDir/proc $cmdMount --bind /proc $pChrootDir/proc chroot_root_execute $pChrootDir "$(echo $run | tr '\n' '¤')" $cmdUmount $pChrootDir/proc } chroot_root_execute() { local log="chroot_root_execute:" local pChrootDir="$1" local run="" local first=1 for param in $*;do if [ $first -eq 0 ];then local run="$run $param" fi local first=0 done shDebugHidden 1 ""$log" $pChrootDir $run" [ -z "$cmdChroot" ] && cmdChroot="$(which chroot)" [ -z "$cmdChroot" ] && [ -f "/usr/sbin/chroot" ] && cmdChroot="/usr/sbin/chroot" [ -z "$cmdChroot" ] && shDebug 0 ""$log" empty cmdChroot=$cmdChroot" && exit 1 [ ! -x "$cmdChroot" ] && shDebug 0 ""$log" not exec cmdChroot=$cmdChroot" && exit 1 # $cmdChroot --userspec=root:root $pChrootDir $cmdSh -c "$(echo $run | tr '¤' '\n')" $cmdChroot $pChrootDir "$cmdSh" -c "export LD_LIBRARY_PATH=\"/usr/local/ssl/lib:/usr/local/lib\";$(echo $run | tr '¤' '\n')" } #chroot_gen_conf "$pChrootDir" "$pName" "$pUser" "$pGroup" "$pMountFolder" "$pDirUserGroupRight" chroot_gen_conf() { local pChrootDir="$1" local pName="$2" local pUser="$3" local pGroup="$4" local pMountFolder="$5" local pDirUserGroupRight="$6" local dataFile="$rootDarkContainer/data/$($cmdEcho $pChrootDir | base64 -i)"; $cmdEcho -e "#!/bin/sh" > $dataFile $cmdEcho -e "pChrootDir=\"$pChrootDir\"" >> $dataFile $cmdEcho -e "pMountFolder=\"$pMountFolder\"" >> $dataFile $cmdEcho -e "pUser=\"$pUser\"" >> $dataFile $cmdEcho -e "pGroup=\"$pGroup\"" >> $dataFile $cmdEcho -e "pDirUserGroupRight=\"$pDirUserGroupRight\"" >> $dataFile } chroot_install() { local log="chroot_install:" local dest="$1" local exec="$2" if [ -z "$dest" ] || [ -z "$exec" ];then return 0;fi local repOfExec="$(echo $exec | rev | cut -f2- -d'/' | rev | tr -d ' ')" if [ -n "$repOfExec" ] && [ ! -d "$dest/$repOfExec" ];then $cmdMkdir -p "$dest/$repOfExec";fi if [ ! "$(echo $dest | rev | cut -c1-1)" = "/" ];then local dest="$dest/";fi local execFile="$(echo $exec | rev | cut -f1 -d'/' | rev | tr -d ' ')" # [ "$chroot_install_debug" ] && shDebug 1 "${log} $exec => $dest" if [ "$exec" = "/proc" ];then return 0 # mkdir -p "$dest$exec" # mount -t proc proc $dest$exec elif [ -d "$exec" ];then [ "$chroot_install_debug" ] && shDebug 1 "${log} dir :$exec" if [ ! "$(echo $exec | rev | cut -c1-1)" = "/" ];then local exec="$exec/";fi if [ ! -d "$dest$exec" ];then $cmdMkdir -p "$dest$exec";fi for sub in $(ls -a "$exec");do if [ ! "$sub" = "." ] && [ ! "$sub" = ".." ];then # if ! chroot_install "$dest" "$exec$sub"; then return 1;fi chroot_install "$dest" "$exec$sub" fi done elif [ -L "$exec" ];then [ "$chroot_install_debug" ] && shDebug 1 "${log} link :$exec" if [ -L "$dest/$exec" ];then return 0;fi local linkDest="$(ls -an $exec | cut -d'>' -f2 | tr -d ' ')" if [ "$(echo $linkDest | cut -c1-1)" = "/" ];then if ! chroot_install "$dest" "$linkDest";then return 1;else local oldPwd="$(pwd)" cd "$dest$repOfExec" [ "$chroot_install_debug" ] && shDebug 1 "${log} ln -s "$linkDest" "$execFile"" ln -s "$linkDest" "$execFile" cd "$oldPwd" fi else if ! chroot_install "$dest" "$repOfExec/$linkDest";then return 1;else local oldPwd="$(pwd)" cd "$dest$repOfExec" [ "$chroot_install_debug" ] && shDebug 1 "${log} ln -s "$repOfExec/$linkDest" "$execFile"" ln -s "$repOfExec/$linkDest" "$execFile" cd "$oldPwd" fi fi elif [ -b "$exec" ];then [ "$chroot_install_debug" ] && shDebug 1 "${log} block device :$exec" [ -b "$dest/$exec" ] && return 0 local majeur="$(ls -an $exec | cut -d' ' -f5,6 | cut -d',' -f 1 | tr -d ' ')" local mineur="$(ls -an $exec | cut -d' ' -f5,6 | cut -d',' -f 2 | tr -d ' ')" mknod "$dest$exec" b "$majeur" "$mineur" elif [ -c "$exec" ];then [ "$chroot_install_debug" ] && shDebug 1 "${log} caractere device :$exec" [ -c "$dest/$exec" ] && return 0 local majeur="$(ls -an $exec | cut -d' ' -f5,6 | cut -d',' -f 1 | tr -d ' ')" local mineur="$(ls -an $exec | cut -d' ' -f5,6 | cut -d',' -f 2 | tr -d ' ')" mknod "$dest$exec" c "$majeur" "$mineur" elif [ -f "$exec" ];then [ "$chroot_install_debug" ] && shDebug 1 "${log} file :$exec => $dest/$repOfExec" [ -L "$dest" ] && ( [ "$chroot_install_debug" ] && shDebug 2 "${log} dest is link:$dest" ) && exit 0 if [ -L "$dest/$repOfExec" ];then [ "$chroot_install_debug" ] && shDebug 2 "${log} dest final is link:$dest/$repOfExec" linkedDest="$( ls -ahn "$dest$repOfExec" | cut -f2 -d'>' | cut -f2- -d'/' )" [ -f "$dest$linkedDest/$execFile" ] && return 0 chroot_libanalyse "$exec" "$dest$linkedDest" [ "$chroot_install_debug" ] && shDebug 2 "${log} cp "$exec" "$dest$linkedDest"" # echo cp "$exec" "$dest$linkedDest" cp "$exec" "$dest$linkedDest" # exit 0 fi [ -f "$dest/$exec" ] && return 0 chroot_libanalyse "$exec" "$dest" [ "$chroot_install_debug" ] && shDebug 2 "${log} cp "$exec" "$dest/$repOfExec"" cp "$exec" "$dest/$repOfExec" else shDebug 2 "not exist exec :$exec" return 1 fi # [ "$chroot_install_debug" ] && shDebug 1 "${log} stat -c '%a %n' $exec | cut -d' ' -f 1 " local perm="$(stat -c '%a %n' $exec | cut -d' ' -f 1)" chmod $perm "$dest$exec" local pUser="$(stat -c '%u' $exec)" local pGroup="$(stat -c '%g' $exec)" chown $pUser:$pGroup "$dest$exec" return 0 } chroot_libanalyse() { local libToAnalyse="$1" local dest="$2" for libToLink in $(ldd $libToAnalyse 2>/dev/null | cut -f2 -d '>' | cut -f1 -d'(' | tr -d ' ' );do # if [ -n "$(echo $libToAnalyse | grep /usr/local/apache2/modules/mod_deflat)" ];then # echo "chroot_libcp "$libToLink" "$dest"" # fi chroot_libcp "$libToLink" "$dest" done } chroot_libcp() { local libFullPath="$1" local dest="$2" local libRep="$(echo $libFullPath | rev | cut -f2- -d'/' | rev | tr -d ' ')" local libFile="$(echo $libFullPath | rev | cut -f1 -d'/' | rev | tr -d ' ')" if [ "$libRep" = "$libFile" ] || [ "$libFile" = "statically linked" ];then return ;fi if [ -f "$dest/$libFullPath" ] || [ -L "$dest/$libFullPath" ];then return ;fi #echo "chroot_libcp $libFullPath $dest" if [ ! -d "$dest/$libRep" ];then mkdir -p "$dest/$libRep";fi #echo "libRep :$libRep, libFile :$libFile, libFullPath:$libFullPath" if [ -L "$libFullPath" ];then local linkDestFullPath="$(ls -an $libFullPath | cut -d'>' -f2 | tr -d ' ')" if [ "$(echo $linkDestFullPath | cut -c1-1)" = "/" ];then chroot_libcp "$linkDestFullPath" "$dest" else chroot_libcp "$libRep/$linkDestFullPath" "$dest" fi local linkDestRep="$(echo $linkDestFullPath | rev | cut -f2- -d'/' | rev)" local linkDestFile="$(echo $linkDestFullPath | rev | cut -f1 -d'/' | rev)" oldPwd="$(pwd)" cd "$dest/$libRep" if [ "$(echo $linkDestFullPath | cut -c1-1)" = "/" ];then ln -s "$linkDestFullPath" "$libFile" else if [ "$(echo $linkDestFullPath | cut -c1-1)" = "." ];then ln -s "./$linkDestFullPath" "$libFile" else if [ -n "$(echo $linkDestFullPath | grep /)" ];then echo "linkDestFullPath with folder ? :$$linkDestFullPath" else # pwd # echo ln -s "$linkDestFullPath" "$libFile" ln -s "$linkDestFullPath" "$libFile" # else # echo "[unknow]ln -s => ? ./ => "$linkDestFullPath" "$libFile"" fi fi fi cd "$oldPwd" else cp "$libFullPath" "$dest$libFullPath" chroot_libanalyse "$libFullPath" "$dest" fi }