VirtualBox

source: vbox/trunk/src/VBox/Frontends/Common/PasswordInput.cpp@ 85442

Last change on this file since 85442 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: PasswordInput.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * Frontend shared bits - Password file and console input helpers.
4 */
5
6/*
7 * Copyright (C) 2012-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "PasswordInput.h"
23
24#include <iprt/ctype.h>
25#include <iprt/message.h>
26#include <iprt/stream.h>
27
28#include <VBox/com/errorprint.h>
29
30
31/**
32 * Reads a password from the password file.
33 *
34 * Only first line is used. The passwords length must be less than 512 bytes
35 *
36 * @param pszFilename The path to file containing the password
37 * @param pPasswd The string where password will be returned
38 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + msg.
39 */
40RTEXITCODE readPasswordFile(const char *pszFilename, com::Utf8Str *pPasswd)
41{
42 size_t cbFile;
43 char szPasswd[512] = { 0 };
44 int vrc = VINF_SUCCESS;
45 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
46 bool fStdIn = !strcmp(pszFilename, "stdin");
47 PRTSTREAM pStrm;
48 if (!fStdIn)
49 vrc = RTStrmOpen(pszFilename, "r", &pStrm);
50 else
51 pStrm = g_pStdIn;
52 if (RT_SUCCESS(vrc))
53 {
54 vrc = RTStrmReadEx(pStrm, szPasswd, sizeof(szPasswd)-1, &cbFile);
55 if (RT_SUCCESS(vrc))
56 {
57 size_t cbSize = RT_MIN(sizeof(szPasswd)-1, cbFile);
58 unsigned i;
59 for (i = 0; i < cbSize && !RTLocCIsCntrl(szPasswd[i]); i++)
60 ;
61 szPasswd[i] = '\0';
62 /* If the line containing password doesn't fit into buffer */
63 if (i >= sizeof(szPasswd)-1 && cbFile >= sizeof(szPasswd))
64 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Provided password in file '%s' is too long", pszFilename);
65 else
66 *pPasswd = szPasswd;
67 }
68 else
69 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot read password from file '%s': %Rrc", pszFilename, vrc);
70 if (!fStdIn)
71 RTStrmClose(pStrm);
72 }
73 else
74 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot open password file '%s' (%Rrc)", pszFilename, vrc);
75
76 return rcExit;
77}
78
79
80/**
81 * Sets password for settings from password file
82 *
83 * Only first line is used. The passwords length must be less than 512 bytes
84 *
85 * @param virtualBox The IVirtualBox interface the settings password will be set for
86 * @param pszFilename The path to file containing the password
87 * @return RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + msg.
88 */
89RTEXITCODE settingsPasswordFile(ComPtr<IVirtualBox> virtualBox, const char *pszFilename)
90{
91 com::Utf8Str passwd;
92 RTEXITCODE rcExit = readPasswordFile(pszFilename, &passwd);
93 if (rcExit == RTEXITCODE_SUCCESS)
94 {
95 int rc;
96 CHECK_ERROR(virtualBox, SetSettingsSecret(com::Bstr(passwd).raw()));
97 if (FAILED(rc))
98 rcExit = RTEXITCODE_FAILURE;
99 }
100
101 return rcExit;
102}
103
104
105/**
106 * Gets the password from the user input
107 * *
108 * @param pPassword The string where password will be returned
109 * @param pszPrompt The prompt string for user
110 * @return RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + msg.
111 */
112RTEXITCODE readPasswordFromConsole(com::Utf8Str *pPassword, const char *pszPrompt, ...)
113{
114 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
115 char aszPwdInput[_1K] = { 0 };
116 va_list vaArgs;
117
118 va_start(vaArgs, pszPrompt);
119 int vrc = RTStrmPrintfV(g_pStdOut, pszPrompt, vaArgs);
120 if (RT_SUCCESS(vrc))
121 {
122 bool fEchoOld = false;
123 vrc = RTStrmInputGetEchoChars(g_pStdIn, &fEchoOld);
124 if (RT_SUCCESS(vrc))
125 {
126 vrc = RTStrmInputSetEchoChars(g_pStdIn, false);
127 if (RT_SUCCESS(vrc))
128 {
129 vrc = RTStrmGetLine(g_pStdIn, &aszPwdInput[0], sizeof(aszPwdInput));
130 if (RT_SUCCESS(vrc))
131 *pPassword = aszPwdInput;
132 else
133 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed read password from command line (%Rrc)", vrc);
134
135 int vrc2 = RTStrmInputSetEchoChars(g_pStdIn, fEchoOld);
136 AssertRC(vrc2);
137 }
138 else
139 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to disable echoing typed characters (%Rrc)", vrc);
140 }
141 else
142 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to retrieve echo setting (%Rrc)", vrc);
143
144 RTStrmPutStr(g_pStdOut, "\n");
145 }
146 else
147 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to print prompt (%Rrc)", vrc);
148 va_end(vaArgs);
149
150 return rcExit;
151}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette