#!/bin/sh # ===================================================================== # Projet : DARKCONTAINER # Fichier : lib/systemctl # 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 2>&1; then racineSh="${racineSh:-/etc/darksh}"; . "$racineSh/lib/shTools"; shDebugEnable="1";fi rootDarkContainer="${rootDarkContainer:-/etc/darkcontainer}" #|-------------------------------------------------------------| #| which "base" tools : cp | rm | echo | head | tail | #|-------------------------------------------------------------| # &&&&& #|-----------------------------------------------------------------------| #| 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/systemctl) && $(shInclude $rootDarkContainer/which/lib/userRight) cmdStartStopDaemon="$(which start-stop-daemon 2>/dev/null )" cmdSh="$(which sh)" #systemctl_create $pName systemctl_create() { local log="systemctl_create:" local pName="$1" if [ -z "$cmdSystemctl" ];then initv_create "$pName" else if [ -n "$($cmdSystemctl status ${pName}* | head -n 3 | tail -n 1 | grep running)" ];then shDebug 1 ""$log" service $pName is running, we stop them." $cmdSystemctl stop $pName $cmdSystemctl stop ${pName}@* fi fi } #initv_create $pName initv_create() { local log="initv_create:" local pName="$1" shDebug 1 ""$log" not systemctl system, gone to initV" /etc/init.d/$pName stop } #pServiceDoc="man:dhcpd(8)" #systemctl_install $pName $pServiceInfo $pServiceDoc $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight $ExecStart $ExecStartPre $ExecStop $ExecReload #systemctl_install $pName $pServiceInfo $pServiceDoc $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight "$cmdDhcpd -- -cf /etc/darksrv/dhcp/data/dhcpd.conf -lf /var/run/dhcp/dhcpd.leases" "$cmdTouch $pChrootDir/var/run/dhcp/dhcpd.leases" #systemctl_install $pName $pServiceInfo $pServiceDoc $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight "$cmdNamed -- -f -u bind" "" "$cmdRndc stop > /dev/null 2>&1 || $cmdKill -TERM \$MAINPID" "$cmdRndc reload > /dev/null 2>&1 || $cmdKill -HUP \$MAINPID" #systemctl_install $pName $pServiceInfo $pServiceDoc $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight "$cmdNamed -- -f -u bind -t $pChrootDir" "" "$cmdRndc stop > /dev/null 2>&1 || $cmdKill -TERM \$MAINPID" "$cmdRndc reload > /dev/null 2>&1 || $cmdKill -HUP \$MAINPID" #systemctl_install $pName $pServiceInfo $pServiceDoc $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight "$cmdChroot -- $pDir $cmdNamed -4 -f -u bind" "" "$cmdRndc stop > /dev/null 2>&1 || $cmdKill -TERM \$MAINPID" "$cmdRndc reload > /dev/null 2>&1 || $cmdKill -HUP \$MAINPID" systemctl_install () { local log="systemctl_install" local pName="$1" local pServiceInfo="$2" local pServiceDoc="$3" local pUser="$4" local pGroup="$5" local pChrootDir="$6" local pMountFolders="$7" local pDirUserGroupRight="$8" local ExecStart="$9" local ExecStartPre="${10}" local ExecStop="${11}" local ExecReload="${12}" local PidFile="${13}" local serviceType="${14}" if [ -z "$cmdSystemctl" ];then shDebug 1 ""$log" not systemctl system, gone to initV" initv_install "$pName" "$pServiceInfo" "$pServiceDoc" "$pUser" "$pGroup" "$pChrootDir" "$pMountFolders" "$pDirUserGroupRight" "$ExecStart" "$ExecStartPre" "$ExecStop" "$ExecReload" "$PidFile" else local serviceFile="/etc/systemd/system/${pName}.service" if [ -n "$(echo $ExecStart | grep %i)" ];then local serviceFile="/etc/systemd/system/${pName}@.service";fi systemctl_unit_gen "$pName" "$pServiceInfo" "$pServiceDoc" > $serviceFile systemctl_service_gen "$pName" "$pUser" "$pGroup" "$pChrootDir" "$pMountFolders" "$pDirUserGroupRight" \ "$ExecStart" "$ExecStartPre" "$ExecStop" "$ExecReload" "$PidFile" "$serviceType" >> $serviceFile $cmdEcho -e " \n\ [Install] \n\ WantedBy=multi-user.target \n\ " >> $serviceFile $cmdSystemctl daemon-reload $cmdSystemctl enable ${pName} $cmdSystemctl restart ${pName} $cmdSystemctl status ${pName} fi } initv_install () { local log="initv_install" local pName="$1" local pServiceInfo="$2" local pServiceDoc="$3" local pUser="$4" local pGroup="$5" local pChrootDir="$6" local pMountFolders="$7" local pDirUserGroupRight="$8" local ExecStart="$9" local ExecStartPre="${10}" local ExecStop="${11}" local ExecReload="${12}" local pIdFile="${13}" shDebug 1 ""$log" not systemctl system, gone to initV" initv_unit_gen "$pName" "$pServiceInfo" "$pServiceDoc" > /etc/init.d/${pName} initv_service_gen "$pName" "$pUser" "$pGroup" "$pChrootDir" "$pMountFolders" "$pDirUserGroupRight" \ "$ExecStart" "$ExecStartPre" "$ExecStop" "$ExecReload" "$pIdFile">> /etc/init.d/${pName} chmod u+x /etc/init.d/${pName} update-rc.d ${pName} defaults } #initv_unit_gen $pName $pServiceInfo $pServiceDoc initv_unit_gen() { local pName="$1" local pServiceInfo="$2" local pServiceDoc="$3" $cmdEcho -e "#!/bin/sh -e\n\ \n\ ### BEGIN INIT INFO\n\ # Provides: $pName\n\ # Required-Start: \$remote_fs\n\ # Required-Stop: \$remote_fs\n\ # Should-Start: \$network \$syslog\n\ # Should-Stop: \$network \$syslog\n\ # Default-Start: 2 3 4 5\n\ # Default-Stop: 0 1 6\n\ # Short-Description: Start and stop $pName\n\ # Description: $pServiceInfo\n\ ### END INIT INFO\n\ \n\ if [ -f /lib/lsb/init-functions ];then . /lib/lsb/init-functions;fi\n\ " } #systemctl_unit_gen $pName $pServiceInfo $pServiceDoc systemctl_unit_gen() { local pName="$1" local pServiceInfo="$2" local pServiceDoc="$3" $cmdEcho -e " \n\ [Unit] \n\ Description=$pServiceInfo \n\ Documentation=$pServiceDoc \n\ DefaultDependencies=no \n\ " if [ "$pName" = "dhcpd" ];then $cmdEcho -e " \n\ After=network-online.target \n\ " elif [ "$pName" = "bind9" ];then $cmdEcho -e " \n\ Wants=nss-lookup.target \n\ Before=nss-lookup.target \n\ After=network.target \n\ " fi } #initv_service_gen $pName $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight $ExecStart $ExecStartPre $ExecStop $ExecReload $pIdFile initv_service_gen() { local log="initv_service_gen" local pName="$1" local pUser="$2" local pGroup="$3" local pChrootDir="$4" local pMountFolders="$5" local pDirUserGroupRight="$6" local ExecStart="$7" local ExecStartPre="$8" local ExecStop="$9" local ExecReload="${10}" local pIdFile="${11}" if [ -z "$pIdFile" ];then pIdFile="$pChrootDir/var/run/$pName/$pName.pid";fi local Type="Type=forking" if [ -z "$ExecStop" ] || [ -z "$(echo $ExecStart | grep $pChrootDir)" ];then local ExecStop="$cmdStartStopDaemon --pidfile \${pChrootDir}/\${pIdFile} --stop || echo \"echec stop service\"" fi if [ -n "ExecStart" ] && [ -z "$(echo $ExecStart | grep $pChrootDir)" ];then local ExecStart="$cmdStartStopDaemon --chroot $pChrootDir --pidfile \${pChrootDir}/\${pIdFile} --start --exec $ExecStart" fi if [ -n "$ExecReload" ];then local ExecReload="ExecReload=$ExecReload" fi chroot_gen_conf "$pChrootDir" "$pName" "$pUser" "$pGroup" "$pMountFolders" "$pDirUserGroupRight" local systemdExecStartPre="rootDarkContainer=\"$rootDarkContainer\" sh \"$rootDarkContainer/bin/container\" start \"\$dataFile\"" local systemdExecStopPost="rootDarkContainer=\"$rootDarkContainer\" sh \"$rootDarkContainer/bin/container\" stop \"\$dataFile\"" $cmdEcho -e " \n\ rootDarkContainer=\"$rootDarkContainer\" \n\ pChrootDir=\"$pChrootDir\" \n\ pIdFile=\"$pIdFile\" \n\ pIdFolder=\"\$(echo \$pIdFile | rev | cut -d/ -f2- | rev)\" \n\ dataFile=\"$rootDarkContainer/data/$(echo $pChrootDir | base64)\" \n\ if [ ! -d \"\$pIdFolder\" ];then mkdir -p \"\$pIdFolder\";fi \n\ case \"\$1\" in \n\ start) \n\ log_daemon_msg \"Starting service...\" \"$pName\" \n\ \n\ sh \"$rootDarkContainer/bin/container\" start \"\$dataFile\" \n\ $ExecStart \n\ log_end_msg 0 \n\ ;; \n\ \n\ stop) \n\ log_daemon_msg \"Stopping service...\" \"$pName\" \n\ $ExecStop \n\ sh \"$rootDarkContainer/bin/container\" stop \"\$dataFile\" \n\ log_end_msg 0 \n\ ;; \n\ \n\ reload|force-reload) \n\ log_daemon_msg \"Reloading service...\" \"$pName\" \n\ $ExecReload >/dev/null && log_end_msg 0 || log_end_msg 1 \n\ ;; \n\ \n\ restart) \n\ \$0 stop || log_daemon_msg \"Stopping service echec\" \"$pName\" \n\ \$0 start \n\ ;; \n\ execute) \n\ sh \"$rootDarkContainer/bin/container\" execute \"\$dataFile\" \"\$3\" \n\ ;; \n\ \n\ status) \n\ ret=0 \n\ status_of_proc -p \${pChrootDir}/\${pIdFile} $pName 2>/dev/null || ret=\$? \n\ exit \$ret \n\ ;; \n\ \n\ *) \n\ log_action_msg \"Usage: \$0 {start|stop|reload|restart|force-reload|status}\" \n\ exit 1 \n\ ;; \n\ esac \n\ " } #systemctl_service_gen $pName $pUser $pGroup $pChrootDir $pMountFolders $pDirUserGroupRight $ExecStart $ExecStartPre $ExecStop $ExecReload $pidFile systemctl_service_gen() { local log="systemctl_install" local pName="$1" local pUser="$2" local pGroup="$3" local pChrootDir="$4" local pMountFolders="$5" local pDirUserGroupRight="$6" local ExecStart="$7" local ExecStartPre="$8" local ExecStop="$9" local ExecReload="${10}" local pidFile="${11:-$pChrootDir/var/run/$pName/$pName.pid}" local Type="${12:-forking}" #local startStopParamPidFils="" if [ "$pName" = "bind9" ];then # local pidFile="$pChrootDir/var/run/named/named.pid" local Type="simple" fi if [ -z "$ExecStop" ];then if [ -z "$(echo $ExecStart | grep $pChrootDir)" ];then local ExecStop="ExecStop=$cmdStartStopDaemon --stop --pid \$MAINPID -p $pidFile" else local ExecStop="" fi else local ExecStop="ExecStop=$ExecStop" fi if [ -n "$ExecStartPre" ];then local ExecStartPre="ExecStartPre=$ExecStartPre" fi if [ -n "ExecStart" ];then if [ -z "$(echo $ExecStart | grep $pChrootDir)" ];then local ExecStart="ExecStart=$cmdStartStopDaemon -p $pidFile --chroot $pChrootDir --start --exec $ExecStart" else local ExecStart="ExecStart=$ExecStart" fi fi if [ -n "$ExecReload" ];then local ExecReload="ExecReload=$ExecReload" fi chroot_gen_conf "$pChrootDir" "$pName" "$pUser" "$pGroup" "$pMountFolders" "$pDirUserGroupRight" local systemdExecStartPre="sh \"$rootDarkContainer/bin/container\" start \"$rootDarkContainer/data/$(echo $pChrootDir | base64)\"" local systemdExecStopPost="sh \"$rootDarkContainer/bin/container\" stop \"$rootDarkContainer/data/$(echo $pChrootDir | base64)\"" $cmdEcho -e " \n\ [Service] \n\ Type=$Type \n\ PIDFile=$pChrootDir/$pidFile \n\ ExecStartPre=$cmdSh -c '$systemdExecStartPre' \n\ $ExecStartPre \n\ $ExecStart \n\ $ExecReload \n\ $ExecStop \n\ \n\ ExecStopPost=$cmdSh -c '$systemdExecStopPost' \n\ \n\ PrivateTmp=false \n\ \n\ " } # systemctl_install_debug "$debugFile" "$pName" "$pChrootDir" "$pUser" "$pGroup" "$ExecStart" "$pMountFolders" "$pDirUserGroupRight" systemctl_install_debug() { local log="systemctl_install_debug" local debugFile="$1" local pName="$2" local pChrootDir="$3" local pUser="$4" local pGroup="$5" local ExecStart="$6" local pMountFolders="$7" local pDirUserGroupRight="$8" $cmdEcho -e "#!/bin/sh \n\ racine=\"/etc/darksrv\" \n\ \n\ \n\ . \$racineSh/lib/shTools \n\ \$(shInclude $rootDarkContainer/lib/chroot) \n\ " > "$debugFile" for myParams in bash ls ldd gdb strace ldconfig ldconfig.real;do $cmdEcho -e "cmd=\"\$(which $myParams)\" \n\ if [ -n \"\$cmd\" ];then chroot_install \"$pChrootDir\" \"\$cmd\" ;fi \n\ " >> "$debugFile" done $cmdEcho -e "if [ -n \"$cmdSystemctl\" ];then \n\ $cmdSystemctl stop "$pName" \n\ else \n\ /etc/init.d/$pName stop \n\ fi \n\ " >> "$debugFile" $cmdChmod u+x "$debugFile" #chroot_gen_start "$pChrootDir" "$pUser" "$pGroup" "$pMountFolders" $cmdEcho -e "dataFile=\"$rootDarkContainer/data/$(echo $pChrootDir | base64)\"" >> "$debugFile" $cmdEcho -e "sh \"$rootDarkContainer/bin/container\" start \"\$dataFile\"" >> "$debugFile" $cmdEcho -e "$cmdEcho -e \"ldconfig -f /etc/ld.so.conf -C /etc/ld.so.cache\"" >> "$debugFile" $cmdEcho -e "$cmdEcho -e \"$ExecStart\"" >> "$debugFile" $cmdEcho -e "$cmdChroot $pChrootDir" >> "$debugFile" $cmdEcho -e "sh \"$rootDarkContainer/bin/container\" stop \"\$dataFile\"" >> "$debugFile" }