[2] | 1 | #!/usr/bin/env python |
---|
[6] | 2 | # |
---|
[14] | 3 | # Copyright (C) 2002 |
---|
[6] | 4 | # |
---|
[14] | 5 | # This file is part of the pxeconfig utils |
---|
[6] | 6 | # |
---|
[14] | 7 | # This program is free software; you can redistribute it and/or modify it |
---|
| 8 | # under the terms of the GNU General Public License as published by the |
---|
| 9 | # Free Software Foundation; either version 2, or (at your option) any |
---|
| 10 | # later version. |
---|
[6] | 11 | # |
---|
[14] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | # |
---|
[29] | 21 | # SVN info |
---|
| 22 | # $Id: pxeconfigd 45 2004-09-03 11:40:04Z bas $ |
---|
[2] | 23 | """ |
---|
| 24 | Author: Bas van der Vlies <basv@sara.nl> |
---|
| 25 | Date : 12 February 2002 |
---|
| 26 | Desc. : This script is used to control how a node is booted with PXE |
---|
[3] | 27 | enabled network cards. The node boots and fetch a pxe |
---|
| 28 | config file which tells how the node must boot. This daemon |
---|
| 29 | enables a client to remove his/here pxe config file. With the |
---|
| 30 | next boot it will use the default one. |
---|
| 31 | |
---|
| 32 | command line option: |
---|
| 33 | -d/--directory <dir> |
---|
| 34 | Where <dir> is the directory where the pxe config files reside. |
---|
[26] | 35 | -V/--version |
---|
| 36 | Prints the version number and exits |
---|
[2] | 37 | |
---|
| 38 | Note: |
---|
| 39 | This server can ONLY be started from inetd. |
---|
| 40 | """ |
---|
| 41 | import time |
---|
| 42 | import socket |
---|
| 43 | import sys |
---|
| 44 | import os |
---|
| 45 | import string |
---|
| 46 | import syslog |
---|
[3] | 47 | import getopt |
---|
[2] | 48 | |
---|
| 49 | # DEBUG |
---|
| 50 | # |
---|
| 51 | DEBUG=1 |
---|
| 52 | |
---|
| 53 | # Some Global Constants |
---|
| 54 | # |
---|
| 55 | BUFSIZE=4096 |
---|
| 56 | STDIN=0 |
---|
| 57 | STDOUT=1 |
---|
[25] | 58 | SHORTOPT_LIST='Vd:' |
---|
| 59 | LONGOPT_LIST=['version', 'directory='] |
---|
[2] | 60 | |
---|
| 61 | PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' |
---|
[45] | 62 | VERSION = '0.4.4' |
---|
[2] | 63 | |
---|
| 64 | # Give the current time |
---|
| 65 | # |
---|
| 66 | def now(): |
---|
| 67 | return time.ctime(time.time()) |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | def remove_link(filename): |
---|
| 71 | """This removes the pxe config filename for the host that is connected: |
---|
| 72 | filename : string |
---|
| 73 | """ |
---|
| 74 | |
---|
| 75 | file = os.path.join(PXE_CONF_DIR, filename) |
---|
| 76 | |
---|
| 77 | if DEBUG: |
---|
| 78 | print 'file = %s' %file |
---|
| 79 | |
---|
| 80 | if not os.path.exists(file): return |
---|
| 81 | |
---|
| 82 | if os.path.islink(file): |
---|
| 83 | try: |
---|
| 84 | os.unlink(file) |
---|
| 85 | syslog.openlog("pxeconfigd") |
---|
| 86 | syslog.syslog(syslog.LOG_INFO, file) |
---|
| 87 | syslog.closelog() |
---|
| 88 | except OSError: |
---|
| 89 | err_msg = "No permission at directory: %s" %PXE_CONF_DIR |
---|
| 90 | os.write(STDOUT, err_msg) |
---|
| 91 | sys.exit(1) |
---|
| 92 | |
---|
| 93 | # This function handles the client connection. It closes |
---|
| 94 | # the connection if there is no data |
---|
| 95 | # |
---|
| 96 | def handleConnection(): |
---|
| 97 | """Determines which host connects to the server |
---|
| 98 | """ |
---|
| 99 | |
---|
| 100 | # Determine client address |
---|
| 101 | # |
---|
| 102 | try: |
---|
| 103 | client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, |
---|
| 104 | socket.SOCK_STREAM) |
---|
| 105 | client_ip = client_addr.getpeername()[0] |
---|
| 106 | |
---|
| 107 | except socket.error, detail: |
---|
| 108 | print "pxeconfigd can only be started from inetd!!!" |
---|
| 109 | sys.exit(1) |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | # translate ip address ---> hex address |
---|
| 113 | # |
---|
| 114 | d = string.split(client_ip, '.') |
---|
| 115 | client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3])) |
---|
| 116 | |
---|
| 117 | if DEBUG: |
---|
| 118 | print 'ip = %s, hex = %s' %(client_ip, client_haddr) |
---|
| 119 | |
---|
| 120 | remove_link(client_haddr) |
---|
| 121 | sys.exit(0) |
---|
| 122 | |
---|
[3] | 123 | def check_args(argv): |
---|
| 124 | """ |
---|
| 125 | Must we use another directory for the PXE configuration |
---|
| 126 | """ |
---|
[4] | 127 | global PXE_CONF_DIR |
---|
| 128 | |
---|
[3] | 129 | try: |
---|
| 130 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
| 131 | except getopt.error, detail: |
---|
| 132 | print __doc__ |
---|
| 133 | print detail |
---|
| 134 | sys.exit(1) |
---|
[2] | 135 | |
---|
[25] | 136 | for opt, value in opts: |
---|
[3] | 137 | |
---|
[25] | 138 | if opt in ['-d', '--directory']: |
---|
| 139 | if not os.path.isdir(value): |
---|
| 140 | os.write(STDOUT, "PXE booting is not configured") |
---|
| 141 | sys.exit(1) |
---|
| 142 | else: |
---|
| 143 | PXE_CONF_DIR = value |
---|
| 144 | |
---|
| 145 | elif opt in ['-V', '--version']: |
---|
| 146 | print VERSION |
---|
| 147 | sys.exit(0) |
---|
| 148 | |
---|
[2] | 149 | def server(): |
---|
| 150 | """Start the daemon |
---|
| 151 | """ |
---|
[3] | 152 | check_args(sys.argv) |
---|
[2] | 153 | handleConnection() |
---|
| 154 | |
---|
| 155 | if __name__ == '__main__': |
---|
| 156 | server() |
---|