(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            // Author: Mike Brasher (mbrasher@bmc.com)
 33            //
 34 mike  1.14 // Modified By: Sushma Fernandes (sushma_fernandes@hp.com)
 35            //              Nag Boranna, Hewlett-Packard Company (nagaraja_boranna@hp.com)
 36 kumpf 1.35 //              Bapu Patil (bapu_patil@hp.com)
 37 mike  1.13 //
 38 david 1.29 // Modified By: Dave Rosckes (rosckes@us.ibm.com)
 39 kumpf 1.36 //              Terry Martin, Hewlett-Packard Company (terry.martin@hp.com)
 40 a.arora 1.40 //              Amit K Arora, IBM (amita@in.ibm.com) for Bug#1428
 41 h.sterling 1.46 //              Seema Gupta (gseema@in.ibm.com) for Bug#1617
 42 david.dillard 1.43 //              David Dillard, VERITAS Software Corp.
 43                    //                  (david.dillard@veritas.com)
 44 david         1.29 //
 45 mike          1.13 //%/////////////////////////////////////////////////////////////////////////////
 46                    
 47                    #include "System.h"
 48                    
 49                    #include <windows.h>
 50 tony          1.31 #ifndef _WINSOCKAPI_
 51                    #include <winsock2.h>
 52                    #endif
 53 mike          1.21 #include <fcntl.h>
 54 mike          1.13 #include <sys/types.h>
 55                    #include <time.h>
 56                    #include <sys/timeb.h>
 57                    #include <io.h>
 58 tony          1.33 #include <conio.h>
 59 mike          1.13 #include <direct.h>
 60                    #include <sys/types.h>
 61 mday          1.19 #include <windows.h>
 62 kumpf         1.18 #include <process.h>
 63 kumpf         1.38 #include <lm.h>
 64 mike          1.13 
 65 h.sterling    1.52 #define SECURITY_WIN32
 66                    #include <security.h>
 67 h.sterling    1.50 
 68 kumpf         1.36 PEGASUS_NAMESPACE_BEGIN
 69                    
 70 se.gupta      1.41 #define PEGASUS_ACCESS_EXISTS 0
 71                    #define PEGASUS_ACCESS_WRITE 2
 72                    #define PEGASUS_ACCESS_READ 4
 73                    #define PEGASUS_ACCESS_READ_AND_WRITE 6
 74 mike          1.13 
 75 tony          1.33 #define PW_BUFF_LEN 65
 76                    
 77 mike          1.13 void System::getCurrentTime(Uint32& seconds, Uint32& milliseconds)
 78                    {
 79                        FILETIME ft;
 80                        GetSystemTimeAsFileTime(&ft);
 81                        ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime };
 82                        largeInt.QuadPart -= 0x19db1ded53e8000;
 83                        seconds = long(largeInt.QuadPart / (10000 * 1000));
 84                        milliseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10);
 85 karl          1.23     // This is a real hack. Added the following line after timevalue was
 86                        // corrected and this apparently wrong. ks 7 apri 2002
 87                        milliseconds = milliseconds / 1000;
 88 mike          1.13 }
 89                    
 90 jim.wunderlich 1.60 void System::getCurrentTimeUsec(Uint32& seconds, Uint32& microseconds)
 91                     {
 92                         FILETIME ft;
 93                         GetSystemTimeAsFileTime(&ft);
 94                         ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime };
 95                         largeInt.QuadPart -= 0x19db1ded53e8000;
 96                         seconds = long(largeInt.QuadPart / (10000 * 1000));
 97                         microseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10);
 98                     }
 99                     
100 mike           1.13 String System::getCurrentASCIITime()
101                     {
102                         char tmpbuf[128];
103 kumpf          1.28     _strdate( tmpbuf );
104                         String date = tmpbuf;
105 mike           1.13     _strtime( tmpbuf );
106 kumpf          1.28     date.append("-");
107                         date.append(tmpbuf);
108                         return date;
109 mike           1.13 }
110                     
111                     void System::sleep(Uint32 seconds)
112                     {
113                         Sleep(seconds * 1000);
114                     }
115                     
116                     Boolean System::exists(const char* path)
117                     {
118 se.gupta       1.41     return _access(path, PEGASUS_ACCESS_EXISTS) == 0;
119 mike           1.13 }
120                     
121                     Boolean System::canRead(const char* path)
122                     {
123 se.gupta       1.41     return _access(path, PEGASUS_ACCESS_READ) == 0;
124 mike           1.13 }
125                     
126                     Boolean System::canWrite(const char* path)
127                     {
128 se.gupta       1.41     return _access(path, PEGASUS_ACCESS_WRITE) == 0;
129 mike           1.13 }
130                     
131                     Boolean System::getCurrentDirectory(char* path, Uint32 size)
132                     {
133                         return GetCurrentDirectory(size, path) != 0;
134                     }
135                     
136                     Boolean System::isDirectory(const char* path)
137                     {
138                         struct stat st;
139                     
140                         if (stat(path, &st) != 0)
141 david.dillard  1.49         return false;
142 mike           1.13 
143                         return (st.st_mode & _S_IFDIR) != 0;
144                     }
145                     
146                     Boolean System::changeDirectory(const char* path)
147                     {
148                         return chdir(path) == 0;
149                     }
150                     
151                     Boolean System::makeDirectory(const char* path)
152                     {
153                         return _mkdir(path) == 0;
154                     }
155                     
156                     Boolean System::getFileSize(const char* path, Uint32& size)
157                     {
158                         struct stat st;
159                     
160                         if (stat(path, &st) != 0)
161 david.dillard  1.49         return false;
162 mike           1.13 
163                         size = st.st_size;
164                         return true;
165                     }
166                     
167                     Boolean System::removeDirectory(const char* path)
168                     {
169 david.dillard  1.49     return rmdir(path) == 0;
170 mike           1.13 }
171                     
172                     Boolean System::removeFile(const char* path)
173                     {
174 david.dillard  1.49     return unlink(path) == 0;
175 mike           1.13 }
176                     
177                     Boolean System::renameFile(const char* oldPath, const char* newPath)
178                     {
179                         return rename(oldPath, newPath) == 0;
180                     }
181                     
182                     DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName)
183                     {
184                         return DynamicLibraryHandle(LoadLibrary(fileName));
185                     }
186                     
187 mike           1.14 void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle)
188                     {
189 h.sterling     1.46     FreeLibrary(HINSTANCE(libraryHandle));
190 mike           1.14 }
191                     
192 mike           1.13 String System::dynamicLoadError(void) {
193                     return String();
194                     }
195                     
196                     DynamicSymbolHandle System::loadDynamicSymbol(
197                         DynamicLibraryHandle libraryHandle,
198                         const char* symbolName)
199                     {
200                         return DynamicSymbolHandle(GetProcAddress(
201 h.sterling     1.46     (HINSTANCE)libraryHandle, symbolName));
202 mike           1.13 }
203                     
204                     String System::getHostName()
205                     {
206 kumpf          1.55     static char hostname[PEGASUS_MAXHOSTNAMELEN + 1];
207 mike           1.13 
208                         if (!*hostname)
209 kumpf          1.55     {
210 mike           1.13         gethostname(hostname, sizeof(hostname));
211 kumpf          1.55     }
212                         hostname[sizeof(hostname)-1] = 0;
213 mike           1.13 
214                         return hostname;
215 kumpf          1.16 }
216                     
217 kumpf          1.22 String System::getFullyQualifiedHostName ()
218                     {
219 kumpf          1.56     static char FQHostName[PEGASUS_MAXHOSTNAMELEN + 1];
220 a.arora        1.40 
221                         if (!*FQHostName)
222                         {
223                             String hostname = getHostName();
224                             struct hostent* hostEnt;
225                     
226                             hostEnt = gethostbyname((const char *)hostname.getCString());
227                             if (hostEnt == NULL)
228                             {
229                                 return String::EMPTY;
230                             }
231 kumpf          1.56         strncpy(FQHostName, hostEnt->h_name, sizeof(FQHostName)-1);
232 a.arora        1.40     }
233 david.dillard  1.49 
234 a.arora        1.40     return FQHostName;
235 kumpf          1.22 }
236                     
237                     String System::getSystemCreationClassName ()
238                     {
239 karl           1.39     return "CIM_ComputerSystem";
240 kumpf          1.22 }
241                     
242 kumpf          1.16 Uint32 System::lookupPort(
243                         const char * serviceName,
244                         Uint32 defaultPort)
245                     {
246                         Uint32 localPort;
247                     
248                         struct servent *serv;
249                     
250                         //
251 david.dillard  1.48     // Get the port number.
252 kumpf          1.16     //
253 david.dillard  1.49     if ( (serv = getservbyname(serviceName, TCP)) != NULL )
254 kumpf          1.16     {
255 david.dillard  1.48         localPort = ntohs(serv->s_port);
256 kumpf          1.16     }
257                         else
258                         {
259                             localPort = defaultPort;
260                         }
261                     
262                         return localPort;
263 mike           1.13 }
264                     
265 mike           1.14 String System::getPassword(const char* prompt)
266                     {
267 david.dillard  1.49     char password[PW_BUFF_LEN] = {0};
268                         int num_chars = 0;
269                         int ch;
270 tony           1.33 
271 david.dillard  1.49     fputs(prompt, stderr);
272 tony           1.33 
273 david.dillard  1.49     while ((ch = _getch()) != '\r' &&
274                                 num_chars < PW_BUFF_LEN)
275 tony           1.33         {
276 david.dillard  1.49         // EOF
277                             if (ch == EOF)
278 tony           1.33         {
279 david.dillard  1.49             fputs("[EOF]\n", stderr);
280                                 return String::EMPTY;
281 tony           1.33         }
282 david.dillard  1.49         // Backspace or Delete
283                             else if ((ch == '\b' || ch == 127) &&
284                                     num_chars > 0)
285 tony           1.33         {
286 david.dillard  1.49             password[--num_chars] = '\0';
287                                 fputs("\b \b", stderr);
288 tony           1.33         }
289 david.dillard  1.49         // CTRL+C
290                             else if (ch == 3)
291 tony           1.33         {
292 david.dillard  1.49             // _getch() does not catch CTRL+C
293                                 fputs("^C\n", stderr);
294                                 exit(-1);
295 tony           1.33         }
296 david.dillard  1.49         // CTRL+Z
297                             else if (ch == 26)
298 tony           1.33         {
299 david.dillard  1.49             fputs("^Z\n", stderr);
300                                 return String::EMPTY;
301 tony           1.33         }
302 david.dillard  1.49         // Esc
303                             else if (ch == 27)
304 tony           1.33         {
305 david.dillard  1.49             fputc('\n', stderr);
306                                 fputs(prompt, stderr);
307                                 num_chars = 0;
308                             }
309                             // Function keys (0 or E0) are a guards for a Function key codes
310                             else if (ch == 0 || ch == 0xE0)
311                             {
312                                 ch = (ch << 4) | _getch();
313                                 // Handle DELETE, left arrow, keypad DEL, and keypad left arrow
314                                 if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) &&
315                                         num_chars > 0)
316 tony           1.33             {
317 david.dillard  1.49                 password[--num_chars] = '\0';
318                                     fputs("\b \b", stderr);
319 tony           1.33             }
320 david.dillard  1.49             else
321 tony           1.33             {
322 david.dillard  1.49                 fputc('\a', stderr);
323 tony           1.33             }
324                             }
325 david.dillard  1.49         else if ((num_chars < sizeof(password) - 1) &&
326                                         !iscntrl(((unsigned char)(ch))))
327 tony           1.33         {
328 david.dillard  1.49             password[num_chars++] = ch;
329                                 fputc('*', stderr);
330 tony           1.33         }
331 david.dillard  1.49         else
332 tony           1.33         {
333 david.dillard  1.49             fputc('\a', stderr);
334 tony           1.33         }
335                         }
336 mike           1.14 
337 david.dillard  1.49     fputc('\n', stderr);
338                         password[num_chars] = '\0';
339 tony           1.33 
340 david.dillard  1.49     return String(password);
341 mike           1.14 }
342                     
343 kumpf          1.24 String System::getEffectiveUserName()
344 mike           1.14 {
345 h.sterling     1.52 #if (_MSC_VER >= 1300) || defined(PEGASUS_WINDOWS_SDK_HOME)
346                     
347                     	//Bug 3076 fix
348 h.sterling     1.57 	wchar_t fullUserName[UNLEN+1];
349 h.sterling     1.59 	DWORD userNameSize = sizeof(fullUserName)/sizeof(fullUserName[0]);
350 h.sterling     1.57 	wchar_t computerName[MAX_COMPUTERNAME_LENGTH+1];
351 h.sterling     1.59     DWORD computerNameSize = sizeof(computerName)/sizeof(computerName[0]);    
352 h.sterling     1.57 	wchar_t userName[UNLEN+1];
353                         wchar_t userDomain[UNLEN+1];
354 h.sterling     1.50 	String userId;
355                     
356 h.sterling     1.57 	if (!GetUserNameExW(NameSamCompatible, fullUserName, &userNameSize))
357 h.sterling     1.50 	{
358                     		return String();
359                     	}
360                     
361 h.sterling     1.57 	wchar_t* index = wcschr(fullUserName, '\\');
362                     	*index = 0;
363                     	wcscpy(userDomain, fullUserName);
364                     	wcscpy(userName, index + 1);
365 h.sterling     1.50  
366                     	//The above function will return the system name as the domain if
367                     	//the user is not on a real domain.  Strip this out so that the rest of
368                     	//our windows user functions work.  What if the system name and the domain
369                     	//name are the same?
370 h.sterling     1.57     GetComputerNameW(computerName, &computerNameSize);
371 h.sterling     1.50 		
372 h.sterling     1.57 	if (wcscmp(computerName, userDomain) != 0) 
373 h.sterling     1.50 	{
374 h.sterling     1.57         //userId.append(userDomain);
375                             Uint32 n = wcslen(userDomain);
376                             for(unsigned long i = 0; i < n; i++)
377                             {
378                                 userId.append(Char16(userDomain[i]));
379                             }
380 h.sterling     1.50 		userId.append("\\");
381 h.sterling     1.57 		//userId.append(userName);
382                             n = wcslen(userName);
383                             for(unsigned long i = 0; i < n; i++)
384                             {
385                                 userId.append(Char16(userName[i]));
386                             }
387                     
388 h.sterling     1.50 	} else
389                     	{
390 h.sterling     1.57 		//userId.append(userName);
391                             Uint32 n = wcslen(userName);
392                             for(unsigned long i = 0; i < n; i++)
393                             {
394                                 userId.append(Char16(userName[i]));
395                             }
396                     
397 h.sterling     1.50 	}
398                     
399                     	return userId;
400 h.sterling     1.52 
401                     #else //original getEffectiveUserName function
402                         
403 david.dillard  1.49     int retcode = 0;
404 tony           1.33 
405 david.dillard  1.49     // UNLEN (256) is the limit, not including null
406 h.sterling     1.57     wchar_t pUserName[256+1] = {0};
407 h.sterling     1.59     DWORD nSize = sizeof(pUserName)/sizeof(pUserName[0]);
408 mike           1.14 
409 h.sterling     1.57     retcode = GetUserNameW(pUserName, &nSize);
410 david.dillard  1.49     if (retcode == 0)
411 tony           1.33     {
412 david.dillard  1.49         // zero is failure
413                             return String();
414 tony           1.33     }
415 h.sterling     1.58     String userId;
416 h.sterling     1.57     Uint32 n = wcslen(pUserName);
417                         for(unsigned long i = 0; i < n; i++)
418                         {
419                             userId.append(Char16(pUserName[i]));
420                         }
421                     
422 david.dillard  1.49 
423 h.sterling     1.57     return userId;
424 h.sterling     1.52 #endif
425 mike           1.14 }
426                     
427                     String System::encryptPassword(const char* password, const char* salt)
428                     {
429 david.dillard  1.49     BYTE pbBuffer[PW_BUFF_LEN] = {0};
430                         DWORD dwByteCount;
431                         char pcSalt[3] = {0};
432                     
433                         strncpy(pcSalt, salt, 2);
434                         dwByteCount = strlen(password);
435                         memcpy(pbBuffer, password, dwByteCount);
436                         for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++)
437                                 (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0];
438                     
439                         return String(pcSalt) + String((char *)pbBuffer);
440 mike           1.14 }
441                     
442 kumpf          1.26 Boolean System::isSystemUser(const char* userName)
443 mike           1.14 {
444 h.sterling     1.46     Boolean isSystemUser = false;
445                     
446                         char mUserName[UNLEN+1];
447                         char mDomainName[UNLEN+1];
448 david.dillard  1.51     char tUserName[UNLEN+1];
449 h.sterling     1.46     wchar_t wUserName[UNLEN+1];
450                         wchar_t wDomainName[UNLEN+1];
451                         char* pbs;
452                         bool usingDomain = false;
453 david.dillard  1.49 
454 h.sterling     1.46     LPBYTE pComputerName=NULL;
455                         DWORD dwLevel = 1;
456                         LPUSER_INFO_1 pUserInfo = NULL;
457                         NET_API_STATUS nStatus = NULL;
458                     
459 david.dillard  1.51     // Make a copy of the specified username, it cannot be used directly because it's
460                         // declared as const and strchr() may modify the string.
461                         strncpy(tUserName, userName, sizeof(tUserName) - 1);
462 david.dillard  1.54     tUserName[sizeof(tUserName)- 1] = '\0';
463 david.dillard  1.51 
464 david.dillard  1.49     //separate the domain and user name if both are present.
465 david.dillard  1.51     if (NULL != (pbs = strchr(tUserName, '\\')))
466 h.sterling     1.46     {
467                             *pbs = '\0';
468 david.dillard  1.51         strcpy(mDomainName, tUserName);
469 h.sterling     1.46         strcpy(mUserName, pbs+1);
470                             usingDomain = true;
471                     
472 david.dillard  1.51     } else if ((NULL != (pbs = (strchr(tUserName, '@')))) ||
473                                    (NULL != (pbs = (strchr(tUserName, '.')))))
474 h.sterling     1.46     {
475                             *pbs = '\0';
476                             strcpy(mDomainName, pbs+1);
477 david.dillard  1.51         strcpy(mUserName, tUserName);
478 h.sterling     1.46         usingDomain = true;
479 david.dillard  1.49 
480 h.sterling     1.46     } else
481                         {
482                             strcpy(mDomainName, ".");
483 david.dillard  1.51         strcpy(mUserName, tUserName);
484 h.sterling     1.46     }
485                     
486                         //convert domain name to unicode
487                         if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1))
488                         {
489                             return false;
490                         }
491                     
492                         //convert username to unicode
493                         if (!MultiByteToWideChar(CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName)+1))
494                         {
495                             return false;
496                         }
497 david.dillard  1.49 
498 h.sterling     1.46     if (usingDomain)
499                         {
500                             //get domain controller
501                             DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName);
502 david.dillard  1.49         if (rc == NERR_Success)
503 h.sterling     1.46         {
504                                 wcscpy(wDomainName, (LPWSTR) pComputerName); //this is automatically prefixed with "\\"
505 david.dillard  1.49         }
506 h.sterling     1.46         /*
507                             else
508                             {
509                                 // failover
510                                 // ATTN: This is commented out until there is resolution on Bugzilla 2236. -hns 2/2005
511                                 // This needs to be more thoroughly tested when we uncomment it out.
512 david.dillard  1.49 
513 h.sterling     1.46             PDOMAIN_CONTROLLER_INFO DomainControllerInfo = NULL;
514                     
515                                 //this function does not take wide strings
516                                 rc = DsGetDcName(NULL,
517                                                  mDomainName,
518                                                  NULL,
519                                                  NULL,
520                                                  DS_DIRECTORY_SERVICE_REQUIRED,  //not sure what flags we want here
521                                                  &DomainControllerInfo);
522                     
523                                 if (rc == ERROR_SUCCESS && DomainControllerInfo)
524                                 {
525                                     strcpy(mDomainName, DomainControllerInfo->DomainName);
526                                     NetApiBufferFree(DomainControllerInfo);
527                     
528                                     if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1))
529                                     {
530                                         return false;
531                                     }
532                                 }
533                             }
534 h.sterling     1.46         */
535                         }
536                     
537                         //get user info
538                         nStatus = NetUserGetInfo(wDomainName,
539                                                  wUserName,
540                                                  dwLevel,
541                                                  (LPBYTE *)&pUserInfo);
542                     
543                         if (nStatus == NERR_Success)
544                         {
545                             isSystemUser = true;
546                         }
547 david.dillard  1.49 
548                         if (pComputerName != NULL)
549 h.sterling     1.46     {
550                             NetApiBufferFree(pComputerName);
551                         }
552                     
553                         if (pUserInfo != NULL)
554                         {
555                             NetApiBufferFree(pUserInfo);
556                         }
557                     
558                         return isSystemUser;
559 mike           1.14 }
560                     
561 h.sterling     1.46 
562 david.dillard  1.43 Boolean System::isPrivilegedUser(const String& userName)
563 mike           1.14 {
564 h.sterling     1.46     Boolean isPrivileged = false;
565                     
566                         char mUserName[UNLEN+1];
567                         char mDomainName[UNLEN+1];
568                         wchar_t wUserName[UNLEN+1];
569                         wchar_t wDomainName[UNLEN+1];
570                         char* pbs;
571                         char userStr[UNLEN+1];
572                         bool usingDomain = false;
573                     
574                         LPBYTE pComputerName=NULL;
575                         DWORD dwLevel = 1;
576                         LPUSER_INFO_1 pUserInfo = NULL;
577                         NET_API_STATUS nStatus = NULL;
578                     
579                         //get the username in the correct format
580                         strcpy(userStr, (const char*)userName.getCString());
581                     
582 david.dillard  1.49     //separate the domain and user name if both are present.
583 h.sterling     1.46     if (NULL != (pbs = strchr(userStr, '\\')))
584                         {
585                             *pbs = '\0';
586                             strcpy(mDomainName, userStr);
587                             strcpy(mUserName, pbs+1);
588                             usingDomain = true;
589                     
590                         } else if ((NULL != (pbs = (strchr(userStr, '@')))) ||
591                                    (NULL != (pbs = (strchr(userStr, '.')))))
592                         {
593                             *pbs = '\0';
594                             strcpy(mDomainName, pbs+1);
595                             strcpy(mUserName, userStr);
596                             usingDomain = true;
597 david.dillard  1.49 
598 h.sterling     1.46     } else
599                         {
600                             strcpy(mDomainName, ".");
601                             strcpy(mUserName, userStr);
602                         }
603                     
604                         //convert domain name to unicode
605                         if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1))
606                         {
607                             return false;
608                         }
609                     
610                         //convert username to unicode
611                         if (!MultiByteToWideChar(CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName)+1))
612                         {
613                             return false;
614                         }
615                     
616                         if (usingDomain)
617                         {
618                             //get domain controller
619 h.sterling     1.46         DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName);
620 david.dillard  1.49         if (rc == NERR_Success)
621 h.sterling     1.46         {
622                                 wcscpy(wDomainName, (LPWSTR) pComputerName); //this is automatically prefixed with "\\"
623 david.dillard  1.49         }
624 h.sterling     1.46         /*
625                             else
626                             {
627                                 // failover
628                                 // ATTN: This is commented out until there is resolution on Bugzilla 2236. -hns 2/2005
629                                 // 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                                                  DS_DIRECTORY_SERVICE_REQUIRED,  //not sure what flags we want here
639                                                  &DomainControllerInfo);
640                     
641                                 if (rc == ERROR_SUCCESS && DomainControllerInfo)
642                                 {
643                                     strcpy(mDomainName, DomainControllerInfo->DomainName);
644                                     NetApiBufferFree(DomainControllerInfo);
645                     
646                                     if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1))
647                                     {
648                                         return false;
649                                     }
650                                 }
651                             }
652 h.sterling     1.46         */
653                         }
654                     
655                         //get privileges
656                         nStatus = NetUserGetInfo(wDomainName,
657                                                  wUserName,
658                                                  dwLevel,
659                                                  (LPBYTE *)&pUserInfo);
660                     
661 david.dillard  1.49     if ((nStatus == NERR_Success) &&
662 h.sterling     1.46         (pUserInfo != NULL) &&
663                             (pUserInfo->usri1_priv == USER_PRIV_ADMIN))
664                         {
665                             isPrivileged = true;
666                         }
667                     
668 david.dillard  1.49     if (pComputerName != NULL)
669 h.sterling     1.46     {
670                             NetApiBufferFree(pComputerName);
671                         }
672                     
673                         if (pUserInfo != NULL)
674                         {
675                             NetApiBufferFree(pUserInfo);
676                         }
677                     
678                         return isPrivileged;
679 mike           1.14 }
680 kumpf          1.20 
681                     String System::getPrivilegedUserName()
682                     {
683                         // ATTN-NB-03-20000304: Implement better way to get the privileged
684                         // user on the system.
685                     
686                         return (String("Administrator"));
687                     }
688 kumpf          1.37 
689                     Boolean System::isGroupMember(const char* userName, const char* groupName)
690                     {
691 david.dillard  1.49     Boolean retVal = false;
692 kumpf          1.38 
693 david.dillard  1.49     LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;
694                         DWORD dwLevel = 0;
695                         DWORD dwFlags = LG_INCLUDE_INDIRECT ;
696                         DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
697                         DWORD dwEntriesRead = 0;
698                         DWORD dwTotalEntries = 0;
699                         NET_API_STATUS nStatus;
700                     
701                     
702                         //
703                         // Call the NetUserGetLocalGroups function
704                         // specifying information level 0.
705                         //
706                         // The LG_INCLUDE_INDIRECT flag specifies that the
707                         // function should also return the names of the local
708                         // groups in which the user is indirectly a member.
709                         //
710                         nStatus = NetUserGetLocalGroups(NULL,
711 kumpf          1.38                                    (LPCWSTR)userName,
712                                                        dwLevel,
713                                                        dwFlags,
714                                                        (LPBYTE *) &pBuf,
715                                                        dwPrefMaxLen,
716                                                        &dwEntriesRead,
717                                                        &dwTotalEntries);
718                     
719 david.dillard  1.49     //
720                         // If the call succeeds,
721                         //
722                         if (nStatus == NERR_Success)
723                         {
724                             LPLOCALGROUP_USERS_INFO_0 pTmpBuf;
725                             DWORD i;
726                             DWORD dwTotalCount = 0;
727                     
728                             if ((pTmpBuf = pBuf) != NULL)
729                             {
730 kumpf          1.38             //
731 david.dillard  1.49             // Loop through the local groups that the user belongs
732                                 // and find the matching group name.
733 kumpf          1.38             //
734 david.dillard  1.49             for (i = 0; i < dwEntriesRead; i++)
735 kumpf          1.38             {
736 david.dillard  1.49                 //
737                                     // Compare the user's group name to groupName.
738                                     //
739                                     if ( strcmp ((char *)pTmpBuf->lgrui0_name, groupName) == 0 )
740                                     {
741                                         // User is a member of the group.
742                                         retVal = true;
743                                         break;
744                                     }
745                     
746                                     pTmpBuf++;
747                                     dwTotalCount++;
748 kumpf          1.38             }
749 david.dillard  1.49         }
750                         }
751 kumpf          1.38 
752 david.dillard  1.49     //
753                         // Free the allocated memory.
754                         //
755                         if (pBuf != NULL)
756                             NetApiBufferFree(pBuf);
757                     
758                         //
759                         // If the given user and group are not found in the local group
760                         // then try on the global groups.
761                         //
762                         if (!retVal)
763                         {
764                             LPGROUP_USERS_INFO_0 pBuf = NULL;
765                             dwLevel = 0;
766                             dwPrefMaxLen = MAX_PREFERRED_LENGTH;
767                             dwEntriesRead = 0;
768                             dwTotalEntries = 0;
769                     
770                             //
771                             // Call the NetUserGetGroups function, specifying level 0.
772                             //
773 david.dillard  1.49         nStatus = NetUserGetGroups(NULL,
774 kumpf          1.38                                   (LPCWSTR)userName,
775                                                       dwLevel,
776                                                       (LPBYTE*)&pBuf,
777                                                       dwPrefMaxLen,
778                                                       &dwEntriesRead,
779                                                       &dwTotalEntries);
780 david.dillard  1.49         //
781                             // If the call succeeds,
782                             //
783                             if (nStatus == NERR_Success)
784                             {
785                                 LPGROUP_USERS_INFO_0 pTmpBuf;
786                                 DWORD i;
787                                 DWORD dwTotalCount = 0;
788                     
789                                 if ((pTmpBuf = pBuf) != NULL)
790                                 {
791 kumpf          1.38                 //
792 david.dillard  1.49                 // Loop through the global groups to which the user belongs
793                                     // and find the matching group name.
794 kumpf          1.38                 //
795 david.dillard  1.49                 for (i = 0; i < dwEntriesRead; i++)
796 kumpf          1.38                 {
797 david.dillard  1.49                     //
798                                         // Compare the user's group name to groupName.
799                                         //
800                                         if ( strcmp ((char *)pTmpBuf->grui0_name, groupName) == 0 )
801                                         {
802                                             // User is a member of the group.
803                                             retVal = true;
804                                             break;
805                                         }
806                     
807                                         pTmpBuf++;
808                                         dwTotalCount++;
809 kumpf          1.38                 }
810 david.dillard  1.49             }
811                             }
812 kumpf          1.38 
813 david.dillard  1.49         //
814                             // Free the allocated buffer.
815                             //
816                             if (pBuf != NULL)
817                                 NetApiBufferFree(pBuf);
818                         }
819 kumpf          1.38 
820 david.dillard  1.49     return retVal;
821 kumpf          1.37 }
822 kumpf          1.44 
823                     Boolean System::changeUserContext(const char* userName)
824                     {
825                         // ATTN: Implement this method to change the process user context to the
826                         //       specified user
827                         return false;
828                     }
829                     
830 kumpf          1.15 Uint32 System::getPID()
831                     {
832 kumpf          1.18     return _getpid();
833 mike           1.21 }
834                     
835                     Boolean System::truncateFile(
836 david.dillard  1.49     const char* path,
837 mike           1.21     size_t newSize)
838                     {
839 david.dillard  1.49 
840                         Boolean rv = false;
841 mike           1.21     int fd = open(path, O_RDWR);
842 david.dillard  1.49     if (fd != -1)
843                         {
844                             if (chsize(fd, newSize) == 0)
845                             {
846                                 rv = true;
847                             }
848 mike           1.21 
849 david.dillard  1.49         close(fd);
850                         }
851 mike           1.21 
852 david.dillard  1.49     return rv;
853 kumpf          1.15 }
854                     
855 tony           1.27 // Is absolute path?
856                     Boolean System::is_absolute_path(const char *path)
857                     {
858 david.dillard  1.49     char full[_MAX_PATH];
859                         char path_slash[_MAX_PATH];
860                         char *p;
861                     
862                         strncpy(path_slash, path, _MAX_PATH);
863                         path_slash[_MAX_PATH-1] = '\0';
864                     
865                         for(p = path_slash; p < path_slash + strlen(path_slash); p++)
866                           if (*p == '/')
867                               *p = '\\';
868                     
869                         return (strcasecmp(_fullpath( full, path_slash, _MAX_PATH ), path_slash) == 0) ? true : false;
870 kumpf          1.34 }
871                     
872                     // Changes file permissions on the given file.
873                     Boolean System::changeFilePermissions(const char* path, mode_t mode)
874                     {
875                         // ATTN: File permissions are not currently defined in Windows
876                         return true;
877 tony           1.27 }
878 david          1.29 
879 kumpf          1.44 Boolean System::verifyFileOwnership(const char* path)
880                     {
881                         // ATTN: Implement this to check that the owner of the specified file is
882                         //       the same as the effective user for this process.
883                         return true;
884                     }
885                     
886 kumpf          1.53 void System::syslog(const String& ident, Uint32 severity, const char* message)
887 david          1.29 {
888 kumpf          1.53     // Not implemented
889 david          1.29 }
890                     
891                     // System ID constants for Logger::put and Logger::trace
892                     const String System::CIMSERVER = "cimserver";  // Server system ID
893 tony           1.27 
894 mike           1.13 PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2