#!/bin/sh # ===================================================================== # Projet : DARKCONTAINER # Fichier : bin/container # 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. # ===================================================================== racineSh="${racineSh:-/etc/darksh}" rootDarkMake="${rootDarkMake:-/etc/darkmake}" rootDarkContainer="${rootDarkContainer:-/etc/darkcontainer}" if ! type "shInclude" > /dev/null 2>&1; then . "$racineSh/lib/shTools";fi $( shInclude $racineSh/lib/shWait ) $( shInclude $rootDarkContainer/lib/chroot ) shScriptName="chroot-container" shLog="$shScriptName/$1:" shDebugEnable="1" LogFile="/run/darkcontainer.log" shDebug 1 "$*" >> $LogFile 2>&1 action="$1" dataFile="$2" filePid="$3" #if [ ! "$1" = "check-pid" ];then $0 check-pid "$2" "$3";fi shStartStopScript "$shScriptName" >> $LogFile 2>&1 #|------------------------------| #| base "which" tools | #| cp | rm | echo | head | tail | #| grep | rev | tr | cut | sh | #| date | cat | touch | ps | ls | #|------------------------------| # &&&&& #|-----------------------------------------------------------------------| #| which "directory" tools : mkdir | chmod | chgrp | mount | umount | #|-----------------------------------------------------------------------| # &&&&& #|------------------------------------------------------------------------------------| #| which "user right" tools : chmod | chown | chgrp | groupadd | useradd | chroot | #|------------------------------------------------------------------------------------| $(shInclude ${rootDarkContainer}/which/lib/base) && $(shInclude ${rootDarkContainer}/which/lib/directory) && $(shInclude ${rootDarkContainer}/which/lib/userRight) cmdLs="$(which ls)" if [ ! -d "/run/darkcontainer" ];then mkdir /run/darkcontainer;fi chrootTmp="/run/darkcontainer" if [ -z "$dataFile" ];then shDebug 0 "usage :\$action \$dataFile [\$param]" >> $LogFile 2>&1 shDebug 0 "usage :$action $dataFile [$param] $1 $2 $3" >> $LogFile 2>&1 else if [ -f "${rootDarkContainer}/data/$dataFile" ];then dataFile="${rootDarkContainer}/data/$dataFile" fi $(shInclude "$dataFile") [ ! -f "$dataFile" ] && shDebug 0 "no data:$dataFile" >> $LogFile 2>&1 && exit 1 if [ -z "$action" ] || [ -z "$pChrootDir" ] || [ -z "$pMountFolder" ] || [ -z "$pUser" ] || [ -z "$pGroup" ];then shDebug 0 "usage : invalid conf \$pChrootDir \$pMountFolder" >> $LogFile 2>&1 else if [ -n "${filePid}" ];then #filePid="${pChrootDir}/${param}" if [ ! -f "${pChrootDir}/${filePid}" ];then shDebug 1 "[${pChrootDir}] no file pid :${filePid}" >> $LogFile 2>&1 else registerPid="$(cat ${pChrootDir}/${filePid} | tr -d '[:space:]' )" shDebug 1 "[${pChrootDir}] register pid : ${registerPid} ${filePid}" >> $LogFile 2>&1 fi fi idB64="$(echo $pChrootDir | base64 -i | tr -d '[:space:]' | tr -d '=+' | tr '/' '_' )" [ ! -d "${chrootTmp}/${idB64}" ] && mkdir "${chrootTmp}/${idB64}" pidsFile="${chrootTmp}/${idB64}/pids" lockChrootFile="${chrootTmp}/${idB64}/lock" LogFile="${chrootTmp}/${idB64}/log" shDebug 1 "[${pChrootDir}] idB64:${idB64}" >> $LogFile 2>&1 case "$action" in check-pid) if [ -f $lockChrootFile ];then oneFound=0 [ -f "${pidsFile}" ] && for pid in $(cat "${pidsFile}" | tr -d '|' );do if [ -n "$pid" ];then shDebug 1 "[${pChrootDir}:check-pid] chroot check pid [$pid]:$(ps -ax -o pid | grep "$pid" 2>&1)" >> $LogFile 2>&1 found=0 for testPid in $( ps -ax -o pid | grep "$pid" );do if [ "|${testPid}|" = "|${pid}|" ];then found=1;oneFound=1;break;fi;done if [ 0 -eq ${found} ];then shDebug 1 "[${pChrootDir}:check-pid] chroot remove dead pid [$pid]" >> $LogFile 2>&1 cat "${pidsFile}" | grep -v "\|\|" | grep -v "|${pid}|" > "${pidsFile}.new" mv "${pidsFile}.new" "${pidsFile}" fi fi done if [ 0 -eq ${oneFound} ];then shDebug 1 "[${pChrootDir}:check-pid] unlock chroot no more pid." >> $LogFile 2>&1 chroot_umount "$pChrootDir" >> $LogFile 2>&1 rm $lockChrootFile fi fi ;; register-pid) echo "|${registerPid}|" >> "${pidsFile}" shDebug 1 "[${pChrootDir}:register-pid] register pid [${registerPid}/${filePid}]" >> $LogFile 2>&1 ;; start) if [ -f $lockChrootFile ];then shDebug 1 "[${pChrootDir}:start] service mount chroot, active yet:$lockChrootFile." else touch $lockChrootFile shDebug 1 "[${pChrootDir}:start] service mount chroot, active on:$lockChrootFile." chroot_mount "$pChrootDir" "$pMountFolder" >> $LogFile 2>&1 ${rootDarkContainer}/bin/dirfileright "$pChrootDir" "$pDirUserGroupRight" >> $LogFile 2>&1 fi ;; stop) if [ -f $lockChrootFile ];then found=0 [ -f "${pidsFile}" ] && for pid in $(cat "${pidsFile}" | tr -d '|' );do if [ -n "$pid" ];then for testPid in $( ps -ax -o pid | grep "$pid" );do if [ "|${testPid}|" = "|${pid}|" ];then found=1;break;fi;done fi if [ 1 -eq ${found} ];then break;fi done if [ 0 -eq ${found} ];then shDebug 1 "[${pChrootDir}:stop] last service mount stop, umount chroot service" >> $LogFile 2>&1 chroot_umount "$pChrootDir" >> $LogFile 2>&1 rm $lockChrootFile else shDebug 1 "[${pChrootDir}:stop] chroot service mount have to stay alive for ${pid}." >> $LogFile 2>&1 fi else shDebug 0 "[${pChrootDir}:stop] no more lock file, umount,yet???" >> $LogFile 2>&1 fi ;; force-stop) [ -f "${pidsFile}" ] && for pid in $(cat "${pidsFile}" | tr -d '|' );do if [ -n "$pid" ];then for testPid in $( ps -ax -o pid | grep "$pid" );do if [ "|${testPid}|" = "|${pid}|" ];then shDebug 0 "[${pChrootDir}:force-stop] kill ${pid}" >> $LogFile 2>&1 kill -9 ${pid}; break fi;done fi done chroot_umount "$pChrootDir" >> $LogFile 2>&1 [ -f $lockChrootFile ] && rm $lockChrootFile ;; execute) cmdExecute="$3" if [ -z "$cmdExecute" ];then shDebug 0 "[${pChrootDir}:execute] usage :\$action \$dataFile \$cmdExecute" else if [ -f $lockChrootFile ];then shDebug 1 "[${pChrootDir}:execute] service mount chroot, active yet:$lockChrootFile" else chroot_mount "$pChrootDir" "$pMountFolder" ${rootDarkContainer}/bin/dirfileright "$pChrootDir" "$pDirUserGroupRight" fi chroot_root_execute "$pChrootDir" "$cmdExecute" if [ -f $lockChrootFile ];then shDebug 1 "[${pChrootDir}:execute] service mount chroot, was active yet." else shDebug 1 "[${pChrootDir}:execute] last service mount stop, umount chroot service" chroot_umount "$pChrootDir" fi fi ;; esac fi fi shStartStopScript "$shScriptName" >> $LogFile 2>&1 #exit 0 #return 0