/[AnywhereTS-MSSQL]/trunk/TSAdminTool/Globals.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 99 - (show annotations) (download)
Sat Jul 14 04:50:27 2012 UTC (8 years, 6 months ago) by william
File size: 46968 byte(s)
+ bulk commit

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Reflection; // For path (assembly)
5 using System.IO; // For Path
6 using System.Security; // Security Exception
7 using System.Windows.Forms; // Message Box
8 using System.Text.RegularExpressions; // For validate MAC address
9 using log4net;
10
11 namespace AnywhereTS
12 {
13 public static class ATSGlobals
14 {
15 #region Global Definitions
16 // Version
17 public const string strDatabaseVersion = "1.0.0.4"; // The version string for the ATS database
18 public const string strDatabaseName = "AnywhereTS"; // THe name of the ATS database
19 //[SerializableAttribute] public struct Color;
20
21 // Registry keys
22 public static string strATSregRoot; // Registry root for AnywhereTS
23 public static string strTFTPD32RegRoot; // Registry root for TFTPD32
24 public const string strRegDHCPconfig = "DHCP"; // DCHP config in registry
25 public const string strRegTFTPconfig = "TFTP"; // TFTP root directory in registry
26 public const string strRegTFTP_root = "TFTP_root"; // TFTP root directory in registry
27 public const string strRegManagedMode = "ManagedMode"; // Reg key for managed mode.
28 public const string strRegConfigured = "Configured"; // Reg key for indicating of ATS is configured.
29 public const string strRegTerminalServer = "TerminalServer"; // Reg key for terinal server config.
30 public const string strRegRunFirstTime = "RunFirstTime"; // Reg key for first time run.
31 public const string strRegAdminVersion = "AdminVersion"; // Reg key for Admin version. Indicates that Admin app is intalled on this computer and the version of the admin app.
32
33 // Help
34 public static string helpFile; // The ATS help file
35
36 // Directories
37 static public string strTFTPdir; // The TFP root directory, including final "\"
38 static public string strExecPath; // The directory for the application exe-file
39 static public string strHelpFilePath; // The path (incl filname) to the application help file
40
41
42 // Other global variables
43 static public int managedMode; // 0=Not definied, 1=Managed, 2=Unmanged
44 static public int configured; // 0=ATS not configured, 1=ATS is configured, 2=Configured for control panel only.
45 static public int dhcpConfig; // 0=Use internal DHCP, 1=Other DHCP
46 static public int tftpConfig; // 0=Use internal TFTP, 1=Other TFTP
47 static public int terminalServerConfig; // 0=Terminal server on this computer, 1=No terminal server on this computer.
48 static public int runFirstTime; // 1=The application (or new version of it) is newly installed and the first time run procedures should be run. 0=The application has already been run.
49
50 static public bool isPro=true; // True = we are running the Pro version. Always true for the open source version.
51 static public bool isEval=true; //True if evalversion of client Non pro feature
52 static public string SelectedGraphicsAdaptersFile; // Name of the file for graphics adapters that corresponds to the users selection of linux kernel
53 static public string SelectedNicAdaptersFile; // Name of the file for network adapters that corresponds to the users selection of linux kernel
54 static public string SelectedSoundAdaptersFile; // Name of the file for sound adapters that corresponds to the users selection of linux kernel
55 static public string ApplicationName; // The name of the application. Used everywhere except for in about box and main window bar. Can be either original or an OEM name.
56 static public string ApplicationTitle = ApplicationName; // The title of the application, used for the main window bar and about box.
57
58 //File names
59 public const string GraphicsAdaptersA = "Graphics30.ats"; //The file containing the list of all graphics adapters
60 public const string NicAdaptersA = "Nic30.ats"; // The file containing the list of all network adapters
61 public const string SoundAdaptersA = "Sound30.ats"; // The file containing the list of all sound adapters
62
63 // Client connect time options
64 public static int[,] ScreenResolutions = new int[,] { { 640, 480 }, { 800, 600 }, { 1024, 600 }, { 1024, 768 }, { 1152, 864 }, { 1280, 768 }, { 1280, 800 }, { 1280, 960 }, { 1280, 1024 }, { 1368, 768 }, { 1400, 1050 }, { 1440, 900 }, { 1440, 1050 }, { 1600, 1000 }, { 1600, 1024 }, { 1600, 1200 }, { 1680, 1050 }, { 1792, 1344 }, { 1856, 1392 }, { 1920, 1080 }, { 1920, 1200 }, { 1920, 1440 } };
65 #endregion
66 #region static ATSGlobals()
67 // Constructor
68 static ATSGlobals()
69 {
70 Logging.Initialize();
71 Logging.ATSAdminLog.Debug("Entering ATSGlobals()");
72 ApplicationName = "Anywhere" + "TS"; // The name of the application. Used everywhere except for in about box and main window bar. Can be either original or an OEM name.
73
74 // Set registry root
75 // This is how we find the OS on run time
76 // on 32 bit OS IntPtr.Size = 4
77 // on 64 bit OS IntPtr.Size = 8
78 if (IntPtr.Size == 8)
79 { // 64 bit OS
80 strATSregRoot = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
81 }
82 else
83 { // 32 bit OS
84 strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
85 }
86
87
88 // Set registry root for TFTPD32
89 // This is how we find the OS on run time
90 // on 32 bit OS IntPtr.Size = 4
91 // on 64 bit OS IntPtr.Size = 8
92 if (IntPtr.Size == 8)
93 { // 64 bit OS
94 strTFTPD32RegRoot = @"SOFTWARE\Wow6432Node\TFTPD32";
95 }
96 else
97 { // 32 bit OS
98 strTFTPD32RegRoot = @"SOFTWARE\TFTPD32";
99 }
100
101
102 helpFile = "AnywhereTS.chm"; // The ATS help file
103
104 if (GetATSRegValueInt(strRegRunFirstTime) == 1)
105 { // We are runnning this version for the first time, write the needed registry values
106 CreateRegistryValues();
107 }
108
109 strExecPath = Path.GetDirectoryName(Application.ExecutablePath);
110
111 strHelpFilePath = strExecPath + @"\" + helpFile;
112
113 // Get TFTP config
114 tftpConfig = GetATSRegValueInt(strRegTFTPconfig);
115
116 // Get TFTP root
117 strTFTPdir = GetATSRegValueString(strRegTFTP_root);
118
119 // Get DHCP config
120 dhcpConfig = GetATSRegValueInt(strRegDHCPconfig);
121
122 configured = GetATSRegValueInt(strRegConfigured);
123
124 managedMode = GetATSRegValueInt(strRegManagedMode);
125
126 runFirstTime = GetATSRegValueInt(strRegRunFirstTime);
127
128 // Currently only the 3.0 version of the drivers are supported
129 SelectedGraphicsAdaptersFile = GraphicsAdaptersA;
130 SelectedNicAdaptersFile = NicAdaptersA;
131 SelectedSoundAdaptersFile = SoundAdaptersA;
132 Logging.ATSAdminLog.Debug("Leaving ATSGlobals()");
133
134 }
135 #endregion
136
137 #region public static string GetATSRegValueString(string regValue)
138 // Get a registry string
139 public static string GetATSRegValueString(string regValue)
140 {
141 return GetRegValue(strATSregRoot, regValue);
142 }
143 #endregion
144
145 #region public static int GetATSRegValueInt(string regValue)
146 // Get a registry integer from the application key
147 public static int GetATSRegValueInt(string regValue)
148 {
149 return GetRegValueInt(strATSregRoot, regValue);
150 }
151 #endregion
152
153 #region public static void SetATSRegValue(string regValue, int value)
154 // Set a registry integer in the the application key
155 public static void SetATSRegValue(string regValue, int value)
156 {
157 SetRegValue(strATSregRoot, regValue, value);
158 }
159 #endregion
160
161 #region public static void SetATSRegValue(string regValue, string value)
162 // Set a registry string in the the application key
163 public static void SetATSRegValue(string regValue, string value)
164 {
165 SetRegValue(strATSregRoot, regValue, value);
166 }
167 #endregion
168
169 #region public static string GetRegValue(string regKey, string regValue)
170 // Get a registry string
171 public static string GetRegValue(string regKey, string regValue)
172 {
173 Microsoft.Win32.RegistryKey objRegkey;
174
175 try
176 {
177 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
178 }
179 catch (ArgumentNullException ex)
180 {
181 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
182 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
183 {
184 using (log4net.NDC.Push("Installation info missing."))
185 {
186 Logging.ATSAdminLog.Error("Error when reading from registry.");
187 }
188 }
189 Application.Exit();
190 return "";
191 }
192 catch (SecurityException ex)
193 {
194 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: "+ regKey + " Value: " + regValue + " Error details:" + ex.Message);
195 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
196 {
197 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
198 {
199 using (log4net.NDC.Push("Invalid users rights."))
200 {
201 Logging.ATSAdminLog.Error("Error when reading from registry.");
202 }
203 }
204 }
205 Application.Exit();
206 return "";
207 }
208 catch (Exception ex)
209 {
210 MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message);
211 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
212 {
213 Logging.ATSAdminLog.Error("Error when reading from registry.");
214 }
215 Application.Exit();
216 return "";
217 }
218
219 if (objRegkey != null)
220 {
221 if (objRegkey.GetValue(regValue) != null)
222 {
223 try
224 {
225 return objRegkey.GetValue(regValue).ToString();
226 }
227 catch (Exception e)
228 {
229 MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
230 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
231 {
232 Logging.ATSAdminLog.Error("Error when reading from registry.");
233 }
234 Application.Exit();
235 return "";
236 }
237 }
238 else
239 {
240 MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
241 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
242 {
243 using (log4net.NDC.Push("Value is missing"))
244 {
245 Logging.ATSAdminLog.Error("Error when reading from registry.");
246 }
247 }
248 Application.Exit();
249 return "";
250 }
251 }
252 else
253 {
254 MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
255 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
256 {
257 using (log4net.NDC.Push("Key is missing"))
258 {
259 Logging.ATSAdminLog.Error("Error when reading from registry.");
260 }
261 }
262 Application.Exit();
263 }
264 return ""; // No value could be retrieved from registry
265 }
266 #endregion
267
268 #region public static int GetRegValueInt(string regKey, string regValue)
269 // Get a registry integer
270 public static int GetRegValueInt(string regKey, string regValue)
271 {
272 Microsoft.Win32.RegistryKey objRegkey;
273
274 try
275 {
276 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
277 }
278 catch (ArgumentNullException ex)
279 {
280 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
281 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
282 {
283 using (log4net.NDC.Push("Installation info missing."))
284 {
285 Logging.ATSAdminLog.Error("Error when reading from registry.");
286 }
287 }
288 Application.Exit();
289 return 0;
290 }
291 catch (SecurityException e)
292 {
293 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
294 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
295 {
296 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
297 {
298 using (log4net.NDC.Push("Invalid user rights."))
299 {
300 Logging.ATSAdminLog.Error("Error when reading from registry.");
301 }
302 }
303 }
304 Application.Exit();
305 return 0;
306 }
307 catch (Exception e)
308 {
309 MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
310 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
311 {
312 Logging.ATSAdminLog.Error("Error when reading from registry.");
313 }
314 Application.Exit();
315 return 0;
316 }
317
318 if (objRegkey != null)
319 {
320 if (objRegkey.GetValue(regValue) != null)
321 {
322 try
323 {
324 return (int)objRegkey.GetValue(regValue);
325 }
326 catch (Exception e)
327 {
328 MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
329 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
330 {
331 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
332 {
333 Logging.ATSAdminLog.Error("Error when reading from registry.");
334 }
335 }
336 Application.Exit();
337 return 0;
338 }
339 }
340 else
341 {
342 MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue);
343
344 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
345 {
346 using (log4net.NDC.Push("Value is missing"))
347 {
348 Logging.ATSAdminLog.Error("Error when reading from registry.");
349 }
350 }
351
352 Application.Exit();
353 return 0;
354 }
355 }
356 else
357 {
358 MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + regValue);
359
360 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
361 {
362 using (log4net.NDC.Push("Key is missing"))
363 {
364 Logging.ATSAdminLog.Error("Error when reading from registry.");
365 }
366 }
367
368 Application.Exit();
369 }
370 return 0; // No value could be retrieved from registry
371 }
372 #endregion
373
374 #region public static void SetRegValue(string regKey, string regValue, int value)
375 // Set a registry integer
376 public static void SetRegValue(string regKey, string regValue, int value)
377 {
378 Microsoft.Win32.RegistryKey objRegkey;
379
380 try
381 {
382 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey,true);
383 }
384 catch (ArgumentNullException e)
385 {
386 MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
387 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
388 {
389 using (log4net.NDC.Push("Installation info missing"))
390 {
391 Logging.ATSAdminLog.Error("Error when writing to registry.");
392 }
393 }
394 Application.Exit();
395 return;
396 }
397 catch (SecurityException e)
398 {
399 MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
400 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
401 {
402 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
403 {
404 using (log4net.NDC.Push("Invalid user rights"))
405 {
406 Logging.ATSAdminLog.Error("Error when writing to registry.");
407 }
408 }
409 }
410 Application.Exit();
411 return;
412 }
413 catch (Exception e)
414 {
415 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
416 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
417 {
418 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
419 {
420 Logging.ATSAdminLog.Error("Error when reading from registry.");
421 }
422 }
423 Application.Exit();
424 return;
425 }
426
427 if (objRegkey != null)
428 {
429 try
430 {
431 objRegkey.SetValue(regValue, value);
432 }
433 catch (Exception e)
434 {
435 MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
436 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
437 {
438 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
439 {
440 Logging.ATSAdminLog.Error("Error when writing to registry.");
441 }
442 }
443 Application.Exit();
444 }
445 }
446 else
447 {
448 MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
449
450 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
451 {
452 using (log4net.NDC.Push("Key is missing"))
453 {
454 Logging.ATSAdminLog.Error("Error when writing to registry.");
455 }
456 }
457
458 Application.Exit();
459 }
460 }
461 #endregion
462
463 #region public static void SetRegValue(string regKey, string regValue, string value)
464 // Set a registry string
465 public static void SetRegValue(string regKey, string regValue, string value)
466 {
467 Microsoft.Win32.RegistryKey objRegkey;
468
469 try
470 {
471 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true);
472 }
473 catch (ArgumentNullException e)
474 {
475 MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
476 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
477 {
478 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
479 {
480 Logging.ATSAdminLog.Error("Error when writing to registry.");
481 }
482 }
483 Application.Exit();
484 return;
485 }
486 catch (SecurityException e)
487 {
488 MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
489 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
490 {
491 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
492 {
493 using (log4net.NDC.Push("Invalid user rights"))
494 {
495 Logging.ATSAdminLog.Error("Error when writing to registry.");
496 }
497 }
498 }
499 Application.Exit();
500 return;
501 }
502 catch (Exception e)
503 {
504 MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
505 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
506 {
507 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
508 {
509 Logging.ATSAdminLog.Error("Error when reading from registry.");
510 }
511 }
512 Application.Exit();
513 return;
514 }
515
516 if (objRegkey != null)
517 {
518 try
519 {
520 objRegkey.SetValue(regValue, value);
521 }
522 catch (Exception e)
523 {
524 MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
525 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
526 {
527 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
528 {
529 Logging.ATSAdminLog.Error("Error when writing to registry.");
530 }
531 }
532 Application.Exit();
533 }
534 }
535 else
536 {
537 MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
538 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
539 {
540 using (log4net.NDC.Push("Key is missing"))
541 {
542 Logging.ATSAdminLog.Error("Error when writing to registry.");
543 }
544 }
545
546 Application.Exit();
547 }
548 }
549 #endregion
550
551 #region public static bool RegValueExists(string regKey, string regValue)
552 // Check if a registry int value exists
553 public static bool RegValueExists(string regKey, string regValue)
554 {
555 Microsoft.Win32.RegistryKey objRegkey;
556
557 try
558 {
559 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
560 }
561 catch (ArgumentNullException e)
562 {
563 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (29411)");
564 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
565 {
566 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
567 {
568 using (log4net.NDC.Push("Installation info missing"))
569 {
570 Logging.ATSAdminLog.Error("Error when reading from registry.");
571 }
572 }
573 }
574 Application.Exit();
575 return false;
576 }
577 catch (SecurityException e)
578 {
579 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (29412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
580 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
581 {
582 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
583 {
584 using (log4net.NDC.Push("Invalid user rights"))
585 {
586 Logging.ATSAdminLog.Error("Error when reading from registry.");
587 }
588 }
589 }
590 Application.Exit();
591 return false;
592 }
593 catch (Exception e)
594 {
595 MessageBox.Show("Error when reading from registry (29413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
596 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
597 {
598 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
599 {
600 Logging.ATSAdminLog.Error("Error when reading from registry.");
601 }
602 }
603 Application.Exit();
604 return false;
605 }
606
607 if (objRegkey != null)
608 {
609 if (objRegkey.GetValue(regValue) != null)
610 {
611 return true; // We could retrive the value
612 }
613 else
614 {
615 return false; // No value found
616 }
617 }
618 else
619 {
620 MessageBox.Show("Error when reading from registry (59221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
621
622 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
623 {
624 using (log4net.NDC.Push("Key is missing"))
625 {
626 Logging.ATSAdminLog.Error("Error when reading from registry.");
627 }
628 }
629
630 Application.Exit();
631 }
632 return false; // No value could be retrieved from registry
633 }
634 #endregion
635
636 #region public static string IsValidMAC(string macAddress)
637 // Validate MAC a MAC address.
638 public static string IsValidMAC(string macAddress)
639 {
640 macAddress = macAddress.Replace(":", "");
641 macAddress = macAddress.ToUpper();
642 string result = "";
643 Regex rx = new Regex("([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
644 Match m = rx.Match(macAddress);
645 result = m.Groups[0].Value;
646 if (result.Length == 17)
647 {
648 return result;
649 }
650 else
651 {
652 rx = new Regex("([0-9a-fA-F][0-9a-fA-F]){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase);
653 Match m2 = rx.Match(macAddress);
654 result = m2.Groups[0].Value;
655 if (result.Length == 12)
656 {
657 return result;
658 }
659 return result;
660 }
661 }
662 #endregion
663
664 #region public static string GetMacFromATSname(string name)
665 // Try to extract a mac address from an ATS client name in the format 'ATSxxxxxxxxxxxx', where xxxxxxxxxx is a mac address.
666 public static string GetMacFromATSname(string name)
667 {
668 string strMac;
669 if (name.Length != 15)
670 {
671 return ""; // Not an ATS name
672 }
673 if (!name.StartsWith("ATS"))
674 {
675 return ""; // Not an ATS name
676 }
677 strMac = ATSGlobals.IsValidMAC(name.Substring(3));
678 if (strMac == "")
679 {
680 return ""; // Not an ATS name
681 }
682 // We have a MAC address
683 return strMac;
684 }
685 #endregion
686
687 #region public static void CreateRegistryValues()
688 // Create all needed registry values
689 public static void CreateRegistryValues()
690 {
691 // Create registry values for ATS Admin Application
692 CreateRegistryValue(strATSregRoot, "CheckUpdate", 1);
693 CreateRegistryValue(strATSregRoot, strRegConfigured, 0);
694 CreateRegistryValue(strATSregRoot, ProSupport.strRegAnywhereTSServer, "");
695 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseDir, "");
696 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, "");
697 CreateRegistryValue(strATSregRoot, ProSupport.strRegDestDir, "");
698 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, "");
699 CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseInstance, "");
700
701
702 CreateRegistryValue(strATSregRoot, strRegDHCPconfig , 0);
703 CreateRegistryValue(strATSregRoot, strRegManagedMode, 1);
704 CreateRegistryValue(strATSregRoot, strRegTerminalServer, 0);
705 CreateRegistryValue(strATSregRoot, strRegTFTPconfig, 0);
706 CreateRegistryValue(strATSregRoot, strRegTFTP_root,"");
707
708 // Check if the admin app is intalled on this computer and if so, add reg values for TFTP 32
709 if (RegValueExists(strATSregRoot, strRegAdminVersion))
710 { // The admin app is installed, define keys for TFTP32. These should not be installed for the control panel.
711 // Create registry values for TFTPD32
712 CreateRegistryValue(strTFTPD32RegRoot, "BaseDirectory", "");
713 CreateRegistryValue(strTFTPD32RegRoot, "Beep", 0);
714 CreateRegistryValue(strTFTPD32RegRoot, "DirText", 0);
715 CreateRegistryValue(strTFTPD32RegRoot, "Hide", 0);
716 CreateRegistryValue(strTFTPD32RegRoot, "LastWindowPos", "60 49 860 642 ");
717 CreateRegistryValue(strTFTPD32RegRoot, "LocalIP", "");
718 CreateRegistryValue(strTFTPD32RegRoot, "MaxRetransmit", 6);
719 CreateRegistryValue(strTFTPD32RegRoot, "Negociate", 1);
720 CreateRegistryValue(strTFTPD32RegRoot, "PXECompatibility", 0);
721 CreateRegistryValue(strTFTPD32RegRoot, "SaveSyslogFile", "");
722 CreateRegistryValue(strTFTPD32RegRoot, "SecurityLevel", 1);
723 CreateRegistryValue(strTFTPD32RegRoot, "Services", 2);
724 CreateRegistryValue(strTFTPD32RegRoot, "ShowProgressBar", 0);
725 CreateRegistryValue(strTFTPD32RegRoot, "TftpLogFile", "");
726 CreateRegistryValue(strTFTPD32RegRoot, "TftpPort", 69);
727 CreateRegistryValue(strTFTPD32RegRoot, "Timeout", 3);
728 CreateRegistryValue(strTFTPD32RegRoot, "UnixStrings", 1);
729 CreateRegistryValue(strTFTPD32RegRoot, "VirtualRoot", 1);
730 CreateRegistryValue(strTFTPD32RegRoot, "WinSize", 0);
731
732 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber1", 0);
733 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber2", 0);
734 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue1", "");
735 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue2", "");
736 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "BootFile", "client.zpxe");
737 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DNS", 16834314);
738 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DomainName", "");
739 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Gateway", 16834314);
740 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "IP_Pool", 335601418);
741 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Mask", 65535);
742 CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "PoolSize", 200);
743 }
744 }
745 #endregion
746
747 #region public static void CreateRegistryValue(string regKey, string regValue, int value)
748 // If a registry key does not exist, create it with the given default value
749 public static void CreateRegistryValue(string regKey, string regValue, int value)
750 {
751 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}",regKey, regValue, value)))
752 {
753 Logging.ATSAdminLog.InfoFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
754 Microsoft.Win32.RegistryKey objRegkey;
755
756 try
757 {
758 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
759 }
760 catch (ArgumentNullException e)
761 {
762 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
763 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
764 {
765 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
766 {
767 using (log4net.NDC.Push("Installation info missing"))
768 {
769 Logging.ATSAdminLog.Error("Failed to create value in registry");
770 }
771 }
772 }
773 Application.Exit();
774 return;
775 }
776 catch (SecurityException e)
777 {
778 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
779 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
780 {
781 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
782 {
783 using (log4net.NDC.Push("Invalid user rights"))
784 {
785 Logging.ATSAdminLog.Error("Failed to create value in registry");
786 }
787 }
788 }
789 Application.Exit();
790 return;
791 }
792 catch (Exception e)
793 {
794 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
795 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
796 {
797 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
798 {
799 Logging.ATSAdminLog.Error("Failed to create value in registry");
800
801 }
802 }
803 Application.Exit();
804 return;
805 }
806
807 if (objRegkey != null)
808 {
809 if (objRegkey.GetValue(regValue) == null)
810 { // No reg value, create one
811 SetRegValue(regKey, regValue, value);
812 }
813 }
814 else
815 {
816 MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
817
818 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
819 {
820 Logging.ATSAdminLog.Error("Failed to create value in registry");
821 }
822
823 Application.Exit();
824 }
825 Logging.ATSAdminLog.InfoFormat(@"Created value in Registry: {0}\{1}={2}", regKey, regValue, value);
826 }
827 }
828 #endregion
829
830 #region public static void CreateRegistryValue(string regKey, string regValue, string value)
831 // If a registry key does not exist, create it with the given default value
832 public static void CreateRegistryValue(string regKey, string regValue, string value)
833 {
834 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
835 {
836 Logging.ATSAdminLog.InfoFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
837 Microsoft.Win32.RegistryKey objRegkey;
838
839 try
840 {
841 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
842 }
843 catch (ArgumentNullException e)
844 {
845 MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
846 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
847 {
848 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
849 {
850 using (log4net.NDC.Push("Installation info missing"))
851 {
852 Logging.ATSAdminLog.Error("Failed to create value in registry");
853 }
854 }
855 }
856 Application.Exit();
857 return;
858 }
859 catch (SecurityException e)
860 {
861 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
862 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
863 {
864 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
865 {
866 using (log4net.NDC.Push("Invalid users rights"))
867 {
868 Logging.ATSAdminLog.Error("Failed to create value in registry");
869 }
870 }
871 }
872 Application.Exit();
873 return;
874 }
875 catch (Exception e)
876 {
877 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
878 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
879 {
880 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
881 {
882 Logging.ATSAdminLog.Error("Failed to create value in registry");
883 }
884 }
885 Application.Exit();
886 return;
887 }
888
889 if (objRegkey != null)
890 {
891 if (objRegkey.GetValue(regValue) == null)
892 { // No reg value, create one
893 SetRegValue(regKey, regValue, value);
894 }
895 }
896 else
897 {
898 MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
899
900 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
901 {
902 Logging.ATSAdminLog.Error("Failed to create value in registry");
903 }
904
905 Application.Exit();
906 }
907 Logging.ATSAdminLog.InfoFormat(@"Created value in Registry: {0}\{1}={2}", regKey, regValue, value);
908 }
909 }
910 #endregion
911
912 #region public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
913 // Extract two integers from a string, separated by a character
914 // Result: True = extraction ok, false = invalid string.
915 // twointegers: String with two integers, separated by a character
916 // separator: the character separating the strings
917 // value1: (out) The first integer in the string
918 // value2: (out) The second integer in the string
919 public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
920 {
921 string[] parts;
922 parsed_integers = new int[0];
923 parts = integers.Split(separator);
924
925 List<int> t_ints = new List<int>();
926
927 foreach (string t in parts)
928 {
929 t_ints.Add(Convert.ToInt32(t));
930 }
931 parsed_integers = t_ints.ToArray();
932 return true;
933 }
934 #endregion
935 } // Class ATSGlobals
936 } // Namespace Anywhere TS

  ViewVC Help
Powered by ViewVC 1.1.22