summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/au/authenticodeCheckHook/setup-hook.bash
blob: ea333f89b2cc2f123005fbcedec9d5607415630e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
isPE() {
  local fd
  local magic
  exec {fd}< "$1"
  LANG=C read -r -n 2 -u "$fd" magic
  exec {fd}<&-
  if [[ $magic == MZ ]]; then
    # Let’s just assume this isn’t a DOS executable…
    return 0
  else
    return 1
  fi
}

authenticodeCheckHook() {
  local excludeFlags=()
  for pattern in "${authenticodeCheckExclude[@]}"; do
    excludeFlags+=(
      -a '!' '(' -name "$pattern" -o -wholename "$prefix/$pattern" ')'
    )
  done

  local checked=

  while read -rd '' file; do
    if isPE "$file"; then
      checked=1
      "@pesigcheck@" \
        --no-system-db=0 \
        --certfile="$authenticodeCertificate" \
        --in="$file"
    fi
  done < <(find -L -- "$prefix" -type f "${excludeFlags[@]}" -print0)

  if [[ -z $checked ]]; then
    nixErrorLog 'no PE files found'
    exit 1
  fi
}

fixupOutputHooks+=(authenticodeCheckHook)