(file) Return to SystemWindows.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.32 //%2003////////////////////////////////////////////////////////////////////////
  2 mike  1.13 //
  3 karl  1.32 // 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            // IBM Corp.; EMC Corporation, The Open Group.
  7 mike  1.13 //
  8            // Permission is hereby granted, free of charge, to any person obtaining a copy
  9 mike  1.14 // of this software and associated documentation files (the "Software"), to
 10            // deal in the Software without restriction, including without limitation the
 11            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12 mike  1.13 // sell copies of the Software, and to permit persons to whom the Software is
 13            // furnished to do so, subject to the following conditions:
 14 kumpf 1.25 // 
 15 mike  1.14 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 16 mike  1.13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 17            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 18 mike  1.14 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 19            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 21 mike  1.13 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23            //
 24            //==============================================================================
 25            //
 26            // Author: Mike Brasher (mbrasher@bmc.com)
 27            //
 28 mike  1.14 // Modified By: Sushma Fernandes (sushma_fernandes@hp.com)
 29            //
 30            //              Nag Boranna, Hewlett-Packard Company (nagaraja_boranna@hp.com)
 31 mike  1.13 //
 32 david 1.29 // Modified By: Dave Rosckes (rosckes@us.ibm.com)
 33            //
 34 mike  1.13 //%/////////////////////////////////////////////////////////////////////////////
 35            
 36            #include "System.h"
 37            
 38            PEGASUS_NAMESPACE_BEGIN
 39            
 40            #include <windows.h>
 41 tony  1.31 #ifndef _WINSOCKAPI_
 42            #include <winsock2.h>
 43            #endif
 44 mike  1.21 #include <fcntl.h>
 45 mike  1.13 #include <sys/types.h>
 46            #include <time.h>
 47            #include <sys/timeb.h>
 48            #include <io.h>
 49 tony  1.33 #include <conio.h>
 50 mike  1.13 #include <direct.h>
 51            #include <sys/types.h>
 52 mday  1.19 #include <windows.h>
 53 kumpf 1.18 #include <process.h>
 54 mike  1.13 
 55            #define ACCESS_EXISTS 0
 56            #define ACCESS_WRITE 2
 57            #define ACCESS_READ 4
 58            #define ACCESS_READ_AND_WRITE 6
 59            
 60 tony  1.33 #define PW_BUFF_LEN 65
 61            
 62 mike  1.13 void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
 63            {
 64                FILETIME ft;
 65                GetSystemTimeAsFileTime(&ft);
 66                ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime };
 67                largeInt.QuadPart -= 0x19db1ded53e8000;
 68                seconds = long(largeInt.QuadPart / (10000 * 1000));
 69                milliseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10);
 70 karl  1.23     // This is a real hack. Added the following line after timevalue was
 71                // corrected and this apparently wrong. ks 7 apri 2002
 72                milliseconds = milliseconds / 1000;
 73 mike  1.13 }
 74            
 75            String System::getCurrentASCIITime()
 76            {
 77                char tmpbuf[128];
 78 kumpf 1.28     _strdate( tmpbuf );
 79                String date = tmpbuf;
 80 mike  1.13     _strtime( tmpbuf );
 81 kumpf 1.28     date.append("-");
 82                date.append(tmpbuf);
 83                return date;
 84 mike  1.13 }
 85            
 86            void System::sleep(Uint32 seconds)
 87            {
 88                Sleep(seconds * 1000);
 89            }
 90            
 91            Boolean System::exists(const char* path)
 92            {
 93                return _access(path, ACCESS_EXISTS) == 0;
 94            }
 95            
 96            Boolean System::canRead(const char* path)
 97            {
 98                return _access(path, ACCESS_READ) == 0;
 99            }
100            
101            Boolean System::canWrite(const char* path)
102            {
103                return _access(path, ACCESS_WRITE) == 0;
104            }
105 mike  1.13 
106            Boolean System::getCurrentDirectory(char* path, Uint32 size)
107            {
108                return GetCurrentDirectory(size, path) != 0;
109            }
110            
111            Boolean System::isDirectory(const char* path)
112            {
113                struct stat st;
114            
115                if (stat(path, &st) != 0)
116            	return false;
117            
118                return (st.st_mode & _S_IFDIR) != 0;
119            }
120            
121            Boolean System::changeDirectory(const char* path)
122            {
123                return chdir(path) == 0;
124            }
125            
126 mike  1.13 Boolean System::makeDirectory(const char* path)
127            {
128                return _mkdir(path) == 0;
129            }
130            
131            Boolean System::getFileSize(const char* path, Uint32& size)
132            {
133                struct stat st;
134            
135                if (stat(path, &st) != 0)
136            	return false;
137            
138                size = st.st_size;
139                return true;
140            }
141            
142            Boolean System::removeDirectory(const char* path)
143            {
144                return rmdir(path) == 0;	
145            }
146            
147 mike  1.13 Boolean System::removeFile(const char* path)
148            {
149                return unlink(path) == 0;	
150            }
151            
152            Boolean System::renameFile(const char* oldPath, const char* newPath)
153            {
154                return rename(oldPath, newPath) == 0;
155            }
156            
157            DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
158            {
159                return DynamicLibraryHandle(LoadLibrary(fileName));
160            }
161            
162 mike  1.14 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
163            {
164            	FreeLibrary(HINSTANCE(libraryHandle));
165            }
166            
167 mike  1.13 String System::dynamicLoadError(void) {
168            return String();
169            }
170            
171            DynamicSymbolHandle System::loadDynamicSymbol(
172                DynamicLibraryHandle libraryHandle,
173                const char* symbolName)
174            {
175                return DynamicSymbolHandle(GetProcAddress(
176            	(HINSTANCE)libraryHandle, symbolName));
177            }
178            
179            String System::getHostName()
180            {
181                static char hostname[64];
182            
183                if (!*hostname)
184                    gethostname(hostname, sizeof(hostname));
185            
186                return hostname;
187 kumpf 1.16 }
188            
189 kumpf 1.22 String System::getFullyQualifiedHostName ()
190            {
191                //
192                //  ATTN: Implement this method to return the fully qualified host name
193                //
194                return String::EMPTY;
195            }
196            
197            String System::getSystemCreationClassName ()
198            {
199 karl  1.34.4.1     return "CIM_ComputerSystem";
200 kumpf 1.22     }
201                
202 kumpf 1.16     Uint32 System::lookupPort(
203                    const char * serviceName,
204                    Uint32 defaultPort)
205                {
206                    Uint32 localPort;
207                
208                    struct servent *serv;
209                
210                    //
211                    // Get wbem-local port from /etc/services
212                    //
213                    if (  (serv = getservbyname(serviceName, TCP)) != NULL )
214                    {
215 tony  1.34.4.2         localPort = ntohs(serv->s_port);
216 kumpf 1.16         }
217                    else
218                    {
219                        localPort = defaultPort;
220                    }
221                
222                    return localPort;
223 mike  1.13     }
224                
225 mike  1.14     String System::getPassword(const char* prompt)
226                {
227 tony  1.33       char password[PW_BUFF_LEN] = {0};
228                  int num_chars = 0;
229                  int ch;
230                
231                  fputs(prompt, stderr);
232                
233                  while ((ch = _getch()) != '\r' &&
234                         num_chars < PW_BUFF_LEN)
235                    {
236                      // EOF
237                      if (ch == EOF)
238                        {
239                           fputs("[EOF]\n", stderr);
240                           return String::EMPTY;
241                        }
242                      // Backspace or Delete
243                      else if ((ch == '\b' || ch == 127) &&
244                               num_chars > 0) 
245                        {
246                          password[--num_chars] = '\0';
247                          fputs("\b \b", stderr);
248 tony  1.33             }
249                      // CTRL+C
250                      else if (ch == 3)
251                        {
252                          // _getch() does not catch CTRL+C
253                          fputs("^C\n", stderr);
254                          exit(-1);
255                        }
256                      // CTRL+Z
257                      else if (ch == 26)
258                        {
259                          fputs("^Z\n", stderr);
260                          return String::EMPTY;
261                        }
262                      // Esc
263                      else if (ch == 27)
264                        {
265                          fputc('\n', stderr);
266                          fputs(prompt, stderr);
267                          num_chars = 0;
268                        }
269 tony  1.33           // Function keys (0 or E0) are a guards for a Function key codes
270                      else if (ch == 0 || ch == 0xE0)
271                        {
272                          ch = (ch << 4) | _getch();
273                          // Handle DELETE, left arrow, keypad DEL, and keypad left arrow
274                          if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) &&
275                              num_chars > 0)
276                            {
277                              password[--num_chars] = '\0';
278                              fputs("\b \b", stderr);
279                            }
280                          else
281                            {
282                              fputc('\a', stderr);
283                            }
284                        }
285                      else if ((num_chars < sizeof(password) - 1) &&
286                               !iscntrl(((unsigned char)(ch))))
287                        {
288                          password[num_chars++] = ch;
289                          fputc('*', stderr);
290 tony  1.33             }
291                      else
292                        {
293                          fputc('\a', stderr);
294                        }
295                    }
296 mike  1.14     
297 tony  1.33       fputc('\n', stderr);
298                  password[num_chars] = '\0';
299                
300                  return String(password);
301 mike  1.14     }
302                
303 kumpf 1.24     String System::getEffectiveUserName()
304 mike  1.14     {
305 tony  1.33       int retcode = 0;
306                
307                  // UNLEN (256) is the limit, not including null
308                  char pUserName[256+1] = {0};
309                  DWORD nSize = sizeof(pUserName);
310 mike  1.14     
311 tony  1.33       retcode = GetUserName(pUserName, &nSize);
312                  if (retcode == 0)
313                    {
314                      // zero is failure
315                      return String();
316                    }
317                  
318                  return String(pUserName);
319 mike  1.14     }
320                
321                String System::encryptPassword(const char* password, const char* salt)
322                {
323 tony  1.33       BYTE pbBuffer[PW_BUFF_LEN] = {0};
324                  DWORD dwByteCount;
325                  char pcSalt[3] = {0};
326                
327                  strncpy(pcSalt, salt, 2);
328                  dwByteCount = strlen(password);
329                  memcpy(pbBuffer, password, dwByteCount);
330                  for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++)
331                    (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0];
332                  
333                  return String(pcSalt) + String((char *)pbBuffer);
334 mike  1.14     }
335                
336 kumpf 1.26     Boolean System::isSystemUser(const char* userName)
337 mike  1.14     {
338                    //ATTN: Implement this method to verify if user is vaild on the local system
339                    //      This is used in User Manager
340                    return true;
341                }
342                
343 kumpf 1.17     Boolean System::isPrivilegedUser(const String userName)
344 mike  1.14     {
345                    // ATTN: Implement this method to verify if user executing the current
346 kumpf 1.17         //       command is a priviliged user, when user name is not passed as
347                    //       as argument. If user name is passed the function checks 
348                    //       whether the given user is a priviliged user.
349                    //       This is used in cimuser CLI and CIMOperationRequestAuthorizer
350 mike  1.14         return true;
351                }
352 kumpf 1.20     
353                String System::getPrivilegedUserName()
354                {
355                    // ATTN-NB-03-20000304: Implement better way to get the privileged
356                    // user on the system.
357                
358                    return (String("Administrator"));
359                }
360 mike  1.14         
361 kumpf 1.15     Uint32 System::getPID()
362                {
363 kumpf 1.18         return _getpid();
364 mike  1.21     }
365                
366                Boolean System::truncateFile(
367                    const char* path, 
368                    size_t newSize)
369                {
370                    int fd = open(path, O_RDWR);
371                
372                    if (fd == -1)
373                        return false;
374                
375                    if (chsize(fd, newSize) != 0)
376                	return false;
377                
378                    close(fd);
379                    return true;
380 kumpf 1.15     }
381                
382 tony  1.27     // Is absolute path?
383                Boolean System::is_absolute_path(const char *path)
384                {
385                  char full[_MAX_PATH];
386 tony  1.30       char path_slash[_MAX_PATH];
387                  char *p;
388                
389                  strncpy(path_slash, path, _MAX_PATH);
390                  path_slash[_MAX_PATH-1] = '\0';
391                
392                  for(p = path_slash; p < path_slash + strlen(path_slash); p++)
393                    if (*p == '/') *p = '\\';
394                  
395                  return (strcasecmp(_fullpath( full, path_slash, _MAX_PATH ), path_slash) == 0) ? true : false;
396 kumpf 1.34     }
397                
398                // Changes file permissions on the given file.
399                Boolean System::changeFilePermissions(const char* path, mode_t mode)
400                {
401                    // ATTN: File permissions are not currently defined in Windows
402                    return true;
403 tony  1.27     }
404 david 1.29     
405                void System::openlog(const String ident)
406                {
407                    return;
408                }
409                
410                void System::syslog(Uint32 severity, const char *data)
411                {
412                    return;
413                }
414                
415                void System::closelog()
416                {
417                    return;
418                }
419                
420                // System ID constants for Logger::put and Logger::trace
421                const String System::CIMSERVER = "cimserver";  // Server system ID
422 tony  1.27     
423 mike  1.13     PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2