VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/UnattendedScript.cpp@ 93190

Last change on this file since 93190 was 93190, checked in by vboxsync, 3 years ago

Main/UnattendedScript: Added @@VBOX_INSERT[expr]@@ and @@VBOX_COND[expr]@@, merging the variable & condition lookup with the previous replacement getter. Added simple testcase for checking the basics. bugref:9781

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.4 KB
Line 
1/* $Id: UnattendedScript.cpp 93190 2022-01-11 23:22:13Z vboxsync $ */
2/** @file
3 * Classes for reading/parsing/saving scripts for unattended installation.
4 */
5
6/*
7 * Copyright (C) 2006-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#define LOG_GROUP LOG_GROUP_MAIN_UNATTENDED
23#include "LoggingNew.h"
24#include "VirtualBoxBase.h"
25#include "AutoCaller.h"
26#include <VBox/com/ErrorInfo.h>
27
28#include "UnattendedScript.h"
29#include "UnattendedImpl.h"
30
31#include <iprt/err.h>
32
33#include <iprt/ctype.h>
34#include <iprt/file.h>
35#include <iprt/vfs.h>
36#include <iprt/getopt.h>
37#include <iprt/path.h>
38
39using namespace std;
40
41#ifdef VBOX_WITH_UNATTENDED
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47static const char g_szPrefix[] = "@@VBOX_";
48static const char g_szPrefixInsert[] = "@@VBOX_INSERT";
49static const char g_szPrefixInsertXxx[] = "@@VBOX_INSERT_";
50static const char g_szPrefixInsertExpr[] = "@@VBOX_INSERT[";
51static const char g_szPrefixCond[] = "@@VBOX_COND";
52static const char g_szPrefixCondXxx[] = "@@VBOX_COND_";
53static const char g_szPrefixCondExpr[] = "@@VBOX_COND[";
54static const char g_szPrefixCondElse[] = "@@VBOX_COND_ELSE@@";
55static const char g_szPrefixCondEnd[] = "@@VBOX_COND_END@@";
56static const char g_szPrefixSplitter[] = "@@VBOX_SPLITTER";
57
58
59/*********************************************************************************************************************************
60* UnattendedScriptTemplate Implementation *
61*********************************************************************************************************************************/
62
63UnattendedScriptTemplate::UnattendedScriptTemplate(Unattended *pUnattended, const char *pszDefaultTemplateFilename,
64 const char *pszDefaultFilename)
65 : BaseTextScript(pUnattended, pszDefaultTemplateFilename, pszDefaultFilename), mpUnattended(pUnattended)
66{
67}
68
69HRESULT UnattendedScriptTemplate::saveToString(Utf8Str &rStrDst)
70{
71 RTEXPREVAL hEvaluator = NIL_RTEXPREVAL;
72 int vrc = RTExprEvalCreate(&hEvaluator, 0, "unattended", this, UnattendedScriptTemplate::queryVariableForExpr);
73 AssertRCReturn(vrc, mpSetError->setErrorVrc(vrc));
74
75 struct
76 {
77 bool fSavedOutputting;
78 } aConds[8];
79 unsigned cConds = 0;
80 bool fOutputting = true;
81 HRESULT hrc = E_FAIL;
82 size_t offTemplate = 0;
83 size_t cchTemplate = mStrScriptFullContent.length();
84 rStrDst.setNull();
85 for (;;)
86 {
87 /*
88 * Find the next placeholder and add any text before it to the output.
89 */
90 size_t offPlaceholder = mStrScriptFullContent.find(g_szPrefix, offTemplate);
91 size_t cchToCopy = offPlaceholder != RTCString::npos ? offPlaceholder - offTemplate : cchTemplate - offTemplate;
92 if (cchToCopy > 0)
93 {
94 if (fOutputting)
95 {
96 try
97 {
98 rStrDst.append(mStrScriptFullContent, offTemplate , cchToCopy);
99 }
100 catch (std::bad_alloc &)
101 {
102 hrc = E_OUTOFMEMORY;
103 break;
104 }
105 }
106 offTemplate += cchToCopy;
107 }
108
109 /*
110 * Process placeholder.
111 */
112 if (offPlaceholder != RTCString::npos)
113 {
114 /*
115 * First we must find the end of the placeholder string.
116 */
117 size_t const cchMaxPlaceholder = RT_MIN(cchTemplate - offPlaceholder, _1K);
118 const char *pszPlaceholder = mStrScriptFullContent.c_str() + offPlaceholder;
119 size_t cchPlaceholder = sizeof(g_szPrefix) - 1;
120 char ch;
121 while ( offPlaceholder + cchPlaceholder < cchTemplate
122 && (ch = pszPlaceholder[cchPlaceholder]) != '\0'
123 && (RT_C_IS_PRINT(ch) || RT_C_IS_SPACE(ch))
124 && ch != '@')
125 cchPlaceholder++;
126
127 if ( offPlaceholder + cchPlaceholder < cchTemplate
128 && pszPlaceholder[cchPlaceholder] == '@')
129 {
130 cchPlaceholder++;
131 if ( offPlaceholder + cchPlaceholder < cchTemplate
132 && pszPlaceholder[cchPlaceholder] == '@')
133 cchPlaceholder++;
134 }
135
136 if ( pszPlaceholder[cchPlaceholder - 1] != '@'
137 || pszPlaceholder[cchPlaceholder - 2] != '@'
138 || ( strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixInsert)) != 0
139 && strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixCond)) != 0
140 && strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixSplitter)) != 0 ) )
141 {
142 hrc = mpSetError->setError(E_FAIL, tr("Malformed or too long template placeholder '%.*s'"),
143 cchPlaceholder, pszPlaceholder);
144 break;
145 }
146
147 offTemplate += cchPlaceholder;
148
149 /*
150 * @@VBOX_INSERT_XXX@@:
151 */
152 if (strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixInsertXxx)) == 0)
153 {
154 /*
155 * Get the placeholder value and add it to the output.
156 */
157 RTCString strValue;
158 hrc = getReplacement(pszPlaceholder, cchPlaceholder, fOutputting, strValue);
159 if (SUCCEEDED(hrc))
160 {
161 if (fOutputting)
162 {
163 try
164 {
165 rStrDst.append(strValue);
166 }
167 catch (std::bad_alloc &)
168 {
169 hrc = E_OUTOFMEMORY;
170 break;
171 }
172 }
173 }
174 else
175 break;
176 }
177 /*
178 * @@VBOX_INSERT[expr]@@:
179 * @@VBOX_INSERT[expr]SH@@:
180 * @@VBOX_INSERT[expr]ELEMENT@@:
181 * @@VBOX_INSERT[expr]ATTRIB_DQ@@:
182 */
183 else if (strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixInsertExpr)) == 0)
184 {
185 /*
186 * Get the placeholder value and add it to the output.
187 */
188 char *pszValue = NULL;
189 hrc = getReplacementForExpr(hEvaluator, pszPlaceholder, cchPlaceholder, fOutputting, &pszValue);
190 if (SUCCEEDED(hrc))
191 {
192 if (fOutputting && pszValue)
193 {
194 try
195 {
196 rStrDst.append(pszValue);
197 }
198 catch (std::bad_alloc &)
199 {
200 hrc = E_OUTOFMEMORY;
201 break;
202 }
203 }
204 RTStrFree(pszValue);
205 }
206 else
207 break;
208 }
209 /*
210 * @@VBOX_COND_END@@: Pop one item of the conditional stack.
211 */
212 else if (strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixCondEnd)) == 0)
213 {
214 if (cConds > 0)
215 {
216 cConds--;
217 fOutputting = aConds[cConds].fSavedOutputting;
218 }
219 else
220 {
221 hrc = mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR,
222 tr("%s without @@VBOX_COND_XXX@@ at offset %zu (%#zx)"),
223 g_szPrefixCondEnd, offPlaceholder, offPlaceholder);
224 break;
225 }
226 }
227 /*
228 * @@VBOX_COND_ELSE@@: Flip the output setting of the current condition.
229 */
230 else if (strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixCondElse)) == 0)
231 {
232 if (cConds > 0)
233 fOutputting = !fOutputting;
234 else
235 {
236 hrc = mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR,
237 tr("%s without @@VBOX_COND_XXX@@ at offset %zu (%#zx)"),
238 g_szPrefixCondElse, offPlaceholder, offPlaceholder);
239 break;
240 }
241 }
242 /*
243 * @@VBOX_COND_XXX@@: Push the previous outputting state and combine it with the
244 * one from the condition.
245 */
246 else if (strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixCondXxx)) == 0)
247 {
248 if (cConds + 1 < RT_ELEMENTS(aConds))
249 {
250 aConds[cConds].fSavedOutputting = fOutputting;
251 bool fNewOutputting = fOutputting;
252 hrc = getConditional(pszPlaceholder, cchPlaceholder, &fNewOutputting);
253 if (SUCCEEDED(hrc))
254 fOutputting = fOutputting && fNewOutputting;
255 else
256 break;
257 cConds++;
258 }
259 else
260 {
261 hrc = mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR,
262 tr("Too deep conditional nesting at offset %zu (%#zx)"),
263 offPlaceholder, offPlaceholder);
264 break;
265 }
266 }
267 /*
268 * @@VBOX_COND[expr]@@: Push the previous outputting state and combine it with the
269 * one from the condition.
270 */
271 else if (strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixCondExpr)) == 0)
272 {
273 if (cConds + 1 < RT_ELEMENTS(aConds))
274 {
275 aConds[cConds].fSavedOutputting = fOutputting;
276 bool fNewOutputting = fOutputting;
277 hrc = resolveConditionalExpr(hEvaluator, pszPlaceholder, cchPlaceholder, &fNewOutputting);
278 if (SUCCEEDED(hrc))
279 fOutputting = fOutputting && fNewOutputting;
280 else
281 break;
282 cConds++;
283 }
284 else
285 {
286 hrc = mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR,
287 tr("Too deep conditional nesting at offset %zu (%#zx)"),
288 offPlaceholder, offPlaceholder);
289 break;
290 }
291 }
292 /*
293 * @@VBOX_SPLITTER_START/END[filename]@@: Ignored in this pass.
294 */
295 else
296 {
297 Assert(strncmp(pszPlaceholder, RT_STR_TUPLE(g_szPrefixSplitter)) == 0);
298 if (fOutputting)
299 {
300 try
301 {
302 rStrDst.append(pszPlaceholder, cchPlaceholder);
303 }
304 catch (std::bad_alloc &)
305 {
306 hrc = E_OUTOFMEMORY;
307 break;
308 }
309 }
310 }
311 }
312
313 /*
314 * Done?
315 */
316 if (offTemplate >= cchTemplate)
317 {
318 if (cConds == 0)
319 return S_OK;
320 if (cConds == 1)
321 hrc = mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR, tr("Missing @@VBOX_COND_END@@"));
322 else
323 hrc = mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR, tr("Missing %u @@VBOX_COND_END@@"), cConds);
324 break;
325 }
326 }
327
328 /* failed */
329 rStrDst.setNull();
330 RTExprEvalRelease(hEvaluator);
331 return hrc;
332}
333
334HRESULT UnattendedScriptTemplate::getReplacement(const char *pachPlaceholder, size_t cchPlaceholder,
335 bool fOutputting, RTCString &rValue)
336{
337 /*
338 * Check for an escaping suffix. Drop the '@@'.
339 */
340 size_t const cchFullPlaceholder = cchPlaceholder;
341 kEvalEscaping_T enmEscaping;
342#define PLACEHOLDER_ENDS_WITH(a_szSuffix) \
343 ( cchPlaceholder > sizeof(a_szSuffix) - 1U \
344 && memcmp(&pachPlaceholder[cchPlaceholder - sizeof(a_szSuffix) + 1U], a_szSuffix, sizeof(a_szSuffix) - 1U) == 0)
345 if (PLACEHOLDER_ENDS_WITH("_SH@@"))
346 {
347 cchPlaceholder -= 3 + 2;
348 enmEscaping = kValueEscaping_Bourne;
349 }
350 else if (PLACEHOLDER_ENDS_WITH("_ELEMENT@@"))
351 {
352 cchPlaceholder -= 8 + 2;
353 enmEscaping = kValueEscaping_XML_Element;
354 }
355 else if (PLACEHOLDER_ENDS_WITH("_ATTRIB_DQ@@"))
356 {
357 cchPlaceholder -= 10 + 2;
358 enmEscaping = kValueEscaping_XML_Attribute_Double_Quotes;
359 }
360 else
361 {
362 Assert(PLACEHOLDER_ENDS_WITH("@@"));
363 cchPlaceholder -= 2;
364 enmEscaping = kValueEscaping_None;
365 }
366#undef PLACEHOLDER_ENDS_WITH
367
368 /*
369 * Resolve and escape the value.
370 */
371 HRESULT hrc;
372 try
373 {
374 Utf8Str strTmp;
375 const char *pszReadOnlyValue = NULL;
376 int vrc = queryVariable(pachPlaceholder + sizeof(g_szPrefixInsertXxx) - 1,
377 cchPlaceholder - sizeof(g_szPrefixInsertXxx) + 1,
378 strTmp, fOutputting ? &pszReadOnlyValue : NULL);
379 if (RT_SUCCESS(vrc))
380 {
381 if (fOutputting)
382 {
383 Assert(pszReadOnlyValue != NULL);
384 switch (enmEscaping)
385 {
386 case kValueEscaping_None:
387 rValue = pszReadOnlyValue;
388 return S_OK;
389
390 case kValueEscaping_Bourne:
391 case kValueEscaping_XML_Element:
392 case kValueEscaping_XML_Attribute_Double_Quotes:
393 {
394 switch (enmEscaping)
395 {
396 case kValueEscaping_Bourne:
397 {
398 const char * const papszArgs[2] = { pszReadOnlyValue, NULL };
399 char *pszEscaped = NULL;
400 vrc = RTGetOptArgvToString(&pszEscaped, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
401 if (RT_SUCCESS(vrc))
402 {
403 try
404 {
405 rValue = pszEscaped;
406 RTStrFree(pszEscaped);
407 return S_OK;
408 }
409 catch (std::bad_alloc &)
410 {
411 hrc = E_OUTOFMEMORY;
412 }
413 RTStrFree(pszEscaped);
414 }
415 else
416 hrc = mpSetError->setErrorVrc(vrc);
417 break;
418 }
419
420 case kValueEscaping_XML_Element:
421 rValue.printf("%RMes", pszReadOnlyValue);
422 return S_OK;
423
424 case kValueEscaping_XML_Attribute_Double_Quotes:
425 {
426 RTCString strTmp2;
427 strTmp2.printf("%RMas", pszReadOnlyValue);
428 rValue = RTCString(strTmp2, 1, strTmp2.length() - 2);
429 return S_OK;
430 }
431
432 default:
433 hrc = E_FAIL;
434 break;
435 }
436 break;
437 }
438
439 default:
440 AssertFailedStmt(hrc = E_FAIL);
441 break;
442 }
443 }
444 else
445 hrc = S_OK;
446 }
447 else
448 hrc = E_FAIL;
449 }
450 catch (std::bad_alloc &)
451 {
452 hrc = E_OUTOFMEMORY;
453 }
454 rValue.setNull();
455 return hrc;
456}
457
458HRESULT UnattendedScriptTemplate::getReplacementForExpr(RTEXPREVAL hEvaluator, const char *pachPlaceholder, size_t cchPlaceholder,
459 bool fOutputting, char **ppszValue) RT_NOEXCEPT
460{
461 /*
462 * Process the tail of the placeholder to figure out the escaping rules.
463 *
464 * @@VBOX_INSERT[expr]@@:
465 * @@VBOX_INSERT[expr]SH@@:
466 * @@VBOX_INSERT[expr]ELEMENT@@:
467 * @@VBOX_INSERT[expr]ATTRIB_DQ@@:
468 */
469 size_t const cchFullPlaceholder = cchPlaceholder;
470 kEvalEscaping_T enmEscaping;
471#define PLACEHOLDER_ENDS_WITH(a_szSuffix) \
472 ( cchPlaceholder > sizeof(a_szSuffix) - 1U \
473 && memcmp(&pachPlaceholder[cchPlaceholder - sizeof(a_szSuffix) + 1U], a_szSuffix, sizeof(a_szSuffix) - 1U) == 0)
474 if (PLACEHOLDER_ENDS_WITH("]SH@@"))
475 {
476 cchPlaceholder -= sizeof("]SH@@") - 1;
477 enmEscaping = kValueEscaping_Bourne;
478 }
479 else if (PLACEHOLDER_ENDS_WITH("]ELEMENT@@"))
480 {
481 cchPlaceholder -= sizeof("]ELEMENT@@") - 1;
482 enmEscaping = kValueEscaping_XML_Element;
483 }
484 else if (PLACEHOLDER_ENDS_WITH("]ATTRIB_DQ@@"))
485 {
486 cchPlaceholder -= sizeof("]ATTRIB_DQ@@") - 1;
487 enmEscaping = kValueEscaping_XML_Attribute_Double_Quotes;
488 }
489 else if (PLACEHOLDER_ENDS_WITH("]@@"))
490 {
491 cchPlaceholder -= sizeof("]@@") - 1;
492 enmEscaping = kValueEscaping_None;
493 }
494 else
495 return mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR, tr("Malformed @@VBOX_INSERT[expr]@@: Missing ']' (%.*s)"),
496 cchPlaceholder, pachPlaceholder);
497#undef PLACEHOLDER_ENDS_WITH
498
499 /* The placeholder prefix length. The expression is from cchPrefix to cchPlaceholder. */
500 size_t const cchPrefix = sizeof(g_szPrefixInsertExpr) - 1;
501 Assert(pachPlaceholder[cchPrefix - 1] == '[');
502
503 /*
504 * Evaluate the expression. We do this regardless of fOutput for now.
505 */
506 RTERRINFOSTATIC ErrInfo;
507 char *pszValue = NULL;
508 int vrc = RTExprEvalToString(hEvaluator, &pachPlaceholder[cchPrefix], cchPlaceholder - cchPrefix, &pszValue,
509 RTErrInfoInitStatic(&ErrInfo));
510 LogFlowFunc(("RTExprEvalToString(%.*s) -> %Rrc pszValue=%s\n",
511 cchPlaceholder - cchPrefix, &pachPlaceholder[cchPrefix], vrc, pszValue));
512 if (RT_SUCCESS(vrc))
513 {
514 if (fOutputting)
515 {
516 switch (enmEscaping)
517 {
518 case kValueEscaping_None:
519 *ppszValue = pszValue;
520 pszValue = NULL;
521 break;
522
523 case kValueEscaping_Bourne:
524 {
525 const char * const papszArgs[2] = { pszValue, NULL };
526 vrc = RTGetOptArgvToString(ppszValue, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
527 break;
528 }
529
530 case kValueEscaping_XML_Element:
531 vrc = RTStrAPrintf(ppszValue, "%RMes", pszValue);
532 break;
533
534 case kValueEscaping_XML_Attribute_Double_Quotes:
535 vrc = RTStrAPrintf(ppszValue, "%RMas", pszValue);
536 if (RT_SUCCESS(vrc))
537 {
538 /* drop the quotes */
539 char *pszRet = *ppszValue;
540 size_t const cchRet = strlen(pszRet) - 2;
541 memmove(pszRet, &pszRet[1], cchRet);
542 pszRet[cchRet] = '\0';
543 }
544 break;
545
546 default:
547 AssertFailedStmt(vrc = VERR_IPE_NOT_REACHED_DEFAULT_CASE);
548 break;
549 }
550 RTStrFree(pszValue);
551 if (RT_FAILURE(vrc))
552 return mpSetError->setErrorVrc(vrc);
553 }
554 else
555 {
556 *ppszValue = NULL;
557 RTStrFree(pszValue);
558 }
559 }
560 else
561 return mpSetError->setErrorBoth(E_FAIL, vrc, tr("Expression evaluation error for '%.*s': %#RTeic"),
562 cchPlaceholder, pachPlaceholder, &ErrInfo.Core);
563 return S_OK;
564}
565
566HRESULT UnattendedScriptTemplate::resolveConditionalExpr(RTEXPREVAL hEvaluator, const char *pachPlaceholder,
567 size_t cchPlaceholder, bool *pfOutputting) RT_NOEXCEPT
568{
569 /*
570 * Check the placeholder tail: @@VBOX_COND[expr]@@
571 */
572 static const char s_szTail[] = "]@@";
573 if (memcmp(&pachPlaceholder[cchPlaceholder - sizeof(s_szTail) + 1], RT_STR_TUPLE(s_szTail)) != 0)
574 return mpSetError->setErrorBoth(E_FAIL, VERR_PARSE_ERROR, tr("Malformed @@VBOX_COND[expr]@@: Missing ']' (%.*s)"),
575 cchPlaceholder, pachPlaceholder);
576 Assert(pachPlaceholder[sizeof(g_szPrefixCondExpr) - 2 ] == '[');
577
578 /*
579 * Evaluate the expression.
580 */
581 RTERRINFOSTATIC ErrInfo;
582 const char * const pchExpr = &pachPlaceholder[sizeof(g_szPrefixCondExpr) - 1];
583 size_t const cchExpr = cchPlaceholder - sizeof(g_szPrefixCondExpr) + 1 - sizeof(s_szTail) + 1;
584 int vrc = RTExprEvalToBool(hEvaluator, pchExpr, cchExpr, pfOutputting, RTErrInfoInitStatic(&ErrInfo));
585 LogFlowFunc(("RTExprEvalToBool(%.*s) -> %Rrc *pfOutputting=%s\n", cchExpr, pchExpr, vrc, *pfOutputting));
586 if (RT_SUCCESS(vrc))
587 return S_OK;
588 return mpSetError->setErrorBoth(E_FAIL, vrc, tr("Expression evaluation error for '%.*s': %#RTeic"),
589 cchPlaceholder, pachPlaceholder, &ErrInfo.Core);
590}
591
592/*static */ DECLCALLBACK(int)
593UnattendedScriptTemplate::queryVariableForExpr(const char *pchName, size_t cchName, void *pvUser, char **ppszValue)
594{
595 UnattendedScriptTemplate *pThis = (UnattendedScriptTemplate *)pvUser;
596 int vrc;
597 try
598 {
599 const char *pszReadOnlyValue = NULL;
600 Utf8Str strTmp;
601 vrc = pThis->queryVariable(pchName, cchName, strTmp, ppszValue ? &pszReadOnlyValue : NULL);
602 if (ppszValue)
603 {
604 if (RT_SUCCESS(vrc))
605 vrc = RTStrDupEx(ppszValue, pszReadOnlyValue);
606 else
607 *ppszValue = NULL;
608 }
609 }
610 catch (std::bad_alloc &)
611 {
612 vrc = VERR_NO_MEMORY;
613 *ppszValue = NULL;
614 }
615 return vrc;
616}
617
618int UnattendedScriptTemplate::queryVariable(const char *pchName, size_t cchName, Utf8Str &rstrTmp, const char **ppszValue)
619{
620#define IS_MATCH(a_szMatch) \
621 (cchName == sizeof(a_szMatch) - 1U && memcmp(pchName, a_szMatch, sizeof(a_szMatch) - 1U) == 0)
622
623 const char *pszValue;
624
625 /*
626 * Variables
627 */
628 if (IS_MATCH("USER_LOGIN"))
629 pszValue = mpUnattended->i_getUser().c_str();
630 else if (IS_MATCH("USER_PASSWORD"))
631 pszValue = mpUnattended->i_getPassword().c_str();
632 else if (IS_MATCH("ROOT_PASSWORD"))
633 pszValue = mpUnattended->i_getPassword().c_str();
634 else if (IS_MATCH("USER_FULL_NAME"))
635 pszValue = mpUnattended->i_getFullUserName().c_str();
636 else if (IS_MATCH("PRODUCT_KEY"))
637 pszValue = mpUnattended->i_getProductKey().c_str();
638 else if (IS_MATCH("POST_INSTALL_COMMAND"))
639 pszValue = mpUnattended->i_getPostInstallCommand().c_str();
640 else if (IS_MATCH("AUXILIARY_INSTALL_DIR"))
641 pszValue = mpUnattended->i_getAuxiliaryInstallDir().c_str();
642 else if (IS_MATCH("IMAGE_INDEX"))
643 pszValue = rstrTmp.printf("%u", mpUnattended->i_getImageIndex()).c_str();
644 else if (IS_MATCH("OS_ARCH"))
645 pszValue = mpUnattended->i_isGuestOs64Bit() ? "amd64" : "x86";
646 else if (IS_MATCH("OS_ARCH2"))
647 pszValue = mpUnattended->i_isGuestOs64Bit() ? "x86_64" : "x86";
648 else if (IS_MATCH("OS_ARCH3"))
649 pszValue = mpUnattended->i_isGuestOs64Bit() ? "x86_64" : "i386";
650 else if (IS_MATCH("OS_ARCH4"))
651 pszValue = mpUnattended->i_isGuestOs64Bit() ? "x86_64" : "i486";
652 else if (IS_MATCH("OS_ARCH6"))
653 pszValue = mpUnattended->i_isGuestOs64Bit() ? "x86_64" : "i686";
654 else if (IS_MATCH("GUEST_OS_VERSION"))
655 pszValue = mpUnattended->i_getDetectedOSVersion().c_str();
656 else if (IS_MATCH("GUEST_OS_MAJOR_VERSION"))
657 {
658 Utf8Str const &rstrOsVer = mpUnattended->i_getDetectedOSVersion();
659 size_t offDot = rstrOsVer.find('.');
660 if (offDot > 0 && offDot != Utf8Str::npos)
661 pszValue = rstrTmp.assign(rstrOsVer, 0, offDot).c_str(); /* caller catches std::bad_alloc */
662 else if (!ppszValue)
663 return VERR_NOT_FOUND;
664 else
665 {
666 mpSetError->setErrorBoth(E_FAIL, VERR_NO_DATA, tr("Unknown guest OS major version '%s'"), rstrOsVer.c_str());
667 return VERR_NO_DATA;
668 }
669 }
670 else if (IS_MATCH("TIME_ZONE_UX"))
671 pszValue = mpUnattended->i_getTimeZoneInfo()
672 ? mpUnattended->i_getTimeZoneInfo()->pszUnixName : mpUnattended->i_getTimeZone().c_str();
673 else if (IS_MATCH("TIME_ZONE_WIN_NAME"))
674 {
675 PCRTTIMEZONEINFO pInfo = mpUnattended->i_getTimeZoneInfo();
676 if (pInfo)
677 pszValue = pInfo->pszWindowsName ? pInfo->pszWindowsName : "GMT";
678 else
679 pszValue = mpUnattended->i_getTimeZone().c_str();
680 }
681 else if (IS_MATCH("TIME_ZONE_WIN_INDEX"))
682 {
683 PCRTTIMEZONEINFO pInfo = mpUnattended->i_getTimeZoneInfo();
684 if (pInfo)
685 pszValue = rstrTmp.printf("%u", pInfo->idxWindows ? pInfo->idxWindows : 85 /*GMT*/).c_str();
686 else
687 pszValue = mpUnattended->i_getTimeZone().c_str();
688 }
689 else if (IS_MATCH("LOCALE"))
690 pszValue = mpUnattended->i_getLocale().c_str();
691 else if (IS_MATCH("DASH_LOCALE"))
692 {
693 Assert(mpUnattended->i_getLocale()[2] == '_');
694 pszValue = rstrTmp.assign(mpUnattended->i_getLocale()).replace(2, 1, "-").c_str();
695 }
696 else if (IS_MATCH("LANGUAGE"))
697 pszValue = mpUnattended->i_getLanguage().c_str();
698 else if (IS_MATCH("COUNTRY"))
699 pszValue = mpUnattended->i_getCountry().c_str();
700 else if (IS_MATCH("HOSTNAME_FQDN"))
701 pszValue = mpUnattended->i_getHostname().c_str();
702 else if (IS_MATCH("HOSTNAME_WITHOUT_DOMAIN"))
703 pszValue = rstrTmp.assign(mpUnattended->i_getHostname(), 0, mpUnattended->i_getHostname().find(".")).c_str();
704 else if (IS_MATCH("HOSTNAME_WITHOUT_DOMAIN_MAX_15"))
705 pszValue = rstrTmp.assign(mpUnattended->i_getHostname(), 0, RT_MIN(mpUnattended->i_getHostname().find("."), 15)).c_str();
706 else if (IS_MATCH("HOSTNAME_DOMAIN"))
707 pszValue = rstrTmp.assign(mpUnattended->i_getHostname(), mpUnattended->i_getHostname().find(".") + 1).c_str();
708 else if (IS_MATCH("PROXY"))
709 pszValue = mpUnattended->i_getProxy().c_str();
710 /*
711 * Indicator variables.
712 */
713 else if (IS_MATCH("IS_INSTALLING_ADDITIONS"))
714 pszValue = mpUnattended->i_getInstallGuestAdditions() ? "1" : "0";
715 else if (IS_MATCH("IS_USER_LOGIN_ADMINISTRATOR"))
716 pszValue = mpUnattended->i_getUser().compare("Administrator", RTCString::CaseInsensitive) == 0 ? "1" : "0";
717 else if (IS_MATCH("IS_INSTALLING_TEST_EXEC_SERVICE"))
718 pszValue = mpUnattended->i_getInstallTestExecService() ? "1" : "0";
719 else if (IS_MATCH("HAS_POST_INSTALL_COMMAND"))
720 pszValue = mpUnattended->i_getPostInstallCommand().isNotEmpty() ? "1" : "0";
721 else if (IS_MATCH("HAS_PRODUCT_KEY"))
722 pszValue = mpUnattended->i_getProductKey().isNotEmpty() ? "1" : "0";
723 else if (IS_MATCH("IS_MINIMAL_INSTALLATION"))
724 pszValue = mpUnattended->i_isMinimalInstallation() ? "1" : "0";
725 else if (IS_MATCH("IS_FIRMWARE_UEFI"))
726 pszValue = mpUnattended->i_isFirmwareEFI() ? "1" : "0";
727 else if (IS_MATCH("IS_RTC_USING_UTC"))
728 pszValue = mpUnattended->i_isRtcUsingUtc() ? "1" : "0";
729 else if (IS_MATCH("HAS_PROXY"))
730 pszValue = mpUnattended->i_getProxy().isNotEmpty() ? "1" : "0";
731 /*
732 * Unknown variable.
733 */
734 else if (!ppszValue)
735 return VERR_NOT_FOUND;
736 else
737 {
738 mpSetError->setErrorBoth(E_FAIL, VERR_NOT_FOUND, tr("Unknown variable '%.*s'"), cchName, pchName);
739 return VERR_NO_DATA;
740 }
741 if (ppszValue)
742 *ppszValue = pszValue;
743 return VINF_SUCCESS;
744}
745
746HRESULT UnattendedScriptTemplate::getConditional(const char *pachPlaceholder, size_t cchPlaceholder, bool *pfOutputting)
747{
748#define IS_PLACEHOLDER_MATCH(a_szMatch) \
749 ( cchPlaceholder == sizeof("@@VBOX_COND_" a_szMatch "@@") - 1U \
750 && memcmp(pachPlaceholder, "@@VBOX_COND_" a_szMatch "@@", sizeof("@@VBOX_COND_" a_szMatch "@@") - 1U) == 0)
751#define IS_PLACEHOLDER_PARTIALLY_MATCH(a_szMatch) \
752 (memcmp(pachPlaceholder, "@@VBOX_COND_" a_szMatch, sizeof("@@VBOX_COND_" a_szMatch) - 1U) == 0)
753
754 /* Install Guest Additions: */
755 if (IS_PLACEHOLDER_MATCH("IS_INSTALLING_ADDITIONS"))
756 *pfOutputting = mpUnattended->i_getInstallGuestAdditions();
757 else if (IS_PLACEHOLDER_MATCH("IS_NOT_INSTALLING_ADDITIONS"))
758 *pfOutputting = !mpUnattended->i_getInstallGuestAdditions();
759 /* User == Administrator: */
760 else if (IS_PLACEHOLDER_MATCH("IS_USER_LOGIN_ADMINISTRATOR"))
761 *pfOutputting = mpUnattended->i_getUser().compare("Administrator", RTCString::CaseInsensitive) == 0;
762 else if (IS_PLACEHOLDER_MATCH("IS_USER_LOGIN_NOT_ADMINISTRATOR"))
763 *pfOutputting = mpUnattended->i_getUser().compare("Administrator", RTCString::CaseInsensitive) != 0;
764 /* Install TXS: */
765 else if (IS_PLACEHOLDER_MATCH("IS_INSTALLING_TEST_EXEC_SERVICE"))
766 *pfOutputting = mpUnattended->i_getInstallTestExecService();
767 else if (IS_PLACEHOLDER_MATCH("IS_NOT_INSTALLING_TEST_EXEC_SERVICE"))
768 *pfOutputting = !mpUnattended->i_getInstallTestExecService();
769 /* Post install command: */
770 else if (IS_PLACEHOLDER_MATCH("HAS_POST_INSTALL_COMMAND"))
771 *pfOutputting = mpUnattended->i_getPostInstallCommand().isNotEmpty();
772 else if (IS_PLACEHOLDER_MATCH("HAS_NO_POST_INSTALL_COMMAND"))
773 *pfOutputting = mpUnattended->i_getPostInstallCommand().isEmpty();
774 /* Product key: */
775 else if (IS_PLACEHOLDER_MATCH("HAS_PRODUCT_KEY"))
776 *pfOutputting = mpUnattended->i_getProductKey().isNotEmpty();
777 else if (IS_PLACEHOLDER_MATCH("HAS_NO_PRODUCT_KEY"))
778 *pfOutputting = mpUnattended->i_getProductKey().isEmpty();
779 /* Minimal installation: */
780 else if (IS_PLACEHOLDER_MATCH("IS_MINIMAL_INSTALLATION"))
781 *pfOutputting = mpUnattended->i_isMinimalInstallation();
782 else if (IS_PLACEHOLDER_MATCH("IS_NOT_MINIMAL_INSTALLATION"))
783 *pfOutputting = !mpUnattended->i_isMinimalInstallation();
784 /* Is firmware UEFI: */
785 else if (IS_PLACEHOLDER_MATCH("IS_FIRMWARE_UEFI"))
786 *pfOutputting = mpUnattended->i_isFirmwareEFI();
787 else if (IS_PLACEHOLDER_MATCH("IS_NOT_FIRMWARE_UEFI"))
788 *pfOutputting = !mpUnattended->i_isFirmwareEFI();
789 /* Is RTC using UTC (i.e. set to UTC time on startup): */
790 else if (IS_PLACEHOLDER_MATCH("IS_RTC_USING_UTC"))
791 *pfOutputting = mpUnattended->i_isRtcUsingUtc();
792 else if (IS_PLACEHOLDER_MATCH("IS_NOT_RTC_USING_UTC"))
793 *pfOutputting = !mpUnattended->i_isRtcUsingUtc();
794 else if (IS_PLACEHOLDER_MATCH("HAS_PROXY"))
795 *pfOutputting = mpUnattended->i_getProxy().isNotEmpty();
796 else if (IS_PLACEHOLDER_PARTIALLY_MATCH("GUEST_VERSION"))
797 {
798 //parse the placeholder and extract the OS version from there
799 RTCString strPlaceHolder(pachPlaceholder); /** @todo r=bird: What's the meaning of duplicating the rest of the script here
800 * when you could just add cchPlaceholder to the parameter list and limit it to
801 * what is actually needed. OTOH it's really not needed to make copies here,
802 * validating the "[" can be done in the partial match above and "]@@" by using
803 * cchPlaceholder, what you wnat to get at is inbetween and does not need
804 * two copies (only one for RTStrVersionCompare). */
805 size_t startPos = sizeof("@@VBOX_COND_GUEST_VERSION") - 1;//-1 is for '\n'
806 size_t endPos = strPlaceHolder.find("@@", startPos + 2);
807 //next part should look like [>8.0.0] for example where:
808 // - "[,]" is just the brackets to wrap up the condition;
809 // - ">" is "greater". Also possible comparison is "<";
810 // - 8.0.0 is required guest OS version.
811 //The end of placeholder is "@@" like for others.
812
813 /** @todo r=bird: What kind of syntax checking is this? Ignore any kind of
814 * mistyped stuff and let the user figure out what he did wrong
815 * without clues? Lazy. */
816 if ( strPlaceHolder[endPos] == '@'
817 && strPlaceHolder[endPos+1] == '@' )
818 {
819 if ( strPlaceHolder[startPos++] == '[' && strPlaceHolder[--endPos] == ']' )
820 {
821 char chComp = strPlaceHolder[startPos++];
822 RTCString strRequiredOSVersion = strPlaceHolder.substr(startPos, endPos - startPos);
823 RTCString strDetectedOSVersion = mpUnattended->i_getDetectedOSVersion();
824 int res = RTStrVersionCompare(strDetectedOSVersion.c_str(), strRequiredOSVersion.c_str());
825 if ( res >= 0 && chComp == '>' )
826 *pfOutputting = true;
827 else if ( res < 0 && chComp == '<' )
828 *pfOutputting = true;
829 else
830 *pfOutputting = false;
831 }
832 }
833 else
834 *pfOutputting = false;//initially is set to "false"
835 }
836 else
837 return mpSetError->setErrorBoth(E_FAIL, VERR_NOT_FOUND, tr("Unknown conditional placeholder '%.*s'"),
838 cchPlaceholder, pachPlaceholder);
839 return S_OK;
840#undef IS_PLACEHOLDER_MATCH
841}
842
843#endif /* VBOX_WITH_UNATTENDED */
844#if 0 /* Keeping this a reference */
845
846
847/*********************************************************************************************************************************
848* UnattendedSUSEXMLScript Implementation *
849*********************************************************************************************************************************/
850
851HRESULT UnattendedSUSEXMLScript::parse()
852{
853 HRESULT hrc = UnattendedXMLScript::parse();
854 if (SUCCEEDED(hrc))
855 {
856 /*
857 * Check that we've got the right root element type.
858 */
859 const xml::ElementNode *pelmRoot = mDoc.getRootElement();
860 if ( pelmRoot
861 && strcmp(pelmRoot->getName(), "profile") == 0)
862 {
863 /*
864 * Work thought the sections.
865 */
866 try
867 {
868 LoopThruSections(pelmRoot);
869 hrc = S_OK;
870 }
871 catch (std::bad_alloc &)
872 {
873 hrc = E_OUTOFMEMORY;
874 }
875 }
876 else if (pelmRoot)
877 hrc = mpSetError->setError(E_FAIL, tr("XML document root element is '%s' instead of 'profile'"),
878 pelmRoot->getName());
879 else
880 hrc = mpSetError->setError(E_FAIL, tr("Missing XML root element"));
881 }
882 return hrc;
883}
884
885HRESULT UnattendedSUSEXMLScript::setFieldInElement(xml::ElementNode *pElement, const DataId enmDataId, const Utf8Str &rStrValue)
886{
887 /*
888 * Don't set empty values.
889 */
890 if (rStrValue.isEmpty())
891 {
892 Utf8Str strProbableValue;
893 try
894 {
895 strProbableValue = createProbableValue(enmDataId, pElement);
896 }
897 catch (std::bad_alloc &)
898 {
899 return E_OUTOFMEMORY;
900 }
901 return UnattendedXMLScript::setFieldInElement(pElement, enmDataId, strProbableValue);
902 }
903 return UnattendedXMLScript::setFieldInElement(pElement, enmDataId, rStrValue);
904}
905
906HRESULT UnattendedSUSEXMLScript::LoopThruSections(const xml::ElementNode *pelmRoot)
907{
908 xml::NodesLoop loopChildren(*pelmRoot);
909 const xml::ElementNode *pelmOuterLoop;
910 while ((pelmOuterLoop = loopChildren.forAllNodes()) != NULL)
911 {
912 const char *pcszElemName = pelmOuterLoop->getName();
913 if (!strcmp(pcszElemName, "users"))
914 {
915 xml::NodesLoop loopUsers(*pelmOuterLoop);
916 const xml::ElementNode *pelmUser;
917 while ((pelmUser = loopUsers.forAllNodes()) != NULL)
918 {
919 HRESULT hrc = HandleUserAccountsSection(pelmUser);
920 if (FAILED(hrc))
921 return hrc;
922 }
923 }
924 }
925 return S_OK;
926}
927
928HRESULT UnattendedSUSEXMLScript::HandleUserAccountsSection(const xml::ElementNode *pelmSection)
929{
930 xml::NodesLoop loopUser(*pelmSection);
931
932 const xml::ElementNode *pelmCur;
933 while ((pelmCur = loopUser.forAllNodes()) != NULL)
934 {
935 const char *pszValue = pelmCur->getValue();
936#ifdef LOG_ENABLED
937 if (!RTStrCmp(pelmCur->getName(), "uid"))
938 LogRelFunc(("UnattendedSUSEXMLScript::HandleUserAccountsSection profile/users/%s/%s = %s\n",
939 pelmSection->getName(), pelmCur->getName(), pszValue));
940#endif
941
942 if (!RTStrCmp(pszValue, "$homedir"))
943 mNodesForCorrectionMap.insert(make_pair(USERHOMEDIR_ID, pelmCur));
944
945 if (!RTStrCmp(pszValue, "$user"))
946 mNodesForCorrectionMap.insert(make_pair(USERNAME_ID, pelmCur));
947
948 if (!RTStrCmp(pszValue, "$password"))
949 mNodesForCorrectionMap.insert(make_pair(USERPASSWORD_ID, pelmCur));
950 }
951 return S_OK;
952}
953
954Utf8Str UnattendedSUSEXMLScript::createProbableValue(const DataId enmDataId, const xml::ElementNode *pCurElem)
955{
956 const xml::ElementNode *pElem = pCurElem;
957
958 switch (enmDataId)
959 {
960 case USERHOMEDIR_ID:
961// if ((pElem = pElem->findChildElement("home")))
962// {
963 return createProbableUserHomeDir(pElem);
964// }
965 break;
966 default:
967 break;
968 }
969
970 return Utf8Str::Empty;
971}
972
973Utf8Str UnattendedSUSEXMLScript::createProbableUserHomeDir(const xml::ElementNode *pCurElem)
974{
975 Utf8Str strCalcValue;
976 const xml::ElementNode *pElem = pCurElem->findNextSibilingElement("username");
977 if (pElem)
978 {
979 const char *pszValue = pElem->getValue();
980 strCalcValue = "/home/";
981 strCalcValue.append(pszValue);
982 }
983
984 return strCalcValue;
985}
986#endif /* just for reference */
987
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