1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 """RAPI users config file parser.
31
32 """
33
34 import errno
35 import logging
36
37 from ganeti import utils
38
39
41 """Data structure for users from password file.
42
43 """
44 - def __init__(self, name, password, options):
48
49
51 """Parses the contents of a password file.
52
53 Lines in the password file are of the following format::
54
55 <username> <password> [options]
56
57 Fields are separated by whitespace. Username and password are mandatory,
58 options are optional and separated by comma (','). Empty lines and comments
59 ('#') are ignored.
60
61 @type contents: str
62 @param contents: Contents of password file
63 @rtype: dict
64 @return: Dictionary containing L{PasswordFileUser} instances
65
66 """
67 users = {}
68
69 for line in utils.FilterEmptyLinesAndComments(contents):
70 parts = line.split(None, 2)
71 if len(parts) < 2:
72
73
74 logging.warning("Ignoring non-comment line with less than two fields")
75 continue
76
77 name = parts[0]
78 password = parts[1]
79
80
81 options = []
82 if len(parts) >= 3:
83 for part in parts[2].split(","):
84 options.append(part.strip())
85 else:
86 logging.warning("Ignoring values for user '%s': %s", name, parts[3:])
87
88 users[name] = PasswordFileUser(name, password, options)
89
90 return users
91
92
95 """Initializes this class.
96
97 """
98 self._users = None
99
100 - def Get(self, username):
101 """Checks whether a user exists.
102
103 """
104 if self._users:
105 return self._users.get(username, None)
106 else:
107 return None
108
109 - def Load(self, filename):
110 """Loads a file containing users and passwords.
111
112 @type filename: string
113 @param filename: Path to file
114
115 """
116 logging.info("Reading users file at %s", filename)
117 try:
118 try:
119 contents = utils.ReadFile(filename)
120 except EnvironmentError, err:
121 self._users = None
122 if err.errno == errno.ENOENT:
123 logging.warning("No users file at %s", filename)
124 else:
125 logging.warning("Error while reading %s: %s", filename, err)
126 return False
127
128 users = ParsePasswordFile(contents)
129
130 except Exception, err:
131
132 logging.error("Error while parsing %s: %s", filename, err)
133 return False
134
135 self._users = users
136
137 return True
138