#!/bin/sh # ===================================================================== # Projet : DARKCONTAINER # Fichier : lib/apt # 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-apt-init" shDebugEnable="${shDebugEnable:-1}" shDebugHidden "[ib/chroot-apt] racineSh:${racineSh} rootDarkContainer:${rootDarkContainer}" >&2 fi [ ! -d "$rootDarkContainer" ] && shDebug 0 "exit cause rootDarkContainer is not dir:$rootDarkContainer" >&2 && exit 0 #$( shInclude "${rootDarkContainer}/lib/chroot" ) #|------------------------------| #| 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 ln" 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) cmdStartStopDaemon="${cmdStartStopDaemon:-$(which start-stop-daemon 2>/dev/null)}" # apt_getArch "pkg[:arch]" -> amd64 | all | ... | (vide si inconnu) apt_getArch() { local p="$1" a="" case "$p" in *:*) echo "${p##*:}"; return 0;; esac # 1) installé (on ignore les états "un"/"rc"), première ligne seulement a=$(LC_ALL=C dpkg-query -W -f='${db:Status-Abbrev} ${Architecture}\n' "$p" 2>/dev/null \ | awk '$1 ~ /^.i/ {print $2; exit}') # 2) sinon le candidat apt [ -z "$a" ] && a=$(LC_ALL=C apt-cache show --no-all-versions "$p" 2>/dev/null \ | awk '/^Architecture:/ {print $2; exit}') echo "$a" } # apt_getPkgName "pkg" -> pkg:amd64 | pkg (si all/inconnu) apt_getPkgName() { local p="${1%%_*}" a a=$(apt_getArch "$p") case "$a" in ""|all) echo "${p%%:*}" ;; *) echo "${p%%:*}:$a" ;; esac } # apt_getPkgName "$aptToInstall" #apt_getPkgName() { # local aptToInstall="$1" # local arch=$(dpkg-query -W -f='${Architecture}\n' "$aptToInstall") # if [ -n "${arch}" ];then # local pkgName="${aptToInstall}:${arch}" # else # local pkgName="${aptToInstall}" # fi # echo "$pkgName"; #} # apt_getDebName "pkg" -> pkg_ver_arch (sans .deb), nom exact selon apt apt_getDebName() { local p="${1%%_*}" f v a # Méthode fiable : apt donne lui-même le nom du fichier (gère all, epoch, multiarch) f=$(LC_ALL=C apt-get download --print-uris "$p" 2>/dev/null | awk 'NR==1 {print $2}') if [ -n "$f" ]; then echo "${f%.deb}"; return 0; fi # Repli : on reconstruit le nom v=$(LC_ALL=C apt-cache policy "${p%%:*}" 2>/dev/null \ | awk '/Candidate:/ && $2 != "(none)" {print $2; exit}') a=$(apt_getArch "$p") v=$(printf '%s' "$v" | sed 's/:/%3a/') if [ -n "$v" ] && [ -n "$a" ]; then echo "${p%%:*}_${v}_${a}" elif [ -n "$v" ]; then echo "${p%%:*}_${v}" else echo "${p%%:*}"; return 1 fi } # apt_getPkgName "$aptToInstall" #apt_getDebName() { # local aptToInstall="$1" # #local ver=$(dpkg-query -W -f='${Version}\n' "$aptToInstall" | sed 's/:/%3a/g') # #[ -z "$ver" ] && ver=$(apt-cache policy "$aptToInstall" | awk '/Candidate:/ {print $2}' | sed 's/:/%3a/g') # local ver=$(apt-cache policy "$( echo $aptToInstall | cut -d '_' -f1 )" | grep "Candidat" | grep -v "\(none\)" | grep -v "\(aucun\)" | cut -d: -f2- |tr -d ' ' ) ## local ver=$(apt-cache policy "$( echo $aptToInstall | cut -d '_' -f1 )" | grep "Candidat" | grep -v "\(none\)" | grep -v "\(aucun\)" | cut -d: -f2- |tr -d ' ' | sed 's/:/%3a/g' ) # local arch=$(dpkg-query -W -f='${Architecture}\n' "$aptToInstall") # # if [ -n "${arch}" ] && [ -n "${ver}" ];then # local debName="${aptToInstall}_${ver}_${arch}" # elif [ -n "${ver}" ];then # local debName="${aptToInstall}_${ver}" # else # local debName="${aptToInstall}" # fi # echo "$debName"; #} #apt_download $pChrootDir $apt apt_download() { local aptToInstall="$*" local debFileName="" debFileName="$( apt_getDebName ${aptToInstall} )" [ ! -f "/var/cache/apt/archives/${debFileName}.deb" ] && [ ! -f "/var/cache/apt/archives/$(echo ${debFileName} | sed 's/:/%3a/g').deb" ] && \ shDebug 2 "apt-get --allow-unauthenticated download "${aptToInstall}"" && apt-get --allow-unauthenticated download "${aptToInstall}" # chown -R _apt "/var/cache/apt/archives" } # apt_getDepsInOne "$aptToInstall" apt_getDepsInOne() { local log="apt_getDepsInOne" local aptToInstall="$1" local aptFound="" for pkgLongInstalled in $( echo $aptDepsListGetDepsInOne | tr 'µ' ' ' | tr '\n' ' ');do [ "$( echo $pkgLongInstalled | tr 'µ' ' ' )" = "${aptToInstall}" ] && aptFound="1" && break; done [ -n "$aptFound" ] && return 0 # shDebugHidden 1 "${log} aptToInstall:${aptToInstall}" >&2 aptDepsListGetDepsInOne="$aptDepsListGetDepsInOne $aptToInstall" for dep in $( apt-cache depends --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances "$aptToInstall" \ | grep -E '^\s*(Dépend:|Depends:|Pré-Dépend:|Pre-Depends:)' | cut -d: -f2 | tr -d ' ' | tr -d '<' | tr -d '>' \ ); do aptDepsListGetDepsInOne="$aptDepsListGetDepsInOne" apt_getDepsInOne "$dep" aptDepsListGetDepsInOne="$aptDepsListGetDepsInOne $dep" # . apt_getDepsInOne "$dep" done [ -n "$aptToInstall" ] && echo "$aptToInstall" } # apt_getalldepsInOne "$aptToInstall" apt_getalldepsInOne() { local log="apt_getalldepsInOne" local aptToInstall="$1" local aptFound="" # shDebugHidden 1 "${log} aptToInstall:${aptToInstall}" >&2 for pkgLongInstalled in $( echo $aptDepsListGetalldepsInOne | tr 'µ' ' ' | tr '\n' ' ');do [ "$( echo $pkgLongInstalled | tr 'µ' ' ' )" = "${aptToInstall}" ] && aptFound="1" && break; #&& shDebugHidden 0 "founded:${aptToInstall}" >&2 && break; done # shDebugHidden 1 "${log} aptFound:${aptFound}" >&2 [ -n "$aptFound" ] && return 0 aptDepsListGetalldepsInOne="$aptDepsListGetalldepsInOne $aptToInstall" for dep in $( apt-cache depends --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances "$aptToInstall" \ | grep -E '^\s*(Dépend:|Depends:|Pré-Dépend:|Pre-Depends:)' | cut -d: -f2 | tr -d ' ' | tr -d '<' | tr -d '>' \ ); do aptDepsListGetalldepsInOne="$aptDepsListGetalldepsInOne" apt_getalldepsInOne "$dep" aptDepsListGetalldepsInOne="$aptDepsListGetalldepsInOne $dep" # . apt_getalldepsInOne "$dep" done apt_getDebName "$aptToInstall" } # apt_getmissingdepsInOne "$aptToInstall" apt_getmissingdepsInOne() { local log="apt_getmissingdepsInOne:" local aptToInstall="$1" local aptFound="" for pkgLongInstalled in $( echo $aptDepsListGetalldepsInOne | tr 'µ' ' ' | tr '\n' ' ');do [ "$( echo $pkgLongInstalled | tr 'µ' ' ' )" = "${aptToInstall}" ] && aptFound="1" && break; done [ -n "$aptFound" ] && return 0 aptDepsListGetalldepsInOne="$aptDepsListGetalldepsInOne $aptToInstall" for dep in $( apt-cache depends --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances "$aptToInstall" \ | grep -E '^\s*(Dépend:|Depends:|Pré-Dépend:|Pre-Depends:)' | cut -d: -f2 | tr -d ' ' | tr -d '<' | tr -d '>' \ ); do # shDebugHidden "$log DEP $dep" 2>&1 aptDepsListGetalldepsInOne="$aptDepsListGetalldepsInOne" apt_getmissingdepsInOne "$dep" aptDepsListGetalldepsInOne="$aptDepsListGetalldepsInOne $dep" done # shDebugHidden "$log check if '${aptToInstall}' is in $aptInstalledList" 2>&1 [ -z "$aptFound" ] && [ -f "$aptInstalledList" ] && for pkgLongInstalled in $( cat $aptInstalledList | tr 'µ' ' ' | tr '\n' ' ');do [ "$( echo $pkgLongInstalled | tr 'µ' ' ' )" = "${aptToInstall}" ] && aptFound="1" && break; done [ -z "$aptFound" ] && apt_getDebName "$aptToInstall" }