[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 |
---|
[3] | 6 | enabled network cards. The node boots and fetch a pxe |
---|
| 7 | config file which tells how the node must boot. This daemon |
---|
| 8 | enables a client to remove his/here pxe config file. With the |
---|
| 9 | next boot it will use the default one. |
---|
| 10 | |
---|
| 11 | command line option: |
---|
| 12 | -d/--directory <dir> |
---|
| 13 | Where <dir> is the directory where the pxe config files reside. |
---|
[2] | 14 | |
---|
| 15 | Note: |
---|
| 16 | This server can ONLY be started from inetd. |
---|
| 17 | |
---|
| 18 | CVS info |
---|
[4] | 19 | $Date: 2002/02/19 09:57:40 $ |
---|
| 20 | $Revision: 1.3 $ |
---|
[2] | 21 | """ |
---|
| 22 | import time |
---|
| 23 | import socket |
---|
| 24 | import sys |
---|
| 25 | import os |
---|
| 26 | import string |
---|
| 27 | import syslog |
---|
[3] | 28 | import getopt |
---|
[2] | 29 | |
---|
| 30 | # DEBUG |
---|
| 31 | # |
---|
| 32 | DEBUG=1 |
---|
| 33 | |
---|
| 34 | # Some Global Constants |
---|
| 35 | # |
---|
| 36 | BUFSIZE=4096 |
---|
| 37 | STDIN=0 |
---|
| 38 | STDOUT=1 |
---|
[3] | 39 | SHORTOPT_LIST='d:' |
---|
| 40 | LONGOPT_LIST=['directory='] |
---|
[2] | 41 | |
---|
| 42 | PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' |
---|
| 43 | |
---|
| 44 | # Give the current time |
---|
| 45 | # |
---|
| 46 | def now(): |
---|
| 47 | return time.ctime(time.time()) |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | def remove_link(filename): |
---|
| 51 | """This removes the pxe config filename for the host that is connected: |
---|
| 52 | filename : string |
---|
| 53 | """ |
---|
| 54 | |
---|
| 55 | file = os.path.join(PXE_CONF_DIR, filename) |
---|
| 56 | |
---|
| 57 | if DEBUG: |
---|
| 58 | print 'file = %s' %file |
---|
| 59 | |
---|
| 60 | if not os.path.exists(file): return |
---|
| 61 | |
---|
| 62 | if os.path.islink(file): |
---|
| 63 | try: |
---|
| 64 | os.unlink(file) |
---|
| 65 | syslog.openlog("pxeconfigd") |
---|
| 66 | syslog.syslog(syslog.LOG_INFO, file) |
---|
| 67 | syslog.closelog() |
---|
| 68 | except OSError: |
---|
| 69 | err_msg = "No permission at directory: %s" %PXE_CONF_DIR |
---|
| 70 | os.write(STDOUT, err_msg) |
---|
| 71 | sys.exit(1) |
---|
| 72 | |
---|
| 73 | # This function handles the client connection. It closes |
---|
| 74 | # the connection if there is no data |
---|
| 75 | # |
---|
| 76 | def handleConnection(): |
---|
| 77 | """Determines which host connects to the server |
---|
| 78 | """ |
---|
| 79 | |
---|
| 80 | # Determine client address |
---|
| 81 | # |
---|
| 82 | try: |
---|
| 83 | client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, |
---|
| 84 | socket.SOCK_STREAM) |
---|
| 85 | client_ip = client_addr.getpeername()[0] |
---|
| 86 | |
---|
| 87 | except socket.error, detail: |
---|
| 88 | print "pxeconfigd can only be started from inetd!!!" |
---|
| 89 | sys.exit(1) |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | # translate ip address ---> hex address |
---|
| 93 | # |
---|
| 94 | d = string.split(client_ip, '.') |
---|
| 95 | client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3])) |
---|
| 96 | |
---|
| 97 | if DEBUG: |
---|
| 98 | print 'ip = %s, hex = %s' %(client_ip, client_haddr) |
---|
| 99 | |
---|
| 100 | remove_link(client_haddr) |
---|
| 101 | sys.exit(0) |
---|
| 102 | |
---|
[3] | 103 | def check_args(argv): |
---|
| 104 | """ |
---|
| 105 | Must we use another directory for the PXE configuration |
---|
| 106 | """ |
---|
[4] | 107 | global PXE_CONF_DIR |
---|
| 108 | |
---|
[3] | 109 | try: |
---|
| 110 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
| 111 | except getopt.error, detail: |
---|
| 112 | print __doc__ |
---|
| 113 | print detail |
---|
| 114 | sys.exit(1) |
---|
[2] | 115 | |
---|
[3] | 116 | if opts: |
---|
[4] | 117 | opt, PXE_CONF_DIR = opts[0] |
---|
[3] | 118 | |
---|
[2] | 119 | def server(): |
---|
| 120 | """Start the daemon |
---|
| 121 | """ |
---|
[3] | 122 | check_args(sys.argv) |
---|
[2] | 123 | |
---|
| 124 | if not os.path.isdir(PXE_CONF_DIR): |
---|
| 125 | os.write(STDOUT, "PXE booting is not configured") |
---|
| 126 | sys.exit(1) |
---|
| 127 | |
---|
| 128 | handleConnection() |
---|
| 129 | |
---|
| 130 | if __name__ == '__main__': |
---|
| 131 | server() |
---|