| 9 |
Kevin |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.Diagnostics;
|
|
|
6 |
using System.IO;
|
|
|
7 |
using System.Windows.Forms;
|
|
|
8 |
using System.Security.AccessControl;
|
| 53 |
Kevin |
9 |
using System.DirectoryServices.AccountManagement;
|
| 9 |
Kevin |
10 |
|
|
|
11 |
namespace SWAT_Office_App
|
|
|
12 |
{
|
|
|
13 |
class ManageUserAccounts
|
|
|
14 |
{
|
| 53 |
Kevin |
15 |
private static string studentGroupName = "StudentAccounts";
|
|
|
16 |
public static List<UserPrincipal> UserAccountsList = new List<UserPrincipal>();
|
| 9 |
Kevin |
17 |
public static bool AddUser(string username, string password)
|
|
|
18 |
{
|
|
|
19 |
try
|
|
|
20 |
{
|
| 53 |
Kevin |
21 |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
|
|
|
22 |
// Creates a new user after checking if it already exists or not
|
|
|
23 |
UserPrincipal newUser = new UserPrincipal(localSystem);
|
|
|
24 |
newUser.Name = username;
|
|
|
25 |
PrincipalSearcher searcher = new PrincipalSearcher(newUser);
|
|
|
26 |
Principal result = searcher.FindOne();
|
|
|
27 |
if (result == null)
|
| 9 |
Kevin |
28 |
{
|
| 53 |
Kevin |
29 |
newUser = new UserPrincipal(localSystem);
|
|
|
30 |
newUser.Name = username;
|
|
|
31 |
newUser.SetPassword(password);
|
|
|
32 |
newUser.PasswordNeverExpires = true;
|
|
|
33 |
newUser.PasswordNotRequired = false;
|
|
|
34 |
newUser.UserCannotChangePassword = true;
|
|
|
35 |
newUser.Description = DateTime.Now.ToShortDateString();
|
|
|
36 |
newUser.Save();
|
|
|
37 |
|
|
|
38 |
// Creates a new group after checking if it already exists or not
|
|
|
39 |
GroupPrincipal studentGroup = new GroupPrincipal(localSystem);
|
|
|
40 |
studentGroup.Name = studentGroupName;
|
|
|
41 |
searcher = new PrincipalSearcher(studentGroup);
|
|
|
42 |
result = searcher.FindOne();
|
|
|
43 |
// Creates group if it doesnt already exist
|
|
|
44 |
if (result == null)
|
| 9 |
Kevin |
45 |
{
|
| 53 |
Kevin |
46 |
studentGroup = new GroupPrincipal(localSystem, studentGroupName);
|
|
|
47 |
studentGroup.Name = studentGroupName;
|
|
|
48 |
studentGroup.IsSecurityGroup = true;
|
|
|
49 |
studentGroup.Members.Add(newUser);
|
|
|
50 |
studentGroup.Save();
|
| 9 |
Kevin |
51 |
}
|
| 53 |
Kevin |
52 |
// Otherwise add to existing group
|
|
|
53 |
else
|
|
|
54 |
{
|
|
|
55 |
studentGroup = (GroupPrincipal)result;
|
|
|
56 |
studentGroup.Members.Add(newUser);
|
|
|
57 |
studentGroup.Save();
|
|
|
58 |
}
|
|
|
59 |
DebugText.appendText("Account " + username + " has been created");
|
|
|
60 |
return true;
|
| 9 |
Kevin |
61 |
}
|
| 53 |
Kevin |
62 |
else
|
|
|
63 |
{
|
|
|
64 |
// local account already exists, return with error
|
|
|
65 |
return false;
|
|
|
66 |
}
|
| 9 |
Kevin |
67 |
}
|
|
|
68 |
catch (Exception e)
|
|
|
69 |
{
|
| 53 |
Kevin |
70 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
71 |
DebugText.appendText(e.ToString());
|
|
|
72 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
| 9 |
Kevin |
73 |
return false;
|
|
|
74 |
}
|
|
|
75 |
}
|
| 53 |
Kevin |
76 |
public static bool DeleteUser(List<string> usernames)
|
| 9 |
Kevin |
77 |
{
|
|
|
78 |
try
|
|
|
79 |
{
|
| 53 |
Kevin |
80 |
bool Success = true;
|
|
|
81 |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
|
|
|
82 |
// Iterates through and deletes selected users
|
| 9 |
Kevin |
83 |
foreach (string user in usernames)
|
|
|
84 |
{
|
| 53 |
Kevin |
85 |
bool deleteShare = false;
|
| 9 |
Kevin |
86 |
if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + user))
|
|
|
87 |
{
|
|
|
88 |
// Prompts for deletion of folder as well as the user account
|
|
|
89 |
DialogResult result = MessageBox.Show("A shared folder exists for the user " + user + "!" +
|
|
|
90 |
"\nDelete the folder and all data within the folder?",
|
|
|
91 |
"Warning!", MessageBoxButtons.YesNo);
|
|
|
92 |
if (result == DialogResult.Yes)
|
| 53 |
Kevin |
93 |
deleteShare = true;
|
| 9 |
Kevin |
94 |
}
|
| 53 |
Kevin |
95 |
// Finds and deletes user and share
|
|
|
96 |
UserPrincipal toDelete = new UserPrincipal(localSystem);
|
|
|
97 |
toDelete.Name = user;
|
|
|
98 |
PrincipalSearcher searcher = new PrincipalSearcher(toDelete);
|
|
|
99 |
Principal found = searcher.FindOne();
|
|
|
100 |
toDelete = (UserPrincipal)found;
|
|
|
101 |
toDelete.Delete();
|
|
|
102 |
DebugText.appendText("Account " + user + " has been deleted");
|
|
|
103 |
// Deletes share if selected
|
|
|
104 |
if (deleteShare)
|
|
|
105 |
if (!DeleteShareFolder(user))
|
|
|
106 |
Success = false;
|
| 9 |
Kevin |
107 |
}
|
| 53 |
Kevin |
108 |
return Success;
|
| 9 |
Kevin |
109 |
}
|
|
|
110 |
catch (Exception e)
|
|
|
111 |
{
|
| 53 |
Kevin |
112 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
113 |
DebugText.appendText(e.ToString());
|
|
|
114 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
115 |
return false;
|
| 9 |
Kevin |
116 |
}
|
|
|
117 |
}
|
| 53 |
Kevin |
118 |
public static bool QueryUserAccounts()
|
| 9 |
Kevin |
119 |
{
|
|
|
120 |
// Function that reads the user accounts on the local computer to UserAccountsList
|
|
|
121 |
UserAccountsList.Clear();
|
|
|
122 |
try
|
|
|
123 |
{
|
| 53 |
Kevin |
124 |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
|
|
|
125 |
UserPrincipal user = new UserPrincipal(localSystem);
|
|
|
126 |
user.Name = "*";
|
|
|
127 |
PrincipalSearcher searcher = new PrincipalSearcher(user);
|
|
|
128 |
PrincipalSearchResult<Principal> result = searcher.FindAll();
|
|
|
129 |
foreach (Principal p in result)
|
|
|
130 |
UserAccountsList.Add((UserPrincipal)p);
|
|
|
131 |
return true;
|
| 9 |
Kevin |
132 |
}
|
|
|
133 |
catch (Exception e)
|
|
|
134 |
{
|
| 53 |
Kevin |
135 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
136 |
DebugText.appendText(e.ToString());
|
|
|
137 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
138 |
return false;
|
| 9 |
Kevin |
139 |
}
|
|
|
140 |
}
|
|
|
141 |
public static bool QueryUserSharedFolderExist(string username)
|
|
|
142 |
{
|
|
|
143 |
return Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username);
|
|
|
144 |
}
|
| 53 |
Kevin |
145 |
public static bool CreateShareFolder(string username)
|
| 9 |
Kevin |
146 |
{
|
|
|
147 |
|
|
|
148 |
try
|
|
|
149 |
{
|
|
|
150 |
// Creates the directory with only the specific NTFS and share permissions for the user.
|
|
|
151 |
DirectorySecurity dSecurity = new DirectorySecurity();
|
| 53 |
Kevin |
152 |
// Adds NTFS permissions for system accounts
|
| 9 |
Kevin |
153 |
foreach (string systemUser in Settings_Form.systemAccounts)
|
|
|
154 |
{
|
|
|
155 |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
|
|
|
156 |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
|
|
|
157 |
}
|
| 53 |
Kevin |
158 |
// Adds NTFS permissions for the user
|
| 9 |
Kevin |
159 |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
|
|
|
160 |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
|
|
|
161 |
Directory.CreateDirectory(Settings_Form.sharedFolderLocation + @"\" + username, dSecurity);
|
| 53 |
Kevin |
162 |
DebugText.appendText("Shared folder for " + username + " has been created");
|
|
|
163 |
return SetSharePermissions(username);
|
| 9 |
Kevin |
164 |
}
|
|
|
165 |
catch (Exception e)
|
|
|
166 |
{
|
| 53 |
Kevin |
167 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
168 |
DebugText.appendText(e.ToString());
|
|
|
169 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
170 |
return false;
|
| 9 |
Kevin |
171 |
}
|
|
|
172 |
}
|
| 53 |
Kevin |
173 |
public static bool DeleteShareFolder(string username)
|
| 9 |
Kevin |
174 |
{
|
|
|
175 |
try
|
|
|
176 |
{
|
| 53 |
Kevin |
177 |
bool Success = true;
|
| 9 |
Kevin |
178 |
// Removes the share BEFORE deleting the folder. Otherwise share will error on remove.
|
| 53 |
Kevin |
179 |
if (!RemoveSharePermissions(username))
|
|
|
180 |
Success = false;
|
| 9 |
Kevin |
181 |
Directory.Delete(Settings_Form.sharedFolderLocation + @"\" + username, true);
|
| 53 |
Kevin |
182 |
DebugText.appendText("Shared folder for " + username + " has been deleted");
|
|
|
183 |
return Success;
|
| 9 |
Kevin |
184 |
}
|
|
|
185 |
catch (Exception e)
|
|
|
186 |
{
|
| 53 |
Kevin |
187 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
188 |
DebugText.appendText(e.ToString());
|
|
|
189 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
190 |
return false;
|
| 9 |
Kevin |
191 |
}
|
|
|
192 |
}
|
| 53 |
Kevin |
193 |
public static bool ToggleShare(string username)
|
| 9 |
Kevin |
194 |
{
|
|
|
195 |
try
|
|
|
196 |
{
|
| 53 |
Kevin |
197 |
// Checks if a share already exists for the user
|
| 9 |
Kevin |
198 |
if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username))
|
|
|
199 |
{
|
|
|
200 |
DialogResult result = MessageBox.Show("A shared folder exists for the user " + username + "!" +
|
|
|
201 |
"\nDelete the folder and all data within the folder?",
|
|
|
202 |
"Warning!", MessageBoxButtons.YesNo);
|
|
|
203 |
if (result == DialogResult.Yes)
|
| 53 |
Kevin |
204 |
return DeleteShareFolder(username);
|
| 9 |
Kevin |
205 |
}
|
| 53 |
Kevin |
206 |
// Otherwise creates the share for the user
|
| 9 |
Kevin |
207 |
else
|
| 53 |
Kevin |
208 |
return CreateShareFolder(username);
|
|
|
209 |
return true;
|
| 9 |
Kevin |
210 |
}
|
|
|
211 |
catch (Exception e)
|
|
|
212 |
{
|
| 53 |
Kevin |
213 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
214 |
DebugText.appendText(e.ToString());
|
|
|
215 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
216 |
return false;
|
| 9 |
Kevin |
217 |
}
|
|
|
218 |
}
|
| 48 |
Kevin |
219 |
public static long GetShareSize(string username)
|
|
|
220 |
{
|
|
|
221 |
DirectoryInfo dir = new DirectoryInfo(Settings_Form.sharedFolderLocation + @"\" + username);
|
|
|
222 |
long size = GetDirSize(dir);
|
|
|
223 |
return size;
|
|
|
224 |
}
|
| 53 |
Kevin |
225 |
private static long GetDirSize(DirectoryInfo input)
|
| 9 |
Kevin |
226 |
{
|
|
|
227 |
try
|
|
|
228 |
{
|
| 53 |
Kevin |
229 |
if (input.Exists)
|
|
|
230 |
{
|
|
|
231 |
long size = 0;
|
|
|
232 |
FileInfo[] files = input.GetFiles();
|
|
|
233 |
foreach (FileInfo file in files)
|
|
|
234 |
{
|
|
|
235 |
size += file.Length;
|
|
|
236 |
}
|
|
|
237 |
DirectoryInfo[] dirs = input.GetDirectories();
|
|
|
238 |
foreach (DirectoryInfo dir in dirs)
|
|
|
239 |
{
|
|
|
240 |
size += GetDirSize(dir);
|
|
|
241 |
}
|
|
|
242 |
return size;
|
|
|
243 |
}
|
|
|
244 |
else
|
|
|
245 |
return 0;
|
| 9 |
Kevin |
246 |
}
|
|
|
247 |
catch (Exception e)
|
|
|
248 |
{
|
| 53 |
Kevin |
249 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
250 |
DebugText.appendText(e.ToString());
|
|
|
251 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
252 |
return 0;
|
| 9 |
Kevin |
253 |
}
|
|
|
254 |
}
|
| 53 |
Kevin |
255 |
public static bool ChangeUserPassword(string username, string password)
|
|
|
256 |
{
|
|
|
257 |
try
|
|
|
258 |
{
|
|
|
259 |
// Queries for the specified user password
|
|
|
260 |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
|
|
|
261 |
UserPrincipal user = new UserPrincipal(localSystem);
|
|
|
262 |
user.Name = username;
|
|
|
263 |
PrincipalSearcher searcher = new PrincipalSearcher(user);
|
|
|
264 |
Principal result = searcher.FindOne();
|
|
|
265 |
user = (UserPrincipal)result;
|
|
|
266 |
user.SetPassword(password);
|
|
|
267 |
user.Save();
|
|
|
268 |
DebugText.appendText("Password for " + username + " has been changed");
|
|
|
269 |
return true;
|
|
|
270 |
}
|
|
|
271 |
catch (Exception e)
|
|
|
272 |
{
|
|
|
273 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
274 |
DebugText.appendText(e.ToString());
|
|
|
275 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
276 |
return false;
|
|
|
277 |
}
|
|
|
278 |
}
|
| 9 |
Kevin |
279 |
public static bool SessionsOpen()
|
|
|
280 |
{
|
|
|
281 |
try
|
|
|
282 |
{
|
|
|
283 |
Process netProcess = new Process();
|
|
|
284 |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
|
|
|
285 |
netProcess.StartInfo.FileName = "net.exe";
|
|
|
286 |
netProcess.StartInfo.UseShellExecute = false;
|
|
|
287 |
netProcess.StartInfo.RedirectStandardOutput = true;
|
|
|
288 |
netProcess.StartInfo.CreateNoWindow = true;
|
|
|
289 |
netProcess.StartInfo.Arguments = "SESSION";
|
|
|
290 |
netProcess.Start();
|
|
|
291 |
string netOutput = netProcess.StandardOutput.ReadToEnd();
|
|
|
292 |
// Splits the output into seperate strings for further processing
|
|
|
293 |
string[] tempSplit = netOutput.Split(new string[] { " ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
294 |
netProcess.WaitForExit();
|
|
|
295 |
netProcess.Close();
|
|
|
296 |
|
|
|
297 |
if (tempSplit[0] == "There are no entries in the list.")
|
| 53 |
Kevin |
298 |
return false;
|
| 50 |
Kevin |
299 |
else
|
| 53 |
Kevin |
300 |
return true;
|
| 50 |
Kevin |
301 |
}
|
|
|
302 |
catch (Exception e)
|
|
|
303 |
{
|
| 53 |
Kevin |
304 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
305 |
DebugText.appendText(e.ToString());
|
|
|
306 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
307 |
return true;
|
| 50 |
Kevin |
308 |
}
|
|
|
309 |
}
|
| 53 |
Kevin |
310 |
private static bool SetSharePermissions(string username)
|
| 9 |
Kevin |
311 |
{
|
|
|
312 |
string combinationString = "";
|
|
|
313 |
foreach (string systemUser in Settings_Form.systemAccounts)
|
|
|
314 |
{
|
|
|
315 |
combinationString = combinationString.Insert(combinationString.Length, " /GRANT:\"" + systemUser + "\",FULL");
|
|
|
316 |
}
|
|
|
317 |
combinationString = combinationString.Insert(combinationString.Length, " /GRANT:\"" + username + "\",FULL");
|
|
|
318 |
try
|
|
|
319 |
{
|
|
|
320 |
Process netProcess = new Process();
|
|
|
321 |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
|
|
|
322 |
netProcess.StartInfo.FileName = "net.exe";
|
|
|
323 |
netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
324 |
netProcess.StartInfo.CreateNoWindow = true;
|
|
|
325 |
netProcess.StartInfo.Arguments = "SHARE \"" + username + "\"=\"" + Settings_Form.sharedFolderLocation + "\\" + username + "\" " + combinationString;
|
|
|
326 |
netProcess.Start();
|
|
|
327 |
netProcess.WaitForExit();
|
|
|
328 |
netProcess.Close();
|
| 53 |
Kevin |
329 |
DebugText.appendText("Share permissions for " + username + " has been set");
|
|
|
330 |
return true;
|
| 9 |
Kevin |
331 |
}
|
|
|
332 |
catch (Exception e)
|
|
|
333 |
{
|
| 53 |
Kevin |
334 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
335 |
DebugText.appendText(e.ToString());
|
|
|
336 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
337 |
return false;
|
| 9 |
Kevin |
338 |
}
|
|
|
339 |
}
|
| 53 |
Kevin |
340 |
private static bool RemoveSharePermissions(string username)
|
| 9 |
Kevin |
341 |
{
|
|
|
342 |
try
|
|
|
343 |
{
|
|
|
344 |
Process netProcess = new Process();
|
|
|
345 |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
|
|
|
346 |
netProcess.StartInfo.FileName = "net.exe";
|
|
|
347 |
netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
348 |
netProcess.StartInfo.CreateNoWindow = true;
|
|
|
349 |
netProcess.StartInfo.Arguments = "SHARE \"" + username + "\" /Delete";
|
|
|
350 |
netProcess.Start();
|
|
|
351 |
netProcess.WaitForExit();
|
|
|
352 |
netProcess.Close();
|
| 53 |
Kevin |
353 |
DebugText.appendText("Share permissions for " + username + " has been removed");
|
|
|
354 |
return true;
|
| 9 |
Kevin |
355 |
}
|
|
|
356 |
catch (Exception e)
|
|
|
357 |
{
|
| 53 |
Kevin |
358 |
//MessageBox.Show(e.ToString(), "Error");
|
|
|
359 |
DebugText.appendText(e.ToString());
|
|
|
360 |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
|
|
|
361 |
return false;
|
| 9 |
Kevin |
362 |
}
|
|
|
363 |
}
|
|
|
364 |
}
|
|
|
365 |
}
|