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