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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2