Debian Codegraph @

src/functions/chk_dir.sh

raw
1 #!/usr/bin/env bash
2 # shellcheck disable=SC2154
3 # these top lines are moved during build
4
5 chk_dir() {
6 if [[ -z "${CHK_DIR}" ]]; then
7 printf "\033[31mError: Please provide a valid directory.\033[m\n\n"
8 exit 1
9 fi
10 # follow symlink
11 if [[ -L "${CHK_DIR}" ]]; then
12 CHK_DIR=$(readlink -f "${CHK_DIR}")
13 fi
14 # remove trailing slashes
15 tempdir=$(echo "${CHK_DIR}" | sed -e "s/\/*$//")
16 if [[ ! -d "${tempdir}" ]]; then
17 printf "\033[31mError: The directory '%s' does not exist.\033[m\n\n" "${tempdir}"
18 exit 1
19 fi
20 if ${extended_checks}; then
21 echo_message "RELRO STACK CANARY NX PIE SELFRANDO Clang CFI SafeStack RPATH RUNPATH Symbols \tFORTIFY\tFortified\tFortifiable Filename\n" '' "<dir name='$tempdir'>\n" "{ \"dir\": { \"name\":\"$tempdir\" }"
22 else
23 echo_message "RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols \tFORTIFY\tFortified\tFortifiable Filename\n" '' "<dir name='$tempdir'>\n" "{ \"dir\": { \"name\":\"$tempdir\" }"
24 fi
25 fdircount=0
26 fdirtotal=0
27
28 while read -r N; do
29 if [[ "${N}" != "[A-Za-z1-0]*" ]]; then
30 out=$(file "$(readlink -f "${N}")")
31 if [[ ${out} =~ ELF ]]; then
32 ((fdirtotal++))
33 fi
34 fi
35 done < <(find "${tempdir}" -type f 2> /dev/null)
36 if [[ $fdirtotal -gt 0 ]]; then
37 echo_message "" "" "" ","
38 fi
39 while read -r N; do
40 if [[ "${N}" != "[A-Za-z1-0]*" ]]; then
41 # read permissions?
42 if [[ ! -r "${N}" ]]; then
43 printf "\033[31mError: No read permissions for '%s' (run as root).\033[m\n" ", ${N}"
44 else
45 # ELF executable?
46 out=$(file "$(readlink -f "${N}")")
47 if [[ ! ${out} =~ ELF ]]; then
48 if [[ "${verbose}" = "true" ]]; then
49 echo_message "\033[34m*** Not an ELF file: ${tempdir}/" "" "" ""
50 file "${N}"
51 echo_message "\033[m" "" "" ""
52 fi
53 else
54 ((fdircount++))
55 echo_message "" "" " " ""
56 filecheck "${N}"
57 if [[ "$(find "${N}" \( -perm -004000 -o -perm -002000 \) -type f -print)" ]]; then
58 echo_message "\033[37;41m${N}\033[m\n" ",${N}\n" " filename='${N}' />\n" ", \"filename\":\"${N}\"}"
59 else
60 echo_message "${N}\n" ",${N}\n" " filename='${N}' />\n" ", \"filename\":\"${N}\"}"
61 fi
62 if [[ "${fdircount}" == "${fdirtotal}" ]]; then
63 echo_message "" "" "" ""
64 else
65 echo_message "" "" "" ","
66 fi
67 fi
68 fi
69 fi
70 done < <(find "${tempdir}" -type f 2> /dev/null)
71 echo_message "" "" "</dir>\n" "}"
72 }