pageant.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * pageant.h: header for pageant.c.
  3. */
  4. #include <stdarg.h>
  5. /*
  6. * Upper limit on length of any agent message. Used as a basic sanity
  7. * check on messages' length fields, and used by the Windows Pageant
  8. * client IPC to decide how large a file mapping to allocate.
  9. */
  10. #define AGENT_MAX_MSGLEN 262144
  11. typedef void (*pageant_logfn_t)(void *logctx, const char *fmt, va_list ap);
  12. /*
  13. * Initial setup.
  14. */
  15. void pageant_init(void);
  16. /*
  17. * The main agent function that answers messages.
  18. *
  19. * Expects a message/length pair as input, minus its initial length
  20. * field but still with its type code on the front.
  21. *
  22. * Returns a fully formatted message as output, *with* its initial
  23. * length field, and sets *outlen to the full size of that message.
  24. */
  25. void pageant_handle_msg(BinarySink *bs,
  26. const void *msg, int msglen,
  27. void *logctx, pageant_logfn_t logfn);
  28. /*
  29. * Construct a failure response. Useful for agent front ends which
  30. * suffer a problem before they even get to pageant_handle_msg.
  31. *
  32. * 'log_reason' is only used if logfn is not NULL.
  33. */
  34. void pageant_failure_msg(BinarySink *bs,
  35. const char *log_reason,
  36. void *logctx, pageant_logfn_t logfn);
  37. /*
  38. * Construct a list of public keys, just as the two LIST_IDENTITIES
  39. * requests would have returned them.
  40. */
  41. void pageant_make_keylist1(BinarySink *);
  42. void pageant_make_keylist2(BinarySink *);
  43. /*
  44. * Accessor functions for Pageant's internal key lists. Fetch the nth
  45. * key; count the keys; attempt to add a key (returning true on
  46. * success, in which case the ownership of the key structure has been
  47. * taken over by pageant.c); attempt to delete a key (returning true
  48. * on success, in which case the ownership of the key structure is
  49. * passed back to the client).
  50. */
  51. RSAKey *pageant_nth_ssh1_key(int i);
  52. ssh2_userkey *pageant_nth_ssh2_key(int i);
  53. int pageant_count_ssh1_keys(void);
  54. int pageant_count_ssh2_keys(void);
  55. bool pageant_add_ssh1_key(RSAKey *rkey);
  56. bool pageant_add_ssh2_key(ssh2_userkey *skey);
  57. bool pageant_delete_ssh1_key(RSAKey *rkey);
  58. bool pageant_delete_ssh2_key(ssh2_userkey *skey);
  59. /*
  60. * This callback must be provided by the Pageant front end code.
  61. * pageant_handle_msg calls it to indicate that the message it's just
  62. * handled has changed the list of keys held by the agent. Front ends
  63. * which expose that key list through dedicated UI may need to refresh
  64. * that UI's state in this function; other front ends can leave it
  65. * empty.
  66. */
  67. void keylist_update(void);
  68. /*
  69. * Functions to establish a listening socket speaking the SSH agent
  70. * protocol. Call pageant_listener_new() to set up a state; then
  71. * create a socket using the returned Plug; then call
  72. * pageant_listener_got_socket() to give the listening state its own
  73. * socket pointer. Also, provide a logging function later if you want
  74. * to.
  75. */
  76. struct pageant_listen_state;
  77. struct pageant_listen_state *pageant_listener_new(Plug **plug);
  78. void pageant_listener_got_socket(struct pageant_listen_state *pl, Socket *);
  79. void pageant_listener_set_logfn(struct pageant_listen_state *pl,
  80. void *logctx, pageant_logfn_t logfn);
  81. void pageant_listener_free(struct pageant_listen_state *pl);
  82. /*
  83. * Functions to perform specific key actions, either as a client of an
  84. * ssh-agent running elsewhere, or directly on the agent state in this
  85. * process. (On at least one platform we want to do this in an
  86. * agnostic way between the two situations.)
  87. *
  88. * pageant_get_keylist{1,2} work just like pageant_make_keylist{1,2}
  89. * above, except that they can also cope if they have to contact an
  90. * external agent.
  91. *
  92. * pageant_add_keyfile() is used to load a private key from a file and
  93. * add it to the agent. Initially, you should call it with passphrase
  94. * NULL, and it will check if the key is already in the agent, and
  95. * whether a passphrase is required. Return values are given in the
  96. * enum below. On return, *retstr will either be NULL, or a
  97. * dynamically allocated string containing a key comment or an error
  98. * message.
  99. *
  100. * pageant_add_keyfile() also remembers passphrases with which it's
  101. * successfully decrypted keys (because if you try to add multiple
  102. * keys in one go, you might very well have used the same passphrase
  103. * for keys that have the same trust properties). Call
  104. * pageant_forget_passphrases() to get rid of them all.
  105. */
  106. void *pageant_get_keylist1(int *length);
  107. void *pageant_get_keylist2(int *length);
  108. enum {
  109. PAGEANT_ACTION_OK, /* success; no further action needed */
  110. PAGEANT_ACTION_FAILURE, /* failure; *retstr is error message */
  111. PAGEANT_ACTION_NEED_PP /* need passphrase: *retstr is key comment */
  112. };
  113. int pageant_add_keyfile(Filename *filename, const char *passphrase,
  114. char **retstr);
  115. void pageant_forget_passphrases(void);
  116. struct pageant_pubkey {
  117. /* Everything needed to identify a public key found by
  118. * pageant_enum_keys and pass it back to the agent or other code
  119. * later */
  120. strbuf *blob;
  121. char *comment;
  122. int ssh_version;
  123. };
  124. struct pageant_pubkey *pageant_pubkey_copy(struct pageant_pubkey *key);
  125. void pageant_pubkey_free(struct pageant_pubkey *key);
  126. typedef void (*pageant_key_enum_fn_t)(void *ctx,
  127. const char *fingerprint,
  128. const char *comment,
  129. struct pageant_pubkey *key);
  130. int pageant_enum_keys(pageant_key_enum_fn_t callback, void *callback_ctx,
  131. char **retstr);
  132. int pageant_delete_key(struct pageant_pubkey *key, char **retstr);
  133. int pageant_delete_all_keys(char **retstr);