VirtualBox

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

Last change on this file was 99739, checked in by vboxsync, 13 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

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

© 2023 Oracle
ContactPrivacy policyTerms of Use