blob: 46bdaae87bab69415db21bd0326ee519e4cafc9b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11005 *
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 Miller95def091999-11-25 00:26:21 +110011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000014RCSID("$OpenBSD: tildexpand.c,v 1.11 2001/02/08 19:30:53 itojun Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000018#include "tildexpand.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Damien Miller95def091999-11-25 00:26:21 +110020/*
21 * Expands tildes in the file name. Returns data allocated by xmalloc.
22 * Warning: this calls getpw*.
23 */
24char *
25tilde_expand_filename(const char *filename, uid_t my_uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026{
Damien Miller95def091999-11-25 00:26:21 +110027 const char *cp;
Ben Lindstrom46c16222000-12-22 01:43:59 +000028 u_int userlen;
Damien Miller95def091999-11-25 00:26:21 +110029 char *expanded;
30 struct passwd *pw;
31 char user[100];
Damien Miller037a0dc1999-12-07 15:38:31 +110032 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller95def091999-11-25 00:26:21 +110034 /* Return immediately if no tilde. */
35 if (filename[0] != '~')
36 return xstrdup(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Damien Miller95def091999-11-25 00:26:21 +110038 /* Skip the tilde. */
39 filename++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Damien Miller95def091999-11-25 00:26:21 +110041 /* Find where the username ends. */
42 cp = strchr(filename, '/');
43 if (cp)
44 userlen = cp - filename; /* Something after username. */
45 else
46 userlen = strlen(filename); /* Nothing after username. */
47 if (userlen == 0)
Damien Miller5428f641999-11-25 11:54:57 +110048 pw = getpwuid(my_uid); /* Own home directory. */
Damien Miller95def091999-11-25 00:26:21 +110049 else {
50 /* Tilde refers to someone elses home directory. */
51 if (userlen > sizeof(user) - 1)
52 fatal("User name after tilde too long.");
53 memcpy(user, filename, userlen);
54 user[userlen] = 0;
55 pw = getpwnam(user);
56 }
Damien Miller95def091999-11-25 00:26:21 +110057 if (!pw)
58 fatal("Unknown user %100s.", user);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller95def091999-11-25 00:26:21 +110060 /* If referring to someones home directory, return it now. */
Damien Miller5428f641999-11-25 11:54:57 +110061 if (!cp) {
62 /* Only home directory specified */
Damien Miller95def091999-11-25 00:26:21 +110063 return xstrdup(pw->pw_dir);
64 }
65 /* Build a path combining the specified directory and path. */
Damien Miller037a0dc1999-12-07 15:38:31 +110066 len = strlen(pw->pw_dir) + strlen(cp + 1) + 2;
67 if (len > MAXPATHLEN)
68 fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1);
69 expanded = xmalloc(len);
70 snprintf(expanded, len, "%s/%s", pw->pw_dir, cp + 1);
Damien Miller95def091999-11-25 00:26:21 +110071 return expanded;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072}