2 * SSH agent client code.
9 #include <sys/socket.h>
18 int agent_exists(void)
20 const char *p = getenv("SSH_AUTH_SOCK");
26 static tree234 *agent_connections;
27 struct agent_connection {
32 void (*callback)(void *, void *, int);
35 static int agent_conncmp(void *av, void *bv)
37 struct agent_connection *a = (struct agent_connection *) av;
38 struct agent_connection *b = (struct agent_connection *) bv;
45 static int agent_connfind(void *av, void *bv)
47 int afd = *(int *) av;
48 struct agent_connection *b = (struct agent_connection *) bv;
57 * Attempt to read from an agent socket fd. Returns 0 if the expected
58 * response is as yet incomplete; returns 1 if it's either complete
59 * (conn->retbuf non-NULL and filled with something useful) or has
60 * failed totally (conn->retbuf is NULL).
62 static int agent_try_read(struct agent_connection *conn)
66 ret = read(conn->fd, conn->retbuf+conn->retlen,
67 conn->retsize-conn->retlen);
69 if (conn->retbuf != conn->sizebuf) sfree(conn->retbuf);
75 if (conn->retsize == 4 && conn->retlen == 4) {
76 conn->retsize = toint(GET_32BIT(conn->retbuf) + 4);
77 if (conn->retsize <= 0) {
80 return -1; /* way too large */
82 assert(conn->retbuf == conn->sizebuf);
83 conn->retbuf = snewn(conn->retsize, char);
84 memcpy(conn->retbuf, conn->sizebuf, 4);
87 if (conn->retlen < conn->retsize)
88 return 0; /* more data to come */
93 static int agent_select_result(int fd, int event)
95 struct agent_connection *conn;
97 assert(event == 1); /* not selecting for anything but R */
99 conn = find234(agent_connections, &fd, agent_connfind);
105 if (!agent_try_read(conn))
106 return 0; /* more data to come */
109 * We have now completed the agent query. Do the callback, and
110 * clean up. (Of course we don't free retbuf, since ownership
111 * of that passes to the callback.)
113 conn->callback(conn->callback_ctx, conn->retbuf, conn->retlen);
116 del234(agent_connections, conn);
121 int agent_query(void *in, int inlen, void **out, int *outlen,
122 void (*callback)(void *, void *, int), void *callback_ctx)
126 struct sockaddr_un addr;
128 struct agent_connection *conn;
130 name = getenv("SSH_AUTH_SOCK");
134 sock = socket(PF_UNIX, SOCK_STREAM, 0);
136 perror("socket(PF_UNIX)");
142 addr.sun_family = AF_UNIX;
143 strncpy(addr.sun_path, name, sizeof(addr.sun_path));
144 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
149 for (done = 0; done < inlen ;) {
150 int ret = write(sock, (char *)in + done, inlen - done);
158 conn = snew(struct agent_connection);
160 conn->retbuf = conn->sizebuf;
163 conn->callback = callback;
164 conn->callback_ctx = callback_ctx;
168 * Bodge to permit making deliberately synchronous agent
169 * requests. Used by Unix Pageant in command-line client mode,
170 * which is legit because it really is true that no other part
171 * of the program is trying to get anything useful done
172 * simultaneously. But this special case shouldn't be used in
173 * any more general program.
175 no_nonblock(conn->fd);
176 while (!agent_try_read(conn))
177 /* empty loop body */;
180 *outlen = conn->retlen;
186 * Otherwise do it properly: add conn to the tree of agent
187 * connections currently in flight, return 0 to indicate that the
188 * response hasn't been received yet, and call the callback when
189 * select_result comes back to us.
191 if (!agent_connections)
192 agent_connections = newtree234(agent_conncmp);
193 add234(agent_connections, conn);
195 uxsel_set(sock, 1, agent_select_result);