hostfile.h 4.0 KB

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