cli-agentfwd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2005 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #ifdef ENABLE_CLI_AGENTFWD
  26. #include "agentfwd.h"
  27. #include "session.h"
  28. #include "ssh.h"
  29. #include "dbutil.h"
  30. #include "chansession.h"
  31. #include "channel.h"
  32. #include "packet.h"
  33. #include "buffer.h"
  34. #include "dbrandom.h"
  35. #include "listener.h"
  36. #include "runopts.h"
  37. #include "atomicio.h"
  38. #include "signkey.h"
  39. #include "auth.h"
  40. /* The protocol implemented to talk to OpenSSH's SSH2 agent is documented in
  41. PROTOCOL.agent in recent OpenSSH source distributions (5.1p1 has it). */
  42. static int new_agent_chan(struct Channel * channel);
  43. const struct ChanType cli_chan_agent = {
  44. 0, /* sepfds */
  45. "auth-agent@openssh.com",
  46. new_agent_chan,
  47. NULL,
  48. NULL,
  49. NULL
  50. };
  51. static int connect_agent() {
  52. int fd = -1;
  53. char* agent_sock = NULL;
  54. agent_sock = getenv("SSH_AUTH_SOCK");
  55. if (agent_sock == NULL)
  56. return -1;
  57. fd = connect_unix(agent_sock);
  58. if (fd < 0) {
  59. dropbear_log(LOG_INFO, "Failed to connect to agent");
  60. }
  61. return fd;
  62. }
  63. /* handle a request for a connection to the locally running ssh-agent
  64. or forward. */
  65. static int new_agent_chan(struct Channel * channel) {
  66. int fd = -1;
  67. if (!cli_opts.agent_fwd)
  68. return SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
  69. fd = connect_agent();
  70. if (fd < 0) {
  71. return SSH_OPEN_CONNECT_FAILED;
  72. }
  73. setnonblocking(fd);
  74. ses.maxfd = MAX(ses.maxfd, fd);
  75. channel->readfd = fd;
  76. channel->writefd = fd;
  77. return 0;
  78. }
  79. /* Sends a request to the agent, returning a newly allocated buffer
  80. * with the response */
  81. /* This function will block waiting for a response - it will
  82. * only be used by client authentication (not for forwarded requests)
  83. * won't cause problems for interactivity. */
  84. /* Packet format (from draft-ylonen)
  85. 4 bytes Length, msb first. Does not include length itself.
  86. 1 byte Packet type. The value 255 is reserved for future extensions.
  87. data Any data, depending on packet type. Encoding as in the ssh packet
  88. protocol.
  89. */
  90. static buffer * agent_request(unsigned char type, buffer *data) {
  91. buffer * payload = NULL;
  92. buffer * inbuf = NULL;
  93. size_t readlen = 0;
  94. ssize_t ret;
  95. const int fd = cli_opts.agent_fd;
  96. unsigned int data_len = 0;
  97. if (data)
  98. {
  99. data_len = data->len;
  100. }
  101. payload = buf_new(4 + 1 + data_len);
  102. buf_putint(payload, 1 + data_len);
  103. buf_putbyte(payload, type);
  104. if (data) {
  105. buf_putbytes(payload, data->data, data->len);
  106. }
  107. buf_setpos(payload, 0);
  108. ret = atomicio(write, fd, buf_getptr(payload, payload->len), payload->len);
  109. if ((size_t)ret != payload->len) {
  110. TRACE(("write failed fd %d for agent_request, %s", fd, strerror(errno)))
  111. goto out;
  112. }
  113. buf_free(payload);
  114. payload = NULL;
  115. TRACE(("Wrote out bytes for agent_request"))
  116. /* Now we read the response */
  117. inbuf = buf_new(4);
  118. ret = atomicio(read, fd, buf_getwriteptr(inbuf, 4), 4);
  119. if (ret != 4) {
  120. TRACE(("read of length failed for agent_request"))
  121. goto out;
  122. }
  123. buf_setpos(inbuf, 0);
  124. buf_setlen(inbuf, ret);
  125. readlen = buf_getint(inbuf);
  126. if (readlen > MAX_AGENT_REPLY) {
  127. TRACE(("agent reply is too big"));
  128. goto out;
  129. }
  130. inbuf = buf_resize(inbuf, readlen);
  131. buf_setpos(inbuf, 0);
  132. ret = atomicio(read, fd, buf_getwriteptr(inbuf, readlen), readlen);
  133. if ((size_t)ret != readlen) {
  134. TRACE(("read of data failed for agent_request"))
  135. goto out;
  136. }
  137. buf_incrwritepos(inbuf, readlen);
  138. buf_setpos(inbuf, 0);
  139. out:
  140. if (payload)
  141. buf_free(payload);
  142. return inbuf;
  143. }
  144. static void agent_get_key_list(m_list * ret_list)
  145. {
  146. buffer * inbuf = NULL;
  147. unsigned int num = 0;
  148. unsigned char packet_type;
  149. unsigned int i;
  150. int ret;
  151. inbuf = agent_request(SSH2_AGENTC_REQUEST_IDENTITIES, NULL);
  152. if (!inbuf) {
  153. TRACE(("agent_request failed returning identities"))
  154. goto out;
  155. }
  156. /* The reply has a format of:
  157. byte SSH2_AGENT_IDENTITIES_ANSWER
  158. uint32 num_keys
  159. Followed by zero or more consecutive keys, encoded as:
  160. string key_blob
  161. string key_comment
  162. */
  163. packet_type = buf_getbyte(inbuf);
  164. if (packet_type != SSH2_AGENT_IDENTITIES_ANSWER) {
  165. goto out;
  166. }
  167. num = buf_getint(inbuf);
  168. for (i = 0; i < num; i++) {
  169. sign_key * pubkey = NULL;
  170. enum signkey_type key_type = DROPBEAR_SIGNKEY_ANY;
  171. buffer * key_buf;
  172. /* each public key is encoded as a string */
  173. key_buf = buf_getstringbuf(inbuf);
  174. pubkey = new_sign_key();
  175. ret = buf_get_pub_key(key_buf, pubkey, &key_type);
  176. buf_free(key_buf);
  177. if (ret != DROPBEAR_SUCCESS) {
  178. TRACE(("Skipping bad/unknown type pubkey from agent"));
  179. sign_key_free(pubkey);
  180. } else {
  181. pubkey->type = key_type;
  182. pubkey->source = SIGNKEY_SOURCE_AGENT;
  183. list_append(ret_list, pubkey);
  184. }
  185. /* We'll ignore the comment for now. might want it later.*/
  186. buf_eatstring(inbuf);
  187. }
  188. out:
  189. if (inbuf) {
  190. buf_free(inbuf);
  191. inbuf = NULL;
  192. }
  193. }
  194. void cli_setup_agent(struct Channel *channel) {
  195. if (!getenv("SSH_AUTH_SOCK")) {
  196. return;
  197. }
  198. start_send_channel_request(channel, "auth-agent-req@openssh.com");
  199. /* Don't want replies */
  200. buf_putbyte(ses.writepayload, 0);
  201. encrypt_packet();
  202. }
  203. /* Returned keys are prepended to ret_list, which will
  204. be updated. */
  205. void cli_load_agent_keys(m_list *ret_list) {
  206. /* agent_fd will be closed after successful auth */
  207. cli_opts.agent_fd = connect_agent();
  208. if (cli_opts.agent_fd < 0) {
  209. return;
  210. }
  211. agent_get_key_list(ret_list);
  212. }
  213. void agent_buf_sign(buffer *sigblob, sign_key *key,
  214. buffer *data_buf) {
  215. buffer *request_data = NULL;
  216. buffer *response = NULL;
  217. unsigned int siglen;
  218. int packet_type;
  219. /* Request format
  220. byte SSH2_AGENTC_SIGN_REQUEST
  221. string key_blob
  222. string data
  223. uint32 flags
  224. */
  225. request_data = buf_new(MAX_PUBKEY_SIZE + data_buf->len + 12);
  226. buf_put_pub_key(request_data, key, key->type);
  227. buf_putbufstring(request_data, data_buf);
  228. buf_putint(request_data, 0);
  229. response = agent_request(SSH2_AGENTC_SIGN_REQUEST, request_data);
  230. if (!response) {
  231. goto fail;
  232. }
  233. packet_type = buf_getbyte(response);
  234. if (packet_type != SSH2_AGENT_SIGN_RESPONSE) {
  235. goto fail;
  236. }
  237. /* Response format
  238. byte SSH2_AGENT_SIGN_RESPONSE
  239. string signature_blob
  240. */
  241. siglen = buf_getint(response);
  242. buf_putbytes(sigblob, buf_getptr(response, siglen), siglen);
  243. goto cleanup;
  244. fail:
  245. /* XXX don't fail badly here. instead propagate a failure code back up to
  246. the cli auth pubkey code, and just remove this key from the list of
  247. ones to try. */
  248. dropbear_exit("Agent failed signing key");
  249. cleanup:
  250. if (request_data) {
  251. buf_free(request_data);
  252. }
  253. if (response) {
  254. buf_free(response);
  255. }
  256. }
  257. #endif