VBoxAuthSimple.cpp revision 8b98c71a5a01d215eafbc3605cb7a66cc91ea774
0N/A/** @file
3677N/A *
0N/A * VBox Remote Desktop Protocol:
0N/A * External Authentication Library:
0N/A * Simple Authentication.
0N/A */
0N/A
0N/A/*
0N/A * Copyright (C) 2006-2010 Oracle Corporation
0N/A *
0N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License (GPL) as published by the Free Software
0N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
0N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A */
1472N/A
1472N/A#include <stdlib.h>
1472N/A#include <stdio.h>
0N/A#include <string.h>
0N/A
0N/A#include <iprt/cdefs.h>
1879N/A#include <iprt/uuid.h>
1879N/A#include <iprt/sha.h>
1879N/A
1879N/A#include <VBox/VRDPAuth.h>
1879N/A
1879N/A#include <VBox/com/com.h>
1879N/A#include <VBox/com/string.h>
1879N/A#include <VBox/com/Guid.h>
1879N/A#include <VBox/com/VirtualBox.h>
0N/A
0N/Ausing namespace com;
0N/A
0N/A/* If defined, debug messages will be written to the specified file. */
0N/A//#define VRDPAUTH_DEBUG_FILE_NAME "/tmp/VRDPAuth.log"
0N/A
0N/A
0N/Astatic void dprintf(const char *fmt, ...)
0N/A{
0N/A#ifdef VRDPAUTH_DEBUG_FILE_NAME
0N/A va_list va;
0N/A
0N/A va_start(va, fmt);
0N/A
0N/A char buffer[1024];
0N/A
0N/A vsnprintf(buffer, sizeof(buffer), fmt, va);
0N/A
0N/A FILE *f = fopen(VRDPAUTH_DEBUG_FILE_NAME, "ab");
0N/A if (f)
0N/A {
0N/A fprintf(f, "%s", buffer);
0N/A fclose(f);
0N/A }
0N/A
0N/A va_end (va);
0N/A#endif
0N/A}
0N/A
0N/ART_C_DECLS_BEGIN
0N/ADECLEXPORT(AuthResult) AUTHCALL AuthEntry(const char *szCaller,
0N/A PAUTHUUID pUuid,
0N/A AuthGuestJudgement guestJudgement,
0N/A const char *szUser,
2233N/A const char *szPassword,
0N/A const char *szDomain,
0N/A int fLogon,
0N/A unsigned clientId)
0N/A{
0N/A /* default is failed */
0N/A AuthResult result = AuthResultAccessDenied;
0N/A
0N/A /* only interested in logon */
0N/A if (!fLogon)
0N/A /* return value ignored */
0N/A return result;
0N/A
0N/A char uuid[RTUUID_STR_LENGTH] = {0};
0N/A if (pUuid)
0N/A RTUuidToStr((PCRTUUID)pUuid, (char*)uuid, RTUUID_STR_LENGTH);
0N/A
0N/A /* the user might contain a domain name, split it */
0N/A char *user = strchr((char*)szUser, '\\');
0N/A if (user)
0N/A user++;
0N/A else
0N/A user = (char*)szUser;
0N/A
2233N/A dprintf("VBoxAuth: uuid: %s, user: %s, szPassword: %s\n", uuid, user, szPassword);
0N/A
0N/A ComPtr<IVirtualBox> virtualBox;
0N/A HRESULT rc;
0N/A
0N/A rc = virtualBox.createLocalObject(CLSID_VirtualBox);
0N/A if (SUCCEEDED(rc))
0N/A {
0N/A Bstr key = BstrFmt("VBoxAuthSimple/users/%s", user);
3677N/A Bstr password;
0N/A
0N/A /* lookup in VM's extra data? */
1879N/A if (pUuid)
1879N/A {
1879N/A ComPtr<IMachine> machine;
1879N/A virtualBox->FindMachine(Bstr(uuid).raw(), machine.asOutParam());
1879N/A if (machine)
1879N/A machine->GetExtraData(key.raw(), password.asOutParam());
1879N/A } else
1879N/A /* lookup global extra data */
1879N/A virtualBox->GetExtraData(key.raw(), password.asOutParam());
2796N/A
2796N/A if (!password.isEmpty())
2796N/A {
1879N/A /* calculate hash */
0N/A uint8_t abDigest[RTSHA256_HASH_SIZE];
0N/A RTSha256(szPassword, strlen(szPassword), abDigest);
0N/A char pszDigest[RTSHA256_DIGEST_LEN + 1];
0N/A RTSha256ToString(abDigest, pszDigest, sizeof(pszDigest));
0N/A
0N/A if (password == pszDigest)
0N/A result = AuthResultAccessGranted;
0N/A }
0N/A }
0N/A
0N/A return result;
0N/A}
0N/ART_C_DECLS_END
0N/A
0N/A/* Verify the function prototype. */
0N/Astatic PAUTHENTRY3 gpfnAuthEntry = AuthEntry;
0N/A