private static bool CheckServiceEnabled(HttpContext context, string apiVersion, string httpMethod) { bool isEnabled; switch (apiVersion) { case "1": isEnabled = WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRestfulv1); break; case "2": isEnabled = WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRestfulv2); break; case "file": isEnabled = (WebServiceSettings.IsEnabled(WebServiceSettings.ServiceFileUpload) && httpMethod.Is("POST")) || (WebServiceSettings.IsEnabled(WebServiceSettings.ServiceFileDownload) && httpMethod.Is("GET")); break; case "media": isEnabled = ((WebServiceSettings.IsEnabled(WebServiceSettings.ServiceMediaUpload) && httpMethod.Is("POST")) || (WebServiceSettings.IsEnabled(WebServiceSettings.ServiceMediaDownload) && httpMethod.Is("GET"))); break; case "handle": isEnabled = WebServiceSettings.IsEnabled(WebServiceSettings.ServiceHandleDownload); break; case "script": isEnabled = WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting); break; default: isEnabled = false; break; } if (isEnabled) { return(true); } const string disabledMessage = "The request could not be completed because the service is disabled."; context.Response.StatusCode = 403; context.Response.StatusDescription = disabledMessage; PowerShellLog.Error($"Attempt to call the {apiVersion} service failed as it is not enabled."); return(false); }
private static bool CheckServiceEnabled(HttpContext context, string serviceName) { var isEnabled = WebServiceSettings.IsEnabled(serviceName); if (isEnabled) { return(true); } var errorMessage = $"The request could not be completed because the {serviceName} service is disabled."; context.Response.StatusCode = 403; context.Response.StatusDescription = errorMessage; PowerShellLog.Warn(errorMessage); return(false); }
public NameValue[] ExecuteScript(string userName, string password, string script, string returnVariables) { if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting)) { return(new NameValue[0]); } if (!Login(userName, password)) { return(new[] { new NameValue() { Name = "login failed", Value = "login failed" } }); } PowerShellLog.Info($"Script executed through remoting by user: '******' in disposable session."); using (var scriptSession = ScriptSessionManager.NewSession(ApplicationNames.RemoteAutomation, false)) { scriptSession.ExecuteScriptPart(script); var result = new List <NameValue>(); if (scriptSession.Output.Count > 0) { result.Add(new NameValue { Name = "output", Value = scriptSession.Output.Select(p => p.Terminated ? p.Text + "\n" : p.Text).Aggregate( (current, next) => current + next) }); } result.AddRange( returnVariables.Split('|').Select(variable => new NameValue { Name = variable, Value = (scriptSession.GetVariable(variable) ?? string.Empty).ToString() })); return(result.ToArray()); } }
public bool UploadFile(string userName, string password, string filePath, byte[] fileContent, string database, string language) { if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting)) { return(false); } try { if (!Login(userName, password)) { return(false); } PowerShellLog.Info($"File '{filePath}' uploaded through remoting by user: '******'"); var dirName = (Path.GetDirectoryName(filePath) ?? string.Empty).Replace('\\', '/'); if (!dirName.StartsWith(Constants.MediaLibraryPath)) { dirName = Constants.MediaLibraryPath + (dirName.StartsWith("/") ? dirName : "/" + dirName); } var mco = new MediaCreatorOptions { Database = Factory.GetDatabase(database), Language = Language.Parse(language), Versioned = Settings.Media.UploadAsVersionableByDefault, Destination = $"{dirName}/{Path.GetFileNameWithoutExtension(filePath)}" }; var mc = new MediaCreator(); using (var stream = new MemoryStream(fileContent)) { mc.CreateFromStream(stream, Path.GetFileName(filePath), mco); } } catch (Exception ex) { PowerShellLog.Error("Error during uploading file using PowerShell web service", ex); return(false); } return(true); }
public string ExecuteScriptBlockinSite2(string userName, string password, string script, string cliXmlArgs, string siteName, string sessionId) { if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting)) { return(string.Empty); } if (!Login(userName, password)) { return("<Objs xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><Obj RefId=\"0\"><S>login failed</S></Obj></Objs>"); } PowerShellLog.Info($"Script executed in session {sessionId} through remoting by user: '******'"); var scriptSession = ScriptSessionManager.GetSession(sessionId, ApplicationNames.RemoteAutomation, false); Sitecore.Context.SetActiveSite(siteName); if (!String.IsNullOrEmpty(cliXmlArgs)) { scriptSession.SetVariable("cliXmlArgs", cliXmlArgs); scriptSession.ExecuteScriptPart("$params = ConvertFrom-CliXml -InputObject $cliXmlArgs", false, true); script = script.TrimEnd(' ', '\t', '\n'); } var outObjects = scriptSession.ExecuteScriptPart(script, false, false, false); if (scriptSession.LastErrors != null && scriptSession.LastErrors.Any()) { outObjects.AddRange(scriptSession.LastErrors); } scriptSession.SetVariable("results", outObjects); scriptSession.Output.Clear(); scriptSession.ExecuteScriptPart("ConvertTo-CliXml -InputObject $results"); var result = scriptSession.Output.Select(p => p.Text).Aggregate((current, next) => current + next); if (String.IsNullOrEmpty(sessionId)) { ScriptSessionManager.RemoveSession(scriptSession); } return(result); }
public string DisposeScriptSession(string userName, string password, string sessionId) { if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting)) { return(string.Empty); } if (!Login(userName, password)) { return("login failed"); } PowerShellLog.Info($"Session '{sessionId}' disposed by user: '******'"); if (ScriptSessionManager.GetSessionIfExists(sessionId) is ScriptSession session) { ScriptSessionManager.RemoveSession(session); return("removed"); } return("not found"); }
public byte[] DownloadFile(string userName, string password, string filePath, string database, string language) { if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRemoting)) { return(new byte[0]); } try { if (!Login(userName, password)) { return(Encoding.ASCII.GetBytes("login failed")); } PowerShellLog.Info($"File '{filePath}' downloaded through remoting by user: '******'"); var dirName = (Path.GetDirectoryName(filePath) ?? string.Empty).Replace('\\', '/'); if (!dirName.StartsWith(Constants.MediaLibraryPath)) { dirName = Constants.MediaLibraryPath + (dirName.StartsWith("/") ? dirName : "/" + dirName); } var itemname = dirName + "/" + Path.GetFileNameWithoutExtension(filePath); var db = Factory.GetDatabase(database); var item = (MediaItem)db.GetItem(itemname); using (var stream = item.GetMediaStream()) { var result = new byte[stream.Length]; stream.Read(result, 0, (int)stream.Length); return(result); } } catch (Exception ex) { PowerShellLog.Error("Error during uploading file using PowerShell web service", ex); return(new byte[0]); } }
private static bool CheckServiceEnabled(string apiVersion, string httpMethod) { var isEnabled = true; const string disabledMessage = "The request could not be completed because the service is disabled."; switch (apiVersion) { case "1": if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRestfulv1)) { HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.StatusDescription = disabledMessage; isEnabled = false; } break; case "2": if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceRestfulv2)) { HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.StatusDescription = disabledMessage; isEnabled = false; } break; case "file": if ((WebServiceSettings.IsEnabled(WebServiceSettings.ServiceFileUpload) && httpMethod.Is("POST")) || (WebServiceSettings.IsEnabled(WebServiceSettings.ServiceFileDownload) && httpMethod.Is("GET"))) { break; } HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.StatusDescription = disabledMessage; isEnabled = false; break; case "media": if ((WebServiceSettings.IsEnabled(WebServiceSettings.ServiceMediaUpload) && httpMethod.Is("POST")) || (WebServiceSettings.IsEnabled(WebServiceSettings.ServiceMediaDownload) && httpMethod.Is("GET"))) { break; } HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.StatusDescription = disabledMessage; isEnabled = false; break; case "handle": if (!WebServiceSettings.IsEnabled(WebServiceSettings.ServiceHandleDownload)) { HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.StatusDescription = disabledMessage; isEnabled = false; } break; default: HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.StatusDescription = disabledMessage; isEnabled = false; break; } return(isEnabled); }