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 |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 11 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 12 | |
| 13 | #include "includes.h" |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 14 | RCSID("$OpenBSD: tildexpand.c,v 1.8 2000/09/07 20:27:55 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "xmalloc.h" |
| 17 | #include "ssh.h" |
| 18 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 19 | /* |
| 20 | * Expands tildes in the file name. Returns data allocated by xmalloc. |
| 21 | * Warning: this calls getpw*. |
| 22 | */ |
| 23 | char * |
| 24 | tilde_expand_filename(const char *filename, uid_t my_uid) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 26 | const char *cp; |
| 27 | unsigned int userlen; |
| 28 | char *expanded; |
| 29 | struct passwd *pw; |
| 30 | char user[100]; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 31 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 33 | /* Return immediately if no tilde. */ |
| 34 | if (filename[0] != '~') |
| 35 | return xstrdup(filename); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | /* Skip the tilde. */ |
| 38 | filename++; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | /* Find where the username ends. */ |
| 41 | cp = strchr(filename, '/'); |
| 42 | if (cp) |
| 43 | userlen = cp - filename; /* Something after username. */ |
| 44 | else |
| 45 | userlen = strlen(filename); /* Nothing after username. */ |
| 46 | if (userlen == 0) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 47 | pw = getpwuid(my_uid); /* Own home directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | else { |
| 49 | /* Tilde refers to someone elses home directory. */ |
| 50 | if (userlen > sizeof(user) - 1) |
| 51 | fatal("User name after tilde too long."); |
| 52 | memcpy(user, filename, userlen); |
| 53 | user[userlen] = 0; |
| 54 | pw = getpwnam(user); |
| 55 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 56 | if (!pw) |
| 57 | fatal("Unknown user %100s.", user); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 58 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | /* If referring to someones home directory, return it now. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 60 | if (!cp) { |
| 61 | /* Only home directory specified */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 62 | return xstrdup(pw->pw_dir); |
| 63 | } |
| 64 | /* Build a path combining the specified directory and path. */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 65 | len = strlen(pw->pw_dir) + strlen(cp + 1) + 2; |
| 66 | if (len > MAXPATHLEN) |
| 67 | fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1); |
| 68 | expanded = xmalloc(len); |
| 69 | snprintf(expanded, len, "%s/%s", pw->pw_dir, cp + 1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | return expanded; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | } |