Example #1
0
void
LWRaise(
    LWException** dest, 
    DWORD code
    )
{
    DWORD ceError;
    char *shortMsg;
    char *longMsg;
    const char* desc = LwWin32ExtErrorToName(code);
    const char* help = LwWin32ExtErrorToDescription(code);

    if (!desc)
    {
        shortMsg = "Undocumented exception";
    }
    if ((ceError = CTAllocateString(desc, &shortMsg)))
    {
	*dest = CreateException(ceError, __FILE__, __LINE__, NULL, NULL);
	return;
    }

    if (!help)
    {
        longMsg = "An undocumented exception has occurred. Please contact Likewise technical support and use the error code to identify this exception.";
    }
    if ((ceError = CTAllocateString(help, &longMsg)))
    {
	*dest = CreateException(ceError, __FILE__, __LINE__, NULL, NULL);
	return;
    }
    
    *dest = CreateException(code, NULL, 0, shortMsg, longMsg);
}
Example #2
0
static
DWORD
SetAIXHostname(
    PSTR pszComputerName
    )
{
    DWORD ceError = ERROR_SUCCESS;
    PPROCINFO pProcInfo = NULL;
    PSTR* ppszArgs = NULL;
    DWORD nArgs = 6;
    CHAR  szBuf[512];
    LONG  status = 0;

    DJ_LOG_INFO("Setting hostname to [%s]", pszComputerName);

    ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, PPCAST(&ppszArgs));
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("chdev", ppszArgs);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("-a", ppszArgs+1);
    BAIL_ON_CENTERIS_ERROR(ceError);

    sprintf(szBuf, "hostname=%s", pszComputerName);
    ceError = CTAllocateString(szBuf, ppszArgs+2);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("-l", ppszArgs+3);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("inet0", ppszArgs+4);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = DJGetProcessStatus(pProcInfo, &status);
    BAIL_ON_CENTERIS_ERROR(ceError);

    if (status != 0) {
        ceError = ERROR_BAD_COMMAND;
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

error:

    if (ppszArgs)
        CTFreeStringArray(ppszArgs, nArgs);

    if (pProcInfo)
        FreeProcInfo(pProcInfo);

    return ceError;
}
Example #3
0
DWORD
CTGetCurrentDirectoryPath(
    PSTR* ppszPath
    )
{
    DWORD ceError = ERROR_SUCCESS;
    CHAR szBuf[PATH_MAX+1];
    PSTR pszPath = NULL;

    if (getcwd(szBuf, PATH_MAX) == NULL) {
        ceError = LwMapErrnoToLwError(errno);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    ceError = CTAllocateString(szBuf, &pszPath);
    BAIL_ON_CENTERIS_ERROR(ceError);

    *ppszPath = pszPath;

    return ceError;

error:

    if (pszPath) {
        CTFreeString(pszPath);
    }

    return ceError;
}
Example #4
0
// Currently unused
static
DWORD
DJGetDnsDomain(
    PSTR pszHostname,
    PSTR* ppszDnsDomain
    )
{
    DWORD ceError = ERROR_SUCCESS;
    PSTR pszDnsDomain = NULL;
    PSTR pszTmp = strchr(pszHostname, '.');

    if (pszTmp && *(pszTmp+1) != '\0') {
        ceError = CTAllocateString(pszTmp+1, &pszDnsDomain);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    *ppszDnsDomain = pszDnsDomain;

    return ceError;

error:

    if (pszDnsDomain)
        CTFreeString(pszDnsDomain);

    return ceError;
}
Example #5
0
DWORD
DJGetComputerName(
    PSTR* ppszComputerName
    )
{
    DWORD ceError = ERROR_SUCCESS;
    CHAR szBuf[256+1];
    PSTR pszTmp = NULL;

    if (gethostname(szBuf, 256) < 0) {
        ceError = LwMapErrnoToLwError(errno);
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    pszTmp = szBuf;
    while (*pszTmp != '\0') {
        if (*pszTmp == '.') {
            *pszTmp = '\0';
            break;
        }
        pszTmp++;
    }

    if (IsNullOrEmptyString(szBuf)) {
        ceError = ERROR_INVALID_COMPUTERNAME;
        BAIL_ON_CENTERIS_ERROR(ceError);
    }

    ceError = CTAllocateString(szBuf, ppszComputerName);
    BAIL_ON_CENTERIS_ERROR(ceError);

error:

    return ceError;
}
Example #6
0
DWORD
DJIsAppleADPluginInUse(BOOLEAN* pExists)
{
    DWORD ceError = ERROR_SUCCESS;
    PPROCINFO pProcInfo = NULL;
    PSTR* ppszArgs = NULL;
    DWORD nArgs = 7;
    LONG status = 0;
    BOOLEAN bInUse = FALSE;

    DJ_LOG_INFO("Testing to see if Apple AD plugin is already in use");

    ceError = CTAllocateMemory(sizeof(PSTR)*nArgs, (PVOID*)(PVOID)&ppszArgs);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("/usr/bin/dscl", ppszArgs);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("localhost", ppszArgs+1);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("-list", ppszArgs+2);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = CTAllocateString("|", ppszArgs+3);
    BAIL_ON_CENTERIS_ERROR(ceError);

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

    ceError = CTAllocateString(APPLEADDSPLUGIN_NAME, ppszArgs+5);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = DJSpawnProcess(ppszArgs[0], ppszArgs, &pProcInfo);
    BAIL_ON_CENTERIS_ERROR(ceError);

    ceError = DJGetProcessStatus(pProcInfo, &status);
    BAIL_ON_CENTERIS_ERROR(ceError);

    if (status == 0)
    {
        bInUse = TRUE;
    }

error:

    if (ppszArgs)
    {
       CTFreeStringArray(ppszArgs, nArgs);
    }

    if (pProcInfo)
    {
       FreeProcInfo(pProcInfo);
    }

    *pExists = bInUse;

    return ceError;
}
Example #7
0
void
DJQuery(
    char **computer, 
    char **domain,
    DJOptions* options,
    LWException** exc
    )
{
    PDOMAINJOININFO info = NULL;

    LW_TRY(exc, QueryInformation(&info, &LW_EXC));
    
    if (info->pszName)
    {
	LW_CLEANUP_CTERR(exc,
			 CTAllocateString(info->pszName, computer));
    }
    else
    {
	*computer = NULL;
    }

    if (info->pszDomainName)
    {
	LW_CLEANUP_CTERR(exc,
			 CTAllocateString(info->pszDomainName, domain));
    }
    else
    {
	*domain = NULL;
    }
    
cleanup:
    
    if (info)
    {
	FreeDomainJoinInfo(info);
    }
}
Example #8
0
//Adds an alias to the head of the alias list
static
DWORD
DJAddAlias(
    PHOSTSFILELINE pLine,
    PCSTR pszName,
    BOOLEAN alwaysAdd,
    PBOOLEAN pbAdded
    )
{
    DWORD ceError = ERROR_SUCCESS;
    PHOSTFILEALIAS pAlias = NULL;

    if (alwaysAdd || !DJEntryHasAlias(pLine->pEntry->pAliasList, pszName)) {

        ceError = CTAllocateMemory(sizeof(HOSTFILEALIAS), (PVOID*)(PVOID)&pAlias);
        BAIL_ON_CENTERIS_ERROR(ceError);

        ceError = CTAllocateString(pszName, &pAlias->pszAlias);
        BAIL_ON_CENTERIS_ERROR(ceError);

        pAlias->pNext = pLine->pEntry->pAliasList;
        pLine->pEntry->pAliasList = pAlias;

        pAlias = NULL;

        if (pbAdded != NULL)
            *pbAdded = TRUE;

        pLine->bModified = TRUE;

    } else if (pbAdded != NULL) {

        *pbAdded = FALSE;

    }

error:

    if (pAlias)
        DJFreeAlias(pAlias);

    return ceError;
}
Example #9
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;
}
Example #10
0
static
void
FixNetworkInterfaces(
    PSTR pszComputerName,
    LWException **exc
    )
{
    DWORD ceError = ERROR_SUCCESS;
    int EE = 0;
    BOOLEAN bFileExists = FALSE;
    BOOLEAN bDirExists = FALSE;
    PSTR* ppszPaths = NULL;
    DWORD nPaths = 0;
    DWORD iPath = 0;
    CHAR szBuf[1024];
    PSTR pszPathifcfg = NULL;
    BOOLEAN bDHCPHost = FALSE;
    PSTR pszMachineSID = NULL;
    PCSTR networkConfigPath = "/etc/sysconfig/network";

    LW_CLEANUP_CTERR(exc, DJGetMachineSID(&pszMachineSID));

    /*
     * fixup HOSTNAME variable in /etc/sysconfig/network file if it exists
     * note that 'network' is a *directory* on some dists (ie SUSE),
     * is a *file* on others (ie Redhat). weird.
     */
    LW_CLEANUP_CTERR(exc, CTCheckFileExists(networkConfigPath, &bFileExists));

    if (bFileExists) {
        sprintf(szBuf, "s/^.*\\(HOSTNAME\\).*=.*$/\\1=%s/", pszComputerName);
        LW_CLEANUP_CTERR(exc, CTRunSedOnFile(networkConfigPath, networkConfigPath,
                FALSE, szBuf));
    }

    LW_CLEANUP_CTERR(exc, CTCheckDirectoryExists("/etc/sysconfig/network", &bDirExists));
    if (!bDirExists)
    {
        LW_CLEANUP_CTERR(exc, CTCheckDirectoryExists("/etc/sysconfig/network-scripts", &bDirExists));
    }

    if (bDirExists) {

        struct
        {
            PCSTR dir;
            PCSTR glob;
        } const searchPaths[] = {
            {"/etc/sysconfig/network", "ifcfg-eth-id-[^.]*$"},
            {"/etc/sysconfig/network", "ifcfg-eth0[^.]*$"},
            {"/etc/sysconfig/network", "ifcfg-eth[^.]*$"},
            {"/etc/sysconfig/network", "ifcfg-eth-bus[^.]*$"},
            //SLES 10.1 on zSeries uses one of:
            //  /etc/sysconfig/network/ifcfg-qeth-bus-ccw-0.0.0500
            //  /etc/sysconfig/network/ifcfg-ctc-bus-ccw-0.0.0004
            {"/etc/sysconfig/network", "ifcfg-qeth-bus.*\\.[0-9]\\+$"},
            {"/etc/sysconfig/network", "ifcfg-ctc-bus.*\\.[0-9]\\+$"},
            // Redhat uses /etc/sysconfig/network-scripts/ifcfg-eth<number>
            {"/etc/sysconfig/network-scripts", "ifcfg-eth[^.]*$"},
            // RHEL 6 uses this
            {"/etc/sysconfig/network-scripts", "ifcfg-Auto_eth[^.]*$"},
            // ESX 3.5 and 4.0 use
            // /etc/sysconfig/network-scripts/ifcfg-vswif<number>
            {"/etc/sysconfig/network-scripts", "ifcfg-vswif[^.]*$"},
            // RHEL 7: network interface naming seems to be ensXX or enoXXXX, etc.
            {"/etc/sysconfig/network-scripts", "ifcfg-en[^.]*$"},
            {NULL, NULL}
        };

        // Find the ifcfg file
        pszPathifcfg = NULL;

        for(iPath = 0; searchPaths[iPath].dir != NULL && pszPathifcfg == NULL; iPath++)
        {
            if (ppszPaths)
            {
                CTFreeStringArray(ppszPaths, nPaths);
                ppszPaths = NULL;
            }

            ceError = CTGetMatchingFilePathsInFolder(searchPaths[iPath].dir,
                                                         searchPaths[iPath].glob,
                                                         &ppszPaths,
                                                         &nPaths);
            if(ceError == ERROR_DIRECTORY)
            {
                ceError = ERROR_SUCCESS;
                continue;
            }
            LW_CLEANUP_CTERR(exc, ceError);

            if(nPaths > 0)
            {
                LW_CLEANUP_CTERR(exc, CTAllocateString(ppszPaths[0], &pszPathifcfg));
            }
        }

        if (IsNullOrEmptyString(pszPathifcfg)) {
            LW_CLEANUP_CTERR(exc, ERROR_FILE_NOT_FOUND);
        }

        DJ_LOG_INFO("Found ifcfg file at %s", pszPathifcfg);

        LW_CLEANUP_CTERR(exc, DJCheckIfDHCPHost(pszPathifcfg, &bDHCPHost));

        if (bDHCPHost) {
            LW_CLEANUP_CTERR(exc, DJFixDHCPHost(pszPathifcfg, pszComputerName));
        }
    }

    ceError = CTShell("/bin/hostname %hostname >/dev/null",
            CTSHELL_STRING(hostname, pszComputerName));

    LW_CLEANUP_CTERR(exc, ceError);

    // Only DHCP boxes need to restart their networks
    if (bDHCPHost) {
        LW_CLEANUP_CTERR(exc, DJConfigureDHCPService(pszComputerName));
    }

cleanup:

    // This ensures that we do not change the SID after a machine name
    // change.  The issue here is that Samba implements its SAM such that
    // a machine name change changes the seeding used for the machine SID.
    // Therefore, we must re-store the old SID with the new machine name
    // seed.
    if (pszMachineSID) {
        if (*pszMachineSID != '\0')
            DJSetMachineSID(pszMachineSID);
        CTFreeString(pszMachineSID);
    }

    if (ppszPaths)
        CTFreeStringArray(ppszPaths, nPaths);

    if (pszPathifcfg)
        CTFreeString(pszPathifcfg);

    DJ_LOG_VERBOSE("FixNetworkInterfaces LEAVE -> 0x%08x (EE = %d)", ceError, EE);
}
Example #11
0
/* 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;
}
Example #12
0
void
DJSetComputerName(
    PCSTR pszComputerName,
    PCSTR pszDnsDomainName,
    LWException **exc
    )
{
    DWORD ceError = ERROR_SUCCESS;
    BOOLEAN bValidComputerName = FALSE;
    PSTR oldShortHostname = NULL;
    PSTR oldFqdnHostname = NULL;
    PSTR pszComputerName_lower = NULL;
    PSTR pNewFqdnHostname = NULL;
    PSTR ppszHostfilePaths[] = { "/etc/hostname", "/etc/HOSTNAME", NULL };
    LwDistroInfo distro;

    memset(&distro, 0, sizeof(distro));

    LW_CLEANUP_CTERR(exc, DJGetDistroInfo(NULL, &distro));

    LW_CLEANUP_CTERR(exc, DJIsValidComputerName(pszComputerName, &bValidComputerName));

    if (!bValidComputerName) {
        LW_CLEANUP_CTERR(exc, ERROR_INVALID_COMPUTERNAME);
    }

    LW_CLEANUP_CTERR(exc, CTAllocateString(pszComputerName, &pszComputerName_lower));

    CTStrToLower(pszComputerName_lower);

    /* Start spelunking for various hostname holding things. Rather
       than trying to worry about what flavor of linux we are
       running, we look for various files and fix them up if they
       exist. That way we dont end up with a huge wad of repeated
       code for each linux flavor.

       change the repositories of the 'HOSTNAME' variable.
       it's a string in /etc/HOSTNAME for some dists, it's a variable in
       /etc/sysconfig/network for others

       fixup HOSTNAME file if it exists
       Ubuntu/Debian have /etc/hostname, so add that...
    */

    LW_CLEANUP_CTERR(exc, WriteHostnameToFiles(pszComputerName_lower,
                                   ppszHostfilePaths));

    // insert/correct the new hostname in /etc/hosts - note that this
    // has to be done *before* we update the running hostname because
    // we call hostname to get the current hostname so that we can
    // find it and replace it.
    LW_CLEANUP_CTERR(exc, DJGetFQDN(&oldShortHostname, &oldFqdnHostname));

    //Don't replace localhost in /etc/hosts, always add our new hostname instead
    if(oldFqdnHostname != NULL && !strcmp(oldFqdnHostname, "localhost"))
    {
        CTFreeString(oldFqdnHostname);
        oldFqdnHostname = NULL;
    }
    if(oldShortHostname != NULL && !strcmp(oldShortHostname, "localhost"))
    {
        CTFreeString(oldShortHostname);
        oldShortHostname = NULL;
    }

    if (pszDnsDomainName[0])
    {
        ceError = LwAllocateStringPrintf(
                    &pNewFqdnHostname,
                    "%s.%s",
                    pszComputerName,
                    pszDnsDomainName);
        LW_CLEANUP_CTERR(exc, ceError);
    }
    else
    {
        ceError = LwAllocateStringPrintf(
                    &pNewFqdnHostname,
                    "%s",
                    pszComputerName);
        LW_CLEANUP_CTERR(exc, ceError);
    }

    ceError = DJCopyMissingHostsEntry("/etc/inet/ipnodes", "/etc/hosts",
            pszComputerName_lower, oldShortHostname);
    if(ceError == ERROR_FILE_NOT_FOUND)
        ceError = ERROR_SUCCESS;
    LW_CLEANUP_CTERR(exc, ceError);

    LW_CLEANUP_CTERR(exc, DJReplaceNameInHostsFile("/etc/hosts",
            oldShortHostname, oldFqdnHostname,
            pszComputerName_lower, pszDnsDomainName));

    ceError = DJReplaceNameInHostsFile("/etc/inet/ipnodes",
            oldShortHostname, oldFqdnHostname,
            pszComputerName_lower, pszDnsDomainName);
    if(ceError == ERROR_FILE_NOT_FOUND)
        ceError = ERROR_SUCCESS;
    LW_CLEANUP_CTERR(exc, ceError);

    switch (distro.os)
    {
        case OS_SUNOS:
            LW_CLEANUP_CTERR(exc, WriteHostnameToSunFiles(
                        oldShortHostname,
                        pszComputerName_lower,
                        pszDnsDomainName,
                        oldFqdnHostname,
                        pNewFqdnHostname
                        ));
            break;
        case OS_AIX:
            LW_CLEANUP_CTERR(exc, SetAIXHostname(pszComputerName_lower));
            break;
        case OS_HPUX:
            LW_CLEANUP_CTERR(exc, SetHPUXHostname(pszComputerName_lower));
            break;
        case OS_DARWIN:
            LW_CLEANUP_CTERR(exc, SetMacOsXHostName(pszComputerName_lower));
            break;
        default:
            break;
    }

    LW_TRY(exc, FixNetworkInterfaces(pszComputerName_lower, &LW_EXC));

cleanup:
    CT_SAFE_FREE_STRING(oldShortHostname);
    CT_SAFE_FREE_STRING(oldFqdnHostname);
    CT_SAFE_FREE_STRING(pszComputerName_lower);
    CT_SAFE_FREE_STRING(pNewFqdnHostname);
    DJFreeDistroInfo(&distro);
}
Example #13
0
DWORD
DJGetFQDN(
    PSTR *shortName,
    PSTR *fqdn
    )
{
    DWORD ceError = ERROR_SUCCESS;
    PSTR _shortName = NULL;
    PSTR _fqdn = NULL;
    size_t i;
    struct hostent* pHostent = NULL;

    if(shortName != NULL)
        *shortName = NULL;
    if(fqdn != NULL)
        *fqdn = NULL;

    ceError = DJGetComputerName(&_shortName);
    CLEANUP_ON_DWORD(ceError);

    //We have the short hostname that the hostname command returns, now we're
    //going to get the long hostname. This is the same as 'hostname -f' on
    //systems which support it.
    //Try to look it up upto 3 times
    for(i = 0; i < 3; i++)
    {
        PSTR foundFqdn = NULL;
        pHostent = gethostbyname(_shortName);
        if (pHostent == NULL) {
            if (h_errno == TRY_AGAIN) {
                sleep(1);
                continue;
            }
            break;
        }
        /*
         * We look for the first name that looks like an FQDN.  This is
         * the same heuristics used by other software such as Kerberos and
         * Samba.
         */
        if (strchr(pHostent->h_name, '.') != 0)
        {
            foundFqdn = pHostent->h_name;
        }
        else
        {
            for (i = 0; pHostent->h_aliases[i]; i++)
            {
                if (strchr(pHostent->h_aliases[i], '.') != 0)
                {
                    foundFqdn = pHostent->h_aliases[i];
                    break;
                }
            }
       }
        /* If we still have nothing, just return the first name */
        if (!foundFqdn)
        {
            foundFqdn = pHostent->h_name;
        }
        ceError = CTAllocateString(foundFqdn, &_fqdn);
        CLEANUP_ON_DWORD(ceError);
        break;
    }

    if(shortName != NULL)
    {
        *shortName = _shortName;
        _shortName = NULL;
    }
    if(fqdn != NULL)
    {
        *fqdn = _fqdn;
        _fqdn = NULL;
    }

cleanup:
    CT_SAFE_FREE_STRING(_fqdn);
    CT_SAFE_FREE_STRING(_shortName);
    return ceError;
}
Example #14
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;
}
Example #15
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;
}
Example #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;
}
Example #17
0
void
LWRaiseEx(
    LWException** dest,
    DWORD code,
    const char* file,
    unsigned int line,
    const char* _shortMsg,
    const char* fmt,
    ...
    )
{
    if (dest)
    {
	DWORD ceError;
	char* shortMsg;
	char* longMsg;
	va_list ap;
	
	va_start(ap, fmt);
	
	if (!_shortMsg)
	{
	    _shortMsg = LwWin32ExtErrorToName(code);
	}
        if (!_shortMsg)
        {
            _shortMsg = "Undocumented exception";
        }

	if (!fmt)
	{
	    fmt = LwWin32ExtErrorToDescription(code);
	}
        if (!fmt)
        {
            fmt = "An undocumented exception has occurred. Please contact Likewise technical support and use the error code to identify this exception.";
        }

	if (_shortMsg)
	{
	    if ((ceError = CTAllocateString(_shortMsg, &shortMsg)))
	    {
		*dest = CreateException(ceError, __FILE__, __LINE__, NULL, NULL);
		return;
	    }
	}
	else
	{
	    shortMsg = NULL;
	}
	    
	if (fmt)
	{
	    if ((ceError = CTAllocateStringPrintfV(&longMsg, fmt, ap)))
	    {
		CTFreeString(shortMsg);
		*dest = CreateException(ceError, __FILE__, __LINE__, NULL, NULL);
		return;
	    }
	}
	else
	{
	    longMsg = NULL;
	}

	*dest = CreateException(code, file, line, shortMsg, longMsg);
    }
}
Example #18
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;
}
Example #19
0
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;
}
Example #20
0
/* 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;
}