123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #include "includes.h"
- #ifdef ENABLE_CLI_AGENTFWD
- #include "agentfwd.h"
- #include "session.h"
- #include "ssh.h"
- #include "dbutil.h"
- #include "chansession.h"
- #include "channel.h"
- #include "packet.h"
- #include "buffer.h"
- #include "dbrandom.h"
- #include "listener.h"
- #include "runopts.h"
- #include "atomicio.h"
- #include "signkey.h"
- #include "auth.h"
- static int new_agent_chan(struct Channel * channel);
- const struct ChanType cli_chan_agent = {
- 0,
- "auth-agent@openssh.com",
- new_agent_chan,
- NULL,
- NULL,
- NULL
- };
- static int connect_agent() {
- int fd = -1;
- char* agent_sock = NULL;
- agent_sock = getenv("SSH_AUTH_SOCK");
- if (agent_sock == NULL)
- return -1;
- fd = connect_unix(agent_sock);
- if (fd < 0) {
- dropbear_log(LOG_INFO, "Failed to connect to agent");
- }
- return fd;
- }
- static int new_agent_chan(struct Channel * channel) {
- int fd = -1;
- if (!cli_opts.agent_fwd)
- return SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
- fd = connect_agent();
- if (fd < 0) {
- return SSH_OPEN_CONNECT_FAILED;
- }
- setnonblocking(fd);
- ses.maxfd = MAX(ses.maxfd, fd);
- channel->readfd = fd;
- channel->writefd = fd;
- return 0;
- }
- static buffer * agent_request(unsigned char type, buffer *data) {
- buffer * payload = NULL;
- buffer * inbuf = NULL;
- size_t readlen = 0;
- ssize_t ret;
- const int fd = cli_opts.agent_fd;
- unsigned int data_len = 0;
- if (data)
- {
- data_len = data->len;
- }
- payload = buf_new(4 + 1 + data_len);
- buf_putint(payload, 1 + data_len);
- buf_putbyte(payload, type);
- if (data) {
- buf_putbytes(payload, data->data, data->len);
- }
- buf_setpos(payload, 0);
- ret = atomicio(write, fd, buf_getptr(payload, payload->len), payload->len);
- if ((size_t)ret != payload->len) {
- TRACE(("write failed fd %d for agent_request, %s", fd, strerror(errno)))
- goto out;
- }
- buf_free(payload);
- payload = NULL;
- TRACE(("Wrote out bytes for agent_request"))
-
- inbuf = buf_new(4);
- ret = atomicio(read, fd, buf_getwriteptr(inbuf, 4), 4);
- if (ret != 4) {
- TRACE(("read of length failed for agent_request"))
- goto out;
- }
- buf_setpos(inbuf, 0);
- buf_setlen(inbuf, ret);
- readlen = buf_getint(inbuf);
- if (readlen > MAX_AGENT_REPLY) {
- TRACE(("agent reply is too big"));
- goto out;
- }
-
- inbuf = buf_resize(inbuf, readlen);
- buf_setpos(inbuf, 0);
- ret = atomicio(read, fd, buf_getwriteptr(inbuf, readlen), readlen);
- if ((size_t)ret != readlen) {
- TRACE(("read of data failed for agent_request"))
- goto out;
- }
- buf_incrwritepos(inbuf, readlen);
- buf_setpos(inbuf, 0);
- out:
- if (payload)
- buf_free(payload);
- return inbuf;
- }
- static void agent_get_key_list(m_list * ret_list)
- {
- buffer * inbuf = NULL;
- unsigned int num = 0;
- unsigned char packet_type;
- unsigned int i;
- int ret;
- inbuf = agent_request(SSH2_AGENTC_REQUEST_IDENTITIES, NULL);
- if (!inbuf) {
- TRACE(("agent_request failed returning identities"))
- goto out;
- }
-
- packet_type = buf_getbyte(inbuf);
- if (packet_type != SSH2_AGENT_IDENTITIES_ANSWER) {
- goto out;
- }
- num = buf_getint(inbuf);
- for (i = 0; i < num; i++) {
- sign_key * pubkey = NULL;
- enum signkey_type key_type = DROPBEAR_SIGNKEY_ANY;
- buffer * key_buf;
-
- key_buf = buf_getstringbuf(inbuf);
- pubkey = new_sign_key();
- ret = buf_get_pub_key(key_buf, pubkey, &key_type);
- buf_free(key_buf);
- if (ret != DROPBEAR_SUCCESS) {
- TRACE(("Skipping bad/unknown type pubkey from agent"));
- sign_key_free(pubkey);
- } else {
- pubkey->type = key_type;
- pubkey->source = SIGNKEY_SOURCE_AGENT;
- list_append(ret_list, pubkey);
- }
-
- buf_eatstring(inbuf);
- }
- out:
- if (inbuf) {
- buf_free(inbuf);
- inbuf = NULL;
- }
- }
- void cli_setup_agent(struct Channel *channel) {
- if (!getenv("SSH_AUTH_SOCK")) {
- return;
- }
-
- start_send_channel_request(channel, "auth-agent-req@openssh.com");
-
- buf_putbyte(ses.writepayload, 0);
- encrypt_packet();
- }
- void cli_load_agent_keys(m_list *ret_list) {
-
- cli_opts.agent_fd = connect_agent();
- if (cli_opts.agent_fd < 0) {
- return;
- }
- agent_get_key_list(ret_list);
- }
- void agent_buf_sign(buffer *sigblob, sign_key *key,
- buffer *data_buf) {
- buffer *request_data = NULL;
- buffer *response = NULL;
- unsigned int siglen;
- int packet_type;
-
-
- request_data = buf_new(MAX_PUBKEY_SIZE + data_buf->len + 12);
- buf_put_pub_key(request_data, key, key->type);
-
- buf_putbufstring(request_data, data_buf);
- buf_putint(request_data, 0);
-
- response = agent_request(SSH2_AGENTC_SIGN_REQUEST, request_data);
-
- if (!response) {
- goto fail;
- }
-
- packet_type = buf_getbyte(response);
- if (packet_type != SSH2_AGENT_SIGN_RESPONSE) {
- goto fail;
- }
-
-
- siglen = buf_getint(response);
- buf_putbytes(sigblob, buf_getptr(response, siglen), siglen);
- goto cleanup;
-
- fail:
-
- dropbear_exit("Agent failed signing key");
- cleanup:
- if (request_data) {
- buf_free(request_data);
- }
- if (response) {
- buf_free(response);
- }
- }
- #endif
|