1 karl 1.19 //%2004////////////////////////////////////////////////////////////////////////
|
2 mike 1.2 //
|
3 karl 1.19 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.15 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.19 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 mike 1.2 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to
12 // deal in the Software without restriction, including without limitation the
13 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 // sell copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
|
16 kumpf 1.6 //
|
17 mike 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
18 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
19 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
20 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 //==============================================================================
27 //
28 // Author: Sushma Fernandes, Hewlett Packard Company (sushma_fernandes@hp.com)
29 //
|
30 a.arora 1.17 // Modified By: Amit K Arora, IBM (amita@in.ibm.com) for Bug#1519
|
31 mike 1.2 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34
35 #include <cctype>
36 #include <fstream>
37
38 #include <Pegasus/Common/FileSystem.h>
39 #include <Pegasus/Common/Destroyer.h>
40 #include <Pegasus/Common/Logger.h>
41 #include <Pegasus/Common/Tracer.h>
|
42 david 1.13 #if defined(PEGASUS_OS_OS400)
43 #include "OS400ConvertChar.h"
44 #endif
|
45 mike 1.2
46 #include <Pegasus/Security/UserManager/PasswordFile.h>
47 #include <Pegasus/Security/UserManager/UserExceptions.h>
48
49
50 PEGASUS_USING_STD;
51
52 PEGASUS_NAMESPACE_BEGIN
53
54 const char COLON = ':';
55
56
57 ////////////////////////////////////////////////////////////////////////////////
58 //
59 // PasswordFile Class
60 //
61 ////////////////////////////////////////////////////////////////////////////////
62
63
64
65 /**
66 mike 1.2 Constructor.
67 */
68 PasswordFile::PasswordFile (const String& fileName)
69 {
|
70 kumpf 1.7 PEG_METHOD_ENTER(TRC_USER_MANAGER, "PasswordFile::PasswordFile");
|
71 mike 1.2
72 _passwordFile = fileName;
73
74 _passwordBackupFile = fileName + ".bak";
75
|
76 kumpf 1.3 try
77 {
78 PasswordTable pt;
79 load(pt);
80 }
|
81 david.dillard 1.18 catch(const NoSuchFile&)
|
82 mike 1.2 {
|
83 humberto 1.12 //l10n
84 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
85 //"Password file not found : $0.", _passwordFile);
86 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
87 //"Creating blank password file.");
88 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
89 "Security.UserManager.PasswordFile.PWD_FILE_NOT_FOUND",
|
90 kumpf 1.3 "Password file not found : $0.", _passwordFile);
|
91 humberto 1.12 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
92 "Security.UserManager.PasswordFile.CREATING_BLANK_PWD_FILE",
|
93 kumpf 1.3 "Creating blank password file.");
|
94 mike 1.2 PasswordTable pt;
95 save(pt);
96 }
|
97 david.dillard 1.18 catch (const Exception&)
|
98 kumpf 1.3 {
|
99 david.dillard 1.18 throw;
|
100 kumpf 1.3 }
|
101 kumpf 1.7 PEG_METHOD_EXIT();
|
102 mike 1.2 }
103
104 /**
105 Destructor.
106 */
107 PasswordFile::~PasswordFile ()
108 {
|
109 kumpf 1.7 PEG_METHOD_ENTER(TRC_USER_MANAGER, "PasswordFile::~PasswordFile");
|
110 mike 1.2
|
111 kumpf 1.7 PEG_METHOD_EXIT();
|
112 mike 1.2 }
113
114 /**
115 Load the username and password from the password file.
116 */
117 void PasswordFile::load (PasswordTable& passwordTable)
118 {
119 String line;
120
|
121 kumpf 1.7 PEG_METHOD_ENTER(TRC_USER_MANAGER, "PasswordFile::load");
|
122 mike 1.2
|
123 kumpf 1.3 //
124 // Check if the backup file exists, if it does use the backup file
125 // If not try to use the password file
126 //
127 if (FileSystem::exists(_passwordBackupFile))
128 {
129 if (FileSystem::exists(_passwordFile))
130 {
131 if (! FileSystem::removeFile(_passwordFile))
132 {
133 throw CannotRemoveFile(_passwordFile);
134 }
135 }
|
136 humberto 1.12 //l10n
137 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
138 //"Trying to use the backup file : $0.", _passwordBackupFile);
139 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
140 "Security.UserManager.PasswordFile.TRYING_TO_BACKUP_FILE",
|
141 kumpf 1.3 "Trying to use the backup file : $0.", _passwordBackupFile);
142 if (! FileSystem::renameFile(_passwordBackupFile, _passwordFile))
143 {
|
144 humberto 1.12 //l10n
145 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
146 //"Unable to use the backup file : $0.", _passwordBackupFile);
147 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
148 "Security.UserManager.PasswordFile.CANNOT_USE_BACKUP_FILE",
149 "Unable to use the backup file : $0.", _passwordBackupFile);
|
150 kumpf 1.3 throw CannotRenameFile(_passwordBackupFile);
151 }
|
152 humberto 1.12 //l10n
153 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
154 //"Recovered using the backup file : $0.", _passwordBackupFile);
155 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
156 "Security.UserManager.PasswordFile.RECOVERED_USING_BACKUP_FILE",
|
157 kumpf 1.3 "Recovered using the backup file : $0.", _passwordBackupFile);
158 }
159 if (! FileSystem::exists(_passwordFile))
160 {
161 throw NoSuchFile(_passwordFile);
162 }
163
|
164 mike 1.2 //
165 // Open the password file
166 //
|
167 david 1.13 #if defined(PEGASUS_OS_OS400)
|
168 david 1.16 ifstream ifs(_passwordFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
|
169 david 1.13 #else
|
170 david 1.16 ifstream ifs(_passwordFile.getCString());
|
171 david 1.13 #endif
|
172 mike 1.2 if (!ifs)
173 {
|
174 humberto 1.12 //l10n
175 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
176 //"Error opening password file : $0.", _passwordFile);
177 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
178 "Security.UserManager.PasswordFile.ERROR_OPENING_PWD_FILE",
|
179 kumpf 1.3 "Error opening password file : $0.", _passwordFile);
180 return;
|
181 mike 1.2 }
182
183 //
184 // Read each line of the file
185 //
186 for (Uint32 lineNumber = 1; GetLine(ifs, line); lineNumber++)
187 {
188 // Get the userName and password
189
190 //
191 // Skip leading whitespace
192 //
|
193 kumpf 1.10 const Char16* p = line.getChar16Data();
|
194 mike 1.2
195 while (*p && isspace(*p))
196 {
197 p++;
198 }
199
200 if (!*p)
201 {
202 continue;
203 }
204
205 //
206 // Get the userName
207 //
208 String userName = String::EMPTY;
209
|
210 kumpf 1.8 userName.append(*p++);
|
211 mike 1.2
212 while (isalnum(*p))
213 {
|
214 kumpf 1.8 userName.append(*p++);
|
215 mike 1.2 }
216
217 //
218 // Skip whitespace after user name
219 //
220 while (*p && isspace(*p))
221 {
222 p++;
223 }
224
225 //
226 // Expect a colon sign
227 //
228 if (*p != COLON)
229 {
230 //
231 // Did not find Colon, log a message and skip entry
|
232 humberto 1.12 //l10n
233 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
234 //"Error in reading password entry for : $0.", userName);
235 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
236 "Security.UserManager.PasswordFile.ERROR_READING_PWD_ENTRY",
237 "Error in reading password entry for : $0.", userName);
|
238 mike 1.2 continue;
239 }
240
241 p++;
242
243 //
244 // Skip whitespace after : sign
245 //
246 while (*p && isspace(*p))
247 {
248 p++;
249 }
250
251 //
252 // Get the password
253 //
254 String password = String::EMPTY;
255
256 while (*p)
257 {
|
258 kumpf 1.8 password.append(*p++);
|
259 mike 1.2 }
260
261 //
262 // Store the user name and password in the table
263 //
264 if (!passwordTable.insert(userName, password))
265 {
266 //
267 // Duplicate entry for user, ignore the new entry.
268 //
|
269 humberto 1.12 //l10n
270 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
271 //"Duplicate user: $0.", userName);
272 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::INFORMATION,
273 "Security.UserManager.PasswordFile.DUPLICATE_USER",
274 "Duplicate user: $0.", userName);
|
275 mike 1.2 }
276 }
277
278 ifs.close();
|
279 kumpf 1.7 PEG_METHOD_EXIT();
|
280 mike 1.2 }
281
282
283 /**
284 Save the username and password to the password file.
285 */
286 void PasswordFile::save (PasswordTable& passwordTable)
287 {
|
288 kumpf 1.7 PEG_METHOD_ENTER(TRC_USER_MANAGER, "PasswordFile::save");
|
289 mike 1.2
290 //
|
291 kumpf 1.3 // Check if backup password file exists, if it does remove the password file
292 // If it does not rename the password file to password backup file
|
293 mike 1.2 //
294 if (FileSystem::exists(_passwordBackupFile))
295 {
|
296 kumpf 1.3 if ( FileSystem::exists(_passwordFile))
|
297 mike 1.2 {
|
298 kumpf 1.3 if ( ! FileSystem::removeFile(_passwordFile))
299 {
|
300 humberto 1.12 //l10n
301 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
302 //"Cannot remove password file : $0.", _passwordFile);
303 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
304 "Security.UserManager.PasswordFile.CANNOT_REMOVE_PWD_FILE",
305 "Cannot remove password file : $0.", _passwordFile);
|
306 kumpf 1.3 throw CannotRemoveFile(_passwordFile);
307 }
|
308 mike 1.2 }
309 }
|
310 kumpf 1.3 else
|
311 mike 1.2 {
|
312 kumpf 1.3 if ( FileSystem::exists(_passwordFile))
|
313 mike 1.2 {
|
314 kumpf 1.3 if ( ! FileSystem::renameFile(_passwordFile, _passwordBackupFile))
315 {
|
316 humberto 1.12 //l10n
317 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
318 //"Cannot rename password file : $0.", _passwordFile);
319 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
320 "Security.UserManager.PasswordFile.CANNOT_RENAME_PWD_FILE",
321 "Cannot rename password file : $0.", _passwordFile);
|
322 kumpf 1.3 throw CannotRenameFile(_passwordFile);
323 }
|
324 mike 1.2 }
325 }
326
327 //
|
328 kumpf 1.3 // Open the password file for writing
|
329 mike 1.2 //
|
330 david 1.13 #if defined(PEGASUS_OS_OS400)
|
331 david 1.16 ofstream ofs(_passwordFile.getCString(), PEGASUS_STD(_CCSID_T(1208)));
|
332 david 1.13 #else
|
333 david 1.16 ofstream ofs(_passwordFile.getCString());
|
334 david 1.13 #endif
|
335 mike 1.2 if (!ofs)
336 {
|
337 kumpf 1.7 PEG_METHOD_EXIT();
|
338 mike 1.2 throw CannotOpenFile(getFileName());
339 }
340
341 ofs.clear();
342
343 //
344 // Save user names and passwords to the new file
345 //
346 for (PasswordTable::Iterator i = passwordTable.start(); i; i++)
347 {
348 ofs << i.key() << ":" << i.value() << endl;
349 }
350
351 ofs.close();
352
|
353 kumpf 1.3 if ( FileSystem::exists(_passwordBackupFile))
|
354 mike 1.2 {
|
355 kumpf 1.3 if ( ! FileSystem::removeFile(_passwordBackupFile))
|
356 mike 1.2 {
|
357 humberto 1.12 //l10n
358 //Logger::put(Logger::ERROR_LOG, System::CIMSERVER,
359 //Logger::SEVERE,
360 // "Cannot remove backup password file : $0.",
361 //_passwordBackupFile);
362 Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
363 "Security.UserManager.PasswordFile.CANNOT_REMOVE_BACKUP_PWD_FILE",
364 "Cannot remove backup password file : $0.", _passwordBackupFile);
|
365 kumpf 1.3 throw CannotRemoveFile(_passwordBackupFile);
|
366 mike 1.2 }
367 }
|
368 kumpf 1.7 PEG_METHOD_EXIT();
|
369 mike 1.2 }
370
371 PEGASUS_NAMESPACE_END
372
|