public void SetAll() { var isError = false; var reader = new IniReader(); reader.CheckForConfig(); Nic = reader.ReadConfig("settings", "interface"); NextServer = reader.ReadConfig("settings", "next-server"); try { ListenDiscover = Convert.ToBoolean(reader.ReadConfig("settings", "listen-dhcp")); } catch (Exception) { Console.WriteLine("listen-dhcp Is Not Valid. Valid Entries Include true or false"); isError = true; } try { ListenProxy = Convert.ToBoolean(reader.ReadConfig("settings", "listen-proxy")); } catch (Exception) { Console.WriteLine("listen-proxy Is Not Valid. Valid Entries Include true or false"); isError = true; } try { AllowAll = Convert.ToBoolean(reader.ReadConfig("settings", "allow-all-mac")); } catch (Exception) { Console.WriteLine("allow-all-mac Is Not Valid. Valid Entries Include true or false"); isError = true; } PXEClient = Encoding.UTF8.GetBytes("PXEClient"); AAPLBSDPC = Encoding.UTF8.GetBytes("AAPLBSDPC"); CloneDeployServiceURL = reader.ReadConfig("settings", "clonedeploy-service-url"); BiosBootFile = reader.ReadConfig("settings", "bios-bootfile"); Efi32BootFile = reader.ReadConfig("settings", "efi32-bootfile"); Efi64BootFile = reader.ReadConfig("settings", "efi64-bootfile"); AppleBootFile = reader.ReadConfig("settings", "apple-boot-file"); RootPath = reader.ReadConfig("settings", "apple-root-path"); VendorInfo = reader.ReadConfig("settings", "apple-vendor-specific-information"); ServerIdentifierOverride = reader.ReadConfig("settings", "server-identifier-override"); try { ListenBSDP = Convert.ToBoolean(reader.ReadConfig("settings", "listen-apple-bsdp")); } catch (Exception) { Console.WriteLine("listen-apple-bsdp Is Not Valid. Valid Entries Include true or false"); isError = true; } AppleEFIBootFile = reader.ReadConfig("settings", "apple-efi-boot-file"); if (!string.IsNullOrEmpty(Settings.CloneDeployServiceURL)) { using (var client = new WebClient()) { try { client.DownloadString(Settings.CloneDeployServiceURL + "Test"); } catch { Console.WriteLine("CloneDeploy Web Service Test Failed. Web Reservations Will Not Be Processed"); CloneDeployServiceURL = null; } } } else { Console.WriteLine("CloneDeploy Web Service Is Not Populated. Web Reservations Will Not Be Processed"); } Console.WriteLine(); try { IPAddress.Parse(Nic); } catch { Console.WriteLine("Interface Is Not Valid. Ensure The Interface Is A Valid IPv4 Address"); isError = true; } try { IPAddress.Parse(NextServer); } catch { Console.WriteLine("Next-Server Is Not Valid. Ensure The Next-Server Is A Valid IPv4 Address"); isError = true; } if (ListenBSDP && !ListenDiscover) { Console.WriteLine("Cannot Listen For Apple BSDP Requests. listen-dhcp Must Be true"); isError = true; } if (Nic == "0.0.0.0" && string.IsNullOrEmpty(ServerIdentifierOverride)) { Console.WriteLine("When the interface Is Set To Listen For Requests On Any IP, server-identifier-override Must Have An Address"); isError = true; } if (Type.GetType("Mono.Runtime") != null) { if (Nic != "0.0.0.0") { Console.WriteLine("When running on Mono the interface must be set to 0.0.0.0"); isError = true; } if (string.IsNullOrEmpty(ServerIdentifierOverride)) { Console.WriteLine("When running on Mono the server-identifier-override must have a value"); isError = true; } } if (isError) { Console.WriteLine("CloneDeploy Proxy DHCP Could Not Be Started"); Console.WriteLine("Press [Enter] to Exit"); Console.Read(); Environment.Exit(1); } var rdr = new FileReader(); if (!AllowAll) { if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"allow")) { foreach (var mac in rdr.ReadFile("allow")) { DHCPServer.DHCPServer.AddAcl(PhysicalAddress.Parse(mac), false); } } } if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"deny")) { foreach (var mac in rdr.ReadFile("deny")) { DHCPServer.DHCPServer.AddAcl(PhysicalAddress.Parse(mac), true); } } try { if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"reservations")) { foreach (var reservation in rdr.ReadFile("reservations")) { var arrayReservation = reservation.Split(','); var serveroptions = new DHCPServer.DHCPServer.ReservationOptions(); if (arrayReservation.Length == 4) { serveroptions.ReserveBCDFile = arrayReservation[3]; } serveroptions.ReserveBootFile = arrayReservation[2]; serveroptions.ReserveNextServer = arrayReservation[1]; DHCPServer.DHCPServer.Reservations.Add(PhysicalAddress.Parse(arrayReservation[0]), serveroptions); } } } catch (Exception) { Console.WriteLine("Could Not Parse Local Reservations File. Local Reservations Will Not Be Processed"); DHCPServer.DHCPServer.Reservations.Clear(); } }
/*BSDP Options * Code Length Values Name Client Or Server * -1- 1 1-List, 2-Select,3-Failed Message Type Both * -2- 2 uint16 encoding Version Client * -3- 4 ipaddr encoding Server Identifier Server * -4- 2 uint16 0-65535 Server Priority Server * -5- 2 uint16 < 1024 Reply Port Client * -7- 4 single:1-4095 cluster:4096-65535 Default Image Id Server * -8- 4 Selected Image Id Both * -9- 255 Max Image List Server */ /*Example Vendor Option String Serving two netboot images. 1 named net and 1 named boot * 01:01:01:03:04:A2:33:4B:A0:04:02:FF:FF:07:04:01:00:00:89:09:11:01:00:00:89:03:6E:65:74:01:00:00:88:04:62:6F:6F:74 * * 01:01: option 1 * 01: List * 03:04: option 3 * A2:33:4B:A0: Ip address * 04:02: option 4 * FF:FF: Max Value * 07:04: Option 7 * 01:00:00:89: default image id - always starts with 01:00 for Netboot OSX * 09:11: Option 9 Length = 5 * [number of images] + [sum of image names] * 01:00:00:89: [image id] * 03: [image name length] * 6E:65:74: [image name] * 01:00:00:88: [image id] * 04: [image name length] * 62:6f:6f:74 [image name] */ public void Generate() { Console.WriteLine(); string output = string.Empty; var listNbis = new List <Nbi>(); var reader = new IniReader(); var vendorOptions = new StringBuilder(); //Read in the config.ini nbi ids var netBootServer = reader.ReadConfig("vendor-specific-info-generator", "netboot-server-ip"); for (int i = 1; i <= 5; i++) { var tmpId = reader.ReadConfig("vendor-specific-info-generator", "apple_nbi_id_" + i); var tmpName = reader.ReadConfig("vendor-specific-info-generator", "apple_nbi_name_" + i); if (!string.IsNullOrEmpty(tmpId) && !string.IsNullOrEmpty(tmpName)) { var nbi = new Nbi(); nbi.Id = tmpId; nbi.Name = tmpName; listNbis.Add(nbi); } } if (listNbis.Count == 0) { return; } //Set the target netboot server ip address vendorOptions.Append("01:01:01:03:04:"); IPAddress ip; try { ip = IPAddress.Parse(netBootServer); } catch { Console.WriteLine("Could Not Parse netboot-server-ip"); return; } foreach (byte i in ip.GetAddressBytes()) { vendorOptions.Append(i.ToString("X2") + ":"); } //Set the default nbi and determine the total nbi list length vendorOptions.Append("04:02:FF:FF:07:04:"); int rowCount = 0; int totalNameLength = 0; foreach (var nbi in listNbis) { rowCount++; if (rowCount == 1) { vendorOptions.Append("01:00:"); vendorOptions.Append(Helpers.Utility.AddHexColons(Convert.ToInt32(nbi.Id).ToString("X4"))); vendorOptions.Append(":"); } totalNameLength += nbi.Name.Length; } vendorOptions.Append("09:" + (5 * rowCount + totalNameLength).ToString("X2")); vendorOptions.Append(":"); //Add the nbis List <string> listIds = new List <string>(); var counter = 1; foreach (var nbi in listNbis) { vendorOptions.Append("01:00:"); var nbiIdHex = Convert.ToInt32(nbi.Id).ToString("X4"); vendorOptions.Append(Helpers.Utility.AddHexColons(nbiIdHex)); vendorOptions.Append(":"); vendorOptions.Append(nbi.Name.Length.ToString("X2")); vendorOptions.Append(":"); vendorOptions.Append(Helpers.Utility.StringToHex(nbi.Name)); if (counter != listNbis.Count) { vendorOptions.Append(":"); } listIds.Add(nbiIdHex); if (nbiIdHex != "0F49" && nbiIdHex != "98DB") { output += "Place The " + nbi.Name + " NBI NetBoot.dmg In A Folder Named " + nbiIdHex + " On Your Web Server" + Environment.NewLine; output += "Place The " + nbi.Name + " NBI i386 Folder In A Folder Named " + nbiIdHex + " On Your TFTP Server" + Environment.NewLine; } counter++; } var duplicateIds = listIds.GroupBy(x => x) .Where(group => group.Count() > 1) .Select(group => group.Key); if (duplicateIds.Any()) { Console.WriteLine("The list cannot contain duplicate ids"); return; } output += Environment.NewLine + "Copy The Following String To Your config.ini For The apple-vendor-specific-information Key" + Environment.NewLine; output += vendorOptions.ToString(); Console.WriteLine(output); }
public void SetAll() { var isError = false; var reader = new IniReader(); reader.CheckForConfig(); Nic = reader.ReadConfig("settings", "interface"); NextServer = reader.ReadConfig("settings", "next-server"); try { ListenDiscover = Convert.ToBoolean(reader.ReadConfig("settings", "listen-dhcp")); } catch (Exception) { Console.WriteLine("listen-dhcp Is Not Valid. Valid Entries Include true or false"); isError = true; } try { ListenProxy = Convert.ToBoolean(reader.ReadConfig("settings", "listen-proxy")); } catch (Exception) { Console.WriteLine("listen-proxy Is Not Valid. Valid Entries Include true or false"); isError = true; } try { AllowAll = Convert.ToBoolean(reader.ReadConfig("settings", "allow-all-mac")); } catch (Exception) { Console.WriteLine("allow-all-mac Is Not Valid. Valid Entries Include true or false"); isError = true; } try { CheckWebReservations = Convert.ToBoolean(reader.ReadConfig("settings", "check-web-reservations")); } catch (Exception) { Console.WriteLine("check-web-reservations Is Not Valid. Valid Entries Include true or false"); isError = true; } try { CheckTftpCluster = Convert.ToBoolean(reader.ReadConfig("settings", "check-tftp-cluster")); } catch (Exception) { Console.WriteLine("check-tftp-cluster Is Not Valid. Valid Entries Include true or false"); isError = true; } PXEClient = Encoding.UTF8.GetBytes("PXEClient"); AAPLBSDPC = Encoding.UTF8.GetBytes("AAPLBSDPC"); CloneDeployServiceURL = reader.ReadConfig("settings", "clonedeploy-base-url"); if (!CloneDeployServiceURL.Trim().EndsWith("/")) { CloneDeployServiceURL += "/"; } BiosBootFile = reader.ReadConfig("settings", "bios-bootfile"); Efi32BootFile = reader.ReadConfig("settings", "efi32-bootfile"); Efi64BootFile = reader.ReadConfig("settings", "efi64-bootfile"); AppleBootFile = reader.ReadConfig("settings", "apple-boot-file"); RootPath = reader.ReadConfig("settings", "apple-root-path"); VendorInfo = reader.ReadConfig("settings", "apple-vendor-specific-information"); ServerIdentifierOverride = reader.ReadConfig("settings", "server-identifier-override"); try { ListenBSDP = Convert.ToBoolean(reader.ReadConfig("settings", "listen-apple-bsdp")); } catch (Exception) { Console.WriteLine("listen-apple-bsdp Is Not Valid. Valid Entries Include true or false"); isError = true; } AppleEFIBootFile = reader.ReadConfig("settings", "apple-efi-boot-file"); if (!string.IsNullOrEmpty(CloneDeployServiceURL)) { Console.Write("CloneDeploy ServiceURL Is Populated. Testing API ... "); CheckAppleApi = true; var testResult = new ApiCalls.APICall().ProxyDhcpApi.Test(); if (!testResult) { Console.WriteLine("FAILED"); Console.WriteLine("... Web Reservations Will Not Be Processed"); Console.WriteLine("... Clustered Tftp Servers Will Not Be Processed"); CloneDeployServiceURL = null; CheckTftpCluster = false; CheckWebReservations = false; CheckAppleApi = false; } else { Console.WriteLine("PASSED"); } if (ListenBSDP && CheckAppleApi) { var profileIp = ""; Console.WriteLine("Checking For Apple NetBoot Info From CloneDeploy API"); if (Nic == "0.0.0.0") { profileIp = ServerIdentifierOverride; Console.WriteLine("Looking For Matching NetBoot Profile For IP " + ServerIdentifierOverride); } else { profileIp = Nic; Console.WriteLine("Looking For Matching NetBoot Profile For IP " + Nic); } var appleVendorDto = new ApiCalls.APICall().ProxyDhcpApi.GetAppleVendorString(profileIp); if (appleVendorDto.Success) { Console.WriteLine("Found. " + appleVendorDto.VendorString); VendorInfo = appleVendorDto.VendorString; } else { Console.WriteLine(appleVendorDto.ErrorMessage); } } if (CheckTftpCluster) { try { TftpPollingInterval = Int32.Parse(reader.ReadConfig("settings", "tftp-polling-interval")); } catch (Exception) { Console.WriteLine( "tftp-polling-interval Is Not Valid. Valid Entries Include A Positive Integer"); isError = true; CheckTftpCluster = false; } try { Console.WriteLine("Clustered Tftp Server Check Is Enabled. Enumerating Tftp Servers ..."); new TftpMonitor().Run(); //Fix me - the last item in the list seems to get cut off because it might not have finished the tftp get again. Running it again, adds a delay. new TftpMonitor().Run(); for (int i = 0; i < TftpMonitor.TftpStatus.Keys.Count; i++) { Console.WriteLine(TftpMonitor.TftpStatus.ElementAt(i)); } Console.WriteLine("... Complete"); } catch (Exception) { Console.WriteLine( "Could Not Enumerate Tftp Servers"); isError = true; CheckTftpCluster = false; } } } else { Console.WriteLine("CloneDeploy Web Service Is Not Populated. Web Reservations And Apple NetBoot Profiles Will Not Be Processed"); } Console.WriteLine(); try { IPAddress.Parse(Nic); } catch { Console.WriteLine("Interface Is Not Valid. Ensure The Interface Is A Valid IPv4 Address"); isError = true; } try { IPAddress.Parse(NextServer); } catch { Console.WriteLine("Next-Server Is Not Valid. Ensure The Next-Server Is A Valid IPv4 Address"); isError = true; } if (ListenBSDP && !ListenDiscover) { Console.WriteLine("Cannot Listen For Apple BSDP Requests. listen-dhcp Must Be true"); isError = true; } if (Nic == "0.0.0.0" && string.IsNullOrEmpty(ServerIdentifierOverride)) { Console.WriteLine("When the interface Is Set To Listen For Requests On Any IP, server-identifier-override Must Have An Address"); isError = true; } if (Type.GetType("Mono.Runtime") != null) { if (Nic != "0.0.0.0") { Console.WriteLine("When running on Mono the interface must be set to 0.0.0.0"); isError = true; } if (string.IsNullOrEmpty(ServerIdentifierOverride)) { Console.WriteLine("When running on Mono the server-identifier-override must have a value"); isError = true; } } if (isError) { Console.WriteLine("CloneDeploy Proxy DHCP Could Not Be Started"); Console.WriteLine("Press [Enter] to Exit"); Console.Read(); Environment.Exit(1); } var rdr = new FileReader(); if (!AllowAll) { if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"allow")) { foreach (var mac in rdr.ReadFile("allow")) { DHCPServer.DHCPServer.AddAcl(PhysicalAddress.Parse(mac), false); } } } if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"deny")) { foreach (var mac in rdr.ReadFile("deny")) { DHCPServer.DHCPServer.AddAcl(PhysicalAddress.Parse(mac), true); } } try { if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"reservations")) { foreach (var reservation in rdr.ReadFile("reservations")) { var arrayReservation = reservation.Split(','); var serveroptions = new DHCPServer.DHCPServer.ReservationOptions(); if (arrayReservation.Length == 4) { serveroptions.ReserveBCDFile = arrayReservation[3]; } serveroptions.ReserveBootFile = arrayReservation[2]; serveroptions.ReserveNextServer = arrayReservation[1]; DHCPServer.DHCPServer.Reservations.Add(PhysicalAddress.Parse(arrayReservation[0]), serveroptions); } } } catch (Exception) { Console.WriteLine("Could Not Parse Local Reservations File. Local Reservations Will Not Be Processed"); DHCPServer.DHCPServer.Reservations.Clear(); } }