DWORD DJCopyLine( PHOSTSFILELINE pSrc, PHOSTSFILELINE *ppDest) { PHOSTSFILELINE ret = NULL; PHOSTFILEALIAS srcAlias = NULL, *destAlias = NULL; DWORD ceError = ERROR_SUCCESS; ceError = CTAllocateMemory(sizeof(HOSTSFILELINE), (PVOID*)(PVOID)&ret); BAIL_ON_CENTERIS_ERROR(ceError); ret->bModified = pSrc->bModified; ceError = CTDupOrNullStr(pSrc->pszComment, &ret->pszComment); BAIL_ON_CENTERIS_ERROR(ceError); if(pSrc->pEntry != NULL) { ceError = CTAllocateMemory(sizeof(HOSTSFILEENTRY), (PVOID*)(PVOID)&ret->pEntry); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTDupOrNullStr(pSrc->pEntry->pszIpAddress, &ret->pEntry->pszIpAddress); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTDupOrNullStr(pSrc->pEntry->pszCanonicalName, &ret->pEntry->pszCanonicalName); BAIL_ON_CENTERIS_ERROR(ceError); srcAlias = pSrc->pEntry->pAliasList; destAlias = &ret->pEntry->pAliasList; } while(srcAlias != NULL) { ceError = CTAllocateMemory(sizeof(HOSTFILEALIAS), (PVOID*)(PVOID)destAlias); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTDupOrNullStr(srcAlias->pszAlias, &(*destAlias)->pszAlias); BAIL_ON_CENTERIS_ERROR(ceError); srcAlias = srcAlias->pNext; destAlias = &(*destAlias)->pNext; } *ppDest = ret; ret = NULL; error: if(ret != NULL) DJFreeHostsFileLineList(ret); return ceError; }
DWORD DJIsAppleADPluginInUse(BOOLEAN* pExists) { DWORD ceError = ERROR_SUCCESS; PPROCINFO pProcInfo = NULL; PSTR* ppszArgs = NULL; DWORD nArgs = 7; LONG status = 0; BOOLEAN bInUse = FALSE; DJ_LOG_INFO("Testing to see if Apple AD plugin is already in use"); ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, (PVOID*)(PVOID)&ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/usr/bin/dscl", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("localhost", ppszArgs+1); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-list", ppszArgs+2); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("|", ppszArgs+3); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("grep", ppszArgs+4); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString(APPLEADDSPLUGIN_NAME, ppszArgs+5); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status == 0) { bInUse = TRUE; } error: if (ppszArgs) { CTFreeStringArray(ppszArgs, nArgs); } if (pProcInfo) { FreeProcInfo(pProcInfo); } *pExists = bInUse; return ceError; }
DWORD CTAllocateString( PCSTR pszInputString, PSTR * ppszOutputString ) { DWORD ceError = ERROR_SUCCESS; size_t len = 0; PSTR pszOutputString = NULL; if (!pszInputString || !ppszOutputString){ ceError = ERROR_INVALID_PARAMETER; BAIL_ON_CENTERIS_ERROR(ceError); } len = strlen(pszInputString); ceError = CTAllocateMemory(len+1, (PVOID *)&pszOutputString); BAIL_ON_CENTERIS_ERROR(ceError); memcpy(pszOutputString, pszInputString, len); pszOutputString[len] = 0; error: *ppszOutputString = pszOutputString; return(ceError); }
DWORD CTStrndup( PCSTR pszInputString, size_t size2, PSTR * ppszOutputString ) { DWORD ceError = ERROR_SUCCESS; size_t copylen = 0; PSTR pszOutputString = NULL; if (!pszInputString || !ppszOutputString){ ceError = ERROR_INVALID_PARAMETER; BAIL_ON_CENTERIS_ERROR(ceError); } copylen = strlen(pszInputString); if(copylen > size2) copylen = size2; ceError = CTAllocateMemory(copylen+1, (PVOID *)&pszOutputString); BAIL_ON_CENTERIS_ERROR(ceError); memcpy(pszOutputString, pszInputString, copylen); pszOutputString[copylen] = 0; error: *ppszOutputString = pszOutputString; return(ceError); }
static DWORD SetAIXHostname( PSTR pszComputerName ) { DWORD ceError = ERROR_SUCCESS; PPROCINFO pProcInfo = NULL; PSTR* ppszArgs = NULL; DWORD nArgs = 6; CHAR szBuf[512]; LONG status = 0; DJ_LOG_INFO("Setting hostname to [%s]", pszComputerName); ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, PPCAST(&ppszArgs)); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("chdev", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-a", ppszArgs+1); BAIL_ON_CENTERIS_ERROR(ceError); sprintf(szBuf, "hostname=%s", pszComputerName); ceError = CTAllocateString(szBuf, ppszArgs+2); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-l", ppszArgs+3); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("inet0", ppszArgs+4); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status != 0) { ceError = ERROR_BAD_COMMAND; BAIL_ON_CENTERIS_ERROR(ceError); } error: if (ppszArgs) CTFreeStringArray(ppszArgs, nArgs); if (pProcInfo) FreeProcInfo(pProcInfo); return ceError; }
DWORD CTArrayConstruct(DynamicArray* array, size_t itemSize) { DWORD ceError = ERROR_SUCCESS; array->size = 0; array->capacity = 32; BAIL_ON_CENTERIS_ERROR(ceError = CTAllocateMemory(array->capacity*itemSize, (PVOID*)&array->data)); error: return ceError; }
BOOLEAN CTParseInt(PCSTR str, int *val) { char *temp; BOOLEAN rc = TRUE; errno = 0; long lVal = 0; PSTR sanitizedStr = NULL; int iStrLen = 0; if(str == NULL) { return FALSE; } //strip precdeding '0' characters iStrLen = strlen(str); CTAllocateMemory((iStrLen +1 )* sizeof(char), (PVOID*)&sanitizedStr); int i = 0; for(i = 0; i < iStrLen; i++) { if(str[i] != '0') { break; } } strncpy(sanitizedStr, str + i, iStrLen); lVal = strtol(sanitizedStr, &temp, 0); if (temp == sanitizedStr || *temp != '\0' || ((lVal == LONG_MIN || lVal == LONG_MAX) && errno == ERANGE) || lVal < INT_MIN || lVal > INT_MAX) { rc = FALSE; *val = 0; } else { *val = lVal; } CTFreeMemory(sanitizedStr); return rc; }
DWORD CTCopyToken(const CTParseToken *source, CTParseToken **dest) { DWORD ceError = ERROR_SUCCESS; *dest = NULL; if(source != NULL) { BAIL_ON_CENTERIS_ERROR(ceError = CTAllocateMemory(sizeof(CTParseToken), (void **) dest)); BAIL_ON_CENTERIS_ERROR(ceError = CTCopyTokenContents(*dest, source)); } error: if (ceError) { CTFreeParseToken(dest); } return ceError; }
//Adds an alias to the head of the alias list static DWORD DJAddAlias( PHOSTSFILELINE pLine, PCSTR pszName, BOOLEAN alwaysAdd, PBOOLEAN pbAdded ) { DWORD ceError = ERROR_SUCCESS; PHOSTFILEALIAS pAlias = NULL; if (alwaysAdd || !DJEntryHasAlias(pLine->pEntry->pAliasList, pszName)) { ceError = CTAllocateMemory(sizeof(HOSTFILEALIAS), (PVOID*)(PVOID)&pAlias); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString(pszName, &pAlias->pszAlias); BAIL_ON_CENTERIS_ERROR(ceError); pAlias->pNext = pLine->pEntry->pAliasList; pLine->pEntry->pAliasList = pAlias; pAlias = NULL; if (pbAdded != NULL) *pbAdded = TRUE; pLine->bModified = TRUE; } else if (pbAdded != NULL) { *pbAdded = FALSE; } error: if (pAlias) DJFreeAlias(pAlias); return ceError; }
/* Use dscl to place the DSPlugin in the authenticator list */ static DWORD DJRegisterDSPlugin() { DWORD ceError = ERROR_SUCCESS; PPROCINFO pProcInfo = NULL; PSTR* ppszArgs = NULL; DWORD nArgs = 7; LONG status = 0; DWORD retryCount = 3; DJ_LOG_INFO("Registering DSPlugin for Macintosh Directory Services Authentication"); ceError = DJSetSearchPath(CSPSearchPath); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, (PVOID*)(PVOID)&ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/usr/bin/dscl", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/Search", ppszArgs+1); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-append", ppszArgs+2); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/", ppszArgs+3); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("CSPSearchPath", ppszArgs+4); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString(LWDSPLUGIN_NAME, ppszArgs+5); BAIL_ON_CENTERIS_ERROR(ceError); while (retryCount) { ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status == 0) { goto error; } if (pProcInfo) { FreeProcInfo(pProcInfo); pProcInfo = NULL; } retryCount--; sleep(5); // Set last error ceError = ERROR_REGISTRY_IO_FAILED; } error: if (ppszArgs) { CTFreeStringArray(ppszArgs, nArgs); } if (pProcInfo) { FreeProcInfo(pProcInfo); } return ceError; }
/* Remove DSPlugin from the authenticator list */ static DWORD DJUnregisterDSPlugin() { DWORD ceError = ERROR_SUCCESS; PPROCINFO pProcInfo = NULL; PSTR* ppszArgs = NULL; DWORD nArgs = 7; LONG status = 0; DJ_LOG_INFO("Unregistering DSPlugin from Open Directory Authentication"); ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, (PVOID*)(PVOID)&ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/usr/bin/dscl", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/Search", ppszArgs+1); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-delete", ppszArgs+2); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/", ppszArgs+3); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("CSPSearchPath", ppszArgs+4); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString(LWDSPLUGIN_NAME, ppszArgs+5); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status != 0) { ceError = ERROR_REGISTRY_IO_FAILED; BAIL_ON_CENTERIS_ERROR(ceError); } ceError = DJSetSearchPath(NSPSearchPath); BAIL_ON_CENTERIS_ERROR(ceError); error: if (ppszArgs) { CTFreeStringArray(ppszArgs, nArgs); } if (pProcInfo) { FreeProcInfo(pProcInfo); } return ceError; }
DWORD CTAllocateStringPrintfV( PSTR* result, PCSTR format, va_list args ) { DWORD ceError = ERROR_SUCCESS; char *smallBuffer; unsigned int bufsize; int requiredLength; unsigned int newRequiredLength; PSTR outputString = NULL; va_list args2; va_copy(args2, args); bufsize = 4; /* Use a small buffer in case libc does not like NULL */ do { ceError = CTAllocateMemory(bufsize, (PVOID*) &smallBuffer); CLEANUP_ON_DWORD(ceError); requiredLength = vsnprintf(smallBuffer, bufsize, format, args); if (requiredLength < 0) { bufsize *= 2; } CTFreeMemory(smallBuffer); } while (requiredLength < 0); if (requiredLength >= (UINT32_MAX - 1)) { ceError = ERROR_OUTOFMEMORY; CLEANUP_ON_DWORD(ceError); } ceError = CTAllocateMemory(requiredLength + 2, (PVOID*)&outputString); CLEANUP_ON_DWORD(ceError); newRequiredLength = vsnprintf(outputString, requiredLength + 1, format, args2); if (newRequiredLength < 0) { ceError = LwMapErrnoToLwError(errno); CLEANUP_ON_DWORD(ceError); } else if (newRequiredLength > requiredLength) { /* unexpected, ideally should log something, or use better error code */ ceError = ERROR_OUTOFMEMORY; CLEANUP_ON_DWORD(ceError); } else if (newRequiredLength < requiredLength) { /* unexpected, ideally should log something -- do not need an error, though */ } cleanup: va_end(args2); if (ceError) { if (outputString) { CTFreeMemory(outputString); outputString = NULL; } } *result = outputString; return ceError; }
DWORD CTEscapeString( PCSTR pszOrig, PSTR * ppszEscapedString ) { DWORD ceError = ERROR_SUCCESS; int nQuotes = 0; PCSTR pszTmp = pszOrig; PSTR pszNew = NULL; PSTR pszNewTmp = NULL; if ( !ppszEscapedString || !pszOrig ) { ceError = ERROR_INVALID_PARAMETER; BAIL_ON_CENTERIS_ERROR(ceError); } while(pszTmp && *pszTmp) { if (*pszTmp=='\'') { nQuotes++; } pszTmp++; } if (!nQuotes) { ceError = CTAllocateString(pszOrig, &pszNew); BAIL_ON_CENTERIS_ERROR(ceError); } else { /* * We are going to escape each single quote and enclose it in two other * single-quotes */ ceError = CTAllocateMemory( strlen(pszOrig)+3*nQuotes+1, (PVOID*)&pszNew ); BAIL_ON_CENTERIS_ERROR(ceError); pszTmp = pszOrig; pszNewTmp = pszNew; while(pszTmp && *pszTmp) { if (*pszTmp=='\'') { *pszNewTmp++='\''; *pszNewTmp++='\\'; *pszNewTmp++='\''; *pszNewTmp++='\''; pszTmp++; } else { *pszNewTmp++ = *pszTmp++; } } *pszNewTmp = '\0'; } *ppszEscapedString = pszNew; pszNew = NULL; error: CT_SAFE_FREE_STRING(pszNew); return ceError; }
static DWORD DJSetSearchPath( SearchPolicyType searchPolicyType ) { DWORD ceError = ERROR_SUCCESS; PPROCINFO pProcInfo = NULL; PSTR* ppszArgs = NULL; DWORD nArgs = 7; LONG status = 0; DJ_LOG_INFO("Setting search policy to %s", searchPolicyType == CSPSearchPath ? "Custom path" : "Automatic"); ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, (PVOID*)(PVOID)&ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/usr/bin/dscl", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/Search", ppszArgs+1); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-create", ppszArgs+2); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/", ppszArgs+3); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("SearchPolicy", ppszArgs+4); BAIL_ON_CENTERIS_ERROR(ceError); switch (searchPolicyType) { case CSPSearchPath: { ceError = CTAllocateString("CSPSearchPath", ppszArgs+5); BAIL_ON_CENTERIS_ERROR(ceError); break; } case NSPSearchPath: { ceError = CTAllocateString("NSPSearchPath", ppszArgs+5); BAIL_ON_CENTERIS_ERROR(ceError); break; } } ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status != 0) { ceError = ERROR_BAD_COMMAND; BAIL_ON_CENTERIS_ERROR(ceError); } error: if (ppszArgs) { CTFreeStringArray(ppszArgs, nArgs); } if (pProcInfo) { FreeProcInfo(pProcInfo); } return ceError; }
static DWORD SetHPUXHostname(PSTR pszComputerName) { DWORD ceError = ERROR_SUCCESS; PPROCINFO pProcInfo = NULL; PSTR *ppszArgs = NULL; DWORD nArgs = 6; CHAR szBuf[512]; LONG status = 0; DJ_LOG_INFO("Setting hostname to [%s]", pszComputerName); ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, PPCAST(&ppszArgs)); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/bin/sh", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("-c", ppszArgs+1); BAIL_ON_CENTERIS_ERROR(ceError); memset(szBuf, 0, sizeof(szBuf)); snprintf(szBuf, sizeof(szBuf), "/usr/bin/sed s/HOSTNAME=\\\"[a-zA-Z0-9].*\\\"/HOSTNAME=\\\"%s\\\"/ %s > %s.new", pszComputerName, NETCONF, NETCONF); ceError = CTAllocateString(szBuf, ppszArgs+2); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status != 0) { ceError = ERROR_BAD_COMMAND; BAIL_ON_CENTERIS_ERROR(ceError); } memset(szBuf, 0, sizeof(szBuf)); snprintf(szBuf, sizeof(szBuf), "%s.new", NETCONF); ceError = CTMoveFile(szBuf, NETCONF); BAIL_ON_CENTERIS_ERROR(ceError); CTFreeStringArray(ppszArgs, nArgs); ppszArgs = NULL; FreeProcInfo(pProcInfo); pProcInfo = NULL; /* After updating the file, HP-UX wants us to "start" the hostname */ ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, PPCAST(&ppszArgs)); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("/sbin/init.d/hostname", ppszArgs); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString("start", ppszArgs + 1); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJGetProcessStatus(pProcInfo, &status); BAIL_ON_CENTERIS_ERROR(ceError); if (status != 0) { ceError = ERROR_BAD_COMMAND; BAIL_ON_CENTERIS_ERROR(ceError); } error: if(ppszArgs) CTFreeStringArray(ppszArgs, nArgs); if(pProcInfo) FreeProcInfo(pProcInfo); return ceError; }
DWORD CTCopyFileWithPerms( PCSTR pszSrcPath, PCSTR pszDstPath, mode_t dwPerms ) { DWORD ceError = ERROR_SUCCESS; PCSTR pszTmpSuffix = ".tmp_likewise"; PSTR pszTmpPath = NULL; BOOLEAN bRemoveFile = FALSE; CHAR szBuf[1024+1]; int iFd = -1; int oFd = -1; ssize_t bytesRead = 0; if (IsNullOrEmptyString(pszSrcPath) || IsNullOrEmptyString(pszDstPath)) { ceError = ERROR_INVALID_PARAMETER; BAIL_ON_CENTERIS_ERROR(ceError); } ceError = CTAllocateMemory(strlen(pszDstPath)+strlen(pszTmpSuffix)+2, (PVOID*)&pszTmpPath); BAIL_ON_CENTERIS_ERROR(ceError); strcpy(pszTmpPath, pszDstPath); strcat(pszTmpPath, pszTmpSuffix); if ((iFd = open(pszSrcPath, O_RDONLY, S_IRUSR)) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } if ((oFd = open(pszTmpPath, O_WRONLY|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } bRemoveFile = TRUE; while (1) { if ((bytesRead = read(iFd, szBuf, 1024)) < 0) { if (errno == EINTR) continue; ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } if (bytesRead == 0) break; if (write(oFd, szBuf, bytesRead) != bytesRead) { if (errno == EINTR) continue; ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } close(iFd); iFd = -1; close(oFd); oFd = -1; ceError = CTMoveFile(pszTmpPath, pszDstPath); BAIL_ON_CENTERIS_ERROR(ceError); bRemoveFile = FALSE; ceError = CTChangePermissions(pszDstPath, dwPerms); BAIL_ON_CENTERIS_ERROR(ceError); error: if (iFd >= 0) close(iFd); if (oFd >= 0) close(oFd); if (bRemoveFile) { CTRemoveFile(pszTmpPath); } if (pszTmpPath) CTFreeString(pszTmpPath); return ceError; }
// newFdqnHostname = <shortHostname>.<dnsDomainName> DWORD DJReplaceHostnameInMemory( PHOSTSFILELINE pHostsFileLineList, PCSTR oldShortHostname, PCSTR oldFqdnHostname, PCSTR shortHostname, PCSTR dnsDomainName ) { DWORD ceError = ERROR_SUCCESS; PSTR pszDomainName = NULL; PSTR pszHostName = NULL; PSTR pszCanonicalName = NULL; PHOSTSFILELINE pLine = NULL; PHOSTSFILELINE pCreatedLine = NULL; BOOLEAN bFound = FALSE; BOOLEAN bModified = FALSE; PHOSTFILEALIAS pAlias = NULL; // // Ideal algorithm: // // 1) Find any lines with hostname. // 2) Make sure the the FQDN is present as the first // name in each of those lines. // 3) If no lines were found, then add hostname to 127.0.0.1 // and put FQDN first. // 4) If 127.0.0.2 line is present, edit that to just have our info. // if (IsNullOrEmptyString(shortHostname)) { ceError = ERROR_INVALID_PARAMETER; BAIL_ON_CENTERIS_ERROR(ceError); } if (strchr(shortHostname, '.')) { ceError = ERROR_INVALID_COMPUTERNAME; BAIL_ON_CENTERIS_ERROR(ceError); } ceError = CTAllocateString(shortHostname, &pszHostName); BAIL_ON_CENTERIS_ERROR(ceError); CTStripWhitespace(pszHostName); CTStrToLower(pszHostName); if (dnsDomainName != NULL) { ceError = CTAllocateString(dnsDomainName, &pszDomainName); BAIL_ON_CENTERIS_ERROR(ceError); CTStripWhitespace(pszDomainName); CTStrToLower(pszDomainName); ceError = CTAllocateMemory(strlen(pszHostName)+strlen(pszDomainName)+2, (PVOID*)(PVOID)&pszCanonicalName); BAIL_ON_CENTERIS_ERROR(ceError); sprintf(pszCanonicalName, "%s.%s", pszHostName, pszDomainName); } else { ceError = CTAllocateString(pszHostName, &pszCanonicalName); BAIL_ON_CENTERIS_ERROR(ceError); } for (pLine = pHostsFileLineList; pLine; pLine = pLine->pNext) { if (pLine->pEntry != NULL) { if (pLine->pEntry->pszCanonicalName != NULL && (!strcasecmp(pLine->pEntry->pszCanonicalName, pszHostName) || !strcasecmp(pLine->pEntry->pszCanonicalName, oldShortHostname ? oldShortHostname : "") || !strcasecmp(pLine->pEntry->pszCanonicalName, oldFqdnHostname ? oldFqdnHostname : "") || !strcasecmp(pLine->pEntry->pszCanonicalName, pszCanonicalName))) { ceError = DJUpdateHostEntry( pLine, pszHostName, pszCanonicalName, oldShortHostname, oldFqdnHostname); BAIL_ON_CENTERIS_ERROR(ceError); bFound = TRUE; } else if (DJEntryHasAlias(pLine->pEntry->pAliasList, pszHostName)) { bFound = TRUE; ceError = DJUpdateHostEntry( pLine, pszHostName, pszCanonicalName, oldShortHostname, oldFqdnHostname); BAIL_ON_CENTERIS_ERROR(ceError); } } } if (!bFound) { //First try to setup ip address on the loop back device which are not //127.0.0.1 for (pLine = pHostsFileLineList; pLine; pLine = pLine->pNext) { if (pLine->pEntry != NULL && !strncmp(pLine->pEntry->pszIpAddress, "127.0.", strlen("127.0.")) && strcmp(pLine->pEntry->pszIpAddress, "127.0.0.1")) { ceError = DJUpdateHostEntry( pLine, pszHostName, pszCanonicalName, oldShortHostname, oldFqdnHostname); BAIL_ON_CENTERIS_ERROR(ceError); bFound = TRUE; } } if (!bFound) { //Have to add it to the 127.0.0.1 address pLine = DJFindLineByIPAddress(pHostsFileLineList, "127.0.0.1"); if(pLine == NULL) { //We have to create the 127.0.0.1 address ceError = CTAllocateMemory(sizeof(HOSTSFILELINE), (PVOID*)(PVOID)&pCreatedLine); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateMemory(sizeof(HOSTSFILEENTRY), (PVOID*)(PVOID)&pCreatedLine->pEntry); ceError = CTAllocateString("127.0.0.1", &pCreatedLine->pEntry->pszIpAddress); BAIL_ON_CENTERIS_ERROR(ceError); ceError = DJAddAlias(pCreatedLine, "localhost", FALSE, &bModified); BAIL_ON_CENTERIS_ERROR(ceError); *DJGetLastHostsLine(&pHostsFileLineList) = pCreatedLine; pLine = pCreatedLine; pCreatedLine = NULL; } ceError = DJUpdateHostEntry( pLine, pszHostName, pszCanonicalName, oldShortHostname, oldFqdnHostname); BAIL_ON_CENTERIS_ERROR(ceError); } } error: if (pAlias) DJFreeAlias(pAlias); if (pszHostName) CTFreeString(pszHostName); if (pszDomainName) CTFreeString(pszDomainName); if (pszCanonicalName) CTFreeMemory(pszCanonicalName); if(pCreatedLine) DJFreeHostsLine(pCreatedLine); return ceError; }
DWORD DJParseHostsFile( const char *filename, PHOSTSFILELINE* ppHostsFileLineList ) { DWORD ceError = ERROR_SUCCESS; PHOSTSFILELINE pLineHead = NULL; PHOSTSFILELINE pHostsLine = NULL; PHOSTFILEALIAS pAlias = NULL; FILE* fp = NULL; CHAR szBuf[1024+1]; PSTR pszTmp = NULL; DWORD iToken = 0; PHOSTSFILELINE pLineTail = NULL; BOOLEAN exists; BAIL_ON_CENTERIS_ERROR(ceError = CTCheckFileOrLinkExists(filename, &exists)); if(!exists) BAIL_ON_CENTERIS_ERROR(ceError = ERROR_FILE_NOT_FOUND); fp = fopen(filename, "r"); if (fp == NULL) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } while (1) { if (fgets(szBuf, 1024, fp) == NULL) { if (!feof(fp)) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } else { break; } } CTStripWhitespace(szBuf); ceError = CTAllocateMemory(sizeof(HOSTSFILELINE), (PVOID*)(PVOID)&pHostsLine); BAIL_ON_CENTERIS_ERROR(ceError); pHostsLine->pEntry = NULL; pHostsLine->pszComment = NULL; pszTmp = strchr(szBuf, '#'); if (pszTmp != NULL) { ceError = CTAllocateString(pszTmp, &pHostsLine->pszComment); BAIL_ON_CENTERIS_ERROR(ceError); *pszTmp = '\0'; } if(szBuf[0] != '\0') { ceError = CTAllocateMemory(sizeof(HOSTSFILEENTRY), (PVOID*)(PVOID)&pHostsLine->pEntry); BAIL_ON_CENTERIS_ERROR(ceError); iToken = 0; pszTmp = strtok(szBuf, " \t"); while (pszTmp != NULL) { if (iToken == 0) { ceError = CTAllocateString(pszTmp, &pHostsLine->pEntry->pszIpAddress); BAIL_ON_CENTERIS_ERROR(ceError); } else if (iToken == 1) { ceError = CTAllocateString(pszTmp, &pHostsLine->pEntry->pszCanonicalName); BAIL_ON_CENTERIS_ERROR(ceError); } else { ceError = CTAllocateMemory(sizeof(HOSTFILEALIAS), (PVOID*)(PVOID)&pAlias); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTAllocateString(pszTmp, &pAlias->pszAlias); BAIL_ON_CENTERIS_ERROR(ceError); //The alias list is first built in reverse pAlias->pNext = pHostsLine->pEntry->pAliasList; pHostsLine->pEntry->pAliasList = pAlias; pAlias = NULL; } iToken++; pszTmp = strtok(NULL, " \t"); } if (pHostsLine->pEntry->pAliasList) { pHostsLine->pEntry->pAliasList = DJReverseAliasList(pHostsLine->pEntry->pAliasList); } } if(pLineTail != NULL) pLineTail->pNext = pHostsLine; else pLineHead = pHostsLine; pLineTail = pHostsLine; pHostsLine = NULL; } *ppHostsFileLineList = pLineHead; pLineHead = NULL; error: if (pAlias) DJFreeAlias(pAlias); if (pHostsLine) DJFreeHostsLine(pHostsLine); if (pLineHead) DJFreeHostsFileLineList(pLineHead); if (fp) fclose(fp); return ceError; }