Ejemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="networkdevice">A string indicating starting point for
 /// network probe</param>
 protected RpcDHCPServer(DHCPConfig config)
     : base(config)
 {
     LocalIP = new byte[4];
       BaseIP.CopyTo(LocalIP, 0);
       LocalIP[3] = 2;
 }
Ejemplo n.º 2
0
        /// <summary>We need to get the DHCPConfig as soon as possible so that we
        /// can allocate static addresses, this method helps us do that.</summary>
        protected override void GetDhcpConfig()
        {
            if (Interlocked.Exchange(ref _lock, 1) == 1)
            {
                return;
            }

            WaitCallback wcb = delegate(object o) {
                bool       success     = false;
                DHCPConfig dhcp_config = null;
                try {
                    dhcp_config = DhtDhcpServer.GetDhcpConfig(AppNode.Dht, _ipop_config.IpopNamespace);
                    success     = true;
                } catch (Exception e) {
                    ProtocolLog.WriteIf(IpopLog.DhcpLog, e.ToString());
                }

                if (success)
                {
                    lock (_sync) {
                        _dhcp_config = dhcp_config;
                        _dhcp_server = new DhtDhcpServer(AppNode.Dht, _dhcp_config, _ipop_config.EnableMulticast);
                    }
                }
                base.GetDhcpConfig();

                Interlocked.Exchange(ref _lock, 0);
            };

            ThreadPool.QueueUserWorkItem(wcb);
        }
Ejemplo n.º 3
0
        public override int Parse(string[] args)
        {
            if (base.Parse(args) != 0)
            {
                return(-1);
            }

            if (_ipop_config_path == string.Empty || !File.Exists(_ipop_config_path))
            {
                _error_message = "Missing IpopConfig";
                return(-1);
            }

            try {
                Validator.Validate(_ipop_config_path, IPOP_XSD);
                _ipop_config      = Utils.ReadConfig <IpopConfig>(_ipop_config_path);
                _ipop_config.Path = _ipop_config_path;
            } catch (Exception e) {
                _error_message = "Invalid IpopConfig file:" + e.Message;
                return(-1);
            }

            if (_dhcp_config_path != string.Empty)
            {
                if (!File.Exists(_dhcp_config_path))
                {
                    _error_message = "No such DhtIpop file";
                    return(-1);
                }

                try {
                    Validator.Validate(_dhcp_config_path, DHCP_XSD);
                    _dhcp_config = Utils.ReadConfig <DHCPConfig>(_dhcp_config_path);
                } catch (Exception e) {
                    _error_message = "Invalid DhcpConfig file: " + e.Message;
                    return(-1);
                }

                if (!_dhcp_config.Namespace.Equals(_ipop_config.IpopNamespace))
                {
                    _error_message = "IpopConfig.Namespace isn't the same as DHCPConfig.Namespace";
                    return(-1);
                }
            }

            if (_node_config.PrivateNodeConfig != null && _node_config.PrivateNodeConfig.Enabled)
            {
                _sp = _node_config.PrivateNodeConfig.Security;
            }
            else
            {
                _sp = _node_config.Security;
            }

            _group_vpn = _sp.Enabled &&
                         _ipop_config.GroupVPN.Enabled &&
                         (_ipop_config.EndToEndSecurity || _sp != _node_config.Security);
            return(0);
        }
Ejemplo n.º 4
0
        ///<summary>Creates a DhtIpopNode.</summary>
        /// <param name="NodeConfig">NodeConfig object</param>
        /// <param name="IpopConfig">IpopConfig object</param>
        public DhtIpopNode(NodeConfig node_config, IpopConfig ipop_config,
                           DHCPConfig dhcp_config) : base(node_config, ipop_config, dhcp_config)
        {
            DhtAddressResolver dar = new DhtAddressResolver(AppNode.Dht, _ipop_config.IpopNamespace);

            Shutdown.OnExit  += dar.Stop;
            _address_resolver = dar;

            _connected = false;
            AppNode.Node.StateChangeEvent += StateChangeHandler;
            StateChangeHandler(AppNode.Node, AppNode.Node.ConState);
        }
Ejemplo n.º 5
0
    public static ManagedDhcpServer GetManagedDhcpServer(MemBlock ip, MemBlock netmask) {
      DHCPConfig config = new DHCPConfig();
      config.LeaseTime = 3200;
      config.Netmask = Utils.MemBlockToString(netmask, '.');
      config.IPBase = Utils.MemBlockToString(ip, '.');

      config.ReservedIPs = new DHCPConfig.ReservedIP[1];
      config.ReservedIPs[0] = new DHCPConfig.ReservedIP();

      byte[] local_ip = new byte[4];
      ip.CopyTo(local_ip, 0);
      local_ip[3] = 2;

      config.ReservedIPs[0].IPBase = Utils.BytesToString(local_ip, '.');
      config.ReservedIPs[0].Mask = "255.255.255.255";

      return new ManagedDhcpServer(config);
    }
Ejemplo n.º 6
0
        public static ManagedDhcpServer GetManagedDhcpServer(MemBlock ip, MemBlock netmask)
        {
            DHCPConfig config = new DHCPConfig();

            config.LeaseTime = 3200;
            config.Netmask   = Utils.MemBlockToString(netmask, '.');
            config.IPBase    = Utils.MemBlockToString(ip, '.');

            config.ReservedIPs    = new DHCPConfig.ReservedIP[1];
            config.ReservedIPs[0] = new DHCPConfig.ReservedIP();

            byte[] local_ip = new byte[4];
            ip.CopyTo(local_ip, 0);
            local_ip[3] = 2;

            config.ReservedIPs[0].IPBase = Utils.BytesToString(local_ip, '.');
            config.ReservedIPs[0].Mask   = "255.255.255.255";

            return(new ManagedDhcpServer(config));
        }
Ejemplo n.º 7
0
        public static DHCPConfig GetDhcpConfig(IDht dht, string ipop_namespace)
        {
            byte[]      ns_key  = Encoding.UTF8.GetBytes("dhcp:" + ipop_namespace);
            Hashtable[] results = dht.Get(ns_key);

            if (results.Length == 0)
            {
                throw new Exception("Namespace does not exist: " + ipop_namespace);
            }

            string result = Encoding.UTF8.GetString((byte[])results[0]["value"]);

            XmlSerializer serializer   = new XmlSerializer(typeof(DHCPConfig));
            TextReader    stringReader = new StringReader(result);
            DHCPConfig    dhcp_config  = (DHCPConfig)serializer.Deserialize(stringReader);

            if (!ipop_namespace.Equals(dhcp_config.Namespace))
            {
                throw new Exception(String.Format("Namespace mismatch, expected {0}, got {1}",
                                                  ipop_namespace, dhcp_config.Namespace));
            }
            return(dhcp_config);
        }
Ejemplo n.º 8
0
        public override int Parse(string[] args)
        {
            if(base.Parse(args) != 0) {
            return -1;
              }

              if(_ipop_config_path == string.Empty || !File.Exists(_ipop_config_path)) {
            _error_message = "Missing IpopConfig";
            return -1;
              }

              try {
            Validator.Validate(_ipop_config_path, IPOP_XSD);
            _ipop_config = Utils.ReadConfig<IpopConfig>(_ipop_config_path);
            _ipop_config.Path = _ipop_config_path;
              } catch (Exception e) {
            _error_message = "Invalid IpopConfig file:" + e.Message;
            return -1;
              }

              if(_dhcp_config_path != string.Empty) {
            if(!File.Exists(_dhcp_config_path)) {
              _error_message = "No such DhtIpop file";
              return -1;
            }

            try {
              Validator.Validate(_dhcp_config_path, DHCP_XSD);
              _dhcp_config = Utils.ReadConfig<DHCPConfig>(_dhcp_config_path);
            } catch(Exception e) {
              _error_message = "Invalid DhcpConfig file: " + e.Message;
              return -1;
            }

            if(!_dhcp_config.Namespace.Equals(_ipop_config.IpopNamespace)) {
              _error_message = "IpopConfig.Namespace isn't the same as DHCPConfig.Namespace";
              return -1;
            }
              }

              _group_vpn = _node_config.Security.Enabled &&
            _ipop_config.GroupVPN.Enabled &&
            _ipop_config.EndToEndSecurity;
              return 0;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="networkdevice">A string indicating starting point for
 /// network probe</param>
 protected ManagedDhcpServer(DHCPConfig config) : base(config)
 {
     LocalIP = new byte[4];
     BaseIP.CopyTo(LocalIP, 0);
     LocalIP[3] = 2;
 }
Ejemplo n.º 10
0
 /**
  * <summary>Creates a DhtDhcpLeaseController for a specific namespace</summary>
  * <param name="dht">The dht object use to store lease information.</param>
  * <param name="config">The DHCPConfig used to define the Lease
  * parameters.</param>
  * <param name="EnableMulticast">Defines if Multicast is to be enabled during
  * the lease.</param>
  */
 public DhtDhcpServer(IDht dht, DHCPConfig config, bool EnableMulticast) :
     base(config)
 {
     _dht       = dht;
     _multicast = EnableMulticast;
 }
Ejemplo n.º 11
0
        public static DhtDhcpServer GetDhtDhcpServer(IDht dht, string ipop_namespace, bool enable_multicast)
        {
            DHCPConfig config = GetDhcpConfig(dht, ipop_namespace);

            return(new DhtDhcpServer(dht, config, enable_multicast));
        }
Ejemplo n.º 12
0
        public static int Main(string[] args)
        {
            DhtNodeParameters parameters = new DhtNodeParameters();

            if (parameters.Parse(args) != 0)
            {
                Console.WriteLine(parameters.ErrorMessage);
                parameters.ShowHelp();
                return(-1);
            }
            else if (parameters.Help)
            {
                parameters.ShowHelp();
                return(0);
            }

            DHCPConfig dhcp_config = parameters.DhcpConfig;
            IpopConfig ipop_config = parameters.IpopConfig;
            NodeConfig node_config = parameters.NodeConfig;

            NodeConfig.SecurityPolicy security = parameters.Security;


            if (node_config.NodeAddress == null)
            {
                node_config.NodeAddress = (Utils.GenerateAHAddress()).ToString();
                node_config.WriteConfig();
            }

            if (parameters.GroupVPN)
            {
                // check to see if we have a valid private key
                RSACryptoServiceProvider public_key = new RSACryptoServiceProvider();
                bool create = true;
                // If this succeeds, the key is good, if it fails, we need to create a new one...
                if (File.Exists(security.KeyPath))
                {
                    try {
                        using (FileStream fs = File.Open(security.KeyPath, FileMode.Open)) {
                            byte[] blob = new byte[fs.Length];
                            fs.Read(blob, 0, blob.Length);
                            public_key.ImportCspBlob(blob);
                            public_key.ImportCspBlob(public_key.ExportCspBlob(false));
                        }
                        create = false;
                    } catch { }
                }

                // we don't, let's create one
                if (create)
                {
                    using (FileStream fs = File.Open(security.KeyPath, FileMode.Create)) {
                        RSACryptoServiceProvider private_key = new RSACryptoServiceProvider(2048);
                        byte[] blob = private_key.ExportCspBlob(true);
                        fs.Write(blob, 0, blob.Length);
                        public_key.ImportCspBlob(private_key.ExportCspBlob(false));
                    }
                }

                // verify we have a cacert
                string cacert_path = Path.Combine(security.CertificatePath, "cacert");
                if (!File.Exists(cacert_path))
                {
                    Console.WriteLine("Missing CACert: " + cacert_path);
                    parameters.ShowHelp();
                    return(-1);
                }

                // do we already have a certificate that matches our node id?
                string cert_path = Path.Combine(security.CertificatePath,
                                                "lc." + node_config.NodeAddress.Substring(12));
                // no, let's create one
                if (create || !File.Exists(cert_path))
                {
                    // prepare access to the groupvpn site
                    string webcert_path = Path.Combine(security.CertificatePath, "webcert");
                    if (!File.Exists(webcert_path))
                    {
                        Console.WriteLine("Missing Servers signed cert: " + webcert_path);
                        parameters.ShowHelp();
                        return(-1);
                    }

                    X509Certificate webcert = X509Certificate.CreateFromCertFile(webcert_path);
                    CertificatePolicy.Register(webcert);

                    // get certificate and store it to file
                    GroupVPNClient gvc = new GroupVPNClient(ipop_config.GroupVPN.UserName,
                                                            ipop_config.GroupVPN.Group, ipop_config.GroupVPN.Secret,
                                                            ipop_config.GroupVPN.ServerURI, node_config.NodeAddress,
                                                            public_key);
                    gvc.Start();
                    if (gvc.State != GroupVPNClient.States.Finished)
                    {
                        Console.WriteLine("Failure attempting to use GroupVPN");
                        parameters.ShowHelp();
                        return(-1);
                    }

                    using (FileStream fs = File.Open(cert_path, FileMode.Create)) {
                        byte[] blob = gvc.Certificate.X509.RawData;
                        fs.Write(blob, 0, blob.Length);
                    }
                }
            }

            if (dhcp_config != null)
            {
                _current_node = new DhtIpopNode(node_config, ipop_config, dhcp_config);
            }
            else
            {
                _current_node = new DhtIpopNode(node_config, ipop_config);
            }

            if (parameters.GroupVPN)
            {
                // hack until I can come up with a cleaner solution to add features
                // that don't break config files on previous IPOP
                string cacert_path    = Path.Combine(security.CertificatePath, "cacert");
                string revocation_url = ipop_config.GroupVPN.ServerURI.Replace("mono/GroupVPN.rem",
                                                                               "data/" + ipop_config.GroupVPN.Group + "/revocation_list");
                revocation_url = revocation_url.Replace("https", "http");
                var icv = new GroupCertificateVerification(revocation_url, cacert_path);
                _current_node.AppNode.SecurityOverlord.CertificateHandler.AddCertificateVerification(icv);
                icv.RevocationUpdate += _current_node.AppNode.SecurityOverlord.VerifySAs;
            }

            Console.WriteLine("Starting IPOP: " + DateTime.UtcNow);
            _current_node.Run();
            return(0);
        }