2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
2020-12-29 16:28:26 +01:00
|
|
|
|
using System.Threading;
|
2021-03-08 17:47:10 +01:00
|
|
|
|
using System.Xml;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
|
|
|
|
public abstract class AsyncAction : CancellingAction
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
2020-03-29 03:06:35 +02:00
|
|
|
|
|
|
|
|
|
private readonly Func<List<Role>, IXenConnection, string, SudoElevationResult> getElevatedSession = XenAdminConfigManager.Provider.ElevatedSessionDelegate;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private string _result;
|
|
|
|
|
|
2013-08-07 12:09:35 +02:00
|
|
|
|
protected AsyncAction(IXenConnection connection, string title, string description, bool suppressHistory)
|
|
|
|
|
: base(title, description, suppressHistory)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
this.Connection = connection;
|
2014-07-21 12:40:26 +02:00
|
|
|
|
Pool = Helpers.GetPoolOfOne(connection);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AsyncAction(IXenConnection connection, string title, string description)
|
|
|
|
|
: this(connection, title, description, false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AsyncAction(IXenConnection connection, string title)
|
|
|
|
|
: this(connection, title, "", false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AsyncAction(IXenConnection connection, string title, bool suppress_history)
|
|
|
|
|
: this(connection, title, "", suppress_history)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Result
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Exception != null)
|
|
|
|
|
{
|
|
|
|
|
throw Exception;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return _result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set { _result = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-06-20 15:29:22 +02:00
|
|
|
|
/// If you want the action to run a pre-check that the current user can perform all the necessary api calls, list them under this field.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// If empty, then no checks will be run.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected RbacMethodList ApiMethodsToRoleCheck = new RbacMethodList();
|
2020-03-29 03:06:35 +02:00
|
|
|
|
public virtual RbacMethodList GetApiMethodsToRoleCheck => ApiMethodsToRoleCheck;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If the sudoUsername and sudoPassword fields are both not null, then the action will use these credentials when making new sessions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string sudoUsername;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If the sudoUsername and sudoPassword fields are both not null, then the action will use these credentials when making new sessions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string sudoPassword;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks to see if we are using elevated credentials for this action. Returns a session using them if they exist, otherwise
|
|
|
|
|
/// using the basic credentials of the IXenConnection. Important - will throw exceptions similar to connection.NewSession
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-09-17 22:43:39 +02:00
|
|
|
|
protected override Session NewSession()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (Connection == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(sudoPassword) || String.IsNullOrEmpty(sudoUsername))
|
|
|
|
|
return Connection.DuplicateSession();
|
|
|
|
|
|
|
|
|
|
return Connection.ElevatedSession(sudoUsername, sudoPassword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used for cross connection actions (e.g adding a host to a pool, we need to get a session from the connection we are joining)
|
|
|
|
|
/// Checks to see if we are using elevated credentials for this action. Returns a session using them if they exist, otherwise
|
|
|
|
|
/// using the basic credentials of the _supplied_ IXenConnection. Important - will throw exceptions similar to connection.NewSession
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="xc"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-09-17 22:43:39 +02:00
|
|
|
|
protected Session NewSession(IXenConnection xc)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (Connection == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(sudoPassword) || String.IsNullOrEmpty(sudoUsername))
|
|
|
|
|
return xc.DuplicateSession();
|
|
|
|
|
|
|
|
|
|
return xc.ElevatedSession(sudoUsername, sudoPassword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prepare the action's task for exit by removing the XenCenterUUID.
|
|
|
|
|
/// A call here just before exit will mean that the task will get picked
|
2021-03-09 01:31:57 +01:00
|
|
|
|
/// up as a meddling action on restart of xencenter, and thus reappear in the EventsTab.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// </summary>
|
2022-12-05 23:55:18 +01:00
|
|
|
|
public void PrepareForEventReloadAfterRestart()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-05 23:55:18 +01:00
|
|
|
|
if (Session != null && !string.IsNullOrEmpty(RelatedTask?.opaque_ref))
|
|
|
|
|
Task.remove_from_other_config(Session, RelatedTask.opaque_ref, "XenCenterUUID");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-12-05 23:55:18 +01:00
|
|
|
|
catch (Failure f)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-05 23:55:18 +01:00
|
|
|
|
// Read only user without task.other_config rights - just ignore this request
|
|
|
|
|
if (f.ErrorDescription.Count > 0 && f.ErrorDescription[0] == Failure.RBAC_PERMISSION_DENIED)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
log.Debug($"Removing XenCenterUUID failed: {f.Message}");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-12-05 23:55:18 +01:00
|
|
|
|
catch (Exception e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-05 23:55:18 +01:00
|
|
|
|
log.Debug("Removing XenCenterUUID failed", e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 03:06:35 +02:00
|
|
|
|
public void RunAsync()
|
2020-09-17 22:43:39 +02:00
|
|
|
|
{
|
|
|
|
|
RunAsync(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RunAsync(SudoElevationResult sudoElevationResult)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
AuditLogStarted();
|
2022-04-05 12:52:32 +02:00
|
|
|
|
ThreadPool.QueueUserWorkItem(RunWorkerThread, sudoElevationResult);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use this function to run this action non-async, but do the appropriate tidy up code.
|
|
|
|
|
/// If a session is passed in, which it always should be if called from another Action,
|
|
|
|
|
/// use that session for the action: it is then the responsibility of the calling function
|
|
|
|
|
/// to make sure the session has the appropriate privileges and tidy it up afterwards.
|
|
|
|
|
/// </summary>
|
2022-04-05 12:52:32 +02:00
|
|
|
|
public void RunSync(Session session)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
RunWorkerThread(session);
|
|
|
|
|
if (Exception != null)
|
|
|
|
|
throw Exception;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void Run();
|
|
|
|
|
|
|
|
|
|
private void RunWorkerThread(object o)
|
|
|
|
|
{
|
|
|
|
|
StartedRunning = true;
|
|
|
|
|
if (Cancelled) // already cancelled before it's started
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-17 22:43:39 +02:00
|
|
|
|
if (o is Session session)
|
|
|
|
|
Session = session;
|
|
|
|
|
else if (o is SudoElevationResult ser)
|
|
|
|
|
{
|
|
|
|
|
sudoUsername = ser.ElevatedUsername;
|
|
|
|
|
sudoPassword = ser.ElevatedPassword;
|
|
|
|
|
Session = ser.ElevatedSession ?? NewSession();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2020-03-29 03:06:35 +02:00
|
|
|
|
SetSessionByRole(); //construct a new session and sudo it if necessary
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Run();
|
|
|
|
|
AuditLogSuccess();
|
|
|
|
|
MarkCompleted();
|
|
|
|
|
}
|
|
|
|
|
catch (CancelledException e)
|
|
|
|
|
{
|
|
|
|
|
Cancelled = true;
|
|
|
|
|
AuditLogCancelled();
|
|
|
|
|
MarkCompleted(e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-03-29 03:06:35 +02:00
|
|
|
|
if (e is Failure f && Connection != null && f.ErrorDescription[0] == Failure.RBAC_PERMISSION_DENIED)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Failure.ParseRBACFailure(f, Connection, Session ?? Connection.Session);
|
2020-03-29 03:06:35 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
log.Error(e);
|
|
|
|
|
AuditLogFailure();
|
|
|
|
|
MarkCompleted(e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Clean();
|
|
|
|
|
|
|
|
|
|
if (Exception != null)
|
|
|
|
|
CleanOnError();
|
|
|
|
|
|
|
|
|
|
if (o == null && Session != null && Session.IsElevatedSession)
|
|
|
|
|
{
|
|
|
|
|
// The session is a new, sudo-ed session: we need to log these ones out
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Session.logout();
|
|
|
|
|
}
|
2019-11-08 22:00:03 +01:00
|
|
|
|
catch (Failure f)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-11-08 22:00:03 +01:00
|
|
|
|
log.Debug("Session.logout() failed. ", f);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Session = null;
|
|
|
|
|
LogoutCancelSession();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 16:28:26 +01:00
|
|
|
|
protected void DestroyTask()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-17 03:07:43 +02:00
|
|
|
|
//Null or empty RelatedTask.opaque_ref can happen during an RBAC dry-run
|
|
|
|
|
if (Session == null || string.IsNullOrEmpty(Session.opaque_ref) ||
|
|
|
|
|
RelatedTask == null || string.IsNullOrEmpty(RelatedTask.opaque_ref))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-29 03:06:35 +02:00
|
|
|
|
PerformSilentTaskOp(() => Task.destroy(Session, RelatedTask));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
RelatedTask = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 00:07:03 +01:00
|
|
|
|
public void PollToCompletion(double start = 0, double finish = 100, bool suppressFailures = false)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-17 03:07:43 +02:00
|
|
|
|
//Null or empty RelatedTask.opaque_ref can happen during an RBAC dry-run
|
|
|
|
|
if (RelatedTask == null || string.IsNullOrEmpty(RelatedTask.opaque_ref))
|
2021-03-09 01:31:57 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2020-12-29 16:28:26 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
|
int lastDebug = 0;
|
|
|
|
|
log.InfoFormat("Started polling task {0}", RelatedTask.opaque_ref);
|
|
|
|
|
log.DebugFormat("Polling for action {0}", Description); //log once we start
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (XenAdminConfigManager.Provider.ForcedExiting && !SafeToExit)
|
|
|
|
|
throw new CancelledException();
|
|
|
|
|
|
|
|
|
|
//then log every 30sec
|
|
|
|
|
int currDebug = (int)(DateTime.Now - startTime).TotalSeconds / 30;
|
|
|
|
|
if (currDebug > lastDebug)
|
|
|
|
|
{
|
|
|
|
|
lastDebug = currDebug;
|
|
|
|
|
log.DebugFormat("Polling for action {0}", Description);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 00:07:03 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (Poll(start, finish))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (suppressFailures)
|
|
|
|
|
break;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 16:28:26 +01:00
|
|
|
|
Thread.Sleep(900);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
DestroyTask();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Polls task and returns whether it is completed
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool Poll(double start, double finish)
|
|
|
|
|
{
|
|
|
|
|
Session session = Session;
|
|
|
|
|
Task task;
|
|
|
|
|
Result = "";
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
task = (Task)DoWithSessionRetry(ref session, (Task.TaskGetRecordOp)Task.get_record,
|
|
|
|
|
RelatedTask.opaque_ref);
|
|
|
|
|
}
|
|
|
|
|
catch (Failure exn)
|
|
|
|
|
{
|
|
|
|
|
if (exn.ErrorDescription.Count > 1 &&
|
|
|
|
|
exn.ErrorDescription[0] == Failure.HANDLE_INVALID &&
|
|
|
|
|
exn.ErrorDescription[1] == "task")
|
|
|
|
|
{
|
|
|
|
|
log.WarnFormat("Invalid task handle {0} - task is finished.", RelatedTask.opaque_ref);
|
|
|
|
|
PercentComplete = (int)finish;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Session = session;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 01:31:57 +01:00
|
|
|
|
PercentComplete = (int)(start + task.progress * (finish - start));
|
2020-12-29 16:28:26 +01:00
|
|
|
|
|
|
|
|
|
switch (task.status)
|
|
|
|
|
{
|
|
|
|
|
case task_status_type.failure:
|
|
|
|
|
if (task.error_info.Length > 1 &&
|
|
|
|
|
task.error_info[0] == Failure.HANDLE_INVALID &&
|
|
|
|
|
task.error_info[1] == "task")
|
|
|
|
|
{
|
|
|
|
|
log.WarnFormat("Invalid task handle {0} - task is finished.", RelatedTask.opaque_ref);
|
|
|
|
|
PercentComplete = (int)finish;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.WarnFormat("Task {0} failed: {1}", RelatedTask.opaque_ref,
|
|
|
|
|
task.error_info.Length > 0 ? task.error_info[0] : "Unknown failure");
|
|
|
|
|
throw new Failure(task.error_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case task_status_type.success:
|
|
|
|
|
log.InfoFormat("Task {0} finished successfully", RelatedTask.opaque_ref);
|
|
|
|
|
if (task.result != "") // Work around CA-6597
|
|
|
|
|
{
|
2021-03-30 16:02:59 +02:00
|
|
|
|
try
|
2021-03-08 17:47:10 +01:00
|
|
|
|
{
|
2021-03-30 16:02:59 +02:00
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
doc.LoadXml(task.result);
|
|
|
|
|
var nodes = doc.GetElementsByTagName("value");
|
|
|
|
|
|
2021-10-04 16:09:42 +02:00
|
|
|
|
if (nodes.Count > 0)
|
2021-03-30 16:02:59 +02:00
|
|
|
|
{
|
2021-10-04 16:09:42 +02:00
|
|
|
|
Result = nodes[0].InnerText;
|
2021-03-30 16:02:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch //CA-352946
|
|
|
|
|
{
|
|
|
|
|
log.WarnFormat("Task {0} result is not valid xml", RelatedTask.opaque_ref);
|
|
|
|
|
Result = task.result;
|
2021-03-08 17:47:10 +01:00
|
|
|
|
}
|
2020-12-29 16:28:26 +01:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case task_status_type.cancelled:
|
|
|
|
|
log.InfoFormat("Task {0} was cancelled", RelatedTask.opaque_ref);
|
|
|
|
|
throw new CancelledException();
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-29 03:06:35 +02:00
|
|
|
|
/// Finds the roles allowed to perform the API calls this action has listed as subject to RBAC checks.
|
|
|
|
|
/// If the current user's role is not on the list, show the Role Elevation Dialog so they can enter
|
|
|
|
|
/// credentials of a user with a permitted role.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
private void SetSessionByRole()
|
|
|
|
|
{
|
|
|
|
|
if (Connection == null
|
|
|
|
|
|| Connection.Session == null
|
|
|
|
|
|| Session != null) // We have been pre-seeded with a Session to use
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
RbacMethodList rbacMethodList;
|
|
|
|
|
|
2020-03-29 03:06:35 +02:00
|
|
|
|
if (Connection.Session.IsLocalSuperuser || XenAdminConfigManager.Provider.DontSudo)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
rbacMethodList = new RbacMethodList();
|
|
|
|
|
else
|
|
|
|
|
rbacMethodList = GetApiMethodsToRoleCheck;
|
|
|
|
|
|
|
|
|
|
if (rbacMethodList.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Session = NewSession();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 03:06:35 +02:00
|
|
|
|
bool ableToCompleteAction = Role.CanPerform(rbacMethodList, Connection, out var allowedRoles);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-03-29 03:06:35 +02:00
|
|
|
|
log.DebugFormat("Roles able to complete action: {0}", Role.FriendlyCSVRoleList(allowedRoles));
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.DebugFormat("Subject {0} has roles: {1}", Connection.Session.UserLogName(), Role.FriendlyCSVRoleList(Connection.Session.Roles));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (ableToCompleteAction)
|
|
|
|
|
{
|
|
|
|
|
log.Debug("Subject authorized to complete action");
|
|
|
|
|
Session = Connection.Session;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Debug("Subject not authorized to complete action, showing sudo dialog");
|
2020-03-29 03:06:35 +02:00
|
|
|
|
var result = getElevatedSession(allowedRoles, Connection, Title);
|
|
|
|
|
if (result == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
log.Debug("User cancelled sudo dialog, cancelling action");
|
|
|
|
|
throw new CancelledException();
|
|
|
|
|
}
|
2020-03-29 03:06:35 +02:00
|
|
|
|
|
|
|
|
|
sudoUsername = result.ElevatedUsername;
|
|
|
|
|
sudoPassword = result.ElevatedPassword;
|
|
|
|
|
Session = result.ElevatedSession;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SudoElevationResult
|
|
|
|
|
{
|
|
|
|
|
public readonly string ElevatedUsername;
|
|
|
|
|
public readonly string ElevatedPassword;
|
|
|
|
|
public readonly Session ElevatedSession;
|
|
|
|
|
|
2020-03-29 03:06:35 +02:00
|
|
|
|
public SudoElevationResult(string user, string password, Session session)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
ElevatedUsername = user;
|
|
|
|
|
ElevatedPassword = password;
|
|
|
|
|
ElevatedSession = session;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AddCommonAPIMethodsToRoleCheck()
|
|
|
|
|
{
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(Role.CommonTaskApiList);
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(Role.CommonSessionApiList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|