DWORD DJFixLoginConfigFile( PCSTR pszPath ) { DWORD ceError = ERROR_SUCCESS; PCSTR pszFilePath = NULL; PSTR pszTmpPath = NULL; PSTR pszFinalPath = NULL; BOOLEAN bFileExists = FALSE; FILE* fp = NULL; FILE* fp_new = NULL; DynamicArray lines; PSTR currentSystem = NULL; memset(&lines, 0, sizeof(lines)); if (IsNullOrEmptyString(pszPath)) pszFilePath = LOGIN_CONFIG_PATH; else pszFilePath = pszPath; GCE(ceError = CTGetFileTempPath( pszFilePath, &pszFinalPath, &pszTmpPath)); GCE(ceError = CTCheckFileExists(pszFinalPath, &bFileExists)); if (!bFileExists) goto cleanup; GCE(ceError = CTOpenFile(pszFinalPath, "r", &fp)); GCE(ceError = CTReadLines(fp, &lines)); GCE(ceError = CTSafeCloseFile(&fp)); GCE(ceError = GetAuthType(&lines, ¤tSystem)); if(!strcmp(currentSystem, "PAM_AUTH")) goto cleanup; GCE(ceError = SetAuthType(&lines, "PAM_AUTH")); GCE(ceError = CTOpenFile(pszTmpPath, "w", &fp_new)); GCE(ceError = CTWriteLines(fp_new, &lines)); GCE(ceError = CTSafeCloseFile(&fp_new)); GCE(ceError = CTSafeReplaceFile(pszFilePath, pszTmpPath)); cleanup: CTSafeCloseFile(&fp); CTSafeCloseFile(&fp_new); CT_SAFE_FREE_STRING(currentSystem); CT_SAFE_FREE_STRING(pszTmpPath); CT_SAFE_FREE_STRING(pszFinalPath); CTFreeLines(&lines); return ceError; }
static DWORD WriteNsswitchConfiguration(const char *rootPrefix, NsswitchConf *conf) { DWORD ceError = ERROR_SUCCESS; DynamicArray printedLine; int i; char *tempName = NULL; char *finalName = NULL; char *prefixedPath = NULL; FILE *file = NULL; memset(&printedLine, 0, sizeof(printedLine)); GCE(ceError = CTAllocateStringPrintf(&prefixedPath, "%s%s", rootPrefix, conf->filename)); GCE(ceError = CTGetFileTempPath( prefixedPath, &finalName, &tempName)); DJ_LOG_INFO("Writing nsswitch configuration for %s", finalName); ceError = CTOpenFile(tempName, "w", &file); if(ceError) { DJ_LOG_ERROR("Unable to open '%s' for writing", tempName); GCE(ceError); } for(i = 0; i < conf->lines.size; i++) { GCE(ceError = GetPrintedLine(&printedLine, conf, i)); GCE(ceError = CTFilePrintf(file, "%s\n", printedLine.data)); } GCE(ceError = CTCloseFile(file)); file = NULL; GCE(ceError = CTSafeReplaceFile( finalName, tempName)); DJ_LOG_INFO("File moved into place"); cleanup: if(file != NULL) CTCloseFile(file); CTArrayFree(&printedLine); CT_SAFE_FREE_STRING(tempName); CT_SAFE_FREE_STRING(finalName); CT_SAFE_FREE_STRING(prefixedPath); return ceError; }
static DWORD DJConfigureDHCPService( PSTR pszComputerName ) { DWORD ceError = ERROR_SUCCESS; int EE = 0; BOOLEAN bFileExists = FALSE; PSTR dhcpFilePath = "/etc/sysconfig/network/dhcp"; PSTR ppszArgs[] = { "/bin/sed", "s/^.*\\(DHCLIENT_SET_HOSTNAME\\).*=.*$/\\1=\\\"no\\\"/", dhcpFilePath, NULL }; #if !defined(HAVE_SETHOSTNAME) || !HAVE_DECL_SETHOSTNAME PSTR ppszNetArgs[] = { #if defined(_AIX) "/etc/rc.d/init.d/network", #else "/etc/init.d/network", #endif "restart", NULL }; #endif PPROCINFO pProcInfo = NULL; LONG status = 0; PSTR pszFinalPath = NULL; PSTR pszTmpPath = NULL; ceError = CTCheckFileExists(dhcpFilePath, &bFileExists); CLEANUP_ON_DWORD_EE(ceError, EE); if (bFileExists) { ceError = CTGetFileTempPath( dhcpFilePath, &pszFinalPath, &pszTmpPath); CLEANUP_ON_DWORD_EE(ceError, EE); ppszArgs[2] = pszFinalPath; ceError = DJSpawnProcessOutputToFile(ppszArgs[0], ppszArgs, pszTmpPath, &pProcInfo); CLEANUP_ON_DWORD_EE(ceError, EE); ceError = DJGetProcessStatus(pProcInfo, &status); CLEANUP_ON_DWORD_EE(ceError, EE); if (status != 0) { ceError = ERROR_FAIL_RESTART; CLEANUP_ON_DWORD_EE(ceError, EE); } ceError = CTSafeReplaceFile(pszFinalPath, pszTmpPath); CLEANUP_ON_DWORD_EE(ceError, EE); } if (pProcInfo) { FreeProcInfo(pProcInfo); pProcInfo = NULL; } #if defined(HAVE_SETHOSTNAME) && HAVE_DECL_SETHOSTNAME if (sethostname(pszComputerName, strlen(pszComputerName)) < 0) { ceError = LwMapErrnoToLwError(errno); CLEANUP_ON_DWORD_EE(ceError, EE); } #else ceError = DJFixNetworkManagerOnlineTimeout(); CLEANUP_ON_DWORD_EE(ceError, EE); /* Restart network */ ceError = DJSpawnProcess(ppszNetArgs[0], ppszNetArgs, &pProcInfo); CLEANUP_ON_DWORD_EE(ceError, EE); ceError = DJGetProcessStatus(pProcInfo, &status); CLEANUP_ON_DWORD_EE(ceError, EE); if (status != 0) { ceError = ERROR_BAD_COMMAND; CLEANUP_ON_DWORD_EE(ceError, EE); } #endif cleanup: if (pProcInfo) FreeProcInfo(pProcInfo); DJ_LOG_VERBOSE("DJRestartDHCPService LEAVE -> 0x%08x (EE = %d)", ceError, EE); CT_SAFE_FREE_STRING(pszFinalPath); CT_SAFE_FREE_STRING(pszTmpPath); return ceError; }
static DWORD DJReplaceNameValuePair( PSTR pszFilePath, PSTR pszName, PSTR pszValue ) { DWORD ceError = ERROR_SUCCESS; PSTR pszTmpPath = NULL; PSTR pszFinalPath = NULL; FILE* fpSrc = NULL; FILE* fpDst = NULL; regex_t rx; CHAR szRegExp[256]; CHAR szBuf[1024+1]; BOOLEAN bRemoveFile = FALSE; memset(&rx, 0, sizeof(rx)); ceError = CTGetFileTempPath( pszFilePath, &pszFinalPath, &pszTmpPath); BAIL_ON_CENTERIS_ERROR(ceError); sprintf(szRegExp, "^[[:space:]]*%s[[:space:]]*=.*$", pszName); if (regcomp(&rx, szRegExp, REG_EXTENDED) < 0) { ceError = LW_ERROR_REGEX_COMPILE_FAILED; BAIL_ON_CENTERIS_ERROR(ceError); } if ((fpSrc = fopen(pszFinalPath, "r")) == NULL) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } if ((fpDst = fopen(pszTmpPath, "w")) == NULL) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } bRemoveFile = TRUE; while (1) { if (fgets(szBuf, 1024, fpSrc) == NULL) { if (feof(fpSrc)) { break; } else { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } if (!IsComment(szBuf) && !regexec(&rx, szBuf, (size_t)0, NULL, 0)) { if (fprintf(fpDst, "%s=%s\n", pszName, pszValue) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } else { if (fputs(szBuf, fpDst) == EOF) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } } fclose(fpSrc); fpSrc = NULL; fclose(fpDst); fpDst = NULL; ceError = CTSafeReplaceFile(pszFinalPath, pszTmpPath); BAIL_ON_CENTERIS_ERROR(ceError); bRemoveFile = FALSE; error: if (fpSrc) fclose(fpSrc); if (fpDst) fclose(fpDst); regfree(&rx); if (bRemoveFile) CTRemoveFile(pszTmpPath); CT_SAFE_FREE_STRING(pszTmpPath); CT_SAFE_FREE_STRING(pszFinalPath); return ceError; }
static DWORD WriteHostnameToSunFiles( PCSTR pOldShortHostname, PCSTR pNewShortHostname, PCSTR pDnsDomainName, PCSTR pOldFqdnHostname, PCSTR pNewFqdnHostname ) { DWORD ceError = ERROR_SUCCESS; FILE* fp = NULL; PSTR* ppszHostfilePaths = NULL; DWORD nPaths = 0; DWORD iPath = 0; PSTR pRealPath = NULL; PSTR pTempPath = NULL; PSTR pOldEscapedShortHostname = NULL; PSTR pOldEscapedFqdnHostname = NULL; PSTR pOldSedExpression = NULL; PSTR pSedExpression = NULL; BOOLEAN isSame = FALSE; DJ_LOG_INFO("Setting hostname to [%s]", pNewShortHostname); ceError = CTGetFileTempPath( "/etc/nodename", &pRealPath, &pTempPath); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTOpenFile( pTempPath, "w", &fp); BAIL_ON_CENTERIS_ERROR(ceError); if (fprintf(fp, "%s\n", pNewShortHostname) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } ceError = CTSafeCloseFile(&fp); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTFileContentsSame( pTempPath, pRealPath, &isSame); BAIL_ON_CENTERIS_ERROR(ceError); if (isSame) { BAIL_ON_CENTERIS_ERROR(ceError = CTRemoveFile(pTempPath)); } else { ceError = CTSafeReplaceFile(pRealPath, pTempPath); BAIL_ON_CENTERIS_ERROR(ceError); } CT_SAFE_FREE_STRING(pRealPath); CT_SAFE_FREE_STRING(pTempPath); ceError = CTGetFileTempPath( "/etc/defaultdomain", &pRealPath, &pTempPath); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTOpenFile( pTempPath, "w", &fp); BAIL_ON_CENTERIS_ERROR(ceError); fprintf(fp, "%s\n", pDnsDomainName); ceError = CTSafeCloseFile(&fp); BAIL_ON_CENTERIS_ERROR(ceError); ceError = CTFileContentsSame( pTempPath, pRealPath, &isSame); BAIL_ON_CENTERIS_ERROR(ceError); if (isSame) { BAIL_ON_CENTERIS_ERROR(ceError = CTRemoveFile(pTempPath)); } else { ceError = CTSafeReplaceFile(pRealPath, pTempPath); BAIL_ON_CENTERIS_ERROR(ceError); } /* The first column of the /etc/hostname.<interface> files is the IP * address of the interface. It may either be specified directly in the * file, or the file may refer to an entry in /etc/hosts. This program is * editing /etc/hosts, any old entries in the hostname files need to be * fixed. */ ceError = CTGetMatchingFilePathsInFolder("/etc", "hostname.*", &ppszHostfilePaths, &nPaths); BAIL_ON_CENTERIS_ERROR(ceError); ceError = SedEscapeLiteral( pOldShortHostname, &pOldEscapedShortHostname); BAIL_ON_CENTERIS_ERROR(ceError); ceError = LwAllocateStringPrintf( &pSedExpression, "s/^%s\\([ ].*\\)\\{0,1\\}$/%s\\1/", pOldEscapedShortHostname, pNewShortHostname); BAIL_ON_CENTERIS_ERROR(ceError); if (pOldFqdnHostname != NULL) { ceError = SedEscapeLiteral( pOldFqdnHostname, &pOldEscapedFqdnHostname); BAIL_ON_CENTERIS_ERROR(ceError); pOldSedExpression = pSedExpression; pSedExpression = NULL; ceError = LwAllocateStringPrintf( &pSedExpression, "%s;s/^%s\\([ ].*\\)\\{0,1\\}$/%s\\1/", pOldSedExpression, pOldEscapedFqdnHostname, pNewFqdnHostname); BAIL_ON_CENTERIS_ERROR(ceError); } for (iPath = 0; iPath < nPaths; iPath++) { if (!CTStrEndsWith(ppszHostfilePaths[iPath], ".lwidentity.bak") && !CTStrEndsWith(ppszHostfilePaths[iPath], ".lwidentity.orig")) { ceError = CTRunSedOnFile( ppszHostfilePaths[iPath], ppszHostfilePaths[iPath], FALSE, pSedExpression); BAIL_ON_CENTERIS_ERROR(ceError); } } error: if (ppszHostfilePaths) CTFreeStringArray(ppszHostfilePaths, nPaths); if (fp) { fclose(fp); } CT_SAFE_FREE_STRING(pRealPath); CT_SAFE_FREE_STRING(pTempPath); CT_SAFE_FREE_STRING(pOldEscapedShortHostname); CT_SAFE_FREE_STRING(pOldEscapedFqdnHostname); CT_SAFE_FREE_STRING(pOldSedExpression); CT_SAFE_FREE_STRING(pSedExpression); return ceError; }
static void ConfigureApparmor(BOOLEAN enable, LWException **exc) { DWORD ceError = ERROR_SUCCESS; BOOLEAN hasApparmor; BOOLEAN configured; BOOLEAN usingMr; FILE *file = NULL; PCSTR addString; PSTR restartPath = NULL; PSTR restartCommand = NULL; char *tempName = NULL; char *finalName = NULL; LW_CLEANUP_CTERR(exc, IsApparmorConfigured(&configured)); if(configured == enable) goto cleanup; LW_CLEANUP_CTERR(exc, CTCheckFileOrLinkExists(APPARMOR_NSSWITCH, &hasApparmor)); if(!hasApparmor) goto cleanup; GCE(ceError = CTGetFileTempPath( APPARMOR_NSSWITCH, &finalName, &tempName)); LW_CLEANUP_CTERR(exc, CTCheckFileHoldsPattern(finalName, "mr,", &usingMr)); if(usingMr) addString = PREFIXDIR "/lib/*.so* mr,\n" PREFIXDIR "/lib64/*.so* mr,\n" "/tmp/.lwidentity/pipe rw,\n" LOCALSTATEDIR "/lib/likewise/.lsassd rw,\n" LOCALSTATEDIR "/tmp/.lsaclient_* rw,\n"; else addString = PREFIXDIR "/lib/*.so* r,\n" PREFIXDIR "/lib64/*.so* r,\n" "/tmp/.lwidentity/pipe rw,\n" LOCALSTATEDIR "/lib/likewise/.lsassd rw,\n" LOCALSTATEDIR "/tmp/.lsaclient_* rw,\n"; if(enable) { LW_CLEANUP_CTERR(exc, CTCopyFileWithOriginalPerms(finalName, tempName)); LW_CLEANUP_CTERR(exc, CTOpenFile(tempName, "a", &file)); LW_CLEANUP_CTERR(exc, CTFilePrintf(file, "# likewise\n%s# end likewise\n", addString)); CTSafeCloseFile(&file); LW_CLEANUP_CTERR(exc, CTSafeReplaceFile(finalName, tempName)); } else { LW_CLEANUP_CTERR(exc, CTRunSedOnFile(finalName, finalName, FALSE, "/^[ \t]*#[ \t]*likewise[ \t]*$/,/^[ \t]*#[ \t]*end likewise[ \t]*$/d")); LW_CLEANUP_CTERR(exc, CTRunSedOnFile(finalName, finalName, FALSE, "/^[ \t]*#[ \t]*centeris[ \t]*$/,/^[ \t]*#[ \t]*end centeris[ \t]*$/d")); } ceError = CTFindFileInPath("rcapparmor", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", &restartPath); if(ceError == ERROR_FILE_NOT_FOUND) { ceError = CTFindFileInPath("apparmor", "/etc/init.d/apparmor", &restartPath); } if(ceError == ERROR_FILE_NOT_FOUND) { ceError = ERROR_SUCCESS; } else if(!ceError) { LW_CLEANUP_CTERR(exc, CTAllocateStringPrintf(&restartCommand, "%s restart", restartPath)); LW_TRY(exc, CTCaptureOutputToExc(restartCommand, &LW_EXC)); } LW_CLEANUP_CTERR(exc, ceError); cleanup: if(file != NULL) { CTCloseFile(file); CTRemoveFile(tempName); } CT_SAFE_FREE_STRING(restartPath); CT_SAFE_FREE_STRING(restartCommand); CT_SAFE_FREE_STRING(tempName); CT_SAFE_FREE_STRING(finalName); }
static DWORD DJWriteHostsFileIfModified( const char *filename, PHOSTSFILELINE pHostFileLineList ) { DWORD ceError = ERROR_SUCCESS; PHOSTSFILELINE pLine = pHostFileLineList; FILE* fp = NULL; PHOSTFILEALIAS pAlias = NULL; BOOLEAN bRemoveFile = FALSE; char *tempName = NULL; char *finalName = NULL; ceError = CTGetFileTempPath( filename, &finalName, &tempName); BAIL_ON_CENTERIS_ERROR(ceError); if (DJHostsFileWasModified(pHostFileLineList)) { DJ_LOG_INFO("Writing out updated %s file", finalName); fp = fopen(tempName, "w"); if (fp == NULL) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } bRemoveFile = TRUE; while (pLine) { if (pLine->pEntry != NULL) { if (!IsNullOrEmptyString(pLine->pEntry->pszIpAddress)) { if (fprintf(fp, "%s", pLine->pEntry->pszIpAddress) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } if (!IsNullOrEmptyString(pLine->pEntry->pszCanonicalName)) { if (fprintf(fp, " %s", pLine->pEntry->pszCanonicalName) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } pAlias = pLine->pEntry->pAliasList; while (pAlias) { if (!IsNullOrEmptyString(pAlias->pszAlias)) { if (fprintf(fp, " %s", pAlias->pszAlias) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } pAlias = pAlias->pNext; } if (pLine->pszComment != NULL) { if (fprintf(fp, " %s", pLine->pszComment) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } if (fprintf(fp, "\n") < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } else if(pLine->pszComment != NULL) { if (fprintf(fp, "%s\n", pLine->pszComment) < 0) { ceError = LwMapErrnoToLwError(errno); BAIL_ON_CENTERIS_ERROR(ceError); } } pLine = pLine->pNext; } if (fp) { fclose(fp); fp = NULL; } ceError = CTSafeReplaceFile(finalName, tempName); BAIL_ON_CENTERIS_ERROR(ceError); #if defined(__LWI_DARWIN__) MacDnsCacheFlush(); #endif bRemoveFile = FALSE; } else DJ_LOG_INFO("%s file was not modified; not rewriting", finalName); error: if (fp) fclose(fp); if (bRemoveFile) CTRemoveFile(tempName); CT_SAFE_FREE_STRING(finalName); CT_SAFE_FREE_STRING(tempName); return ceError; }