source: trunk/pxeconfig/pxeconfigd @ 26

Last change on this file since 26 was 26, checked in by sscpbas, 21 years ago

* empty log message *

File size: 3.8 KB
Line 
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"""
22Author: Bas van der Vlies <basv@sara.nl>
23Date  : 12 February 2002
24Desc. : 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          -V/--version
34            Prints the version number and exits
35         
36        Note:
37          This server can ONLY be started from inetd.
38
39CVS info
40  $Date: 2002/11/29 16:15:50 $
41  $Revision: 1.7 $
42"""
43import time
44import socket
45import sys
46import os
47import string
48import syslog
49import getopt
50
51# DEBUG
52#
53DEBUG=1
54
55# Some Global Constants
56#
57BUFSIZE=4096
58STDIN=0
59STDOUT=1
60SHORTOPT_LIST='Vd:'
61LONGOPT_LIST=['version', 'directory=']
62
63PXE_CONF_DIR = '/tftpboot/pxelinux.cfg'
64VERSION = '0.4.1'
65
66# Give the current time
67#
68def now():
69    return time.ctime(time.time())
70
71
72def remove_link(filename):
73  """This removes the pxe config filename for the host that is connected:
74      filename : string
75  """
76
77  file = os.path.join(PXE_CONF_DIR, filename)
78
79  if DEBUG:
80    print 'file = %s' %file
81
82  if not os.path.exists(file): return
83
84  if os.path.islink(file): 
85    try:
86      os.unlink(file)
87      syslog.openlog("pxeconfigd")
88      syslog.syslog(syslog.LOG_INFO, file)
89      syslog.closelog()
90    except OSError:
91      err_msg = "No permission at directory: %s" %PXE_CONF_DIR
92      os.write(STDOUT, err_msg)
93      sys.exit(1)
94
95# This function handles the client connection. It closes
96# the connection if there is no data
97#
98def handleConnection():
99  """Determines which host connects to the server
100  """
101
102  # Determine client address
103  #
104  try:
105    client_addr = socket.fromfd(sys.stdin.fileno(), socket.AF_INET, 
106                          socket.SOCK_STREAM)
107    client_ip = client_addr.getpeername()[0] 
108
109  except socket.error, detail:
110    print "pxeconfigd can only be started from inetd!!!"
111    sys.exit(1)
112
113
114  # translate ip address ---> hex address
115  #
116  d = string.split(client_ip, '.')
117  client_haddr = '%02X%02X%02X%02X' %(int(d[0]), int(d[1]), int(d[2]), int(d[3]))
118
119  if DEBUG:
120    print 'ip = %s, hex = %s' %(client_ip, client_haddr)
121
122  remove_link(client_haddr)
123  sys.exit(0)
124
125def check_args(argv):
126  """
127  Must we use another directory for the PXE configuration
128  """
129  global PXE_CONF_DIR
130
131  try:
132    opts, args = getopt.getopt(argv[1:], SHORTOPT_LIST, LONGOPT_LIST)
133  except getopt.error, detail:
134    print __doc__
135    print detail
136    sys.exit(1)
137
138  for opt, value in opts:
139
140    if opt in ['-d', '--directory']:
141      if not os.path.isdir(value):
142        os.write(STDOUT, "PXE booting is not configured")
143        sys.exit(1)
144      else:
145        PXE_CONF_DIR = value
146
147    elif opt in ['-V', '--version']:
148      print VERSION
149      sys.exit(0)
150
151def server():
152  """Start the daemon
153  """
154  check_args(sys.argv)
155  handleConnection()
156
157if __name__ == '__main__':
158  server()
Note: See TracBrowser for help on using the repository browser.