VirtualBox

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

Last change on this file since 96316 was 95140, checked in by vboxsync, 2 years ago

Frontends + Main: Adjust to the new rules wrt. to rc -> hrc,vrc usage. This also fixes quite a few bugs wrt shadow variables, wrong return values and output error translations / exit codes. Also see @todos added. ​​bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* $Id: PasswordInput.cpp 95140 2022-05-31 09:11:39Z vboxsync $ */
2/** @file
3 * Frontend shared bits - Password file and console input helpers.
4 */
5
6/*
7 * Copyright (C) 2012-2022 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/errcore.h>
26#include <iprt/message.h>
27#include <iprt/stream.h>
28
29#include <VBox/com/errorprint.h>
30
31
32/**
33 * Reads a password from the password file.
34 *
35 * Only first line is used. The passwords length must be less than 512 bytes
36 *
37 * @param pszFilename The path to file containing the password
38 * @param pPasswd The string where password will be returned
39 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + msg.
40 */
41RTEXITCODE readPasswordFile(const char *pszFilename, com::Utf8Str *pPasswd)
42{
43 size_t cbFile;
44 char szPasswd[512] = { 0 };
45 int vrc = VINF_SUCCESS;
46 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
47 bool fStdIn = !strcmp(pszFilename, "stdin");
48 PRTSTREAM pStrm;
49 if (!fStdIn)
50 vrc = RTStrmOpen(pszFilename, "r", &pStrm);
51 else
52 pStrm = g_pStdIn;
53 if (RT_SUCCESS(vrc))
54 {
55 vrc = RTStrmReadEx(pStrm, szPasswd, sizeof(szPasswd)-1, &cbFile);
56 if (RT_SUCCESS(vrc))
57 {
58 size_t cbSize = RT_MIN(sizeof(szPasswd)-1, cbFile);
59 unsigned i;
60 for (i = 0; i < cbSize && !RTLocCIsCntrl(szPasswd[i]); i++)
61 ;
62 szPasswd[i] = '\0';
63 /* If the line containing password doesn't fit into buffer */
64 if (i >= sizeof(szPasswd)-1 && cbFile >= sizeof(szPasswd))
65 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Provided password in file '%s' is too long", pszFilename);
66 else
67 *pPasswd = szPasswd;
68 }
69 else
70 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot read password from file '%s': %Rrc", pszFilename, vrc);
71 if (!fStdIn)
72 RTStrmClose(pStrm);
73 }
74 else
75 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot open password file '%s' (%Rrc)", pszFilename, vrc);
76
77 return rcExit;
78}
79
80
81/**
82 * Sets password for settings from password file
83 *
84 * Only first line is used. The passwords length must be less than 512 bytes
85 *
86 * @param virtualBox The IVirtualBox interface the settings password will be set for
87 * @param pszFilename The path to file containing the password
88 * @return RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + msg.
89 */
90RTEXITCODE settingsPasswordFile(ComPtr<IVirtualBox> virtualBox, const char *pszFilename)
91{
92 com::Utf8Str passwd;
93 RTEXITCODE rcExit = readPasswordFile(pszFilename, &passwd);
94 if (rcExit == RTEXITCODE_SUCCESS)
95 {
96 HRESULT hrc;
97 CHECK_ERROR(virtualBox, SetSettingsSecret(com::Bstr(passwd).raw()));
98 if (FAILED(hrc))
99 rcExit = RTEXITCODE_FAILURE;
100 }
101
102 return rcExit;
103}
104
105
106/**
107 * Gets the password from the user input
108 * *
109 * @param pPassword The string where password will be returned
110 * @param pszPrompt The prompt string for user
111 * @return RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + msg.
112 */
113RTEXITCODE readPasswordFromConsole(com::Utf8Str *pPassword, const char *pszPrompt, ...)
114{
115 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
116 char aszPwdInput[_1K] = { 0 };
117 va_list vaArgs;
118
119 va_start(vaArgs, pszPrompt);
120 int vrc = RTStrmPrintfV(g_pStdOut, pszPrompt, vaArgs);
121 if (RT_SUCCESS(vrc))
122 {
123 bool fEchoOld = false;
124 vrc = RTStrmInputGetEchoChars(g_pStdIn, &fEchoOld);
125 if (RT_SUCCESS(vrc))
126 {
127 vrc = RTStrmInputSetEchoChars(g_pStdIn, false);
128 if (RT_SUCCESS(vrc))
129 {
130 vrc = RTStrmGetLine(g_pStdIn, &aszPwdInput[0], sizeof(aszPwdInput));
131 if (RT_SUCCESS(vrc))
132 {
133#ifdef RT_OS_WINDOWS
134 /*
135 * Returned string encoded in console code page (e.g. Win-125X or CP-XXX).
136 * Convert it to Utf-8
137 */
138 char *pszPassword = NULL;
139 vrc = RTStrConsoleCPToUtf8(&pszPassword, aszPwdInput);
140 if (RT_SUCCESS(vrc) && pszPassword)
141 {
142 *pPassword = pszPassword;
143 RTMemFree(pszPassword);
144 }
145 else
146 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE,
147 "Failed to convert password from windows console codepage to Utf-8 (%Rrc)",
148 vrc);
149#else
150 *pPassword = aszPwdInput;
151#endif
152 }
153 else
154 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed read password from command line (%Rrc)", vrc);
155
156 int vrc2 = RTStrmInputSetEchoChars(g_pStdIn, fEchoOld);
157 AssertRC(vrc2);
158 }
159 else
160 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to disable echoing typed characters (%Rrc)", vrc);
161 }
162 else
163 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to retrieve echo setting (%Rrc)", vrc);
164
165 RTStrmPutStr(g_pStdOut, "\n");
166 }
167 else
168 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to print prompt (%Rrc)", vrc);
169 va_end(vaArgs);
170
171 return rcExit;
172}
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