djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 1 | /* $OpenBSD: hostfile.h,v 1.24 2015/02/16 22:08:57 djm Exp $ */ |
Ben Lindstrom | 36579d3 | 2001-01-29 07:39:26 +0000 | [diff] [blame] | 2 | |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 3 | /* |
| 4 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 5 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 6 | * All rights reserved |
| 7 | * |
| 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 14 | #ifndef HOSTFILE_H |
| 15 | #define HOSTFILE_H |
| 16 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 17 | typedef enum { |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 18 | HOST_OK, HOST_NEW, HOST_CHANGED, HOST_REVOKED, HOST_FOUND |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 19 | } HostStatus; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 20 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 21 | typedef enum { |
| 22 | MRK_ERROR, MRK_NONE, MRK_REVOKE, MRK_CA |
| 23 | } HostkeyMarker; |
| 24 | |
| 25 | struct hostkey_entry { |
| 26 | char *host; |
| 27 | char *file; |
| 28 | u_long line; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 29 | struct sshkey *key; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 30 | HostkeyMarker marker; |
| 31 | }; |
| 32 | struct hostkeys; |
| 33 | |
| 34 | struct hostkeys *init_hostkeys(void); |
| 35 | void load_hostkeys(struct hostkeys *, const char *, const char *); |
| 36 | void free_hostkeys(struct hostkeys *); |
| 37 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 38 | HostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 39 | const struct hostkey_entry **); |
| 40 | int lookup_key_in_hostkeys_by_type(struct hostkeys *, int, |
| 41 | const struct hostkey_entry **); |
| 42 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 43 | int hostfile_read_key(char **, u_int *, struct sshkey *); |
| 44 | int add_host_to_hostfile(const char *, const char *, |
| 45 | const struct sshkey *, int); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 46 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 47 | int hostfile_replace_entries(const char *filename, |
| 48 | const char *host, const char *ip, struct sshkey **keys, size_t nkeys, |
| 49 | int store_hash, int quiet, int hash_alg); |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 50 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 51 | #define HASH_MAGIC "|1|" |
| 52 | #define HASH_DELIM '|' |
| 53 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 54 | #define CA_MARKER "@cert-authority" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 55 | #define REVOKE_MARKER "@revoked" |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 56 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 57 | char *host_hash(const char *, const char *, u_int); |
| 58 | |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 59 | /* |
| 60 | * Iterate through a hostkeys file, optionally parsing keys and matching |
| 61 | * hostnames. Allows access to the raw keyfile lines to allow |
| 62 | * streaming edits to the file to take place. |
| 63 | */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 64 | #define HKF_WANT_MATCH (1) /* return only matching hosts/addrs */ |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 65 | #define HKF_WANT_PARSE_KEY (1<<1) /* need key parsed */ |
| 66 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 67 | #define HKF_STATUS_OK 0 /* Line parsed, didn't match host */ |
| 68 | #define HKF_STATUS_INVALID 1 /* line had parse error */ |
| 69 | #define HKF_STATUS_COMMENT 2 /* valid line contained no key */ |
| 70 | #define HKF_STATUS_MATCHED 3 /* hostname or IP matched */ |
| 71 | |
| 72 | #define HKF_MATCH_HOST (1) /* hostname matched */ |
| 73 | #define HKF_MATCH_IP (1<<1) /* address matched */ |
| 74 | #define HKF_MATCH_HOST_HASHED (1<<2) /* hostname was hashed */ |
| 75 | #define HKF_MATCH_IP_HASHED (1<<3) /* address was hashed */ |
| 76 | /* XXX HKF_MATCH_KEY_TYPE? */ |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * The callback function receives this as an argument for each matching |
| 80 | * hostkey line. The callback may "steal" the 'key' field by setting it to NULL. |
| 81 | * If a parse error occurred, then "hosts" and subsequent options may be NULL. |
| 82 | */ |
| 83 | struct hostkey_foreach_line { |
| 84 | const char *path; /* Path of file */ |
| 85 | u_long linenum; /* Line number */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 86 | u_int status; /* One of HKF_STATUS_* */ |
| 87 | u_int match; /* Zero or more of HKF_MATCH_* OR'd together */ |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 88 | char *line; /* Entire key line; mutable by callback */ |
| 89 | int marker; /* CA/revocation markers; indicated by MRK_* value */ |
| 90 | const char *hosts; /* Raw hosts text, may be hashed or list multiple */ |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 91 | const char *rawkey; /* Text of key and any comment following it */ |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 92 | int keytype; /* Type of key; KEY_UNSPEC for invalid/comment lines */ |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 93 | struct sshkey *key; /* Key, if parsed ok and HKF_WANT_MATCH_HOST set */ |
| 94 | const char *comment; /* Any comment following the key */ |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Callback fires for each line (or matching line if a HKF_WANT_* option |
| 99 | * is set). The foreach loop will terminate if the callback returns a non- |
| 100 | * zero exit status. |
| 101 | */ |
| 102 | typedef int hostkeys_foreach_fn(struct hostkey_foreach_line *l, void *ctx); |
| 103 | |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 104 | /* Iterate over a hostkeys file */ |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 105 | int hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx, |
djm@openbsd.org | 6c5c949 | 2015-02-16 22:08:57 +0000 | [diff] [blame] | 106 | const char *host, const char *ip, u_int options); |
djm@openbsd.org | c29811c | 2015-01-18 21:40:23 +0000 | [diff] [blame] | 107 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 108 | #endif |