Exemple #1
0
DWORD
RegSrvStartListenThread(
    void
    )
{
    PSTR pszCachePath = NULL;
    PSTR pszCommPath = NULL;
    BOOLEAN bDirExists = FALSE;
    DWORD dwError = 0;
    static LWMsgTime idleTimeout = {30, 0};

    dwError = RegSrvGetCachePath(&pszCachePath);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegCheckDirectoryExists(pszCachePath, &bDirExists);
    BAIL_ON_REG_ERROR(dwError);

    if (!bDirExists)
    {
        // Directory should be RWX for root and accessible to all
        // (so they can see the socket.
        mode_t mode = S_IRWXU | S_IRGRP| S_IXGRP | S_IROTH | S_IXOTH;
        dwError = RegCreateDirectory(pszCachePath, mode);
        BAIL_ON_REG_ERROR(dwError);
    }


    dwError = LwRtlCStringAllocatePrintf(&pszCommPath, "%s/%s",
                                      pszCachePath, REG_SERVER_FILENAME);
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_context_new(NULL, &gpContext));
    BAIL_ON_REG_ERROR(dwError);

    lwmsg_context_set_log_function(gpContext, RegSrvLogIpc, NULL);

    /* Set up IPC protocol object */
    dwError = MAP_LWMSG_ERROR(lwmsg_protocol_new(gpContext, &gpProtocol));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_protocol_add_protocol_spec(
                              gpProtocol,
                              RegIPCGetProtocolSpec()));
    BAIL_ON_REG_ERROR(dwError);

    /* Set up IPC server object */
    dwError = MAP_LWMSG_ERROR(lwmsg_peer_new(gpContext, gpProtocol, &gpServer));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_dispatch_spec(
                              gpServer,
                              RegSrvGetDispatchSpec()));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_listen_endpoint(
                              gpServer,
                              LWMSG_ENDPOINT_DIRECT,
                              "lwreg",
                              0));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_listen_endpoint(
                              gpServer,
                              LWMSG_ENDPOINT_LOCAL,
                              pszCommPath,
                              0666));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_max_listen_clients(
                                  gpServer,
                                  MAX_CLIENTS));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_max_listen_backlog(
                                  gpServer,
                                  REG_MAX(5, MAX_CLIENTS / 4)));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_timeout(
                                  gpServer,
                                  LWMSG_TIMEOUT_IDLE,
                                  &idleTimeout));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_listen_session_functions(
                                  gpServer,
                                  RegSrvIpcConstructSession,
                                  RegSrvIpcDestructSession,
                                  NULL));
    BAIL_ON_REG_ERROR(dwError);

    dwError = MAP_LWMSG_ERROR(lwmsg_peer_start_listen(gpServer));

error:

    LWREG_SAFE_FREE_STRING(pszCachePath);
    LWREG_SAFE_FREE_STRING(pszCommPath);

    if (dwError)
    {
        if (gpServer)
        {
            lwmsg_peer_stop_listen(gpServer);
            lwmsg_peer_delete(gpServer);
            gpServer = NULL;
        }
    }

    return dwError;
}
Exemple #2
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;
}
Exemple #3
0
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;
}
Exemple #4
0
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;
}
int main(int argc, char *argv[])
{
    PIV_CONVERT_CTX ivHandle = NULL;
    PCHAR strDefaultList[] = {
        "1: Now is the time",
        "2: For all good people",
        "3: All work and no play",
        "4: Makes Jack a dull boy",
        NULL,
    };

    PCHAR *argcList = NULL;
    PCHAR *multiStringList = strDefaultList;
    PBYTE multiString = NULL;
    ssize_t multiStringLen = 0;
    PSTR exportString = NULL;
    DWORD exportStringLen = 0;
    DWORD dwError = 0;
    DWORD count = 0;


    if (argc > 1)
    {
        dwError = RegAllocateMemory(sizeof(PCHAR) * argc, (PVOID*)&argcList);
        BAIL_ON_REG_ERROR(dwError);

        for (count=1; count < argc; count++)
        {
            argcList[count-1] = argv[count];
        }
        multiStringList = argcList;
    }

    RegIconvConvertOpen(&ivHandle, "ucs-2le", "utf-8");
    RegMultiStrsToByteArray(multiStringList,
                                &multiString,
                                &multiStringLen);
    RegExportEntry("HKLM_LINUX/likewise/registry/devel",
                   NULL,
                   0,
                   NULL,
                   REG_KEY,
                   NULL,
                   0,
                   &exportString,
                   &exportStringLen);
    printf("%s\n", exportString);

    RegExportEntry(NULL,
                   NULL,
                   0,
                   "testkey1",
                   REG_MULTI_SZ,
                   multiString,
                   multiStringLen,
                   &exportString,
                   &exportStringLen);
    printf("%s\n", exportString);

    RegIconvConvertClose(ivHandle);

cleanup:
    return dwError;

error:
    goto cleanup;

}
int main(int argc, char *argv[])
{
    DWORD dwError;
    HANDLE hReg = NULL;
    PLWREG_VALUE_ATTRIBUTES pAttr = NULL;
    PLWREG_VALUE_ATTRIBUTES pAttr_int = NULL;
    PWSTR* ppwszRootKeyNames = NULL;
    DWORD dwNumRootKeys = 0;
    wchar16_t szSubKey[] = {'a', 0};
    wchar16_t szSubKey1[] = {'b', 0};
    wchar16_t szValueName[] = {'a','t','t','r',0};
    wchar16_t szValueName1[] = {'a','t','t','r','1',0};
    HKEY hKey = NULL;
    HKEY hSubKey = NULL;
    HKEY hSubSubKey = NULL;

    PLWREG_CURRENT_VALUEINFO pCurrentValue = NULL;
    PLWREG_VALUE_ATTRIBUTES pValueAttributes = NULL;
    PLWREG_VALUE_ATTRIBUTES pValueAttributes_int = NULL;

    ValueAttribute.Range.ppszRangeEnumStrings = ppszRangeEnumStrings;
    ValueAttribute_int.Range.RangeInteger.Max = 100;
    ValueAttribute_int.Range.RangeInteger.Min = 1;

    DWORD dwType = REG_NONE;
    PBYTE pData[MAX_VALUE_LENGTH] = {0};
    DWORD cbData = MAX_VALUE_LENGTH;

    wchar16_t pCurrData[] = {'C', 'u', 'r', 'r', 'e', 'n', 't', 0};


    dwError = RegConvertValueAttributesAToW(ValueAttribute,
                                            &pAttr);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegConvertValueAttributesAToW(ValueAttribute_int,
                                            &pAttr_int);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegOpenServer(&hReg);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegEnumRootKeysW(hReg,
                               &ppwszRootKeyNames,
                               &dwNumRootKeys);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegOpenKeyExW(
                hReg,
                NULL,
                ppwszRootKeyNames[0],
                0,
                KEY_ALL_ACCESS | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | DELETE,
                &hKey);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegCreateKeyExW(
                hReg,
                hKey,
                szSubKey,
                0,
                NULL,
                0,
                KEY_ALL_ACCESS,
                NULL,
                &hSubKey,
                NULL);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegCreateKeyExW(
                hReg,
                hSubKey,
                szSubKey1,
                0,
                NULL,
                0,
                KEY_ALL_ACCESS,
                NULL,
                &hSubSubKey,
                NULL);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueAttributesW(
                   hReg,
                   hKey,
                   NULL,
                   szValueName,
                   pAttr);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueAttributesW(
                   hReg,
                   hKey,
                   NULL,
                   szValueName1,
                   pAttr_int);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueAttributesW(
                   hReg,
                   hKey,
                   szSubKey,
                   szValueName,
                   pAttr);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueAttributesW(
                   hReg,
                   hKey,
                   szSubKey,
                   szValueName1,
                   pAttr);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueAttributesW(
                   hReg,
                   hSubSubKey,
                   NULL,
                   szValueName1,
                   pAttr_int);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueAttributesW(
                   hReg,
                   hSubSubKey,
                   NULL,
                   szValueName,
                   pAttr);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegGetValueAttributesW(
                   hReg,
                   hKey,
                   NULL,
                   szValueName,
                   &pCurrentValue,
                   &pValueAttributes);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegGetValueAttributesW(
                   hReg,
                   hKey,
                   NULL,
                   szValueName1,
                   NULL,
                   &pValueAttributes_int);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegSetValueExW(
                  hReg,
                  hSubKey,
                  szValueName,
                  0,
                  REG_SZ,
                  (const BYTE*)pCurrData,
                  (wc16slen(pCurrData)+1)*sizeof(*pCurrData));
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegGetValueW(
                   hReg,
                   hKey,
                   szSubKey,
                   szValueName,
                   RRF_RT_REG_NONE,
                   &dwType,
                   pData,
                   &cbData);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegDeleteValueAttributesW(
                   hReg,
                   hKey,
                   NULL,
                   szValueName);
    BAIL_ON_REG_ERROR(dwError);

    dwError = RegDeleteTreeW(
                   hReg,
                   hKey,
                   szSubKey);
    if (LWREG_ERROR_KEY_IS_ACTIVE == dwError)
    {
        if (hSubKey)
        {
            RegCloseKey(hReg, hSubKey);
        }
        if (hSubSubKey)
        {
            RegCloseKey(hReg, hSubSubKey);
        }
        dwError = RegDeleteTreeW(
                hReg,
                hKey,
                szSubKey);
        BAIL_ON_REG_ERROR(dwError);
    }
    BAIL_ON_REG_ERROR(dwError);

cleanup:

    if (hKey)
    {
        RegCloseKey(hReg, hKey);
    }
    if (hSubKey)
    {
        RegCloseKey(hReg, hSubKey);
    }
    if (hSubSubKey)
    {
        RegCloseKey(hReg, hSubSubKey);
    }


    if (hReg)
    {
        RegCloseServer(hReg);
    }

    RegFreeWC16StringArray(ppwszRootKeyNames,
                           dwNumRootKeys);

    RegSafeFreeValueAttributes(&pAttr);
    RegSafeFreeCurrentValueInfo(&pCurrentValue);
    RegSafeFreeValueAttributes(&pValueAttributes);


    return dwError;

error:

    goto cleanup;
}