VBoxGuestR3LibCredentials.cpp revision 4fd422211bfa08d134940933274daafa9f2a3ef7
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync/* $Id$ */
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync/** @file
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, user credentials.
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync */
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync/*
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync *
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * available from http://www.virtualbox.org. This file is free software;
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * you can redistribute it and/or modify it under the terms of the GNU
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * General Public License (GPL) as published by the Free Software
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync *
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * additional information or have any questions.
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync */
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync/*******************************************************************************
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync* Header Files *
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync*******************************************************************************/
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync#include <iprt/string.h>
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync#include <VBox/log.h>
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync#include "VBGLR3Internal.h"
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync/**
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * Checks whether user credentials are available to the guest or not.
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync *
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * @returns true if credentials are available, false if not (or error occured).
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync */
a8e1fc1b96d1436189fdb4530d31f20b88b70ea1vboxsyncVBGLR3DECL(bool) VbglR3CredentialsAreAvailable(void)
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync{
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync VMMDevCredentials Req;
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync RT_ZERO(Req);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync Req.u32Flags |= VMMDEV_CREDENTIALS_QUERYPRESENCE;
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync int rc = vbglR3GRPerform(&Req.header);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync return RT_SUCCESS(rc)
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync && (Req.u32Flags & VMMDEV_CREDENTIALS_PRESENT) != 0;
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync}
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync/**
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync * Retrieves and clears the user credentials for logging into the guest OS.
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync *
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync * @returns IPRT status value
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync * @param ppszUser Receives pointer of allocated user name string.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * The returned pointer must be freed using VbglR3CredentialsDestroy().
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync * @param ppszPassword Receives pointer of allocated user password string.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * The returned pointer must be freed using VbglR3CredentialsDestroy().
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync * @param ppszDomain Receives pointer of allocated domain name string.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * The returned pointer must be freed using VbglR3CredentialsDestroy().
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync */
bd44be5c996657f50cadcb987ee2879f4114d249vboxsyncVBGLR3DECL(int) VbglR3CredentialsRetrieve(char **ppszUser, char **ppszPassword, char **ppszDomain)
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync{
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync VMMDevCredentials Req;
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync RT_ZERO(Req);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync Req.u32Flags |= VMMDEV_CREDENTIALS_READ | VMMDEV_CREDENTIALS_CLEAR;
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync int rc = vbglR3GRPerform(&Req.header);
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync if (RT_SUCCESS(rc))
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync {
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync rc = RTStrDupEx(ppszUser, Req.szUserName);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync if (RT_SUCCESS(rc))
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync {
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync rc = RTStrDupEx(ppszPassword, Req.szPassword);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync if (RT_SUCCESS(rc))
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync {
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync rc = RTStrDupEx(ppszDomain, Req.szDomain);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync if (RT_SUCCESS(rc))
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync return VINF_SUCCESS;
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync RTStrFree(*ppszPassword);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync }
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync RTStrFree(*ppszUser);
f5663ddc90362ab5cfe32e277fa34f2b057e8cfdvboxsync }
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync }
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync return rc;
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync}
bd44be5c996657f50cadcb987ee2879f4114d249vboxsync
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync/**
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * Clears and frees the strings
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync *
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * @returns IPRT status value
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync * @param pszUser Receives pointer of the user name string to destroy.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * Optional.
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync * @param pszPassword Receives pointer of the password string to destroy.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * Optional.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * @param pszDomain Receives pointer of allocated domain name string.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * Optional.
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync * @param u8NumPasses Number of wipe passes. The higher the better (and slower!).
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync */
940534f83612182a14887c7ccac92bfc43cbebd2vboxsyncVBGLR3DECL(void) VbglR3CredentialsDestroy(char *pszUser, char *pszPassword, char *pszDomain, uint8_t u8NumPasses)
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync{
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync size_t l;
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync if (u8NumPasses == 0) /* We at least want to have one wipe pass. */
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync u8NumPasses = 1;
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync /** @todo add some for-loop with randomized content instead of
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync * zero'ing out the string only one time. Use u8NumPasses for that. */
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync if (pszUser)
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync {
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync l = strlen(pszUser);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync RT_BZERO(pszUser, l);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync RTStrFree(pszUser);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync }
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync if (pszPassword)
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync {
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync l = strlen(pszPassword);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync RT_BZERO(pszPassword, l);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync RTStrFree(pszPassword);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync }
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync if (pszUser)
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync {
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync l = strlen(pszDomain);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync RT_BZERO(pszDomain, l);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync RTStrFree(pszDomain);
4fd422211bfa08d134940933274daafa9f2a3ef7vboxsync }
940534f83612182a14887c7ccac92bfc43cbebd2vboxsync}