VBoxGuestR3LibAutoLogon.cpp revision 99e94accbcf65ea44e5edb07c8f5dfec0ccc22fe
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence/* $Id */
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence/** @file
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * VBoxGuestR3LibAutoLogon - Ring-3 utility functions for auto-logon modules
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence * (VBoxGINA / VBoxCredProv / pam_vbox).
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence */
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/*
15a44745412679c30a6d022733925af70a38b715David Lawrence * Copyright (C) 2012 Oracle Corporation
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * This file is part of VirtualBox Open Source Edition (OSE), as
15a44745412679c30a6d022733925af70a38b715David Lawrence * available from http://www.virtualbox.org. This file is free software;
15a44745412679c30a6d022733925af70a38b715David Lawrence * you can redistribute it and/or modify it under the terms of the GNU
15a44745412679c30a6d022733925af70a38b715David Lawrence * General Public License (GPL) as published by the Free Software
15a44745412679c30a6d022733925af70a38b715David Lawrence * Foundation, in version 2 as it comes in the "COPYING" file of the
15a44745412679c30a6d022733925af70a38b715David Lawrence * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The contents of this file may alternatively be used under the terms
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * of the Common Development and Distribution License Version 1.0
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * VirtualBox OSE distribution, in which case the provisions of the
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * CDDL are applicable instead of those of the GPL.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence *
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * You may elect to license modified versions of this file under the
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * terms and conditions of either the GPL or the CDDL or both.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence */
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence/*******************************************************************************
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence* Header Files *
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence*******************************************************************************/
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence#include <iprt/asm.h>
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence#include <iprt/mem.h>
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence#include <iprt/rand.h>
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence#include <iprt/string.h>
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence#include <VBox/log.h>
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence#include "VBGLR3Internal.h"
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence/**
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * Reports the current auto-logon status to the host.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence *
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * This makes sure that the Failed state is sticky.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence *
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * @return IPRT status code.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * @param enmStatus Status to report to the host.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence */
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid LawrenceVBGLR3DECL(int) VbglR3AutoLogonReportStatus(VBoxGuestFacilityStatus enmStatus)
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence{
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence /*
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * VBoxGuestFacilityStatus_Failed is sticky.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence */
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence static VBoxGuestFacilityStatus s_enmLastStatus = VBoxGuestFacilityStatus_Inactive;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence if (s_enmLastStatus != VBoxGuestFacilityStatus_Failed)
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence {
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence int rc = VbglR3ReportAdditionsStatus(VBoxGuestFacilityType_AutoLogon,
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence enmStatus, 0 /* Flags */);
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence if (rc == VERR_NOT_SUPPORTED)
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence {
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence /*
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * To maintain backwards compatibility to older hosts which don't have
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * VMMDevReportGuestStatus implemented we set the appropriate status via
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence * guest property to have at least something.
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence */
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence uint32_t u32ClientId = 0;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence rc = VbglR3GuestPropConnect(&u32ClientId);
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence if (RT_SUCCESS(rc))
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence {
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence /** @todo Move VBoxGuestStatusCurrent -> const char* to an own function. */
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence char szStatus[RTPATH_MAX];
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence size_t cbRet = 0;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence switch (enmStatus)
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence {
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence case VBoxGuestFacilityStatus_Inactive:
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Inactive");
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence break;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence case VBoxGuestFacilityStatus_Paused:
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Disabled");
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence break;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence case VBoxGuestFacilityStatus_PreInit:
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "PreInit");
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence break;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence case VBoxGuestFacilityStatus_Init:
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Init");
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence break;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence case VBoxGuestFacilityStatus_Active:
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Active");
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence break;
a904de0bc85cc3974a87a32deedb69d6a68ec36cDavid Lawrence case VBoxGuestFacilityStatus_Terminating:
cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Terminating");
break;
case VBoxGuestFacilityStatus_Terminated:
cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Terminated");
break;
case VBoxGuestFacilityStatus_Failed:
cbRet = RTStrPrintf(szStatus, sizeof(szStatus), "Failed");
break;
default:
/* cbRet will be 0. */
break;
}
if (cbRet)
{
const char szPath[] = "/VirtualBox/GuestInfo/OS/AutoLogonStatus";
/*
* Because a value can be temporary we have to make sure it also
* gets deleted when the property cache did not have the chance to
* gracefully clean it up (due to a hard VM reset etc), so set this
* guest property using the TRANSRESET flag..
*/
rc = VbglR3GuestPropWrite(u32ClientId, szPath, szStatus, "TRANSRESET");
if (rc == VERR_PARSE_ERROR)
{
/* Host does not support the "TRANSRESET" flag, so only
* use the "TRANSIENT" flag -- better than nothing :-). */
rc = VbglR3GuestPropWrite(u32ClientId, szPath, szStatus, "TRANSIENT");
}
}
else
rc = VERR_INVALID_PARAMETER;
VbglR3GuestPropDisconnect(u32ClientId);
}
}
s_enmLastStatus = enmStatus;
}
return VINF_SUCCESS;
}
/**
* Detects whether our process is running in a remote session or not.
*
* @return bool true if running in a remote session, false if not.
*/
VBGLR3DECL(bool) VbglR3AutoLogonIsRemoteSession(void)
{
#ifdef RT_OS_WINDOWS
return (0 != GetSystemMetrics(SM_REMOTESESSION)) ? true : false;
#else
return false; /* Not implemented. */
#endif
}