diff options
Diffstat (limited to 'usr/src/cmd/ssh/libssh/common/tildexpand.c')
-rw-r--r-- | usr/src/cmd/ssh/libssh/common/tildexpand.c | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/usr/src/cmd/ssh/libssh/common/tildexpand.c b/usr/src/cmd/ssh/libssh/common/tildexpand.c index 6d3850a9ea..5fcd07ebe6 100644 --- a/usr/src/cmd/ssh/libssh/common/tildexpand.c +++ b/usr/src/cmd/ssh/libssh/common/tildexpand.c @@ -9,11 +9,15 @@ * incompatible with the protocol description in the RFC file, it must be * called by a name other than "ssh" or "Secure Shell". */ +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ #include "includes.h" RCSID("$OpenBSD: tildexpand.c,v 1.13 2002/06/23 03:25:50 deraadt Exp $"); -#pragma ident "%Z%%M% %I% %E% SMI" +#include <libgen.h> #include "xmalloc.h" #include "log.h" @@ -27,15 +31,16 @@ char * tilde_expand_filename(const char *filename, uid_t my_uid) { const char *cp; - u_int userlen; + uint_t userlen; char *expanded; struct passwd *pw; + char *pw_dir; char user[100]; int len; /* Return immediately if no tilde. */ if (filename[0] != '~') - return xstrdup(filename); + return (xstrdup(filename)); /* Skip the tilde. */ filename++; @@ -46,30 +51,45 @@ tilde_expand_filename(const char *filename, uid_t my_uid) userlen = cp - filename; /* Something after username. */ else userlen = strlen(filename); /* Nothing after username. */ + + /* This is the ~/xyz case with no ~username specification. */ if (userlen == 0) - pw = getpwuid(my_uid); /* Own home directory. */ + pw = getpwuid(my_uid); else { /* Tilde refers to someone elses home directory. */ - if (userlen > sizeof(user) - 1) + if (userlen > sizeof (user) - 1) fatal("User name after tilde too long."); memcpy(user, filename, userlen); user[userlen] = 0; pw = getpwnam(user); } - if (!pw) - fatal("Unknown user %100s.", user); + + /* Use the HOME variable now. */ + if (pw == NULL) { + debug("User account's password entry not found, trying to use " + "the HOME variable."); + if ((pw_dir = getenv("HOME")) == NULL) { + fatal("User account's password entry not found and " + "the HOME variable not set."); + } + } else { + pw_dir = pw->pw_dir; + } /* If referring to someones home directory, return it now. */ - if (!cp) { + if (cp == NULL) { /* Only home directory specified */ - return xstrdup(pw->pw_dir); + return (xstrdup(pw_dir)); } + /* Build a path combining the specified directory and path. */ - len = strlen(pw->pw_dir) + strlen(cp + 1) + 2; + len = strlen(pw_dir) + strlen(cp + 1) + 2; if (len > MAXPATHLEN) - fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1); + fatal("Home directory too long (%d > %d)", len - 1, + MAXPATHLEN - 1); + expanded = xmalloc(len); - snprintf(expanded, len, "%s%s%s", pw->pw_dir, - strcmp(pw->pw_dir, "/") ? "/" : "", cp + 1); - return expanded; + snprintf(expanded, len, "%s%s%s", pw_dir, + strcmp(pw_dir, "/") ? "/" : "", cp + 1); + return (expanded); } |