Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
| 5 | * Created: Wed Jul 12 01:07:36 1995 ylo |
| 6 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 7 | |
| 8 | #include "includes.h" |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 9 | RCSID("$Id: tildexpand.c,v 1.3 1999/11/25 00:54:59 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 10 | |
| 11 | #include "xmalloc.h" |
| 12 | #include "ssh.h" |
| 13 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | /* |
| 15 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 16 | * Warning: this calls getpw*. |
| 17 | */ |
| 18 | char * |
| 19 | tilde_expand_filename(const char *filename, uid_t my_uid) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 21 | const char *cp; |
| 22 | unsigned int userlen; |
| 23 | char *expanded; |
| 24 | struct passwd *pw; |
| 25 | char user[100]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 27 | /* Return immediately if no tilde. */ |
| 28 | if (filename[0] != '~') |
| 29 | return xstrdup(filename); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 31 | /* Skip the tilde. */ |
| 32 | filename++; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 34 | /* Find where the username ends. */ |
| 35 | cp = strchr(filename, '/'); |
| 36 | if (cp) |
| 37 | userlen = cp - filename; /* Something after username. */ |
| 38 | else |
| 39 | userlen = strlen(filename); /* Nothing after username. */ |
| 40 | if (userlen == 0) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | pw = getpwuid(my_uid); /* Own home directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 42 | else { |
| 43 | /* Tilde refers to someone elses home directory. */ |
| 44 | if (userlen > sizeof(user) - 1) |
| 45 | fatal("User name after tilde too long."); |
| 46 | memcpy(user, filename, userlen); |
| 47 | user[userlen] = 0; |
| 48 | pw = getpwnam(user); |
| 49 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 50 | if (!pw) |
| 51 | fatal("Unknown user %100s.", user); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | /* If referring to someones home directory, return it now. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 54 | if (!cp) { |
| 55 | /* Only home directory specified */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 56 | return xstrdup(pw->pw_dir); |
| 57 | } |
| 58 | /* Build a path combining the specified directory and path. */ |
| 59 | expanded = xmalloc(strlen(pw->pw_dir) + strlen(cp + 1) + 2); |
| 60 | sprintf(expanded, "%s/%s", pw->pw_dir, cp + 1); |
| 61 | return expanded; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | } |