Exemple #1
0
static
DWORD
VmDnsSockPosixSetDescriptorNonBlocking(
    int fd
    )
{
    DWORD dwError = 0;
    int flags = 0;

    if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }

    flags |= O_NONBLOCK;

    if ((flags = fcntl(fd, F_SETFL, flags)) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }

error:

    return dwError;
}
Exemple #2
0
DWORD
LWNetSrvStartNetBiosThread(
    VOID)
{
    DWORD dwError = 0;
    pthread_t thread;
    pthread_attr_t attrib;

    gpNbCtx->udpTimeout = LWNetConfigIsNetBiosUdpTimeout();

    dwError = LwErrnoToWin32Error(pthread_attr_init(&attrib));
    BAIL_ON_LWNET_ERROR(dwError);

    dwError = LwErrnoToWin32Error(pthread_attr_setdetachstate(
                  &attrib, PTHREAD_CREATE_DETACHED));
    BAIL_ON_LWNET_ERROR(dwError);
    dwError = LwErrnoToWin32Error(pthread_create(&thread,
                         &attrib,
                         LWNetSrvStartNetBiosThreadRoutine,
                         (void *) gpNbCtx));
    BAIL_ON_LWNET_ERROR(dwError);

cleanup:
    return dwError;
error:
    goto cleanup;
}
Exemple #3
0
DWORD
VmwDeployWriteToFile(
    PCSTR pszContent,
    PCSTR pszDirPath,
    PCSTR pszFilename
    )
{
    DWORD dwError = 0;
    PSTR  pszPath = NULL;
    FILE* fp = NULL;
    size_t nWritten = 0;
    size_t nToWrite = 0;

    if (IsNullOrEmptyString(pszContent) ||
        IsNullOrEmptyString(pszDirPath) ||
        IsNullOrEmptyString(pszFilename))
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_DEPLOY_ERROR(dwError);
    }

    dwError = VmwDeployAllocateStringPrintf(
                    &pszPath,
                    "%s/%s",
                    pszDirPath,
                    pszFilename);
    BAIL_ON_DEPLOY_ERROR(dwError);

    if ((fp = fopen(pszPath, "w")) == NULL)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_DEPLOY_ERROR(dwError);
    }

    nToWrite = strlen(pszContent);

    nWritten = fwrite(pszContent, 1, nToWrite, fp);
    if (nWritten != nToWrite)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_DEPLOY_ERROR(dwError);
    }

cleanup:

    if (fp)
    {
        fclose(fp);
    }
    if (pszPath)
    {
        VmwDeployFreeMemory(pszPath);
    }

    return dwError;

error:

    goto cleanup;
}
DWORD
VmAfdWriteDataImpl(
	PVM_AFD_CONNECTION pConnection,
	PBYTE pRequest,
	DWORD dwRequestSize
)
{
	DWORD dwError = 0;
	DWORD dwBytesWritten = 0;
        DWORD dwTotalBytesWritten = 0;
        PBYTE pRequestCursor = pRequest;
        DWORD dwActualRequestSize = dwRequestSize;

        do
        {
                dwBytesWritten = write(pConnection->fd, (PVOID)&dwRequestSize, sizeof (DWORD));
        }while (dwBytesWritten == -1 && errno == EINTR);


        if (dwBytesWritten < sizeof (DWORD))
        {
                dwError = LwErrnoToWin32Error(errno);
                BAIL_ON_VMAFD_ERROR (dwError);
        }

        dwBytesWritten = 0;

        while (dwRequestSize > 0)
        {
                DWORD dwBytesToWrite = VMAFD_IPC_PACKET_SIZE<dwRequestSize?VMAFD_IPC_PACKET_SIZE:dwRequestSize;

                do
                {
                    dwBytesWritten = write(pConnection->fd,pRequestCursor,dwBytesToWrite);
                }while (dwBytesWritten == -1 && errno == EINTR);

                if (dwBytesWritten < 0)
                {
                        dwError = LwErrnoToWin32Error(errno);
                        BAIL_ON_VMAFD_ERROR(dwError);
                }

                dwTotalBytesWritten += dwBytesWritten;
                pRequestCursor += dwBytesWritten;
                dwRequestSize -= dwBytesWritten;
	}
	if (dwActualRequestSize != dwTotalBytesWritten){
		dwError = ERROR_IO_INCOMPLETE;
		BAIL_ON_VMAFD_ERROR(dwError);
	}
cleanup:
	return dwError;
error:
	goto cleanup;
}
DWORD
VmAfdOpenServerConnectionImpl(
	PVM_AFD_CONNECTION *ppConnection
	)
{
	DWORD dwError = 0;
	int socket_fd = -1, on = 1;
	struct sockaddr_un address = {0};
	PVM_AFD_CONNECTION pConnection = NULL;

	socket_fd = socket(PF_UNIX,SOCK_STREAM, 0);

	if (socket_fd < 0){
		dwError = LwErrnoToWin32Error(errno);
		BAIL_ON_VMAFD_ERROR(dwError);
	}

	if( setsockopt( socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0){
		dwError = LwErrnoToWin32Error(errno);
		BAIL_ON_VMAFD_ERROR(dwError);
	}

	unlink (SOCKET_FILE_PATH);

	address.sun_family = AF_UNIX;
	snprintf (address.sun_path, sizeof(SOCKET_FILE_PATH), SOCKET_FILE_PATH);

	if (bind (socket_fd, (struct sockaddr *)&address, sizeof(struct sockaddr_un)) < 0){
		dwError = LwErrnoToWin32Error(errno);
		BAIL_ON_VMAFD_ERROR(dwError);
	}

	if (listen (socket_fd, 5) < 0){
		dwError = LwErrnoToWin32Error(errno);
		BAIL_ON_VMAFD_ERROR(dwError);
	}

	dwError = VmAfdAllocateMemory(sizeof(VM_AFD_CONNECTION), (PVOID *)&pConnection);
	BAIL_ON_VMAFD_ERROR(dwError);
	pConnection->fd = socket_fd;
	*ppConnection = pConnection;

cleanup:
	return dwError;
error:
	if (ppConnection != NULL){
		*ppConnection = NULL;
	}
	if (socket_fd >=0 ){
		close (socket_fd);
	}
	VMAFD_SAFE_FREE_MEMORY (pConnection);
	goto cleanup;
}
DWORD
VmAfdOpenClientConnectionImpl(
	PVM_AFD_CONNECTION *ppConnection
	)
{
	DWORD dwError = 0;
	int socket_fd = 0;
	struct sockaddr_un address;
	PVM_AFD_CONNECTION pConnection = NULL;

	socket_fd = socket (PF_UNIX, SOCK_STREAM, 0);
	if (socket_fd < 0){
		dwError = LwErrnoToWin32Error(errno);
		BAIL_ON_VMAFD_ERROR(dwError);
	}
	memset (&address, 0, sizeof(struct sockaddr_un));
	address.sun_family = AF_UNIX;
	snprintf (address.sun_path, sizeof(SOCKET_FILE_PATH), SOCKET_FILE_PATH);

	if (connect(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) <0)
    {
        if (errno == ENOENT || errno == ECONNREFUSED)
        {
            dwError = ERROR_CANNOT_CONNECT_VMAFD;
        }
        else if (errno == EACCES)
        {
            dwError = ERROR_ACCESS_DENIED;
        }
        else
        {
            dwError = LwErrnoToWin32Error (errno);
        }
        BAIL_ON_VMAFD_ERROR(dwError);
    }

	dwError = VmAfdAllocateMemory (sizeof(VM_AFD_CONNECTION),(PVOID *)&pConnection);
	BAIL_ON_VMAFD_ERROR(dwError);
	pConnection->fd = socket_fd;
	*ppConnection = pConnection;

cleanup:
	return dwError;
error:
	if (ppConnection != NULL){
		*ppConnection = NULL;
	}
	if (socket_fd >= 0){
		close (socket_fd);
	}
	VMAFD_SAFE_FREE_MEMORY(pConnection);
	goto cleanup;
}
Exemple #7
0
DWORD
VmDnsSockPosixSetTimeOut(
    PVM_SOCKET pSocket,
    DWORD      dwTimeOut
    )
{
    DWORD dwError = 0;
    BOOLEAN bLocked = FALSE;

    if (!pSocket)
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }

    if (dwTimeOut)
    {
        struct timeval tv = {0};
        dwError = VmDnsLockMutex(pSocket->pMutex);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);

        bLocked = TRUE;

        tv.tv_sec = dwTimeOut;
        if (setsockopt(pSocket->fd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
        {
            dwError = LwErrnoToWin32Error(errno);
            BAIL_ON_POSIX_SOCK_ERROR(dwError);
        }

        if (setsockopt(pSocket->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
        {
            dwError = LwErrnoToWin32Error(errno);
            BAIL_ON_POSIX_SOCK_ERROR(dwError);
        }
    }

cleanup:

    if (bLocked)
    {
        VmDnsUnlockMutex(pSocket->pMutex);
    }
    return dwError;

error:

    goto cleanup;
}
DWORD
VmAfdGenRandomImpl (
        PDWORD pdwRandomNumber
        )
{
    DWORD dwError = 0;
    FILE *fpUrandom = NULL;

    DWORD dwRandomNumber = 0;
    DWORD dwBytesRead = 0;

    fpUrandom = fopen("/dev/urandom", "r");

    if (!fpUrandom)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_VMAFD_ERROR (dwError);
    }

    dwBytesRead = fread(
                        (VOID *)&dwRandomNumber,
                        sizeof (DWORD),
                        1,
                        fpUrandom
                       );
    if (dwBytesRead < sizeof (DWORD) &&
        !feof (fpUrandom)
       )
    {
        dwError = LwErrnoToWin32Error (ferror(fpUrandom));
        BAIL_ON_VMAFD_ERROR (dwError);
    }

    *pdwRandomNumber = dwRandomNumber;

cleanup:

    if (fpUrandom)
    {
        fclose(fpUrandom);
    }
    return dwError;

error:
    *pdwRandomNumber = 0;

    goto cleanup;
}
Exemple #9
0
static
DWORD
VmDnsSockPosixEventQueueRearm_inlock(
    PVM_SOCK_EVENT_QUEUE pQueue,
    BOOL                 bOneShot,
    PVM_SOCKET           pSocket
    )
{
    DWORD dwError = 0;
    struct epoll_event event = {0};

    event.data.ptr = pSocket;
    event.events = EPOLLIN;
    if (bOneShot)
    {
       event.events = event.events | EPOLLONESHOT;
    }

    if (pSocket->bInEventQueue == TRUE &&
        epoll_ctl(pQueue->epollFd, EPOLL_CTL_MOD, pSocket->fd, &event) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }

error:

    return dwError;
}
Exemple #10
0
DWORD
VmDnsRpcServerStartListen(
    VOID
    )
{
    DWORD dwError = 0;
    int status = 0;

    status = dcethread_create(
                            &gVmdnsGlobals.pRPCServerThread,
                            NULL,
                            VmDnsListenRpcServer,
                            NULL);
#ifndef _WIN32
    dwError = LwErrnoToWin32Error(status);
#else
    dwError = status;
#endif

    BAIL_ON_VMDNS_ERROR(dwError);

    while (!VmDnsRpcCheckServerIsActive())
    {
        // Wait for RPC Server to come up.
    }

error:

    return dwError;
}
Exemple #11
0
static
DWORD
ReadPassword(
    PSTR* ppszPassword
    )
{
    DWORD dwError = ERROR_SUCCESS;
    struct termios orig, nonecho;
    sigset_t sig, osig;
    CHAR  szPassword[33] = "";
    PSTR  pszPassword = NULL;

    fprintf(stdout, "Password: "******"\n")] = '\0';

    if (IsNullOrEmptyString(szPassword))
    {
        dwError = ERROR_PASSWORD_RESTRICTION;
        BAIL_ON_VMAFD_ERROR(dwError);
    }

    dwError = VmAfdAllocateStringA(szPassword, &pszPassword);
    BAIL_ON_VMAFD_ERROR(dwError);

    *ppszPassword = pszPassword;

cleanup:

    tcsetattr(0, TCSANOW, &orig);
    sigprocmask(SIG_SETMASK,&osig,NULL);

    return dwError;

error:

    *ppszPassword = NULL;

    goto cleanup;
}
Exemple #12
0
DWORD
VmwDeployGetHostname(
    PSTR* ppszHostname
    )
{
    DWORD dwError = 0;
    CHAR  szHostname[HOST_NAME_MAX + 1] = "";
    PSTR  pszHostname = NULL;

    if (gethostname(szHostname, sizeof(szHostname)-1) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_DEPLOY_ERROR(dwError);
    }

    dwError = VmwDeployAllocateStringA(szHostname, &pszHostname);
    BAIL_ON_DEPLOY_ERROR(dwError);

    *ppszHostname = pszHostname;

cleanup:

    return dwError;

error:

    *ppszHostname = NULL;

    goto cleanup;
}
Exemple #13
0
static
DWORD
LwSmControlLock(
    VOID
    )
{
    DWORD dwError = ERROR_SUCCESS;
    struct flock lock = {0};

    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    lock.l_pid = getpid();

    if ((gState.ControlLock = open(CONTROL_LOCK, O_WRONLY | O_CREAT | O_TRUNC, 0200)) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_ERROR(dwError);
    }

    if (fcntl(gState.ControlLock, F_SETFD, FD_CLOEXEC) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_ERROR(dwError);
    }

    if (fcntl(gState.ControlLock, F_SETLK, &lock) < 0)
    {
        switch(errno)
        {
        case EACCES:
        case EAGAIN:
            dwError = ERROR_SERVICE_ALREADY_RUNNING;
            break;
        default:
            dwError = LwErrnoToWin32Error(errno);
            break;
        }

        BAIL_ON_ERROR(dwError);
    }

error:

    return dwError;
}
Exemple #14
0
static
DWORD
GetPassword(
    PSTR *ppszPassword
    )
{
    CHAR pszPasswordBuff[100] = {0};
    PSTR pszPassword = NULL;
    DWORD dwError = 0;
    struct termios tp, save;

    fflush(stdout);

    tcgetattr(0, &tp) ;
    memcpy (&save, &tp, sizeof (struct termios));
    save.c_lflag &= ~ECHO;                /* ECHO off, other bits unchanged */
    tcsetattr(0, TCSANOW, &save);

    if (!fgets(pszPasswordBuff, 100, stdin) && ferror(stdin))
    {
        dwError = LwErrnoToWin32Error(ferror(stdin));
        BAIL_ON_VMAFD_ERROR (dwError);
    }

    if (pszPasswordBuff[strlen(pszPasswordBuff)-1] == '\n')
    {
        pszPasswordBuff[strlen(pszPasswordBuff)-1] = '\0';
    }

    dwError = VmAfdAllocateStringPrintf(
                                        &pszPassword,
                                        "%s",
                                        pszPasswordBuff
                                       );
    BAIL_ON_VMAFD_ERROR (dwError);

    *ppszPassword = pszPassword;

cleanup:

    tcsetattr(0, TCSANOW, &tp);

    fflush (stdin);

    return dwError;

error:
    if (ppszPassword)
    {
        *ppszPassword = NULL;
    }

    VMAFD_SAFE_FREE_MEMORY (pszPassword);

    goto cleanup;
}
Exemple #15
0
static
DWORD
VmDnsSockPosixAcceptConnection(
    PVM_SOCKET  pListener,
    PVM_SOCKET* ppSocket
    )
{
    DWORD dwError = 0;
    PVM_SOCKET pSocket = NULL;
    int fd = -1;

    dwError = VmDnsAllocateMemory(sizeof(*pSocket), (PVOID*)&pSocket);
    BAIL_ON_POSIX_SOCK_ERROR(dwError);

    pSocket->refCount = 1;

    dwError = VmDnsAllocateMutex(&pSocket->pMutex);
    BAIL_ON_POSIX_SOCK_ERROR(dwError);

    pSocket->protocol = pListener->protocol;
    pSocket->type = VM_SOCK_TYPE_SERVER;

    fd = accept(pListener->fd, &pSocket->addr, &pSocket->addrLen);
    if (fd < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }


    pSocket->fd = fd;

    pSocket->pAddr = &pSocket->addr;

    *ppSocket = pSocket;

cleanup:

    return dwError;

error:

    *ppSocket = NULL;

    if (pSocket)
    {
        VmDnsSockPosixFreeSocket(pSocket);
    }
    if (fd >= 0)
    {
        close(fd);
    }

    goto cleanup;
}
Exemple #16
0
static
DWORD
GetError(
    VOID
    )
{
#if defined _WIN32
        return GetLastError();
#else
        return LwErrnoToWin32Error(errno);
#endif
}
Exemple #17
0
static
DWORD
VmDirNotifyLikewiseServiceManager(
    VOID
    )
{
    DWORD dwError = ERROR_SUCCESS;
    PCSTR   pszSmNotify = NULL;
    int  ret = 0;
    int  notifyFd = -1;
    char notifyCode = 0;

    // interact with likewise service manager (start/stop control)
    if ((pszSmNotify = getenv("LIKEWISE_SM_NOTIFY")) != NULL)
    {
        notifyFd = atoi(pszSmNotify);

        do
        {
            ret = write(notifyFd, &notifyCode, sizeof(notifyCode));

        } while (ret != sizeof(notifyCode) && errno == EINTR);

        if (ret < 0)
        {
#define BUFFER_SIZE 1024
            char buffer[BUFFER_SIZE]= {0};
            int errorNumber = errno;

            VmDirStringErrorA( buffer, BUFFER_SIZE, errorNumber );
            VMDIR_LOG_ERROR( VMDIR_LOG_MASK_ALL,
                      "Could not notify service manager: %s (%i)",
                      buffer,
                      errorNumber);

            dwError = LwErrnoToWin32Error(errno);
            BAIL_ON_VMDIR_ERROR(dwError);
#undef BUFFER_SIZE
        }
    }

error:
    if(notifyFd != -1)
    {
        close(notifyFd);
    }

    return dwError;
}
Exemple #18
0
static
DWORD
VMCASrvCreateDirSyncParams(
    DWORD dwInterval,
    PVMCA_DIR_SYNC_PARAMS* ppSyncInfo
    )
{
    DWORD dwError = 0;
    PVMCA_DIR_SYNC_PARAMS pSyncInfo = NULL;

    dwError = VMCAAllocateMemory(
                    sizeof(VMCA_DIR_SYNC_PARAMS),
                    (PVOID*)&pSyncInfo);
    BAIL_ON_VMCA_ERROR(dwError);

    pSyncInfo->refCount = 1;

    dwError = pthread_mutex_init(&pSyncInfo->mutex, NULL);
    if (dwError)
    {
#ifndef _WIN32
        dwError = LwErrnoToWin32Error(dwError);
#endif
        BAIL_ON_VMCA_ERROR(dwError);
    }

    pSyncInfo->pMutex = &pSyncInfo->mutex;

    pSyncInfo->dwSyncIntervalSecs = dwInterval;

    *ppSyncInfo = pSyncInfo;

cleanup:

    return dwError;

error:

    *ppSyncInfo = NULL;

    if (pSyncInfo)
    {
        VMCASrvReleaseDirSyncParams(pSyncInfo);
    }

    goto cleanup;
}
Exemple #19
0
DWORD
VmLinuxWaitOnEvent(
    VMNETEVENT_FD FD,
    PFN_VMNETEVENT_CALLBACK pCallback,
    pthread_t* pEventThread
    )
{
    DWORD dwError = 0;
    PVMNETEVENT_DATA pData = NULL;
    pthread_t eventWorkerThread;

    if (!pEventThread)
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_VMNETEVENT_ERROR(dwError);
    }

    dwError = VmAfdAllocateMemory(
                            sizeof(VMNETEVENT_DATA),
                            (PVOID*)&pData
                            );
    BAIL_ON_VMNETEVENT_ERROR(dwError);

    pData->eventFd = FD;
    pData->pfnCallBack = pCallback;

    dwError = pthread_create(
                        &eventWorkerThread,
                        NULL,
                        VmLinuxWaitOnEventWorker,
                        (PVOID)pData
                        );
    if (dwError)
    {
        dwError = LwErrnoToWin32Error(dwError);
        BAIL_ON_VMNETEVENT_ERROR(dwError);
    }

    *pEventThread = eventWorkerThread;

cleanup:

    return dwError;
error:
    goto cleanup;
}
Exemple #20
0
static
DWORD
VmDnsSockPosixSetReuseAddress(
    int fd
    )
{
    DWORD dwError = 0;
    int on = 1;

    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }

error:

    return dwError;
}
Exemple #21
0
DWORD
VmLinuxOpenConnection(
    DWORD dwEventType,
    PVMNETEVENT_FD pFD
    )
{
    DWORD dwError = 0;
    int netLinkFd = -1;

    if (!pFD)
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_VMNETEVENT_ERROR(dwError);
    }

    netLinkFd = socket(
                AF_NETLINK,
                SOCK_RAW,
                NETLINK_ROUTE);
    if(netLinkFd < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_VMAFD_ERROR(dwError);
    }

    pFD->dwNetlinkFD = netLinkFd;

cleanup:

    return dwError;
error:

    if (pFD)
    {
        pFD->dwNetlinkFD = -1;
    }
    if (netLinkFd>=0)
    {
        close(netLinkFd);
    }
    goto cleanup;
}
Exemple #22
0
static
DWORD
VmDnsSockPosixEventQueueDelete_inlock(
    PVM_SOCK_EVENT_QUEUE pQueue,
    PVM_SOCKET           pSocket
    )
{
    DWORD dwError = 0;
    struct epoll_event event = {0};

    if (pSocket->bInEventQueue == TRUE &&
        epoll_ctl(pQueue->epollFd, EPOLL_CTL_DEL, pSocket->fd, &event) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_POSIX_SOCK_ERROR(dwError);
    }
    pSocket->bInEventQueue = FALSE;
    VmDnsSockPosixReleaseSocket(pSocket);

error:

    return dwError;
}
static
DWORD
GetHostname(
    OUT PSTR* ppszHostname
    )
{
    DWORD dwError = 0;
    CHAR buffer[1024] = { 0 };
    PSTR dot = NULL;
    PSTR pszHostname = NULL;

    if (gethostname(buffer, sizeof(buffer) - 1) == -1)
    {
        dwError = LwErrnoToWin32Error(errno);
        assert(dwError);
        GOTO_CLEANUP();
    }

    dot = strchr(buffer, '.');
    if (dot)
    {
        *dot = 0;
    }

    dwError = LwAllocateString(buffer, &pszHostname);
    GOTO_CLEANUP_ON_WINERROR(dwError);

cleanup:
    if (dwError)
    {
        LW_SAFE_FREE_STRING(pszHostname);
    }

    *ppszHostname = pszHostname;

    return dwError;
}
DWORD
VmAfdAcceptConnectionImpl(
  PVM_AFD_CONNECTION pConnection,
  PVM_AFD_CONNECTION *ppConnection
  )
{
	DWORD dwError = 0;
	PVM_AFD_CONNECTION pTempConnection = NULL;
	int connection_fd = 0;
	struct sockaddr_un address = {0};
	socklen_t address_length = 0;

	connection_fd = accept(pConnection->fd,(struct sockaddr *)&address, &address_length);
	if (connection_fd < 0){
		dwError = LwErrnoToWin32Error(errno);
		BAIL_ON_VMAFD_ERROR(dwError);
	}

	dwError = VmAfdAllocateMemory(sizeof(VM_AFD_CONNECTION),(PVOID *)&pTempConnection);
	BAIL_ON_VMAFD_ERROR(dwError);

	pTempConnection->fd = connection_fd;
	*ppConnection = pTempConnection;
cleanup:
	return dwError;
error:
	if (pTempConnection){
		VMAFD_SAFE_FREE_MEMORY(pTempConnection);
	}
	if (ppConnection){
		*ppConnection = NULL;
	}
	if (connection_fd >= 0){
		close (connection_fd);
	}
	goto cleanup;
}
Exemple #25
0
DWORD
EventLogGetHostName(
    PSTR* ppszHostName
)
{
    DWORD dwError = ERROR_SUCCESS;
    char hostBuf[HOST_NAME_MAX];
    DWORD dwBufLen = sizeof(hostBuf) - 1;
    PSTR pszHostName = NULL;

    if (gethostname(hostBuf, dwBufLen) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
    }

    dwError = EventLogAllocateStringA(hostBuf, &pszHostName);
    BAIL_ON_VMEVENT_ERROR(dwError);

    *ppszHostName = pszHostName;

error:

    return dwError;
}
DWORD
VmAfdInitializeSecurityContextImpl(
    PVM_AFD_CONNECTION pConnection,
    PVM_AFD_SECURITY_CONTEXT *ppSecurityContext
    )
{
    DWORD dwError = 0;
    struct ucred credentials = {0};
    int credLength = sizeof (struct ucred);
    PVM_AFD_SECURITY_CONTEXT pSecurityContext = NULL;
    if ((getsockopt (
            pConnection->fd,
            SOL_SOCKET,
            SO_PEERCRED,
            &credentials,
            &credLength)) < 0){
      dwError = LwErrnoToWin32Error (errno);
      BAIL_ON_VMAFD_ERROR (dwError);
    }
    dwError = VmAfdAllocateMemory(
                    sizeof (VM_AFD_SECURITY_CONTEXT),
                    (PVOID *)&pSecurityContext);
    BAIL_ON_VMAFD_ERROR (dwError);
    pSecurityContext->uid = credentials.uid;
    *ppSecurityContext = pSecurityContext;
cleanup:
    return dwError;
error:
    if (ppSecurityContext != NULL){
      *ppSecurityContext = NULL;
    }
    if (pSecurityContext){
      VmAfdFreeSecurityContext(pSecurityContext);
    }
    goto cleanup;
}
Exemple #27
0
DWORD
DirectoryAddObject(
    HANDLE        hDirectory,
    PWSTR         pwszObjectDN,
    DIRECTORY_MOD attributes[]
    )
{
    DWORD dwError = 0;
    PDIRECTORY_CONTEXT pContext = (PDIRECTORY_CONTEXT)hDirectory;
    PWSTR pwszBase = NULL;
    DWORD dwScope = 0;
    wchar_t wszFilterFmt[] = L"%ws='%ws'";
    size_t sDnLen = 0;
    DWORD dwFilterLen = 0;
    PWSTR pwszFilter = NULL;
    WCHAR wszAttrDn[] = DIRECTORY_ATTR_DISTINGUISHED_NAME;
    WCHAR wszAttrObjectClass[] = DIRECTORY_ATTR_OBJECT_CLASS;
    PDIRECTORY_ENTRY pEntries = NULL;
    PDIRECTORY_ENTRY pEntry = NULL;
    DWORD dwNumEntries = 0;
    DWORD dwObjectClass = 0;

    PWSTR wszAttributes[] = {
        wszAttrDn,
        wszAttrObjectClass,
        NULL
    };

    if (!pContext || !pContext->pProvider)
    {
        dwError = LW_ERROR_INVALID_PARAMETER;
        BAIL_ON_DIRECTORY_ERROR(dwError);
    }

    dwError = LwWc16sLen(pwszObjectDN, &sDnLen);
    BAIL_ON_DIRECTORY_ERROR(dwError);

    dwFilterLen = ((sizeof(wszFilterFmt)/sizeof(wszFilterFmt[0])) - 1) +
                   sizeof(wszAttrDn) +
                   (sizeof(WCHAR) * sDnLen);

    dwError = DirectoryAllocateMemory(dwFilterLen,
                                      OUT_PPVOID(&pwszFilter));
    BAIL_ON_DIRECTORY_ERROR(dwError);

    if (sw16printfw(pwszFilter, dwFilterLen, wszFilterFmt,
                    wszAttrDn,
                    pwszObjectDN) < 0)
    {
        dwError = LwErrnoToWin32Error(errno);
        BAIL_ON_DIRECTORY_ERROR(dwError);
    }

    dwError = DirectorySearch(hDirectory,
                              pwszBase,
                              dwScope,
                              pwszFilter,
                              wszAttributes,
                              FALSE,
                              &pEntries,
                              &dwNumEntries);
    BAIL_ON_DIRECTORY_ERROR(dwError);

    if (dwNumEntries == 1)
    {
        pEntry = &(pEntries[0]);

        dwError = DirectoryGetEntryAttrValueByName(
                              pEntry,
                              wszAttrObjectClass,
                              DIRECTORY_ATTR_TYPE_INTEGER,
                              &dwObjectClass);
        BAIL_ON_DIRECTORY_ERROR(dwError);

        switch (dwObjectClass)
        {
        case DIR_OBJECT_CLASS_USER:
            dwError = ERROR_USER_EXISTS;
            break;

        case DIR_OBJECT_CLASS_LOCAL_GROUP:
            dwError = ERROR_ALIAS_EXISTS;
            break;

        default:
            dwError = ERROR_ALREADY_EXISTS;
            break;
        }
        BAIL_ON_DIRECTORY_ERROR(dwError);
    }
    else if (dwNumEntries > 1)
    {
        dwError = LW_ERROR_SAM_DATABASE_ERROR;
        BAIL_ON_DIRECTORY_ERROR(dwError);
    }

    dwError = pContext->pProvider->pProviderFnTbl->pfnDirectoryAdd(
                    pContext->hBindHandle,
                    pwszObjectDN,
                    attributes);

error:
    if (pEntries)
    {
        DirectoryFreeEntries(pEntries, dwNumEntries);
    }

    DIRECTORY_FREE_MEMORY(pwszFilter);

    return dwError;
}
Exemple #28
0
DWORD
VmAfdInitRootFlushThread(
    PVMAFD_THREAD* ppThread
    )
{
   DWORD dwError = 0;
   PVMAFD_THREAD pThread = NULL;

   VmAfdLog(VMAFD_DEBUG_ANY, "Starting Roots Flush Thread, %s", __FUNCTION__);

   dwError = VmAfdAllocateMemory(
                sizeof(VMAFD_THREAD),
                (PVOID*)&pThread);
   BAIL_ON_VMAFD_ERROR(dwError);

   dwError = pthread_mutex_init(&pThread->thrData.mutex, NULL);
   if (dwError)
   {
#ifndef _WIN32
        dwError = LwErrnoToWin32Error(dwError);
#endif
        BAIL_ON_VMAFD_ERROR(dwError);
   }

   pThread->thrData.pMutex = &pThread->thrData.mutex;

   dwError = pthread_cond_init(&pThread->thrData.cond, NULL);
   if (dwError)
   {
#ifndef _WIN32
        dwError = LwErrnoToWin32Error(dwError);
#endif
        BAIL_ON_VMAFD_ERROR(dwError);
   }

   pThread->thrData.pCond = &pThread->thrData.cond;

   dwError = pthread_create(
                &pThread->thread,
                NULL,
                &VmAfdFlushTrustedRoots,
                (PVOID)&pThread->thrData);
    if (dwError)
    {
#ifndef _WIN32
        dwError = LwErrnoToWin32Error(dwError);
#endif
        BAIL_ON_VMAFD_ERROR(dwError);
    }

    pThread->pThread = &pThread->thread;

    *ppThread = pThread;

    VmAfdLog(VMAFD_DEBUG_ANY, "Started Roots Flush Thread successfully, %s", __FUNCTION__);

cleanup:

    return dwError;

error:

    *ppThread = NULL;

    if (pThread)
    {
        VmAfdShutdownRootFlushThread(pThread);
    }

    goto cleanup;
}
Exemple #29
0
static
DWORD
VmAfdSrvWriteRootToDisk(
    PCSTR pszCertificate,
    PCSTR pszCAPath,
    PCSTR pszFilename
    )
{
    DWORD dwError = 0;
    LONG  maxIndex = -1;
    PSTR  pszPath = NULL;
    PSTR  pszAlias = NULL;
    FILE* pFile = NULL;
    size_t len = 0;
    size_t bytesToWrite = 0;
    BOOLEAN bCertOnDisk = FALSE;
    PCSTR   pszCursor = NULL;

    dwError = VmAfdFindFileIndex(pszCAPath, pszFilename, &maxIndex);
    if (dwError != ERROR_FILE_NOT_FOUND)
    {
        BAIL_ON_VMAFD_ERROR(dwError);

        dwError = VecsComputeCertAliasA((PSTR)pszCertificate, &pszAlias);
        BAIL_ON_VMAFD_ERROR(dwError);

        dwError = VmAfdCheckCertOnDisk(
                        pszAlias,
                        pszCAPath,
                        pszFilename,
                        maxIndex,
                        &bCertOnDisk);
        BAIL_ON_VMAFD_ERROR(dwError);

        if (bCertOnDisk)
        {
            goto cleanup;
        }
    }

    dwError = VmAfdAllocateStringPrintf(
                    &pszPath,
                    "%s%s%s.%ld",
                    pszCAPath,
                    VMAFD_PATH_SEPARATOR_STR,
                    pszFilename,
                    maxIndex+1);
    BAIL_ON_VMAFD_ERROR(dwError);

    dwError = VmAfdOpenFilePath(pszPath, "w", &pFile);
    BAIL_ON_VMAFD_ERROR(dwError);

    len = strlen(pszCertificate);
    bytesToWrite = len;
    pszCursor = pszCertificate;

    while (bytesToWrite > 0)
    {
        size_t bytesWritten = 0;

        if ((bytesWritten = fwrite(pszCursor, 1, len, pFile)) == 0)
        {
#ifndef _WIN32
            dwError = LwErrnoToWin32Error(errno);
#else
            dwError = GetLastError();
#endif
            BAIL_ON_VMAFD_ERROR(dwError);
        }

        pszCursor += bytesWritten;
        bytesToWrite -= bytesWritten;
    }

cleanup:

    if (pFile)
    {
        fclose(pFile);
    }

    VMAFD_SAFE_FREE_MEMORY(pszPath);
    VMAFD_SAFE_FREE_MEMORY(pszAlias);

    return dwError;

error:

    goto cleanup;
}
Exemple #30
0
VOID
LwSmTableNotifyEntryStateChanged(
    PSM_TABLE_ENTRY pEntry,
    LW_SERVICE_STATE state
    )
{
    DWORD error = ERROR_SUCCESS;
    BOOLEAN bLocked = FALSE;
    PSM_LINK pLink = NULL;
    PSM_LINK pNext = NULL;
    PSM_ENTRY_NOTIFY pNotify = NULL;
    PSTR pServiceName = NULL;
    time_t now = 0;
    
    LOCK(bLocked, pEntry->pLock);

    for (pLink = LwSmLinkBegin(&pEntry->waiters);
         LwSmLinkValid(&pEntry->waiters, pLink);
         pLink = pNext)
    {
        pNext = LwSmLinkNext(pLink);
        pNotify = STRUCT_FROM_MEMBER(pLink, SM_ENTRY_NOTIFY, link);

        LwSmLinkRemove(pLink);

        pNotify->pfnNotifyEntryStateChange(state, pNotify->pData);

        LwFreeMemory(pNotify);
    }

    pthread_cond_broadcast(pEntry->pEvent);

    if (state == LW_SERVICE_STATE_DEAD && gState.bWatchdog)
    {
        now = time(NULL);
        if (now == (time_t) -1)
        {
            error = LwErrnoToWin32Error(errno);
            BAIL_ON_ERROR(error);
        }

        error = LwWc16sToMbs(pEntry->pInfo->pwszName, &pServiceName);
        BAIL_ON_ERROR(error);

        if (pEntry->StartAttempts >= 1 &&
            (now - pEntry->LastRestartPeriod) > RESTART_PERIOD)
        {
            pEntry->StartAttempts = 1;
            pEntry->LastRestartPeriod = now;
        }

        if (pEntry->StartAttempts > 0 && pEntry->StartAttempts < RESTART_LIMIT)
        {
            pEntry->StartAttempts++;
            pEntry->dwRefCount++;

            SM_LOG_WARNING(
                "Restarting dead service: %s (attempt %u/%u)",
                pServiceName,
                (unsigned int) pEntry->StartAttempts,
                (unsigned int) RESTART_LIMIT);


            error = LwNtStatusToWin32Error(LwRtlQueueWorkItem(gpPool, LwSmTableWatchdog, pEntry, 0));
            if (error)
            {
                pEntry->dwRefCount--;
            }
            BAIL_ON_ERROR(error);
        }
        else if (pEntry->StartAttempts >= 1)
        {
            SM_LOG_ERROR(
                "Service died: %s (restarted %u times in %lu seconds)",
                pServiceName,
                (unsigned int) pEntry->StartAttempts,
                (unsigned long) (now - pEntry->LastRestartPeriod));
        }
        else
        {
            SM_LOG_ERROR(
                "Service failed to start: %s",
                pServiceName);
        }
    }

error:

    UNLOCK(bLocked, pEntry->pLock);

    LW_SAFE_FREE_MEMORY(pServiceName);
}