diff options
author | Damien Miller <djm@cvs.openbsd.org> | 2005-04-09 04:32:55 +0000 |
---|---|---|
committer | Damien Miller <djm@cvs.openbsd.org> | 2005-04-09 04:32:55 +0000 |
commit | 5bded88db3d9a585bb2660ac2e5737474d4d2766 (patch) | |
tree | 6cebd70ed87728e4c446df198cf3ab7ce8d3904e /usr.bin | |
parent | d4590880f41d0fd0ffe4a4c49bca02d57c56cc7e (diff) |
replace tilde_expand_filename with a simpler implementation, ahead of more
whacking; ok deraadt@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/ssh/lib/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/ssh/misc.c | 47 | ||||
-rw-r--r-- | usr.bin/ssh/misc.h | 7 | ||||
-rw-r--r-- | usr.bin/ssh/tildexpand.c | 73 |
4 files changed, 50 insertions, 81 deletions
diff --git a/usr.bin/ssh/lib/Makefile b/usr.bin/ssh/lib/Makefile index 7b4eace82be..78cbb0495dd 100644 --- a/usr.bin/ssh/lib/Makefile +++ b/usr.bin/ssh/lib/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.50 2004/12/22 02:13:19 djm Exp $ +# $OpenBSD: Makefile,v 1.51 2005/04/09 04:32:54 djm Exp $ .PATH: ${.CURDIR}/.. @@ -7,7 +7,7 @@ SRCS= authfd.c authfile.c bufaux.c buffer.c canohost.c channels.c \ cipher.c cipher-3des1.c cipher-bf1.c cipher-ctr.c \ cleanup.c compat.c compress.c crc32.c deattack.c fatal.c \ hostfile.c log.c match.c nchan.c packet.c readpass.c \ - rsa.c tildexpand.c ttymodes.c xmalloc.c atomicio.c \ + rsa.c ttymodes.c xmalloc.c atomicio.c \ key.c dispatch.c kex.c mac.c uidswap.c uuencode.c misc.c \ ssh-dss.c ssh-rsa.c dh.c kexdh.c kexgex.c \ kexdhc.c kexgexc.c scard.c msg.c progressmeter.c dns.c \ diff --git a/usr.bin/ssh/misc.c b/usr.bin/ssh/misc.c index 3d3af188288..7372ee13bd9 100644 --- a/usr.bin/ssh/misc.c +++ b/usr.bin/ssh/misc.c @@ -23,7 +23,7 @@ */ #include "includes.h" -RCSID("$OpenBSD: misc.c,v 1.29 2005/03/10 22:01:05 deraadt Exp $"); +RCSID("$OpenBSD: misc.c,v 1.30 2005/04/09 04:32:54 djm Exp $"); #include "misc.h" #include "log.h" @@ -370,6 +370,51 @@ addargs(arglist *args, char *fmt, ...) } /* + * Expands tildes in the file name. Returns data allocated by xmalloc. + * Warning: this calls getpw*. + */ +char * +tilde_expand_filename(const char *filename, uid_t uid) +{ + const char *path; + char user[128], ret[MAXPATHLEN]; + struct passwd *pw; + int len; + + if (*filename != '~') + return (xstrdup(filename)); + filename++; + + path = strchr(filename, '/'); + if (path != NULL && path > filename) { /* ~user/path */ + if (path - filename > sizeof(user) - 1) + fatal("tilde_expand_filename: ~username too long"); + memcpy(user, filename, path - filename); + user[path - filename] = '\0'; + if ((pw = getpwnam(user)) == NULL) + fatal("tilde_expand_filename: No such user %s", user); + } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ + fatal("tilde_expand_filename: No such uid %d", uid); + + if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret)) + fatal("tilde_expand_filename: Path too long"); + + /* Make sure directory has a trailing '/' */ + len = strlen(pw->pw_dir); + if ((len == 0 || pw->pw_dir[len - 1] != '/') && + strlcat(ret, "/", sizeof(ret)) >= sizeof(ret)) + fatal("tilde_expand_filename: Path too long"); + + /* Skip leading '/' from specified path */ + if (path != NULL) + filename = path + 1; + if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret)) + fatal("tilde_expand_filename: Path too long"); + + return (xstrdup(ret)); +} + +/* * Read an entire line from a public key file into a static buffer, discarding * lines that exceed the buffer size. Returns 0 on success, -1 on failure. */ diff --git a/usr.bin/ssh/misc.h b/usr.bin/ssh/misc.h index 8bbc87f0dbd..798d80fbf3f 100644 --- a/usr.bin/ssh/misc.h +++ b/usr.bin/ssh/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.21 2005/03/01 10:09:52 djm Exp $ */ +/* $OpenBSD: misc.h,v 1.22 2005/04/09 04:32:54 djm Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> @@ -24,6 +24,7 @@ char *hpdelim(char **); char *cleanhostname(char *); char *colon(char *); long convtime(const char *); +char *tilde_expand_filename(const char *, uid_t); struct passwd *pwcopy(struct passwd *); @@ -35,10 +36,6 @@ struct arglist { }; void addargs(arglist *, char *, ...) __attribute__((format(printf, 2, 3))); -/* tildexpand.c */ - -char *tilde_expand_filename(const char *, uid_t); - /* readpass.c */ #define RP_ECHO 0x0001 diff --git a/usr.bin/ssh/tildexpand.c b/usr.bin/ssh/tildexpand.c deleted file mode 100644 index cedb653b244..00000000000 --- a/usr.bin/ssh/tildexpand.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Author: Tatu Ylonen <ylo@cs.hut.fi> - * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland - * All rights reserved - * - * 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". - */ - -#include "includes.h" -RCSID("$OpenBSD: tildexpand.c,v 1.15 2004/05/21 08:43:03 markus Exp $"); - -#include "xmalloc.h" -#include "log.h" -#include "misc.h" - -/* - * 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) -{ - const char *cp; - u_int userlen; - char *expanded; - struct passwd *pw; - char user[100]; - int len; - - /* 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) - pw = getpwuid(my_uid); /* Own home directory. */ - 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. */ - if (!cp) { - /* Only home directory specified */ - return xstrdup(pw->pw_dir); - } - /* Build a path combining the specified directory and path. */ - 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); - snprintf(expanded, len, "%s%s%s", pw->pw_dir, - strcmp(pw->pw_dir, "/") ? "/" : "", cp + 1); - return expanded; -} |