1 |
// Douglas Thrift |
2 |
// |
3 |
// CCS Computer Science |
4 |
// |
5 |
// Windows Finger Daemon |
6 |
|
7 |
using System; |
8 |
using System.Collections.Generic; |
9 |
using System.IO; |
10 |
using System.Net; |
11 |
using System.Net.Sockets; |
12 |
using System.ServiceProcess; |
13 |
using System.Text; |
14 |
using System.Threading; |
15 |
|
16 |
public class CCSFinger : ServiceBase |
17 |
{ |
18 |
private class Finger |
19 |
{ |
20 |
private class Login |
21 |
{ |
22 |
private Net.USER_INFO_11 info; |
23 |
private uint id; |
24 |
private string session; |
25 |
private string client; |
26 |
private Wts.WTS_PROTOCOL_TYPE protocol; |
27 |
private Wts.WTS_CONNECTSTATE_CLASS status; |
28 |
private DateTime logon = DateTime.MinValue; |
29 |
|
30 |
public Login(string login) |
31 |
{ |
32 |
Net.UserGetInfo(null, login, 11, out info); |
33 |
|
34 |
this.id = uint.MaxValue; |
35 |
} |
36 |
|
37 |
public Login(string login, uint id, string session, string client, Wts.WTS_PROTOCOL_TYPE protocol, Wts.WTS_CONNECTSTATE_CLASS status) |
38 |
{ |
39 |
Net.UserGetInfo(null, login, 11, out info); |
40 |
|
41 |
this.id = id; |
42 |
this.session = session; |
43 |
this.client = client; |
44 |
this.protocol = protocol; |
45 |
this.status = status; |
46 |
} |
47 |
|
48 |
public string Client |
49 |
{ |
50 |
get { return protocol == Wts.WTS_PROTOCOL_TYPE.WTS_PROTOCOL_TYPE_CONSOLE ? "the Console" : client; } |
51 |
} |
52 |
|
53 |
public string Directory |
54 |
{ |
55 |
get { return @"\\Zweihander\" + info.usri11_name; } |
56 |
} |
57 |
|
58 |
public uint Id |
59 |
{ |
60 |
get { return id; } |
61 |
} |
62 |
|
63 |
public DateTime LastLogon |
64 |
{ |
65 |
get |
66 |
{ |
67 |
if (logon == DateTime.MinValue) |
68 |
logon = new DateTime(1970, 1, 1) + new TimeSpan((long)info.usri11_last_logon * 10000000); |
69 |
|
70 |
return logon; |
71 |
} |
72 |
} |
73 |
|
74 |
public string Name |
75 |
{ |
76 |
get { return info.usri11_full_name; } |
77 |
} |
78 |
|
79 |
public string Session |
80 |
{ |
81 |
get { return session; } |
82 |
} |
83 |
|
84 |
public string Status |
85 |
{ |
86 |
get { return status.ToString().Substring(3); } |
87 |
} |
88 |
} |
89 |
|
90 |
private StringWriter writer = new StringWriter(); |
91 |
private SortedDictionary<string, List<Login>> logins = new SortedDictionary<string, List<Login>>(); |
92 |
private List<string> nots = new List<string>(); |
93 |
|
94 |
private void Full() |
95 |
{ |
96 |
foreach (string not in nots) |
97 |
writer.Write("finger: {0}: no such user\r\n", not); |
98 |
|
99 |
bool first = true; |
100 |
|
101 |
foreach (KeyValuePair<string, List<Login>> login in logins) |
102 |
{ |
103 |
if (first) |
104 |
first = true; |
105 |
else |
106 |
writer.Write("\r\n"); |
107 |
|
108 |
writer.Write("Login: {0,-32} Name: {1}\r\nDirectory: {2}\r\n", login.Key, login.Value[0].Name, login.Value[0].Directory); |
109 |
|
110 |
foreach (Login login_ in login.Value) |
111 |
if (login_.Id != uint.MaxValue) |
112 |
writer.Write("{0} on {1}, from {2}\r\n", login_.Status, login_.Id, login_.Client); |
113 |
|
114 |
writer.Write("Last login {0:ddd MMM dd HH:mm} ({1})\r\n", login.Value[0].LastLogon.ToLocalTime(), login.Value[0].LastLogon.ToLocalTime().IsDaylightSavingTime() ? "PDT" : "PST"); |
115 |
|
116 |
string[] files = { ".project", ".plan" }; |
117 |
|
118 |
foreach (string file in files) |
119 |
try |
120 |
{ |
121 |
StreamReader reader = new StreamReader(new FileStream(Directory.GetFiles(login.Value[0].Directory, file)[0], FileMode.Open, FileAccess.Read)); |
122 |
|
123 |
switch (file) |
124 |
{ |
125 |
case ".project": |
126 |
writer.Write("Project:\r\n"); |
127 |
break; |
128 |
case ".plan": |
129 |
writer.Write("Plan:\r\n"); |
130 |
break; |
131 |
} |
132 |
|
133 |
writer.Write("{0}\r\n", reader.ReadToEnd()); |
134 |
} |
135 |
catch (IndexOutOfRangeException) {} |
136 |
} |
137 |
} |
138 |
|
139 |
private void Sessions(IDictionary<string, List<Login>> logins) |
140 |
{ |
141 |
Wts.WTS_SESSION_INFO[] sessions; |
142 |
uint count; |
143 |
|
144 |
Wts.EnumerateSessions(IntPtr.Zero, 0, 1, out sessions, out count); |
145 |
|
146 |
for (uint index = 0; index != count; ++index) |
147 |
{ |
148 |
string name; |
149 |
uint size; |
150 |
|
151 |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, out name, out size); |
152 |
|
153 |
if (name.Length > 0) |
154 |
{ |
155 |
string session, client; |
156 |
Wts.WTS_PROTOCOL_TYPE protocol; |
157 |
Wts.WTS_CONNECTSTATE_CLASS status; |
158 |
|
159 |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, out session, out size); |
160 |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientName, out client, out size); |
161 |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientProtocolType, out protocol, out size); |
162 |
Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSConnectState, out status, out size); |
163 |
|
164 |
if (!logins.ContainsKey(name)) |
165 |
logins.Add(string.Copy(name), new List<Login>()); |
166 |
|
167 |
logins[name].Add(new Login(name, sessions[index].SessionId, session, client, protocol, status)); |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
public Finger(bool full) |
173 |
{ |
174 |
Sessions(logins); |
175 |
|
176 |
if (logins.Count < 1) |
177 |
writer.Write("No one logged on.\r\n"); |
178 |
else if (full) |
179 |
Full(); |
180 |
else |
181 |
{ |
182 |
writer.Write("Login Name Id Session Status\r\n"); |
183 |
|
184 |
foreach (KeyValuePair<string, List<Login>> login_ in logins) |
185 |
foreach (Login login in login_.Value) |
186 |
writer.Write("{0,-15} {1,-20} {2,-6} {3,-13} {4}\r\n", login_.Key, login.Name.Length > 20 ? login.Name.Substring(0, 20) : login.Name, login.Id, login.Session, login.Status); |
187 |
} |
188 |
} |
189 |
|
190 |
public Finger(ICollection<string> names) |
191 |
{ |
192 |
uint count; |
193 |
Net.NET_DISPLAY_USER[] users; |
194 |
|
195 |
Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, out count, out users); |
196 |
|
197 |
Dictionary<string, List<Login>> logins_ = new Dictionary<string, List<Login>>(); |
198 |
|
199 |
Sessions(logins_); |
200 |
|
201 |
foreach (string name in names) |
202 |
{ |
203 |
bool not = true; |
204 |
|
205 |
for (uint index = 0; index != count; ++index) |
206 |
if (users[index].usri1_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 || users[index].usri1_full_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 && !logins.ContainsKey(users[index].usri1_name)) |
207 |
{ |
208 |
logins.Add(users[index].usri1_name, new List<Login>()); |
209 |
|
210 |
logins[users[index].usri1_name].Add(new Login(users[index].usri1_name)); |
211 |
|
212 |
if (logins_.ContainsKey(users[index].usri1_name)) |
213 |
logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]); |
214 |
|
215 |
not = false; |
216 |
} |
217 |
|
218 |
if (not) |
219 |
nots.Add(name); |
220 |
} |
221 |
|
222 |
Full(); |
223 |
} |
224 |
|
225 |
public override string ToString() |
226 |
{ |
227 |
return writer.ToString(); |
228 |
} |
229 |
} |
230 |
|
231 |
/*private static void Do() |
232 |
{ |
233 |
TcpListener listener = null; |
234 |
|
235 |
try |
236 |
{ |
237 |
listener = new TcpListener(IPAddress.Any, 79); |
238 |
|
239 |
listener.Start(); |
240 |
|
241 |
while (true) |
242 |
{ |
243 |
TcpClient client = listener.AcceptTcpClient(); |
244 |
Thread thread = new Thread(new ParameterizedThreadStart(Do)); |
245 |
|
246 |
thread.Start(client); |
247 |
} |
248 |
} |
249 |
catch (SocketException exception) |
250 |
{ |
251 |
Console.Error.WriteLine(exception); |
252 |
} |
253 |
finally |
254 |
{ |
255 |
listener.Stop(); |
256 |
} |
257 |
}*/ |
258 |
|
259 |
private static void Do(object client) |
260 |
{ |
261 |
/*NetworkStream stream = ((TcpClient)client).GetStream(); |
262 |
byte[] buffer = new byte[1024]; |
263 |
int size = 0, byte_; |
264 |
|
265 |
while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1) |
266 |
buffer[size++] = (byte)byte_;*/ |
267 |
|
268 |
string line = /*Encoding.ASCII.GetString(buffer, 0, size)*/ Console.ReadLine(); |
269 |
List<string> names = new List<string>(); |
270 |
bool full = false; |
271 |
|
272 |
foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) |
273 |
{ |
274 |
if (name == "/W") |
275 |
full = true; |
276 |
else |
277 |
names.Add(name); |
278 |
|
279 |
Console.WriteLine(name); |
280 |
} |
281 |
|
282 |
//StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false)); |
283 |
|
284 |
/*writer*/Console.Write(names.Count > 0 ? new Finger(names) : new Finger(full)); |
285 |
//writer.Close(); |
286 |
//stream.Close(); |
287 |
//((TcpClient)client).Close(); |
288 |
} |
289 |
|
290 |
public static int Main(string[] args) |
291 |
{ |
292 |
Do(null); |
293 |
|
294 |
return 0; |
295 |
} |
296 |
} |