diff options
| author | Ed Maste <emaste@FreeBSD.org> | 2023-03-16 12:41:22 +0000 |
|---|---|---|
| committer | Ed Maste <emaste@FreeBSD.org> | 2023-03-16 12:41:22 +0000 |
| commit | 36b354d74be5adbb73d7dcd2ceb49b5105145d02 (patch) | |
| tree | ee4155f948be143506b5d1cb86d6485fb799e768 /hostfile.c | |
| parent | 7ee81174ca89a92a4c1686170d433dae500ae144 (diff) | |
Diffstat (limited to 'hostfile.c')
| -rw-r--r-- | hostfile.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/hostfile.c b/hostfile.c index bd49e3ac7c48..c5669c703735 100644 --- a/hostfile.c +++ b/hostfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hostfile.c,v 1.93 2022/01/06 22:02:52 djm Exp $ */ +/* $OpenBSD: hostfile.c,v 1.95 2023/02/21 06:48:18 dtucker Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -515,14 +515,23 @@ add_host_to_hostfile(const char *filename, const char *host, const struct sshkey *key, int store_hash) { FILE *f; - int success; + int success, addnl = 0; if (key == NULL) return 1; /* XXX ? */ hostfile_create_user_ssh_dir(filename, 0); - f = fopen(filename, "a"); + f = fopen(filename, "a+"); if (!f) return 0; + /* Make sure we have a terminating newline. */ + if (fseek(f, -1L, SEEK_END) == 0 && fgetc(f) != '\n') + addnl = 1; + if (fseek(f, 0L, SEEK_END) != 0 || (addnl && fputc('\n', f) != '\n')) { + error("Failed to add terminating newline to %s: %s", + filename, strerror(errno)); + fclose(f); + return 0; + } success = write_host_entry(f, host, NULL, key, store_hash); fclose(f); return success; |
