VirtualBox

source: vbox/trunk/src/VBox/Additions/common/pam/pam_vbox.cpp@ 95328

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

GA/pam_vbox.cpp: Fixed leak in pam_vbox_wait_prop.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.1 KB
Line 
1/* $Id: pam_vbox.cpp 95328 2022-06-21 14:42:41Z vboxsync $ */
2/** @file
3 * pam_vbox - PAM module for auto logons.
4 */
5
6/*
7 * Copyright (C) 2008-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 PAM_SM_AUTH
23#define PAM_SM_ACCOUNT
24#define PAM_SM_PASSWORD
25#define PAM_SM_SESSION
26
27#ifdef DEBUG
28# define PAM_DEBUG
29#endif
30
31#include <security/pam_appl.h>
32#ifdef RT_OS_LINUX
33# include <security/_pam_macros.h>
34#endif
35
36#include <pwd.h>
37#include <syslog.h>
38#include <stdlib.h>
39
40#include <iprt/assert.h>
41#include <iprt/buildconfig.h>
42#include <iprt/err.h>
43#include <iprt/env.h>
44#include <iprt/initterm.h>
45#include <iprt/mem.h>
46#include <iprt/stream.h>
47#include <iprt/string.h>
48#include <iprt/thread.h>
49#include <iprt/time.h>
50
51#include <VBox/VBoxGuestLib.h>
52
53#include <VBox/log.h>
54#include <VBox/HostServices/GuestPropertySvc.h>
55
56#define VBOX_MODULE_NAME "pam_vbox"
57
58#define VBOX_PAM_FLAG_SILENT "PAM_SILENT"
59#define VBOX_PAM_FLAG_DISALLOW_NULL_AUTHTOK "PAM_DISALLOW_NULL_AUTHTOK"
60#define VBOX_PAM_FLAG_ESTABLISH_CRED "PAM_ESTABLISH_CRED"
61#define VBOX_PAM_FLAG_DELETE_CRED "PAM_DELETE_CRED"
62#define VBOX_PAM_FLAG_REINITIALIZE_CRED "PAM_REINITIALIZE_CRED"
63#define VBOX_PAM_FLAG_REFRESH_CRED "PAM_REFRESH_CRED"
64
65RT_C_DECLS_BEGIN
66DECLEXPORT(int) pam_sm_authenticate(pam_handle_t *hPAM, int iFlags, int argc, const char **argv);
67DECLEXPORT(int) pam_sm_setcred(pam_handle_t *hPAM, int iFlags, int argc, const char **argv);
68DECLEXPORT(int) pam_sm_acct_mgmt(pam_handle_t *hPAM, int iFlags, int argc, const char **argv);
69DECLEXPORT(int) pam_sm_open_session(pam_handle_t *hPAM, int iFlags, int argc, const char **argv);
70DECLEXPORT(int) pam_sm_close_session(pam_handle_t *hPAM, int iFlags, int argc, const char **argv);
71DECLEXPORT(int) pam_sm_chauthtok(pam_handle_t *hPAM, int iFlags, int argc, const char **argv);
72RT_C_DECLS_END
73
74/** For debugging. */
75#ifdef DEBUG
76static pam_handle_t *g_pam_handle;
77static int g_verbosity = 99;
78#else
79static int g_verbosity = 0;
80#endif
81
82/**
83 * User-provided thread data for the credentials waiting thread.
84 */
85typedef struct PAMVBOXTHREAD
86{
87 /** The PAM handle. */
88 pam_handle_t *hPAM;
89 /** The timeout (in ms) to wait for credentials. */
90 uint32_t uTimeoutMS;
91 /** The overall result of the thread operation. */
92 int rc;
93} PAMVBOXTHREAD, *PPAMVBOXTHREAD;
94
95/**
96 * Write to system log.
97 *
98 * @param pszBuf Buffer to write to the log (NULL-terminated)
99 */
100static void pam_vbox_writesyslog(char *pszBuf)
101{
102#ifdef RT_OS_LINUX
103 openlog("pam_vbox", LOG_PID, LOG_AUTHPRIV);
104 syslog(LOG_ERR, "%s", pszBuf);
105 closelog();
106#elif defined(RT_OS_SOLARIS)
107 syslog(LOG_ERR, "pam_vbox: %s\n", pszBuf);
108#endif
109}
110
111
112/**
113 * Displays an error message.
114 *
115 * @param hPAM PAM handle.
116 * @param pszFormat The message text.
117 * @param ... Format arguments.
118 */
119static void pam_vbox_error(pam_handle_t *hPAM, const char *pszFormat, ...)
120{
121 RT_NOREF1(hPAM);
122 va_list va;
123 char *buf;
124 va_start(va, pszFormat);
125 if (RT_SUCCESS(RTStrAPrintfV(&buf, pszFormat, va)))
126 {
127 LogRel(("%s: Error: %s", VBOX_MODULE_NAME, buf));
128 pam_vbox_writesyslog(buf);
129 RTStrFree(buf);
130 }
131 va_end(va);
132}
133
134
135/**
136 * Displays a debug message.
137 *
138 * @param hPAM PAM handle.
139 * @param pszFormat The message text.
140 * @param ... Format arguments.
141 */
142static void pam_vbox_log(pam_handle_t *hPAM, const char *pszFormat, ...)
143{
144 RT_NOREF1(hPAM);
145 if (g_verbosity)
146 {
147 va_list va;
148 char *buf;
149 va_start(va, pszFormat);
150 if (RT_SUCCESS(RTStrAPrintfV(&buf, pszFormat, va)))
151 {
152 /* Only do normal logging in debug mode; could contain
153 * sensitive data! */
154 LogRel(("%s: %s", VBOX_MODULE_NAME, buf));
155 /* Log to syslog */
156 pam_vbox_writesyslog(buf);
157 RTStrFree(buf);
158 }
159 va_end(va);
160 }
161}
162
163
164/**
165 * Sets a message using PAM's conversation function.
166 *
167 * @return IPRT status code.
168 * @param hPAM PAM handle.
169 * @param iStyle Style of message (0 = Information, 1 = Prompt
170 * echo off, 2 = Prompt echo on, 3 = Error).
171 * @param pszText Message text to set.
172 */
173static int vbox_set_msg(pam_handle_t *hPAM, int iStyle, const char *pszText)
174{
175 AssertPtrReturn(hPAM, VERR_INVALID_POINTER);
176 AssertPtrReturn(pszText, VERR_INVALID_POINTER);
177
178 if (!iStyle)
179 iStyle = PAM_TEXT_INFO;
180
181 int rc = VINF_SUCCESS;
182
183 pam_message msg;
184 msg.msg_style = iStyle;
185#ifdef RT_OS_SOLARIS
186 msg.msg = (char*)pszText;
187#else
188 msg.msg = pszText;
189#endif
190
191#ifdef RT_OS_SOLARIS
192 pam_conv *conv = NULL;
193 int pamrc = pam_get_item(hPAM, PAM_CONV, (void **)&conv);
194#else
195 const pam_conv *conv = NULL;
196 int pamrc = pam_get_item(hPAM, PAM_CONV, (const void **)&conv);
197#endif
198 if ( pamrc == PAM_SUCCESS
199 && conv)
200 {
201 pam_response *resp = NULL;
202#ifdef RT_OS_SOLARIS
203 pam_message *msg_p = &msg;
204#else
205 const pam_message *msg_p = &msg;
206#endif
207 pam_vbox_log(hPAM, "Showing message \"%s\" (type %d)", pszText, iStyle);
208
209 pamrc = conv->conv(1 /* One message only */, &msg_p, &resp, conv->appdata_ptr);
210 if (resp != NULL) /* If we use PAM_TEXT_INFO we never will get something back! */
211 {
212 if (resp->resp)
213 {
214 pam_vbox_log(hPAM, "Response to message \"%s\" was \"%s\"",
215 pszText, resp->resp);
216 /** @todo Save response! */
217 free(resp->resp);
218 }
219 free(resp);
220 }
221 }
222 else
223 rc = VERR_NOT_FOUND;
224 return rc;
225}
226
227
228/**
229 * Initializes pam_vbox.
230 *
231 * @return IPRT status code.
232 * @param hPAM PAM handle.
233 */
234static int pam_vbox_init(pam_handle_t *hPAM)
235{
236#ifdef DEBUG
237 g_pam_handle = hPAM; /* hack for getting assertion text */
238#endif
239
240 /* Don't make assertions panic because the PAM stack +
241 * the current logon module won't work anymore (or just restart).
242 * This could result in not able to log into the system anymore. */
243 RTAssertSetMayPanic(false);
244
245 pam_vbox_log(hPAM, "pam_vbox: %sr%s, running on %s\n",
246 RTBldCfgVersion(), RTBldCfgRevisionStr(), RTBldCfgTargetArch());
247
248 int rc = RTR3InitDll(0);
249 if (RT_FAILURE(rc))
250 {
251 pam_vbox_error(hPAM, "pam_vbox_init: could not init runtime! rc=%Rrc. Aborting\n", rc);
252 return PAM_SUCCESS; /* Jump out as early as we can to not mess around. */
253 }
254
255 pam_vbox_log(hPAM, "pam_vbox_init: runtime initialized\n");
256 if (RT_SUCCESS(rc))
257 {
258 rc = VbglR3InitUser();
259 if (RT_FAILURE(rc))
260 {
261 switch(rc)
262 {
263 case VERR_ACCESS_DENIED:
264 pam_vbox_error(hPAM, "pam_vbox_init: access is denied to guest driver! Please make sure you run with sufficient rights. Aborting\n");
265 break;
266
267 case VERR_FILE_NOT_FOUND:
268 pam_vbox_error(hPAM, "pam_vbox_init: guest driver not found! Guest Additions installed? Aborting\n");
269 break;
270
271 default:
272 pam_vbox_error(hPAM, "pam_vbox_init: could not init VbglR3 library! rc=%Rrc. Aborting\n", rc);
273 break;
274 }
275 }
276 pam_vbox_log(hPAM, "pam_vbox_init: guest lib initialized\n");
277 }
278
279 if (RT_SUCCESS(rc))
280 {
281 char *rhost = NULL;
282 char *tty = NULL;
283 char *prompt = NULL;
284#ifdef RT_OS_SOLARIS
285 pam_get_item(hPAM, PAM_RHOST, (void**) &rhost);
286 pam_get_item(hPAM, PAM_TTY, (void**) &tty);
287 pam_get_item(hPAM, PAM_USER_PROMPT, (void**) &prompt);
288#else
289 pam_get_item(hPAM, PAM_RHOST, (const void**) &rhost);
290 pam_get_item(hPAM, PAM_TTY, (const void**) &tty);
291 pam_get_item(hPAM, PAM_USER_PROMPT, (const void**) &prompt);
292#endif
293 pam_vbox_log(hPAM, "pam_vbox_init: rhost=%s, tty=%s, prompt=%s\n",
294 rhost ? rhost : "<none>", tty ? tty : "<none>", prompt ? prompt : "<none>");
295 }
296
297 return rc;
298}
299
300
301/**
302 * Shuts down pam_vbox.
303 *
304 * @return IPRT status code.
305 * @param hPAM PAM handle.
306 */
307static void pam_vbox_shutdown(pam_handle_t *hPAM)
308{
309 RT_NOREF1(hPAM);
310 VbglR3Term();
311}
312
313
314/**
315 * Checks for credentials provided by the host / HGCM.
316 *
317 * @return IPRT status code. VERR_NOT_FOUND if no credentials are available,
318 * VINF_SUCCESS on successful retrieval or another IPRT error.
319 * @param hPAM PAM handle.
320 */
321static int pam_vbox_check_creds(pam_handle_t *hPAM)
322{
323 int rc = VbglR3CredentialsQueryAvailability();
324 if (RT_FAILURE(rc))
325 {
326 if (rc != VERR_NOT_FOUND)
327 pam_vbox_error(hPAM, "pam_vbox_check_creds: could not query for credentials! rc=%Rrc. Aborting\n", rc);
328#ifdef DEBUG
329 else
330 pam_vbox_log(hPAM, "pam_vbox_check_creds: no credentials available\n");
331#endif
332 }
333 else
334 {
335 char *pszUsername;
336 char *pszPassword;
337 char *pszDomain;
338
339 rc = VbglR3CredentialsRetrieve(&pszUsername, &pszPassword, &pszDomain);
340 if (RT_FAILURE(rc))
341 {
342 pam_vbox_error(hPAM, "pam_vbox_check_creds: could not retrieve credentials! rc=%Rrc. Aborting\n", rc);
343 }
344 else
345 {
346#ifdef DEBUG
347 pam_vbox_log(hPAM, "pam_vbox_check_creds: credentials retrieved: user=%s, password=%s, domain=%s\n",
348 pszUsername, pszPassword, pszDomain);
349#else
350 /* Don't log passwords in release mode! */
351 pam_vbox_log(hPAM, "pam_vbox_check_creds: credentials retrieved: user=%s, password=XXX, domain=%s\n",
352 pszUsername, pszDomain);
353#endif
354 /* Fill credentials into PAM. */
355 int pamrc = pam_set_item(hPAM, PAM_USER, pszUsername);
356 if (pamrc != PAM_SUCCESS)
357 {
358 pam_vbox_error(hPAM, "pam_vbox_check_creds: could not set user name! pamrc=%d, msg=%s. Aborting\n",
359 pamrc, pam_strerror(hPAM, pamrc));
360 }
361 else
362 {
363 pamrc = pam_set_item(hPAM, PAM_AUTHTOK, pszPassword);
364 if (pamrc != PAM_SUCCESS)
365 pam_vbox_error(hPAM, "pam_vbox_check_creds: could not set password! pamrc=%d, msg=%s. Aborting\n",
366 pamrc, pam_strerror(hPAM, pamrc));
367
368 }
369 /** @todo Add handling domains as well. */
370 VbglR3CredentialsDestroy(pszUsername, pszPassword, pszDomain,
371 3 /* Three wipe passes */);
372 pam_vbox_log(hPAM, "pam_vbox_check_creds: returned with pamrc=%d, msg=%s\n",
373 pamrc, pam_strerror(hPAM, pamrc));
374 }
375 }
376
377#ifdef DEBUG
378 pam_vbox_log(hPAM, "pam_vbox_check_creds: returned with rc=%Rrc\n", rc);
379#endif
380 return rc;
381}
382
383/**
384 * Reads a guest property.
385 *
386 * @return IPRT status code.
387 * @param hPAM PAM handle.
388 * @param uClientID Guest property service client ID.
389 * @param pszKey Key (name) of guest property to read.
390 * @param fReadOnly Indicates whether this key needs to be
391 * checked if it only can be read (and *not* written)
392 * by the guest.
393 * @param pszValue Buffer where to store the key's value.
394 * @param cbValue Size of buffer (in bytes).
395 */
396static int pam_vbox_read_prop(pam_handle_t *hPAM, uint32_t uClientID,
397 const char *pszKey, bool fReadOnly,
398 char *pszValue, size_t cbValue)
399{
400 AssertPtrReturn(hPAM, VERR_INVALID_POINTER);
401 AssertReturn(uClientID, VERR_INVALID_PARAMETER);
402 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
403 AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
404
405 int rc;
406
407 uint64_t u64Timestamp = 0;
408 char *pszValTemp;
409 char *pszFlags = NULL;
410 /* The buffer for storing the data and its initial size. We leave a bit
411 * of space here in case the maximum values are raised. */
412 void *pvBuf = NULL;
413 uint32_t cbBuf = GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN + _1K;
414
415 /* Because there is a race condition between our reading the size of a
416 * property and the guest updating it, we loop a few times here and
417 * hope. Actually this should never go wrong, as we are generous
418 * enough with buffer space. */
419 for (unsigned i = 0; ; i++)
420 {
421 void *pvTmpBuf = RTMemRealloc(pvBuf, cbBuf);
422 if (pvTmpBuf)
423 {
424 pvBuf = pvTmpBuf;
425 rc = VbglR3GuestPropRead(uClientID, pszKey, pvBuf, cbBuf,
426 &pszValTemp, &u64Timestamp, &pszFlags,
427 &cbBuf);
428 if (rc == VERR_BUFFER_OVERFLOW && i < 10)
429 {
430 /* Buffer too small, try it with a bigger one next time. */
431 cbBuf += _1K;
432 continue; /* Try next round. */
433 }
434 }
435 else
436 rc = VERR_NO_MEMORY;
437 break; /* Everything except VERR_BUFFER_OVERFLOW makes us bail out ... */
438 }
439
440 if (RT_SUCCESS(rc))
441 {
442 /* Check security bits. */
443 if (pszFlags)
444 {
445 if ( fReadOnly
446 && !RTStrStr(pszFlags, "RDONLYGUEST"))
447 {
448 /* If we want a property which is read-only on the guest
449 * and it is *not* marked as such, deny access! */
450 pam_vbox_error(hPAM, "pam_vbox_read_prop: key \"%s\" should be read-only on guest but it is not\n", pszKey);
451 rc = VERR_ACCESS_DENIED;
452 }
453 }
454 else /* No flags, no access! */
455 {
456 pam_vbox_error(hPAM, "pam_vbox_read_prop: key \"%s\" contains no/wrong flags (%s)\n", pszKey, pszFlags);
457 rc = VERR_ACCESS_DENIED;
458 }
459
460 if (RT_SUCCESS(rc))
461 {
462 /* If everything went well copy property value to our destination buffer. */
463 if (!RTStrPrintf(pszValue, cbValue, "%s", pszValTemp))
464 {
465 pam_vbox_error(hPAM, "pam_vbox_read_prop: could not store value of key \"%s\"\n", pszKey);
466 rc = VERR_INVALID_PARAMETER;
467 }
468
469 if (RT_SUCCESS(rc))
470 pam_vbox_log(hPAM, "pam_vbox_read_prop: read key \"%s\"=\"%s\"\n", pszKey, pszValue);
471 }
472 }
473
474 RTMemFree(pvBuf);
475 pam_vbox_log(hPAM, "pam_vbox_read_prop: read key \"%s\" with rc=%Rrc\n",
476 pszKey, rc);
477 return rc;
478}
479
480
481/**
482 * Waits for a guest property to be changed.
483 *
484 * @return IPRT status code.
485 * @param hPAM PAM handle.
486 * @param uClientID Guest property service client ID.
487 * @param pszKey Key (name) of guest property to wait for.
488 * @param uTimeoutMS Timeout (in ms) to wait for the change. Specify
489 * RT_INDEFINITE_WAIT to wait indefinitly.
490 */
491static int pam_vbox_wait_prop(pam_handle_t *hPAM, uint32_t uClientID,
492 const char *pszKey, uint32_t uTimeoutMS)
493{
494 AssertPtrReturn(hPAM, VERR_INVALID_POINTER);
495 AssertReturn(uClientID, VERR_INVALID_PARAMETER);
496 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
497
498 int rc;
499
500 /* The buffer for storing the data and its initial size. We leave a bit
501 * of space here in case the maximum values are raised. */
502 void *pvBuf = NULL;
503 uint32_t cbBuf = GUEST_PROP_MAX_NAME_LEN + GUEST_PROP_MAX_VALUE_LEN + GUEST_PROP_MAX_FLAGS_LEN + _1K;
504
505 for (int i = 0; ; i++)
506 {
507 void *pvTmpBuf = RTMemRealloc(pvBuf, cbBuf);
508 if (pvTmpBuf)
509 {
510 char *pszName = NULL;
511 char *pszValue = NULL;
512 uint64_t u64TimestampOut = 0;
513 char *pszFlags = NULL;
514
515 pvBuf = pvTmpBuf;
516 rc = VbglR3GuestPropWait(uClientID, pszKey, pvBuf, cbBuf,
517 0 /* Last timestamp; just wait for next event */, uTimeoutMS,
518 &pszName, &pszValue, &u64TimestampOut,
519 &pszFlags, &cbBuf, NULL /* pfWasDeleted */);
520 if (rc == VERR_BUFFER_OVERFLOW && i < 10)
521 {
522 cbBuf += _1K; /* Buffer too small, try it with a bigger one more time. */
523 continue;
524 }
525 }
526 else
527 rc = VERR_NO_MEMORY;
528 break;
529 }
530
531 RTMemFree(pvBuf);
532 return rc;
533}
534
535/**
536 * Thread function waiting for credentials to arrive.
537 *
538 * @return IPRT status code.
539 * @param hThreadSelf Thread handle.
540 * @param pvUser Pointer to a PAMVBOXTHREAD structure providing
541 * required data used / set by the thread.
542 */
543static DECLCALLBACK(int) pam_vbox_wait_thread(RTTHREAD hThreadSelf, void *pvUser)
544{
545 RT_NOREF1(hThreadSelf);
546 PPAMVBOXTHREAD pUserData = (PPAMVBOXTHREAD)pvUser;
547 AssertPtr(pUserData);
548
549 int rc = VINF_SUCCESS;
550 /* Get current time stamp to later calculate rest of timeout left. */
551 uint64_t u64StartMS = RTTimeMilliTS();
552
553 uint32_t uClientID = 0;
554 rc = VbglR3GuestPropConnect(&uClientID);
555 if (RT_FAILURE(rc))
556 {
557 pam_vbox_error(pUserData->hPAM, "pam_vbox_wait_thread: Unable to connect to guest property service, rc=%Rrc\n", rc);
558 }
559 else
560 {
561 pam_vbox_log(pUserData->hPAM, "pam_vbox_wait_thread: clientID=%u\n", uClientID);
562
563 for (;;)
564 {
565
566 if (uClientID)
567 {
568 rc = pam_vbox_wait_prop(pUserData->hPAM, uClientID,
569 "/VirtualBox/GuestAdd/PAM/CredsWaitAbort",
570 500 /* Wait 500ms, same as VBoxGINA/VBoxCredProv. */);
571 switch (rc)
572 {
573 case VINF_SUCCESS:
574 /* Somebody (guest/host) wants to abort waiting for credentials. */
575 break;
576
577 case VERR_INTERRUPTED:
578 pam_vbox_error(pUserData->hPAM, "pam_vbox_wait_thread: The abort notification request timed out or was interrupted\n");
579 break;
580
581 case VERR_TIMEOUT:
582 /* We did not receive an abort message within time. */
583 break;
584
585 case VERR_TOO_MUCH_DATA:
586 pam_vbox_error(pUserData->hPAM, "pam_vbox_wait_thread: Temporarily unable to get abort notification\n");
587 break;
588
589 default:
590 pam_vbox_error(pUserData->hPAM, "pam_vbox_wait_thread: The abort notification request failed with rc=%Rrc\n", rc);
591 break;
592 }
593
594 if (RT_SUCCESS(rc)) /* Abort waiting. */
595 {
596 pam_vbox_log(pUserData->hPAM, "pam_vbox_wait_thread: Got notification to abort waiting\n");
597 rc = VERR_CANCELLED;
598 break;
599 }
600 }
601
602 if ( RT_SUCCESS(rc)
603 || rc == VERR_TIMEOUT)
604 {
605 rc = pam_vbox_check_creds(pUserData->hPAM);
606 if (RT_SUCCESS(rc))
607 {
608 /* Credentials retrieved. */
609 break; /* Thread no longer is required, bail out. */
610 }
611 else if (rc == VERR_NOT_FOUND)
612 {
613 /* No credentials found, but try next round (if there's
614 * time left for) ... */
615 RTThreadSleep(500); /* Wait 500 ms. */
616 }
617 else
618 break; /* Something bad happend ... */
619 }
620 else
621 break;
622
623 /* Calculate timeout value left after process has been started. */
624 uint64_t u64Elapsed = RTTimeMilliTS() - u64StartMS;
625 /* Is it time to bail out? */
626 if (pUserData->uTimeoutMS < u64Elapsed)
627 {
628 pam_vbox_log(pUserData->hPAM, "pam_vbox_wait_thread: Waiting thread has reached timeout (%dms), exiting ...\n",
629 pUserData->uTimeoutMS);
630 rc = VERR_TIMEOUT;
631 break;
632 }
633 }
634 }
635 VbglR3GuestPropDisconnect(uClientID);
636
637 /* Save result. */
638 pUserData->rc = rc; /** @todo Use ASMAtomicXXX? */
639
640 int rc2 = RTThreadUserSignal(RTThreadSelf());
641 AssertRC(rc2);
642
643 pam_vbox_log(pUserData->hPAM, "pam_vbox_wait_thread: Waiting thread returned with rc=%Rrc\n", rc);
644 return rc;
645}
646
647/**
648 * Waits for credentials to arrive by creating and waiting for a thread.
649 *
650 * @return IPRT status code.
651 * @param hPAM PAM handle.
652 * @param uClientID Guest property service client ID.
653 * @param uTimeoutMS Timeout (in ms) to wait for the change. Specify
654 * RT_INDEFINITE_WAIT to wait indefinitly.
655 */
656static int pam_vbox_wait_for_creds(pam_handle_t *hPAM, uint32_t uClientID, uint32_t uTimeoutMS)
657{
658 RT_NOREF1(uClientID);
659 PAMVBOXTHREAD threadData;
660 threadData.hPAM = hPAM;
661 threadData.uTimeoutMS = uTimeoutMS;
662
663 RTTHREAD threadWait;
664 int rc = RTThreadCreate(&threadWait, pam_vbox_wait_thread,
665 (void *)&threadData, 0,
666 RTTHREADTYPE_DEFAULT, 0 /* Flags */, "pam_vbox");
667 if (RT_SUCCESS(rc))
668 {
669 pam_vbox_log(hPAM, "pam_vbox_wait_for_creds: Waiting for credentials (%dms) ...\n", uTimeoutMS);
670 /* Wait for thread to initialize. */
671 /** @todo We can do something else here in the meantime later. */
672 rc = RTThreadUserWait(threadWait, RT_INDEFINITE_WAIT);
673 if (RT_SUCCESS(rc))
674 rc = threadData.rc; /* Get back thread result to take further actions. */
675 }
676 else
677 pam_vbox_error(hPAM, "pam_vbox_wait_for_creds: Creating thread failed with rc=%Rrc\n", rc);
678
679 pam_vbox_log(hPAM, "pam_vbox_wait_for_creds: Waiting for credentials returned with rc=%Rrc\n", rc);
680 return rc;
681}
682
683DECLEXPORT(int) pam_sm_authenticate(pam_handle_t *hPAM, int iFlags, int argc, const char **argv)
684{
685 RT_NOREF1(iFlags);
686
687 /* Parse arguments. */
688 for (int i = 0; i < argc; i++)
689 {
690 if (!RTStrICmp(argv[i], "debug"))
691 g_verbosity = 1;
692 else
693 pam_vbox_error(hPAM, "pam_vbox_authenticate: unknown command line argument \"%s\"\n", argv[i]);
694 }
695 pam_vbox_log(hPAM, "pam_vbox_authenticate called\n");
696
697 int rc = pam_vbox_init(hPAM);
698 if (RT_FAILURE(rc))
699 return PAM_SUCCESS; /* Jump out as early as we can to not mess around. */
700
701 bool fFallback = true;
702
703 uint32_t uClientId;
704 rc = VbglR3GuestPropConnect(&uClientId);
705 if (RT_SUCCESS(rc))
706 {
707 char szVal[256];
708 rc = pam_vbox_read_prop(hPAM, uClientId,
709 "/VirtualBox/GuestAdd/PAM/CredsWait",
710 true /* Read-only on guest */,
711 szVal, sizeof(szVal));
712 if (RT_SUCCESS(rc))
713 {
714 /* All calls which are checked against rc2 are not critical, e.g. it does
715 * not matter if they succeed or not. */
716 uint32_t uTimeoutMS = RT_INDEFINITE_WAIT; /* Wait infinite by default. */
717 int rc2 = pam_vbox_read_prop(hPAM, uClientId,
718 "/VirtualBox/GuestAdd/PAM/CredsWaitTimeout",
719 true /* Read-only on guest */,
720 szVal, sizeof(szVal));
721 if (RT_SUCCESS(rc2))
722 {
723 uTimeoutMS = RTStrToUInt32(szVal);
724 if (!uTimeoutMS)
725 {
726 pam_vbox_error(hPAM, "pam_vbox_authenticate: invalid waiting timeout value specified, defaulting to infinite timeout\n");
727 uTimeoutMS = RT_INDEFINITE_WAIT;
728 }
729 else
730 uTimeoutMS = uTimeoutMS * 1000; /* Make ms out of s. */
731 }
732
733 rc2 = pam_vbox_read_prop(hPAM, uClientId,
734 "/VirtualBox/GuestAdd/PAM/CredsMsgWaiting",
735 true /* Read-only on guest */,
736 szVal, sizeof(szVal));
737 const char *pszWaitMsg = NULL;
738 if (RT_SUCCESS(rc2))
739 pszWaitMsg = szVal;
740
741 rc2 = vbox_set_msg(hPAM, 0 /* Info message */,
742 pszWaitMsg ? pszWaitMsg : "Waiting for credentials ...");
743 if (RT_FAILURE(rc2)) /* Not critical. */
744 pam_vbox_error(hPAM, "pam_vbox_authenticate: error setting waiting information message, rc=%Rrc\n", rc2);
745
746 if (RT_SUCCESS(rc))
747 {
748 /* Before we actuall wait for credentials just make sure we didn't already get credentials
749 * set so that we can skip waiting for them ... */
750 rc = pam_vbox_check_creds(hPAM);
751 if (rc == VERR_NOT_FOUND)
752 {
753 rc = pam_vbox_wait_for_creds(hPAM, uClientId, uTimeoutMS);
754 if (rc == VERR_TIMEOUT)
755 {
756 pam_vbox_log(hPAM, "pam_vbox_authenticate: no credentials given within time\n");
757
758 rc2 = pam_vbox_read_prop(hPAM, uClientId,
759 "/VirtualBox/GuestAdd/PAM/CredsMsgWaitTimeout",
760 true /* Read-only on guest */,
761 szVal, sizeof(szVal));
762 if (RT_SUCCESS(rc2))
763 {
764 rc2 = vbox_set_msg(hPAM, 0 /* Info message */, szVal);
765 AssertRC(rc2);
766 }
767 }
768 else if (rc == VERR_CANCELLED)
769 {
770 pam_vbox_log(hPAM, "pam_vbox_authenticate: waiting aborted\n");
771
772 rc2 = pam_vbox_read_prop(hPAM, uClientId,
773 "/VirtualBox/GuestAdd/PAM/CredsMsgWaitAbort",
774 true /* Read-only on guest */,
775 szVal, sizeof(szVal));
776 if (RT_SUCCESS(rc2))
777 {
778 rc2 = vbox_set_msg(hPAM, 0 /* Info message */, szVal);
779 AssertRC(rc2);
780 }
781 }
782 }
783
784 /* If we got here we don't need the fallback, so just deactivate it. */
785 fFallback = false;
786 }
787 }
788
789 VbglR3GuestPropDisconnect(uClientId);
790 }
791
792 if (fFallback)
793 {
794 pam_vbox_log(hPAM, "pam_vbox_authenticate: falling back to old method\n");
795
796 /* If anything went wrong in the code above we just do a credentials
797 * check like it was before: Try retrieving the stuff and authenticating. */
798 int rc2 = pam_vbox_check_creds(hPAM);
799 if (RT_SUCCESS(rc))
800 rc = rc2;
801 }
802
803 pam_vbox_shutdown(hPAM);
804
805 pam_vbox_log(hPAM, "pam_vbox_authenticate: overall result rc=%Rrc\n", rc);
806
807 /* Never report an error here because if no credentials from the host are available or something
808 * went wrong we then let do the authentication by the next module in the stack. */
809
810 /* We report success here because this is all we can do right now -- we passed the credentials
811 * to the next PAM module in the block above which then might do a shadow (like pam_unix/pam_unix2)
812 * password verification to "really" authenticate the user. */
813 return PAM_SUCCESS;
814}
815
816
817DECLEXPORT(int) pam_sm_setcred(pam_handle_t *hPAM, int iFlags, int argc, const char **argv)
818{
819 pam_vbox_log(hPAM, "pam_vbox_setcred called, iFlags=0x%x\n", iFlags);
820 for (int i = 0; i < argc; i++)
821 pam_vbox_log(hPAM, "pam_vbox_setcred: argv[%d] = %s\n", i, argv[i]);
822 return PAM_SUCCESS;
823}
824
825
826DECLEXPORT(int) pam_sm_acct_mgmt(pam_handle_t *hPAM, int iFlags, int argc, const char **argv)
827{
828 RT_NOREF3(iFlags, argc, argv);
829 pam_vbox_log(hPAM, "pam_vbox_acct_mgmt called\n");
830 return PAM_SUCCESS;
831}
832
833
834DECLEXPORT(int) pam_sm_open_session(pam_handle_t *hPAM, int iFlags, int argc, const char **argv)
835{
836 RT_NOREF3(iFlags, argc, argv);
837 pam_vbox_log(hPAM, "pam_vbox_open_session called\n");
838 RTPrintf("This session was provided by VirtualBox Guest Additions. Have a lot of fun!\n");
839 return PAM_SUCCESS;
840}
841
842
843DECLEXPORT(int) pam_sm_close_session(pam_handle_t *hPAM, int iFlags, int argc, const char **argv)
844{
845 RT_NOREF3(iFlags, argc, argv);
846 pam_vbox_log(hPAM, "pam_vbox_close_session called\n");
847 return PAM_SUCCESS;
848}
849
850
851DECLEXPORT(int) pam_sm_chauthtok(pam_handle_t *hPAM, int iFlags, int argc, const char **argv)
852{
853 RT_NOREF3(iFlags, argc, argv);
854 pam_vbox_log(hPAM, "pam_vbox_sm_chauthtok called\n");
855 return PAM_SUCCESS;
856}
857
858
859#ifdef DEBUG
860RTDECL(void) RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
861{
862 pam_vbox_log(g_pam_handle,
863 "\n!!Assertion Failed!!\n"
864 "Expression: %s\n"
865 "Location : %s(%d) %s\n",
866 pszExpr, pszFile, uLine, pszFunction);
867 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
868}
869#endif
870
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