xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/tildexpand.c (revision 9845:0d705da26956)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate  *                    All rights reserved
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
70Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
80Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
90Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
100Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
110Sstevel@tonic-gate  */
12*9845SJan.Pechanec@Sun.COM /*
13*9845SJan.Pechanec@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
14*9845SJan.Pechanec@Sun.COM  * Use is subject to license terms.
15*9845SJan.Pechanec@Sun.COM  */
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #include "includes.h"
180Sstevel@tonic-gate RCSID("$OpenBSD: tildexpand.c,v 1.13 2002/06/23 03:25:50 deraadt Exp $");
190Sstevel@tonic-gate 
20*9845SJan.Pechanec@Sun.COM #include <libgen.h>
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #include "xmalloc.h"
230Sstevel@tonic-gate #include "log.h"
240Sstevel@tonic-gate #include "tildexpand.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Expands tildes in the file name.  Returns data allocated by xmalloc.
280Sstevel@tonic-gate  * Warning: this calls getpw*.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate char *
tilde_expand_filename(const char * filename,uid_t my_uid)310Sstevel@tonic-gate tilde_expand_filename(const char *filename, uid_t my_uid)
320Sstevel@tonic-gate {
330Sstevel@tonic-gate 	const char *cp;
34*9845SJan.Pechanec@Sun.COM 	uint_t userlen;
350Sstevel@tonic-gate 	char *expanded;
360Sstevel@tonic-gate 	struct passwd *pw;
37*9845SJan.Pechanec@Sun.COM 	char *pw_dir;
380Sstevel@tonic-gate 	char user[100];
390Sstevel@tonic-gate 	int len;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	/* Return immediately if no tilde. */
420Sstevel@tonic-gate 	if (filename[0] != '~')
43*9845SJan.Pechanec@Sun.COM 		return (xstrdup(filename));
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	/* Skip the tilde. */
460Sstevel@tonic-gate 	filename++;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	/* Find where the username ends. */
490Sstevel@tonic-gate 	cp = strchr(filename, '/');
500Sstevel@tonic-gate 	if (cp)
510Sstevel@tonic-gate 		userlen = cp - filename;	/* Something after username. */
520Sstevel@tonic-gate 	else
530Sstevel@tonic-gate 		userlen = strlen(filename);	/* Nothing after username. */
54*9845SJan.Pechanec@Sun.COM 
55*9845SJan.Pechanec@Sun.COM 	/* This is the ~/xyz case with no ~username specification. */
560Sstevel@tonic-gate 	if (userlen == 0)
57*9845SJan.Pechanec@Sun.COM 		pw = getpwuid(my_uid);
580Sstevel@tonic-gate 	else {
590Sstevel@tonic-gate 		/* Tilde refers to someone elses home directory. */
60*9845SJan.Pechanec@Sun.COM 		if (userlen > sizeof (user) - 1)
610Sstevel@tonic-gate 			fatal("User name after tilde too long.");
620Sstevel@tonic-gate 		memcpy(user, filename, userlen);
630Sstevel@tonic-gate 		user[userlen] = 0;
640Sstevel@tonic-gate 		pw = getpwnam(user);
650Sstevel@tonic-gate 	}
66*9845SJan.Pechanec@Sun.COM 
67*9845SJan.Pechanec@Sun.COM 	/* Use the HOME variable now. */
68*9845SJan.Pechanec@Sun.COM 	if (pw == NULL) {
69*9845SJan.Pechanec@Sun.COM 		debug("User account's password entry not found, trying to use "
70*9845SJan.Pechanec@Sun.COM 		    "the HOME variable.");
71*9845SJan.Pechanec@Sun.COM 		if ((pw_dir = getenv("HOME")) == NULL) {
72*9845SJan.Pechanec@Sun.COM 			fatal("User account's password entry not found and "
73*9845SJan.Pechanec@Sun.COM 			    "the HOME variable not set.");
74*9845SJan.Pechanec@Sun.COM 		}
75*9845SJan.Pechanec@Sun.COM 	} else {
76*9845SJan.Pechanec@Sun.COM 		pw_dir = pw->pw_dir;
77*9845SJan.Pechanec@Sun.COM 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/* If referring to someones home directory, return it now. */
80*9845SJan.Pechanec@Sun.COM 	if (cp == NULL) {
810Sstevel@tonic-gate 		/* Only home directory specified */
82*9845SJan.Pechanec@Sun.COM 		return (xstrdup(pw_dir));
830Sstevel@tonic-gate 	}
84*9845SJan.Pechanec@Sun.COM 
850Sstevel@tonic-gate 	/* Build a path combining the specified directory and path. */
86*9845SJan.Pechanec@Sun.COM 	len = strlen(pw_dir) + strlen(cp + 1) + 2;
870Sstevel@tonic-gate 	if (len > MAXPATHLEN)
88*9845SJan.Pechanec@Sun.COM 		fatal("Home directory too long (%d > %d)", len - 1,
89*9845SJan.Pechanec@Sun.COM 		    MAXPATHLEN - 1);
90*9845SJan.Pechanec@Sun.COM 
910Sstevel@tonic-gate 	expanded = xmalloc(len);
92*9845SJan.Pechanec@Sun.COM 	snprintf(expanded, len, "%s%s%s", pw_dir,
93*9845SJan.Pechanec@Sun.COM 	    strcmp(pw_dir, "/") ? "/" : "", cp + 1);
94*9845SJan.Pechanec@Sun.COM 	return (expanded);
950Sstevel@tonic-gate }
96