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

   1 karl  1.61 //%2006////////////////////////////////////////////////////////////////////////
   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 karl  1.45 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
  10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
  11 karl  1.61 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
  12            // EMC Corporation; Symantec Corporation; The Open Group.
  13 mike  1.13 //
  14            // Permission is hereby granted, free of charge, to any person obtaining a copy
  15 mike  1.14 // 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 mike  1.13 // 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 karl  1.61 // 
  21 mike  1.14 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
  22 mike  1.13 // 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 mike  1.14 // 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 mike  1.13 // 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            //%/////////////////////////////////////////////////////////////////////////////
  33            
  34            #include "System.h"
  35            
  36 mike  1.64 #include "Network.h"
  37 mike  1.65 #include "Mutex.h"
  38 mike  1.21 #include <fcntl.h>
  39 mike  1.13 #include <sys/types.h>
  40            #include <time.h>
  41            #include <sys/timeb.h>
  42            #include <io.h>
  43 tony  1.33 #include <conio.h>
  44 mike  1.13 #include <direct.h>
  45            #include <sys/types.h>
  46 mday  1.19 #include <windows.h>
  47 kumpf 1.18 #include <process.h>
  48 kumpf 1.38 #include <lm.h>
  49 mike  1.13 
  50 h.sterling 1.52 #define SECURITY_WIN32
  51                 #include <security.h>
  52 h.sterling 1.50 
  53 kumpf      1.36 PEGASUS_NAMESPACE_BEGIN
  54                 
  55 se.gupta   1.41 #define PEGASUS_ACCESS_EXISTS 0
  56                 #define PEGASUS_ACCESS_WRITE 2
  57                 #define PEGASUS_ACCESS_READ 4
  58                 #define PEGASUS_ACCESS_READ_AND_WRITE 6
  59 mike       1.13 
  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 jim.wunderlich 1.60 void System::getCurrentTimeUsec(Uint32& seconds, Uint32& microseconds)
  76                     {
  77                         FILETIME ft;
  78                         GetSystemTimeAsFileTime(&ft);
  79                         ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime };
  80                         largeInt.QuadPart -= 0x19db1ded53e8000;
  81                         seconds = long(largeInt.QuadPart / (10000 * 1000));
  82                         microseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10);
  83                     }
  84                     
  85 mike           1.13 String System::getCurrentASCIITime()
  86                     {
  87                         char tmpbuf[128];
  88 kumpf          1.28     _strdate( tmpbuf );
  89                         String date = tmpbuf;
  90 mike           1.13     _strtime( tmpbuf );
  91 kumpf          1.28     date.append("-");
  92                         date.append(tmpbuf);
  93                         return date;
  94 mike           1.13 }
  95                     
  96                     void System::sleep(Uint32 seconds)
  97                     {
  98                         Sleep(seconds * 1000);
  99                     }
 100                     
 101                     Boolean System::exists(const char* path)
 102                     {
 103 se.gupta       1.41     return _access(path, PEGASUS_ACCESS_EXISTS) == 0;
 104 mike           1.13 }
 105                     
 106                     Boolean System::canRead(const char* path)
 107                     {
 108 se.gupta       1.41     return _access(path, PEGASUS_ACCESS_READ) == 0;
 109 mike           1.13 }
 110                     
 111                     Boolean System::canWrite(const char* path)
 112                     {
 113 se.gupta       1.41     return _access(path, PEGASUS_ACCESS_WRITE) == 0;
 114 mike           1.13 }
 115                     
 116                     Boolean System::getCurrentDirectory(char* path, Uint32 size)
 117                     {
 118                         return GetCurrentDirectory(size, path) != 0;
 119                     }
 120                     
 121                     Boolean System::isDirectory(const char* path)
 122                     {
 123                         struct stat st;
 124                     
 125                         if (stat(path, &st) != 0)
 126 david.dillard  1.49         return false;
 127 mike           1.13 
 128                         return (st.st_mode & _S_IFDIR) != 0;
 129                     }
 130                     
 131                     Boolean System::changeDirectory(const char* path)
 132                     {
 133                         return chdir(path) == 0;
 134                     }
 135                     
 136                     Boolean System::makeDirectory(const char* path)
 137                     {
 138                         return _mkdir(path) == 0;
 139                     }
 140                     
 141                     Boolean System::getFileSize(const char* path, Uint32& size)
 142                     {
 143                         struct stat st;
 144                     
 145                         if (stat(path, &st) != 0)
 146 david.dillard  1.49         return false;
 147 mike           1.13 
 148                         size = st.st_size;
 149                         return true;
 150                     }
 151                     
 152                     Boolean System::removeDirectory(const char* path)
 153                     {
 154 david.dillard  1.49     return rmdir(path) == 0;
 155 mike           1.13 }
 156                     
 157                     Boolean System::removeFile(const char* path)
 158                     {
 159 david.dillard  1.49     return unlink(path) == 0;
 160 mike           1.13 }
 161                     
 162                     Boolean System::renameFile(const char* oldPath, const char* newPath)
 163                     {
 164                         return rename(oldPath, newPath) == 0;
 165                     }
 166                     
 167                     String System::getHostName()
 168                     {
 169 kumpf          1.55     static char hostname[PEGASUS_MAXHOSTNAMELEN + 1];
 170 mike           1.13 
 171                         if (!*hostname)
 172 kumpf          1.55     {
 173 mike           1.13         gethostname(hostname, sizeof(hostname));
 174 kumpf          1.55     }
 175                         hostname[sizeof(hostname)-1] = 0;
 176 mike           1.13 
 177                         return hostname;
 178 kumpf          1.16 }
 179                     
 180 kumpf          1.22 String System::getFullyQualifiedHostName ()
 181                     {
 182 kumpf          1.56     static char FQHostName[PEGASUS_MAXHOSTNAMELEN + 1];
 183 a.arora        1.40 
 184                         if (!*FQHostName)
 185                         {
 186                             String hostname = getHostName();
 187                             struct hostent* hostEnt;
 188                     
 189                             hostEnt = gethostbyname((const char *)hostname.getCString());
 190                             if (hostEnt == NULL)
 191                             {
 192                                 return String::EMPTY;
 193                             }
 194 kumpf          1.56         strncpy(FQHostName, hostEnt->h_name, sizeof(FQHostName)-1);
 195 a.arora        1.40     }
 196 david.dillard  1.49 
 197 a.arora        1.40     return FQHostName;
 198 kumpf          1.22 }
 199                     
 200                     String System::getSystemCreationClassName ()
 201                     {
 202 karl           1.39     return "CIM_ComputerSystem";
 203 kumpf          1.22 }
 204                     
 205 kumpf          1.16 Uint32 System::lookupPort(
 206                         const char * serviceName,
 207                         Uint32 defaultPort)
 208                     {
 209                         Uint32 localPort;
 210                     
 211                         struct servent *serv;
 212                     
 213                         //
 214 david.dillard  1.48     // Get the port number.
 215 kumpf          1.16     //
 216 david.dillard  1.49     if ( (serv = getservbyname(serviceName, TCP)) != NULL )
 217 kumpf          1.16     {
 218 david.dillard  1.48         localPort = ntohs(serv->s_port);
 219 kumpf          1.16     }
 220                         else
 221                         {
 222                             localPort = defaultPort;
 223                         }
 224                     
 225                         return localPort;
 226 mike           1.13 }
 227                     
 228 mike           1.14 String System::getPassword(const char* prompt)
 229                     {
 230 david.dillard  1.49     char password[PW_BUFF_LEN] = {0};
 231                         int num_chars = 0;
 232                         int ch;
 233 tony           1.33 
 234 david.dillard  1.49     fputs(prompt, stderr);
 235 tony           1.33 
 236 david.dillard  1.49     while ((ch = _getch()) != '\r' &&
 237                                 num_chars < PW_BUFF_LEN)
 238 tony           1.33         {
 239 david.dillard  1.49         // EOF
 240                             if (ch == EOF)
 241 tony           1.33         {
 242 david.dillard  1.49             fputs("[EOF]\n", stderr);
 243                                 return String::EMPTY;
 244 tony           1.33         }
 245 david.dillard  1.49         // Backspace or Delete
 246                             else if ((ch == '\b' || ch == 127) &&
 247                                     num_chars > 0)
 248 tony           1.33         {
 249 david.dillard  1.49             password[--num_chars] = '\0';
 250                                 fputs("\b \b", stderr);
 251 tony           1.33         }
 252 david.dillard  1.49         // CTRL+C
 253                             else if (ch == 3)
 254 tony           1.33         {
 255 david.dillard  1.49             // _getch() does not catch CTRL+C
 256                                 fputs("^C\n", stderr);
 257                                 exit(-1);
 258 tony           1.33         }
 259 david.dillard  1.49         // CTRL+Z
 260                             else if (ch == 26)
 261 tony           1.33         {
 262 david.dillard  1.49             fputs("^Z\n", stderr);
 263                                 return String::EMPTY;
 264 tony           1.33         }
 265 david.dillard  1.49         // Esc
 266                             else if (ch == 27)
 267 tony           1.33         {
 268 david.dillard  1.49             fputc('\n', stderr);
 269                                 fputs(prompt, stderr);
 270                                 num_chars = 0;
 271                             }
 272                             // Function keys (0 or E0) are a guards for a Function key codes
 273                             else if (ch == 0 || ch == 0xE0)
 274                             {
 275                                 ch = (ch << 4) | _getch();
 276                                 // Handle DELETE, left arrow, keypad DEL, and keypad left arrow
 277                                 if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) &&
 278                                         num_chars > 0)
 279 tony           1.33             {
 280 david.dillard  1.49                 password[--num_chars] = '\0';
 281                                     fputs("\b \b", stderr);
 282 tony           1.33             }
 283 david.dillard  1.49             else
 284 tony           1.33             {
 285 david.dillard  1.49                 fputc('\a', stderr);
 286 tony           1.33             }
 287                             }
 288 david.dillard  1.49         else if ((num_chars < sizeof(password) - 1) &&
 289                                         !iscntrl(((unsigned char)(ch))))
 290 tony           1.33         {
 291 david.dillard  1.49             password[num_chars++] = ch;
 292                                 fputc('*', stderr);
 293 tony           1.33         }
 294 david.dillard  1.49         else
 295 tony           1.33         {
 296 david.dillard  1.49             fputc('\a', stderr);
 297 tony           1.33         }
 298                         }
 299 mike           1.14 
 300 david.dillard  1.49     fputc('\n', stderr);
 301                         password[num_chars] = '\0';
 302 tony           1.33 
 303 david.dillard  1.49     return String(password);
 304 mike           1.14 }
 305                     
 306 kumpf          1.24 String System::getEffectiveUserName()
 307 mike           1.14 {
 308 h.sterling     1.52 #if (_MSC_VER >= 1300) || defined(PEGASUS_WINDOWS_SDK_HOME)
 309                     
 310 kumpf          1.70     //Bug 3076 fix
 311                         wchar_t fullUserName[UNLEN+1];
 312                         DWORD userNameSize = sizeof(fullUserName)/sizeof(fullUserName[0]);
 313                         wchar_t computerName[MAX_COMPUTERNAME_LENGTH+1];
 314                         DWORD computerNameSize = sizeof(computerName)/sizeof(computerName[0]);
 315                         wchar_t userName[UNLEN+1];
 316 h.sterling     1.57     wchar_t userDomain[UNLEN+1];
 317 kumpf          1.70     String userId;
 318                     
 319                         if (!GetUserNameExW(NameSamCompatible, fullUserName, &userNameSize))
 320                         {
 321                             return String();
 322                         }
 323 h.sterling     1.50 
 324 kumpf          1.70     wchar_t* index = wcschr(fullUserName, '\\');
 325                         *index = 0;
 326                         wcscpy(userDomain, fullUserName);
 327                         wcscpy(userName, index + 1);
 328                     
 329                         //The above function will return the system name as the domain if
 330                         //the user is not on a real domain.  Strip this out so that the rest of
 331                         //our windows user functions work.  What if the system name and the domain
 332                         //name are the same?
 333 h.sterling     1.57     GetComputerNameW(computerName, &computerNameSize);
 334 kumpf          1.70 
 335                         if (wcscmp(computerName, userDomain) != 0)
 336                         {
 337 h.sterling     1.57         //userId.append(userDomain);
 338                             Uint32 n = wcslen(userDomain);
 339 kumpf          1.70         for (unsigned long i = 0; i < n; i++)
 340 h.sterling     1.57         {
 341                                 userId.append(Char16(userDomain[i]));
 342                             }
 343 kumpf          1.70         userId.append("\\");
 344                             //userId.append(userName);
 345 h.sterling     1.57         n = wcslen(userName);
 346 kumpf          1.70         for (unsigned long i = 0; i < n; i++)
 347 h.sterling     1.57         {
 348                                 userId.append(Char16(userName[i]));
 349                             }
 350                     
 351 kumpf          1.70     }
 352                         else
 353                         {
 354                             //userId.append(userName);
 355 h.sterling     1.57         Uint32 n = wcslen(userName);
 356 kumpf          1.70         for (unsigned long i = 0; i < n; i++)
 357 h.sterling     1.57         {
 358                                 userId.append(Char16(userName[i]));
 359                             }
 360                     
 361 kumpf          1.70     }
 362 h.sterling     1.50 
 363 kumpf          1.70     return userId;
 364 h.sterling     1.52 
 365                     #else //original getEffectiveUserName function
 366 kumpf          1.70 
 367 david.dillard  1.49     int retcode = 0;
 368 tony           1.33 
 369 david.dillard  1.49     // UNLEN (256) is the limit, not including null
 370 h.sterling     1.57     wchar_t pUserName[256+1] = {0};
 371 h.sterling     1.59     DWORD nSize = sizeof(pUserName)/sizeof(pUserName[0]);
 372 mike           1.14 
 373 h.sterling     1.57     retcode = GetUserNameW(pUserName, &nSize);
 374 david.dillard  1.49     if (retcode == 0)
 375 tony           1.33     {
 376 david.dillard  1.49         // zero is failure
 377                             return String();
 378 tony           1.33     }
 379 h.sterling     1.58     String userId;
 380 h.sterling     1.57     Uint32 n = wcslen(pUserName);
 381 kumpf          1.70     for (unsigned long i = 0; i < n; i++)
 382 h.sterling     1.57     {
 383                             userId.append(Char16(pUserName[i]));
 384                         }
 385                     
 386                         return userId;
 387 h.sterling     1.52 #endif
 388 mike           1.14 }
 389                     
 390                     String System::encryptPassword(const char* password, const char* salt)
 391                     {
 392 david.dillard  1.49     BYTE pbBuffer[PW_BUFF_LEN] = {0};
 393                         DWORD dwByteCount;
 394                         char pcSalt[3] = {0};
 395                     
 396                         strncpy(pcSalt, salt, 2);
 397                         dwByteCount = strlen(password);
 398                         memcpy(pbBuffer, password, dwByteCount);
 399                         for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++)
 400                                 (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0];
 401                     
 402                         return String(pcSalt) + String((char *)pbBuffer);
 403 mike           1.14 }
 404                     
 405 a.dunfey       1.62 String processUserName;
 406                     Mutex processUserNameMut;
 407                     
 408 kumpf          1.26 Boolean System::isSystemUser(const char* userName)
 409 mike           1.14 {
 410 kumpf          1.70     if (processUserName.size() == 0)
 411 a.dunfey       1.62     {
 412                             // Lock and recheck the processUserName length in case two threads
 413                             // enter this block simultaneously
 414                             AutoMutex mut(processUserNameMut);
 415 kumpf          1.70         if (processUserName.size() == 0)
 416 a.dunfey       1.62         {
 417                                 processUserName = getEffectiveUserName();
 418                             }
 419                         }
 420 kumpf          1.70     if (processUserName == userName)
 421 a.dunfey       1.62     {
 422                           return true;
 423                         }
 424                     
 425 h.sterling     1.46     Boolean isSystemUser = false;
 426                     
 427                         char mUserName[UNLEN+1];
 428                         char mDomainName[UNLEN+1];
 429 david.dillard  1.51     char tUserName[UNLEN+1];
 430 h.sterling     1.46     wchar_t wUserName[UNLEN+1];
 431                         wchar_t wDomainName[UNLEN+1];
 432                         char* pbs;
 433                         bool usingDomain = false;
 434 david.dillard  1.49 
 435 h.sterling     1.46     LPBYTE pComputerName=NULL;
 436                         DWORD dwLevel = 1;
 437                         LPUSER_INFO_1 pUserInfo = NULL;
 438                         NET_API_STATUS nStatus = NULL;
 439                     
 440 kumpf          1.70     // Make a copy of the specified username, it cannot be used directly
 441                         // because it's declared as const and strchr() may modify the string.
 442 david.dillard  1.51     strncpy(tUserName, userName, sizeof(tUserName) - 1);
 443 david.dillard  1.54     tUserName[sizeof(tUserName)- 1] = '\0';
 444 david.dillard  1.51 
 445 david.dillard  1.49     //separate the domain and user name if both are present.
 446 david.dillard  1.51     if (NULL != (pbs = strchr(tUserName, '\\')))
 447 h.sterling     1.46     {
 448                             *pbs = '\0';
 449 david.dillard  1.51         strcpy(mDomainName, tUserName);
 450 h.sterling     1.46         strcpy(mUserName, pbs+1);
 451                             usingDomain = true;
 452                     
 453 kumpf          1.70     }
 454                         else if ((NULL != (pbs = (strchr(tUserName, '@')))) ||
 455                                  (NULL != (pbs = (strchr(tUserName, '.')))))
 456 h.sterling     1.46     {
 457                             *pbs = '\0';
 458                             strcpy(mDomainName, pbs+1);
 459 david.dillard  1.51         strcpy(mUserName, tUserName);
 460 h.sterling     1.46         usingDomain = true;
 461 david.dillard  1.49 
 462 kumpf          1.70     }
 463                         else
 464 h.sterling     1.46     {
 465                             strcpy(mDomainName, ".");
 466 david.dillard  1.51         strcpy(mUserName, tUserName);
 467 h.sterling     1.46     }
 468                     
 469                         //convert domain name to unicode
 470 kumpf          1.70     if (!MultiByteToWideChar(
 471                                 CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName) + 1))
 472 h.sterling     1.46     {
 473                             return false;
 474                         }
 475                     
 476                         //convert username to unicode
 477 kumpf          1.70     if (!MultiByteToWideChar(
 478                                 CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName) + 1))
 479 h.sterling     1.46     {
 480                             return false;
 481                         }
 482 david.dillard  1.49 
 483 h.sterling     1.46     if (usingDomain)
 484                         {
 485                             //get domain controller
 486                             DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName);
 487 david.dillard  1.49         if (rc == NERR_Success)
 488 h.sterling     1.46         {
 489 kumpf          1.70             // this is automatically prefixed with "\\"
 490                                 wcscpy(wDomainName, (LPWSTR) pComputerName);
 491 david.dillard  1.49         }
 492 h.sterling     1.46         /*
 493                             else
 494                             {
 495                                 // failover
 496 kumpf          1.70             // ATTN: This is commented out until there is resolution on
 497                                 // Bugzilla 2236. -hns 2/2005
 498 h.sterling     1.46             // This needs to be more thoroughly tested when we uncomment it out.
 499 david.dillard  1.49 
 500 h.sterling     1.46             PDOMAIN_CONTROLLER_INFO DomainControllerInfo = NULL;
 501                     
 502                                 //this function does not take wide strings
 503                                 rc = DsGetDcName(NULL,
 504                                                  mDomainName,
 505                                                  NULL,
 506                                                  NULL,
 507 kumpf          1.70                              //not sure what flags we want here
 508                                                  DS_DIRECTORY_SERVICE_REQUIRED,
 509 h.sterling     1.46                              &DomainControllerInfo);
 510                     
 511                                 if (rc == ERROR_SUCCESS && DomainControllerInfo)
 512                                 {
 513                                     strcpy(mDomainName, DomainControllerInfo->DomainName);
 514                                     NetApiBufferFree(DomainControllerInfo);
 515                     
 516 kumpf          1.70                 if (!MultiByteToWideChar(
 517                                             CP_ACP, 0, mDomainName, -1, wDomainName,
 518                                             strlen(mDomainName) + 1))
 519 h.sterling     1.46                 {
 520                                         return false;
 521                                     }
 522                                 }
 523                             }
 524                             */
 525                         }
 526                     
 527                         //get user info
 528                         nStatus = NetUserGetInfo(wDomainName,
 529                                                  wUserName,
 530                                                  dwLevel,
 531                                                  (LPBYTE *)&pUserInfo);
 532                     
 533                         if (nStatus == NERR_Success)
 534                         {
 535                             isSystemUser = true;
 536                         }
 537 david.dillard  1.49 
 538                         if (pComputerName != NULL)
 539 h.sterling     1.46     {
 540                             NetApiBufferFree(pComputerName);
 541                         }
 542                     
 543                         if (pUserInfo != NULL)
 544                         {
 545                             NetApiBufferFree(pUserInfo);
 546                         }
 547                     
 548                         return isSystemUser;
 549 mike           1.14 }
 550                     
 551 h.sterling     1.46 
 552 david.dillard  1.43 Boolean System::isPrivilegedUser(const String& userName)
 553 mike           1.14 {
 554 h.sterling     1.46     Boolean isPrivileged = false;
 555                     
 556                         char mUserName[UNLEN+1];
 557                         char mDomainName[UNLEN+1];
 558                         wchar_t wUserName[UNLEN+1];
 559                         wchar_t wDomainName[UNLEN+1];
 560                         char* pbs;
 561                         char userStr[UNLEN+1];
 562                         bool usingDomain = false;
 563                     
 564                         LPBYTE pComputerName=NULL;
 565                         DWORD dwLevel = 1;
 566                         LPUSER_INFO_1 pUserInfo = NULL;
 567                         NET_API_STATUS nStatus = NULL;
 568                     
 569                         //get the username in the correct format
 570                         strcpy(userStr, (const char*)userName.getCString());
 571                     
 572 david.dillard  1.49     //separate the domain and user name if both are present.
 573 h.sterling     1.46     if (NULL != (pbs = strchr(userStr, '\\')))
 574                         {
 575                             *pbs = '\0';
 576                             strcpy(mDomainName, userStr);
 577                             strcpy(mUserName, pbs+1);
 578                             usingDomain = true;
 579                     
 580 kumpf          1.70     }
 581                         else if ((NULL != (pbs = (strchr(userStr, '@')))) ||
 582                                  (NULL != (pbs = (strchr(userStr, '.')))))
 583 h.sterling     1.46     {
 584                             *pbs = '\0';
 585                             strcpy(mDomainName, pbs+1);
 586                             strcpy(mUserName, userStr);
 587                             usingDomain = true;
 588 david.dillard  1.49 
 589 kumpf          1.70     }
 590                         else
 591 h.sterling     1.46     {
 592                             strcpy(mDomainName, ".");
 593                             strcpy(mUserName, userStr);
 594                         }
 595                     
 596                         //convert domain name to unicode
 597 kumpf          1.70     if (!MultiByteToWideChar(
 598                                 CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName) + 1))
 599 h.sterling     1.46     {
 600                             return false;
 601                         }
 602                     
 603                         //convert username to unicode
 604 kumpf          1.70     if (!MultiByteToWideChar(
 605                                 CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName) + 1))
 606 h.sterling     1.46     {
 607                             return false;
 608                         }
 609                     
 610                         if (usingDomain)
 611                         {
 612                             //get domain controller
 613                             DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName);
 614 david.dillard  1.49         if (rc == NERR_Success)
 615 h.sterling     1.46         {
 616 kumpf          1.70             // this is automatically prefixed with "\\"
 617                                 wcscpy(wDomainName, (LPWSTR) pComputerName);
 618 david.dillard  1.49         }
 619 h.sterling     1.46         /*
 620                             else
 621                             {
 622                                 // failover
 623 kumpf          1.70             // ATTN: This is commented out until there is resolution on
 624                                 // Bugzilla 2236. -hns 2/2005
 625 h.sterling     1.46             // This needs to be more thoroughly tested when we uncomment it out.
 626 david.dillard  1.49 
 627 h.sterling     1.46             PDOMAIN_CONTROLLER_INFO DomainControllerInfo = NULL;
 628                     
 629                                 //this function does not take wide strings
 630                                 rc = DsGetDcName(NULL,
 631                                                  mDomainName,
 632                                                  NULL,
 633                                                  NULL,
 634 kumpf          1.70                              // not sure what flags we want here
 635                                                  DS_DIRECTORY_SERVICE_REQUIRED,
 636 h.sterling     1.46                              &DomainControllerInfo);
 637                     
 638                                 if (rc == ERROR_SUCCESS && DomainControllerInfo)
 639                                 {
 640                                     strcpy(mDomainName, DomainControllerInfo->DomainName);
 641                                     NetApiBufferFree(DomainControllerInfo);
 642                     
 643 kumpf          1.70                 if (!MultiByteToWideChar(
 644                                             CP_ACP, 0, mDomainName, -1, wDomainName,
 645                                             strlen(mDomainName) + 1))
 646 h.sterling     1.46                 {
 647                                         return false;
 648                                     }
 649                                 }
 650                             }
 651                             */
 652                         }
 653                     
 654                         //get privileges
 655                         nStatus = NetUserGetInfo(wDomainName,
 656                                                  wUserName,
 657                                                  dwLevel,
 658                                                  (LPBYTE *)&pUserInfo);
 659                     
 660 david.dillard  1.49     if ((nStatus == NERR_Success) &&
 661 h.sterling     1.46         (pUserInfo != NULL) &&
 662                             (pUserInfo->usri1_priv == USER_PRIV_ADMIN))
 663                         {
 664                             isPrivileged = true;
 665                         }
 666                     
 667 david.dillard  1.49     if (pComputerName != NULL)
 668 h.sterling     1.46     {
 669                             NetApiBufferFree(pComputerName);
 670                         }
 671                     
 672                         if (pUserInfo != NULL)
 673                         {
 674                             NetApiBufferFree(pUserInfo);
 675                         }
 676                     
 677                         return isPrivileged;
 678 mike           1.14 }
 679 kumpf          1.20 
 680                     String System::getPrivilegedUserName()
 681                     {
 682                         // ATTN-NB-03-20000304: Implement better way to get the privileged
 683                         // user on the system.
 684                     
 685 kumpf          1.70     return String("Administrator");
 686 kumpf          1.20 }
 687 kumpf          1.37 
 688                     Boolean System::isGroupMember(const char* userName, const char* groupName)
 689                     {
 690 david.dillard  1.49     Boolean retVal = false;
 691 kumpf          1.38 
 692 david.dillard  1.49     LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;
 693                         DWORD dwLevel = 0;
 694                         DWORD dwFlags = LG_INCLUDE_INDIRECT ;
 695                         DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
 696                         DWORD dwEntriesRead = 0;
 697                         DWORD dwTotalEntries = 0;
 698                         NET_API_STATUS nStatus;
 699 ms.aruran      1.71     wchar_t wcUserName[UNLEN+1];
 700                         wchar_t wcGroupName[UNLEN+1];
 701 david.dillard  1.49 
 702 ms.aruran      1.71     //Convert user name to unicode
 703                         if (!MultiByteToWideChar(CP_ACP,0,userName, -1, wcUserName, 
 704                             strlen(userName)+1))
 705                         {
 706                             return false;
 707                         }
 708                         
 709                         //Convert group name to unicode
 710                         if (!MultiByteToWideChar(CP_ACP, 0, groupName, -1, wcGroupName, 
 711                             strlen(groupName)+1))
 712                         {
 713                             return false;
 714                         }
 715 david.dillard  1.49 
 716                         //
 717                         // Call the NetUserGetLocalGroups function
 718                         // specifying information level 0.
 719                         //
 720                         // The LG_INCLUDE_INDIRECT flag specifies that the
 721                         // function should also return the names of the local
 722                         // groups in which the user is indirectly a member.
 723                         //
 724 ms.aruran      1.71     nStatus = NetUserGetLocalGroups(
 725                             NULL,   
 726                             (LPCWSTR)wcUserName,
 727                             dwLevel,
 728                             dwFlags,
 729                             (LPBYTE *) &pBuf,
 730                             dwPrefMaxLen,
 731                             &dwEntriesRead,
 732                             &dwTotalEntries);
 733 kumpf          1.38 
 734 david.dillard  1.49     //
 735                         // If the call succeeds,
 736                         //
 737                         if (nStatus == NERR_Success)
 738                         {
 739                             LPLOCALGROUP_USERS_INFO_0 pTmpBuf;
 740                             DWORD i;
 741                             DWORD dwTotalCount = 0;
 742                     
 743                             if ((pTmpBuf = pBuf) != NULL)
 744                             {
 745 kumpf          1.38             //
 746 david.dillard  1.49             // Loop through the local groups that the user belongs
 747                                 // and find the matching group name.
 748 kumpf          1.38             //
 749 david.dillard  1.49             for (i = 0; i < dwEntriesRead; i++)
 750 kumpf          1.38             {
 751 david.dillard  1.49                 //
 752                                     // Compare the user's group name to groupName.
 753                                     //
 754 ms.aruran      1.71 
 755                                     if (wcscmp(pTmpBuf->lgrui0_name, wcGroupName) == 0)
 756 david.dillard  1.49                 {
 757                                         // User is a member of the group.
 758                                         retVal = true;
 759                                         break;
 760                                     }
 761                     
 762                                     pTmpBuf++;
 763                                     dwTotalCount++;
 764 kumpf          1.38             }
 765 david.dillard  1.49         }
 766                         }
 767 kumpf          1.38 
 768 david.dillard  1.49     //
 769                         // Free the allocated memory.
 770                         //
 771                         if (pBuf != NULL)
 772                             NetApiBufferFree(pBuf);
 773                     
 774                         //
 775                         // If the given user and group are not found in the local group
 776                         // then try on the global groups.
 777                         //
 778                         if (!retVal)
 779                         {
 780                             LPGROUP_USERS_INFO_0 pBuf = NULL;
 781                             dwLevel = 0;
 782                             dwPrefMaxLen = MAX_PREFERRED_LENGTH;
 783                             dwEntriesRead = 0;
 784                             dwTotalEntries = 0;
 785                     
 786                             //
 787                             // Call the NetUserGetGroups function, specifying level 0.
 788                             //
 789 ms.aruran      1.71         nStatus = NetUserGetGroups(
 790                                 NULL,
 791                                 (LPCWSTR)wcUserName,
 792                                 dwLevel,
 793                                 (LPBYTE*)&pBuf,
 794                                 dwPrefMaxLen,
 795                                 &dwEntriesRead,
 796                                 &dwTotalEntries);        
 797                     
 798 david.dillard  1.49         //
 799                             // If the call succeeds,
 800                             //
 801                             if (nStatus == NERR_Success)
 802                             {
 803                                 LPGROUP_USERS_INFO_0 pTmpBuf;
 804                                 DWORD i;
 805                                 DWORD dwTotalCount = 0;
 806                     
 807                                 if ((pTmpBuf = pBuf) != NULL)
 808                                 {
 809 kumpf          1.38                 //
 810 david.dillard  1.49                 // Loop through the global groups to which the user belongs
 811                                     // and find the matching group name.
 812 kumpf          1.38                 //
 813 david.dillard  1.49                 for (i = 0; i < dwEntriesRead; i++)
 814 kumpf          1.38                 {
 815 david.dillard  1.49                     //
 816                                         // Compare the user's group name to groupName.
 817                                         //
 818 ms.aruran      1.71                     if (wcscmp(pTmpBuf->grui0_name, wcGroupName) == 0)
 819 david.dillard  1.49                     {
 820                                             // User is a member of the group.
 821                                             retVal = true;
 822                                             break;
 823                                         }
 824                     
 825                                         pTmpBuf++;
 826                                         dwTotalCount++;
 827 kumpf          1.38                 }
 828 david.dillard  1.49             }
 829                             }
 830 kumpf          1.38 
 831 david.dillard  1.49         //
 832                             // Free the allocated buffer.
 833                             //
 834                             if (pBuf != NULL)
 835                                 NetApiBufferFree(pBuf);
 836                         }
 837 kumpf          1.38 
 838 david.dillard  1.49     return retVal;
 839 kumpf          1.37 }
 840 kumpf          1.44 
 841 kumpf          1.63 Boolean System::lookupUserId(
 842                         const char* userName,
 843                         PEGASUS_UID_T& uid,
 844                         PEGASUS_GID_T& gid)
 845                     {
 846                         // ATTN: Implement this method to look up the specified user
 847                         return false;
 848                     }
 849                     
 850 kumpf          1.71.4.1 Boolean System::changeUserContext_SingleThreaded(
 851                             const char* userName,
 852 kumpf          1.63         const PEGASUS_UID_T& uid,
 853                             const PEGASUS_GID_T& gid)
 854 kumpf          1.44     {
 855                             // ATTN: Implement this method to change the process user context to the
 856                             //       specified user
 857                             return false;
 858                         }
 859                         
 860 kumpf          1.15     Uint32 System::getPID()
 861                         {
 862 kumpf          1.18         return _getpid();
 863 mike           1.21     }
 864                         
 865                         Boolean System::truncateFile(
 866 david.dillard  1.49         const char* path,
 867 mike           1.21         size_t newSize)
 868                         {
 869 david.dillard  1.49     
 870                             Boolean rv = false;
 871 mike           1.21         int fd = open(path, O_RDWR);
 872 david.dillard  1.49         if (fd != -1)
 873                             {
 874                                 if (chsize(fd, newSize) == 0)
 875                                 {
 876                                     rv = true;
 877                                 }
 878 mike           1.21     
 879 david.dillard  1.49             close(fd);
 880                             }
 881 mike           1.21     
 882 david.dillard  1.49         return rv;
 883 kumpf          1.15     }
 884                         
 885 tony           1.27     // Is absolute path?
 886                         Boolean System::is_absolute_path(const char *path)
 887                         {
 888 david.dillard  1.49         char full[_MAX_PATH];
 889                             char path_slash[_MAX_PATH];
 890                             char *p;
 891                         
 892                             strncpy(path_slash, path, _MAX_PATH);
 893                             path_slash[_MAX_PATH-1] = '\0';
 894                         
 895 kumpf          1.70         for (p = path_slash; p < path_slash + strlen(path_slash); p++)
 896 david.dillard  1.49           if (*p == '/')
 897                                   *p = '\\';
 898                         
 899 kumpf          1.70         return (strcasecmp(
 900                                 _fullpath(full, path_slash, _MAX_PATH), path_slash) == 0);
 901 kumpf          1.34     }
 902                         
 903                         // Changes file permissions on the given file.
 904                         Boolean System::changeFilePermissions(const char* path, mode_t mode)
 905                         {
 906                             // ATTN: File permissions are not currently defined in Windows
 907                             return true;
 908 tony           1.27     }
 909 david          1.29     
 910 kumpf          1.44     Boolean System::verifyFileOwnership(const char* path)
 911                         {
 912                             // ATTN: Implement this to check that the owner of the specified file is
 913                             //       the same as the effective user for this process.
 914                             return true;
 915                         }
 916                         
 917 kumpf          1.53     void System::syslog(const String& ident, Uint32 severity, const char* message)
 918 david          1.29     {
 919 kumpf          1.53         // Not implemented
 920 david          1.29     }
 921                         
 922 marek          1.69     void System::openlog(const char *ident, int logopt, int facility)
 923                         {
 924                             // Not implemented
 925                         }
 926                         
 927                         void System::closelog()
 928                         {
 929                             // Not implemented
 930                         }
 931                         
 932                         
 933                         
 934 david          1.29     // System ID constants for Logger::put and Logger::trace
 935                         const String System::CIMSERVER = "cimserver";  // Server system ID
 936 tony           1.27     
 937 marek          1.68     // check if a given IP address is defined on the local network interfaces
 938                         Boolean System::isIpOnNetworkInterface(Uint32 inIP)
 939                         {
 940                             SOCKET sock;
 941                             int interfaces = 0;
 942                             int errcode;
 943                         
 944                             if ( SOCKET_ERROR != ( sock  = WSASocket(AF_INET,
 945                                                  SOCK_RAW, 0, NULL, 0, 0) ) )
 946                             {
 947                                 unsigned long *bytes_returned=0;
 948                                 char *output_buf = (char *)calloc(1, 256);
 949                                 int buf_size = 256;
 950 kumpf          1.70     
 951 marek          1.68             if ( 0 == (errcode = WSAIoctl(sock,
 952                                                               SIO_ADDRESS_LIST_QUERY,
 953                                                               NULL,
 954                                                               0,
 955                                                               output_buf,
 956                                                               256,
 957                                                               bytes_returned,
 958                                                               NULL,
 959                                                               NULL)) )
 960                                 {
 961                                     SOCKET_ADDRESS_LIST *addr_list;
 962                                     SOCKET_ADDRESS *addr;
 963                                     Uint32 ip;
 964                                     struct sockaddr_in *sin;
 965 kumpf          1.70     
 966                                     addr_list = (SOCKET_ADDRESS_LIST *)output_buf;
 967 marek          1.68                 addr = addr_list->Address;
 968 kumpf          1.70     
 969 marek          1.68                 sin = (struct sockaddr_in *)addr->lpSockaddr;
 970 kumpf          1.70     
 971                                     for ( ; interfaces < addr_list->iAddressCount; interfaces++)
 972 marek          1.68                 {
 973                                         ip = sin->sin_addr.s_addr;
 974                                         addr++;
 975                                         sin = (struct sockaddr_in *)addr->lpSockaddr;
 976                                         if (ip == inIP)
 977                                         {
 978                                             free(output_buf);
 979                                             closesocket(sock);
 980 kumpf          1.70                         return true;
 981 marek          1.68                     }
 982                                     }
 983 kumpf          1.70             }
 984                                 else
 985                                 {
 986 marek          1.68                 free(output_buf);
 987                                     return false;
 988                                 }
 989                                 free(output_buf);
 990                                 closesocket(sock);
 991                             }
 992                             return false;
 993                         }
 994                         
 995 thilo.boehm    1.71.4.2 String System::getErrorMSG_NLS(int errorCode,int errorCode2)
 996                         {
 997                             LPVOID winErrorMsg = NULL;
 998                         
 999                             if (FormatMessage(
1000                                     FORMAT_MESSAGE_ALLOCATE_BUFFER |
1001                                     FORMAT_MESSAGE_FROM_SYSTEM |
1002                                     FORMAT_MESSAGE_IGNORE_INSERTS,
1003                                     NULL, 
1004                                     errorCode,
1005                                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1006                                     (LPTSTR)&winErrorMsg, 
1007                                     0, 
1008                                     NULL))
1009                             {
1010                                 MessageLoaderParms parms(
1011                                     "Common.System.ERROR_MESSAGE.STANDARD",
1012                                     "$0 (error code $1)",(char*)winErrorMsg,errorCode);
1013                                 LocalFree(winErrorMsg);
1014                                 return MessageLoader::getMessage(parms);
1015                             } 
1016 thilo.boehm    1.71.4.2 
1017                             MessageLoaderParms parms(
1018                                 "Common.System.ERROR_MESSAGE.STANDARD",
1019                                 "$0 (error code $1)","",errorCode);
1020                             return MessageLoader::getMessage(parms);
1021                         
1022                         }
1023                         
1024                         String System::getErrorMSG(int errorCode,int errorCode2)
1025                         {
1026                         
1027                             String buffer;
1028                             LPVOID winErrorMsg = NULL;
1029                         
1030                             char strErrorCode[32];
1031                             sprintf(strErrorCode, "%d", errorCode);
1032 marek          1.68     
1033 thilo.boehm    1.71.4.2     if (FormatMessage(
1034                                     FORMAT_MESSAGE_ALLOCATE_BUFFER |
1035                                     FORMAT_MESSAGE_FROM_SYSTEM |
1036                                     FORMAT_MESSAGE_IGNORE_INSERTS,
1037                                     NULL, 
1038                                     errorCode,
1039                                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1040                                     (LPTSTR)&winErrorMsg, 
1041                                     0, 
1042                                     NULL))
1043                             {
1044                                 buffer.append((char*)winErrorMsg);
1045                                 LocalFree(winErrorMsg);
1046                             }
1047                         
1048                             buffer.append(" (error code ");
1049                             buffer.append(strErrorCode);
1050                             buffer.append(")");
1051                         
1052                             return buffer;
1053                         }
1054 kumpf          1.67     
1055                         ///////////////////////////////////////////////////////////////////////////////
1056                         // AutoFileLock class
1057                         ///////////////////////////////////////////////////////////////////////////////
1058                         
1059                         AutoFileLock::AutoFileLock(const char* fileName)
1060                         {
1061                             // ATTN: Not implemented
1062                         }
1063                         
1064                         AutoFileLock::~AutoFileLock()
1065                         {
1066                             // ATTN: Not implemented
1067                         }
1068                         
1069 mike           1.13     PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2