os.path.expanduser ignores os.seteuid
Sean 'Shaleh' Perry
shalehperry at attbi.com
Tue Jun 4 14:38:33 EDT 2002
On 04-Jun-2002 Graham Guttocks wrote:
> Why doesn't os.path.expanduser pay attention to the fact that I've
> changed the process's effective user id?
>
> As root:
>
> Python 2.1.3 (#1, Apr 11 2002, 18:30:30)
> [GCC 2.95.3 20010315 (release) [FreeBSD]] on freebsd4
> Type "copyright", "credits" or "license" for more information.
>>>> import os
>>>> os.seteuid(667)
>>>> os.path.expanduser("~")
> '/root'
>>>>
>
> Other functions like os.getlogin() don't have this problem.
>
def expanduser(path):
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
do nothing."""
if path[:1] != '~':
return path
i, n = 1, len(path)
while i < n and path[i] != '/':
i = i + 1
if i == 1:
if not os.environ.has_key('HOME'):
return path
userhome = os.environ['HOME']
else:
import pwd
try:
pwent = pwd.getpwnam(path[1:i])
except KeyError:
return path
userhome = pwent[5]
if userhome[-1:] == '/': i = i + 1
return userhome + path[i:]
is from python 2.2. It uses short circuit eval for the '~' case and simply
reads the environment $HOME. If you could pass '~user/' it would give you the
answer you want.
More information about the Python-list
mailing list