void RegLexClose( PREGLEX_ITEM pLexHandle) { if (pLexHandle) { if (pLexHandle->curToken.pszValue) { RegMemoryFree(pLexHandle->curToken.pszValue); pLexHandle->curToken.pszValue = NULL; } RegMemoryFree(pLexHandle); } }
VOID RegFreeSysLogInfo( PREG_SYS_LOG pSysLog ) { if (pSysLog->bOpened) { /* close connection to syslog */ closelog(); } LWREG_SAFE_FREE_STRING(pSysLog->pszIdentifier); RegMemoryFree(pSysLog); }
DWORD RegShellUtilExport( HANDLE hReg, PREG_EXPORT_STATE pExportState, HKEY hKey, PCSTR pszKeyName, DWORD dwNumSubKeys, DWORD dwMaxSubKeyLen ) { DWORD dwError = 0; REG_DATA_TYPE prevType = REG_NONE; SECURITY_INFORMATION SecInfoAll = OWNER_SECURITY_INFORMATION |GROUP_SECURITY_INFORMATION |DACL_SECURITY_INFORMATION |SACL_SECURITY_INFORMATION; PBYTE pSecDescRel = NULL; ULONG ulSecDescLen = SECURITY_DESCRIPTOR_RELATIVE_MAX_SIZE; PSTR pszStringSecurityDescriptor = NULL; dwError = RegGetKeySecurity(hReg, hKey, SecInfoAll, NULL, &ulSecDescLen); BAIL_ON_REG_ERROR(dwError); dwError = RegAllocateMemory(ulSecDescLen, (PVOID)&pSecDescRel); BAIL_ON_REG_ERROR(dwError); dwError = RegGetKeySecurity(hReg, hKey, SecInfoAll, (PSECURITY_DESCRIPTOR_RELATIVE)pSecDescRel, &ulSecDescLen); BAIL_ON_REG_ERROR(dwError); if (LwRtlCStringIsEqual(pszKeyName,HKEY_THIS_MACHINE,TRUE) || ulSecDescLen != pExportState->ulRootKeySecDescLen || !LwRtlEqualMemory(pSecDescRel, pExportState->pRootKeySecDescRel, ulSecDescLen)) { dwError = RegNtStatusToWin32Error( RtlAllocateSddlCStringFromSecurityDescriptor( &pszStringSecurityDescriptor, (PSECURITY_DESCRIPTOR_RELATIVE)pSecDescRel, SDDL_REVISION_1, SecInfoAll) ); BAIL_ON_REG_ERROR(dwError); } if (hKey) { dwError = ProcessExportedKeyInfo(hReg, pExportState, hKey, pszKeyName, pExportState->dwExportFormat == 1 ? NULL : pszStringSecurityDescriptor, &prevType); BAIL_ON_REG_ERROR(dwError); } if (hKey && dwNumSubKeys != 0) { dwError = ProcessSubKeys(hReg, pExportState, hKey, pszKeyName, dwNumSubKeys, dwMaxSubKeyLen); BAIL_ON_REG_ERROR(dwError); } else if (hKey == NULL && dwNumSubKeys == 0) { dwError = ProcessRootKeys(hReg, pExportState); BAIL_ON_REG_ERROR(dwError); } else if (hKey == NULL && dwNumSubKeys != 0) { dwError = ERROR_INTERNAL_ERROR; BAIL_ON_REG_ERROR(dwError); } cleanup: if (pszStringSecurityDescriptor) { RegFreeString(pszStringSecurityDescriptor); } LWREG_SAFE_FREE_MEMORY(pSecDescRel); if (pSecDescRel) { RegMemoryFree(pSecDescRel); pSecDescRel = NULL; } return dwError; error: goto cleanup; }
static DWORD PrintToRegFile( IN FILE* fp, IN PCSTR pszKeyName, IN OPTIONAL PCSTR pszSddlCString, IN REG_DATA_TYPE dataType, IN PCSTR pszValueName, IN REG_DATA_TYPE type, IN BOOLEAN bDefault, IN PVOID value, IN DWORD dwValueLen, OUT PREG_DATA_TYPE pPrevType ) { PSTR dumpString = NULL; DWORD dumpStringLen = 0; PSTR pszStart = NULL; PSTR pszEnd = NULL; PSTR pszComment = bDefault ? "#" : ""; RegExportEntry(pszKeyName, pszSddlCString, dataType, pszValueName, type, value, dwValueLen, &dumpString, &dumpStringLen); if (dumpStringLen > 0 && dumpString) { switch (type) { case REG_KEY: fprintf(fp, "\r\n%s%.*s\r\n", pszComment, dumpStringLen, dumpString); break; case REG_MULTI_SZ: pszStart = dumpString; pszEnd = strchr(pszStart, '\n'); while (pszEnd) { fprintf(fp, "%s%.*s\r\n", pszComment, (int) (pszEnd-pszStart), pszStart); pszStart = pszEnd+1; pszEnd = strchr(pszStart, '\n'); } if (pszStart && *pszStart) { fprintf(fp, "%s%s\r\n", pszComment, pszStart); } break; case REG_PLAIN_TEXT: if (*pPrevType && *pPrevType != type) { printf("\n"); } fprintf(fp, "%s%*s ", pszComment, dwValueLen, (PCHAR) value); break; default: fprintf(fp, "%s%.*s\r\n", pszComment, dumpStringLen, dumpString); break; } } fflush(stdout); *pPrevType = type; if (dumpString) { RegMemoryFree(dumpString); dumpString = NULL; } return 0; }
static DWORD RegCreateDirectoryRecursive( PSTR pszCurDirPath, PSTR pszTmpPath, PSTR *ppszTmp, DWORD dwFileMode, DWORD dwWorkingFileMode, int iPart ) { DWORD dwError = 0; PSTR pszDirPath = NULL; BOOLEAN bDirCreated = FALSE; BOOLEAN bDirExists = FALSE; CHAR szDelimiters[] = "/"; PSTR pszToken = strtok_r((iPart ? NULL : pszTmpPath), szDelimiters, ppszTmp); if (pszToken != NULL) { dwError = RegAllocateMemory(strlen(pszCurDirPath)+strlen(pszToken)+2, (PVOID*)&pszDirPath); BAIL_ON_REG_ERROR(dwError); sprintf(pszDirPath, "%s/%s", (!strcmp(pszCurDirPath, "/") ? "" : pszCurDirPath), pszToken); dwError = RegCheckDirectoryExists(pszDirPath, &bDirExists); BAIL_ON_REG_ERROR(dwError); if (!bDirExists) { if (mkdir(pszDirPath, dwWorkingFileMode) < 0) { dwError = errno; BAIL_ON_REG_ERROR(dwError); } bDirCreated = TRUE; } dwError = RegChangeDirectory(pszDirPath); BAIL_ON_REG_ERROR(dwError); dwError = RegCreateDirectoryRecursive( pszDirPath, pszTmpPath, ppszTmp, dwFileMode, dwWorkingFileMode, iPart+1 ); BAIL_ON_REG_ERROR(dwError); } if (bDirCreated && (dwFileMode != dwWorkingFileMode)) { dwError = RegChangePermissions(pszDirPath, dwFileMode); BAIL_ON_REG_ERROR(dwError); } if (pszDirPath) { RegMemoryFree(pszDirPath); } return dwError; error: if (bDirCreated) { RegRemoveDirectory(pszDirPath); } if (pszDirPath) { RegMemoryFree(pszDirPath); } return dwError; }
DWORD RegGetMatchingFilePathsInFolder( PCSTR pszDirPath, PCSTR pszFileNameRegExp, PSTR** pppszHostFilePaths, PDWORD pdwNPaths ) { typedef struct __PATHNODE { PSTR pszPath; struct __PATHNODE *pNext; } *PPATHNODE; DWORD dwError = 0; DIR* pDir = NULL; struct dirent* pDirEntry = NULL; regex_t rx; BOOLEAN rxAllocated = FALSE; regmatch_t* pResult = NULL; size_t nMatch = 1; DWORD dwNPaths = 0; DWORD iPath = 0; PSTR* ppszHostFilePaths = NULL; CHAR szBuf[PATH_MAX+1]; struct stat statbuf; PPATHNODE pPathList = NULL; PPATHNODE pPathNode = NULL; BOOLEAN bDirExists = FALSE; dwError = RegCheckDirectoryExists(pszDirPath, &bDirExists); BAIL_ON_REG_ERROR(dwError); if(!bDirExists) { dwError = ENOENT; BAIL_ON_REG_ERROR(dwError); } if (regcomp(&rx, pszFileNameRegExp, REG_EXTENDED) != 0) { dwError = LWREG_ERROR_REGEX_COMPILE_FAILED; BAIL_ON_REG_ERROR(dwError); } rxAllocated = TRUE; dwError = RegAllocateMemory(sizeof(*pResult), (PVOID*)&pResult); BAIL_ON_REG_ERROR(dwError); pDir = opendir(pszDirPath); if (!pDir) { dwError = errno; BAIL_ON_REG_ERROR(dwError); } while ((pDirEntry = readdir(pDir)) != NULL) { int copied = snprintf( szBuf, sizeof(szBuf), "%s/%s", pszDirPath, pDirEntry->d_name); if (copied >= sizeof(szBuf)) { //Skip pathnames that are too long continue; } memset(&statbuf, 0, sizeof(struct stat)); if (lstat(szBuf, &statbuf) < 0) { if(errno == ENOENT) { //This occurs when there is a symbolic link pointing to a //location that doesn't exist, because stat looks at the final //file, not the link. Since this file doesn't exist anyway, //just skip it. continue; } dwError = errno; BAIL_ON_REG_ERROR(dwError); } /* * For now, we are searching only for regular files * This may be enhanced in the future to support additional * file system entry types */ if (((statbuf.st_mode & S_IFMT) == S_IFREG) && (regexec(&rx, pDirEntry->d_name, nMatch, pResult, 0) == 0)) { dwNPaths++; dwError = RegAllocateMemory(sizeof(*pPathNode), (PVOID*)&pPathNode); BAIL_ON_REG_ERROR(dwError); dwError = RegCStringDuplicate(&pPathNode->pszPath, szBuf); BAIL_ON_REG_ERROR(dwError); pPathNode->pNext = pPathList; pPathList = pPathNode; pPathNode = NULL; } } if (pPathList) { dwError = RegAllocateMemory(sizeof(*ppszHostFilePaths)*dwNPaths, (PVOID*)&ppszHostFilePaths); BAIL_ON_REG_ERROR(dwError); /* * The linked list is in reverse. * Assign values in reverse to get it right */ iPath = dwNPaths-1; pPathNode = pPathList; while (pPathNode) { *(ppszHostFilePaths+iPath) = pPathNode->pszPath; pPathNode->pszPath = NULL; pPathNode = pPathNode->pNext; iPath--; } } *pppszHostFilePaths = ppszHostFilePaths; *pdwNPaths = dwNPaths; cleanup: if (pPathNode) { LWREG_SAFE_FREE_STRING(pPathNode->pszPath); RegMemoryFree(pPathNode); } while(pPathList) { pPathNode = pPathList; pPathList = pPathList->pNext; LWREG_SAFE_FREE_STRING(pPathNode->pszPath); RegMemoryFree(pPathNode); } if (rxAllocated) { regfree(&rx); } LWREG_SAFE_FREE_MEMORY(pResult); if (pDir) { closedir(pDir); } return dwError; error: if (ppszHostFilePaths) { RegFreeStringArray(ppszHostFilePaths, dwNPaths); } goto cleanup; }
DWORD RegCreateDirectory( PCSTR pszPath, mode_t dwFileMode ) { DWORD dwError = 0; PSTR pszCurDirPath = NULL; PSTR pszTmpPath = NULL; PSTR pszTmp = NULL; mode_t dwWorkingFileMode; if (pszPath == NULL || *pszPath == '\0') { dwError = EINVAL; BAIL_ON_REG_ERROR(dwError); } dwWorkingFileMode = dwFileMode; if (!(dwFileMode & S_IXUSR)) { /* * This is so that we can navigate the folders * when we are creating the subfolders */ dwWorkingFileMode |= S_IXUSR; } dwError = RegGetCurrentDirectoryPath(&pszCurDirPath); BAIL_ON_REG_ERROR(dwError); dwError = RegCStringDuplicate(&pszTmpPath, pszPath); BAIL_ON_REG_ERROR(dwError); if (*pszPath == '/') { dwError = RegChangeDirectory("/"); BAIL_ON_REG_ERROR(dwError); dwError = RegCreateDirectoryRecursive("/", pszTmpPath, &pszTmp, dwFileMode, dwWorkingFileMode, 0); BAIL_ON_REG_ERROR(dwError); } else { dwError = RegCreateDirectoryRecursive(pszCurDirPath, pszTmpPath, &pszTmp, dwFileMode, dwWorkingFileMode, 0); BAIL_ON_REG_ERROR(dwError); } error: if (pszCurDirPath) { RegChangeDirectory(pszCurDirPath); RegMemoryFree(pszCurDirPath); } if (pszTmpPath) { RegMemoryFree(pszTmpPath); } return dwError; }