static BOOLEAN CopyLogEntryCallback( PVOID Element, PVOID Context ) { PVMAFD_SUPERLOG_ENTRY Entry = (PVMAFD_SUPERLOG_ENTRY)Element; PLOG_SELECT_CONTEXT LogContext = (PLOG_SELECT_CONTEXT)Context; PVMAFD_SUPERLOG_ENTRY row = NULL; DWORD dwError = 0; // // We're not interested in this entry, but keep enumerating. // if (LogContext->StartTime >= Entry->iStartTime) { return TRUE; } row = LogContext->DestinationBuffer; dwError = VmAfdStringCpyA((PSTR)&row->pszDCName, VMAFD_MAX_DN_LEN, (PCSTR)Entry->pszDCName); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)&row->pszDomainName, VMAFD_MAX_DN_LEN, (PCSTR)Entry->pszDomainName); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)&row->pszDCAddress, VMAFD_MAX_DN_LEN, (PCSTR)Entry->pszDCAddress); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)&row->pszSiteName, VMAFD_MAX_DN_LEN, (PCSTR)Entry->pszSiteName); BAIL_ON_VMAFD_ERROR(dwError); row->bCDCIsAlive = Entry->bCDCIsAlive; row->bHBIsAlive = Entry->bHBIsAlive; row->dwCDCLastPing = Entry->dwCDCLastPing; row->dwCDCPingTime = Entry->dwCDCPingTime; row->dwHBCount = Entry->dwHBCount; row->dwState = Entry->dwState; row->dwErrorCode = Entry->dwErrorCode; row->iStartTime = Entry->iStartTime; row->iEndTime = Entry->iEndTime; LogContext->dwCount += 1; LogContext->dwDesiredCount -= 1; LogContext->DestinationBuffer += 1; // // Stop enumerating once we've read as many entries as the user requested. // return (LogContext->dwDesiredCount != 0); error: LogContext->dwError = dwError; return FALSE; }
int main(int argc, char* argv[]) { DWORD dwError = 0; int retCode = 0; PSTR pszDomain = NULL; PSTR pszDomainName = NULL; PSTR pszUserName = NULL; PSTR pszPassword = NULL; PSTR pszPwdFile = NULL; PSTR pszSiteName = NULL; PSTR pszPartnerHostName = NULL; PSTR pszLotusServerName = NULL; PSTR pszPasswordBuf = NULL; FILE * fpPwdFile = NULL; PSTR pszPath = NULL; PSTR pszLogPathName = NULL; size_t dPwdLen = 0; BOOL bLogInitialized = FALSE; PCSTR pszErrorMsg = NULL; PSTR pszErrorDesc = NULL; PCSTR pszLocalhost = "localhost"; PSTR pszPnid = NULL; DNS_INIT_FLAG dnsInitFlag = DNS_NONE; #ifdef _WIN32 WSADATA wsaData = { 0 }; BOOLEAN bWsaStartup = FALSE; #endif #ifndef _WIN32 setlocale(LC_ALL, ""); #else dwError = WSAStartup(MAKEWORD(2, 2), &wsaData); BAIL_ON_VMAFD_ERROR(dwError); bWsaStartup = TRUE; #endif dwError = VmAfCfgInit(); BAIL_ON_VMAFD_ERROR(dwError); #ifndef _WIN32 dwError = VmAfdAllocateStringA(VMAFD_LOG_PATH, &pszLogPathName ); BAIL_ON_VMAFD_ERROR(dwError); #else dwError = VmAfdConfigGetString(VMAFD_SOFTWARE_KEY_PATH, VMAFD_LOGPATH_KEY_VALUE, &pszLogPathName); BAIL_ON_VMAFD_ERROR(dwError); #endif dwError = VmAfdAllocateStringAVsnprintf( &pszPath, "%s%s", pszLogPathName, "vdcpromo.log" ); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdLogInitialize(pszPath, 0, 0); BAIL_ON_VMAFD_ERROR(dwError); bLogInitialized = TRUE; dwError = VmAfdParseArgs(argc, argv, &pszDomain, &pszUserName, &pszPassword, &pszSiteName, &pszPartnerHostName, &pszLotusServerName, &pszPwdFile, &dnsInitFlag); if (dwError) { ShowUsage(); BAIL_ON_VMAFD_ERROR(dwError); } if (IsNullOrEmptyString(pszUserName)) { VmAfdLog(VMAFD_DEBUG_ANY, "Username parameter is not valid"); } else { VmAfdLog(VMAFD_DEBUG_ANY, "Username: \"%s\"", pszUserName); } if (!pszPwdFile && IsNullOrEmptyString(pszPassword)) { VmAfdLog(VMAFD_DEBUG_ANY, "Password parameter is not valid"); } if (IsNullOrEmptyString(pszDomain)) { VmAfdLog(VMAFD_DEBUG_ANY, "Domain parameter is not valid"); } else { VmAfdLog(VMAFD_DEBUG_ANY, "Domain: \"%s\"", pszDomain); } dwError = VmAfdAllocateMemory(VMAFD_MAX_PWD_LEN+1, (PVOID *)&pszPasswordBuf); BAIL_ON_VMAFD_ERROR(dwError); if (pszPassword == NULL && pszPwdFile != NULL) { dwError = VmAfdOpenFilePath(pszPwdFile, "rb", &fpPwdFile); if (dwError != ERROR_SUCCESS) { printf("vdcpromo: cannot open password file %s (%u)\n", pszPwdFile, dwError); dwError = ERROR_LOCAL_PASSWORDFILE_CANNOT_OPEN; BAIL_ON_VMAFD_ERROR(dwError); } if ( (dPwdLen = fread(pszPasswordBuf, 1, VMAFD_MAX_PWD_LEN, fpPwdFile)) == 0) { dwError = ERROR_LOCAL_PASSWORDFILE_CANNOT_READ; printf("vdcpromo: Could not read password file\n"); BAIL_ON_VMAFD_ERROR(dwError); } if (*pszPasswordBuf == '\0') { VmAfdLog(VMAFD_DEBUG_ANY, "password is empty"); dwError = ERROR_LOCAL_PASSWORD_EMPTY; BAIL_ON_VMAFD_ERROR(dwError); } } else if (pszPassword != NULL && pszPwdFile == NULL) { dwError = VmAfdStringCpyA(pszPasswordBuf, VMAFD_MAX_PWD_LEN, pszPassword); BAIL_ON_VMAFD_ERROR(dwError); } else //no password nor password-file, read password from stdin { VmAfdReadString("password: "******"Warning: failed to initialize DNS. Error [%d]\n", dwError); } else { fprintf(stdout, "Successfully initialized DNS.\n"); } BAIL_ON_VMAFD_ERROR(dwError); goto cleanup; } printf("Initializing Directory server instance ... \n"); fflush(stdout); dwError = VmAfdPromoteVmDirA( pszLotusServerName ? pszLotusServerName : pszLocalhost, pszDomain, pszUserName, pszPasswordBuf, pszSiteName, pszPartnerHostName); BAIL_ON_VMAFD_ERROR(dwError); printf("Directory host instance created successfully\n"); cleanup: VmAfCfgShutdown(); if (bLogInitialized) { VmAfdLogTerminate(); } #ifdef _WIN32 if (bWsaStartup != FALSE) { WSACleanup(); } #endif VMAFD_SAFE_FREE_MEMORY(pszPnid); VMAFD_SAFE_FREE_MEMORY(pszPasswordBuf); VMAFD_SAFE_FREE_MEMORY(pszPath); VMAFD_SAFE_FREE_MEMORY(pszLogPathName); VMAFD_SAFE_FREE_MEMORY(pszErrorDesc); VMAFD_SAFE_FREE_MEMORY(pszDomainName); return retCode; error: switch (dwError) { case ERROR_LOCAL_OPTION_UNKNOWN: retCode = 2; pszErrorMsg = "An unknown option was present on the command line."; break; case ERROR_LOCAL_OPTION_INVALID: retCode = 3; pszErrorMsg = "The options present on the command line are not valid."; break; case ERROR_LOCAL_PASSWORDFILE_CANNOT_OPEN: retCode = 4; pszErrorMsg = "Could not open password file.\nVerify the path is correct."; break; case ERROR_LOCAL_PASSWORDFILE_CANNOT_READ: retCode = 5; pszErrorMsg = "Problem reading password file.\nVerify contents of password file."; break; case ERROR_LOCAL_PASSWORD_EMPTY: retCode = 6; pszErrorMsg = "Invalid password; password cannot be empty."; break; case ERROR_CANNOT_CONNECT_VMAFD: retCode = 20; pszErrorMsg = "Could not connect to the local service VMware AFD.\nVerify VMware AFD is running."; break; case VMDIR_ERROR_CANNOT_CONNECT_VMDIR: retCode = 21; pszErrorMsg = "Could not connect to the local service VMware Directory Service.\nVerify VMware Directory Service is running."; break; case ERROR_INVALID_CONFIGURATION: retCode = 22; pszErrorMsg = "Configuration is not correct.\nFirst boot scripts need to be executed."; break; case VMDIR_ERROR_SERVER_DOWN: retCode = 23; pszErrorMsg = "Could not connect to VMware Directory Service via LDAP.\nVerify VMware Directory Service is running on the appropriate system and is reachable from this host."; break; case VMDIR_ERROR_USER_INVALID_CREDENTIAL: retCode = 24; pszErrorMsg = "Authentication to VMware Directory Service failed.\nVerify the username and password."; break; case ERROR_ACCESS_DENIED: retCode = 25; pszErrorMsg = "Authorization failed.\nVerify account has proper administrative privileges."; break; case VMDIR_ERROR_SCHEMA_NOT_COMPATIBLE: retCode = 26; pszErrorMsg = "Could not join to the remote service VMWare Directory Service.\nThe remote schema is incompatible with the local schema."; break; default: retCode = 1; } if (bLogInitialized) { if (pszErrorMsg == NULL) { VmAfdLog(VMAFD_DEBUG_ANY, "Vdcpromo failed. Error[%d]", dwError); } else { VmAfdLog(VMAFD_DEBUG_ANY, "Vdcpromo failed. Error[%d]\n%s", dwError, pszErrorMsg); } } if (pszErrorMsg == NULL) { if (!VmAfdGetErrorString(dwError, &pszErrorDesc)) { printf("Vdcpromo failed. Error %d: %s\n", dwError, pszErrorDesc); } else { printf("Vdcpromo failed. Error %d.\n", dwError); } } else { printf("Vdcpromo failed. Error[%d]\n%s\n", dwError, pszErrorMsg); } goto cleanup; }
/** * To add log entry for PCDC_DC_INFO_W **/ DWORD VmAfdAddCDCSuperLogEntry( PVMSUPERLOGGING pLogger, UINT64 iStartTime, UINT64 iEndTime, PCDC_DC_INFO_W pDCEntry, CDC_DC_STATE dwState, DWORD dwErrorCode ) { PVMAFD_SUPERLOG_ENTRY pLogEntry = NULL; DWORD dwError = 0; PSTR pszDCName = NULL; PSTR pszDomainName = NULL; PSTR pszSiteName = NULL; PSTR pszDCAddress = NULL; // AFD should not fail if super logging is disabled. This is why returning 0 here if (!VmAfdIsSuperLoggingEnabled(pLogger)) { dwError = ERROR_NOT_READY; BAIL_ON_VMAFD_ERROR_NO_LOG(dwError); } if(!pDCEntry) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMAFD_ERROR(dwError); } pLogEntry = (PVMAFD_SUPERLOG_ENTRY)VmAfdCircularBufferGetNextEntry(pLogger->pCircularBuffer); if(!pLogEntry) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMAFD_ERROR(dwError); } memset(pLogEntry, 0, sizeof(*pLogEntry)); pLogEntry->iStartTime = iStartTime; pLogEntry->iEndTime = iEndTime; pLogEntry->dwErrorCode = dwErrorCode; pLogEntry->dwState = dwState; if(pDCEntry && pDCEntry->pszDCName) { dwError = VmAfdAllocateStringAFromW(pDCEntry->pszDCName, &pszDCName); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)pLogEntry->pszDCName, VMAFD_MAX_DN_LEN, pszDCName); BAIL_ON_VMAFD_ERROR(dwError); } if(pDCEntry && pDCEntry->pszDomainName) { dwError = VmAfdAllocateStringAFromW(pDCEntry->pszDomainName, &pszDomainName); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)pLogEntry->pszDomainName, VMAFD_MAX_DN_LEN, pszDomainName); BAIL_ON_VMAFD_ERROR(dwError); } if(pDCEntry && pDCEntry->pszDcSiteName) { dwError = VmAfdAllocateStringAFromW(pDCEntry->pszDcSiteName, &pszSiteName); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)pLogEntry->pszSiteName, VMAFD_MAX_DN_LEN, pszSiteName); BAIL_ON_VMAFD_ERROR(dwError); } if(pDCEntry && pDCEntry->pszDCAddress) { dwError = VmAfdAllocateStringAFromW(pDCEntry->pszDCAddress, &pszDCAddress); BAIL_ON_VMAFD_ERROR(dwError); dwError = VmAfdStringCpyA((PSTR)pLogEntry->pszDCAddress, VMAFD_MAX_DN_LEN, pszDCAddress); BAIL_ON_VMAFD_ERROR(dwError); } cleanup: VMAFD_SAFE_FREE_STRINGA(pszDCName); VMAFD_SAFE_FREE_STRINGA(pszDomainName); VMAFD_SAFE_FREE_STRINGA(pszSiteName); VMAFD_SAFE_FREE_STRINGA(pszDCAddress); return dwError; error: goto cleanup; }