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