(file) Return to PasswordFile.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Security / UserManager

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2