static DWORD RemoveOption(struct SshConf *conf, const char *name)
{
    DWORD ceError = ERROR_SUCCESS;
    int line;

    for(line = 0; line < conf->lineCount; line++)
    {
        line = FindOption(conf, line, name);
        if(line == -1)
            break;

        BAIL_ON_CENTERIS_ERROR(ceError = RemoveLine(conf, &line));
        if(line > 0)
            line--;
    }

error:
    UpdatePublicLines(conf);
    return ceError;
}
Exemple #2
0
DWORD
CTCheckDirectoryExists(
    PCSTR pszPath,
    PBOOLEAN pbDirExists
    )
{
    DWORD ceError = ERROR_SUCCESS;

    struct stat statbuf;

    while (1) {

        memset(&statbuf, 0, sizeof(struct stat));

        if (stat(pszPath, &statbuf) < 0) {

            if (errno == EINTR) {
                continue;
            }
            else if (errno == ENOENT || errno == ENOTDIR) {
                *pbDirExists = FALSE;
                break;
            }
            ceError = LwMapErrnoToLwError(errno);
            BAIL_ON_CENTERIS_ERROR(ceError);

        }

        /*
           The path exists. Is it a directory?
         */

        *pbDirExists = (((statbuf.st_mode & S_IFMT) == S_IFDIR) ? TRUE : FALSE);
        break;
    }

error:

    return ceError;
}
Exemple #3
0
DWORD
DJIsDomainNameResolvable(
    PCSTR pszDomainName,
    PBOOLEAN pbIsResolvable
    )
{
    DWORD ceError = ERROR_SUCCESS;
    struct hostent* pHostent = NULL;
    int i = 0;

    *pbIsResolvable = FALSE;

    if (IsNullOrEmptyString(pszDomainName)) {
        ceError = ERROR_INVALID_PARAMETER;
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    for(i = 0; i < 3; i++){
        pHostent = gethostbyname(pszDomainName);
        if (pHostent == NULL) {
            if (h_errno == TRY_AGAIN) {
                continue;
            } else {
                *pbIsResolvable = FALSE;
                break;
            }
        } else {
            *pbIsResolvable = !IsNullOrEmptyString(pHostent->h_name);
            break;
        }
    }

    return ceError;

error:

    *pbIsResolvable = FALSE;

    return ceError;
}
Exemple #4
0
DWORD
CTGetOwnerUID(
    PCSTR pszFilePath,
    uid_t* pUid
    )
{
    DWORD ceError = ERROR_SUCCESS;
    struct stat statbuf;

    memset(&statbuf, 0, sizeof(struct stat));

    if (stat(pszFilePath, &statbuf) < 0) {
        ceError = LwMapErrnoToLwError(errno);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    *pUid = statbuf.st_uid;
    
error:

    return ceError;
}
Exemple #5
0
DWORD
CTRemoveFile(
    PCSTR pszPath
    )
{
    DWORD ceError = ERROR_SUCCESS;

    while (1) {
        if (unlink(pszPath) < 0) {
            if (errno == EINTR) {
                continue;
            }
            ceError = LwMapErrnoToLwError(errno);
            BAIL_ON_CENTERIS_ERROR(ceError);
        } else {
            break;
        }
    }

error:

    return ceError;
}
Exemple #6
0
DWORD
CTChangePermissions(
    PCSTR pszPath,
    mode_t dwFileMode
    )
{
    DWORD ceError = ERROR_SUCCESS;

    while (1) {
        if (chmod(pszPath, dwFileMode) < 0) {
            if (errno == EINTR) {
                continue;
            }
            ceError = LwMapErrnoToLwError(errno);
            BAIL_ON_CENTERIS_ERROR(ceError);
        } else {
            break;
        }
    }

error:

    return ceError;
}
Exemple #7
0
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;
}
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;
}
Exemple #9
0
DWORD
DJIsAppleADPluginInUse(BOOLEAN* pExists)
{
    DWORD ceError = ERROR_SUCCESS;
    PPROCINFO pProcInfo = NULL;
    PSTR* ppszArgs = NULL;
    DWORD nArgs = 8;
    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("|", ppszArgs+4);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("grep", ppszArgs+5);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString(APPLEADDSPLUGIN_NAME, ppszArgs+6);
    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;
}
Exemple #10
0
/* Copy a ssh configuration line and add it below the old line. */
static DWORD SetOption(struct SshConf *conf, const char *name, const char *value)
{
    DWORD ceError = ERROR_SUCCESS;
    int line = -1;
    DynamicArray printedLine;
    struct SshLine lineObj;
    int found = 0;

    memset(&lineObj, 0, sizeof(struct SshLine));
    memset(&printedLine, 0, sizeof(printedLine));

    for(line = 0; line < conf->lineCount; line++)
    {
        line = FindOption(conf, line, name);
        if(line == -1)
            break;
        found++;
        if(!strcmp(conf->lines[line].value.value, value))
            continue;

        //Insert a commented out version of the line
        BAIL_ON_CENTERIS_ERROR(ceError = GetPrintedLine(&printedLine, conf, line));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.leadingWhiteSpace));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.name.value));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.name.trailingSeparator));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.value.value));
        BAIL_ON_CENTERIS_ERROR(ceError = CTAllocateStringPrintf(
            &lineObj.value.trailingSeparator,
            "#Overwritten by lwidentity: %s",
            printedLine.data));
        BAIL_ON_CENTERIS_ERROR(ceError = CTArrayInsert(&conf->private_data,
                    line, sizeof(struct SshLine), &lineObj, 1));
        memset(&lineObj, 0, sizeof(lineObj));
        UpdatePublicLines(conf);
        conf->modified = 1;
        line++;

        //Change the option value of the line
        CT_SAFE_FREE_STRING(conf->lines[line].value.value);
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(value,
            &conf->lines[line].value.value));
    }

    /*If the option wasn't already in the file, search for comments that
      mention the option, and insert the line after the comment*/
    for(line = 0; !found && line < conf->lineCount; line++)
    {
        if(strstr(conf->lines[line].value.trailingSeparator, name) == NULL)
            continue;

        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.leadingWhiteSpace));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(name,
            &lineObj.name.value));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(" ",
            &lineObj.name.trailingSeparator));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(value,
            &lineObj.value.value));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.value.trailingSeparator));
        BAIL_ON_CENTERIS_ERROR(ceError = CTArrayInsert(&conf->private_data,
                    line + 1, sizeof(struct SshLine), &lineObj, 1));
        memset(&lineObj, 0, sizeof(lineObj));
        conf->modified = 1;
        found++;
    }

    /*If the option wasn't even in a comment, just add the option at the
      end of the file
      */
    if(!found)
    {
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.leadingWhiteSpace));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(name,
            &lineObj.name.value));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(" ",
            &lineObj.name.trailingSeparator));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup(value,
            &lineObj.value.value));
        BAIL_ON_CENTERIS_ERROR(ceError = CTStrdup("",
            &lineObj.value.trailingSeparator));
        BAIL_ON_CENTERIS_ERROR(ceError = CTArrayAppend(&conf->private_data,
                    sizeof(struct SshLine), &lineObj, 1));
        memset(&lineObj, 0, sizeof(lineObj));
        conf->modified = 1;
    }

error:
    UpdatePublicLines(conf);
    FreeSshLineContents(&lineObj);
    CTArrayFree(&printedLine);
    return ceError;
}
Exemple #11
0
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;
}
Exemple #12
0
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;
}
Exemple #13
0
// 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;
}
Exemple #14
0
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;
}
Exemple #15
0
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;
}
Exemple #16
0
static
DWORD
DJUpdateHostEntry(
    PHOSTSFILELINE pLine,
    PCSTR pszShortName,
    PCSTR pszFqdnName,
    PCSTR pszRemoveName1,
    PCSTR pszRemoveName2
    )
{
    DWORD ceError = ERROR_SUCCESS;
    PHOSTFILEALIAS *aliasPos;

    //This updates our hostname in a line of the hosts file
    // 1. pszFqdnName will be added as the primary name in the host entry
    // 2. pszShortName will be added as the first alias
    // 3. Whatever was previously the primary name and first alias will be
    // added as the second and third aliases
    // 4. If pszRemoveName1 and or pszRemoveName2 are not null, they will
    // be removed from the alias list

    if (pszFqdnName == NULL && pszShortName != NULL)
    {
        pszFqdnName = pszShortName;
        pszShortName = NULL;
    }
    else if (pszFqdnName != NULL && pszShortName != NULL &&
             !strcasecmp(pszFqdnName, pszShortName))
    {
        pszShortName = NULL;
    }
    DJ_LOG_INFO("Adding %s (fqdn %s) to /etc/hosts ip %s, "
                "removing %s, %s, %s, %s",
                Disp(pszShortName), Disp(pszFqdnName),
                Disp(pLine->pEntry->pszIpAddress),
                Disp(pszShortName), Disp(pszFqdnName),
                Disp(pszRemoveName1), Disp(pszRemoveName2));

    if (pszFqdnName != NULL && (pLine->pEntry->pszCanonicalName == NULL ||
                                strcasecmp(pLine->pEntry->pszCanonicalName, pszFqdnName)))
    {
        if (pLine->pEntry->pszCanonicalName != NULL)
        {
            ceError = DJAddAlias(pLine, pLine->pEntry->pszCanonicalName, FALSE, NULL);
            BAIL_ON_CENTERIS_ERROR(ceError);
            CTFreeString(pLine->pEntry->pszCanonicalName);
            pLine->pEntry->pszCanonicalName = NULL;
        }

        ceError = CTAllocateString(pszFqdnName, &pLine->pEntry->pszCanonicalName);
        BAIL_ON_CENTERIS_ERROR(ceError);

        pLine->bModified = TRUE;
    }

    aliasPos = &pLine->pEntry->pAliasList;
    if (pszShortName != NULL)
    {
        if (pLine->pEntry->pAliasList == NULL || strcasecmp(pLine->pEntry->pAliasList->pszAlias, pszShortName))
        {
            ceError = DJAddAlias(pLine, pszShortName, TRUE, NULL);
            BAIL_ON_CENTERIS_ERROR(ceError);
        }
        //Skip over this so we don't delete it
        aliasPos = &(*aliasPos)->pNext;
    }

    {
        PCSTR removeStrings[] = {
            pszShortName,
            pszFqdnName,
            pszRemoveName1,
            pszRemoveName2 };
        while (*aliasPos != NULL)
        {
            int i;
            for(i=0; i<sizeof(removeStrings)/sizeof(removeStrings[0]); i++)
            {
                if(removeStrings[i] != NULL &&
                   !strcasecmp((*aliasPos)->pszAlias, removeStrings[i]))
                {
                    //Remove it
                    PHOSTFILEALIAS remove = *aliasPos;
                    (*aliasPos) = remove->pNext;
                    DJFreeAlias(remove);
                    pLine->bModified = TRUE;
                    goto removed_entry;
                }
            }
            //Advance to the next entry because nothing was removed.
            aliasPos = &(*aliasPos)->pNext;
removed_entry:
            ;
        }
    }
error:
    return ceError;
}
Exemple #17
0
/*
// TODO: Check access and removability before actual deletion
*/
DWORD
CTRemoveDirectory(
    PCSTR pszPath
    )
{
    DWORD ceError = ERROR_SUCCESS;
    DIR* pDir = NULL;
    struct dirent* pDirEntry = NULL;
    struct stat statbuf;
    CHAR szBuf[PATH_MAX+1];

    if ((pDir = opendir(pszPath)) == NULL) {
        ceError = LwMapErrnoToLwError(errno);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    while ((pDirEntry = readdir(pDir)) != NULL) {

        if (!strcmp(pDirEntry->d_name, "..") ||
            !strcmp(pDirEntry->d_name, "."))
            continue;

        sprintf(szBuf, "%s/%s", pszPath, pDirEntry->d_name);

        memset(&statbuf, 0, sizeof(struct stat));

        if (stat(szBuf, &statbuf) < 0) {
            ceError = LwMapErrnoToLwError(errno);
            BAIL_ON_CENTERIS_ERROR(ceError);
        }

        if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
            ceError = CTRemoveDirectory(szBuf);
            BAIL_ON_CENTERIS_ERROR(ceError);

            if (rmdir(szBuf) < 0) {
                ceError = LwMapErrnoToLwError(ceError);
                BAIL_ON_CENTERIS_ERROR(ceError);
            }

        } else {

            ceError = CTRemoveFile(szBuf);
            BAIL_ON_CENTERIS_ERROR(ceError);

        }
    }
    if(closedir(pDir) < 0)
    {
        pDir = NULL;
        ceError = LwMapErrnoToLwError(ceError);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }
    pDir = NULL;

    if (rmdir(pszPath) < 0) {
        ceError = LwMapErrnoToLwError(ceError);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

error:

    if (pDir)
        closedir(pDir);

    return ceError;
}
Exemple #18
0
DWORD
DJCopyMissingHostsEntry(
        PCSTR destFile, PCSTR srcFile,
        PCSTR entryName1, PCSTR entryName2)
{
    DWORD ceError = ERROR_SUCCESS;
    PHOSTSFILELINE pDestList = NULL;
    PHOSTSFILELINE pSrcList = NULL;
    PHOSTSFILELINE pLine = NULL;
    PHOSTSFILELINE pCopiedLine = NULL;

    ceError = DJParseHostsFile(destFile, &pDestList);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = DJParseHostsFile(srcFile, &pSrcList);
    BAIL_ON_CENTERIS_ERROR(ceError);

    if(entryName2 == NULL)
        entryName2 = "";

    for (pLine = pDestList; pLine; pLine = pLine->pNext) {

        if (pLine->pEntry != NULL) {
            if ((pLine->pEntry->pszCanonicalName != NULL &&
                (!strcasecmp(pLine->pEntry->pszCanonicalName, entryName1) ||
                 !strcasecmp(pLine->pEntry->pszCanonicalName, entryName2))) ||
                DJEntryHasAlias(pLine->pEntry->pAliasList, entryName1) ||
                DJEntryHasAlias(pLine->pEntry->pAliasList, entryName1))
            {
                //The dest file already has this entry
                goto done;
            }
        }
    }

    for (pLine = pSrcList; pLine; pLine = pLine->pNext) {

        if (pLine->pEntry != NULL) {
            if ((pLine->pEntry->pszCanonicalName != NULL &&
                (!strcasecmp(pLine->pEntry->pszCanonicalName, entryName1) ||
                 !strcasecmp(pLine->pEntry->pszCanonicalName, entryName2))) ||
                DJEntryHasAlias(pLine->pEntry->pAliasList, entryName1) ||
                DJEntryHasAlias(pLine->pEntry->pAliasList, entryName1))
            {
                //Copy this line to dest
                ceError = DJCopyLine(pLine, &pCopiedLine);
                BAIL_ON_CENTERIS_ERROR(ceError);

                pCopiedLine->pNext = pDestList;
                pDestList = pCopiedLine;
                pCopiedLine->bModified = TRUE;
                pCopiedLine = NULL;
            }
        }
    }

    ceError = DJWriteHostsFileIfModified(destFile, pDestList);
    BAIL_ON_CENTERIS_ERROR(ceError);

done:
error:
    if (pDestList)
        DJFreeHostsFileLineList(pDestList);
    if (pSrcList)
        DJFreeHostsFileLineList(pSrcList);
    if (pCopiedLine)
        DJFreeHostsFileLineList(pCopiedLine);
    return ceError;
}
Exemple #19
0
DWORD
SetStringRegistryValue(
    PCSTR path,
    PCSTR name,
    PSTR  value
    )
{
    DWORD ceError = ERROR_SUCCESS;
    HANDLE hReg = (HANDLE)NULL;
    HKEY pRootKey = NULL;
    HKEY pNodeKey = NULL;
    char szEmpty[2] = "";

    if (!value)
    {
        value = szEmpty;
    }

    ceError = RegOpenServer(&hReg);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = RegOpenKeyExA(
                hReg,
                NULL,
                HKEY_THIS_MACHINE,
                0,
                KEY_ALL_ACCESS,
                &pRootKey);
    if (ceError)
    {
        DJ_LOG_ERROR( "Failed to open registry root key %s",HKEY_THIS_MACHINE);
        goto error;
    }

    ceError = RegOpenKeyExA(
                hReg,
                pRootKey,
                path,
                0,
                KEY_ALL_ACCESS,
                &pNodeKey);
    if (ceError)
    {
        DJ_LOG_ERROR( "Failed to open registry key %s\\%s",HKEY_THIS_MACHINE, path);
        goto error;
    }

    ceError = RegSetValueExA(
                hReg,
                pNodeKey,
                name,
                0,
                REG_SZ,
                (const BYTE*)value,
                (DWORD)strlen(value)+1);
    if (ceError)
    {
        DJ_LOG_ERROR( "Failed to set registry value %s with value %s", name, value);
        goto error;
    }


cleanup:

    if (hReg)
    {
        if (pNodeKey)
        {
            RegCloseKey(hReg, pNodeKey);
            pNodeKey = NULL;
        }

        if (pRootKey)
        {
            RegCloseKey(hReg, pRootKey);
            pRootKey = NULL;
        }

        RegCloseServer(hReg);
        hReg = NULL;
    }

    return(ceError);

error:
    goto cleanup;
}
Exemple #20
0
DWORD
SetBooleanRegistryValue(
    PCSTR path,
    PCSTR name,
    BOOL  value
    )
{
    DWORD ceError = ERROR_SUCCESS;
    HANDLE hReg = (HANDLE)NULL;
    HKEY pRootKey = NULL;
    HKEY pNodeKey = NULL;
    DWORD dwValue = 0;

    if (value)
    {
        dwValue = 1;
    }

    ceError = RegOpenServer(&hReg);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = RegOpenKeyExA(
                hReg,
                NULL,
                HKEY_THIS_MACHINE,
                0,
                KEY_ALL_ACCESS,
                &pRootKey);
    if (ceError)
    {
        DJ_LOG_ERROR( "Failed to open registry root key %s",HKEY_THIS_MACHINE);
        goto error;
    }

    ceError = RegOpenKeyExA(
                hReg,
                pRootKey,
                path,
                0,
                KEY_ALL_ACCESS,
                &pNodeKey);
    if (ceError)
    {
        DJ_LOG_ERROR( "Failed to open registry key %s\\%s",HKEY_THIS_MACHINE, path);
        goto error;
    }

    ceError = RegSetValueExA(
                hReg,
                pNodeKey,
                name,
                0,
                REG_DWORD,
                (const BYTE*) &dwValue,
                sizeof(dwValue));
    if (ceError)
    {
        DJ_LOG_ERROR( "Failed to set registry value %s with value %d", name, value ? 1 : 0);
        goto error;
    }

cleanup:

    if (hReg)
    {
        if (pNodeKey)
        {
            RegCloseKey(hReg, pNodeKey);
            pNodeKey = NULL;
        }

        if (pRootKey)
        {
            RegCloseKey(hReg, pRootKey);
            pRootKey = NULL;
        }

        RegCloseServer(hReg);
        hReg = NULL;
    }

    return(ceError);

error:
    goto cleanup;
}
/* 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;
}
Exemple #22
0
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;
}
/* 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;
}
Exemple #24
0
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;
}
Exemple #25
0
DWORD
CTIsProgramRunning(
	PCSTR pszPidFile,
	PCSTR pszProgramName,
	pid_t *pPid,
    PBOOLEAN pbRunning
    )
{
    DWORD ceError = ERROR_SUCCESS;
    pid_t pid = 0;
    int fd = -1;
    int result;
    char contents[20];
    BOOLEAN bFileExists = FALSE;

    if(pbRunning != NULL)
        *pbRunning = FALSE;
    if(pPid != NULL)
        *pPid = -1;

    ceError = CTCheckFileExists(pszPidFile, &bFileExists);
    BAIL_ON_CENTERIS_ERROR(ceError);

    if (bFileExists) {

       fd = open(pszPidFile, O_RDONLY, 0644);
       if (fd < 0) {
          ceError = LwMapErrnoToLwError(errno);
          BAIL_ON_CENTERIS_ERROR(ceError);
       }

       result = read(fd, contents, sizeof(contents)-1);
       if (result < 0) {
          ceError = LwMapErrnoToLwError(errno);
          BAIL_ON_CENTERIS_ERROR(ceError);
       }
       else if (result > 0) {
          contents[result-1] = 0;
          result = atoi(contents);
       }

       if (result <= 0) {
          ceError = ERROR_PROC_NOT_FOUND;
          BAIL_ON_CENTERIS_ERROR(ceError);
       }

       pid = (pid_t) result;
       result = kill(pid, 0);
       if (!result)
       {
          // Verify that the peer process is the auth daemon
          ceError = CTMatchProgramToPID(pszProgramName, pid);
          BAIL_ON_CENTERIS_ERROR(ceError);

          if(pbRunning != NULL)
          {
              *pbRunning = TRUE;
          }
          if ( pPid ) {
            *pPid = pid;
          }
       }
       else if (errno == ESRCH) {

          ceError = ERROR_PROC_NOT_FOUND;
          BAIL_ON_CENTERIS_ERROR(ceError);

       } else {

          ceError = LwMapErrnoToLwError(errno);
          BAIL_ON_CENTERIS_ERROR(ceError);
       }

   }

error:

    if (fd != -1) {
        close(fd);
    }

    return (ceError == ERROR_PROC_NOT_FOUND ? ERROR_SUCCESS : ceError);
}