VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/RTPathUserHome-posix.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.6 KB
RevLine 
[1]1/* $Id: RTPathUserHome-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
[8245]3 * IPRT - Path Manipulation, POSIX.
[1]4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[1]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
[5999]11 *
[96407]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 *
[5999]25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
[96407]27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
[5999]29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
[96407]33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
[1]35 */
36
37
[57358]38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
[1]41#define LOG_GROUP RTLOGGROUP_PATH
42#include <stdlib.h>
43#include <limits.h>
44#include <errno.h>
45#include <unistd.h>
46#include <sys/stat.h>
47#include <sys/time.h>
48#include <stdio.h>
[4026]49#include <sys/types.h>
50#include <pwd.h>
[1]51
52#include <iprt/path.h>
[20819]53#include <iprt/env.h>
[1]54#include <iprt/assert.h>
55#include <iprt/string.h>
56#include <iprt/err.h>
57#include <iprt/log.h>
58#include "internal/path.h"
59#include "internal/fs.h"
60
61
[4029]62#ifndef RT_OS_L4
[4556]63/**
[4029]64 * Worker for RTPathUserHome that looks up the home directory
[4556]65 * using the getpwuid_r api.
66 *
[4029]67 * @returns IPRT status code.
68 * @param pszPath The path buffer.
69 * @param cchPath The size of the buffer.
70 * @param uid The User ID to query the home directory of.
[4556]71 */
[4029]72static int rtPathUserHomeByPasswd(char *pszPath, size_t cchPath, uid_t uid)
[1]73{
[4556]74 /*
75 * The getpwuid_r function uses the passed in buffer to "allocate" any
76 * extra memory it needs. On some systems we should probably use the
[4029]77 * sysconf function to find the appropriate buffer size, but since it won't
[4556]78 * work everywhere we'll settle with a 5KB buffer and ASSUME that it'll
79 * suffice for even the lengthiest user descriptions...
[4029]80 */
81 char achBuffer[5120];
82 struct passwd Passwd;
83 struct passwd *pPasswd;
84 memset(&Passwd, 0, sizeof(Passwd));
85 int rc = getpwuid_r(uid, &Passwd, &achBuffer[0], sizeof(achBuffer), &pPasswd);
86 if (rc != 0)
87 return RTErrConvertFromErrno(rc);
88 if (!pPasswd) /* uid not found in /etc/passwd */
89 return VERR_PATH_NOT_FOUND;
[4556]90
[1]91 /*
[4029]92 * Check that it isn't empty and that it exists.
[4026]93 */
[4029]94 struct stat st;
95 if ( !pPasswd->pw_dir
96 || !*pPasswd->pw_dir
97 || stat(pPasswd->pw_dir, &st)
98 || !S_ISDIR(st.st_mode))
99 return VERR_PATH_NOT_FOUND;
100
101 /*
102 * Convert it to UTF-8 and copy it to the return buffer.
103 */
[28915]104 return rtPathFromNativeCopy(pszPath, cchPath, pPasswd->pw_dir, NULL);
[4029]105}
[4026]106#endif
[4029]107
108
[4556]109/**
[4029]110 * Worker for RTPathUserHome that looks up the home directory
[4556]111 * using the HOME environment variable.
112 *
[4029]113 * @returns IPRT status code.
114 * @param pszPath The path buffer.
115 * @param cchPath The size of the buffer.
[4556]116 */
[4029]117static int rtPathUserHomeByEnv(char *pszPath, size_t cchPath)
118{
[4026]119 /*
[1]120 * Get HOME env. var it and validate it's existance.
121 */
[28915]122 int rc = VERR_PATH_NOT_FOUND;
123 const char *pszHome = RTEnvGet("HOME"); /** @todo Codeset confusion in RTEnv. */
[4636]124 if (pszHome)
[4556]125
[1]126 {
[4029]127 struct stat st;
128 if ( !stat(pszHome, &st)
129 && S_ISDIR(st.st_mode))
[28915]130 rc = rtPathFromNativeCopy(pszPath, cchPath, pszHome, NULL);
[1]131 }
[4029]132 return rc;
133}
[1]134
[4029]135
[14050]136RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
[4029]137{
138 int rc;
139#ifndef RT_OS_L4
140 /*
141 * We make an exception for the root user and use the system call
142 * getpwuid_r to determine their initial home path instead of
143 * reading it from the $HOME variable. This is because the $HOME
144 * variable does not get changed by sudo (and possibly su and others)
145 * which can cause root-owned files to appear in user's home folders.
146 */
147 uid_t uid = geteuid();
148 if (!uid)
149 rc = rtPathUserHomeByPasswd(pszPath, cchPath, uid);
150 else
151 rc = rtPathUserHomeByEnv(pszPath, cchPath);
[4556]152
153 /*
[4029]154 * On failure, retry using the alternative method.
155 * (Should perhaps restrict the retry cases a bit more here...)
156 */
157 if ( RT_FAILURE(rc)
158 && rc != VERR_BUFFER_OVERFLOW)
159 {
160 if (!uid)
161 rc = rtPathUserHomeByEnv(pszPath, cchPath);
162 else
163 rc = rtPathUserHomeByPasswd(pszPath, cchPath, uid);
164 }
165#else /* RT_OS_L4 */
166 rc = rtPathUserHomeByEnv(pszPath, cchPath);
167#endif /* RT_OS_L4 */
168
[1]169 LogFlow(("RTPathUserHome(%p:{%s}, %u): returns %Rrc\n", pszPath,
170 RT_SUCCESS(rc) ? pszPath : "<failed>", cchPath, rc));
171 return rc;
172}
173
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use