Exemplo n.º 1
0
        static void SendAppleBootList(DHCPRequest dhcpRequest)
        {
            Trace.WriteLine("Request Is An Apple NetBoot");
            int bsdpPort = 68;
            var vendorSpecificInformation = OptionData.GetOptionData(DHCPOption.VendorSpecificInformation, dhcpRequest.requestData);
            var strVendorInformation      = Utility.ByteArrayToString(vendorSpecificInformation, true);

            if (strVendorInformation.Length >= 21)
            {
                var isReturnPort = strVendorInformation.Substring(14, 4);
                if (isReturnPort == "0502")
                {
                    var returnPort = strVendorInformation.Substring(18, 4);
                    bsdpPort = Convert.ToInt32(returnPort, 16);
                }
            }

            var replyOptions = new DHCPReplyOptions();

            replyOptions.OtherOptions.Add(DHCPOption.Vendorclassidentifier, Settings.AAPLBSDPC);
            replyOptions.OtherOptions.Add(DHCPOption.VendorSpecificInformation, Utility.StringToByteArray(Settings.VendorInfo));
            var reply = new DHCPReply(dhcpRequest);

            reply.Send(DHCPMsgType.DHCPACK, replyOptions, bsdpPort);
        }
Exemplo n.º 2
0
        static void ProcessPXERequest(DHCPRequest dhcpRequest)
        {
            //Trace.WriteLine("Request Is A PXE Boot");
            var replyOptions = new DHCPReplyOptions();

            replyOptions.OtherOptions.Add(DHCPOption.Vendorclassidentifier, Settings.PXEClient);
            var reply = new DHCPReply(dhcpRequest);

            reply.Send(DHCPMsgType.DHCPOFFER, replyOptions, 68);
        }
        public void Process(DHCPRequest dhcpRequest)
        {
            var requestType = dhcpRequest.GetMsgType();

            Trace.WriteLine(requestType + " Proxy Request From " +
                            Utility.ByteArrayToString(dhcpRequest.GetChaddr(), true) + " " +
                            dhcpRequest.GetSourceIP() + ":" + dhcpRequest.GetSourcePort());

            var clientHardwareAddress = new PhysicalAddress(dhcpRequest.GetChaddr());

            if (DHCPServer.AclList.ContainsKey(clientHardwareAddress) && !DHCPServer.AclList[clientHardwareAddress] ||
                !DHCPServer.AclList.ContainsKey(clientHardwareAddress) && !Settings.AllowAll)
            {
                Trace.WriteLine("Request Denied By ACL - Ignoring");
                return;
            }

            var vendorId = dhcpRequest.GetVendorOptionData();

            if (vendorId != null)
            {
                var strVendorId = Utility.ByteArrayToString(vendorId, true);
                Trace.WriteLine("Vendor Class Id " + strVendorId + " " + Encoding.Default.GetString(vendorId));

                if (requestType == DHCPMsgType.DHCPREQUEST && strVendorId.StartsWith("505845436C69656E74"))
                {
                    //Expected Format: 505845436C69656E743A417263683A30303030303A554E44493A303032303031 (PXEClient:Arch:00000:UNDI:002001)
                    ProcessProxyRequest(dhcpRequest);
                }
                else
                {
                    Trace.WriteLine("Request Is Not A Proxy PXE Request - Ignoring");
                }
            }
            else
            {
                Trace.WriteLine("No Proxy Vendor Class Id Supplied - Ignoring");
            }

            Trace.WriteLine("");
        }
Exemplo n.º 4
0
        static void SendSelectedNetBoot(DHCPRequest dhcpRequest)
        {
            //This Reply is the client selecting which image they want to boot from
            Trace.WriteLine("Request Is An Apple NetBoot Selection");

            var vendorSpecificInformation    = dhcpRequest.GetVendorSpecificInformation();
            var strVendorSpecificInformation = Utility.ByteArrayToString(vendorSpecificInformation, true);
            var imageIdHex            = strVendorSpecificInformation.Substring(strVendorSpecificInformation.Length - 4);
            var targetNbi             = Settings.RootPath.Replace("[nbi_id]", imageIdHex);
            var targetAppleBootFile   = Settings.AppleBootFile.Replace("[nbi_id]", imageIdHex);
            var clientHardwareAddress = new PhysicalAddress(dhcpRequest.GetChaddr());

            var replyOptions = new DHCPReplyOptions();

            replyOptions.NextServer = IPAddress.Parse(Settings.NextServer);
            replyOptions.OtherOptions.Add(DHCPOption.Vendorclassidentifier, Settings.AAPLBSDPC);
            replyOptions.OtherOptions.Add(DHCPOption.VendorSpecificInformation, Utility.StringToByteArray(Settings.VendorInfo));
            replyOptions.OtherOptions.Add(DHCPOption.RootPath, Encoding.UTF8.GetBytes(targetNbi));

            //Modification to allow both a clonedeploy linux and osx imaging environment to work simultaneously without two proxy dhcp servers running
            if (imageIdHex == "0F49" || imageIdHex == "98DB") //image ids of 3913 or 39131
            {
                replyOptions.BootFileName = Settings.AppleEFIBootFile;
            }
            else
            {
                replyOptions.BootFileName = targetAppleBootFile;
            }

            if (DHCPServer.Reservations.ContainsKey(clientHardwareAddress))
            {
                replyOptions.NextServer =
                    IPAddress.Parse(DHCPServer.Reservations[clientHardwareAddress].ReserveNextServer);
                replyOptions.BootFileName = DHCPServer.Reservations[clientHardwareAddress].ReserveBootFile;
            }

            var reply = new DHCPReply(dhcpRequest);

            reply.Send(DHCPMsgType.DHCPACK, replyOptions, 68);
        }
Exemplo n.º 5
0
        private void CompleteRequest(object o)
        {
            DhcpData messageData = (DhcpData)o;
            EndPoint source      = new IPEndPoint(IPAddress.Any, 0);

            try
            {
                _socket.EndReceiveFrom(messageData.Result, ref source);
                messageData.Source = (IPEndPoint)source;
                var dhcpRequest = new DHCPRequest(messageData.MessageBuffer, _socket, messageData.Source, this);

                OnDataReceived(dhcpRequest);
                var msgType = dhcpRequest.GetMsgType();

                switch (msgType)
                {
                case DHCPMsgType.DHCPDISCOVER:
                    OnDiscover(dhcpRequest);
                    break;

                case DHCPMsgType.DHCPREQUEST:
                    OnRequest(dhcpRequest);
                    break;

                case DHCPMsgType.DHCPINFORM:
                    OnInform(dhcpRequest);
                    break;

                default:
                    Trace.WriteLine("Message Type Not Handled By CloneDeploy Proxy DHCP - Ignoring Request");
                    break;
                }
            }
            catch
            {
                //throw;
            }
        }
Exemplo n.º 6
0
 public DHCPReply(DHCPRequest request)
 {
     _dhcpRequest = request;
 }
        static void ProcessProxyRequest(DHCPRequest dhcpRequest)
        {
            Trace.WriteLine("Request Is A Proxy PXE Boot");

            bool isWebReservation   = false;
            bool isLocalReservation = false;
            var  replyOptions       = new DHCPReplyOptions();

            var clientHardwareAddress = new PhysicalAddress(dhcpRequest.GetChaddr());

            if (DHCPServer.Reservations.ContainsKey(clientHardwareAddress))
            {
                isLocalReservation = true;
                Trace.WriteLine("Local Reservation Found");
                replyOptions.NextServer =
                    IPAddress.Parse(DHCPServer.Reservations[clientHardwareAddress].ReserveNextServer);
                replyOptions.BootFileName = DHCPServer.Reservations[clientHardwareAddress].ReserveBootFile;
                if (DHCPServer.Reservations[clientHardwareAddress].ReserveBCDFile != null)
                {
                    replyOptions.OtherOptions.Add(DHCPOption.Wpad,
                                                  Encoding.UTF8.GetBytes(DHCPServer.Reservations[clientHardwareAddress].ReserveBCDFile));
                }
            }
            else if (Settings.CheckWebReservations)
            {
                if (!string.IsNullOrEmpty(Settings.CloneDeployServiceURL))
                {
                    ProxyReservationDTO webReservation;

                    lock (dhcpRequest)
                    {
                        var mac = Utility.AddHexColons(Utility.ByteArrayToString(dhcpRequest.GetChaddr(), true));
                        webReservation = new APICall().ProxyDhcpApi.GetProxyReservation(mac);
                    }


                    if (webReservation.BootFile != null && webReservation.BootFile != "NotFound" &&
                        webReservation.BootFile != "NotEnabled")
                    {
                        isWebReservation = true;
                        Trace.WriteLine("Web Reservation Found");
                        if (!string.IsNullOrEmpty(webReservation.NextServer))
                        {
                            replyOptions.NextServer = IPAddress.Parse(webReservation.NextServer);
                        }
                        else
                        {
                            if (Settings.CheckTftpCluster)
                            {
                                var mac = Utility.AddHexColons(Utility.ByteArrayToString(dhcpRequest.GetChaddr(), true));
                                replyOptions.NextServer = new TftpCluster().GetNextServer(mac);
                            }
                            else
                            {
                                replyOptions.NextServer = IPAddress.Parse(Settings.NextServer);
                            }
                        }
                        replyOptions.BootFileName = webReservation.BootFile;
                        if (webReservation.BcdFile != null)
                        {
                            replyOptions.OtherOptions.Add(DHCPOption.Wpad,
                                                          Encoding.UTF8.GetBytes(webReservation.BcdFile));
                        }
                    }
                }
            }

            if (!isWebReservation && !isLocalReservation)
            {
                if (Settings.CheckTftpCluster && !string.IsNullOrEmpty(Settings.CloneDeployServiceURL))
                {
                    var mac = Utility.AddHexColons(Utility.ByteArrayToString(dhcpRequest.GetChaddr(), true));
                    replyOptions.NextServer = new TftpCluster().GetNextServer(mac);
                }
                else
                {
                    replyOptions.NextServer = IPAddress.Parse(Settings.NextServer);
                }

                var clientArch = dhcpRequest.GetClientSystemArch();
                if (clientArch != DHCPRequest.ClientSystemArch.Error)
                {
                    Trace.WriteLine("Client Architecture: " + clientArch);
                    bool unsupportedArch = false;
                    switch (clientArch)
                    {
                    case DHCPRequest.ClientSystemArch.Intelx86PC:     //legacy bios
                        replyOptions.BootFileName = Settings.BiosBootFile;
                        replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx86"));
                        break;

                    case DHCPRequest.ClientSystemArch.EFIIA32:     //efi x86
                        replyOptions.BootFileName = Settings.Efi32BootFile;
                        replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx86"));
                        break;

                    case DHCPRequest.ClientSystemArch.EFIBC:     //efi x64
                        replyOptions.BootFileName = Settings.Efi64BootFile;
                        replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx64"));
                        break;

                    case DHCPRequest.ClientSystemArch.EFIx8664:     //efi x64
                        replyOptions.BootFileName = Settings.Efi64BootFile;
                        replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx64"));
                        break;

                    default:
                        Trace.WriteLine("Unsupported Client System Architecture " + clientArch + " - Ignoring");
                        unsupportedArch = true;
                        break;
                    }

                    if (unsupportedArch)
                    {
                        return;
                    }
                }
                else
                {
                    Trace.WriteLine("Unsupported Client System Architecture " + clientArch + " - Ignoring");
                    return;
                }
            }


            var replyPort = dhcpRequest.GetSourcePort() == 4011 ? 4011 : 68;
            var reply     = new DHCPReply(dhcpRequest);

            reply.Send(DHCPMsgType.DHCPACK, replyOptions, replyPort);
        }
Exemplo n.º 8
0
        static void ProcessProxyRequest(DHCPRequest dhcpRequest)
        {
            Trace.WriteLine("Request Is A Proxy PXE Boot");

            var replyOptions = new DHCPReplyOptions();

            replyOptions.NextServer = IPAddress.Parse(Settings.NextServer);

            var clientHardwareAddress = new PhysicalAddress(dhcpRequest.GetChaddr());

            if (DHCPServer.Reservations.ContainsKey(clientHardwareAddress))
            {
                Trace.WriteLine("Local Reservation Found");
                replyOptions.NextServer =
                    IPAddress.Parse(DHCPServer.Reservations[clientHardwareAddress].ReserveNextServer);
                replyOptions.BootFileName = DHCPServer.Reservations[clientHardwareAddress].ReserveBootFile;
                if (DHCPServer.Reservations[clientHardwareAddress].ReserveBCDFile != null)
                {
                    replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(DHCPServer.Reservations[clientHardwareAddress].ReserveBCDFile));
                }
            }
            else
            {
                //Check for Web Reservation
                var webReservation = new WebReservation();
                if (!string.IsNullOrEmpty(Settings.CloneDeployServiceURL))
                {
                    lock (dhcpRequest)
                    {
                        using (var client = new WebClient())
                        {
                            var mac  = Utility.AddHexColons(Utility.ByteArrayToString(dhcpRequest.GetChaddr(), true));
                            var json =
                                client.DownloadString(Settings.CloneDeployServiceURL + "GetProxyReservation?mac=" + mac);
                            webReservation = JsonConvert.DeserializeObject <WebReservation>(json);
                        }
                    }
                }

                if (webReservation.BootFile != null && webReservation.BootFile != "NotFound" &&
                    webReservation.BootFile != "NotEnabled")
                {
                    Trace.WriteLine("Web Reservation Found");
                    replyOptions.NextServer   = IPAddress.Parse(webReservation.NextServer);
                    replyOptions.BootFileName = webReservation.BootFile;
                    if (webReservation.BcdFile != null)
                    {
                        replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(webReservation.BcdFile));
                    }
                }
                else
                {
                    Trace.WriteLine("No Reservation Found. Using Default config");
                    var clientArch = dhcpRequest.GetClientSystemArch();
                    if (clientArch != DHCPRequest.ClientSystemArch.Error)
                    {
                        Trace.WriteLine("Client Architecture: " + clientArch);
                        bool unsupportedArch = false;
                        switch (clientArch)
                        {
                        case DHCPRequest.ClientSystemArch.Intelx86PC:     //legacy bios
                            replyOptions.BootFileName = Settings.BiosBootFile;
                            replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx86"));
                            break;

                        case DHCPRequest.ClientSystemArch.EFIIA32:     //efi x86
                            replyOptions.BootFileName = Settings.Efi32BootFile;
                            replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx86"));
                            break;

                        case DHCPRequest.ClientSystemArch.EFIBC:     //efi x64
                            replyOptions.BootFileName = Settings.Efi64BootFile;
                            replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx64"));
                            break;

                        case DHCPRequest.ClientSystemArch.EFIx8664:     //efi x64
                            replyOptions.BootFileName = Settings.Efi64BootFile;
                            replyOptions.OtherOptions.Add(DHCPOption.Wpad, Encoding.UTF8.GetBytes(@"\boot\BCDx64"));
                            break;

                        default:
                            Trace.WriteLine("Unsupported Client System Architecture " + clientArch + " - Ignoring");
                            unsupportedArch = true;
                            break;
                        }

                        if (unsupportedArch)
                        {
                            return;
                        }
                    }
                    else
                    {
                        Trace.WriteLine("Unsupported Client System Architecture " + clientArch + " - Ignoring");
                        return;
                    }
                }
            }

            var replyPort = dhcpRequest.GetSourcePort() == 4011 ? 4011 : 68;
            var reply     = new DHCPReply(dhcpRequest);

            reply.Send(DHCPMsgType.DHCPACK, replyOptions, replyPort);
        }
Exemplo n.º 9
0
        public void Process(DHCPRequest dhcpRequest)
        {
            var requestType = dhcpRequest.GetMsgType();

            if (requestType != DHCPMsgType.DHCPDISCOVER && requestType != DHCPMsgType.DHCPINFORM)
            {
                return;
            }



            var clientHardwareAddress = new PhysicalAddress(dhcpRequest.GetChaddr());

            if (DHCPServer.AclList.ContainsKey(clientHardwareAddress) && !DHCPServer.AclList[clientHardwareAddress] ||
                !DHCPServer.AclList.ContainsKey(clientHardwareAddress) && !Settings.AllowAll)
            {
                Trace.WriteLine("Request Denied By ACL - Ignoring");
                return;
            }


            var vendorId = dhcpRequest.GetVendorOptionData();

            if (vendorId != null)
            {
                Trace.WriteLine(requestType + " Request From " +
                                Utility.ByteArrayToString(dhcpRequest.GetChaddr(), true));

                var strVendorId = Utility.ByteArrayToString(vendorId, true);

                //Expected Format: 505845436C69656E743A417263683A30303030303A554E44493A303032303031 (PXEClient:Arch:00000:UNDI:002001)
                if (strVendorId.StartsWith("505845436C69656E74"))
                {
                    ProcessPXERequest(dhcpRequest);
                }

                if (!Settings.ListenBSDP)
                {
                    return;
                }

                Trace.WriteLine("Vendor Class Id " + strVendorId);
                //Expected Format: 4141504C42534450432F693338362F694D616331342C33 (AAPLBSDPC/i386/iMac14,3)
                if (strVendorId.StartsWith("4141504C4253445043"))
                {
                    var vendorSpecificInformation = dhcpRequest.GetVendorSpecificInformation();
                    if (vendorSpecificInformation != null)
                    {
                        var strVendorInformation = Utility.ByteArrayToString(vendorSpecificInformation, true);
                        if (strVendorInformation.Length >= 6)
                        {
                            switch (strVendorInformation.Substring(0, 6))
                            {
                            case "010101":
                                SendAppleBootList(dhcpRequest);
                                break;

                            case "010102":
                                var interfaceHex =
                                    Utility.ByteArrayToString(
                                        string.IsNullOrEmpty(Settings.ServerIdentifierOverride)
                                                ? IPAddress.Parse(Settings.Nic).GetAddressBytes()
                                                : IPAddress.Parse(Settings.ServerIdentifierOverride)
                                        .GetAddressBytes(),
                                        true);

                                if (strVendorInformation.Contains(interfaceHex))
                                {
                                    SendSelectedNetBoot(dhcpRequest);
                                }
                                else
                                {
                                    Trace.WriteLine("Different BSDP Server Targeted - Ignoring");
                                }
                                break;

                            default:
                                Trace.WriteLine(
                                    "Not An Apple BSDP Request, Vendor Specific Information Mismatch - Ignoring");

                                break;
                            }
                        }
                    }
                }
            }

            Trace.WriteLine("");
        }