blob: f615362f9393e6c5ea15bd408992d42fbea172f8 [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
5 * Created: Wed Jul 12 01:07:36 1995 ylo
6 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10007
8#include "includes.h"
Damien Miller037a0dc1999-12-07 15:38:31 +11009RCSID("$Id: tildexpand.c,v 1.4 1999/12/07 04:38:32 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100010
11#include "xmalloc.h"
12#include "ssh.h"
13
Damien Miller95def091999-11-25 00:26:21 +110014/*
15 * Expands tildes in the file name. Returns data allocated by xmalloc.
16 * Warning: this calls getpw*.
17 */
18char *
19tilde_expand_filename(const char *filename, uid_t my_uid)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020{
Damien Miller95def091999-11-25 00:26:21 +110021 const char *cp;
22 unsigned int userlen;
23 char *expanded;
24 struct passwd *pw;
25 char user[100];
Damien Miller037a0dc1999-12-07 15:38:31 +110026 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller95def091999-11-25 00:26:21 +110028 /* Return immediately if no tilde. */
29 if (filename[0] != '~')
30 return xstrdup(filename);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031
Damien Miller95def091999-11-25 00:26:21 +110032 /* Skip the tilde. */
33 filename++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
Damien Miller95def091999-11-25 00:26:21 +110035 /* Find where the username ends. */
36 cp = strchr(filename, '/');
37 if (cp)
38 userlen = cp - filename; /* Something after username. */
39 else
40 userlen = strlen(filename); /* Nothing after username. */
41 if (userlen == 0)
Damien Miller5428f641999-11-25 11:54:57 +110042 pw = getpwuid(my_uid); /* Own home directory. */
Damien Miller95def091999-11-25 00:26:21 +110043 else {
44 /* Tilde refers to someone elses home directory. */
45 if (userlen > sizeof(user) - 1)
46 fatal("User name after tilde too long.");
47 memcpy(user, filename, userlen);
48 user[userlen] = 0;
49 pw = getpwnam(user);
50 }
Damien Miller95def091999-11-25 00:26:21 +110051 if (!pw)
52 fatal("Unknown user %100s.", user);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller95def091999-11-25 00:26:21 +110054 /* If referring to someones home directory, return it now. */
Damien Miller5428f641999-11-25 11:54:57 +110055 if (!cp) {
56 /* Only home directory specified */
Damien Miller95def091999-11-25 00:26:21 +110057 return xstrdup(pw->pw_dir);
58 }
59 /* Build a path combining the specified directory and path. */
Damien Miller037a0dc1999-12-07 15:38:31 +110060 len = strlen(pw->pw_dir) + strlen(cp + 1) + 2;
61 if (len > MAXPATHLEN)
62 fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1);
63 expanded = xmalloc(len);
64 snprintf(expanded, len, "%s/%s", pw->pw_dir, cp + 1);
Damien Miller95def091999-11-25 00:26:21 +110065 return expanded;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066}