[2] | 1 | #!/usr/bin/env python |
---|
[6] | 2 | # Copyright (C) 2000, SARA. |
---|
| 3 | # |
---|
| 4 | # Permission to use, copy, modify, and distribute this software and its |
---|
| 5 | # documentation for non-commercial purposes and without fee is hereby |
---|
| 6 | # granted, provided that the above copyright notice appears in all copies |
---|
| 7 | # and that both the copyright notice and this permission notice appear in |
---|
| 8 | # supporting documentation. |
---|
| 9 | # |
---|
| 10 | # Neither SARA (Stichting Academisch Rekencentrum Amsterdam) nor the |
---|
| 11 | # nor the author make any representations about the suitability of this |
---|
| 12 | # software for any purpose. This software is provided ``as is'' without |
---|
| 13 | # express or implied warranty. |
---|
| 14 | # |
---|
| 15 | # |
---|
[2] | 16 | """ |
---|
| 17 | Author: Bas van der Vlies <basv@sara.nl> |
---|
| 18 | Date : 12 February 2002 |
---|
| 19 | Desc. : This script is used to control how a node is booted with PXE |
---|
[3] | 20 | enabled network cards. The node boots and fetch a pxe |
---|
| 21 | config file which tells how the node must boot. This daemon |
---|
| 22 | enables a client to remove his/here pxe config file. With the |
---|
| 23 | next boot it will use the default one. |
---|
| 24 | |
---|
| 25 | command line option: |
---|
| 26 | -d/--directory <dir> |
---|
| 27 | Where <dir> is the directory where the pxe config files reside. |
---|
[2] | 28 | |
---|
| 29 | Note: |
---|
| 30 | This server can ONLY be started from inetd. |
---|
| 31 | |
---|
| 32 | CVS info |
---|
[6] | 33 | $Date: 2002/02/21 13:21:52 $ |
---|
| 34 | $Revision: 1.4 $ |
---|
[2] | 35 | """ |
---|
| 36 | import time |
---|
| 37 | import socket |
---|
| 38 | import sys |
---|
| 39 | import os |
---|
| 40 | import string |
---|
| 41 | import syslog |
---|
[3] | 42 | import getopt |
---|
[2] | 43 | |
---|
| 44 | # DEBUG |
---|
| 45 | # |
---|
| 46 | DEBUG=1 |
---|
| 47 | |
---|
| 48 | # Some Global Constants |
---|
| 49 | # |
---|
| 50 | BUFSIZE=4096 |
---|
| 51 | STDIN=0 |
---|
| 52 | STDOUT=1 |
---|
[3] | 53 | SHORTOPT_LIST='d:' |
---|
| 54 | LONGOPT_LIST=['directory='] |
---|
[2] | 55 | |
---|
| 56 | PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' |
---|
| 57 | |
---|
| 58 | # Give the current time |
---|
| 59 | # |
---|
| 60 | def now(): |
---|
| 61 | return time.ctime(time.time()) |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | def remove_link(filename): |
---|
| 65 | """This removes the pxe config filename for the host that is connected: |
---|
| 66 | filename : string |
---|
| 67 | """ |
---|
| 68 | |
---|
| 69 | file = os.path.join(PXE_CONF_DIR, filename) |
---|
| 70 | |
---|
| 71 | if DEBUG: |
---|
| 72 | print 'file = %s' %file |
---|
| 73 | |
---|
| 74 | if not os.path.exists(file): return |
---|
| 75 | |
---|
| 76 | if os.path.islink(file): |
---|
| 77 | try: |
---|
| 78 | os.unlink(file) |
---|
| 79 | syslog.openlog("pxeconfigd") |
---|
| 80 | syslog.syslog(syslog.LOG_INFO, file) |
---|
| 81 | syslog.closelog() |
---|
| 82 | except OSError: |
---|
| 83 | err_msg = "No permission at directory: %s" %PXE_CONF_DIR |
---|
| 84 | os.write(STDOUT, err_msg) |
---|
| 85 | sys.exit(1) |
---|
| 86 | |
---|
| 87 | # This function handles the client connection. It closes |
---|
| 88 | # the connection if there is no data |
---|
| 89 | # |
---|
| 90 | def handleConnection(): |
---|
| 91 | """Determines which host connects to the server |
---|
| 92 | """ |
---|
| 93 | |
---|
| 94 | # Determine client address |
---|
| 95 | # |
---|
| 96 | try: |
---|
| 97 | client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, |
---|
| 98 | socket.SOCK_STREAM) |
---|
| 99 | client_ip = client_addr.getpeername()[0] |
---|
| 100 | |
---|
| 101 | except socket.error, detail: |
---|
| 102 | print "pxeconfigd can only be started from inetd!!!" |
---|
| 103 | sys.exit(1) |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | # translate ip address ---> hex address |
---|
| 107 | # |
---|
| 108 | d = string.split(client_ip, '.') |
---|
| 109 | client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3])) |
---|
| 110 | |
---|
| 111 | if DEBUG: |
---|
| 112 | print 'ip = %s, hex = %s' %(client_ip, client_haddr) |
---|
| 113 | |
---|
| 114 | remove_link(client_haddr) |
---|
| 115 | sys.exit(0) |
---|
| 116 | |
---|
[3] | 117 | def check_args(argv): |
---|
| 118 | """ |
---|
| 119 | Must we use another directory for the PXE configuration |
---|
| 120 | """ |
---|
[4] | 121 | global PXE_CONF_DIR |
---|
| 122 | |
---|
[3] | 123 | try: |
---|
| 124 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
| 125 | except getopt.error, detail: |
---|
| 126 | print __doc__ |
---|
| 127 | print detail |
---|
| 128 | sys.exit(1) |
---|
[2] | 129 | |
---|
[3] | 130 | if opts: |
---|
[4] | 131 | opt, PXE_CONF_DIR = opts[0] |
---|
[3] | 132 | |
---|
[2] | 133 | def server(): |
---|
| 134 | """Start the daemon |
---|
| 135 | """ |
---|
[3] | 136 | check_args(sys.argv) |
---|
[2] | 137 | |
---|
| 138 | if not os.path.isdir(PXE_CONF_DIR): |
---|
| 139 | os.write(STDOUT, "PXE booting is not configured") |
---|
| 140 | sys.exit(1) |
---|
| 141 | |
---|
| 142 | handleConnection() |
---|
| 143 | |
---|
| 144 | if __name__ == '__main__': |
---|
| 145 | server() |
---|