VirtualBox

Changeset 29582 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 17, 2010 7:40:34 PM (15 years ago)
Author:
vboxsync
Message:

Runtime/r3/posix: support executing as different user (Linux only for now)

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/Makefile.kmk

    r29534 r29582  
    10601060 VBoxRT_LIBS                  += lzo2
    10611061endif
     1062VBoxRT_LIBS.linux              = \
     1063        crypt
    10621064VBoxRT_LIBS.darwin             = \
    10631065        iconv
  • trunk/src/VBox/Runtime/r3/posix/process-posix.cpp

    r29328 r29582  
    3939#include <fcntl.h>
    4040#include <signal.h>
     41#if defined(RT_OS_LINUX)
     42# include <pwd.h>
     43# include <shadow.h>
     44#endif
    4145#if defined(RT_OS_LINUX) || defined(RT_OS_OS2)
    4246# define HAVE_POSIX_SPAWN 1
     
    6165#include "internal/process.h"
    6266
     67
     68/**
     69 * Check the credentials and return the gid/uid of user.
     70 *
     71 * @param    pszUser     username
     72 * @param    pszPasswd   password
     73 * @param    gid         where to store the GID of the user
     74 * @param    uid         where to store the UID of the user
     75 * @returns IPRT status code
     76 */
     77static int rtCheckCredentials(const char *pszUser, const char *pszPasswd, gid_t *gid, uid_t *uid)
     78{
     79#if defined(RT_OS_LINUX)
     80    struct passwd *pw;
     81
     82    pw = getpwnam(pszUser);
     83    if (!pw)
     84        return VERR_PERMISSION_DENIED;
     85
     86    if (!pszPasswd)
     87        pszPasswd = "";
     88
     89    struct spwd *spwd;
     90    /* works only if /etc/shadow is accessible */
     91    spwd = getspnam(pszUser);
     92    if (spwd)
     93        pw->pw_passwd = spwd->sp_pwdp;
     94
     95    char *pszEncPasswd = crypt(pszPasswd, pw->pw_passwd);
     96    if (strcmp(pszEncPasswd, pw->pw_passwd))
     97        return VERR_PERMISSION_DENIED;
     98
     99    *gid = pw->pw_gid;
     100    *uid = pw->pw_uid;
     101    return VINF_SUCCESS;
     102#else
     103    return VERR_PERMISSION_DENIED;
     104#endif
     105}
    63106
    64107
     
    148191    if (pszAsUser)
    149192    {
    150         AssertMsgFailed(("Implement get uid by name lookup\n"));
    151         return VERR_NOT_IMPLEMENTED;
     193        rc = rtCheckCredentials(pszAsUser, pszPassword, &gid, &uid);
     194        if (RT_FAILURE(rc))
     195            return rc;
    152196    }
    153197
Note: See TracChangeset for help on using the changeset viewer.

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