version 1.71.4.2, 2008/02/13 08:30:11
|
version 1.79, 2007/09/11 17:56:33
|
|
|
#include <windows.h> | #include <windows.h> |
#include <process.h> | #include <process.h> |
#include <lm.h> | #include <lm.h> |
|
#include <Pegasus/Common/Tracer.h> |
| |
#define SECURITY_WIN32 | #define SECURITY_WIN32 |
#include <security.h> | #include <security.h> |
|
|
| |
String System::getHostName() | String System::getHostName() |
{ | { |
static char hostname[PEGASUS_MAXHOSTNAMELEN + 1]; |
static String _hostname; |
|
if (0 == _hostname.size()) |
if (!*hostname) |
|
{ | { |
|
char hostname[PEGASUS_MAXHOSTNAMELEN + 1]; |
|
//initialize the buffer to handle the case where gethostname fails. |
|
hostname[0] = 0; |
gethostname(hostname, sizeof(hostname)); | gethostname(hostname, sizeof(hostname)); |
} |
|
hostname[sizeof(hostname)-1] = 0; | hostname[sizeof(hostname)-1] = 0; |
|
_hostname.assign(hostname); |
return hostname; |
} |
|
return _hostname; |
} | } |
| |
String System::getFullyQualifiedHostName () | String System::getFullyQualifiedHostName () |
|
|
String hostname = getHostName(); | String hostname = getHostName(); |
struct hostent* hostEnt; | struct hostent* hostEnt; |
| |
hostEnt = gethostbyname((const char *)hostname.getCString()); |
hostEnt = getHostByName((const char *)hostname.getCString()); |
if (hostEnt == NULL) | if (hostEnt == NULL) |
{ | { |
return String::EMPTY; | return String::EMPTY; |
|
|
if (wcscmp(computerName, userDomain) != 0) | if (wcscmp(computerName, userDomain) != 0) |
{ | { |
//userId.append(userDomain); | //userId.append(userDomain); |
Uint32 n = wcslen(userDomain); |
Uint32 n = (Uint32)wcslen(userDomain); |
for (unsigned long i = 0; i < n; i++) | for (unsigned long i = 0; i < n; i++) |
{ | { |
userId.append(Char16(userDomain[i])); | userId.append(Char16(userDomain[i])); |
} | } |
userId.append("\\"); | userId.append("\\"); |
//userId.append(userName); | //userId.append(userName); |
n = wcslen(userName); |
n = (Uint32)wcslen(userName); |
for (unsigned long i = 0; i < n; i++) | for (unsigned long i = 0; i < n; i++) |
{ | { |
userId.append(Char16(userName[i])); | userId.append(Char16(userName[i])); |
|
|
else | else |
{ | { |
//userId.append(userName); | //userId.append(userName); |
Uint32 n = wcslen(userName); |
Uint32 n = (Uint32)wcslen(userName); |
for (unsigned long i = 0; i < n; i++) | for (unsigned long i = 0; i < n; i++) |
{ | { |
userId.append(Char16(userName[i])); | userId.append(Char16(userName[i])); |
|
|
char pcSalt[3] = {0}; | char pcSalt[3] = {0}; |
| |
strncpy(pcSalt, salt, 2); | strncpy(pcSalt, salt, 2); |
dwByteCount = strlen(password); |
dwByteCount = (DWORD)strlen(password); |
memcpy(pbBuffer, password, dwByteCount); | memcpy(pbBuffer, password, dwByteCount); |
for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++) | for (DWORD i=0; (i<dwByteCount) || (i>=PW_BUFF_LEN); i++) |
(i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0]; | (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0]; |
|
|
| |
//convert domain name to unicode | //convert domain name to unicode |
if (!MultiByteToWideChar( | if (!MultiByteToWideChar( |
CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName) + 1)) |
CP_ACP, 0, mDomainName, -1, wDomainName, |
|
(int)(strlen(mDomainName) + 1))) |
{ | { |
return false; | return false; |
} | } |
| |
//convert username to unicode | //convert username to unicode |
if (!MultiByteToWideChar( | if (!MultiByteToWideChar( |
CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName) + 1)) |
CP_ACP, 0, mUserName, -1, wUserName, (int)(strlen(mUserName) + 1))) |
{ | { |
return false; | return false; |
} | } |
|
|
| |
//convert domain name to unicode | //convert domain name to unicode |
if (!MultiByteToWideChar( | if (!MultiByteToWideChar( |
CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName) + 1)) |
CP_ACP, 0, mDomainName, -1, wDomainName, |
|
(int)(strlen(mDomainName) + 1))) |
{ | { |
return false; | return false; |
} | } |
| |
//convert username to unicode | //convert username to unicode |
if (!MultiByteToWideChar( | if (!MultiByteToWideChar( |
CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName) + 1)) |
CP_ACP, 0, mUserName, -1, wUserName, (int)(strlen(mUserName) + 1))) |
{ | { |
return false; | return false; |
} | } |
|
|
int fd = open(path, O_RDWR); | int fd = open(path, O_RDWR); |
if (fd != -1) | if (fd != -1) |
{ | { |
if (chsize(fd, newSize) == 0) |
if (chsize(fd, (long)newSize) == 0) |
{ | { |
rv = true; | rv = true; |
} | } |
|
|
return false; | return false; |
} | } |
| |
String System::getErrorMSG_NLS(int errorCode,int errorCode2) |
|
{ |
|
LPVOID winErrorMsg = NULL; |
|
|
|
if (FormatMessage( |
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
FORMAT_MESSAGE_FROM_SYSTEM | |
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
NULL, |
|
errorCode, |
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
|
(LPTSTR)&winErrorMsg, |
|
0, |
|
NULL)) |
|
{ |
|
MessageLoaderParms parms( |
|
"Common.System.ERROR_MESSAGE.STANDARD", |
|
"$0 (error code $1)",(char*)winErrorMsg,errorCode); |
|
LocalFree(winErrorMsg); |
|
return MessageLoader::getMessage(parms); |
|
} |
|
|
|
MessageLoaderParms parms( |
|
"Common.System.ERROR_MESSAGE.STANDARD", |
|
"$0 (error code $1)","",errorCode); |
|
return MessageLoader::getMessage(parms); |
|
|
|
} |
|
|
|
String System::getErrorMSG(int errorCode,int errorCode2) |
|
{ |
|
|
|
String buffer; |
|
LPVOID winErrorMsg = NULL; |
|
|
|
char strErrorCode[32]; |
|
sprintf(strErrorCode, "%d", errorCode); |
|
|
|
if (FormatMessage( |
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
FORMAT_MESSAGE_FROM_SYSTEM | |
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
NULL, |
|
errorCode, |
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
|
(LPTSTR)&winErrorMsg, |
|
0, |
|
NULL)) |
|
{ |
|
buffer.append((char*)winErrorMsg); |
|
LocalFree(winErrorMsg); |
|
} |
|
| |
buffer.append(" (error code "); |
|
buffer.append(strErrorCode); |
|
buffer.append(")"); |
|
|
|
return buffer; |
|
} |
|
| |
/////////////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////////////// |
// AutoFileLock class | // AutoFileLock class |
|
|
| |
AutoFileLock::AutoFileLock(const char* fileName) | AutoFileLock::AutoFileLock(const char* fileName) |
{ | { |
// ATTN: Not implemented |
// Repeat createFile, if there is a sharing violation. |
|
do |
|
{ |
|
_hFile = CreateFile (fileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
|
}while ((GetLastError() == ERROR_SHARING_VIOLATION)); |
|
|
|
// If this conditon succeeds, There is an error opening the file. Hence |
|
// returning from here as Lock can not be acquired. |
|
if((_hFile == INVALID_HANDLE_VALUE) |
|
&& (GetLastError() != ERROR_ALREADY_EXISTS) |
|
&& (GetLastError() != ERROR_SUCCESS)) |
|
{ |
|
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL2, |
|
"AutoFileLock: Failed to open lock file '%s', error code %d.", |
|
fileName, GetLastError())); |
|
return; |
|
} |
|
|
|
OVERLAPPED l={0,0,0,0,0}; |
|
if(LockFileEx(_hFile,LOCKFILE_EXCLUSIVE_LOCK, 0, 0, 0, &l) == 0) |
|
{ |
|
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL2, |
|
"AutoFileLock: Failed to Acquire lock on file %s, error code %d.", |
|
fileName, GetLastError())); |
|
CloseHandle(_hFile); |
|
_hFile = INVALID_HANDLE_VALUE; |
|
} |
} | } |
| |
AutoFileLock::~AutoFileLock() | AutoFileLock::~AutoFileLock() |
{ | { |
// ATTN: Not implemented |
if(_hFile != INVALID_HANDLE_VALUE) |
|
{ |
|
OVERLAPPED l={0,0,0,0,0}; |
|
if(UnlockFileEx (_hFile, 0, 0, 0, &l) == 0) |
|
{ |
|
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL2, |
|
"AutoFileLock: Failed to unlock file, error code %d.", |
|
GetLastError())); |
|
} |
|
CloseHandle(_hFile); |
|
} |
} | } |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |