VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/lightdm-greeter/vbox-greeter.cpp@ 70058

Last change on this file since 70058 was 70058, checked in by vboxsync, 7 years ago

GuestPropertySvc.h: Working on making it usable from C (VBoxGuest, ++)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.2 KB
Line 
1/* $Id: vbox-greeter.cpp 70058 2017-12-11 15:02:07Z vboxsync $ */
2/** @file
3 * vbox-greeter - an own LightDM greeter module supporting auto-logons
4 * controlled by the host.
5 */
6
7/*
8 * Copyright (C) 2012-2017 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*********************************************************************************************************************************
21* Header Files *
22*********************************************************************************************************************************/
23#define GLIB_DISABLE_DEPRECATION_WARNINGS 1 /* g_type_init() is deprecated */
24#include <pwd.h>
25#include <syslog.h>
26#include <stdlib.h>
27
28#include <lightdm.h>
29#ifdef VBOX_WITH_FLTK
30# include <FL/Fl.H>
31# include <FL/fl_ask.H> /* Yes, the casing is correct for 1.3.0 -- d'oh. */
32# include <FL/Fl_Box.H>
33# include <FL/Fl_Button.H>
34# include <FL/fl_draw.H> /* Same as above. */
35# include <FL/Fl_Double_Window.H>
36# include <FL/Fl_Input.H>
37# include <FL/Fl_Menu_Button.H>
38# ifdef VBOX_GREETER_WITH_PNG_SUPPORT
39# include <FL/Fl_PNG_Image.H>
40# include <FL/Fl_Shared_Image.H>
41# endif
42# include <FL/Fl_Secret_Input.H>
43#else
44# include <cairo-xlib.h>
45# include <gtk/gtk.h>
46# include <gdk/gdkx.h>
47#endif
48
49#include <package-generated.h>
50#include "product-generated.h"
51
52#include <iprt/assert.h>
53#include <iprt/buildconfig.h>
54#include <iprt/env.h>
55#include <iprt/file.h>
56#include <iprt/getopt.h>
57#include <iprt/initterm.h>
58#include <iprt/mem.h>
59#include <iprt/message.h>
60#include <iprt/path.h>
61#include <iprt/process.h>
62#include <iprt/stream.h>
63#include <iprt/system.h>
64#include <iprt/string.h>
65#include <iprt/thread.h>
66#include <iprt/time.h>
67
68#include <VBox/log.h>
69#include <VBox/VBoxGuestLib.h>
70
71/* The greeter's full name for logging. */
72#define VBOX_MODULE_NAME "vbox-lightdm-greeter"
73
74/* UI elements used in this greeter. */
75#define VBOX_GREETER_UI_WND_GREETER "wnd_greeter"
76
77#define VBOX_GREETER_UI_EDT_USER "edt_username"
78#define VBOX_GREETER_UI_EDT_PASSWORD "edt_password"
79#define VBOX_GREETER_UI_BTN_LOGIN "btn_login"
80#define VBOX_GREETER_UI_LBL_INFO "lbl_info"
81
82/* UI display options. */
83/** Show the restart menu entry / button. */
84#define VBOX_GREETER_UI_SHOW_RESTART RT_BIT(0)
85/** Show the shutdown menu entry / button. */
86#define VBOX_GREETER_UI_SHOW_SHUTDOWN RT_BIT(1)
87/** Show the (customized) top banner. */
88#define VBOX_GREETER_UI_SHOW_BANNER RT_BIT(2)
89/** Enable custom colors */
90#define VBOX_GREETER_UI_USE_THEMING RT_BIT(3)
91
92/** Extracts the 8-bit red component from an uint32_t. */
93#define VBOX_RGB_COLOR_RED(uColor) uColor & 0xFF
94/** Extracts the 8-bit green component from an uint32_t. */
95#define VBOX_RGB_COLOR_GREEN(uColor) (uColor >> 8) & 0xFF
96/** Extracts the 8-bit blue component from an uint32_t. */
97#define VBOX_RGB_COLOR_BLUE(uColor) (uColor >> 16) & 0xFF
98
99#include <VBox/log.h>
100#ifdef VBOX_WITH_GUEST_PROPS
101 #include <VBox/HostServices/GuestPropertySvc.h>
102 using namespace guestProp;
103#endif
104
105/** The program name (derived from argv[0]). */
106char *g_pszProgName = (char *)"";
107/** For debugging. */
108#ifdef DEBUG
109 static int g_iVerbosity = 99;
110#else
111 static int g_iVerbosity = 0;
112#endif
113static bool g_fRunning = true;
114
115/** Logging parameters. */
116/** @todo Make this configurable later. */
117static PRTLOGGER g_pLoggerRelease = NULL;
118static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
119static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
120static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
121
122/**
123 * Context structure which contains all needed
124 * data within callbacks.
125 */
126typedef struct VBOXGREETERCTX
127{
128 /** Pointer to this greeter instance. */
129 LightDMGreeter *pGreeter;
130#ifdef VBOX_WITH_FLTK
131 Fl_Button *pBtnLogin;
132 Fl_Input *pEdtUsername;
133 Fl_Secret_Input *pEdtPassword;
134 Fl_Box *pLblInfo;
135#else
136 /** The GTK builder instance for accessing
137 * the UI elements. */
138 GtkBuilder *pBuilder;
139#endif
140 /** The timeout (in ms) to wait for credentials. */
141 uint32_t uTimeoutMS;
142 /** The starting timestamp (in ms) to calculate
143 * the timeout. */
144 uint64_t uStartMS;
145 /** Timestamp of last abort message. */
146 uint64_t uTsAbort;
147 /** The HGCM client ID. */
148 uint32_t uClientId;
149 /** The credential password. */
150 char *pszPassword;
151} VBOXGREETERCTX, *PVBOXGREETERCTX;
152
153static void vboxGreeterError(const char *pszFormat, ...)
154{
155 va_list va;
156 char *buf;
157 va_start(va, pszFormat);
158 if (RTStrAPrintfV(&buf, pszFormat, va))
159 {
160 RTLogRelPrintf("%s: error: %s", VBOX_MODULE_NAME, buf);
161 RTStrFree(buf);
162 }
163 va_end(va);
164}
165
166static void vboxGreeterLog(const char *pszFormat, ...)
167{
168 if (g_iVerbosity)
169 {
170 va_list va;
171 char *buf;
172 va_start(va, pszFormat);
173 if (RTStrAPrintfV(&buf, pszFormat, va))
174 {
175 /* Only do normal logging in debug mode; could contain
176 * sensitive data! */
177 RTLogRelPrintf("%s: %s", VBOX_MODULE_NAME, buf);
178 RTStrFree(buf);
179 }
180 va_end(va);
181 }
182}
183
184/** @tood Move the following two functions to VbglR3 (also see pam_vbox). */
185#ifdef VBOX_WITH_GUEST_PROPS
186
187/**
188 * Reads a guest property.
189 *
190 * @return IPRT status code.
191 * @param hPAM PAM handle.
192 * @param uClientID Guest property service client ID.
193 * @param pszKey Key (name) of guest property to read.
194 * @param fReadOnly Indicates whether this key needs to be
195 * checked if it only can be read (and *not* written)
196 * by the guest.
197 * @param pszValue Buffer where to store the key's value.
198 * @param cbValue Size of buffer (in bytes).
199 * @param puTimestamp Timestamp of the value
200 * retrieved. Optional.
201 */
202static int vbox_read_prop(uint32_t uClientID,
203 const char *pszKey, bool fReadOnly,
204 char *pszValue, size_t cbValue, uint64_t *puTimestamp)
205{
206 AssertReturn(uClientID, VERR_INVALID_PARAMETER);
207 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
208 AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
209 /* puTimestamp is optional. */
210
211 int rc;
212
213 uint64_t u64Timestamp = 0;
214 char *pszValTemp = NULL;
215 char *pszFlags = NULL;
216 /* The buffer for storing the data and its initial size. We leave a bit
217 * of space here in case the maximum values are raised. */
218 void *pvBuf = NULL;
219 uint32_t cbBuf = GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN + _1K;
220
221 /* Because there is a race condition between our reading the size of a
222 * property and the guest updating it, we loop a few times here and
223 * hope. Actually this should never go wrong, as we are generous
224 * enough with buffer space. */
225 for (unsigned i = 0; i < 10; i++)
226 {
227 pvBuf = RTMemRealloc(pvBuf, cbBuf);
228 if (pvBuf)
229 {
230 rc = VbglR3GuestPropRead(uClientID, pszKey, pvBuf, cbBuf,
231 &pszValTemp, &u64Timestamp, &pszFlags,
232 &cbBuf);
233 }
234 else
235 rc = VERR_NO_MEMORY;
236
237 switch (rc)
238 {
239 case VERR_BUFFER_OVERFLOW:
240 {
241 /* Buffer too small, try it with a bigger one next time. */
242 cbBuf += _1K;
243 continue; /* Try next round. */
244 }
245
246 default:
247 break;
248 }
249
250 /* Everything except VERR_BUFFER_OVERLOW makes us bail out ... */
251 break;
252 }
253
254 if (RT_SUCCESS(rc))
255 {
256 /* Check security bits. */
257 if (pszFlags)
258 {
259 if ( fReadOnly
260 && !RTStrStr(pszFlags, "RDONLYGUEST"))
261 {
262 /* If we want a property which is read-only on the guest
263 * and it is *not* marked as such, deny access! */
264 rc = VERR_ACCESS_DENIED;
265 }
266 }
267 else /* No flags, no access! */
268 rc = VERR_ACCESS_DENIED;
269
270 if (RT_SUCCESS(rc))
271 {
272 /* If everything went well copy property value to our destination buffer. */
273 if (!RTStrPrintf(pszValue, cbValue, "%s", pszValTemp))
274 rc = VERR_BUFFER_OVERFLOW;
275
276 if (puTimestamp)
277 *puTimestamp = u64Timestamp;
278 }
279 }
280
281#ifdef DEBUG
282 vboxGreeterLog("Read guest property \"%s\"=\"%s\" (Flags: %s, TS: %RU64): %Rrc\n",
283 pszKey, pszValTemp ? pszValTemp : "<None>",
284 pszFlags ? pszFlags : "<None>", u64Timestamp, rc);
285#endif
286
287 if (pvBuf)
288 RTMemFree(pvBuf);
289
290 return rc;
291}
292
293# if 0 /* unused */
294/**
295 * Waits for a guest property to be changed.
296 *
297 * @return IPRT status code.
298 * @param hPAM PAM handle.
299 * @param uClientID Guest property service client ID.
300 * @param pszKey Key (name) of guest property to wait for.
301 * @param uTimeoutMS Timeout (in ms) to wait for the change. Specify
302 * RT_INDEFINITE_WAIT to wait indefinitly.
303 */
304static int vbox_wait_prop(uint32_t uClientID,
305 const char *pszKey, uint32_t uTimeoutMS)
306{
307 AssertReturn(uClientID, VERR_INVALID_PARAMETER);
308 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
309
310 int rc;
311
312 /* The buffer for storing the data and its initial size. We leave a bit
313 * of space here in case the maximum values are raised. */
314 void *pvBuf = NULL;
315 uint32_t cbBuf = MAX_NAME_LEN + MAX_VALUE_LEN + MAX_FLAGS_LEN + _1K;
316
317 for (int i = 0; i < 10; i++)
318 {
319 void *pvTmpBuf = RTMemRealloc(pvBuf, cbBuf);
320 if (pvTmpBuf)
321 {
322 char *pszName = NULL;
323 char *pszValue = NULL;
324 uint64_t u64TimestampOut = 0;
325 char *pszFlags = NULL;
326
327 pvBuf = pvTmpBuf;
328 rc = VbglR3GuestPropWait(uClientID, pszKey, pvBuf, cbBuf,
329 0 /* Last timestamp; just wait for next event */, uTimeoutMS,
330 &pszName, &pszValue, &u64TimestampOut,
331 &pszFlags, &cbBuf);
332 }
333 else
334 rc = VERR_NO_MEMORY;
335
336 if (rc == VERR_BUFFER_OVERFLOW)
337 {
338 /* Buffer too small, try it with a bigger one next time. */
339 cbBuf += _1K;
340 continue; /* Try next round. */
341 }
342
343 /* Everything except VERR_BUFFER_OVERLOW makes us bail out ... */
344 break;
345 }
346
347 return rc;
348}
349# endif /* unused */
350
351#endif /* VBOX_WITH_GUEST_PROPS */
352
353/**
354 * Checks for credentials provided by the host / HGCM.
355 *
356 * @return IPRT status code. VERR_NOT_FOUND if no credentials are available,
357 * VINF_SUCCESS on successful retrieval or another IPRT error.
358 * @param pCtx Greeter context.
359 */
360static int vboxGreeterCheckCreds(PVBOXGREETERCTX pCtx)
361{
362 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
363
364 static bool s_fCredsNotFoundMsgShown = false;
365 int rc = VbglR3CredentialsQueryAvailability();
366 if (RT_FAILURE(rc))
367 {
368 if (rc != VERR_NOT_FOUND)
369 vboxGreeterError("vboxGreeterCheckCreds: could not query for credentials! rc=%Rrc. Aborting\n", rc);
370 else if (!s_fCredsNotFoundMsgShown)
371 {
372 vboxGreeterLog("vboxGreeterCheckCreds: no credentials available\n");
373 s_fCredsNotFoundMsgShown = true;
374 }
375 }
376 else
377 {
378 /** @todo Domain handling needed? */
379 char *pszUsername; /* User name only is kept local. */
380 char *pszDomain = NULL;
381 rc = VbglR3CredentialsRetrieve(&pszUsername, &pCtx->pszPassword, &pszDomain);
382 if (RT_FAILURE(rc))
383 {
384 vboxGreeterError("vboxGreeterCheckCreds: could not retrieve credentials! rc=%Rrc. Aborting\n", rc);
385 }
386 else
387 {
388 vboxGreeterLog("vboxGreeterCheckCreds: credentials retrieved: user=%s, password=%s, domain=%s\n",
389 pszUsername,
390#ifdef DEBUG
391 pCtx->pszPassword,
392#else
393 "XXX",
394#endif
395 pszDomain);
396 /* Trigger LightDM authentication with the user name just retrieved. */
397 lightdm_greeter_authenticate(pCtx->pGreeter, pszUsername); /* Must be the real user name from host! */
398
399 /* Securely wipe the user name + domain again. */
400 VbglR3CredentialsDestroy(pszUsername, NULL /* pszPassword */, pszDomain,
401 3 /* Three wipe passes */);
402 }
403 }
404
405#ifdef DEBUG
406 vboxGreeterLog("vboxGreeterCheckCreds: returned with rc=%Rrc\n", rc);
407#endif
408 return rc;
409}
410
411/**
412 * Called by LightDM when greeter is not needed anymore.
413 *
414 * @param signum Signal number.
415 */
416static void cb_sigterm(int signum)
417{
418 RT_NOREF(signum);
419
420 /* Note: This handler must be reentrant-safe. */
421#ifdef VBOX_WITH_FLTK
422 g_fRunning = false;
423#else
424 exit(RTEXITCODE_SUCCESS);
425#endif
426}
427
428/**
429 * Callback for showing a user prompt, issued by the LightDM server.
430 *
431 * @param pGreeter Pointer to this greeter instance.
432 * @param pszText Text to display.
433 * @param enmType Type of prompt to display.
434 * @param pvData Pointer to user-supplied data.
435 */
436static void cb_lightdm_show_prompt(LightDMGreeter *pGreeter,
437 const gchar *pszText, LightDMPromptType enmType,
438 gpointer pvData)
439{
440 vboxGreeterLog("cb_lightdm_show_prompt: text=%s, type=%d\n", pszText, enmType);
441
442 PVBOXGREETERCTX pCtx = (PVBOXGREETERCTX)pvData;
443 AssertPtr(pCtx);
444
445 switch (enmType)
446 {
447 case 1: /* Password. */
448 {
449 if (pCtx->pszPassword)
450 {
451 lightdm_greeter_respond(pGreeter, pCtx->pszPassword);
452 }
453 else
454 {
455#ifdef VBOX_WITH_FLTK
456 AssertPtr(pCtx->pEdtPassword);
457 const char *pszPwd = pCtx->pEdtPassword->value();
458#else
459 GtkEntry *pEdtPwd = GTK_ENTRY(gtk_builder_get_object(pCtx->pBuilder, "edt_password"));
460 AssertPtr(pEdtPwd);
461 const gchar *pszPwd = gtk_entry_get_text(pEdtPwd);
462#endif
463 lightdm_greeter_respond(pGreeter, pszPwd);
464 }
465 break;
466 }
467 /** @todo Other fields? */
468
469 default:
470 break;
471 }
472
473 VbglR3CredentialsDestroy(NULL /* pszUsername */, pCtx->pszPassword, NULL /* pszDomain */,
474 3 /* Three wipe passes */);
475 pCtx->pszPassword = NULL;
476}
477
478/**
479 * Callback for showing a message, issued by the LightDM server.
480 *
481 * @param pGreeter Pointer to this greeter instance.
482 * @param pszText Text to display.
483 * @param enmType Type of message to display.
484 * @param pvData Pointer to user-supplied data.
485 */
486static void cb_lightdm_show_message(LightDMGreeter *pGreeter,
487 const gchar *pszText, LightDMPromptType enmType,
488 gpointer pvData)
489{
490 RT_NOREF(pGreeter);
491 vboxGreeterLog("cb_lightdm_show_message: text=%s, type=%d\n", pszText, enmType);
492
493 PVBOXGREETERCTX pCtx = (PVBOXGREETERCTX)pvData;
494 AssertPtrReturnVoid(pCtx);
495
496#ifdef VBOX_WITH_FLTK
497 AssertPtr(pCtx->pLblInfo);
498 pCtx->pLblInfo->copy_label(pszText);
499#else
500 GtkLabel *pLblInfo = GTK_LABEL(gtk_builder_get_object(pCtx->pBuilder, "lbl_info"));
501 AssertPtr(pLblInfo);
502 gtk_label_set_text(pLblInfo, pszText);
503#endif
504}
505
506/**
507 * Callback for authentication completion, issued by the LightDM server.
508 *
509 * @param pGreeter Pointer to this greeter instance.
510 */
511static void cb_lightdm_auth_complete(LightDMGreeter *pGreeter)
512{
513 vboxGreeterLog("cb_lightdm_auth_complete\n");
514
515 const gchar *pszUser = lightdm_greeter_get_authentication_user(pGreeter);
516 vboxGreeterLog("authenticating user: %s\n", pszUser ? pszUser : "<NULL>");
517
518 if (lightdm_greeter_get_is_authenticated(pGreeter))
519 {
520 /** @todo Add non-default session support. */
521 gchar *pszSession = g_strdup(lightdm_greeter_get_default_session_hint(pGreeter));
522 if (pszSession)
523 {
524 vboxGreeterLog("starting session: %s\n", pszSession);
525 GError *pError = NULL;
526 if (!lightdm_greeter_start_session_sync(pGreeter, pszSession, &pError))
527 {
528 vboxGreeterError("unable to start session '%s': %s\n",
529 pszSession, pError ? pError->message : "Unknown error");
530 }
531 else
532 {
533 AssertPtr(pszSession);
534 vboxGreeterLog("session '%s' successfully started\n", pszSession);
535 }
536 if (pError)
537 g_error_free(pError);
538 g_free(pszSession);
539 }
540 else
541 vboxGreeterError("unable to get default session\n");
542 }
543 else
544 vboxGreeterLog("user not authenticated successfully (yet)\n");
545}
546
547/**
548 * Callback for clicking on the "Login" button.
549 *
550 * @param pWidget Widget this callback is bound to.
551 * @param pvData Pointer to user-supplied data.
552 */
553#ifdef VBOX_WITH_FLTK
554void cb_btn_login(Fl_Widget *pWidget, void *pvData)
555#else
556void cb_btn_login(GtkWidget *pWidget, gpointer pvData)
557#endif
558{
559 PVBOXGREETERCTX pCtx = (PVBOXGREETERCTX)pvData;
560 RT_NOREF(pWidget);
561 AssertPtr(pCtx);
562
563#ifdef VBOX_WITH_FLTK
564 AssertPtr(pCtx->pEdtUsername);
565 const char *pszUser = pCtx->pEdtUsername->value();
566 AssertPtr(pCtx->pEdtPassword);
567 const char *pszPwd = pCtx->pEdtPassword->value();
568#else
569 GtkEntry *pEdtUser = GTK_ENTRY(gtk_builder_get_object(pCtx->pBuilder, VBOX_GREETER_UI_EDT_USER));
570 AssertPtr(pEdtUser);
571 const gchar *pszUser = gtk_entry_get_text(pEdtUser);
572
573 GtkEntry *pEdtPwd = GTK_ENTRY(gtk_builder_get_object(pCtx->pBuilder, VBOX_GREETER_UI_EDT_PASSWORD));
574 AssertPtr(pEdtPwd);
575 const gchar *pszPwd = gtk_entry_get_text(pEdtPwd);
576#endif
577
578 /** @todo Add domain handling? */
579 vboxGreeterLog("login button pressed: greeter=%p, user=%s, password=%s\n",
580 pCtx->pGreeter,
581 pszUser ? pszUser : "<NONE>",
582#ifdef DEBUG
583 pszPwd ? pszPwd : "<NONE>");
584#else
585 /* Don't log passwords in release mode! */
586 "XXX");
587#endif
588 if (strlen(pszUser)) /* Only authenticate if username is given. */
589 {
590 lightdm_greeter_respond(pCtx->pGreeter, pszPwd);
591 lightdm_greeter_authenticate(pCtx->pGreeter, pszUser);
592 }
593}
594
595/**
596 * Callback for clicking on the "Menu" button.
597 *
598 * @param pWidget Widget this callback is bound to.
599 * @param pvData Pointer to user-supplied data.
600 */
601#ifdef VBOX_WITH_FLTK
602void cb_btn_menu(Fl_Widget *pWidget, void *pvData)
603#else
604void cb_btn_menu(GtkWidget *pWidget, gpointer pvData)
605#endif
606{
607 RT_NOREF(pWidget, pvData);
608 vboxGreeterLog("menu button pressed\n");
609}
610
611/**
612 * Callback for clicking on the "Restart" button / menu entry.
613 *
614 * @param pWidget Widget this callback is bound to.
615 * @param pvData Pointer to user-supplied data.
616 */
617#ifdef VBOX_WITH_FLTK
618void cb_btn_restart(Fl_Widget *pWidget, void *pvData)
619#else
620void cb_btn_restart(GtkWidget *pWidget, gpointer pvData)
621#endif
622{
623 RT_NOREF(pWidget, pvData);
624 vboxGreeterLog("restart button pressed\n");
625
626 bool fRestart = true;
627#ifdef VBOX_WITH_FLTK
628 int rc = fl_choice("Really restart the system?", "Yes", "No", NULL);
629 fRestart = rc == 0;
630#endif
631
632 if (fRestart)
633 {
634 vboxGreeterLog("restart requested\n");
635#ifndef DEBUG
636 lightdm_restart(NULL);
637#endif
638 }
639}
640
641/**
642 * Callback for clicking on the "Shutdown" button / menu entry.
643 *
644 * @param pWidget Widget this callback is bound to.
645 * @param pvData Pointer to user-supplied data.
646 */
647#ifdef VBOX_WITH_FLTK
648void cb_btn_shutdown(Fl_Widget *pWidget, void *pvData)
649#else
650void cb_btn_shutdown(GtkWidget *pWidget, gpointer pvData)
651#endif
652{
653 RT_NOREF(pWidget, pvData);
654 vboxGreeterLog("shutdown button pressed\n");
655
656 bool fShutdown = true;
657#ifdef VBOX_WITH_FLTK
658 int rc = fl_choice("Really shutdown the system?", "Yes", "No", NULL);
659 fShutdown = rc == 0;
660#endif
661
662 if (fShutdown)
663 {
664 vboxGreeterLog("shutdown requested\n");
665#ifndef DEBUG
666 lightdm_shutdown(NULL);
667#endif
668 }
669}
670
671#ifdef VBOX_WITH_FLTK
672void cb_edt_username(Fl_Widget *pWidget, void *pvData)
673#else
674void cb_edt_username(GtkWidget *pWidget, gpointer pvData)
675#endif
676{
677 RT_NOREF(pWidget);
678 vboxGreeterLog("cb_edt_username called\n");
679
680 PVBOXGREETERCTX pCtx = (PVBOXGREETERCTX)pvData;
681 AssertPtr(pCtx);
682#ifdef VBOX_WITH_FLTK
683 AssertPtr(pCtx->pEdtPassword);
684 Fl::focus(pCtx->pEdtPassword);
685#endif
686}
687
688#ifdef VBOX_WITH_FLTK
689void cb_edt_password(Fl_Widget *pWidget, void *pvData)
690#else
691void cb_edt_password(GtkWidget *pWidget, gpointer pvData)
692#endif
693{
694 RT_NOREF(pWidget, pvData);
695 vboxGreeterLog("cb_edt_password called\n");
696
697 PVBOXGREETERCTX pCtx = (PVBOXGREETERCTX)pvData;
698 AssertPtr(pCtx);
699#ifdef VBOX_WITH_FLTK
700 AssertPtr(pCtx->pBtnLogin);
701 cb_btn_login(pCtx->pBtnLogin, pvData);
702#endif
703}
704
705/**
706 * Callback for the timer event which is checking for new credentials
707 * from the host.
708 *
709 * @param pvData Pointer to user-supplied data.
710 */
711#ifdef VBOX_WITH_FLTK
712static void cb_check_creds(void *pvData)
713#else
714static gboolean cb_check_creds(gpointer pvData)
715#endif
716{
717 PVBOXGREETERCTX pCtx = (PVBOXGREETERCTX)pvData;
718 AssertPtr(pCtx);
719
720#ifdef DEBUG
721 vboxGreeterLog("cb_check_creds called, clientId=%RU32, timeoutMS=%RU32\n",
722 pCtx->uClientId, pCtx->uTimeoutMS);
723#endif
724
725 int rc = VINF_SUCCESS;
726
727#ifdef VBOX_WITH_GUEST_PROPS
728 bool fAbort = false;
729 char szVal[255];
730 if (pCtx->uClientId)
731 {
732 uint64_t tsAbort;
733 rc = vbox_read_prop(pCtx->uClientId,
734 "/VirtualBox/GuestAdd/PAM/CredsWaitAbort",
735 true /* Read-only on guest */,
736 szVal, sizeof(szVal), &tsAbort);
737 switch (rc)
738 {
739 case VINF_SUCCESS:
740# ifdef DEBUG
741 vboxGreeterLog("cb_check_creds: tsAbort %RU64 <-> %RU64\n",
742 pCtx->uTsAbort, tsAbort);
743# endif
744 if (tsAbort != pCtx->uTsAbort)
745 fAbort = true; /* Timestamps differs, abort. */
746 pCtx->uTsAbort = tsAbort;
747 break;
748
749 case VERR_TOO_MUCH_DATA:
750 vboxGreeterError("cb_check_creds: temporarily unable to get abort notification\n");
751 break;
752
753 case VERR_NOT_FOUND:
754 /* Value not found, continue checking for credentials. */
755 break;
756
757 default:
758 vboxGreeterError("cb_check_creds: the abort notification request failed with rc=%Rrc\n", rc);
759 fAbort = true; /* Abort on error. */
760 break;
761 }
762 }
763
764 if (fAbort)
765 {
766 /* Get optional message. */
767 szVal[0] = '\0';
768 int rc2 = vbox_read_prop(pCtx->uClientId,
769 "/VirtualBox/GuestAdd/PAM/CredsMsgWaitAbort",
770 true /* Read-only on guest */,
771 szVal, sizeof(szVal), NULL /* Timestamp. */);
772 if ( RT_FAILURE(rc2)
773 && rc2 != VERR_NOT_FOUND)
774 vboxGreeterError("cb_check_creds: getting wait abort message failed with rc=%Rrc\n", rc2);
775# ifdef VBOX_WITH_FLTK
776 AssertPtr(pCtx->pLblInfo);
777 pCtx->pLblInfo->copy_label(szVal);
778# else /* !VBOX_WITH_FLTK */
779 GtkLabel *pLblInfo = GTK_LABEL(gtk_builder_get_object(pCtx->pBuilder, VBOX_GREETER_UI_LBL_INFO));
780 AssertPtr(pLblInfo);
781 gtk_label_set_text(pLblInfo, szVal);
782# endif /* !VBOX_WITH_FLTK */
783 vboxGreeterLog("cb_check_creds: got notification from host to abort waiting\n");
784 }
785 else
786 {
787#endif /* VBOX_WITH_GUEST_PROPS */
788 rc = vboxGreeterCheckCreds(pCtx);
789 if (RT_SUCCESS(rc))
790 {
791 /* Credentials retrieved. */
792 }
793 else if (rc == VERR_NOT_FOUND)
794 {
795 /* No credentials found, but try next round (if there's
796 * time left for) ... */
797 }
798#ifdef VBOX_WITH_GUEST_PROPS
799 }
800#endif /* VBOX_WITH_GUEST_PROPS */
801
802 if (rc == VERR_NOT_FOUND) /* No credential found this round. */
803 {
804 /* Calculate timeout value left after process has been started. */
805 uint64_t u64Elapsed = RTTimeMilliTS() - pCtx->uStartMS;
806 /* Is it time to bail out? */
807 if (pCtx->uTimeoutMS < u64Elapsed)
808 {
809#ifdef VBOX_WITH_GUEST_PROPS
810 szVal[0] = '\0';
811 int rc2 = vbox_read_prop(pCtx->uClientId,
812 "/VirtualBox/GuestAdd/PAM/CredsMsgWaitTimeout",
813 true /* Read-only on guest */,
814 szVal, sizeof(szVal), NULL /* Timestamp. */);
815 if ( RT_FAILURE(rc2)
816 && rc2 != VERR_NOT_FOUND)
817 vboxGreeterError("cb_check_creds: getting wait timeout message failed with rc=%Rrc\n", rc2);
818# ifdef VBOX_WITH_FLTK
819 AssertPtr(pCtx->pLblInfo);
820 pCtx->pLblInfo->copy_label(szVal);
821# else
822 GtkLabel *pLblInfo = GTK_LABEL(gtk_builder_get_object(pCtx->pBuilder, VBOX_GREETER_UI_LBL_INFO));
823 AssertPtr(pLblInfo);
824 gtk_label_set_text(pLblInfo, szVal);
825# endif
826#endif /* VBOX_WITH_GUEST_PROPS */
827 vboxGreeterLog("cb_check_creds: no credentials retrieved within time (%RU32ms), giving up\n",
828 pCtx->uTimeoutMS);
829 rc = VERR_TIMEOUT;
830 }
831 }
832
833#ifdef DEBUG
834 vboxGreeterLog("cb_check_creds returned with rc=%Rrc\n", rc);
835#endif
836
837 /* At the moment we only allow *one* shot from the host,
838 * so setting credentials in a second attempt won't be possible
839 * intentionally. */
840
841 if (rc == VERR_NOT_FOUND)
842#ifdef VBOX_WITH_FLTK
843 Fl::repeat_timeout(0.5 /* 500 ms */, cb_check_creds, pvData);
844#else
845 return TRUE; /* No credentials found, do another round. */
846
847 return FALSE; /* Remove timer source on every other error / status. */
848#endif
849}
850
851/**
852 * Release logger callback.
853 *
854 * @return IPRT status code.
855 * @param pLoggerRelease
856 * @param enmPhase
857 * @param pfnLog
858 */
859static DECLCALLBACK(void) vboxGreeterLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
860{
861 /* Some introductory information. */
862 static RTTIMESPEC s_TimeSpec;
863 char szTmp[256];
864 if (enmPhase == RTLOGPHASE_BEGIN)
865 RTTimeNow(&s_TimeSpec);
866 RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
867
868 switch (enmPhase)
869 {
870 case RTLOGPHASE_BEGIN:
871 {
872 pfnLog(pLoggerRelease,
873 "vbox-greeter %s r%s (verbosity: %d) %s (%s %s) release log\n"
874 "Log opened %s\n",
875 RTBldCfgVersion(), RTBldCfgRevisionStr(), g_iVerbosity, VBOX_BUILD_TARGET,
876 __DATE__, __TIME__, szTmp);
877
878 int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
879 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
880 pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
881 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
882 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
883 pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
884 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
885 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
886 pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
887 if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
888 pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
889
890 /* the package type is interesting for Linux distributions */
891 char szExecName[RTPATH_MAX];
892 char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
893 pfnLog(pLoggerRelease,
894 "Executable: %s\n"
895 "Process ID: %u\n"
896 "Package type: %s"
897#ifdef VBOX_OSE
898 " (OSE)"
899#endif
900 "\n",
901 pszExecName ? pszExecName : "unknown",
902 RTProcSelf(),
903 VBOX_PACKAGE_STRING);
904 break;
905 }
906
907 case RTLOGPHASE_PREROTATE:
908 pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
909 break;
910
911 case RTLOGPHASE_POSTROTATE:
912 pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
913 break;
914
915 case RTLOGPHASE_END:
916 pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
917 break;
918
919 default:
920 /* nothing */;
921 }
922}
923
924/**
925 * Creates the default release logger outputting to the specified file.
926 *
927 * @return IPRT status code.
928 * @param pszLogFile Filename for log output. Optional.
929 */
930static int vboxGreeterLogCreate(const char *pszLogFile)
931{
932 /* Create release logger (stdout + file). */
933 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
934 RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
935#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
936 fFlags |= RTLOGFLAGS_USECRLF;
937#endif
938 int rc = RTLogCreateEx(&g_pLoggerRelease, fFlags, "all",
939 "VBOXGREETER_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
940 RTLOGDEST_STDOUT,
941 vboxGreeterLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
942 NULL /*pErrInfo*/, pszLogFile);
943 if (RT_SUCCESS(rc))
944 {
945 /* register this logger as the release logger */
946 RTLogRelSetDefaultInstance(g_pLoggerRelease);
947
948 /* Explicitly flush the log in case of VBOXGREETER_RELEASE_LOG_FLAGS=buffered. */
949 RTLogFlush(g_pLoggerRelease);
950 }
951
952 return rc;
953}
954
955static void vboxGreeterLogDestroy(void)
956{
957 RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
958}
959
960static int vboxGreeterUsage(void)
961{
962 RTPrintf("Usage:\n"
963 " %-12s [-h|-?|--help] [-F|--logfile <file>]\n"
964 " [-v|--verbose] [-V|--version]\n", g_pszProgName);
965
966 RTPrintf("\n"
967 " Copyright (C) 2012-" VBOX_C_YEAR " " VBOX_VENDOR "\n");
968
969 return RTEXITCODE_SYNTAX;
970}
971
972int main(int argc, char **argv)
973{
974 int rc = RTR3InitExe(argc, &argv, 0);
975 if (RT_FAILURE(rc))
976 return RTMsgInitFailure(rc);
977 g_pszProgName = RTPathFilename(argv[0]);
978
979 static const RTGETOPTDEF s_aOptions[] =
980 {
981 { "--logfile", 'F', RTGETOPT_REQ_STRING },
982 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
983 { "--version", 'V', RTGETOPT_REQ_NOTHING }
984 };
985
986 char szLogFile[RTPATH_MAX + 128] = "";
987
988 int ch;
989 RTGETOPTUNION ValueUnion;
990 RTGETOPTSTATE GetState;
991 RTGetOptInit(&GetState, argc, argv,
992 s_aOptions, RT_ELEMENTS(s_aOptions),
993 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
994
995 while ( (ch = RTGetOpt(&GetState, &ValueUnion))
996 && RT_SUCCESS(rc))
997 {
998 /* For options that require an argument, ValueUnion has received the value. */
999 switch (ch)
1000 {
1001 case 'F':
1002 if (!RTStrPrintf(szLogFile, sizeof(szLogFile), "%s", ValueUnion.psz))
1003 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to get prepare log file name");
1004 break;
1005
1006 case 'h':
1007 case '?':
1008 return vboxGreeterUsage();
1009
1010 case 'v': /* Raise verbosity. */
1011 g_iVerbosity++;
1012 break;
1013
1014 case 'V': /* Print version and exit. */
1015 RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
1016 return RTEXITCODE_SUCCESS;
1017 break; /* Never reached. */
1018
1019 default:
1020 return RTGetOptPrintError(ch, &ValueUnion);
1021 }
1022 }
1023
1024 if (RT_FAILURE(rc))
1025 return RTEXITCODE_SYNTAX;
1026
1027 rc = VbglR3InitUser();
1028 if (RT_FAILURE(rc))
1029 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to init Vbgl (%Rrc)", rc);
1030
1031 rc = vboxGreeterLogCreate(strlen(szLogFile) ? szLogFile : NULL);
1032 if (RT_FAILURE(rc))
1033 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to create release log (%s, %Rrc)",
1034 strlen(szLogFile) ? szLogFile : "<None>", rc);
1035
1036 vboxGreeterLog("init\n");
1037
1038 signal(SIGTERM, cb_sigterm);
1039
1040 /** @todo This function already is too long. Move code into
1041 * functions. */
1042
1043 VBOXGREETERCTX ctx;
1044 RT_ZERO(ctx);
1045
1046 /* UI parameters. */
1047 uint32_t uBgColor = 0; /* The background color. */
1048 uint32_t uLogonDlgHdrColor = 0;
1049 uint32_t uLogonDlgBgColor = 0; /* The greeter's dialog color. */
1050 uint32_t uLogonDlgBtnColor = 0; /* The greeter's button color. */
1051
1052#ifdef VBOX_GREETER_WITH_PNG_SUPPORT
1053 char szBannerPath[RTPATH_MAX];
1054#endif
1055
1056 /* By default most UI elements are shown. */
1057 uint32_t uOptsUI = VBOX_GREETER_UI_SHOW_RESTART
1058 | VBOX_GREETER_UI_SHOW_SHUTDOWN;
1059#ifdef VBOX_WITH_GUEST_PROPS
1060 uint32_t uClientId = 0;
1061 rc = VbglR3GuestPropConnect(&uClientId);
1062 if (RT_SUCCESS(rc))
1063 {
1064 vboxGreeterLog("clientId=%RU32\n", uClientId);
1065
1066 ctx.uClientId = uClientId;
1067
1068 char szVal[256];
1069 int rc2 = vbox_read_prop(uClientId,
1070 "/VirtualBox/GuestAdd/Greeter/HideRestart",
1071 true /* Read-only on guest */,
1072 szVal, sizeof(szVal), NULL /* Timestamp. */);
1073 if ( RT_SUCCESS(rc2)
1074 && !RTStrICmp(szVal, "1"))
1075 {
1076 uOptsUI &= ~VBOX_GREETER_UI_SHOW_RESTART;
1077 }
1078
1079 rc2 = vbox_read_prop(uClientId,
1080 "/VirtualBox/GuestAdd/Greeter/HideShutdown",
1081 true /* Read-only on guest */,
1082 szVal, sizeof(szVal), NULL /* Timestamp. */);
1083 if ( RT_SUCCESS(rc2)
1084 && !RTStrICmp(szVal, "1"))
1085 {
1086 uOptsUI &= ~VBOX_GREETER_UI_SHOW_SHUTDOWN;
1087 }
1088
1089# ifdef VBOX_GREETER_WITH_PNG_SUPPORT
1090 /* Load the banner. */
1091 rc2 = vbox_read_prop(uClientId,
1092 "/VirtualBox/GuestAdd/Greeter/BannerPath",
1093 true /* Read-only on guest */,
1094 szBannerPath, sizeof(szBannerPath), NULL /* Timestamp. */);
1095 if (RT_SUCCESS(rc2))
1096 {
1097 if (RTFileExists(szBannerPath))
1098 {
1099 vboxGreeterLog("showing banner from '%s'\n", szBannerPath);
1100 uOptsUI |= VBOX_GREETER_UI_SHOW_BANNER;
1101 }
1102 else
1103 vboxGreeterLog("warning: unable to find banner at '%s', skipping\n", szBannerPath);
1104 }
1105# endif /* VBOX_GREETER_WITH_PNG_SUPPORT */
1106
1107 /* Use theming?. */
1108 rc2 = vbox_read_prop(uClientId,
1109 "/VirtualBox/GuestAdd/Greeter/UseTheming",
1110 true /* Read-only on guest */,
1111 szVal, sizeof(szVal), NULL /* Timestamp. */);
1112 if ( RT_SUCCESS(rc2)
1113 && !RTStrICmp(szVal, "1"))
1114 {
1115 vboxGreeterLog("custom theming enabled\n");
1116 uOptsUI |= VBOX_GREETER_UI_USE_THEMING;
1117 }
1118
1119 if (uOptsUI & VBOX_GREETER_UI_USE_THEMING)
1120 {
1121 /* Get background color. */
1122 rc2 = vbox_read_prop(uClientId,
1123 "/VirtualBox/GuestAdd/Greeter/Theme/BackgroundColor",
1124 true /* Read-only on guest */,
1125 szVal, sizeof(szVal), NULL /* Timestamp. */);
1126 if (RT_SUCCESS(rc2))
1127 {
1128 uBgColor = strtol(szVal, NULL,
1129 /* Change conversion base when having a 0x prefix. */
1130 RTStrStr(szVal, "0x") == szVal ? 0 : 16);
1131 }
1132
1133 /* Logon dialog. */
1134
1135 /* Get header color. */
1136 rc2 = vbox_read_prop(uClientId,
1137 "/VirtualBox/GuestAdd/Greeter/Theme/LogonDialog/HeaderColor",
1138 true /* Read-only on guest */,
1139 szVal, sizeof(szVal), NULL /* Timestamp. */);
1140 if (RT_SUCCESS(rc2))
1141 {
1142 uLogonDlgHdrColor = strtol(szVal, NULL,
1143 /* Change conversion base when having a 0x prefix. */
1144 RTStrStr(szVal, "0x") == szVal ? 0 : 16);
1145 }
1146
1147 /* Get dialog color. */
1148 rc2 = vbox_read_prop(uClientId,
1149 "/VirtualBox/GuestAdd/Greeter/Theme/LogonDialog/BackgroundColor",
1150 true /* Read-only on guest */,
1151 szVal, sizeof(szVal), NULL /* Timestamp. */);
1152 if (RT_SUCCESS(rc2))
1153 {
1154 uLogonDlgBgColor = strtol(szVal, NULL,
1155 /* Change conversion base when having a 0x prefix. */
1156 RTStrStr(szVal, "0x") == szVal ? 0 : 16);
1157 }
1158
1159 /* Get button color. */
1160 rc2 = vbox_read_prop(uClientId,
1161 "/VirtualBox/GuestAdd/Greeter/Theme/LogonDialog/ButtonColor",
1162 true /* Read-only on guest */,
1163 szVal, sizeof(szVal), NULL /* Timestamp. */);
1164 if (RT_SUCCESS(rc2))
1165 {
1166 uLogonDlgBtnColor = strtol(szVal, NULL,
1167 /* Change conversion base when having a 0x prefix. */
1168 RTStrStr(szVal, "0x") == szVal ? 0 : 16);
1169 }
1170 }
1171 }
1172 else
1173 vboxGreeterError("unable to connect to guest property service, rc=%Rrc\n", rc);
1174#endif
1175 vboxGreeterLog("UI options are: %RU32\n", uOptsUI);
1176
1177#ifdef VBOX_WITH_FLTK
1178 int rc2 = Fl::scheme("plastic");
1179 if (!rc2)
1180 vboxGreeterLog("warning: unable to set visual scheme\n");
1181
1182 Fl::visual(FL_DOUBLE | FL_INDEX);
1183 Fl_Double_Window *pWndMain = new Fl_Double_Window(Fl::w(), Fl::h(), "VirtualBox Guest Additions");
1184 AssertPtr(pWndMain);
1185 if (uOptsUI & VBOX_GREETER_UI_USE_THEMING)
1186 pWndMain->color(fl_rgb_color(VBOX_RGB_COLOR_RED(uBgColor),
1187 VBOX_RGB_COLOR_GREEN(uBgColor),
1188 VBOX_RGB_COLOR_BLUE(uBgColor)));
1189 else /* Default colors. */
1190 pWndMain->color(fl_rgb_color(0x73, 0x7F, 0x8C));
1191
1192 Fl_Double_Window *pWndGreeter = new Fl_Double_Window(500, 350);
1193 AssertPtr(pWndGreeter);
1194 pWndGreeter->set_modal();
1195 if (uOptsUI & VBOX_GREETER_UI_USE_THEMING)
1196 pWndGreeter->color(fl_rgb_color(VBOX_RGB_COLOR_RED(uLogonDlgBgColor),
1197 VBOX_RGB_COLOR_GREEN(uLogonDlgBgColor),
1198 VBOX_RGB_COLOR_BLUE(uLogonDlgBgColor)));
1199 else /* Default colors. */
1200 pWndGreeter->color(fl_rgb_color(255, 255, 255));
1201
1202 uint32_t uOffsetX = 130;
1203 /**
1204 * For now we're using a simple Y offset for moving all elements
1205 * down if a banner needs to be shown on top of the greeter. Not
1206 * very clean but does the job. Use some more layouting stuff
1207 * when this gets more complex.
1208 */
1209 uint32_t uOffsetY = 80;
1210
1211# ifdef VBOX_GREETER_WITH_PNG_SUPPORT
1212 fl_register_images();
1213
1214 /** @todo Add basic image type detection based on file
1215 * extension. */
1216
1217 Fl_PNG_Image *pImgBanner = NULL;
1218 if (uOptsUI & VBOX_GREETER_UI_SHOW_BANNER)
1219 {
1220 pImgBanner = new Fl_PNG_Image(szBannerPath);
1221 AssertPtr(pImgBanner);
1222
1223 /** @todo Make the banner size configurable via guest
1224 * properties. For now it's hardcoded to 460 x 90px. */
1225 Fl_Box *pBoxBanner = new Fl_Box(20, uOffsetY, 460, 90, "");
1226 AssertPtr(pBoxBanner);
1227 pBoxBanner->image(pImgBanner);
1228
1229 uOffsetY = 120;
1230 }
1231# endif
1232
1233 Fl_Box *pLblHeader = new Fl_Box(FL_NO_BOX, 242, uOffsetY, 300, 20,
1234 "Desktop Login");
1235 AssertPtr(pLblHeader);
1236
1237 /** Note to use an own font:
1238 * Fl_Font myfnt = FL_FREE_FONT + 1;
1239 * Fl::set_font(myfnt, "MyFont"); */
1240 Fl_Font fntHeader = FL_FREE_FONT;
1241 Fl::set_font(fntHeader, "Courier");
1242
1243 pLblHeader->align(FL_ALIGN_LEFT);
1244 pLblHeader->labelfont(FL_BOLD);
1245 pLblHeader->labelsize(24);
1246 if (uOptsUI & VBOX_GREETER_UI_USE_THEMING)
1247 pLblHeader->labelcolor(fl_rgb_color(VBOX_RGB_COLOR_RED(uLogonDlgHdrColor),
1248 VBOX_RGB_COLOR_GREEN(uLogonDlgHdrColor),
1249 VBOX_RGB_COLOR_BLUE(uLogonDlgHdrColor)));
1250 else /* Default color. */
1251 pLblHeader->labelcolor(fl_rgb_color(0x51, 0x5F, 0x77));
1252 uOffsetY += 40;
1253
1254 /** @todo Add basic NLS support. */
1255
1256 Fl_Input *pEdtUsername = new Fl_Input(uOffsetX, uOffsetY,
1257 300, 20, "User Name");
1258 AssertPtr(pEdtUsername);
1259 pEdtUsername->callback(cb_edt_username, &ctx);
1260 pEdtUsername->when(FL_WHEN_ENTER_KEY_ALWAYS);
1261 Fl::focus(pEdtUsername);
1262 ctx.pEdtUsername = pEdtUsername;
1263
1264 Fl_Secret_Input *pEdtPassword = new Fl_Secret_Input(uOffsetX, uOffsetY + 40,
1265 300, 20, "Password");
1266 AssertPtr(pEdtPassword);
1267 pEdtPassword->callback(cb_edt_password, &ctx);
1268 pEdtPassword->when(FL_WHEN_ENTER_KEY_ALWAYS);
1269 ctx.pEdtPassword = pEdtPassword;
1270
1271 Fl_Button *pBtnLogin = new Fl_Button(uOffsetX, uOffsetY + 70,
1272 100, 40, "Log In");
1273 AssertPtr(pBtnLogin);
1274 pBtnLogin->callback(cb_btn_login, &ctx);
1275 if (uOptsUI & VBOX_GREETER_UI_USE_THEMING)
1276 pBtnLogin->color(fl_rgb_color(VBOX_RGB_COLOR_RED(uLogonDlgBtnColor),
1277 VBOX_RGB_COLOR_GREEN(uLogonDlgBtnColor),
1278 VBOX_RGB_COLOR_BLUE(uLogonDlgBtnColor)));
1279 else /* Default color. */
1280 pBtnLogin->color(fl_rgb_color(255, 255, 255));
1281 ctx.pBtnLogin = pBtnLogin;
1282
1283 Fl_Menu_Button *pBtnMenu = new Fl_Menu_Button(uOffsetX + 120, uOffsetY + 70,
1284 100, 40, "Options");
1285 AssertPtr(pBtnMenu);
1286 pBtnMenu->callback(cb_btn_menu, &ctx);
1287 if (uOptsUI & VBOX_GREETER_UI_USE_THEMING)
1288 pBtnMenu->color(fl_rgb_color(VBOX_RGB_COLOR_RED(uLogonDlgBtnColor),
1289 VBOX_RGB_COLOR_GREEN(uLogonDlgBtnColor),
1290 VBOX_RGB_COLOR_BLUE(uLogonDlgBtnColor)));
1291 else /* Default color. */
1292 pBtnMenu->color(fl_rgb_color(255, 255, 255));
1293
1294 if (uOptsUI & VBOX_GREETER_UI_SHOW_RESTART)
1295 pBtnMenu->add("Restart", "" /* Shortcut */, cb_btn_restart, &ctx, 0 /* Flags */);
1296 if (uOptsUI & VBOX_GREETER_UI_SHOW_SHUTDOWN)
1297 pBtnMenu->add("Shutdown", "" /* Shortcut */, cb_btn_shutdown, &ctx, 0 /* Flags */);
1298
1299 char szLabel[255];
1300 RTStrPrintf(szLabel, sizeof(szLabel), "Oracle VM VirtualBox Guest Additions %sr%s",
1301 RTBldCfgVersion(), RTBldCfgRevisionStr());
1302 Fl_Box *pLblInfo = new Fl_Box(FL_NO_BOX , 50, uOffsetY + 150,
1303 400, 20, szLabel);
1304 AssertPtr(pLblInfo);
1305 ctx.pLblInfo = pLblInfo;
1306
1307 pWndGreeter->end();
1308 pWndGreeter->position((Fl::w() - pWndGreeter->w()) / 2,
1309 (Fl::h() - pWndGreeter->h()) / 2);
1310
1311 pWndMain->fullscreen();
1312 pWndMain->show(argc, argv);
1313 pWndMain->end();
1314
1315 pWndGreeter->show();
1316#else /* !VBOX_WITH_FLTK */
1317 gtk_init(&argc, &argv);
1318
1319 /* Set default cursor */
1320 gdk_window_set_cursor(gdk_get_default_root_window(), gdk_cursor_new(GDK_LEFT_PTR));
1321
1322 GError *pError = NULL;
1323 GtkBuilder *pBuilder = gtk_builder_new();
1324 AssertPtr(pBuilder);
1325 if (!gtk_builder_add_from_file(pBuilder, "/usr/share/xgreeters/vbox-greeter.ui", &pError))
1326 {
1327 AssertPtr(pError);
1328 vboxGreeterError("unable to load UI: %s", pError->message);
1329 return RTEXITCODE_FAILURE;
1330 }
1331
1332 GtkWindow *pWndGreeter = GTK_WINDOW(gtk_builder_get_object(pBuilder, VBOX_GREETER_UI_WND_GREETER));
1333 AssertPtr(pWndGreeter);
1334 GtkButton *pBtnLogin = GTK_BUTTON(gtk_builder_get_object(pBuilder, VBOX_GREETER_UI_BTN_LOGIN));
1335 AssertPtr(pBtnLogin);
1336 GtkLabel *pLblInfo = GTK_LABEL(gtk_builder_get_object(pBuilder, VBOX_GREETER_UI_LBL_INFO));
1337 AssertPtr(pLblInfo);
1338
1339 ctx.pBuilder = pBuilder;
1340
1341 g_signal_connect(G_OBJECT(pBtnLogin), "clicked", G_CALLBACK(cb_btn_login), &ctx);
1342
1343 GdkRectangle rectScreen;
1344 gdk_screen_get_monitor_geometry(gdk_screen_get_default(), gdk_screen_get_primary_monitor(gdk_screen_get_default()), &rectScreen);
1345 vboxGreeterLog("monitor (default) is %dx%d\n", rectScreen.width, rectScreen.height);
1346
1347 gint iWndX, iWndY;
1348 gtk_window_get_default_size(pWndGreeter, &iWndX, &iWndY);
1349 vboxGreeterLog("greeter is %dx%d\n", iWndX, iWndY);
1350
1351 gtk_window_move(pWndGreeter,
1352 (rectScreen.width / 2) - (iWndX / 2),
1353 (rectScreen.height / 2) - (iWndY / 2));
1354 gtk_widget_show(GTK_WIDGET(pWndGreeter));
1355
1356 g_clear_error(&pError);
1357#endif /* !VBOX_WITH_FLTK */
1358
1359 /* GType is needed in any case (for LightDM), whether we
1360 * use GTK3 or not. */
1361 g_type_init();
1362
1363 GMainLoop *pMainLoop = g_main_loop_new(NULL, FALSE /* Not yet running */);
1364 AssertPtr(pMainLoop); NOREF(pMainLoop);
1365
1366 LightDMGreeter *pGreeter = lightdm_greeter_new();
1367 AssertPtr(pGreeter);
1368
1369 g_signal_connect(pGreeter, "show-prompt", G_CALLBACK(cb_lightdm_show_prompt), &ctx);
1370 g_signal_connect(pGreeter, "show-message", G_CALLBACK(cb_lightdm_show_message), &ctx);
1371 g_signal_connect(pGreeter, "authentication-complete", G_CALLBACK(cb_lightdm_auth_complete), &ctx);
1372
1373 ctx.pGreeter = pGreeter;
1374
1375 if (!lightdm_greeter_connect_sync(pGreeter, NULL))
1376 {
1377 vboxGreeterError("unable to connect to LightDM server, aborting\n");
1378 return RTEXITCODE_FAILURE;
1379 }
1380
1381 vboxGreeterLog("connected to LightDM server\n");
1382
1383#ifdef VBOX_WITH_GUEST_PROPS
1384 bool fCheckCreds = false;
1385 if (uClientId) /* Connected to guest property service? */
1386 {
1387 char szVal[256];
1388 rc2 = vbox_read_prop(uClientId,
1389 "/VirtualBox/GuestAdd/PAM/CredsWait",
1390 true /* Read-only on guest */,
1391 szVal, sizeof(szVal), NULL /* Timestamp. */);
1392 if (RT_SUCCESS(rc2))
1393 {
1394 uint32_t uTimeoutMS = RT_INDEFINITE_WAIT; /* Wait infinite by default. */
1395 rc2 = vbox_read_prop(uClientId,
1396 "/VirtualBox/GuestAdd/PAM/CredsWaitTimeout",
1397 true /* Read-only on guest */,
1398 szVal, sizeof(szVal), NULL /* Timestamp. */);
1399 if (RT_SUCCESS(rc2))
1400 {
1401 uTimeoutMS = RTStrToUInt32(szVal);
1402 if (!uTimeoutMS)
1403 {
1404 vboxGreeterError("pam_vbox_authenticate: invalid waiting timeout value specified, defaulting to infinite timeout\n");
1405 uTimeoutMS = RT_INDEFINITE_WAIT;
1406 }
1407 else
1408 uTimeoutMS = uTimeoutMS * 1000; /* Make ms out of s. */
1409 }
1410
1411 ctx.uTimeoutMS = uTimeoutMS;
1412
1413 rc2 = vbox_read_prop(uClientId,
1414 "/VirtualBox/GuestAdd/PAM/CredsMsgWaiting",
1415 true /* Read-only on guest */,
1416 szVal, sizeof(szVal), NULL /* Timestamp. */);
1417 if (RT_SUCCESS(rc2))
1418 {
1419# ifdef VBOX_WITH_FLTK
1420 Assert(pLblInfo);
1421 pLblInfo->copy_label(szVal);
1422# else
1423 gtk_label_set_text(pLblInfo, szVal);
1424# endif
1425 }
1426
1427 /* Get initial timestamp so that we can compare the time
1428 * whether the value has been changed or not in our event callback. */
1429 vbox_read_prop(uClientId,
1430 "/VirtualBox/GuestAdd/PAM/CredsWaitAbort",
1431 true /* Read-only on guest */,
1432 szVal, sizeof(szVal), &ctx.uTsAbort);
1433
1434 if (RT_SUCCESS(rc))
1435 {
1436 /* Before we actuall wait for credentials just make sure we didn't already get credentials
1437 * set so that we can skip waiting for them ... */
1438 rc2 = vboxGreeterCheckCreds(&ctx);
1439 if (rc2 == VERR_NOT_FOUND)
1440 {
1441 /* Get current time stamp to later calculate rest of timeout left. */
1442 ctx.uStartMS = RTTimeMilliTS();
1443
1444 fCheckCreds = true;
1445 }
1446 }
1447 }
1448
1449 /* Start the timer to check credentials availability. */
1450 if (fCheckCreds)
1451 {
1452 vboxGreeterLog("No credentials available on startup, starting to check periodically ...\n");
1453# ifdef VBOX_WITH_FLTK
1454 Fl::add_timeout(0.5 /* 500 ms */, cb_check_creds, &ctx);
1455# else
1456 g_timeout_add(500 /* ms */, (GSourceFunc)cb_check_creds, &ctx);
1457# endif
1458 }
1459 }
1460#endif /* VBOX_WITH_GUEST_PROPS */
1461
1462#ifdef VBOX_WITH_FLTK
1463 /*
1464 * Do own GDK main loop processing because FLTK also needs
1465 * to have the chance of processing its events.
1466 */
1467 GMainContext *pMainCtx = g_main_context_default();
1468 AssertPtr(pMainCtx);
1469
1470 while (g_fRunning)
1471 {
1472 g_main_context_iteration(pMainCtx,
1473 FALSE /* No blocking */);
1474 Fl::check();
1475 RTThreadSleep(10); /* Wait a bit, don't hog the CPU too much. */
1476 }
1477
1478 g_main_context_unref(pMainCtx);
1479
1480# ifdef VBOX_GREETER_WITH_PNG_SUPPORT
1481 if (pImgBanner)
1482 {
1483 delete pImgBanner; /* Call destructor to free bitmap data. */
1484 pImgBanner = NULL;
1485 }
1486# endif /* VBOX_GREETER_WITH_PNG_SUPPORT */
1487#else /* !VBOX_WITH_FLTK */
1488 gtk_main();
1489 /** @todo Never reached so far. LightDM sends a SIGTERM. */
1490#endif /* !VBOX_WITH_FLTK */
1491
1492 vboxGreeterLog("terminating\n");
1493
1494#ifdef VBOX_WITH_GUEST_PROPS
1495 if (uClientId)
1496 {
1497 rc2 = VbglR3GuestPropDisconnect(uClientId);
1498 AssertRC(rc2);
1499 }
1500#endif /* VBOX_WITH_GUEST_PROPS */
1501
1502 VbglR3Term();
1503
1504 RTEXITCODE rcExit = RT_SUCCESS(rc)
1505 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
1506
1507 vboxGreeterLog("terminated with exit code %ld (rc=%Rrc)\n",
1508 rcExit, rc);
1509
1510 vboxGreeterLogDestroy();
1511
1512 return rcExit;
1513}
1514
1515#ifdef DEBUG
1516DECLEXPORT(void) RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1517{
1518 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
1519}
1520#endif
1521
Note: See TracBrowser for help on using the repository browser.

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