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