#!/usr/bin/env python # Copyright (C) 2000, SARA. # # Permission to use, copy, modify, and distribute this software and its # documentation for non-commercial purposes and without fee is hereby # granted, provided that the above copyright notice appears in all copies # and that both the copyright notice and this permission notice appear in # supporting documentation. # # Neither SARA (Stichting Academisch Rekencentrum Amsterdam) nor the # nor the author make any representations about the suitability of this # software for any purpose. This software is provided ``as is'' without # express or implied warranty. # # """ Author: Bas van der Vlies Date : 12 February 2002 Desc. : This script is used to control how a node is booted with PXE enabled network cards. The node boots and fetch a pxe config file which tells how the node must boot. This daemon enables a client to remove his/here pxe config file. With the next boot it will use the default one. command line option: -d/--directory Where is the directory where the pxe config files reside. Note: This server can ONLY be started from inetd. CVS info $Date: 2002/02/21 13:21:52 $ $Revision: 1.4 $ """ import time import socket import sys import os import string import syslog import getopt # DEBUG # DEBUG=1 # Some Global Constants # BUFSIZE=4096 STDIN=0 STDOUT=1 SHORTOPT_LIST='d:' LONGOPT_LIST=['directory='] PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' # Give the current time # def now(): return time.ctime(time.time()) def remove_link(filename): """This removes the pxe config filename for the host that is connected: filename : string """ file = os.path.join(PXE_CONF_DIR, filename) if DEBUG: print 'file = %s' %file if not os.path.exists(file): return if os.path.islink(file): try: os.unlink(file) syslog.openlog("pxeconfigd") syslog.syslog(syslog.LOG_INFO, file) syslog.closelog() except OSError: err_msg = "No permission at directory: %s" %PXE_CONF_DIR os.write(STDOUT, err_msg) sys.exit(1) # This function handles the client connection. It closes # the connection if there is no data # def handleConnection(): """Determines which host connects to the server """ # Determine client address # try: client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, socket.SOCK_STREAM) client_ip = client_addr.getpeername()[0] except socket.error, detail: print "pxeconfigd can only be started from inetd!!!" sys.exit(1) # translate ip address ---> hex address # d = string.split(client_ip, '.') client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3])) if DEBUG: print 'ip = %s, hex = %s' %(client_ip, client_haddr) remove_link(client_haddr) sys.exit(0) def check_args(argv): """ Must we use another directory for the PXE configuration """ global PXE_CONF_DIR try: opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) except getopt.error, detail: print __doc__ print detail sys.exit(1) if opts: opt, PXE_CONF_DIR = opts[0] def server(): """Start the daemon """ check_args(sys.argv) if not os.path.isdir(PXE_CONF_DIR): os.write(STDOUT, "PXE booting is not configured") sys.exit(1) handleConnection() if __name__ == '__main__': server()