#!@PYTHON@ # # Copyright (C) 2002 # # This file is part of the pxeconfig utils # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2, or (at your option) any # later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA # # SVN info # $Id: pxeconfigd.in 120 2008-04-11 08:48:46Z bas $ """ 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. -V/--version Prints the version number and exits Note: This server can ONLY be started from inetd. """ 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='Vd:' LONGOPT_LIST=['version', 'directory='] PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' VERSION = '2.0.0' # 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) for opt, value in opts: if opt in ['-d', '--directory']: if not os.path.isdir(value): os.write(STDOUT, "PXE booting is not configured") sys.exit(1) else: PXE_CONF_DIR = value elif opt in ['-V', '--version']: print VERSION sys.exit(0) def server(): """Start the daemon """ check_args(sys.argv) handleConnection() if __name__ == '__main__': server()