1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # Copyright (C) 2002 |
---|
4 | # |
---|
5 | # This file is part of the pxeconfig utils |
---|
6 | # |
---|
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. |
---|
11 | # |
---|
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 | # |
---|
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 |
---|
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: |
---|
31 | -d/--directory <dir> |
---|
32 | Where <dir> is the directory where the pxe config files reside. |
---|
33 | |
---|
34 | Note: |
---|
35 | This server can ONLY be started from inetd. |
---|
36 | |
---|
37 | CVS info |
---|
38 | $Date: 2002/08/15 06:36:47 $ |
---|
39 | $Revision: 1.5 $ |
---|
40 | """ |
---|
41 | import time |
---|
42 | import socket |
---|
43 | import sys |
---|
44 | import os |
---|
45 | import string |
---|
46 | import syslog |
---|
47 | import getopt |
---|
48 | |
---|
49 | # DEBUG |
---|
50 | # |
---|
51 | DEBUG=1 |
---|
52 | |
---|
53 | # Some Global Constants |
---|
54 | # |
---|
55 | BUFSIZE=4096 |
---|
56 | STDIN=0 |
---|
57 | STDOUT=1 |
---|
58 | SHORTOPT_LIST='d:' |
---|
59 | LONGOPT_LIST=['directory='] |
---|
60 | |
---|
61 | PXE_CONF_DIR = '/tftpboot/pxelinux.cfg' |
---|
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 | # |
---|
95 | def handleConnection(): |
---|
96 | """Determines which host connects to the server |
---|
97 | """ |
---|
98 | |
---|
99 | # Determine client address |
---|
100 | # |
---|
101 | try: |
---|
102 | client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, |
---|
103 | socket.SOCK_STREAM) |
---|
104 | client_ip = client_addr.getpeername()[0] |
---|
105 | |
---|
106 | except socket.error, detail: |
---|
107 | print "pxeconfigd can only be started from inetd!!!" |
---|
108 | sys.exit(1) |
---|
109 | |
---|
110 | |
---|
111 | # translate ip address ---> hex address |
---|
112 | # |
---|
113 | d = string.split(client_ip, '.') |
---|
114 | client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3])) |
---|
115 | |
---|
116 | if DEBUG: |
---|
117 | print 'ip = %s, hex = %s' %(client_ip, client_haddr) |
---|
118 | |
---|
119 | remove_link(client_haddr) |
---|
120 | sys.exit(0) |
---|
121 | |
---|
122 | def check_args(argv): |
---|
123 | """ |
---|
124 | Must we use another directory for the PXE configuration |
---|
125 | """ |
---|
126 | global PXE_CONF_DIR |
---|
127 | |
---|
128 | try: |
---|
129 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
130 | except getopt.error, detail: |
---|
131 | print __doc__ |
---|
132 | print detail |
---|
133 | sys.exit(1) |
---|
134 | |
---|
135 | if opts: |
---|
136 | opt, PXE_CONF_DIR = opts[0] |
---|
137 | |
---|
138 | def server(): |
---|
139 | """Start the daemon |
---|
140 | """ |
---|
141 | check_args(sys.argv) |
---|
142 | |
---|
143 | if not os.path.isdir(PXE_CONF_DIR): |
---|
144 | os.write(STDOUT, "PXE booting is not configured") |
---|
145 | sys.exit(1) |
---|
146 | |
---|
147 | handleConnection() |
---|
148 | |
---|
149 | if __name__ == '__main__': |
---|
150 | server() |
---|