diff options
| author | SkyperTHC | 2026-03-03 06:28:55 +0000 |
|---|---|---|
| committer | SkyperTHC | 2026-03-03 06:28:55 +0000 |
| commit | 5d3573ef7a109ee70416fe94db098fe6a769a798 (patch) | |
| tree | dc2d5b294c9db8ab2db7433511f94e1c4bb8b698 /other/openssh-2.1.1p4/hostfile.c | |
| parent | c6c59dc73cc4586357f93ab38ecf459e98675cc5 (diff) | |
packetstorm sync
Diffstat (limited to 'other/openssh-2.1.1p4/hostfile.c')
| -rw-r--r-- | other/openssh-2.1.1p4/hostfile.c | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/other/openssh-2.1.1p4/hostfile.c b/other/openssh-2.1.1p4/hostfile.c new file mode 100644 index 0000000..f58e1d6 --- /dev/null +++ b/other/openssh-2.1.1p4/hostfile.c | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | /* | ||
| 2 | * | ||
| 3 | * hostfile.c | ||
| 4 | * | ||
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | ||
| 6 | * | ||
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | ||
| 8 | * All rights reserved | ||
| 9 | * | ||
| 10 | * Created: Thu Jun 29 07:10:56 1995 ylo | ||
| 11 | * | ||
| 12 | * Functions for manipulating the known hosts files. | ||
| 13 | * | ||
| 14 | */ | ||
| 15 | |||
| 16 | #include "includes.h" | ||
| 17 | RCSID("$OpenBSD: hostfile.c,v 1.19 2000/06/06 19:32:13 markus Exp $"); | ||
| 18 | |||
| 19 | #include "packet.h" | ||
| 20 | #include "match.h" | ||
| 21 | #include "ssh.h" | ||
| 22 | #include <openssl/rsa.h> | ||
| 23 | #include <openssl/dsa.h> | ||
| 24 | #include "key.h" | ||
| 25 | #include "hostfile.h" | ||
| 26 | |||
| 27 | /* | ||
| 28 | * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the | ||
| 29 | * pointer over the key. Skips any whitespace at the beginning and at end. | ||
| 30 | */ | ||
| 31 | |||
| 32 | int | ||
| 33 | hostfile_read_key(char **cpp, unsigned int *bitsp, Key *ret) | ||
| 34 | { | ||
| 35 | unsigned int bits; | ||
| 36 | char *cp; | ||
| 37 | |||
| 38 | /* Skip leading whitespace. */ | ||
| 39 | for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) | ||
| 40 | ; | ||
| 41 | |||
| 42 | bits = key_read(ret, &cp); | ||
| 43 | if (bits == 0) | ||
| 44 | return 0; | ||
| 45 | |||
| 46 | /* Skip trailing whitespace. */ | ||
| 47 | for (; *cp == ' ' || *cp == '\t'; cp++) | ||
| 48 | ; | ||
| 49 | |||
| 50 | /* Return results. */ | ||
| 51 | *cpp = cp; | ||
| 52 | *bitsp = bits; | ||
| 53 | return 1; | ||
| 54 | } | ||
| 55 | |||
| 56 | int | ||
| 57 | auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n) | ||
| 58 | { | ||
| 59 | Key *k = key_new(KEY_RSA); | ||
| 60 | int ret = hostfile_read_key(cpp, bitsp, k); | ||
| 61 | BN_copy(e, k->rsa->e); | ||
| 62 | BN_copy(n, k->rsa->n); | ||
| 63 | key_free(k); | ||
| 64 | return ret; | ||
| 65 | } | ||
| 66 | |||
| 67 | int | ||
| 68 | hostfile_check_key(int bits, Key *key, const char *host, const char *filename, int linenum) | ||
| 69 | { | ||
| 70 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) | ||
| 71 | return 1; | ||
| 72 | if (bits != BN_num_bits(key->rsa->n)) { | ||
| 73 | log("Warning: %s, line %d: keysize mismatch for host %s: " | ||
| 74 | "actual %d vs. announced %d.", | ||
| 75 | filename, linenum, host, BN_num_bits(key->rsa->n), bits); | ||
| 76 | log("Warning: replace %d with %d in %s, line %d.", | ||
| 77 | bits, BN_num_bits(key->rsa->n), filename, linenum); | ||
| 78 | } | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | /* | ||
| 83 | * Checks whether the given host (which must be in all lowercase) is already | ||
| 84 | * in the list of our known hosts. Returns HOST_OK if the host is known and | ||
| 85 | * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED | ||
| 86 | * if the host is known but used to have a different host key. | ||
| 87 | */ | ||
| 88 | |||
| 89 | HostStatus | ||
| 90 | check_host_in_hostfile(const char *filename, const char *host, Key *key, Key *found) | ||
| 91 | { | ||
| 92 | FILE *f; | ||
| 93 | char line[8192]; | ||
| 94 | int linenum = 0; | ||
| 95 | unsigned int kbits, hostlen; | ||
| 96 | char *cp, *cp2; | ||
| 97 | HostStatus end_return; | ||
| 98 | |||
| 99 | if (key == NULL) | ||
| 100 | fatal("no key to look up"); | ||
| 101 | /* Open the file containing the list of known hosts. */ | ||
| 102 | f = fopen(filename, "r"); | ||
| 103 | if (!f) | ||
| 104 | return HOST_NEW; | ||
| 105 | |||
| 106 | /* Cache the length of the host name. */ | ||
| 107 | hostlen = strlen(host); | ||
| 108 | |||
| 109 | /* | ||
| 110 | * Return value when the loop terminates. This is set to | ||
| 111 | * HOST_CHANGED if we have seen a different key for the host and have | ||
| 112 | * not found the proper one. | ||
| 113 | */ | ||
| 114 | end_return = HOST_NEW; | ||
| 115 | |||
| 116 | /* Go trough the file. */ | ||
| 117 | while (fgets(line, sizeof(line), f)) { | ||
| 118 | cp = line; | ||
| 119 | linenum++; | ||
| 120 | |||
| 121 | /* Skip any leading whitespace, comments and empty lines. */ | ||
| 122 | for (; *cp == ' ' || *cp == '\t'; cp++) | ||
| 123 | ; | ||
| 124 | if (!*cp || *cp == '#' || *cp == '\n') | ||
| 125 | continue; | ||
| 126 | |||
| 127 | /* Find the end of the host name portion. */ | ||
| 128 | for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) | ||
| 129 | ; | ||
| 130 | |||
| 131 | /* Check if the host name matches. */ | ||
| 132 | if (match_hostname(host, cp, (unsigned int) (cp2 - cp)) != 1) | ||
| 133 | continue; | ||
| 134 | |||
| 135 | /* Got a match. Skip host name. */ | ||
| 136 | cp = cp2; | ||
| 137 | |||
| 138 | /* | ||
| 139 | * Extract the key from the line. This will skip any leading | ||
| 140 | * whitespace. Ignore badly formatted lines. | ||
| 141 | */ | ||
| 142 | if (!hostfile_read_key(&cp, &kbits, found)) | ||
| 143 | continue; | ||
| 144 | if (!hostfile_check_key(kbits, found, host, filename, linenum)) | ||
| 145 | continue; | ||
| 146 | |||
| 147 | /* Check if the current key is the same as the given key. */ | ||
| 148 | if (key_equal(key, found)) { | ||
| 149 | /* Ok, they match. */ | ||
| 150 | fclose(f); | ||
| 151 | return HOST_OK; | ||
| 152 | } | ||
| 153 | /* | ||
| 154 | * They do not match. We will continue to go through the | ||
| 155 | * file; however, we note that we will not return that it is | ||
| 156 | * new. | ||
| 157 | */ | ||
| 158 | end_return = HOST_CHANGED; | ||
| 159 | } | ||
| 160 | /* Clear variables and close the file. */ | ||
| 161 | fclose(f); | ||
| 162 | |||
| 163 | /* | ||
| 164 | * Return either HOST_NEW or HOST_CHANGED, depending on whether we | ||
| 165 | * saw a different key for the host. | ||
| 166 | */ | ||
| 167 | return end_return; | ||
| 168 | } | ||
| 169 | |||
| 170 | /* | ||
| 171 | * Appends an entry to the host file. Returns false if the entry could not | ||
| 172 | * be appended. | ||
| 173 | */ | ||
| 174 | |||
| 175 | int | ||
| 176 | add_host_to_hostfile(const char *filename, const char *host, Key *key) | ||
| 177 | { | ||
| 178 | FILE *f; | ||
| 179 | int success = 0; | ||
| 180 | if (key == NULL) | ||
| 181 | return 1; /* XXX ? */ | ||
| 182 | f = fopen(filename, "a"); | ||
| 183 | if (!f) | ||
| 184 | return 0; | ||
| 185 | fprintf(f, "%s ", host); | ||
| 186 | if (key_write(key, f)) { | ||
| 187 | success = 1; | ||
| 188 | } else { | ||
| 189 | error("add_host_to_hostfile: saving key in %s failed", filename); | ||
| 190 | } | ||
| 191 | fprintf(f, "\n"); | ||
| 192 | fclose(f); | ||
| 193 | return success; | ||
| 194 | } | ||
