forked from cedrozor/myrtille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Default.aspx.cs
922 lines (833 loc) · 42.2 KB
/
Default.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/*
Myrtille: A native HTML4/5 Remote Desktop Protocol client.
Copyright(c) 2014-2020 Cedric Coste
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Myrtille.Helpers;
using Myrtille.Services.Contracts;
using Myrtille.Web.Properties;
namespace Myrtille.Web
{
public partial class Default : Page
{
private MFAAuthenticationClient _mfaAuthClient = new MFAAuthenticationClient();
private EnterpriseClient _enterpriseClient = new EnterpriseClient();
private ConnectionClient _connectionClient = new ConnectionClient(Settings.Default.ConnectionServiceUrl);
private bool _allowRemoteClipboard;
private bool _allowFileTransfer;
private bool _allowPrintDownload;
private bool _allowSessionSharing;
private bool _allowAudioPlayback;
private bool _clientIPTracking;
private bool _toolbarEnabled;
private bool _loginEnabled;
private string _loginUrl;
private bool _httpSessionUseUri;
private bool _authorizedRequest = true;
private bool _localAdmin;
private EnterpriseSession _enterpriseSession;
protected RemoteSession RemoteSession;
/// <summary>
/// page init
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Init(
object sender,
EventArgs e)
{
// remote clipboard
if (!bool.TryParse(ConfigurationManager.AppSettings["AllowRemoteClipboard"], out _allowRemoteClipboard))
{
_allowRemoteClipboard = true;
}
// file transfer
if (!bool.TryParse(ConfigurationManager.AppSettings["AllowFileTransfer"], out _allowFileTransfer))
{
_allowFileTransfer = true;
}
// print download
if (!bool.TryParse(ConfigurationManager.AppSettings["AllowPrintDownload"], out _allowPrintDownload))
{
_allowPrintDownload = true;
}
// session sharing
if (!bool.TryParse(ConfigurationManager.AppSettings["AllowSessionSharing"], out _allowSessionSharing))
{
_allowSessionSharing = true;
}
// audio playback
if (!bool.TryParse(ConfigurationManager.AppSettings["AllowAudioPlayback"], out _allowAudioPlayback))
{
_allowAudioPlayback = true;
}
// client ip tracking
if (!bool.TryParse(ConfigurationManager.AppSettings["ClientIPTracking"], out _clientIPTracking))
{
_clientIPTracking = false;
}
// toolbar control
if (!bool.TryParse(ConfigurationManager.AppSettings["ToolbarEnabled"], out _toolbarEnabled))
{
_toolbarEnabled = true;
}
// connect from a login page or url
if (!bool.TryParse(ConfigurationManager.AppSettings["LoginEnabled"], out _loginEnabled))
{
_loginEnabled = true;
}
// if enabled, url of the login page
if (_loginEnabled)
{
_loginUrl = ConfigurationManager.AppSettings["LoginUrl"];
}
// cookieless session
var sessionStateSection = (SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
_httpSessionUseUri = sessionStateSection.Cookieless == HttpCookieMode.UseUri;
}
/// <summary>
/// page load (postback data is now available)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(
object sender,
EventArgs e)
{
// client ip protection
if (_clientIPTracking)
{
var clientIP = ClientIPHelper.ClientIPFromRequest(new HttpContextWrapper(HttpContext.Current).Request, true, new string[] { });
if (Session[HttpSessionStateVariables.ClientIP.ToString()] == null)
{
Session[HttpSessionStateVariables.ClientIP.ToString()] = clientIP;
}
else if (!((string)Session[HttpSessionStateVariables.ClientIP.ToString()]).Equals(clientIP))
{
System.Diagnostics.Trace.TraceWarning("Failed to validate the client ip");
_authorizedRequest = false;
UpdateControls();
return;
}
}
// session spoofing protection
if (_httpSessionUseUri)
{
if (Request.Cookies["clientKey"] == null)
{
if (Session[HttpSessionStateVariables.ClientKey.ToString()] == null)
{
var cookie = new HttpCookie("clientKey");
cookie.Value = Guid.NewGuid().ToString();
cookie.Path = "/";
Response.Cookies.Add(cookie);
}
else
{
System.Diagnostics.Trace.TraceWarning("Failed to validate the client key: missing key");
_authorizedRequest = false;
UpdateControls();
return;
}
}
else
{
var clientKey = Request.Cookies["clientKey"].Value;
if (Session[HttpSessionStateVariables.ClientKey.ToString()] == null)
{
Session[HttpSessionStateVariables.ClientKey.ToString()] = clientKey;
}
else if (!((string)Session[HttpSessionStateVariables.ClientKey.ToString()]).Equals(clientKey))
{
System.Diagnostics.Trace.TraceWarning("Failed to validate the client key: key mismatch");
_authorizedRequest = false;
UpdateControls();
return;
}
}
}
// retrieve the active enterprise session, if any
if (Session[HttpSessionStateVariables.EnterpriseSession.ToString()] != null)
{
try
{
_enterpriseSession = (EnterpriseSession)Session[HttpSessionStateVariables.EnterpriseSession.ToString()];
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to retrieve the active enterprise session ({0})", exc);
}
}
// retrieve the active remote session, if any
if (Session[HttpSessionStateVariables.RemoteSession.ToString()] != null)
{
try
{
RemoteSession = (RemoteSession)Session[HttpSessionStateVariables.RemoteSession.ToString()];
// if using a connection service, send the connection state
if (Session.SessionID.Equals(RemoteSession.OwnerSessionID) && RemoteSession.ConnectionService)
{
_connectionClient.SetConnectionState(RemoteSession.Id, string.IsNullOrEmpty(RemoteSession.VMAddress) ? RemoteSession.ServerAddress : RemoteSession.VMAddress, GuidHelper.ConvertFromString(RemoteSession.VMGuid), RemoteSession.State);
}
if (RemoteSession.State == RemoteSessionState.Disconnected)
{
// if connecting from a login page or url, show any connection failure into a dialog box
// otherwise, this is delegated to the connection API used and its related UI
if (_loginEnabled)
{
// handle connection failure
var script = string.Format("handleRemoteSessionExit({0});", RemoteSession.ExitCode);
// redirect to login page
if (!string.IsNullOrEmpty(_loginUrl))
{
script += string.Format("window.location.href = '{0}';", _loginUrl);
}
ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), script, true);
}
// cleanup
Session[HttpSessionStateVariables.RemoteSession.ToString()] = null;
if (Session[HttpSessionStateVariables.GuestInfo.ToString()] != null)
{
Session[HttpSessionStateVariables.GuestInfo.ToString()] = null;
}
RemoteSession = null;
}
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to retrieve the active remote session ({0})", exc);
}
}
// retrieve a shared remote session from url, if any
else if (!string.IsNullOrEmpty(Request["gid"]))
{
var guestId = Guid.Empty;
if (Guid.TryParse(Request["gid"], out guestId))
{
var sharingInfo = GetSharingInfo(guestId);
if (sharingInfo != null)
{
Session[HttpSessionStateVariables.RemoteSession.ToString()] = sharingInfo.RemoteSession;
Session[HttpSessionStateVariables.GuestInfo.ToString()] = sharingInfo.GuestInfo;
try
{
// remove the shared session guid from url
Response.Redirect("~/", true);
}
catch (ThreadAbortException)
{
// occurs because the response is ended after redirect
}
}
}
}
if (_httpSessionUseUri)
{
// if running myrtille into an iframe, the iframe url is registered (into a cookie) after the remote session is connected
// this is necessary to prevent a new http session from being generated for the iframe if the page is reloaded, due to the missing http session id into the iframe url (!)
// multiple iframes (on the same page), like multiple connections/tabs, requires cookieless="UseUri" for sessionState into web.config
// problem is, there can be many cases where the cookie is not removed after the remote session is disconnected (network issue, server down, etc?)
// if the page is reloaded, the iframe will use it's previously registered http session... which may not exist anymore or have its active remote session disconnected
// if that happens, unregister the iframe url (from the cookie) and reload the page; that will provide a new connection identifier to the iframe and reconnect it
if (!string.IsNullOrEmpty(Request["fid"]) && RemoteSession == null)
{
if (Request.Cookies[Request["fid"]] != null)
{
// remove the cookie for the given iframe
Response.Cookies[Request["fid"]].Expires = DateTime.Now.AddDays(-1);
// reload the page
ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), "parent.location.href = parent.location.href;", true);
}
}
}
// local admin
if (_enterpriseSession == null && RemoteSession == null && _enterpriseClient.GetMode() == EnterpriseMode.Local && !string.IsNullOrEmpty(Request["mode"]) && Request["mode"].Equals("admin"))
{
_localAdmin = true;
}
// postback events may redirect after execution; UI is updated from there
if (!IsPostBack)
{
UpdateControls();
}
// disable the browser cache; in addition to a "noCache" dummy param, with current time, on long-polling and xhr requests
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
/// <summary>
/// force remove the .net viewstate hidden fields from page (large bunch of unwanted data in url)
/// </summary>
/// <param name="writer"></param>
protected override void Render(
HtmlTextWriter writer)
{
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var tw = new HtmlTextWriter(sw);
base.Render(tw);
var html = sb.ToString();
html = Regex.Replace(html, "<input[^>]*id=\"(__VIEWSTATE)\"[^>]*>", string.Empty, RegexOptions.IgnoreCase);
html = Regex.Replace(html, "<input[^>]*id=\"(__VIEWSTATEGENERATOR)\"[^>]*>", string.Empty, RegexOptions.IgnoreCase);
writer.Write(html);
}
/// <summary>
/// update the UI
/// </summary>
private void UpdateControls()
{
// remote session toolbar
if (RemoteSession != null && (RemoteSession.State == RemoteSessionState.Connecting || RemoteSession.State == RemoteSessionState.Connected))
{
if (_toolbarEnabled)
{
// interacting with the remote session is available to guests with control access, but only the remote session owner should have control on the remote session itself
var controlEnabled = Session.SessionID.Equals(RemoteSession.OwnerSessionID) || (Session[HttpSessionStateVariables.GuestInfo.ToString()] != null && ((GuestInfo)Session[HttpSessionStateVariables.GuestInfo.ToString()]).Control);
toolbar.Visible = true;
toolbarToggle.Visible = true;
serverInfo.Value = !string.IsNullOrEmpty(RemoteSession.VMGuid) ? RemoteSession.VMGuid : (!string.IsNullOrEmpty(RemoteSession.HostName) ? RemoteSession.HostName : RemoteSession.ServerAddress);
userInfo.Value = !string.IsNullOrEmpty(RemoteSession.VMGuid) || RemoteSession.SecurityProtocol == SecurityProtocol.rdp ? string.Empty : (string.IsNullOrEmpty(RemoteSession.UserDomain) ? RemoteSession.UserName : string.Format("{0}\\{1}", RemoteSession.UserDomain, RemoteSession.UserName));
userInfo.Visible = !string.IsNullOrEmpty(userInfo.Value);
scale.Value = RemoteSession.BrowserResize == BrowserResize.Scale ? "Scale ON" : "Scale OFF";
scale.Disabled = !Session.SessionID.Equals(RemoteSession.OwnerSessionID) || RemoteSession.HostType == HostType.SSH;
reconnect.Value = RemoteSession.BrowserResize == BrowserResize.Reconnect ? "Reconnect ON" : "Reconnect OFF";
reconnect.Disabled = !Session.SessionID.Equals(RemoteSession.OwnerSessionID) || RemoteSession.HostType == HostType.SSH;
keyboard.Disabled = !controlEnabled;
clipboard.Disabled = !controlEnabled || RemoteSession.HostType == HostType.SSH || !RemoteSession.AllowRemoteClipboard || (!string.IsNullOrEmpty(RemoteSession.VMGuid) && !RemoteSession.VMEnhancedMode);
files.Disabled = !Session.SessionID.Equals(RemoteSession.OwnerSessionID) || RemoteSession.HostType == HostType.SSH || !RemoteSession.AllowFileTransfer || (RemoteSession.ServerAddress.ToLower() != "localhost" && RemoteSession.ServerAddress != "127.0.0.1" && RemoteSession.ServerAddress != "[::1]" && RemoteSession.ServerAddress != Request.Url.Host && string.IsNullOrEmpty(RemoteSession.UserDomain)) || !string.IsNullOrEmpty(RemoteSession.VMGuid);
cad.Disabled = !controlEnabled || RemoteSession.HostType == HostType.SSH;
mrc.Disabled = !controlEnabled || RemoteSession.HostType == HostType.SSH;
vswipe.Disabled = !controlEnabled || RemoteSession.HostType == HostType.SSH;
share.Disabled = !Session.SessionID.Equals(RemoteSession.OwnerSessionID) || !RemoteSession.AllowSessionSharing;
disconnect.Disabled = !Session.SessionID.Equals(RemoteSession.OwnerSessionID);
}
}
// hosts list
else if (_enterpriseSession != null && _enterpriseSession.AuthenticationErrorCode == EnterpriseAuthenticationErrorCode.NONE)
{
hosts.Visible = true;
enterpriseUserInfo.Value = string.IsNullOrEmpty(_enterpriseSession.Domain) ? _enterpriseSession.UserName : string.Format("{0}\\{1}", _enterpriseSession.Domain, _enterpriseSession.UserName);
enterpriseUserInfo.Visible = !string.IsNullOrEmpty(enterpriseUserInfo.Value);
newRDPHost.Visible = _enterpriseSession.IsAdmin;
newSSHHost.Visible = _enterpriseSession.IsAdmin;
hostsList.DataSource = _enterpriseClient.GetSessionHosts(_enterpriseSession.SessionID);
hostsList.DataBind();
}
// login screen
else
{
// connection params are sent when the login form is submitted, either through http post (the default form method) or http get (querystring)
login.Visible = _loginEnabled;
// MFA
if (_mfaAuthClient.GetState())
{
mfaDiv.Visible = true;
mfaProvider.InnerText = _mfaAuthClient.GetPromptLabel();
mfaProvider.HRef = _mfaAuthClient.GetProviderURL();
}
// enterprise mode
if (_enterpriseClient.GetMode() == EnterpriseMode.Domain || _localAdmin)
{
hostConnectDiv.Visible = false;
adminDiv.Visible = _localAdmin;
if (adminDiv.Visible)
{
adminText.InnerText = "Home";
adminUrl.HRef = "~/";
}
}
// standard mode
else
{
connect.Attributes["onclick"] = "initDisplay();";
adminDiv.Visible = _enterpriseClient.GetMode() == EnterpriseMode.Local;
}
}
}
/// <summary>
/// enterprise mode from url: load the enterprise session (from querystring param) and proceed to connection; the user is non admin and the url is only usable once
/// enterprise mode from login: authenticate the user against the enterprise active directory and list the servers available to the user; the user is admin if member of the "EnterpriseAdminGroup" defined into myrtille services config
/// standard mode: connect the specified server; authentication is delegated to the remote server or connection broker (if applicable)
/// if MFA is enabled and not already processed, authenticate the user against the configured MFA provider (OTP preferred)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ConnectButtonClick(
object sender,
EventArgs e)
{
if (!_authorizedRequest)
return;
// one time usage enterprise session url
if (_enterpriseSession == null && Request["SI"] != null && Request["SD"] != null && Request["SK"] != null)
{
CreateEnterpriseSessionFromUrl();
}
// MFA (OTP passcode)
if (_enterpriseSession == null && _mfaAuthClient.GetState())
{
var clientIP = ClientIPHelper.ClientIPFromRequest(new HttpContextWrapper(HttpContext.Current).Request, true, new string[] { });
if (!_mfaAuthClient.Authenticate(user.Value, mfaPassword.Value, clientIP))
{
connectError.InnerText = "MFA Authentication failed!";
UpdateControls();
return;
}
}
// enterprise mode from login
if (_enterpriseSession == null && (_enterpriseClient.GetMode() == EnterpriseMode.Domain || _localAdmin))
{
CreateEnterpriseSessionFromLogin();
}
// connection from:
// > standard mode
// > enterprise mode: hosts list
// > enterprise mode: one time session url
else
{
// the display size is required to start a remote session
// if missing, the client will provide it automatically
if (string.IsNullOrEmpty(width.Value) || string.IsNullOrEmpty(height.Value))
{
return;
}
// connect
if (ConnectRemoteServer())
{
// in enterprise mode from login, a new http session id was already generated (no need to do it each time an host is connected!)
// in standard mode or enterprise mode from url, a new http session id must be generated
if (_enterpriseSession == null || Request["SI"] != null)
{
// session fixation protection
if (_httpSessionUseUri)
{
// generate a new http session id
RemoteSession.OwnerSessionID = HttpSessionHelper.RegenerateSessionId();
}
}
try
{
// standard mode: switch to http get (standard login) or remove the connection params from url (auto-connect / start program from url)
// enterprise mode: remove the host id from url
Response.Redirect("~/", true);
}
catch (ThreadAbortException)
{
// occurs because the response is ended after redirect
}
}
// connection failed from the hosts list or from a one time session url
else if (_enterpriseSession != null && Request["SD"] != null)
{
try
{
// remove the host id from url
Response.Redirect("~/", true);
}
catch (ThreadAbortException)
{
// occurs because the response is ended after redirect
}
}
}
}
/// <summary>
/// connect the remote server
/// </summary>
/// <remarks>
/// authentication is delegated to the remote server or connection broker (if applicable)
/// </remarks>
private bool ConnectRemoteServer()
{
// connection parameters
string loginHostName = null;
var loginHostType = (HostType)Convert.ToInt32(hostType.Value);
var loginProtocol = (SecurityProtocol)securityProtocol.SelectedIndex;
var loginServer = string.IsNullOrEmpty(server.Value) ? "localhost" : server.Value;
var loginVMGuid = vmGuid.Value;
var loginVMAddress = string.Empty;
var loginVMEnhancedMode = vmEnhancedMode.Checked;
var loginDomain = domain.Value;
var loginUser = user.Value;
var loginPassword = string.IsNullOrEmpty(passwordHash.Value) ? password.Value : CryptoHelper.RDP_Decrypt(passwordHash.Value);
var startProgram = program.Value;
// allowed features
var allowRemoteClipboard = _allowRemoteClipboard;
var allowFileTransfer = _allowFileTransfer;
var allowPrintDownload = _allowPrintDownload;
var allowSessionSharing = _allowSessionSharing;
var allowAudioPlayback = _allowAudioPlayback;
// sharing parameters
int maxActiveGuests = int.MaxValue;
var connectionId = Guid.NewGuid();
// connect an host from the hosts list or from a one time session url
if (_enterpriseSession != null && (!string.IsNullOrEmpty(Request["SD"])))
{
long hostId;
if (!long.TryParse(Request["SD"], out hostId))
{
hostId = 0;
}
try
{
// retrieve the host connection details
var connection = _enterpriseClient.GetSessionConnectionDetails(_enterpriseSession.SessionID, hostId, _enterpriseSession.SessionKey);
if (connection == null)
{
System.Diagnostics.Trace.TraceInformation("Unable to retrieve host {0} connection details (invalid host or one time session url already used?)", hostId);
return false;
}
loginHostName = connection.HostName;
loginHostType = connection.HostType;
loginProtocol = connection.Protocol;
loginServer = !string.IsNullOrEmpty(connection.HostAddress) ? connection.HostAddress : connection.HostName;
loginVMGuid = connection.VMGuid;
loginVMEnhancedMode = connection.VMEnhancedMode;
loginDomain = connection.Domain;
loginUser = connection.Username;
loginPassword = CryptoHelper.RDP_Decrypt(connection.Password);
startProgram = connection.StartRemoteProgram;
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to retrieve host {0} connection details ({1})", hostId, exc);
return false;
}
}
// by using a connection service on a backend (connection API), the connection details can be hidden from querystring and mapped to a connection identifier
else if (!string.IsNullOrEmpty(Request["cid"]))
{
if (!Guid.TryParse(Request["cid"], out connectionId))
{
System.Diagnostics.Trace.TraceInformation("Invalid connection id {0}", Request["cid"]);
return false;
}
try
{
// retrieve the connection details
var connection = _connectionClient.GetConnectionInfo(connectionId);
if (connection == null)
{
System.Diagnostics.Trace.TraceInformation("Unable to retrieve connection info {0}", connectionId);
return false;
}
// ensure the user is allowed to connect the host
if (!_connectionClient.IsUserAllowedToConnectHost(connection.User.Domain, connection.User.UserName, connection.Host.IPAddress, connection.VM != null ? connection.VM.Guid : Guid.Empty))
{
System.Diagnostics.Trace.TraceInformation("User: domain={0}, name={1} is not allowed to connect host {2}", connection.User.Domain, connection.User.UserName, connection.Host.IPAddress);
return false;
}
loginHostType = connection.Host.HostType;
loginProtocol = connection.Host.SecurityProtocol;
loginServer = connection.Host.IPAddress;
loginVMGuid = connection.VM != null ? connection.VM.Guid.ToString() : string.Empty;
loginVMAddress = connection.VM != null ? connection.VM.IPAddress : string.Empty;
loginVMEnhancedMode = connection.VM != null ? connection.VM.EnhancedMode : false;
loginDomain = connection.User.Domain;
loginUser = connection.User.UserName;
loginPassword = connection.User.Password;
startProgram = connection.StartProgram;
allowRemoteClipboard = allowRemoteClipboard && connection.AllowRemoteClipboard;
allowFileTransfer = allowFileTransfer && connection.AllowFileTransfer;
allowPrintDownload = allowPrintDownload && connection.AllowPrintDownload;
allowSessionSharing = allowSessionSharing && connection.MaxActiveGuests > 0;
allowAudioPlayback = allowAudioPlayback && connection.AllowAudioPlayback;
maxActiveGuests = connection.MaxActiveGuests;
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to retrieve connection info {0} ({1})", connectionId, exc);
return false;
}
}
// if the connection from login screen or url is disabled, the connection must be done either by using a connection API or from the enterprise mode
else if (!_loginEnabled)
{
return false;
}
// remove any active remote session (disconnected?)
if (RemoteSession != null)
{
// unset the remote session for the current http session
Session[HttpSessionStateVariables.RemoteSession.ToString()] = null;
RemoteSession = null;
}
// create a new remote session
try
{
Application.Lock();
// create the remote session
RemoteSession = new RemoteSession(
connectionId,
loginHostName,
loginHostType,
loginProtocol,
loginServer,
loginVMGuid,
loginVMAddress,
loginVMEnhancedMode,
loginDomain,
loginUser,
loginPassword,
int.Parse(width.Value),
int.Parse(height.Value),
startProgram,
allowRemoteClipboard,
allowFileTransfer,
allowPrintDownload,
allowSessionSharing,
allowAudioPlayback,
maxActiveGuests,
Session.SessionID,
Request["cid"] != null
);
// bind the remote session to the current http session
Session[HttpSessionStateVariables.RemoteSession.ToString()] = RemoteSession;
// register the remote session at the application level
var remoteSessions = (IDictionary<Guid, RemoteSession>)Application[HttpApplicationStateVariables.RemoteSessions.ToString()];
remoteSessions.Add(RemoteSession.Id, RemoteSession);
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to create remote session ({0})", exc);
RemoteSession = null;
}
finally
{
Application.UnLock();
}
// connect it
if (RemoteSession != null)
{
RemoteSession.State = RemoteSessionState.Connecting;
}
else
{
connectError.InnerText = "Failed to create remote session!";
return false;
}
return true;
}
#region enterprise mode
/// <summary>
/// create an enterprise session from a one time url
/// </summary>
private void CreateEnterpriseSessionFromUrl()
{
try
{
// create enterprise session from querystring params
_enterpriseSession = new EnterpriseSession
{
IsAdmin = false, // simple host connection only (no hosts management)
SessionID = Request["SI"],
SessionKey = Request["SK"],
SingleUseConnection = true
};
// bind the enterprise session to the current http session
Session[HttpSessionStateVariables.EnterpriseSession.ToString()] = _enterpriseSession;
// session fixation protection
if (_httpSessionUseUri)
{
// generate a new http session id
HttpSessionHelper.RegenerateSessionId();
}
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to create enterprise session from url ({0})", exc);
}
}
/// <summary>
/// authenticate the user against the enterprise active directory and list the servers available to the user
/// </summary>
private void CreateEnterpriseSessionFromLogin()
{
try
{
// authenticate the user
_enterpriseSession = _enterpriseClient.Authenticate(user.Value, password.Value);
if (_enterpriseSession == null || _enterpriseSession.AuthenticationErrorCode != EnterpriseAuthenticationErrorCode.NONE)
{
if (_enterpriseSession == null)
{
connectError.InnerText = EnterpriseAuthenticationErrorHelper.GetErrorDescription(EnterpriseAuthenticationErrorCode.UNKNOWN_ERROR);
}
else if (_enterpriseSession.AuthenticationErrorCode == EnterpriseAuthenticationErrorCode.PASSWORD_EXPIRED)
{
ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), string.Format("openPopup('changePasswordPopup', 'EnterpriseChangePassword.aspx?userName={0}" + (_localAdmin ? "&mode=admin');" : "');"), user.Value), true);
}
else
{
connectError.InnerText = EnterpriseAuthenticationErrorHelper.GetErrorDescription(_enterpriseSession.AuthenticationErrorCode);
}
UpdateControls();
return;
}
// bind the enterprise session to the current http session
Session[HttpSessionStateVariables.EnterpriseSession.ToString()] = _enterpriseSession;
// session fixation protection
if (_httpSessionUseUri)
{
// generate a new http session id
HttpSessionHelper.RegenerateSessionId();
}
// redirect to the hosts list
Response.Redirect("~/", true);
}
catch (ThreadAbortException)
{
// occurs because the response is ended after redirect
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to create enterprise session from login ({0})", exc);
}
}
/// <summary>
/// populate the enterprise session hosts list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void hostsList_ItemDataBound(
object sender,
RepeaterItemEventArgs e)
{
try
{
var host = e.Item.DataItem as EnterpriseHost;
if (host.PromptForCredentials || string.IsNullOrEmpty(_enterpriseSession.Domain))
{
var hostLink = e.Item.FindControl("hostLink") as HtmlAnchor;
hostLink.HRef = null;
hostLink.Attributes["onclick"] = string.Format("openPopup('editCredentialPopup', 'CredentialsPrompt.aspx?hostId={0}');", host.HostID);
hostLink.Attributes["class"] = "hostLink";
}
else
{
var hostLink = e.Item.FindControl("hostLink") as HtmlAnchor;
hostLink.HRef = string.Format("?SD={0}&__EVENTTARGET=&__EVENTARGUMENT=&connect=Connect%21", host.HostID);
hostLink.Attributes["class"] = "hostLink";
}
var hostName = e.Item.FindControl("hostName") as HtmlGenericControl;
hostName.InnerText = (_enterpriseSession.IsAdmin ? "Edit " : string.Empty) + host.HostName;
if (_enterpriseSession.IsAdmin)
{
hostName.Attributes["class"] = "hostName";
hostName.Attributes["title"] = "edit";
hostName.Attributes["onclick"] = string.Format("openPopup('editHostPopup', 'EditHost.aspx?hostId={0}');", host.HostID);
}
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to populate hosts for the enterprise session {0} ({1})", _enterpriseSession.SessionID, exc);
}
}
/// <summary>
/// logout the enterprise session
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LogoutButtonClick(
object sender,
EventArgs e)
{
if (!_authorizedRequest)
return;
if (_enterpriseSession == null)
return;
try
{
// logout the enterprise session
_enterpriseClient.Logout(_enterpriseSession.SessionID);
Session[HttpSessionStateVariables.EnterpriseSession.ToString()] = null;
_enterpriseSession = null;
// redirect to the login screen
Response.Redirect("~/", true);
}
catch (ThreadAbortException)
{
// occurs because the response is ended after redirect
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to logout the enterprise session {0} ({1})", _enterpriseSession.SessionID, exc);
}
}
#endregion
#region session sharing
/// <summary>
/// retrieve a shared remote session information
/// </summary>
/// <param name="guestId"></param>
/// <returns></returns>
private SharingInfo GetSharingInfo(
Guid guestId)
{
SharingInfo sharingInfo = null;
try
{
Application.Lock();
var sharedSessions = (IDictionary<Guid, SharingInfo>)Application[HttpApplicationStateVariables.SharedRemoteSessions.ToString()];
if (!sharedSessions.ContainsKey(guestId))
{
connectError.InnerText = "Invalid sharing link";
}
else
{
sharingInfo = sharedSessions[guestId];
if (sharingInfo.GuestInfo.Active)
{
connectError.InnerText = "The sharing link was already used";
sharingInfo = null;
}
else if (sharingInfo.RemoteSession.State != RemoteSessionState.Connected)
{
connectError.InnerText = "The session is not connected";
sharingInfo = null;
}
else if (sharingInfo.RemoteSession.ActiveGuests >= sharingInfo.RemoteSession.MaxActiveGuests)
{
connectError.InnerText = "The maximum number of active guests was reached for the session";
sharingInfo = null;
}
else
{
sharingInfo.HttpSession = Session;
sharingInfo.RemoteSession.ActiveGuests++;
sharingInfo.GuestInfo.Active = true;
}
}
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceError("Failed to retrieve the shared remote session for guest {0} ({1})", guestId, exc);
}
finally
{
Application.UnLock();
}
return sharingInfo;
}
#endregion
}
}