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

  1 karl  1.42 //%2004////////////////////////////////////////////////////////////////////////
  2 mike  1.13 //
  3 karl  1.42 // 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.32 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.42 // 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.13 //
 10            // Permission is hereby granted, free of charge, to any person obtaining a copy
 11 mike  1.14 // 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 mike  1.13 // 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.25 // 
 17 mike  1.14 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 18 mike  1.13 // 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 mike  1.14 // 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 mike  1.13 // 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: Mike Brasher (mbrasher@bmc.com)
 29            //
 30 mike  1.14 // Modified By: Sushma Fernandes (sushma_fernandes@hp.com)
 31            //              Nag Boranna, Hewlett-Packard Company (nagaraja_boranna@hp.com)
 32 kumpf 1.35 //              Bapu Patil (bapu_patil@hp.com)
 33 mike  1.13 //
 34 david 1.29 // Modified By: Dave Rosckes (rosckes@us.ibm.com)
 35 kumpf 1.36 //              Terry Martin, Hewlett-Packard Company (terry.martin@hp.com)
 36 a.arora 1.40 //              Amit K Arora, IBM (amita@in.ibm.com) for Bug#1428
 37 se.gupta 1.41 //				Seema Gupta (gseema@in.ibm.com) for Bug#1617
 38 david.dillard 1.43 //              David Dillard, VERITAS Software Corp.
 39                    //                  (david.dillard@veritas.com)
 40 david         1.29 //
 41 mike          1.13 //%/////////////////////////////////////////////////////////////////////////////
 42                    
 43                    #include "System.h"
 44                    
 45                    #include <windows.h>
 46 tony          1.31 #ifndef _WINSOCKAPI_
 47                    #include <winsock2.h>
 48                    #endif
 49 mike          1.21 #include <fcntl.h>
 50 mike          1.13 #include <sys/types.h>
 51                    #include <time.h>
 52                    #include <sys/timeb.h>
 53                    #include <io.h>
 54 tony          1.33 #include <conio.h>
 55 mike          1.13 #include <direct.h>
 56                    #include <sys/types.h>
 57 mday          1.19 #include <windows.h>
 58 kumpf         1.18 #include <process.h>
 59 kumpf         1.38 #include <lm.h>
 60 mike          1.13 
 61 kumpf         1.36 PEGASUS_NAMESPACE_BEGIN
 62                    
 63 se.gupta      1.41 #define PEGASUS_ACCESS_EXISTS 0
 64                    #define PEGASUS_ACCESS_WRITE 2
 65                    #define PEGASUS_ACCESS_READ 4
 66                    #define PEGASUS_ACCESS_READ_AND_WRITE 6
 67 mike          1.13 
 68 tony          1.33 #define PW_BUFF_LEN 65
 69                    
 70 mike          1.13 void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
 71                    {
 72                        FILETIME ft;
 73                        GetSystemTimeAsFileTime(&ft);
 74                        ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime };
 75                        largeInt.QuadPart -= 0x19db1ded53e8000;
 76                        seconds = long(largeInt.QuadPart / (10000 * 1000));
 77                        milliseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10);
 78 karl          1.23     // This is a real hack. Added the following line after timevalue was
 79                        // corrected and this apparently wrong. ks 7 apri 2002
 80                        milliseconds = milliseconds / 1000;
 81 mike          1.13 }
 82                    
 83                    String System::getCurrentASCIITime()
 84                    {
 85                        char tmpbuf[128];
 86 kumpf         1.28     _strdate( tmpbuf );
 87                        String date = tmpbuf;
 88 mike          1.13     _strtime( tmpbuf );
 89 kumpf         1.28     date.append("-");
 90                        date.append(tmpbuf);
 91                        return date;
 92 mike          1.13 }
 93                    
 94                    void System::sleep(Uint32 seconds)
 95                    {
 96                        Sleep(seconds * 1000);
 97                    }
 98                    
 99                    Boolean System::exists(const char* path)
100                    {
101 se.gupta      1.41     return _access(path, PEGASUS_ACCESS_EXISTS) == 0;
102 mike          1.13 }
103                    
104                    Boolean System::canRead(const char* path)
105                    {
106 se.gupta      1.41     return _access(path, PEGASUS_ACCESS_READ) == 0;
107 mike          1.13 }
108                    
109                    Boolean System::canWrite(const char* path)
110                    {
111 se.gupta      1.41     return _access(path, PEGASUS_ACCESS_WRITE) == 0;
112 mike          1.13 }
113                    
114                    Boolean System::getCurrentDirectory(char* path, Uint32 size)
115                    {
116                        return GetCurrentDirectory(size, path) != 0;
117                    }
118                    
119                    Boolean System::isDirectory(const char* path)
120                    {
121                        struct stat st;
122                    
123                        if (stat(path, &st) != 0)
124                    	return false;
125                    
126                        return (st.st_mode & _S_IFDIR) != 0;
127                    }
128                    
129                    Boolean System::changeDirectory(const char* path)
130                    {
131                        return chdir(path) == 0;
132                    }
133 mike          1.13 
134                    Boolean System::makeDirectory(const char* path)
135                    {
136                        return _mkdir(path) == 0;
137                    }
138                    
139                    Boolean System::getFileSize(const char* path, Uint32& size)
140                    {
141                        struct stat st;
142                    
143                        if (stat(path, &st) != 0)
144                    	return false;
145                    
146                        size = st.st_size;
147                        return true;
148                    }
149                    
150                    Boolean System::removeDirectory(const char* path)
151                    {
152                        return rmdir(path) == 0;	
153                    }
154 mike          1.13 
155                    Boolean System::removeFile(const char* path)
156                    {
157                        return unlink(path) == 0;	
158                    }
159                    
160                    Boolean System::renameFile(const char* oldPath, const char* newPath)
161                    {
162                        return rename(oldPath, newPath) == 0;
163                    }
164                    
165                    DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
166                    {
167                        return DynamicLibraryHandle(LoadLibrary(fileName));
168                    }
169                    
170 mike          1.14 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
171                    {
172                    	FreeLibrary(HINSTANCE(libraryHandle));
173                    }
174                    
175 mike          1.13 String System::dynamicLoadError(void) {
176                    return String();
177                    }
178                    
179                    DynamicSymbolHandle System::loadDynamicSymbol(
180                        DynamicLibraryHandle libraryHandle,
181                        const char* symbolName)
182                    {
183                        return DynamicSymbolHandle(GetProcAddress(
184                    	(HINSTANCE)libraryHandle, symbolName));
185                    }
186                    
187                    String System::getHostName()
188                    {
189 kumpf         1.35     static char hostname[PEGASUS_MAXHOSTNAMELEN];
190 mike          1.13 
191                        if (!*hostname)
192                            gethostname(hostname, sizeof(hostname));
193                    
194                        return hostname;
195 kumpf         1.16 }
196                    
197 kumpf         1.22 String System::getFullyQualifiedHostName ()
198                    {
199 a.arora       1.40     static char FQHostName[PEGASUS_MAXHOSTNAMELEN];
200                    
201                        if (!*FQHostName)
202                        {
203                            String hostname = getHostName();
204                            struct hostent* hostEnt;
205                    
206                            hostEnt = gethostbyname((const char *)hostname.getCString());
207                            if (hostEnt == NULL)
208                            {
209                                return String::EMPTY;
210                            }
211                            strcpy(FQHostName, hostEnt->h_name);
212                        }
213                       
214                        return FQHostName;
215 kumpf         1.22 }
216                    
217                    String System::getSystemCreationClassName ()
218                    {
219 karl          1.39     return "CIM_ComputerSystem";
220 kumpf         1.22 }
221                    
222 kumpf         1.16 Uint32 System::lookupPort(
223                        const char * serviceName,
224                        Uint32 defaultPort)
225                    {
226                        Uint32 localPort;
227                    
228                        struct servent *serv;
229                    
230                        //
231                        // Get wbem-local port from /etc/services
232                        //
233                        if (  (serv = getservbyname(serviceName, TCP)) != NULL )
234                        {
235                            localPort = serv->s_port;
236                        }
237                        else
238                        {
239                            localPort = defaultPort;
240                        }
241                    
242                        return localPort;
243 mike          1.13 }
244                    
245 mike          1.14 String System::getPassword(const char* prompt)
246                    {
247 tony          1.33   char password[PW_BUFF_LEN] = {0};
248                      int num_chars = 0;
249                      int ch;
250                    
251                      fputs(prompt, stderr);
252                    
253                      while ((ch = _getch()) != '\r' &&
254                             num_chars < PW_BUFF_LEN)
255                        {
256                          // EOF
257                          if (ch == EOF)
258                            {
259                               fputs("[EOF]\n", stderr);
260                               return String::EMPTY;
261                            }
262                          // Backspace or Delete
263                          else if ((ch == '\b' || ch == 127) &&
264                                   num_chars > 0) 
265                            {
266                              password[--num_chars] = '\0';
267                              fputs("\b \b", stderr);
268 tony          1.33         }
269                          // CTRL+C
270                          else if (ch == 3)
271                            {
272                              // _getch() does not catch CTRL+C
273                              fputs("^C\n", stderr);
274                              exit(-1);
275                            }
276                          // CTRL+Z
277                          else if (ch == 26)
278                            {
279                              fputs("^Z\n", stderr);
280                              return String::EMPTY;
281                            }
282                          // Esc
283                          else if (ch == 27)
284                            {
285                              fputc('\n', stderr);
286                              fputs(prompt, stderr);
287                              num_chars = 0;
288                            }
289 tony          1.33       // Function keys (0 or E0) are a guards for a Function key codes
290                          else if (ch == 0 || ch == 0xE0)
291                            {
292                              ch = (ch << 4) | _getch();
293                              // Handle DELETE, left arrow, keypad DEL, and keypad left arrow
294                              if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) &&
295                                  num_chars > 0)
296                                {
297                                  password[--num_chars] = '\0';
298                                  fputs("\b \b", stderr);
299                                }
300                              else
301                                {
302                                  fputc('\a', stderr);
303                                }
304                            }
305                          else if ((num_chars < sizeof(password) - 1) &&
306                                   !iscntrl(((unsigned char)(ch))))
307                            {
308                              password[num_chars++] = ch;
309                              fputc('*', stderr);
310 tony          1.33         }
311                          else
312                            {
313                              fputc('\a', stderr);
314                            }
315                        }
316 mike          1.14 
317 tony          1.33   fputc('\n', stderr);
318                      password[num_chars] = '\0';
319                    
320                      return String(password);
321 mike          1.14 }
322                    
323 kumpf         1.24 String System::getEffectiveUserName()
324 mike          1.14 {
325 tony          1.33   int retcode = 0;
326                    
327                      // UNLEN (256) is the limit, not including null
328                      char pUserName[256+1] = {0};
329                      DWORD nSize = sizeof(pUserName);
330 mike          1.14 
331 tony          1.33   retcode = GetUserName(pUserName, &nSize);
332                      if (retcode == 0)
333                        {
334                          // zero is failure
335                          return String();
336                        }
337                      
338                      return String(pUserName);
339 mike          1.14 }
340                    
341                    String System::encryptPassword(const char* password, const char* salt)
342                    {
343 tony          1.33   BYTE pbBuffer[PW_BUFF_LEN] = {0};
344                      DWORD dwByteCount;
345                      char pcSalt[3] = {0};
346                    
347                      strncpy(pcSalt, salt, 2);
348                      dwByteCount = strlen(password);
349                      memcpy(pbBuffer, password, dwByteCount);
350                      for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++)
351                        (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0];
352                      
353                      return String(pcSalt) + String((char *)pbBuffer);
354 mike          1.14 }
355                    
356 kumpf         1.26 Boolean System::isSystemUser(const char* userName)
357 mike          1.14 {
358                        //ATTN: Implement this method to verify if user is vaild on the local system
359                        //      This is used in User Manager
360                        return true;
361                    }
362                    
363 david.dillard 1.43 Boolean System::isPrivilegedUser(const String& userName)
364 mike          1.14 {
365                        // ATTN: Implement this method to verify if user executing the current
366 kumpf         1.17     //       command is a priviliged user, when user name is not passed as
367                        //       as argument. If user name is passed the function checks 
368                        //       whether the given user is a priviliged user.
369                        //       This is used in cimuser CLI and CIMOperationRequestAuthorizer
370 mike          1.14     return true;
371                    }
372 kumpf         1.20 
373                    String System::getPrivilegedUserName()
374                    {
375                        // ATTN-NB-03-20000304: Implement better way to get the privileged
376                        // user on the system.
377                    
378                        return (String("Administrator"));
379                    }
380 kumpf         1.37 
381                    Boolean System::isGroupMember(const char* userName, const char* groupName)
382                    {
383 kumpf         1.38    Boolean retVal = false;
384                    
385                       LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;
386                       DWORD dwLevel = 0;
387                       DWORD dwFlags = LG_INCLUDE_INDIRECT ;
388                       DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
389                       DWORD dwEntriesRead = 0;
390                       DWORD dwTotalEntries = 0;
391                       NET_API_STATUS nStatus;
392                    
393                    
394                       //
395                       // Call the NetUserGetLocalGroups function 
396                       // specifying information level 0.
397                       //
398                       // The LG_INCLUDE_INDIRECT flag specifies that the 
399                       // function should also return the names of the local 
400                       // groups in which the user is indirectly a member.
401                       //
402                       nStatus = NetUserGetLocalGroups(NULL,
403                                                       (LPCWSTR)userName,
404 kumpf         1.38                                    dwLevel,
405                                                       dwFlags,
406                                                       (LPBYTE *) &pBuf,
407                                                       dwPrefMaxLen,
408                                                       &dwEntriesRead,
409                                                       &dwTotalEntries);
410                    
411                       //
412                       // If the call succeeds,
413                       //
414                       if (nStatus == NERR_Success)
415                       {
416                          LPLOCALGROUP_USERS_INFO_0 pTmpBuf;
417                          DWORD i;
418                          DWORD dwTotalCount = 0;
419                    
420                          if ((pTmpBuf = pBuf) != NULL)
421                          {
422                             //
423                             // Loop through the local groups that the user belongs
424                             // and find the matching group name.
425 kumpf         1.38          //
426                             for (i = 0; i < dwEntriesRead; i++)
427                             {
428                                //
429                                // Compare the user's group name to groupName.
430                                //
431                                if ( strcmp ((char *)pTmpBuf->lgrui0_name, groupName) == 0 )
432                                {
433                                     // User is a member of the group.
434                                     retVal = true;
435                                     break;
436                                }
437                    
438                                pTmpBuf++;
439                                dwTotalCount++;
440                             }
441                          }
442                       }
443                    
444                       //
445                       // Free the allocated memory.
446 kumpf         1.38    //
447                       if (pBuf != NULL)
448                          NetApiBufferFree(pBuf);
449                    
450                       //
451                       // If the given user and group are not found in the local group
452                       // then try on the global groups.
453                       //
454                       if (!retVal)
455                       {
456                           LPGROUP_USERS_INFO_0 pBuf = NULL;
457                           dwLevel = 0;
458                           dwPrefMaxLen = MAX_PREFERRED_LENGTH;
459                           dwEntriesRead = 0;
460                           dwTotalEntries = 0;
461                    
462                           //
463                           // Call the NetUserGetGroups function, specifying level 0.
464                           //
465                           nStatus = NetUserGetGroups(NULL,
466                                                      (LPCWSTR)userName,
467 kumpf         1.38                                   dwLevel,
468                                                      (LPBYTE*)&pBuf,
469                                                      dwPrefMaxLen,
470                                                      &dwEntriesRead,
471                                                      &dwTotalEntries);
472                           //
473                           // If the call succeeds,
474                           //
475                           if (nStatus == NERR_Success)
476                           {
477                              LPGROUP_USERS_INFO_0 pTmpBuf;
478                              DWORD i;
479                              DWORD dwTotalCount = 0;
480                        
481                              if ((pTmpBuf = pBuf) != NULL)
482                              {
483                                 //
484                                 // Loop through the global groups to which the user belongs
485                                 // and find the matching group name.
486                                 //
487                                 for (i = 0; i < dwEntriesRead; i++)
488 kumpf         1.38              {
489                                    //
490                                    // Compare the user's group name to groupName.
491                                    //
492                                    if ( strcmp ((char *)pTmpBuf->grui0_name, groupName) == 0 )
493                                    {
494                                         // User is a member of the group.
495                                         retVal = true;
496                                         break;
497                                    }
498                    
499                                    pTmpBuf++;
500                                    dwTotalCount++;
501                                 }
502                              }
503                           }
504                    
505                           //
506                           // Free the allocated buffer.
507                           //
508                           if (pBuf != NULL)
509 kumpf         1.38           NetApiBufferFree(pBuf);
510                       }
511                    
512                       return retVal;
513 kumpf         1.37 }
514 mike          1.14     
515 kumpf         1.15 Uint32 System::getPID()
516                    {
517 kumpf         1.18     return _getpid();
518 mike          1.21 }
519                    
520                    Boolean System::truncateFile(
521                        const char* path, 
522                        size_t newSize)
523                    {
524                        int fd = open(path, O_RDWR);
525                    
526                        if (fd == -1)
527                            return false;
528                    
529                        if (chsize(fd, newSize) != 0)
530                    	return false;
531                    
532                        close(fd);
533                        return true;
534 kumpf         1.15 }
535                    
536 tony          1.27 // Is absolute path?
537                    Boolean System::is_absolute_path(const char *path)
538                    {
539                      char full[_MAX_PATH];
540 tony          1.30   char path_slash[_MAX_PATH];
541                      char *p;
542                    
543                      strncpy(path_slash, path, _MAX_PATH);
544                      path_slash[_MAX_PATH-1] = '\0';
545                    
546                      for(p = path_slash; p < path_slash + strlen(path_slash); p++)
547                        if (*p == '/') *p = '\\';
548                      
549                      return (strcasecmp(_fullpath( full, path_slash, _MAX_PATH ), path_slash) == 0) ? true : false;
550 kumpf         1.34 }
551                    
552                    // Changes file permissions on the given file.
553                    Boolean System::changeFilePermissions(const char* path, mode_t mode)
554                    {
555                        // ATTN: File permissions are not currently defined in Windows
556                        return true;
557 tony          1.27 }
558 david         1.29 
559                    void System::openlog(const String ident)
560                    {
561                        return;
562                    }
563                    
564                    void System::syslog(Uint32 severity, const char *data)
565                    {
566                        return;
567                    }
568                    
569                    void System::closelog()
570                    {
571                        return;
572                    }
573                    
574                    // System ID constants for Logger::put and Logger::trace
575                    const String System::CIMSERVER = "cimserver";  // Server system ID
576 tony          1.27 
577 mike          1.13 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2