From 540f20c1f5276091b8c039a08283c72f41d4e0b8 Mon Sep 17 00:00:00 2001 From: Andres Gomez Date: Fri, 8 Apr 2022 21:54:07 +0300 Subject: [PATCH 1/2] server: only load DHCP's static config file if using DHCP --- pypxe/server.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/pypxe/server.py b/pypxe/server.py index 8ff6aa5..89ee15d 100755 --- a/pypxe/server.py +++ b/pypxe/server.py @@ -170,22 +170,6 @@ def main(): if os.getuid() != 0: print(sys.stderr, '\nWARNING: Not root. Servers will probably fail to bind.\n') - - # ideally this would be in dhcp itself, but the chroot below *probably* - # breaks the ability to open the config file. - if args.STATIC_CONFIG: - try: - static_config = io.open(args.STATIC_CONFIG, 'r') - except IOError: - sys.exit("Failed to open {0}".format(args.STATIC_CONFIG)) - try: - loaded_statics = json.load(static_config) - static_config.close() - except ValueError: - sys.exit("{0} does not contain valid json".format(args.STATIC_CONFIG)) - else: - loaded_statics = dict() - # setup main logger sys_logger = logging.getLogger('PyPXE') if args.SYSLOG_SERVER: @@ -250,6 +234,31 @@ def main(): # configure/start DHCP server if args.USE_DHCP: + def load_dhcp_static_config(filename): + result = ['', dict()] + + try: + static_config = io.open(filename, 'r') + except IOError: + result[0] = "Failed to open {0}".format(filename) + return result + + try: + result[1] = json.load(static_config) + static_config.close() + except ValueError: + result[0] = "{0} does not contain valid json".format(filename) + return result + + return result + + if args.STATIC_CONFIG: + message, loaded_statics = load_dhcp_static_config(args.STATIC_CONFIG) + if message: + sys.exit(message) + else: + loaded_statics = dict() + # setup DHCP logger dhcp_logger = helpers.get_child_logger(sys_logger, 'DHCP') if args.DHCP_MODE_PROXY: From 90c263d618cbefa075275333c217995964b4456b Mon Sep 17 00:00:00 2001 From: Andres Gomez Date: Tue, 5 Apr 2022 22:46:50 +0300 Subject: [PATCH 2/2] server: enable TFTP port from CLI --- README.md | 1 + example_cfg.json | 1 + pypxe/server.py | 3 +++ 3 files changed, 5 insertions(+) diff --git a/README.md b/README.md index f9ba7b6..af53ea1 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ The following are arguments that can be passed to `pypxe.server` when running fr |Argument|Description|Default| |---|---|---| |__`--tftp-server-ip TFTP_SERVER_IP`__|Specify TFTP server IP address|`0.0.0.0`| +|__`--tftp-port TFTP_PORT`__|Specify TFTP server Port|69| ##### HTTP Service Arguments diff --git a/example_cfg.json b/example_cfg.json index f97c74e..a80573f 100644 --- a/example_cfg.json +++ b/example_cfg.json @@ -27,6 +27,7 @@ "STATIC_CONFIG": "", "SYSLOG_PORT": 514, "SYSLOG_SERVER": null, + "TFTP_PORT": 69, "TFTP_SERVER_IP": "192.168.2.2", "USE_DHCP": true, "USE_HTTP": false, diff --git a/pypxe/server.py b/pypxe/server.py index 89ee15d..4c1767f 100755 --- a/pypxe/server.py +++ b/pypxe/server.py @@ -38,6 +38,7 @@ 'STATIC_CONFIG':'', 'SYSLOG_SERVER':None, 'SYSLOG_PORT':514, + 'TFTP_PORT':69, 'TFTP_SERVER_IP':'0.0.0.0', 'USE_IPXE':False, 'USE_HTTP':False, @@ -118,6 +119,7 @@ def parse_cli_arguments(): # TFTP server arguments tftp_group = parser.add_argument_group(title = 'TFTP', description = 'Arguments relevant to the TFTP server') + tftp_group.add_argument('--tftp-port', action = 'store', dest = 'TFTP_PORT', help = 'TFTP Server Port', default = SETTINGS['TFTP_PORT']) tftp_group.add_argument('--tftp-server-ip', action = 'store', dest = 'TFTP_SERVER_IP', help = 'TFTP Server IP', default = SETTINGS['TFTP_SERVER_IP']) return parser.parse_args() @@ -225,6 +227,7 @@ def main(): mode_verbose = do_verbose('tftp'), logger = tftp_logger, netboot_directory = args.NETBOOT_DIR, + port = args.TFTP_PORT, ip = args.TFTP_SERVER_IP) tftpd = threading.Thread(target = tftp_server.listen) tftpd.daemon = True