summaryrefslogtreecommitdiff
path: root/hostfile.c
diff options
context:
space:
mode:
authordtucker@openbsd.org <dtucker@openbsd.org>2020-06-26 05:02:03 +0000
committerDamien Miller <djm@mindrot.org>2020-06-26 15:24:27 +1000
commit74344c3ca42c3f53b00b025daf09ae7f6aa38076 (patch)
tree4952081cdbd6c6f3e6e891cd09a7688f003ea639 /hostfile.c
parentc9e24daac6324fcbdba171392c325bf9ccc3c768 (diff)
upstream: Defer creation of ~/.ssh by ssh(1) until we attempt to
write to it so we don't leave an empty .ssh directory when it's not needed. Use the same function to replace the code in ssh-keygen that does the same thing. bz#3156, ok djm@ OpenBSD-Commit-ID: 59c073b569be1a60f4de36f491a4339bc4ae870f
Diffstat (limited to 'hostfile.c')
-rw-r--r--hostfile.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/hostfile.c b/hostfile.c
index a91dbbd94..4b39def04 100644
--- a/hostfile.c
+++ b/hostfile.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: hostfile.c,v 1.80 2020/05/13 09:52:41 djm Exp $ */ 1/* $OpenBSD: hostfile.c,v 1.81 2020/06/26 05:02:03 dtucker Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -57,6 +57,7 @@
57#include "hostfile.h" 57#include "hostfile.h"
58#include "log.h" 58#include "log.h"
59#include "misc.h" 59#include "misc.h"
60#include "pathnames.h"
60#include "ssherr.h" 61#include "ssherr.h"
61#include "digest.h" 62#include "digest.h"
62#include "hmac.h" 63#include "hmac.h"
@@ -450,6 +451,38 @@ write_host_entry(FILE *f, const char *host, const char *ip,
450} 451}
451 452
452/* 453/*
454 * Create user ~/.ssh directory if it doesn't exist and we want to write to it.
455 * If notify is set, a message will be emitted if the directory is created.
456 */
457void
458hostfile_create_user_ssh_dir(const char *filename, int notify)
459{
460 char *dotsshdir = NULL, *p;
461 size_t len;
462 struct stat st;
463
464 if ((p = strrchr(filename, '/')) == NULL)
465 return;
466 len = p - filename;
467 dotsshdir = tilde_expand_filename("~/" _PATH_SSH_USER_DIR, getuid());
468 if ((strlen(dotsshdir) > len || strncmp(filename, dotsshdir, len) != 0
469 || stat(dotsshdir, &st)) == 0)
470 ; /* do nothing, path not in ~/.ssh or dir already exists */
471 else if (errno != ENOENT)
472 error("Could not stat %s: %s", dotsshdir, strerror(errno));
473 else {
474 ssh_selinux_setfscreatecon(dotsshdir);
475 if (mkdir(dotsshdir, 0700) == -1)
476 error("Could not create directory '%.200s' (%s).",
477 dotsshdir, strerror(errno));
478 else if (notify)
479 logit("Created directory '%s'.", dotsshdir);
480 ssh_selinux_setfscreatecon(NULL);
481 }
482 free(dotsshdir);
483}
484
485/*
453 * Appends an entry to the host file. Returns false if the entry could not 486 * Appends an entry to the host file. Returns false if the entry could not
454 * be appended. 487 * be appended.
455 */ 488 */
@@ -462,6 +495,7 @@ add_host_to_hostfile(const char *filename, const char *host,
462 495
463 if (key == NULL) 496 if (key == NULL)
464 return 1; /* XXX ? */ 497 return 1; /* XXX ? */
498 hostfile_create_user_ssh_dir(filename, 0);
465 f = fopen(filename, "a"); 499 f = fopen(filename, "a");
466 if (!f) 500 if (!f)
467 return 0; 501 return 0;