[2] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Author: Bas van der Vlies <basv@sara.nl> |
---|
| 4 | Date : 12 February 2002 |
---|
| 5 | Desc. : This script is used to control how a node is booted with PXE |
---|
| 6 | enabled network cards. The PC boots starts and fetch a pxe |
---|
| 7 | config file which tells how the PC must boot. This daemon |
---|
| 8 | enables a client to remove his pxe-config file. It then uses |
---|
| 9 | the DEFAULT one. |
---|
| 10 | |
---|
| 11 | Note: |
---|
| 12 | This server can ONLY be started from inetd. |
---|
| 13 | |
---|
| 14 | CVS info |
---|
| 15 | $Date: 2002/02/18 15:02:00 $ |
---|
| 16 | $Revision: 1.1 $ |
---|
| 17 | """ |
---|
| 18 | import time |
---|
| 19 | import socket |
---|
| 20 | import sys |
---|
| 21 | import os |
---|
| 22 | import string |
---|
| 23 | import syslog |
---|
| 24 | |
---|
| 25 | # DEBUG |
---|
| 26 | # |
---|
| 27 | DEBUG=1 |
---|
| 28 | |
---|
| 29 | # Some Global Constants |
---|
| 30 | # |
---|
| 31 | BUFSIZE=4096 |
---|
| 32 | STDIN=0 |
---|
| 33 | STDOUT=1 |
---|
| 34 | |
---|
| 35 | PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' |
---|
| 36 | |
---|
| 37 | # Give the current time |
---|
| 38 | # |
---|
| 39 | def now(): |
---|
| 40 | return time.ctime(time.time()) |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | def remove_link(filename): |
---|
| 44 | """This removes the pxe config filename for the host that is connected: |
---|
| 45 | filename : string |
---|
| 46 | """ |
---|
| 47 | |
---|
| 48 | file = os.path.join(PXE_CONF_DIR, filename) |
---|
| 49 | |
---|
| 50 | if DEBUG: |
---|
| 51 | print 'file = %s' %file |
---|
| 52 | |
---|
| 53 | if not os.path.exists(file): return |
---|
| 54 | |
---|
| 55 | if os.path.islink(file): |
---|
| 56 | try: |
---|
| 57 | os.unlink(file) |
---|
| 58 | syslog.openlog("pxeconfigd") |
---|
| 59 | syslog.syslog(syslog.LOG_INFO, file) |
---|
| 60 | syslog.closelog() |
---|
| 61 | except OSError: |
---|
| 62 | err_msg = "No permission at directory: %s" %PXE_CONF_DIR |
---|
| 63 | os.write(STDOUT, err_msg) |
---|
| 64 | sys.exit(1) |
---|
| 65 | |
---|
| 66 | # This function handles the client connection. It closes |
---|
| 67 | # the connection if there is no data |
---|
| 68 | # |
---|
| 69 | def handleConnection(): |
---|
| 70 | """Determines which host connects to the server |
---|
| 71 | """ |
---|
| 72 | |
---|
| 73 | # Determine client address |
---|
| 74 | # |
---|
| 75 | try: |
---|
| 76 | client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, |
---|
| 77 | socket.SOCK_STREAM) |
---|
| 78 | client_ip = client_addr.getpeername()[0] |
---|
| 79 | |
---|
| 80 | except socket.error, detail: |
---|
| 81 | print "pxeconfigd can only be started from inetd!!!" |
---|
| 82 | sys.exit(1) |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | # translate ip address ---> hex address |
---|
| 86 | # |
---|
| 87 | d = string.split(client_ip, '.') |
---|
| 88 | client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3])) |
---|
| 89 | |
---|
| 90 | if DEBUG: |
---|
| 91 | print 'ip = %s, hex = %s' %(client_ip, client_haddr) |
---|
| 92 | |
---|
| 93 | remove_link(client_haddr) |
---|
| 94 | sys.exit(0) |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | def server(): |
---|
| 98 | """Start the daemon |
---|
| 99 | """ |
---|
| 100 | |
---|
| 101 | if not os.path.isdir(PXE_CONF_DIR): |
---|
| 102 | os.write(STDOUT, "PXE booting is not configured") |
---|
| 103 | sys.exit(1) |
---|
| 104 | |
---|
| 105 | handleConnection() |
---|
| 106 | |
---|
| 107 | if __name__ == '__main__': |
---|
| 108 | server() |
---|