ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/CCSFinger.cs
Revision: 721
Committed: 2006-03-30T06:19:41-08:00 (19 years, 3 months ago) by douglas
File size: 8334 byte(s)
Log Message:
Bleh! Still no worky righty!

File Contents

# Content
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 bool forward = false;
93 private List<string> nots = new List<string>();
94
95 private void Full()
96 {
97 if (forward)
98 writer.Write("finger: forwarding service denied\r\n");
99
100 foreach (string not in nots)
101 writer.Write("finger: {0}: no such user\r\n", not);
102
103 bool first = true;
104
105 foreach (KeyValuePair<string, List<Login>> login in logins)
106 {
107 if (first)
108 first = false;
109 else
110 writer.Write("\r\n");
111
112 writer.Write("Login: {0,-32} Name: {1}\r\nDirectory: {2}\r\n", login.Key, login.Value[0].Name, login.Value[0].Directory);
113
114 foreach (Login login_ in login.Value)
115 if (login_.Id != uint.MaxValue)
116 writer.Write("{0} on {1}, from {2}\r\n", login_.Status, login_.Id, login_.Client);
117
118 if (login.Value[0].LastLogon != new DateTime(1970, 1, 1))
119 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");
120 else
121 writer.Write("Never logged in.\r\n");
122
123 writer.Write("No Mail.\r\n");
124
125 string[] files = { ".project", ".plan" };
126
127 foreach (string file in files)
128 try
129 {
130 StreamReader reader = new StreamReader(new FileStream(Directory.GetFiles(login.Value[0].Directory, file)[0], FileMode.Open, FileAccess.Read));
131
132 switch (file)
133 {
134 case ".project":
135 writer.Write("Project:\r\n");
136 break;
137 case ".plan":
138 writer.Write("Plan:\r\n");
139 break;
140 }
141
142 writer.Write("{0}", reader.ReadToEnd());
143 }
144 catch (Exception)
145 {
146 if (file == ".plan")
147 writer.Write("No Plan.\r\n");
148 }
149 }
150 }
151
152 private void Sessions(IDictionary<string, List<Login>> logins)
153 {
154 Wts.WTS_SESSION_INFO[] sessions;
155 uint count;
156
157 Wts.EnumerateSessions(IntPtr.Zero, 0, 1, out sessions, out count);
158
159 for (uint index = 0; index != count; ++index)
160 {
161 string name;
162 uint size;
163
164 Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSUserName, out name, out size);
165
166 if (name.Length > 0)
167 {
168 string session, client;
169 Wts.WTS_PROTOCOL_TYPE protocol;
170 Wts.WTS_CONNECTSTATE_CLASS status;
171
172 Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSWinStationName, out session, out size);
173 Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientName, out client, out size);
174 Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSClientProtocolType, out protocol, out size);
175 Wts.QuerySessionInformation(IntPtr.Zero, sessions[index].SessionId, Wts.WTS_INFO_CLASS.WTSConnectState, out status, out size);
176
177 if (!logins.ContainsKey(name))
178 logins.Add(string.Copy(name), new List<Login>());
179
180 logins[name].Add(new Login(name, sessions[index].SessionId, session, client, protocol, status));
181 }
182 }
183 }
184
185 public Finger(bool full)
186 {
187 Sessions(logins);
188
189 if (logins.Count < 1)
190 writer.Write("No one logged on.\r\n");
191 else if (full)
192 Full();
193 else
194 {
195 writer.Write("Login Name Id Session Status\r\n");
196
197 foreach (KeyValuePair<string, List<Login>> login_ in logins)
198 foreach (Login login in login_.Value)
199 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);
200 }
201 }
202
203 public Finger(ICollection<string> names)
204 {
205 uint count;
206 Net.NET_DISPLAY_USER[] users;
207
208 Net.QueryDisplayInformation(null, 1, 0, 100, uint.MaxValue, out count, out users);
209
210 Dictionary<string, List<Login>> logins_ = new Dictionary<string, List<Login>>();
211
212 Sessions(logins_);
213
214 foreach (string name in names)
215 if (name.Contains("@"))
216 forward = true;
217 else
218 {
219 bool not = true;
220
221 for (uint index = 0; index != count; ++index)
222 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))
223 {
224 logins.Add(users[index].usri1_name, new List<Login>());
225
226 logins[users[index].usri1_name].Add(new Login(users[index].usri1_name));
227
228 if (logins_.ContainsKey(users[index].usri1_name))
229 logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]);
230
231 not = false;
232 }
233
234 if (not)
235 nots.Add(name);
236 }
237
238 Full();
239 }
240
241 public override string ToString()
242 {
243 return writer.ToString();
244 }
245 }
246
247 private static void Do()
248 {
249 TcpListener listener = null;
250
251 try
252 {
253 listener = new TcpListener(IPAddress.Any, 79);
254
255 listener.Start();
256
257 while (true)
258 {
259 TcpClient client = listener.AcceptTcpClient();
260
261 Console.WriteLine(client.Client.RemoteEndPoint);
262
263 client.SendTimeout = 6000;
264 client.LingerState = new LingerOption(true, 60);
265
266 Thread thread = new Thread(new ParameterizedThreadStart(Do));
267
268 thread.Start(client);
269 }
270 }
271 catch (SocketException exception)
272 {
273 Console.Error.WriteLine(exception);
274 }
275 finally
276 {
277 listener.Stop();
278 }
279 }
280
281 private static void Do(object client)
282 {
283 NetworkStream stream = ((TcpClient)client).GetStream();
284 byte[] buffer = new byte[1024];
285 int size = 0, byte_;
286
287 while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
288 buffer[size++] = (byte)byte_;
289
290 string line = Encoding.ASCII.GetString(buffer, 0, size);
291 List<string> names = new List<string>();
292 bool full = false;
293
294 foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
295 {
296 if (name == "/W")
297 full = true;
298 else
299 names.Add(name);
300 }
301
302 StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
303
304 writer.Write(names.Count > 0 ? new Finger(names) : new Finger(full));
305 writer.Close();
306 ((TcpClient)client).Close();
307 }
308
309 public static int Main(string[] args)
310 {
311 Do();
312
313 return 0;
314 }
315 }