From 2c83038ea831bfdb38e7af90068b7ad7e2f88a56 Mon Sep 17 00:00:00 2001 From: kaso17 Date: Thu, 7 Dec 2017 14:35:50 +0100 Subject: [PATCH] Mono: check if the certificate store was initialized --- src/Jackett.Common/Engine.cs | 2 + src/Jackett.Common/Plumbing/JackettModule.cs | 19 +++++--- src/Jackett/Services/ServerService.cs | 49 ++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/Jackett.Common/Engine.cs b/src/Jackett.Common/Engine.cs index 5432f2029..450985206 100644 --- a/src/Jackett.Common/Engine.cs +++ b/src/Jackett.Common/Engine.cs @@ -21,6 +21,8 @@ namespace Jackett { public class Engine { + public static Type WebClientType; + private static IContainer container = null; private static bool _automapperInitialised = false; diff --git a/src/Jackett.Common/Plumbing/JackettModule.cs b/src/Jackett.Common/Plumbing/JackettModule.cs index e12e08411..feebbce31 100644 --- a/src/Jackett.Common/Plumbing/JackettModule.cs +++ b/src/Jackett.Common/Plumbing/JackettModule.cs @@ -61,32 +61,37 @@ namespace Jackett.Common.Plumbing switch (_runtimeSettings.ClientOverride) { case "httpclient": - builder.RegisterType().As(); + RegisterWebClient(builder); break; case "httpclient2": - builder.RegisterType().As(); + RegisterWebClient(builder); break; case "safecurl": - builder.RegisterType().As(); + RegisterWebClient(builder); break; case "libcurl": - builder.RegisterType().As(); + RegisterWebClient(builder); break; case "automatic": default: if (System.Environment.OSVersion.Platform != PlatformID.Unix) { - builder.RegisterType().As(); + RegisterWebClient(builder); break; } var usehttpclient = DetectMonoCompatabilityWithHttpClient(); if (usehttpclient) - builder.RegisterType().As(); + RegisterWebClient(builder); else - builder.RegisterType().As(); + RegisterWebClient(builder); break; } + } + private void RegisterWebClient(ContainerBuilder builder) + { + Engine.WebClientType = typeof(WebClientType); + builder.RegisterType().As(); } private ServerConfig BuildServerConfig(IComponentContext ctx) diff --git a/src/Jackett/Services/ServerService.cs b/src/Jackett/Services/ServerService.cs index 8a7074d55..81c1528bd 100644 --- a/src/Jackett/Services/ServerService.cs +++ b/src/Jackett/Services/ServerService.cs @@ -19,6 +19,8 @@ using System.Threading; using System.Web; using Jackett.Services.Interfaces; using Jacket.Common; +using System.Collections; +using System.Text.RegularExpressions; namespace Jackett.Services { @@ -204,6 +206,53 @@ namespace Jackett.Services logger.Error(e.Message + " Most likely the mono-locale-extras package is not installed."); Engine.Exit(2); } + + if (Engine.WebClientType == typeof(HttpWebClient) || Engine.WebClientType == typeof(HttpWebClient2)) + { + // check if the certificate store was initialized using Mono.Security.X509.X509StoreManager.TrustedRootCertificates.Count + try + { + var monoSecurity = Assembly.Load("Mono.Security"); + Type monoX509StoreManager = monoSecurity.GetType("Mono.Security.X509.X509StoreManager"); + if (monoX509StoreManager != null) + { + var TrustedRootCertificatesProperty = monoX509StoreManager.GetProperty("TrustedRootCertificates"); + var TrustedRootCertificates = (ICollection)TrustedRootCertificatesProperty.GetValue(null); + + logger.Info("TrustedRootCertificates count: " + TrustedRootCertificates.Count); + + if (TrustedRootCertificates.Count == 0) + { + var CACertificatesFiles = new string[] { + "/etc/ssl/certs/ca-certificates.crt", // Debian based + "/etc/pki/tls/certs/ca-bundle.c", // RedHat based + "/etc/ssl/ca-bundle.pem", // SUSE + }; + + var notice = "The mono certificate store is not initialized.
\n"; + var logSpacer = " "; + var CACertificatesFile = CACertificatesFiles.Where(f => File.Exists(f)).FirstOrDefault(); + var CommandRoot = "curl -sS https://curl.haxx.se/ca/cacert.pem | cert-sync /dev/stdin"; + var CommandUser = "curl -sS https://curl.haxx.se/ca/cacert.pem | cert-sync --user /dev/stdin"; + if (CACertificatesFile != null) + { + CommandRoot = "cert-sync " + CACertificatesFile; + CommandUser = "cert-sync --user " + CACertificatesFile; + } + notice += logSpacer + "Please run the following command as root:
\n"; + notice += logSpacer + "
" + CommandRoot + "

\n"; + notice += logSpacer + "If you don't have root access, please run the following command as the jackett user (" + Environment.UserName + "):
\n"; + notice += logSpacer + "
" + CommandUser + "
"; + _notices.Add(notice); + logger.Error(Regex.Replace(notice, "<.*?>", String.Empty)); + } + } + } + catch (Exception e) + { + logger.Error(e, "Error while chekcing the mono certificate store"); + } + } } } catch (Exception e)