Skip to content

Commit 1d24b2d

Browse files
authored
Merge pull request #239 from CMor1184/tia_openness_whitelisting
Tia openness whitelisting
2 parents a6baa79 + 945e68c commit 1d24b2d

File tree

2 files changed

+152
-3
lines changed

2 files changed

+152
-3
lines changed

TiaGitHandler/Program.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
2020
using TiaGitHandler.Properties;
2121
using Application = System.Windows.Application;
22+
using InvalidOperationException = System.InvalidOperationException;
2223
using MessageBox = System.Windows.Forms.MessageBox;
2324
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
2425

@@ -96,6 +97,23 @@ static void Main(string[] args)
9697
Console.WriteLine("Bitte S7 projekt als Parameter angeben!");
9798
return;
9899
}
100+
var version = file.Substring(file.Length - 2, 2) + ".0";
101+
try
102+
{
103+
TiaOpennessWhitelist.EnsureWhitelistEntry(version);
104+
}
105+
catch (InvalidOperationException ex)
106+
{
107+
Console.WriteLine($"Cannot set TIA whitelist registry entry: {ex.Message}");
108+
}
109+
catch (SecurityException ex)
110+
{
111+
Console.WriteLine($"Security exception cannot set TIA whitelist registry entry: {ex.Message}");
112+
}
113+
catch (UnauthorizedAccessException ex)
114+
{
115+
Console.WriteLine($"Unauthorized access exception cannot set TIA whitelist registry entry: {ex.Message}");
116+
}
99117

100118
if (Path.GetExtension(file) == ".ap15_1" || Path.GetExtension(file) == ".ap16")
101119
{
@@ -120,16 +138,33 @@ static void Main(string[] args)
120138
}
121139

122140
exportPath = Path.GetDirectoryName(file);
123-
exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\Export"));
141+
exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\out\\Export"));
124142

125143
}
126144
else if (res != null)
127145
{
128146
var ver = ask.Result as string;
129-
prj = Projects.AttachProject(ver);
147+
148+
try
149+
{
150+
TiaOpennessWhitelist.EnsureWhitelistEntry(ver + ".0");
151+
}
152+
catch (InvalidOperationException ex)
153+
{
154+
Console.WriteLine($"Cannot set TIA whitelist registry entry: {ex.Message}");
155+
}
156+
catch (SecurityException ex)
157+
{
158+
Console.WriteLine($"Authorization context to low cannot set TIA whitelist registry entry: {ex.Message}");
159+
}
160+
catch (UnauthorizedAccessException ex)
161+
{
162+
Console.WriteLine($"Unauthorized access exception cannot set TIA whitelist registry entry: {ex.Message}");
163+
}
130164

165+
prj = Projects.AttachProject(ver);
131166
exportPath = Path.GetDirectoryName(prj.ProjectFile);
132-
exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\Export"));
167+
exportPath = Path.GetFullPath(Path.Combine(exportPath, "..\\out\\Export"));
133168
}
134169
else
135170
{
@@ -165,6 +200,9 @@ static void Main(string[] args)
165200
user = args[3];
166201
if (args.Length > 4)
167202
password = args[4];
203+
if (args.Length > 5)
204+
exportPath = args[5];
205+
168206
}
169207

170208
if (prj == null)
@@ -179,6 +217,26 @@ static void Main(string[] args)
179217
}
180218
}
181219

220+
var version = file.Substring(file.Length - 2, 2) + ".0";
221+
222+
try
223+
{
224+
TiaOpennessWhitelist.EnsureWhitelistEntry(version);
225+
}
226+
catch (InvalidOperationException ex)
227+
{
228+
Console.WriteLine($"Cannot set TIA whitelist registry entry: {ex.Message}");
229+
}
230+
catch (SecurityException ex)
231+
{
232+
Console.WriteLine($"Security exception cannot set TIA whitelist registry entry: {ex.Message}");
233+
}
234+
catch (UnauthorizedAccessException ex)
235+
{
236+
Console.WriteLine($"Unauthorized access exception cannot set TIA whitelist registry entry: {ex.Message}");
237+
}
238+
239+
182240
if (attach)
183241
{
184242
if (file.EndsWith("20"))
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Reflection;
6+
using System.Security.Cryptography;
7+
8+
namespace TiaGitHandler
9+
{
10+
public static class TiaOpennessWhitelist
11+
{
12+
/// <summary>
13+
/// Retrieves the path to the currently executing .exe
14+
/// </summary>
15+
private static string GetApplicationPath()
16+
{
17+
// use the executing assembly’s location
18+
var path = Assembly.GetExecutingAssembly().Location;
19+
20+
if (!string.IsNullOrEmpty(path))
21+
return path;
22+
23+
// use the main module’s file name of the current process
24+
using (var proc = Process.GetCurrentProcess())
25+
{
26+
path = proc.MainModule?.FileName;
27+
if (!string.IsNullOrEmpty(path))
28+
return path;
29+
}
30+
31+
throw new InvalidOperationException("Could not determine the application path.");
32+
}
33+
34+
/// <summary>
35+
/// Ensures that a whitelist entry exists and is up-to-date.
36+
/// Creates or updates the entry only if Path, DateModified, or FileHash differ.
37+
/// </summary>
38+
/// <param name="tiaVersion">TIA Openness version, e.g. "V16.0".</param>
39+
/// <returns>True if the entry was created or updated; false if it was already current.</returns>
40+
public static bool EnsureWhitelistEntry(string tiaVersion)
41+
{
42+
// 1. determine the application path
43+
var applicationPath = GetApplicationPath();
44+
var exeName = Path.GetFileName(applicationPath);
45+
46+
// 2. Compute the desired DateModified value
47+
var fileInfo = new FileInfo(applicationPath);
48+
var desiredDate = fileInfo.LastWriteTimeUtc
49+
.ToString("yyyy/MM/dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
50+
51+
// 3. Compute the desired FileHash value (SHA-256, Base64)
52+
byte[] hashBytes;
53+
using (var sha = SHA256.Create())
54+
using (var stream = File.OpenRead(applicationPath))
55+
hashBytes = sha.ComputeHash(stream);
56+
var desiredHash = Convert.ToBase64String(hashBytes);
57+
58+
// 4. Construct the registry subkey path
59+
var subKey = $@"SOFTWARE\Siemens\Automation\Openness\{tiaVersion}\Whitelist\{exeName}\Entry";
60+
61+
// 5. Open or create the registry key
62+
using (var entryKey = Registry.LocalMachine.OpenSubKey(subKey, writable: true)
63+
?? Registry.LocalMachine.CreateSubKey(subKey, writable: true))
64+
{
65+
if (entryKey == null)
66+
throw new InvalidOperationException($"Could not create or open registry key: HKLM\\{subKey}");
67+
68+
// 6. Read existing values
69+
var currentPath = entryKey.GetValue("Path") as string;
70+
var currentDate = entryKey.GetValue("DateModified") as string;
71+
var currentHash = entryKey.GetValue("FileHash") as string;
72+
73+
// 7. Check if any value differs
74+
var needsUpdate =
75+
currentPath != applicationPath ||
76+
currentDate != desiredDate ||
77+
currentHash != desiredHash;
78+
79+
if (!needsUpdate)
80+
return false; // already up-to-date
81+
82+
// 8. Write the new values
83+
entryKey.SetValue("Path", applicationPath, RegistryValueKind.String);
84+
entryKey.SetValue("DateModified", desiredDate, RegistryValueKind.String);
85+
entryKey.SetValue("FileHash", desiredHash, RegistryValueKind.String);
86+
87+
return true;
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)