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 |
---|
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. |
---|
14 | |
---|
15 | Note: |
---|
16 | This server can ONLY be started from inetd. |
---|
17 | |
---|
18 | CVS info |
---|
19 | $Date: 2002/02/19 09:38:10 $ |
---|
20 | $Revision: 1.2 $ |
---|
21 | """ |
---|
22 | import time |
---|
23 | import socket |
---|
24 | import sys |
---|
25 | import os |
---|
26 | import string |
---|
27 | import syslog |
---|
28 | import getopt |
---|
29 | |
---|
30 | # DEBUG |
---|
31 | # |
---|
32 | DEBUG=1 |
---|
33 | |
---|
34 | # Some Global Constants |
---|
35 | # |
---|
36 | BUFSIZE=4096 |
---|
37 | STDIN=0 |
---|
38 | STDOUT=1 |
---|
39 | SHORTOPT_LIST='d:' |
---|
40 | LONGOPT_LIST=['directory='] |
---|
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 | |
---|
103 | def check_args(argv): |
---|
104 | """ |
---|
105 | Must we use another directory for the PXE configuration |
---|
106 | """ |
---|
107 | try: |
---|
108 | opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST) |
---|
109 | except getopt.error, detail: |
---|
110 | print __doc__ |
---|
111 | print detail |
---|
112 | sys.exit(1) |
---|
113 | |
---|
114 | if opts: |
---|
115 | opt, PXE_CONFIG_DIR = opts[0] |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | def server(): |
---|
122 | """Start the daemon |
---|
123 | """ |
---|
124 | check_args(sys.argv) |
---|
125 | |
---|
126 | if not os.path.isdir(PXE_CONF_DIR): |
---|
127 | os.write(STDOUT, "PXE booting is not configured") |
---|
128 | sys.exit(1) |
---|
129 | |
---|
130 | handleConnection() |
---|
131 | |
---|
132 | if __name__ == '__main__': |
---|
133 | server() |
---|