beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync/* $Id$ */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync/** @file
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * IPRT - Path Manipulation, POSIX.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2010 Oracle Corporation
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync *
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * available from http://www.virtualbox.org. This file is free software;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * you can redistribute it and/or modify it under the terms of the GNU
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * General Public License (GPL) as published by the Free Software
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync *
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * The contents of this file may alternatively be used under the terms
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * of the Common Development and Distribution License Version 1.0
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * VirtualBox OSE distribution, in which case the provisions of the
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * CDDL are applicable instead of those of the GPL.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync *
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * You may elect to license modified versions of this file under the
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * terms and conditions of either the GPL or the CDDL or both.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync/*******************************************************************************
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync* Header Files *
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync*******************************************************************************/
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#define LOG_GROUP RTLOGGROUP_PATH
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <stdlib.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <limits.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <errno.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <unistd.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <sys/stat.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <sys/time.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <stdio.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <sys/types.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <pwd.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <iprt/path.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <iprt/env.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <iprt/assert.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <iprt/string.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <iprt/err.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include <iprt/log.h>
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include "internal/path.h"
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#include "internal/fs.h"
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#ifndef RT_OS_L4
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync/**
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * Worker for RTPathUserHome that looks up the home directory
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * using the getpwuid_r api.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync *
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @returns IPRT status code.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @param pszPath The path buffer.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @param cchPath The size of the buffer.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @param uid The User ID to query the home directory of.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsyncstatic int rtPathUserHomeByPasswd(char *pszPath, size_t cchPath, uid_t uid)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync{
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync /*
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * The getpwuid_r function uses the passed in buffer to "allocate" any
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * extra memory it needs. On some systems we should probably use the
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * sysconf function to find the appropriate buffer size, but since it won't
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * work everywhere we'll settle with a 5KB buffer and ASSUME that it'll
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * suffice for even the lengthiest user descriptions...
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync char achBuffer[5120];
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync struct passwd Passwd;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync struct passwd *pPasswd;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync memset(&Passwd, 0, sizeof(Passwd));
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync int rc = getpwuid_r(uid, &Passwd, &achBuffer[0], sizeof(achBuffer), &pPasswd);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if (rc != 0)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync return RTErrConvertFromErrno(rc);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if (!pPasswd) /* uid not found in /etc/passwd */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync return VERR_PATH_NOT_FOUND;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync /*
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * Check that it isn't empty and that it exists.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync struct stat st;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if ( !pPasswd->pw_dir
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync || !*pPasswd->pw_dir
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync || stat(pPasswd->pw_dir, &st)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync || !S_ISDIR(st.st_mode))
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync return VERR_PATH_NOT_FOUND;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync /*
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * Convert it to UTF-8 and copy it to the return buffer.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync return rtPathFromNativeCopy(pszPath, cchPath, pPasswd->pw_dir, NULL);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync}
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#endif
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync/**
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * Worker for RTPathUserHome that looks up the home directory
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * using the HOME environment variable.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync *
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @returns IPRT status code.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @param pszPath The path buffer.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * @param cchPath The size of the buffer.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsyncstatic int rtPathUserHomeByEnv(char *pszPath, size_t cchPath)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync{
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync /*
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * Get HOME env. var it and validate it's existance.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync int rc = VERR_PATH_NOT_FOUND;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync const char *pszHome = RTEnvGet("HOME"); /** @todo Codeset confusion in RTEnv. */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if (pszHome)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync {
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync struct stat st;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if ( !stat(pszHome, &st)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync && S_ISDIR(st.st_mode))
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync rc = rtPathFromNativeCopy(pszPath, cchPath, pszHome, NULL);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync }
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync return rc;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync}
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsyncRTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync{
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync int rc;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#ifndef RT_OS_L4
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync /*
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * We make an exception for the root user and use the system call
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * getpwuid_r to determine their initial home path instead of
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * reading it from the $HOME variable. This is because the $HOME
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * variable does not get changed by sudo (and possibly su and others)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * which can cause root-owned files to appear in user's home folders.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync uid_t uid = geteuid();
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if (!uid)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync rc = rtPathUserHomeByPasswd(pszPath, cchPath, uid);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync else
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync rc = rtPathUserHomeByEnv(pszPath, cchPath);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync /*
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * On failure, retry using the alternative method.
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync * (Should perhaps restrict the retry cases a bit more here...)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if ( RT_FAILURE(rc)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync && rc != VERR_BUFFER_OVERFLOW)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync {
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync if (!uid)
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync rc = rtPathUserHomeByEnv(pszPath, cchPath);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync else
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync rc = rtPathUserHomeByPasswd(pszPath, cchPath, uid);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync }
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#else /* RT_OS_L4 */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync rc = rtPathUserHomeByEnv(pszPath, cchPath);
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync#endif /* RT_OS_L4 */
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync LogFlow(("RTPathUserHome(%p:{%s}, %u): returns %Rrc\n", pszPath,
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync RT_SUCCESS(rc) ? pszPath : "<failed>", cchPath, rc));
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync return rc;
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync}
beb1b4125f65953ac9ed0ba843cf6248e333d860vboxsync