version 1.40, 2004/07/19 08:46:23
|
version 1.64, 2006/06/26 22:23:29
|
|
|
//%2003//////////////////////////////////////////////////////////////////////// |
//%2006//////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development |
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development |
// Company, L. P., IBM Corp., The Open Group, Tivoli Systems. |
// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. |
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.; | // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.; |
// IBM Corp.; EMC Corporation, The Open Group. | // IBM Corp.; EMC Corporation, The Open Group. |
|
// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.; |
|
// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. |
|
// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
|
// EMC Corporation; VERITAS Software Corporation; The Open Group. |
|
// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
|
// EMC Corporation; Symantec Corporation; The Open Group. |
// | // |
// Permission is hereby granted, free of charge, to any person obtaining a copy | // Permission is hereby granted, free of charge, to any person obtaining a copy |
// of this software and associated documentation files (the "Software"), to | // of this software and associated documentation files (the "Software"), to |
|
|
// Modified By: Dave Rosckes (rosckes@us.ibm.com) | // Modified By: Dave Rosckes (rosckes@us.ibm.com) |
// Terry Martin, Hewlett-Packard Company (terry.martin@hp.com) | // Terry Martin, Hewlett-Packard Company (terry.martin@hp.com) |
// Amit K Arora, IBM (amita@in.ibm.com) for Bug#1428 | // Amit K Arora, IBM (amita@in.ibm.com) for Bug#1428 |
|
// Seema Gupta (gseema@in.ibm.com) for Bug#1617 |
|
// David Dillard, VERITAS Software Corp. |
|
// (david.dillard@veritas.com) |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#include "System.h" | #include "System.h" |
| |
#include <windows.h> |
#include "Network.h" |
#ifndef _WINSOCKAPI_ |
|
#include <winsock2.h> |
|
#endif |
|
#include <fcntl.h> | #include <fcntl.h> |
#include <sys/types.h> | #include <sys/types.h> |
#include <time.h> | #include <time.h> |
|
|
#include <process.h> | #include <process.h> |
#include <lm.h> | #include <lm.h> |
| |
|
#define SECURITY_WIN32 |
|
#include <security.h> |
|
|
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
#define ACCESS_EXISTS 0 |
#define PEGASUS_ACCESS_EXISTS 0 |
#define ACCESS_WRITE 2 |
#define PEGASUS_ACCESS_WRITE 2 |
#define ACCESS_READ 4 |
#define PEGASUS_ACCESS_READ 4 |
#define ACCESS_READ_AND_WRITE 6 |
#define PEGASUS_ACCESS_READ_AND_WRITE 6 |
| |
#define PW_BUFF_LEN 65 | #define PW_BUFF_LEN 65 |
| |
|
|
milliseconds = milliseconds / 1000; | milliseconds = milliseconds / 1000; |
} | } |
| |
|
void System::getCurrentTimeUsec(Uint32& seconds, Uint32& microseconds) |
|
{ |
|
FILETIME ft; |
|
GetSystemTimeAsFileTime(&ft); |
|
ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime }; |
|
largeInt.QuadPart -= 0x19db1ded53e8000; |
|
seconds = long(largeInt.QuadPart / (10000 * 1000)); |
|
microseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10); |
|
} |
|
|
String System::getCurrentASCIITime() | String System::getCurrentASCIITime() |
{ | { |
char tmpbuf[128]; | char tmpbuf[128]; |
|
|
| |
Boolean System::exists(const char* path) | Boolean System::exists(const char* path) |
{ | { |
return _access(path, ACCESS_EXISTS) == 0; |
return _access(path, PEGASUS_ACCESS_EXISTS) == 0; |
} | } |
| |
Boolean System::canRead(const char* path) | Boolean System::canRead(const char* path) |
{ | { |
return _access(path, ACCESS_READ) == 0; |
return _access(path, PEGASUS_ACCESS_READ) == 0; |
} | } |
| |
Boolean System::canWrite(const char* path) | Boolean System::canWrite(const char* path) |
{ | { |
return _access(path, ACCESS_WRITE) == 0; |
return _access(path, PEGASUS_ACCESS_WRITE) == 0; |
} | } |
| |
Boolean System::getCurrentDirectory(char* path, Uint32 size) | Boolean System::getCurrentDirectory(char* path, Uint32 size) |
|
|
| |
String System::getHostName() | String System::getHostName() |
{ | { |
static char hostname[PEGASUS_MAXHOSTNAMELEN]; |
static char hostname[PEGASUS_MAXHOSTNAMELEN + 1]; |
| |
if (!*hostname) | if (!*hostname) |
|
{ |
gethostname(hostname, sizeof(hostname)); | gethostname(hostname, sizeof(hostname)); |
|
} |
|
hostname[sizeof(hostname)-1] = 0; |
| |
return hostname; | return hostname; |
} | } |
| |
String System::getFullyQualifiedHostName () | String System::getFullyQualifiedHostName () |
{ | { |
static char FQHostName[PEGASUS_MAXHOSTNAMELEN]; |
static char FQHostName[PEGASUS_MAXHOSTNAMELEN + 1]; |
| |
if (!*FQHostName) | if (!*FQHostName) |
{ | { |
|
|
{ | { |
return String::EMPTY; | return String::EMPTY; |
} | } |
strcpy(FQHostName, hostEnt->h_name); |
strncpy(FQHostName, hostEnt->h_name, sizeof(FQHostName)-1); |
} | } |
| |
return FQHostName; | return FQHostName; |
|
|
struct servent *serv; | struct servent *serv; |
| |
// | // |
// Get wbem-local port from /etc/services |
// Get the port number. |
// | // |
if ( (serv = getservbyname(serviceName, TCP)) != NULL ) | if ( (serv = getservbyname(serviceName, TCP)) != NULL ) |
{ | { |
localPort = serv->s_port; |
localPort = ntohs(serv->s_port); |
} | } |
else | else |
{ | { |
|
|
| |
String System::getEffectiveUserName() | String System::getEffectiveUserName() |
{ | { |
|
#if (_MSC_VER >= 1300) || defined(PEGASUS_WINDOWS_SDK_HOME) |
|
|
|
//Bug 3076 fix |
|
wchar_t fullUserName[UNLEN+1]; |
|
DWORD userNameSize = sizeof(fullUserName)/sizeof(fullUserName[0]); |
|
wchar_t computerName[MAX_COMPUTERNAME_LENGTH+1]; |
|
DWORD computerNameSize = sizeof(computerName)/sizeof(computerName[0]); |
|
wchar_t userName[UNLEN+1]; |
|
wchar_t userDomain[UNLEN+1]; |
|
String userId; |
|
|
|
if (!GetUserNameExW(NameSamCompatible, fullUserName, &userNameSize)) |
|
{ |
|
return String(); |
|
} |
|
|
|
wchar_t* index = wcschr(fullUserName, '\\'); |
|
*index = 0; |
|
wcscpy(userDomain, fullUserName); |
|
wcscpy(userName, index + 1); |
|
|
|
//The above function will return the system name as the domain if |
|
//the user is not on a real domain. Strip this out so that the rest of |
|
//our windows user functions work. What if the system name and the domain |
|
//name are the same? |
|
GetComputerNameW(computerName, &computerNameSize); |
|
|
|
if (wcscmp(computerName, userDomain) != 0) |
|
{ |
|
//userId.append(userDomain); |
|
Uint32 n = wcslen(userDomain); |
|
for(unsigned long i = 0; i < n; i++) |
|
{ |
|
userId.append(Char16(userDomain[i])); |
|
} |
|
userId.append("\\"); |
|
//userId.append(userName); |
|
n = wcslen(userName); |
|
for(unsigned long i = 0; i < n; i++) |
|
{ |
|
userId.append(Char16(userName[i])); |
|
} |
|
|
|
} else |
|
{ |
|
//userId.append(userName); |
|
Uint32 n = wcslen(userName); |
|
for(unsigned long i = 0; i < n; i++) |
|
{ |
|
userId.append(Char16(userName[i])); |
|
} |
|
|
|
} |
|
|
|
return userId; |
|
|
|
#else //original getEffectiveUserName function |
|
|
int retcode = 0; | int retcode = 0; |
| |
// UNLEN (256) is the limit, not including null | // UNLEN (256) is the limit, not including null |
char pUserName[256+1] = {0}; |
wchar_t pUserName[256+1] = {0}; |
DWORD nSize = sizeof(pUserName); |
DWORD nSize = sizeof(pUserName)/sizeof(pUserName[0]); |
| |
retcode = GetUserName(pUserName, &nSize); |
retcode = GetUserNameW(pUserName, &nSize); |
if (retcode == 0) | if (retcode == 0) |
{ | { |
// zero is failure | // zero is failure |
return String(); | return String(); |
} | } |
|
String userId; |
|
Uint32 n = wcslen(pUserName); |
|
for(unsigned long i = 0; i < n; i++) |
|
{ |
|
userId.append(Char16(pUserName[i])); |
|
} |
|
|
| |
return String(pUserName); |
return userId; |
|
#endif |
} | } |
| |
String System::encryptPassword(const char* password, const char* salt) | String System::encryptPassword(const char* password, const char* salt) |
|
|
return String(pcSalt) + String((char *)pbBuffer); | return String(pcSalt) + String((char *)pbBuffer); |
} | } |
| |
|
String processUserName; |
|
Mutex processUserNameMut; |
|
|
Boolean System::isSystemUser(const char* userName) | Boolean System::isSystemUser(const char* userName) |
{ | { |
//ATTN: Implement this method to verify if user is vaild on the local system |
if(processUserName.size() == 0) |
// This is used in User Manager |
{ |
|
// Lock and recheck the processUserName length in case two threads |
|
// enter this block simultaneously |
|
AutoMutex mut(processUserNameMut); |
|
if(processUserName.size() == 0) |
|
{ |
|
processUserName = getEffectiveUserName(); |
|
} |
|
} |
|
if(processUserName == userName) |
|
{ |
return true; | return true; |
} | } |
| |
Boolean System::isPrivilegedUser(const String userName) |
Boolean isSystemUser = false; |
|
|
|
char mUserName[UNLEN+1]; |
|
char mDomainName[UNLEN+1]; |
|
char tUserName[UNLEN+1]; |
|
wchar_t wUserName[UNLEN+1]; |
|
wchar_t wDomainName[UNLEN+1]; |
|
char* pbs; |
|
bool usingDomain = false; |
|
|
|
LPBYTE pComputerName=NULL; |
|
DWORD dwLevel = 1; |
|
LPUSER_INFO_1 pUserInfo = NULL; |
|
NET_API_STATUS nStatus = NULL; |
|
|
|
// Make a copy of the specified username, it cannot be used directly because it's |
|
// declared as const and strchr() may modify the string. |
|
strncpy(tUserName, userName, sizeof(tUserName) - 1); |
|
tUserName[sizeof(tUserName)- 1] = '\0'; |
|
|
|
//separate the domain and user name if both are present. |
|
if (NULL != (pbs = strchr(tUserName, '\\'))) |
|
{ |
|
*pbs = '\0'; |
|
strcpy(mDomainName, tUserName); |
|
strcpy(mUserName, pbs+1); |
|
usingDomain = true; |
|
|
|
} else if ((NULL != (pbs = (strchr(tUserName, '@')))) || |
|
(NULL != (pbs = (strchr(tUserName, '.'))))) |
|
{ |
|
*pbs = '\0'; |
|
strcpy(mDomainName, pbs+1); |
|
strcpy(mUserName, tUserName); |
|
usingDomain = true; |
|
|
|
} else |
{ | { |
// ATTN: Implement this method to verify if user executing the current |
strcpy(mDomainName, "."); |
// command is a priviliged user, when user name is not passed as |
strcpy(mUserName, tUserName); |
// as argument. If user name is passed the function checks |
} |
// whether the given user is a priviliged user. |
|
// This is used in cimuser CLI and CIMOperationRequestAuthorizer |
//convert domain name to unicode |
return true; |
if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1)) |
|
{ |
|
return false; |
|
} |
|
|
|
//convert username to unicode |
|
if (!MultiByteToWideChar(CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName)+1)) |
|
{ |
|
return false; |
|
} |
|
|
|
if (usingDomain) |
|
{ |
|
//get domain controller |
|
DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName); |
|
if (rc == NERR_Success) |
|
{ |
|
wcscpy(wDomainName, (LPWSTR) pComputerName); //this is automatically prefixed with "\\" |
|
} |
|
/* |
|
else |
|
{ |
|
// failover |
|
// ATTN: This is commented out until there is resolution on Bugzilla 2236. -hns 2/2005 |
|
// This needs to be more thoroughly tested when we uncomment it out. |
|
|
|
PDOMAIN_CONTROLLER_INFO DomainControllerInfo = NULL; |
|
|
|
//this function does not take wide strings |
|
rc = DsGetDcName(NULL, |
|
mDomainName, |
|
NULL, |
|
NULL, |
|
DS_DIRECTORY_SERVICE_REQUIRED, //not sure what flags we want here |
|
&DomainControllerInfo); |
|
|
|
if (rc == ERROR_SUCCESS && DomainControllerInfo) |
|
{ |
|
strcpy(mDomainName, DomainControllerInfo->DomainName); |
|
NetApiBufferFree(DomainControllerInfo); |
|
|
|
if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1)) |
|
{ |
|
return false; |
|
} |
|
} |
|
} |
|
*/ |
|
} |
|
|
|
//get user info |
|
nStatus = NetUserGetInfo(wDomainName, |
|
wUserName, |
|
dwLevel, |
|
(LPBYTE *)&pUserInfo); |
|
|
|
if (nStatus == NERR_Success) |
|
{ |
|
isSystemUser = true; |
|
} |
|
|
|
if (pComputerName != NULL) |
|
{ |
|
NetApiBufferFree(pComputerName); |
|
} |
|
|
|
if (pUserInfo != NULL) |
|
{ |
|
NetApiBufferFree(pUserInfo); |
|
} |
|
|
|
return isSystemUser; |
|
} |
|
|
|
|
|
Boolean System::isPrivilegedUser(const String& userName) |
|
{ |
|
Boolean isPrivileged = false; |
|
|
|
char mUserName[UNLEN+1]; |
|
char mDomainName[UNLEN+1]; |
|
wchar_t wUserName[UNLEN+1]; |
|
wchar_t wDomainName[UNLEN+1]; |
|
char* pbs; |
|
char userStr[UNLEN+1]; |
|
bool usingDomain = false; |
|
|
|
LPBYTE pComputerName=NULL; |
|
DWORD dwLevel = 1; |
|
LPUSER_INFO_1 pUserInfo = NULL; |
|
NET_API_STATUS nStatus = NULL; |
|
|
|
//get the username in the correct format |
|
strcpy(userStr, (const char*)userName.getCString()); |
|
|
|
//separate the domain and user name if both are present. |
|
if (NULL != (pbs = strchr(userStr, '\\'))) |
|
{ |
|
*pbs = '\0'; |
|
strcpy(mDomainName, userStr); |
|
strcpy(mUserName, pbs+1); |
|
usingDomain = true; |
|
|
|
} else if ((NULL != (pbs = (strchr(userStr, '@')))) || |
|
(NULL != (pbs = (strchr(userStr, '.'))))) |
|
{ |
|
*pbs = '\0'; |
|
strcpy(mDomainName, pbs+1); |
|
strcpy(mUserName, userStr); |
|
usingDomain = true; |
|
|
|
} else |
|
{ |
|
strcpy(mDomainName, "."); |
|
strcpy(mUserName, userStr); |
|
} |
|
|
|
//convert domain name to unicode |
|
if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1)) |
|
{ |
|
return false; |
|
} |
|
|
|
//convert username to unicode |
|
if (!MultiByteToWideChar(CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName)+1)) |
|
{ |
|
return false; |
|
} |
|
|
|
if (usingDomain) |
|
{ |
|
//get domain controller |
|
DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName); |
|
if (rc == NERR_Success) |
|
{ |
|
wcscpy(wDomainName, (LPWSTR) pComputerName); //this is automatically prefixed with "\\" |
|
} |
|
/* |
|
else |
|
{ |
|
// failover |
|
// ATTN: This is commented out until there is resolution on Bugzilla 2236. -hns 2/2005 |
|
// This needs to be more thoroughly tested when we uncomment it out. |
|
|
|
PDOMAIN_CONTROLLER_INFO DomainControllerInfo = NULL; |
|
|
|
//this function does not take wide strings |
|
rc = DsGetDcName(NULL, |
|
mDomainName, |
|
NULL, |
|
NULL, |
|
DS_DIRECTORY_SERVICE_REQUIRED, //not sure what flags we want here |
|
&DomainControllerInfo); |
|
|
|
if (rc == ERROR_SUCCESS && DomainControllerInfo) |
|
{ |
|
strcpy(mDomainName, DomainControllerInfo->DomainName); |
|
NetApiBufferFree(DomainControllerInfo); |
|
|
|
if (!MultiByteToWideChar(CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName)+1)) |
|
{ |
|
return false; |
|
} |
|
} |
|
} |
|
*/ |
|
} |
|
|
|
//get privileges |
|
nStatus = NetUserGetInfo(wDomainName, |
|
wUserName, |
|
dwLevel, |
|
(LPBYTE *)&pUserInfo); |
|
|
|
if ((nStatus == NERR_Success) && |
|
(pUserInfo != NULL) && |
|
(pUserInfo->usri1_priv == USER_PRIV_ADMIN)) |
|
{ |
|
isPrivileged = true; |
|
} |
|
|
|
if (pComputerName != NULL) |
|
{ |
|
NetApiBufferFree(pComputerName); |
|
} |
|
|
|
if (pUserInfo != NULL) |
|
{ |
|
NetApiBufferFree(pUserInfo); |
|
} |
|
|
|
return isPrivileged; |
} | } |
| |
String System::getPrivilegedUserName() | String System::getPrivilegedUserName() |
|
|
return retVal; | return retVal; |
} | } |
| |
|
Boolean System::lookupUserId( |
|
const char* userName, |
|
PEGASUS_UID_T& uid, |
|
PEGASUS_GID_T& gid) |
|
{ |
|
// ATTN: Implement this method to look up the specified user |
|
return false; |
|
} |
|
|
|
Boolean System::changeUserContext( |
|
const PEGASUS_UID_T& uid, |
|
const PEGASUS_GID_T& gid) |
|
{ |
|
// ATTN: Implement this method to change the process user context to the |
|
// specified user |
|
return false; |
|
} |
|
|
Uint32 System::getPID() | Uint32 System::getPID() |
{ | { |
return _getpid(); | return _getpid(); |
|
|
const char* path, | const char* path, |
size_t newSize) | size_t newSize) |
{ | { |
int fd = open(path, O_RDWR); |
|
| |
if (fd == -1) |
Boolean rv = false; |
return false; |
int fd = open(path, O_RDWR); |
|
if (fd != -1) |
if (chsize(fd, newSize) != 0) |
{ |
return false; |
if (chsize(fd, newSize) == 0) |
|
{ |
|
rv = true; |
|
} |
| |
close(fd); | close(fd); |
return true; |
} |
|
|
|
return rv; |
} | } |
| |
// Is absolute path? | // Is absolute path? |
|
|
path_slash[_MAX_PATH-1] = '\0'; | path_slash[_MAX_PATH-1] = '\0'; |
| |
for(p = path_slash; p < path_slash + strlen(path_slash); p++) | for(p = path_slash; p < path_slash + strlen(path_slash); p++) |
if (*p == '/') *p = '\\'; |
if (*p == '/') |
|
*p = '\\'; |
| |
return (strcasecmp(_fullpath( full, path_slash, _MAX_PATH ), path_slash) == 0) ? true : false; | return (strcasecmp(_fullpath( full, path_slash, _MAX_PATH ), path_slash) == 0) ? true : false; |
} | } |
|
|
return true; | return true; |
} | } |
| |
void System::openlog(const String ident) |
Boolean System::verifyFileOwnership(const char* path) |
{ | { |
return; |
// ATTN: Implement this to check that the owner of the specified file is |
} |
// the same as the effective user for this process. |
|
return true; |
void System::syslog(Uint32 severity, const char *data) |
|
{ |
|
return; |
|
} | } |
| |
void System::closelog() |
void System::syslog(const String& ident, Uint32 severity, const char* message) |
{ | { |
return; |
// Not implemented |
} | } |
| |
// System ID constants for Logger::put and Logger::trace | // System ID constants for Logger::put and Logger::trace |