Ejemplo n.º 1
0
        public override void DoJob()
        {
            if (Application.CurrentConfiguration.Cluster.Active == false)
            {
                return;
            }
            var clusterStatus = Application.ClusterChecklist;

            if (clusterStatus == null)
            {
                return;
            }
            if (clusterStatus.Length < 1)
            {
                return;
            }
            var nodesKnownHosts = new List <KnownHost>();

            for (var i = 0; i < clusterStatus.Length; i++)
            {
                var nodeIPs     = clusterStatus[i].DiscoveredIpsReach;
                var nodeName    = clusterStatus[i].Hostname;
                var commonNames = new string[] {
                    CommonString.Append(nodeName, "int", i.ToString())
                };
                for (var p = 0; p < nodeIPs.Length; p++)
                {
                    var knownHost = new KnownHost()
                    {
                        IpAddr      = nodeIPs[p].IpAddress,
                        CommonNames = commonNames
                    };
                    nodesKnownHosts.Add(knownHost);
                }
            }
            var currentKnownHosts = Application.CurrentConfiguration.Network.KnownHosts.ToList();

            foreach (var nodeHost in nodesKnownHosts)
            {
                //i casi possono essere tre:
                //  1) l'ip non è presente nella CurrentConfiguration   -> aggiungo il nuovo KnownHost
                //  2) l'ip è presente ma i CommonNames sono differenti -> aggiorno solamente i CommonNames del KnownHost corrispondente
                //  3) l'ip è presente e i CommonNames coincidono       -> non faccio nulla
                if (!currentKnownHosts.Any(_ => CommonString.AreEquals(_.IpAddr, nodeHost.IpAddr) == true))
                {
                    currentKnownHosts.Add(nodeHost);
                }
                else
                {
                    var existingCn = currentKnownHosts.FirstOrDefault(_ => CommonString.AreEquals(_.IpAddr, nodeHost.IpAddr) == true).CommonNames;
                    if (CommonString.AreEquals(CommonString.Build(existingCn), CommonString.Build(nodeHost.CommonNames)) == false)
                    {
                        currentKnownHosts.FirstOrDefault(_ => CommonString.AreEquals(_.IpAddr, nodeHost.IpAddr) == true).CommonNames = nodeHost.CommonNames;
                    }
                }
            }
            Application.CurrentConfiguration.Network.KnownHosts = currentKnownHosts.ToArray();
            ConfigRepo.Save();
            Dns.Set();
        }
Ejemplo n.º 2
0
 public override string ToString()
 {
     if (this == null)
     {
         return(string.Empty);
     }
     return(CommonString.Append(this.Id, CommonString.Build(this.Lower)));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Applica la configurazione current al file /etc/nftables.conf e riavvia il servizio
        /// </summary>
        public static void Apply()
        {
            var current = Application.CurrentConfiguration.Services.Firewall;

            if (current == null)
            {
                return;
            }
            if (current.Tables.Length < 1)
            {
                return;
            }

            #region [    nftables.conf generation    ]
            var lines = new List <string> {
                "flush ruleset;",
                "flush ruleset;",
                "flush ruleset;",
                "flush ruleset;",
                "flush ruleset;"
            };

            for (var t = 0; t < current.Tables.Length; t++)
            {
                var firewalTable = current.Tables[t];
                lines.Add(CommonString.Append("table ", firewalTable.Family, " ", firewalTable.Name, " {"));
                for (var s = 0; s < firewalTable.Sets.Length; s++)
                {
                    var set = firewalTable.Sets[s];
                    lines.Add(CommonString.Append("    set ", set.Name, " {"));
                    lines.Add(CommonString.Append("        type ", set.Type));
                    lines.Add(CommonString.Append("        elements = { ", CommonString.Build(set.Elements, ", "), " }"));
                    lines.Add("    }");
                }
                lines.Add("");
                for (var c = 0; c < firewalTable.Chains.Length; c++)
                {
                    var chain = firewalTable.Chains[c];
                    lines.Add(CommonString.Append("    chain ", chain.Hook, " {"));
                    lines.Add(CommonString.Append("        type ", chain.Type, " hook ", chain.Hook, " priority 0; policy drop;"));

                    for (var r = 0; r < chain.Rules.Length; r++)
                    {
                        var rule = chain.Rules[r];
                        lines.Add(CommonString.Append("              ", rule.Match, " ", rule.MatchArgument, " ", rule.Object, " ", rule.Jump).Replace("  ", " "));
                    }
                    var logPrefix = CommonString.Append("\"a=T", firewalTable.Name, "C", chain.Hook, "\"");
                    lines.Add(CommonString.Append("        log prefix ", logPrefix, " accept"));
                    lines.Add("    }");
                }
                lines.Add("}");
            }

            File.WriteAllLines(nftablesFile, lines);
            #endregion
            Start();
        }
Ejemplo n.º 4
0
        public static void Apply()
        {
            var options = Application.CurrentConfiguration.Services.Bind;

            if (options == null)
            {
                return;
            }
            Stop();
            #region [    named.conf generation    ]
            if (File.Exists(MainFilePath))
            {
                if (File.Exists(MainFilePathBackup))
                {
                    File.Delete(MainFilePathBackup);
                }
                File.Copy(MainFilePath, MainFilePathBackup);
            }
            var lines = new List <string> {
                "options {"
            };
            lines.Add($"notify {options.Notify};");
            lines.Add($"max-cache-size {options.MaxCacheSize};");
            lines.Add($"max-cache-ttl {options.MaxCacheTtl};");
            lines.Add($"max-ncache-ttl {options.MaxNcacheTtl};");
            if (options.Forwarders.Any())
            {
                lines.Add("forwarders {");
                foreach (var fwd in options.Forwarders)
                {
                    lines.Add($"{fwd};");
                }
                lines.Add("}");
            }
            lines.Add($"forwarders {{ {CommonString.Build(options.Forwarders.ToArray(), "; ")} }}");
            lines.Add($"allow-notify {{ {CommonString.Build(options.AllowNotify.ToArray(), "; ")} }}");
            lines.Add($"allow-transfer {{ {CommonString.Build(options.AllowTransfer.ToArray(), "; ")} }}");
            lines.Add($"recursion {options.Recursion};");
            lines.Add($"transfer-format {options.TransferFormat};");
            lines.Add($"query-source address {options.QuerySourceAddress} port {options.QuerySourcePort};");
            lines.Add($"version {options.Version};");
            lines.Add($"allow-query {{ {CommonString.Build(options.AllowQuery.ToArray(), "; ")} }}");
            lines.Add($"allow-recursion {{ {CommonString.Build(options.AllowRecursion.ToArray(), "; ")} }}");
            lines.Add($"ixfr-from-differences {options.IxfrFromDifferences};");
            lines.Add($"listen-on-v6 {{ {CommonString.Build(options.ListenOnV6.ToArray(), "; ")} }}");
            lines.Add($"listen-on port 53 {{ {CommonString.Build(options.ListenOnPort53.ToArray(), "; ")} }}");
            lines.Add($"dnssec-enable {options.DnssecEnabled};");
            lines.Add($"dnssec-validation {options.DnssecValidation};");
            lines.Add($"dnssec-lookaside {options.DnssecLookaside};");
            lines.Add($"auth-nxdomain {options.AuthNxdomain};");
            lines.Add("};");
            lines.Add("");

            lines.Add($"key \"{options.KeyName}\" {{");
            lines.Add("algorithm hmac-md5;");
            lines.Add($"secret \"{options.KeySecret}\";");
            lines.Add("};");
            lines.Add("");

            lines.Add(
                options.ControlKeys.Any()
                    ? $"controls {{ inet {options.ControlIp} port {options.ControlPort} allow {{ {CommonString.Build(options.ControlAllow.ToArray(), "; ")} }} keys {{ {CommonString.Build(options.ControlKeys.Select(_ => "\"" + _ + "\"").ToArray(), ";")} }}"
                    : $"controls {{ inet {options.ControlIp} port {options.ControlPort} allow {{ {CommonString.Build(options.ControlAllow.ToArray(), "; ")} }}");

            lines.Add("");

            foreach (var acl in options.AclList)
            {
                lines.Add($"acl {acl.Name} {{ {CommonString.Build(acl.InterfaceList.ToArray(), "; ")} }}");
            }
            lines.Add("");

            lines.Add("logging {");
            lines.Add("channel syslog {");
            lines.Add("syslog daemon;");
            lines.Add($"severity {options.SyslogSeverity};");
            lines.Add($"print-category {options.SyslogPrintCategory};");
            lines.Add($"print-severity {options.SyslogPrintSeverity};");
            lines.Add($"print-time {options.SyslogPrintTime};");
            lines.Add("};");
            lines.Add("category client { syslog };");
            lines.Add("category config { syslog };");
            lines.Add("category database { syslog };");
            lines.Add("category default { syslog };");
            lines.Add("category delegation-only { syslog };");
            lines.Add("category dispatch { syslog };");
            lines.Add("category dnssec { syslog };");
            lines.Add("category general { syslog };");
            lines.Add("category lame-servers { syslog };");
            lines.Add("category network { syslog };");
            lines.Add("category notify { syslog };");
            lines.Add("category queries { syslog };");
            lines.Add("category resolver { syslog };");
            lines.Add("category rpz { syslog };");
            lines.Add("category rate-limit { syslog };");
            lines.Add("category security { syslog };");
            lines.Add("category unmatched { syslog };");
            lines.Add("category update { syslog };");
            lines.Add("category update-security { syslog };");
            lines.Add("category xfer-in { syslog };");
            lines.Add("category xfer-out { syslog };");
            lines.Add("};");
            lines.Add("");

            lines.Add("trusted-keys {");
            lines.Add(options.TrustedKeys);
            lines.Add("};");
            lines.Add("");

            //var zones = options.Zones;
            //foreach(var zone in zones) {
            //    lines.Add($"zone \"{zone.Name}\" {{");
            //    lines.Add($"type {zone.Type};");
            //    lines.Add($"file \"{zone.File}\";");
            //    if(!string.IsNullOrEmpty(zone.SerialUpdateMethod)) {
            //        lines.Add($"serial-update-method {zone.SerialUpdateMethod};");
            //    }
            //    if(zone.AllowUpdate.Any()) {
            //        lines.Add($"allow-update {{ {CommonString.Build(zone.AllowUpdate.ToArray(), "; ")} }}");
            //    }
            //    if(zone.AllowQuery.Any()) {
            //        lines.Add($"allow-query {{ {CommonString.Build(zone.AllowQuery.ToArray(), "; ")} }}");
            //    }
            //    if(zone.AllowTransfer.Any()) {
            //        lines.Add($"allow-transfer {{ {CommonString.Build(zone.AllowTransfer.ToArray(), "; ")} }}");
            //        lines.Add($"allow-transfer {zone.AllowTransfer};");
            //    }
            //    lines.Add("};");
            //}
            //lines.Add("");

            lines.Add("include \"/etc/bind/master/blackhole.zones\";");
            File.WriteAllLines(MainFilePath, lines);

            var keyLines = new List <string> {
                $"key \"{options.KeyName}\" {{",
                "algorithm hmac-md5;",
                $"secret \"{options.KeySecret}\";",
                "};",
                ""
            };
            File.WriteAllLines(RndcKeyFile, keyLines);

            var rndcConfLines = new List <string> {
                $"key \"{options.KeyName}\" {{",
                "algorithm hmac-md5;",
                $"secret \"{options.KeySecret}\";",
                "};",
                "",
                "options {",
                $"default-key \"{options.KeyName}\";",
                $"default-server \"{options.ControlIp}\";",
                $"default-port \"{options.ControlPort}\";",
                "};"
            };
            File.WriteAllLines(RndcConfFile, rndcConfLines);

            #endregion
            Start();
            RndcReconfig();
        }
Ejemplo n.º 5
0
 public override string ToString()
 {
     return(CommonString.Append(this.Id, this.HardwareConfiguration.ToString(), this.PrimaryAddressConfiguration.ToString(), CommonString.Build(this.SecondaryAddressConfigurations.Select(_ => _.ToString()).ToArray())));
 }
Ejemplo n.º 6
0
 public override string ToString()
 {
     return(CommonString.Append(this.IpAddr, CommonString.Build(this.CommonNames)));
 }
Ejemplo n.º 7
0
 public override string ToString()
 {
     return(CommonString.Append(CommonString.Build(this.Nameserver), this.Search, this.Domain));
 }
Ejemplo n.º 8
0
        public static void Set()
        {
            var currentResolv = Application.CurrentConfiguration.Network.KnownDns;
            var runningResolv = Application.RunningConfiguration.Network.KnownDns;

            if (CommonString.AreEquals(currentResolv.ToString(), runningResolv.ToString()) == false)
            {
                if (File.Exists(etcResolv))
                {
                    File.Copy(etcResolv, etcResolvBackup, true);
                }
                var nameserverLines = new string[currentResolv.Nameserver.Length];
                for (var i = 0; i < currentResolv.Nameserver.Length; i++)
                {
                    nameserverLines[i] = CommonString.Append(nameserver, " ", currentResolv.Nameserver[i]);
                }
                File.WriteAllLines(etcResolv, nameserverLines);
                if (!string.IsNullOrEmpty(currentResolv.Search))
                {
                    var newLines = new string[] { CommonString.Append(search, " ", currentResolv.Search) };
                    File.AppendAllLines(etcResolv, newLines);
                }
                if (!string.IsNullOrEmpty(currentResolv.Domain))
                {
                    var newLines = new string[] { CommonString.Append(domain, " ", currentResolv.Domain) };
                    File.AppendAllLines(etcResolv, newLines);
                }
            }

            var currentHosts = CommonArray.Merge(DefaultHosts, Application.CurrentConfiguration.Network.KnownHosts);
            var runningHosts = Application.RunningConfiguration.Network.KnownHosts;

            if (currentHosts.Select(_ => _.ToString()).SequenceEqual(runningHosts.Select(_ => _.ToString())) == false)
            {
                if (File.Exists(etcHosts))
                {
                    File.Copy(etcHosts, etcHostsBackup, true);
                }
                var lines = new string[currentHosts.Length];
                for (var i = 0; i < currentHosts.Length; i++)
                {
                    lines[i] = CommonString.Append(currentHosts[i].IpAddr, " ", CommonString.Build(currentHosts[i].CommonNames, ' '));
                }
                File.WriteAllLines(etcHosts, lines);
            }

            var currentNetworks = CommonArray.Merge(DefaultNetworks, Application.CurrentConfiguration.Network.KnownNetworks);
            var runningNetworks = Application.RunningConfiguration.Network.KnownNetworks;

            if (currentNetworks.Select(_ => _.ToString()).SequenceEqual(runningNetworks.Select(_ => _.ToString())) == false)
            {
                if (File.Exists(etcNetworks))
                {
                    File.Copy(etcNetworks, etcNetworksBackup, true);
                }
                var lines = new string[currentNetworks.Length];
                for (var i = 0; i < currentNetworks.Length; i++)
                {
                    lines[i] = CommonString.Append(currentNetworks[i].Label, " ", currentNetworks[i].NetAddr);
                }
                File.WriteAllLines(etcNetworks, lines);
            }
        }