1999-10-27 03:42:43 +00:00
|
|
|
/*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
2000-09-16 02:29:08 +00:00
|
|
|
*
|
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
1999-11-24 13:26:21 +00:00
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
2004-05-13 06:06:46 +00:00
|
|
|
RCSID("$OpenBSD: tildexpand.c,v 1.14 2004/05/08 00:01:37 deraadt Exp $");
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "xmalloc.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "log.h"
|
1999-10-27 03:42:43 +00:00
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
/*
|
|
|
|
* Expands tildes in the file name. Returns data allocated by xmalloc.
|
|
|
|
* Warning: this calls getpw*.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
tilde_expand_filename(const char *filename, uid_t my_uid)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
const char *cp;
|
2000-12-22 01:43:59 +00:00
|
|
|
u_int userlen;
|
1999-11-24 13:26:21 +00:00
|
|
|
char *expanded;
|
|
|
|
struct passwd *pw;
|
|
|
|
char user[100];
|
1999-12-07 04:38:31 +00:00
|
|
|
int len;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Return immediately if no tilde. */
|
|
|
|
if (filename[0] != '~')
|
|
|
|
return xstrdup(filename);
|
|
|
|
|
|
|
|
/* Skip the tilde. */
|
|
|
|
filename++;
|
|
|
|
|
|
|
|
/* Find where the username ends. */
|
|
|
|
cp = strchr(filename, '/');
|
|
|
|
if (cp)
|
|
|
|
userlen = cp - filename; /* Something after username. */
|
|
|
|
else
|
|
|
|
userlen = strlen(filename); /* Nothing after username. */
|
|
|
|
if (userlen == 0)
|
1999-11-25 00:54:57 +00:00
|
|
|
pw = getpwuid(my_uid); /* Own home directory. */
|
1999-11-24 13:26:21 +00:00
|
|
|
else {
|
|
|
|
/* Tilde refers to someone elses home directory. */
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* If referring to someones home directory, return it now. */
|
1999-11-25 00:54:57 +00:00
|
|
|
if (!cp) {
|
|
|
|
/* Only home directory specified */
|
1999-11-24 13:26:21 +00:00
|
|
|
return xstrdup(pw->pw_dir);
|
|
|
|
}
|
|
|
|
/* Build a path combining the specified directory and path. */
|
1999-12-07 04:38:31 +00:00
|
|
|
len = strlen(pw->pw_dir) + strlen(cp + 1) + 2;
|
|
|
|
if (len > MAXPATHLEN)
|
|
|
|
fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1);
|
|
|
|
expanded = xmalloc(len);
|
2002-06-23 21:20:34 +00:00
|
|
|
snprintf(expanded, len, "%s%s%s", pw->pw_dir,
|
|
|
|
strcmp(pw->pw_dir, "/") ? "/" : "", cp + 1);
|
1999-11-24 13:26:21 +00:00
|
|
|
return expanded;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|