about summary refs log tree commit diff
path: root/pkgs/santander/default.nix
blob: 802638975f4b5972ea3318a56958e4d0f91ee9a3 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{ stdenv, fetchurl, fetchgit, fetchFromBitbucket
, runCommand, writeScript, writeScriptBin, writeText
, xvfb_run, xdotool, coreutils, wineMinimal, pipelight, dwb, pcsclite
}:

let
  name = "SecurityPluginHBCIChipcard";
  version = "2.9.8.0";
  dllName = "NP_${name}.dll";

  pluginInstaller = fetchurl {
    url = "https://service.santanderbank.de/special/banking/files/"
        + "SecurityPluginHBCIChipcard-${version}-Installer.exe";
    sha256 = "0xnfb730mwxdx83dnqyplp4bxwx6g01wc87xa4dl1spxia9kjmmh";
  };

  patchedWine = let
    libpcsclite = "${pcsclite}/lib/libpcsclite.so";
  in (wineMinimal.override {
    wineBuild = "wine32";
    wineRelease = "staging";
  }).overrideDerivation (drv: {
    scard4wine = fetchgit {
      url = "git://git.code.sf.net/p/scard4wine/code";
      rev = "c14c02c80bf1f2bb4cedd1f53a3a2ab9c48bed76";
      sha256 = "0ffmbl9mdnaih4h3ggpnzqbih3kgbwl3wv6j1ag5s4czn8gcpdq3";
    };

    prePatch = (drv.prePatch or "") + ''
      cp -t dlls/winscard "$scard4wine/src/"*
      sed -i -re 's,"libpcsclite\.so(\.[0-9]+)*","${libpcsclite}",' \
        dlls/winscard/winscard.c
    '';

    patches = (drv.patches or []) ++ [ ./winscard.patch ];

    postPatch = (drv.postPatch or "") + ''
      sed -i -e '/not owned by you/d' libs/wine/config.c
      # Modified patch from https://bugs.winehq.org/show_bug.cgi?id=22450
      patch -p1 < "${./wine-no-unixfs.patch}"
    '';
  });

  installPath = [ "Program Files" "ppi" "SecurityPluginHBCIChipcard" ];

  scard4wine = stdenv.mkDerivation rec {
    name = "scard4wine-${version}";
    version = "1.2.0-2016-06-05";

    src = fetchgit {
      url = "git://git.code.sf.net/p/scard4wine/code";
      rev = "c14c02c80bf1f2bb4cedd1f53a3a2ab9c48bed76";
      sha256 = "0ffmbl9mdnaih4h3ggpnzqbih3kgbwl3wv6j1ag5s4czn8gcpdq3";
    };
  };

  winePrefix = runCommand "santander-wineprefix" {
    installPath = stdenv.lib.concatStringsSep "/" (installPath ++ [ dllName ]);
  } ''
    export WINEPREFIX="$out"
    export WINEDLLOVERRIDES="mscoree,mshtml="
    mkdir -p "$out"
    ${patchedWine}/bin/wine wineboot.exe
    ${xvfb_run}/bin/xvfb-run "${writeScript "install-santander-wine" ''
      ${patchedWine}/bin/wine "${pluginInstaller}" &
      while [ "$(jobs -r | wc -l)" -gt 0 ]; do
        ${xdotool}/bin/xdotool \
          search --sync --onlyvisible \
          --name 'Security-Plugin-HBCI-Chipcard ${version}' \
          key Return &> /dev/null || :
        sleep 1
      done
      wait
    ''}"
    if [ ! -e "$out/drive_c/$installPath" ]; then
      echo "Unable to find plugin in $installPath." >&2
      exit 1
    fi
    ln -sf -T "${builtins.storeDir}" "$WINEPREFIX/dosdevices/z:"
    echo disable > "$WINEPREFIX/.update-timestamp"
  '';

  pluginConfig = {
    winePath = "$share/wine";
    inherit winePrefix dllName;
    wineArch = "win32";
    pluginLoaderPath = "$share/pluginloader.exe";
    dllPath = "c:\\${stdenv.lib.concatStringsSep "\\" installPath}";
  };

  pipelightConfigFile = let
    mkVal = val: if val == true then "true"
            else if val == false then "false"
            else toString val;
    mkCfgLine = key: val: "# ${key} = ${mkVal val}";
  in with stdenv.lib; writeText "pipelight-santander.config" ''
    # ---BEGIN CONFIG---
    ${concatStringsSep "\n" (mapAttrsToList mkCfgLine pluginConfig)}
    # ---END CONFIG---
  '';

  finalPlugin = runCommand "santander-plugin" {
    pipelight = (pipelight.override {
      wineStaging = patchedWine;
    }).overrideDerivation (drv: {
      src = fetchFromBitbucket {
        repo = "pipelight";
        owner = "mmueller2012";
        rev = "181bab804f80b99cb46f63f9ed36e4fdf12ca319";
        sha256 = "0ydivpxayzs5aklf0x5vl5bl4issz10k7zl3cv76649kxxhxkh1z";
      };

      patches = [ ./pipelight.patch ];

      postPatch = (drv.postPatch or "") + ''
        sed -i -e '/static \+bool \+openConfig.*{$/,/}/ {
          /getConfigNameFromLibrary/a \
            configFile.open("${pipelightConfigFile}"); \
            if (configFile.is_open()) return true;
        }' src/linux/libpipelight/configloader.c
      '';

      # We don't want or have share/pipelight/install-dependency!
      preFixup = null;
    });
  } ''
    install -vD "$pipelight/lib/pipelight/libpipelight.so" \
      "$out/lib/pipelight/libpipelight-santander.so"
  '';

  dwbWithPlugin = stdenv.lib.overrideDerivation dwb (wrapperDrv: {
    plugins = [ "${finalPlugin}/lib/pipelight" ];
  });

in writeScriptBin "santander" ''
  #!${stdenv.shell}
  if tmpdir="$("${coreutils}/bin/mktemp" -d)"; then
    trap "rm -rf '$tmpdir'" EXIT
    export XDG_RUNTIME_DIR="$tmpdir"
    export XDG_CONFIG_HOME="$tmpdir"
    export XDG_DATA_HOME="$tmpdir"
    export XDG_CACHE_HOME="$tmpdir"
    "${dwbWithPlugin}/bin/dwb" -t https://karte.santanderbank.de/
    exit $?
  else
    echo "Unable to create temporary profile directory." >&2
    exit 1
  fi
''