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

Annotation of /trunk/TSAdminTool/frmConfigServices.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (hide annotations) (download)
Wed Jul 11 14:19:13 2012 UTC (8 years, 11 months ago) by william
File size: 26678 byte(s)
imported from https://anywherets.svn.sourceforge.net/svnroot/anywherets/trunk/
Revision: 1
Author: beartown
Date: 3:10:40 AM, Monday, June 21, 2010
Message:
First upload. Still todo in order to get a fully working product: Replace Thinstation images with vanilla Thinstation, modify ImageDesigntimeConfig.cs, ImageRuntimeConfig.cs in order to support the Thinstation image
1 william 4 using System;
2     using System.Collections.Generic;
3     using System.ComponentModel;
4     using System.Data;
5     using System.Drawing;
6     using System.Text;
7     using System.Windows.Forms;
8     using System.IO;
9     using System.Security.AccessControl;
10     using System.Security.Principal;
11    
12     namespace AnywhereTS
13     {
14     public partial class frmConfigServices : Form
15     {
16     public frmConfigServices()
17     {
18     InitializeComponent();
19     }
20    
21     private atsDataSet.TftpServerDataTable datatableTFTP; // TFTP server directories
22     private atsDataSet.TerminalServerDataTable datatableTerminalServer; // terminal servers
23     const string strATSshareName = @"AnywhereTS$"; // The name of the AnywhereTS Win32 file share for the local TFTP server.
24     const string strShareDesc = @"Used by AnywhereTS";
25     private List<string> tftpUncPaths; // List with the corresponding UNC paths for items in lstTFTP.
26    
27     private void frmConfigureMode_Load(object sender, EventArgs e)
28     {
29     this.Cursor = Cursors.WaitCursor;
30    
31     tftpUncPaths = new List<string>(); // Create a list for the TFTP UNC paths.
32    
33     // Setup database
34     if (ProSupport.tftpServerTableAdapter == null || ProSupport.terminalServerTableAdapter == null) // Is database initiated?
35     {
36     // Databases is not initiated, do it now
37     ProSupport.InitDatabase();
38     }
39    
40     datatableTFTP = new atsDataSet.TftpServerDataTable();
41     ProSupport.tftpServerTableAdapter.Fill(datatableTFTP);
42     datatableTerminalServer = new atsDataSet.TerminalServerDataTable();
43     ProSupport.terminalServerTableAdapter.Fill(datatableTerminalServer);
44    
45     if (ATSGlobals.configured == 0)
46     { // ATS not configured yet
47     RadTSthisComputer.Checked = true;
48     txtTFTPpath.Text = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\AnywhereTS\TFTP";
49     radTFTPinternal.Checked = true;
50     }
51     else
52     { // ATS already configured.
53     txtTFTPpath.Text = ATSGlobals.strTFTPdir;
54     radTFTPinternal.Checked = (ATSGlobals.tftpConfig == 0);
55     radTFTPexternal.Checked = (ATSGlobals.tftpConfig == 1);
56     if (ATSGlobals.terminalServerConfig == 0)
57     { // This computer is terminal server
58    
59     if (datatableTerminalServer.Rows.Count == 0) //Any other terminal servers?
60     { // No other terminal servers
61     RadTSthisComputer.Checked = true;
62     }
63     else
64     { // At least one other
65     radTSboth.Checked = true;
66     }
67     }
68     else
69     { // This computer is not terminal server
70     radTSother.Checked = true;
71     }
72    
73     }
74    
75    
76     // Update terminal server list
77     UpdateTerminalServerList();
78    
79     // Update TFTP server directories list
80     if (ATSGlobals.tftpConfig == 1)
81     {
82     UpdateTftpList();
83     }
84    
85     helpProvider.HelpNamespace = ATSGlobals.strHelpFilePath; // Initiate helpProvider
86    
87     UpdateControls();
88    
89     this.Cursor = Cursors.Default;
90     }
91    
92    
93     private void btnOK_Click(object sender, EventArgs e)
94     {
95     this.Cursor = Cursors.WaitCursor;
96    
97     if (RadTSthisComputer.Checked)
98     ATSGlobals.terminalServerConfig = 0;
99     else if (radTSboth.Checked)
100     ATSGlobals.terminalServerConfig = 0;
101     else if (radTSother.Checked)
102     ATSGlobals.terminalServerConfig = 1;
103     else
104     throw new Exception("Undefined terminal servers config. 42001");
105    
106    
107     string strPath; // The TFTP path
108     strPath = txtTFTPpath.Text.Trim();
109    
110     if (radTFTPinternal.Checked)
111     { // We are using the internal TFTP
112     ATSGlobals.tftpConfig = 0; // Use ATS TFTP
113     // Validate TFTP directory
114     if (strPath.Length == 0)
115     {
116     this.Cursor = Cursors.Default;
117     MessageBox.Show("You must specifiy a directory for the AnywhereTS shared network files.");
118     txtTFTPpath.Focus();
119     return;
120     }
121    
122     if (strPath.StartsWith(".") || strPath.StartsWith(@"\"))
123     {
124     this.Cursor = Cursors.Default;
125     MessageBox.Show("Invalid path for the AnywhereTS shared network files.");
126     txtTFTPpath.Focus();
127     return;
128     }
129    
130     // Create TFTP directory
131     try
132     {
133     Directory.CreateDirectory(strPath);
134     }
135     catch (Exception ex)
136     {
137     this.Cursor = Cursors.Default;
138     MessageBox.Show("Could not create directory '" + strPath + "'. Error: " + ex.Message);
139     txtTFTPpath.Select();
140     return;
141     }
142    
143     // Check that the directory is local
144     ShareUtil shUtil = new ShareUtil(); // Create a util class
145     if (shUtil.IsNetworkPath(strPath))
146     { // The path is a network path
147     MessageBox.Show("A network path cannot be used for the local TFTP server. Please enter a local path.");
148     txtTFTPpath.Select();
149     return;
150     }
151     // Save the TFTP directory to registry
152     ATSGlobals.strTFTPdir = strPath; // Set the C# version of TFTP dir
153     Globals.strTFTPdir = strPath; // Set the VB version of TFTP dir
154     ATSGlobals.SetATSRegValue(ATSGlobals.strRegTFTP_root, ATSGlobals.strTFTPdir);
155    
156     // Delete all TFTP roots from database
157     foreach (DataRow row in datatableTFTP.Rows)
158     {
159     row.Delete();
160     }
161     ProSupport.tftpServerTableAdapter.Update(datatableTFTP);
162    
163     if (!RadTSthisComputer.Checked)
164     { // There are external terminal servers.
165    
166     // Create a share, save the UNC path to the database, add rights to TFTP directory
167     AddTFTPpath(ATSGlobals.strTFTPdir);
168     }
169     else
170     { // We are only using this computer as terminal server
171     SetTFTPrights(ATSGlobals.strTFTPdir);
172     }
173     }
174     else
175     {
176     // Use other TFTP
177     ATSGlobals.tftpConfig = 1; // Use other TFTP
178    
179     if (lstTFTP.Items.Count == 0)
180     {
181     this.Cursor = Cursors.Default;
182     MessageBox.Show("You must choose at least one TFTP server root directory, where AnywhereTS can place client images and configurations.");
183     btnAddTFTPpath.Focus();
184     return;
185     }
186     }
187    
188     // Check if we are using external terminal servers
189     if (RadTSthisComputer.Checked)
190     { // Only this computer is a terminal server. Delete all other terminal servers.
191     foreach (DataRow row in datatableTerminalServer.Rows)
192     {
193     row.Delete();
194     }
195     ProSupport.terminalServerTableAdapter.Update(datatableTerminalServer);
196     }
197     else
198     { // We have external terminal servers
199     if (lstTerminalServer.Items.Count == 0)
200     {
201     this.Cursor = Cursors.Default;
202     MessageBox.Show("You must specify at least one Terminal Server!");
203     btnAddTerminalServer.Focus();
204     return;
205     }
206    
207     // Close database connections
208     datatableTFTP.Dispose();
209     datatableTerminalServer.Dispose();
210     ProSupport.clientTableAdapter.Connection.Close();
211     ProSupport.terminalServerTableAdapter.Connection.Close();
212     ProSupport.clientTableAdapter.Dispose();
213     ProSupport.terminalServerTableAdapter.Dispose();
214    
215     // Configure SQL browser service (needed for the control panel to be able to access the database).
216    
217     bool sqlServerRestarted = DatabaseSupport.EnableNamedPipes(); // Enable named pipes in the SQL server
218     DatabaseSupport.AutostartSQLbrowserService(); // Configure the SQL Browser service to autostart.
219     DatabaseSupport.StartSQLbrowserService(); // Start the SQL Browser service
220    
221     if (sqlServerRestarted)
222     {
223     // SQL server was restarted. Try to reconnect to database in order to repair the database connectivity
224     ProSupport.InitDatabase();
225    
226     try
227     {
228     datatableTFTP = new atsDataSet.TftpServerDataTable();
229     ProSupport.tftpServerTableAdapter.Fill(datatableTFTP); // This operation causes an error and forces the connectivity to recover.
230     }
231     catch
232     { // Do nothing, just catch the error that will most likely occur here.
233     }
234     finally
235     {
236     System.Threading.Thread.Sleep(5000); // Wait for the SQL connection to recover.
237     }
238     }
239     }
240    
241     // Save DHCP options
242     if (radAtsDhcp.Checked)
243     {
244     ATSGlobals.dhcpConfig = 0; // Use the internal DCHP server
245     }
246     else
247     {
248     ATSGlobals.dhcpConfig = 1; // Use the other DCHP server
249     }
250    
251     // Save TFTP configuration to registry
252     ATSGlobals.SetATSRegValue(ATSGlobals.strRegTFTPconfig,ATSGlobals.tftpConfig);
253    
254     // Save DCHP configuration to registry
255     ATSGlobals.SetATSRegValue(ATSGlobals.strRegDHCPconfig, ATSGlobals.dhcpConfig);
256    
257     // Terminate window
258     this.Cursor = Cursors.Default;
259     DialogResult = DialogResult.OK;
260     }
261    
262    
263     private void AddTFTPpath(string strPath)
264     {
265     string uncPath; // The path in UNC format
266     string strLocalPath = ""; // Local path to save in database for display purposes. Only set if path is local
267    
268     ShareUtil shUtil = new ShareUtil(); // Create a util class
269    
270     // Check if path is a network path
271     if (shUtil.IsNetworkPath(strPath))
272     { // The path is a network path
273     if (strPath.StartsWith("\\"))
274     { // The network path is a UNC path, no need to convert it
275     uncPath = strPath;
276     }
277     else
278     { // The network path is not a UNC path, convert it
279     uncPath = UNCconverter.ConvertLocalPathToUnc(strPath);
280     }
281     }
282     else
283     { // User is adding a local path
284     strLocalPath = strPath;
285     string strThisComputer = Environment.MachineName; // The name of this computer
286     // Compile the UNC path
287     uncPath = @"\\" + strThisComputer + @"\" + strATSshareName;
288    
289     // Delete ATS share if it exists
290     shUtil.DeleteShare(strATSshareName);
291    
292     // Delete share from database if it exists
293     ProSupport.tftpServerTableAdapter.Fill(datatableTFTP); // Refresh data table
294     DataRow rowToDelete;
295     rowToDelete = datatableTFTP.Rows.Find(uncPath);
296     if (rowToDelete != null)
297     { // The local TFTP path exists in database, delete it
298     rowToDelete.Delete();
299     ProSupport.tftpServerTableAdapter.Update(datatableTFTP);
300     }
301    
302     // Create network share
303    
304     uint nRetVal;
305     nRetVal = shUtil.CreateShare(strShareDesc, strATSshareName, strPath);
306     if (nRetVal == 0)
307     {
308     // Share created
309     }
310     else
311     {
312     // Error
313     MessageBox.Show("Could not create a share for the AnywereTS TFTP data. Do you have sufficient rights? (25322)");
314     return;
315     }
316    
317     }
318    
319     // Add the remote or local directory path to the database
320     if (uncPath.Length <= 255)
321     { // The path will fit in the database
322     atsDataSet.TftpServerRow row = (atsDataSet.TftpServerRow)datatableTFTP.NewRow();
323     row["Path"] = uncPath;
324     row["LocalPath"] = strLocalPath;
325    
326     try
327     {
328     datatableTFTP.AddTftpServerRow(row);
329     }
330     catch (ConstraintException)
331     {
332     MessageBox.Show("'" + uncPath + "' is already in the list.");
333     return;
334     }
335    
336     ProSupport.tftpServerTableAdapter.Update(datatableTFTP);
337    
338     // Ask if we are going to add righs to the TFTP root directory, set rights.
339     SetTFTPrights(row["Path"].ToString());
340     UpdateTftpList(); // Update the list box
341     lstTFTP.Select();
342     }
343     else
344     { // Path is too long
345     MessageBox.Show("The directory path is too long, please use a path that is max 255 character");
346     }
347    
348     }
349    
350     private void radAtsDhcp_CheckedChanged(object sender, EventArgs e)
351     {
352     UpdateControls(); // Set text "Next >" or "Finish" on the Ok button
353     }
354    
355     private void radExternalDhcp_CheckedChanged(object sender, EventArgs e)
356     {
357     UpdateControls(); // Set text "Next >" or "Finish" on the Ok button
358     }
359    
360     private void UpdateControls()
361     {
362     if (RadTSthisComputer.Checked)
363     { // Only this computer as terminal server
364     lblTerminalServers.Enabled = false;
365     lstTerminalServer.Enabled = false;
366     btnAddTerminalServer.Enabled = false;
367     btnDeleteTerminalServer.Enabled = false;
368     }
369     else
370     { // Other terminal servers
371     lblTerminalServers.Enabled = true;
372     lstTerminalServer.Enabled = true;
373     btnAddTerminalServer.Enabled = true;
374     if (lstTerminalServer.SelectedIndex == -1)
375     btnDeleteTerminalServer.Enabled = false;
376     else
377     btnDeleteTerminalServer.Enabled = true;
378     }
379    
380     if (radTFTPinternal.Checked)
381     {
382     lblTFTPpath.Enabled = true;
383     txtTFTPpath.Enabled = true;
384     btnBrowseTFTP.Enabled = true;
385     lblTFTPdirectories.Enabled = false;
386     lstTFTP.Enabled = false;
387     btnAddTFTPpath.Enabled = false;
388     btnDeleteTFTP.Enabled = false;
389     radAtsDhcp.Enabled = true;
390     lstTFTP.SelectedIndex = -1;
391     btnOK.Text = "Next >";
392     }
393     else
394     {
395     lblTFTPdirectories.Enabled = true;
396     lstTFTP.Enabled = true;
397     btnAddTFTPpath.Enabled = true;
398     if (lstTFTP.SelectedIndex == -1)
399     btnDeleteTFTP.Enabled = false;
400     else
401     btnDeleteTFTP.Enabled = true;
402     lblTFTPpath.Enabled = false;
403     txtTFTPpath.Enabled = false;
404     btnBrowseTFTP.Enabled = false;
405     btnOK.Text = "Finish";
406     radAtsDhcp.Enabled = false;
407     radExternalDhcp.Checked = true;
408     }
409     }
410    
411     private void radTFTPinternal_CheckedChanged(object sender, EventArgs e)
412     {
413     UpdateControls();
414     }
415    
416     private void radTFTPexternal_CheckedChanged(object sender, EventArgs e)
417     {
418     if (radTFTPexternal.Checked && ATSGlobals.tftpConfig == 0)
419     { // We have just de-selected internal TFTP server
420    
421     // Delete all TFTP roots from database
422     foreach (DataRow row in datatableTFTP.Rows)
423     {
424     row.Delete();
425     }
426     ProSupport.tftpServerTableAdapter.Update(datatableTFTP);
427     UpdateTftpList();
428    
429     // Delete the file share for the internal TFTP directory if it exists.
430     ShareUtil shUtil = new ShareUtil(); // Create a util class
431     shUtil.DeleteShare("AnywhereTS$");
432    
433     // ATS is now in a non configured state
434     ATSGlobals.configured = 0;
435     ATSGlobals.SetATSRegValue(ATSGlobals.strRegConfigured, ATSGlobals.configured);
436     }
437     UpdateControls();
438     }
439    
440     // Update the list with TFTP directories from database.
441     private void UpdateTftpList()
442     {
443     lstTFTP.Items.Clear();
444     tftpUncPaths.Clear();
445     ProSupport.tftpServerTableAdapter.Fill(datatableTFTP);
446     foreach (DataRow row in datatableTFTP.Rows)
447     { // Display local path if possible
448    
449    
450     lstTFTP.Items.Add(ProSupport.GetTftpPath(row));
451     //string gg = row["Path"].ToString();
452     //tftpUncPaths.Add(gg);
453     tftpUncPaths.Add(row["Path"].ToString()); // Add the UNC path to the list.
454     }
455     btnDeleteTFTP.Enabled = false;
456     }
457    
458     // Update the list with terminal servers from database.
459     private void UpdateTerminalServerList()
460     {
461     lstTerminalServer.Items.Clear();
462     foreach (DataRow row in datatableTerminalServer.Rows)
463     {
464     lstTerminalServer.Items.Add(row["Path"].ToString());
465     }
466     btnDeleteTerminalServer.Enabled = false;
467     }
468    
469     private void lstTFTP_SelectedIndexChanged(object sender, EventArgs e)
470     {
471     if (lstTFTP.SelectedIndex == -1)
472     { // No item selcted in the TFTP directory list
473     btnDeleteTFTP.Enabled = false;
474     }
475     else
476     { // User has selcted an item in the list
477     btnDeleteTFTP.Enabled = true;
478     }
479     }
480    
481     // Delete the row selected in the TFTP directory list box.
482     private void btnDeleteTFTP_Click(object sender, EventArgs e)
483     {
484     DataRow rowToDelete;
485     rowToDelete = datatableTFTP.Rows.Find(tftpUncPaths[lstTFTP.SelectedIndex]);
486     rowToDelete.Delete();
487     ProSupport.tftpServerTableAdapter.Update(datatableTFTP);
488     UpdateTftpList(); // Update the list box
489    
490     if (datatableTFTP.Rows.Count == 0)
491     { // Last row deleted
492     // ATS is now in a non configured state
493     ATSGlobals.configured = 0;
494     ATSGlobals.SetATSRegValue(ATSGlobals.strRegConfigured, ATSGlobals.configured);
495     }
496     }
497    
498     // Set user rights to TFTP directory, all remote desktop users should be able to write files.
499     void SetTFTPrights(string path)
500     {
501     // Ask if we are going to add righs to the TFTP root directory
502     DialogResult resultRights;
503     resultRights = MessageBox.Show(this, "In order for users to use the AnywhereTS control panel, they need rights to write in the TFTP root directory. Do you want AnywhereTS to add these rights now?", "AnywhereTS", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
504     if (resultRights == DialogResult.Yes)
505     {
506     try
507     {
508     // Create a new DirectoryInfo object.
509     DirectoryInfo dInfo = new DirectoryInfo(path);
510    
511     // Get a DirectorySecurity object that represents the
512     // current security settings.
513     DirectorySecurity dSecurity = dInfo.GetAccessControl();
514    
515     // Add a FileSystemAccessRule to the security settings, to allow write.
516     // No general rights to delete files. These rights are set on the individual files.
517     dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinRemoteDesktopUsersSid, null), FileSystemRights.WriteData, AccessControlType.Allow));
518    
519     // Set the new access settings.
520     dInfo.SetAccessControl(dSecurity);
521    
522     }
523     catch (Exception ex)
524     {
525     this.Cursor = Cursors.Default;
526     MessageBox.Show("Could not set user access rights for TFTP directory. Do you have sufficient right? (29991). Error: " + ex.Message);
527     return;
528     }
529     }
530     }
531    
532     private void btnDeleteTerminalServer_Click(object sender, EventArgs e)
533     {
534     DataRow rowToDelete;
535     rowToDelete = datatableTerminalServer.Rows.Find(lstTerminalServer.SelectedItem);
536     rowToDelete.Delete();
537     ProSupport.terminalServerTableAdapter.Update(datatableTerminalServer);
538     UpdateTerminalServerList(); // Update the list box
539     }
540    
541     private void btnAddTerminalServer_Click(object sender, EventArgs e)
542     {
543     // Show dialog
544     frmAddTerminalServer addTSdialog = new frmAddTerminalServer();
545     DialogResult result = addTSdialog.ShowDialog();
546     if (result == DialogResult.OK)
547     {
548     // Add the server to the database
549     if (addTSdialog.inputstring.Trim().Length <= 255)
550     { // The path will fit in the database
551     atsDataSet.TerminalServerRow row = (atsDataSet.TerminalServerRow)datatableTerminalServer.NewRow();
552     row["Path"] = addTSdialog.inputstring.Trim();
553     try
554     {
555     datatableTerminalServer.AddTerminalServerRow(row);
556     }
557     catch (ConstraintException)
558     {
559     MessageBox.Show("'" + row["Path"] + "' is already in the list.");
560     return;
561     }
562    
563     ProSupport.terminalServerTableAdapter.Update(datatableTerminalServer);
564    
565     UpdateTerminalServerList(); // Update the list box
566     lstTerminalServer.Select();
567     }
568     else
569     { // Path is too long
570     MessageBox.Show("The server path is too long, please use a path that is max 255 character");
571     }
572     }
573     }
574    
575     private void lstTerminalServer_SelectedIndexChanged(object sender, EventArgs e)
576     {
577     if (lstTerminalServer.SelectedIndex == -1)
578     { // No item selcted in the TFTP directory list
579     btnDeleteTerminalServer.Enabled = false;
580     }
581     else
582     { // User has selcted an item in the list
583     btnDeleteTerminalServer.Enabled = true;
584     }
585     }
586    
587     private void RadTSthisComputer_CheckedChanged(object sender, EventArgs e)
588     {
589     UpdateControls();
590     }
591    
592     private void radTSother_CheckedChanged(object sender, EventArgs e)
593     {
594     UpdateControls();
595     }
596    
597     private void radTSboth_CheckedChanged(object sender, EventArgs e)
598     {
599     UpdateControls();
600     }
601    
602     // Add a TFTP server
603     private void btnAddTFTPpath_Click(object sender, EventArgs e)
604     {
605     // Show dialog
606     frmAddTFTP addTFTPdialog = new frmAddTFTP();
607     DialogResult result = addTFTPdialog.ShowDialog();
608     if (result == DialogResult.OK)
609     {
610     string strPath;
611     strPath = addTFTPdialog.inputstring.Trim();
612    
613     if (Directory.Exists(strPath))
614     {
615     // Convert to UNC, share local directory, add directory to database
616     AddTFTPpath(strPath);
617     }
618     else
619     {
620     MessageBox.Show("Error: Cannot find or access the directory. Please check name and rights. (34212)");
621     }
622     UpdateTftpList();
623     }
624     }
625    
626     private void btnBrowseTFTP_Click(object sender, EventArgs e)
627     {
628     // Show the FolderBrowserDialog.
629     Rebrowse:
630     DialogResult result = folderBrowserDialog.ShowDialog();
631     if (result == DialogResult.OK)
632     {
633     string strPath; // The path provided by the user
634    
635     // Browse to a folder by displaying the folder browse Dialog
636     try
637     {
638     strPath = folderBrowserDialog.SelectedPath.Trim();
639     }
640     catch (NotSupportedException)
641     { // User has browsed to a non folder
642     MessageBox.Show("Object is not a file folder.");
643     goto Rebrowse;
644     }
645     txtTFTPpath.Text = strPath;
646     txtTFTPpath.Select();
647     }
648     }
649     }
650     }
651    

  ViewVC Help
Powered by ViewVC 1.1.22