summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c47
1 files changed, 46 insertions, 1 deletions
diff --git a/misc.c b/misc.c
index 7adbcea1c..4bc07a42a 100644
--- a/misc.c
+++ b/misc.c
@@ -23,7 +23,7 @@
23 */ 23 */
24 24
25#include "includes.h" 25#include "includes.h"
26RCSID("$OpenBSD: misc.c,v 1.29 2005/03/10 22:01:05 deraadt Exp $"); 26RCSID("$OpenBSD: misc.c,v 1.30 2005/04/09 04:32:54 djm Exp $");
27 27
28#include "misc.h" 28#include "misc.h"
29#include "log.h" 29#include "log.h"
@@ -376,6 +376,51 @@ addargs(arglist *args, char *fmt, ...)
376} 376}
377 377
378/* 378/*
379 * Expands tildes in the file name. Returns data allocated by xmalloc.
380 * Warning: this calls getpw*.
381 */
382char *
383tilde_expand_filename(const char *filename, uid_t uid)
384{
385 const char *path;
386 char user[128], ret[MAXPATHLEN];
387 struct passwd *pw;
388 int len;
389
390 if (*filename != '~')
391 return (xstrdup(filename));
392 filename++;
393
394 path = strchr(filename, '/');
395 if (path != NULL && path > filename) { /* ~user/path */
396 if (path - filename > sizeof(user) - 1)
397 fatal("tilde_expand_filename: ~username too long");
398 memcpy(user, filename, path - filename);
399 user[path - filename] = '\0';
400 if ((pw = getpwnam(user)) == NULL)
401 fatal("tilde_expand_filename: No such user %s", user);
402 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
403 fatal("tilde_expand_filename: No such uid %d", uid);
404
405 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
406 fatal("tilde_expand_filename: Path too long");
407
408 /* Make sure directory has a trailing '/' */
409 len = strlen(pw->pw_dir);
410 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
411 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
412 fatal("tilde_expand_filename: Path too long");
413
414 /* Skip leading '/' from specified path */
415 if (path != NULL)
416 filename = path + 1;
417 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
418 fatal("tilde_expand_filename: Path too long");
419
420 return (xstrdup(ret));
421}
422
423/*
379 * Read an entire line from a public key file into a static buffer, discarding 424 * Read an entire line from a public key file into a static buffer, discarding
380 * lines that exceed the buffer size. Returns 0 on success, -1 on failure. 425 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
381 */ 426 */