VirtualBox

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

Last change on this file since 103795 was 98103, checked in by vboxsync, 2 years 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
Line 
1/* $Id: RTPathUserHome-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Path Manipulation, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-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 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
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
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.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
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>
49#include <sys/types.h>
50#include <pwd.h>
51
52#include <iprt/path.h>
53#include <iprt/env.h>
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
62#ifndef RT_OS_L4
63/**
64 * Worker for RTPathUserHome that looks up the home directory
65 * using the getpwuid_r api.
66 *
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.
71 */
72static int rtPathUserHomeByPasswd(char *pszPath, size_t cchPath, uid_t uid)
73{
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
77 * sysconf function to find the appropriate buffer size, but since it won't
78 * work everywhere we'll settle with a 5KB buffer and ASSUME that it'll
79 * suffice for even the lengthiest user descriptions...
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;
90
91 /*
92 * Check that it isn't empty and that it exists.
93 */
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 */
104 return rtPathFromNativeCopy(pszPath, cchPath, pPasswd->pw_dir, NULL);
105}
106#endif
107
108
109/**
110 * Worker for RTPathUserHome that looks up the home directory
111 * using the HOME environment variable.
112 *
113 * @returns IPRT status code.
114 * @param pszPath The path buffer.
115 * @param cchPath The size of the buffer.
116 */
117static int rtPathUserHomeByEnv(char *pszPath, size_t cchPath)
118{
119 /*
120 * Get HOME env. var it and validate it's existance.
121 */
122 int rc = VERR_PATH_NOT_FOUND;
123 const char *pszHome = RTEnvGet("HOME"); /** @todo Codeset confusion in RTEnv. */
124 if (pszHome)
125
126 {
127 struct stat st;
128 if ( !stat(pszHome, &st)
129 && S_ISDIR(st.st_mode))
130 rc = rtPathFromNativeCopy(pszPath, cchPath, pszHome, NULL);
131 }
132 return rc;
133}
134
135
136RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath)
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);
152
153 /*
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
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.

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