summaryrefslogtreecommitdiff
path: root/src/sanitychecks.cpp
blob: 185b1f9cb2358f895e1494d73d5a32a60b85c2c8 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "env.h"
#include "envmodule.h"
#include <log.h>

using namespace MOBase;

enum class SecurityZone
{
  NoZone = -1,
  MyComputer = 0,
  Intranet = 1,
  Trusted = 2,
  Internet = 3,
  Untrusted = 4,
};

QString toCodeName(SecurityZone z)
{
  switch (z)
  {
    case SecurityZone::NoZone: return "NoZone";
    case SecurityZone::MyComputer: return "MyComputer";
    case SecurityZone::Intranet: return "Intranet";
    case SecurityZone::Trusted: return "Trusted";
    case SecurityZone::Internet: return "Internet";
    case SecurityZone::Untrusted: return "Untrusted";
    default: return "Unknown";
  }
}

QString toString(SecurityZone z)
{
  return QString("%1 (%2)")
    .arg(toCodeName(z))
    .arg(static_cast<int>(z));
}

bool isZoneBlocked(SecurityZone z)
{
  return (z == SecurityZone::Internet || z == SecurityZone::Untrusted);
}

bool isFileBlocked(const QFileInfo& fi)
{
  const QString ads = "Zone.Identifier";
  const auto key = "ZoneTransfer/ZoneId";

  const auto path = fi.absoluteFilePath();
  const auto adsPath = path + ":" + ads;

  QFile f(adsPath);
  if (!f.exists()) {
    return false;
  }

  log::debug("'{}' has an ADS for {}", path, adsPath);

  const QSettings qs(adsPath, QSettings::IniFormat);

  if (!qs.contains(key)) {
    log::debug("'{}': key '{}' not found", adsPath, key);
    return false;
  }

  const auto v = qs.value(key);
  if (v.isNull()) {
    log::debug("'{}': key '{}' is null", adsPath, key);
    return false;
  }

  bool ok = false;
  const auto z = static_cast<SecurityZone>(v.toInt(&ok));

  if (!ok) {
    log::debug("'{}': key '{}' is not an int (value is '{}')", adsPath, key, v);
    return false;
  }

  if (!isZoneBlocked(z)) {
    log::debug("'{}': zone id is {}, which is fine", adsPath, toString(z));
    return false;
  }

  log::warn("'{}': file is blocked (zone id is {})", path, toString(z));
  return true;
}

void checkBlockedFiles(const QDir& dir)
{
  if (!dir.exists()) {
    log::error(
      "while checking for blocked files, directory '{}' not found",
      dir.absolutePath());

    return;
  }

  const auto files = dir.entryInfoList({"*.dll", "*.exe"}, QDir::Files);
  if (files.empty()) {
    log::error(
      "while checking for blocked files, directory '{}' is empty",
      dir.absolutePath());

    return;
  }

  for (auto&& fi : files) {
    isFileBlocked(fi);
  }
}

void checkBlocked()
{
  const QString appDir = QCoreApplication::applicationDirPath();

  const QDir dirs[] = {
    appDir,
    appDir + "/dlls",
    appDir + "/loot",
    appDir + "/NCC",
    appDir + "/platforms",
    appDir + "/plugins"
  };

  for (const auto& d : dirs) {
    checkBlockedFiles(d);
  }
}

void checkMissingFiles()
{
  // files that are likely to be eaten
  static const QStringList files({
    "helper.exe", "nxmhandler.exe",
    "usvfs_proxy_x64.exe", "usvfs_proxy_x86.exe",
    "usvfs_x64.dll", "usvfs_x86.dll"
    });

  const auto dir = QCoreApplication::applicationDirPath();

  for (const auto& name : files) {
    const QFileInfo file(dir + "/" + name);

    if (!file.exists()) {
      log::warn(
        "'{}' seems to be missing, an antivirus may have deleted it",
        file.absoluteFilePath());
    }
  }
}

void checkNahimic(const env::Environment& e)
{
  for (auto&& m : e.loadedModules()) {
    const QFileInfo file(m.path());

    if (file.fileName().compare("NahimicOSD.dll", Qt::CaseInsensitive) == 0) {
      log::warn(
        "NahimicOSD.dll is loaded. Nahimic is known to cause issues with "
        "Mod Organizer, such as freezing or blank windows. Consider "
        "uninstalling it.");

      break;
    }
  }
}

void sanityChecks(const env::Environment& e)
{
  log::debug("running sanity checks");

  checkBlocked();
  checkMissingFiles();
  checkNahimic(e);
}