bad3c16d197696de6aaf24f9f2da8fc2a02512db
[bootmanager.git] / source / steps / AuthenticateWithPLC.py
1 import os
2
3 from Exceptions import *
4 import BootAPI
5 import StartDebug
6
7
8 AUTH_FAILURE_COUNT_FILE= "/tmp/authfailurecount"
9
10
11 def Run( vars, log ):
12     """
13     Authenticate this node with PLC. This ensures that the node can operate
14     as normal, and that our management authority has authorized it.
15
16     For this, just call the PLC api function BootCheckAuthentication
17
18     Return 1 if authorized, a BootManagerException if not or the
19     call fails entirely.
20
21     If there are two consecutive authentication failures, put the node
22     into debug mode and exit the bootmanager.
23
24     Expect the following variables from the store:
25     NUM_AUTH_FAILURES_BEFORE_DEBUG    How many failures before debug
26     """
27
28     log.write( "\n\nStep: Authenticating node with PLC.\n" )
29
30     # make sure we have the variables we need
31     try:
32         NUM_AUTH_FAILURES_BEFORE_DEBUG= int(vars["NUM_AUTH_FAILURES_BEFORE_DEBUG"])
33     except KeyError, var:
34         raise BootManagerException, "Missing variable in vars: %s\n" % var
35     except ValueError, var:
36         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
37
38     try:
39         authorized= BootAPI.call_api_function( vars, "BootCheckAuthentication", () )
40         if authorized == 1:
41             log.write( "Authentication successful.\n" )
42
43             try:
44                 os.unlink( AUTH_FAILURE_COUNT_FILE )
45             except OSError, e:
46                 pass
47             
48             return 1
49     except BootManagerException, e:
50         log.write( "Authentication failed: %s.\n" % e )
51
52     # increment auth failure
53     auth_failure_count= 0
54     try:
55         auth_failure_count= int(file(AUTH_FAILURE_COUNT_FILE,"r").read().strip())
56     except IOError:
57         pass
58     except ValueError:
59         pass
60
61     auth_failure_count += 1
62
63     try:
64         fail_file= file(AUTH_FAILURE_COUNT_FILE,"w")
65         fail_file.write( str(auth_failure_count) )
66         fail_file.close()
67     except IOError:
68         pass
69
70     if auth_failure_count >= NUM_AUTH_FAILURES_BEFORE_DEBUG:
71         log.write( "Maximum number of authentication failures reached.\n" )
72         log.write( "Canceling boot process and going into debug mode.\n" )
73
74         StartDebug.Run( vars, log )
75
76     raise BootManagerException, "Unable to authenticate node."
77     
78