summaryrefslogtreecommitdiff
path: root/hostfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'hostfile.c')
-rw-r--r--hostfile.c54
1 files changed, 53 insertions, 1 deletions
diff --git a/hostfile.c b/hostfile.c
index a4a355972..936d8c9be 100644
--- a/hostfile.c
+++ b/hostfile.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: hostfile.c,v 1.79 2020/03/06 18:25:12 markus Exp $ */ 1/* $OpenBSD: hostfile.c,v 1.82 2020/06/26 05:42:16 djm 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"
@@ -406,6 +407,18 @@ lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
406 found) == HOST_FOUND); 407 found) == HOST_FOUND);
407} 408}
408 409
410int
411lookup_marker_in_hostkeys(struct hostkeys *hostkeys, int want_marker)
412{
413 u_int i;
414
415 for (i = 0; i < hostkeys->num_entries; i++) {
416 if (hostkeys->entries[i].marker == (HostkeyMarker)want_marker)
417 return 1;
418 }
419 return 0;
420}
421
409static int 422static int
410write_host_entry(FILE *f, const char *host, const char *ip, 423write_host_entry(FILE *f, const char *host, const char *ip,
411 const struct sshkey *key, int store_hash) 424 const struct sshkey *key, int store_hash)
@@ -438,6 +451,44 @@ write_host_entry(FILE *f, const char *host, const char *ip,
438} 451}
439 452
440/* 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 goto out; /* not ~/.ssh prefixed */
470 if (stat(dotsshdir, &st) == 0)
471 goto out; /* dir already exists */
472 else if (errno != ENOENT)
473 error("Could not stat %s: %s", dotsshdir, strerror(errno));
474 else {
475#ifdef WITH_SELINUX
476 ssh_selinux_setfscreatecon(dotsshdir);
477#endif
478 if (mkdir(dotsshdir, 0700) == -1)
479 error("Could not create directory '%.200s' (%s).",
480 dotsshdir, strerror(errno));
481 else if (notify)
482 logit("Created directory '%s'.", dotsshdir);
483#ifdef WITH_SELINUX
484 ssh_selinux_setfscreatecon(NULL);
485#endif
486 }
487 out:
488 free(dotsshdir);
489}
490
491/*
441 * Appends an entry to the host file. Returns false if the entry could not 492 * Appends an entry to the host file. Returns false if the entry could not
442 * be appended. 493 * be appended.
443 */ 494 */
@@ -450,6 +501,7 @@ add_host_to_hostfile(const char *filename, const char *host,
450 501
451 if (key == NULL) 502 if (key == NULL)
452 return 1; /* XXX ? */ 503 return 1; /* XXX ? */
504 hostfile_create_user_ssh_dir(filename, 0);
453 f = fopen(filename, "a"); 505 f = fopen(filename, "a");
454 if (!f) 506 if (!f)
455 return 0; 507 return 0;