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;
56 static int agent_select_result(int fd, int event)
59 struct agent_connection *conn;
61 assert(event == 1); /* not selecting for anything but R */
63 conn = find234(agent_connections, &fd, agent_connfind);
69 ret = read(fd, conn->retbuf+conn->retlen, conn->retsize-conn->retlen);
71 if (conn->retbuf != conn->sizebuf) sfree(conn->retbuf);
77 if (conn->retsize == 4 && conn->retlen == 4) {
78 conn->retsize = toint(GET_32BIT(conn->retbuf) + 4);
79 if (conn->retsize <= 0) {
84 assert(conn->retbuf == conn->sizebuf);
85 conn->retbuf = snewn(conn->retsize, char);
86 memcpy(conn->retbuf, conn->sizebuf, 4);
89 if (conn->retlen < conn->retsize)
90 return 0; /* more data to come */
94 * We have now completed the agent query. Do the callback, and
95 * clean up. (Of course we don't free retbuf, since ownership
96 * of that passes to the callback.)
98 conn->callback(conn->callback_ctx, conn->retbuf, conn->retlen);
101 del234(agent_connections, conn);
106 int agent_query(void *in, int inlen, void **out, int *outlen,
107 void (*callback)(void *, void *, int), void *callback_ctx)
111 struct sockaddr_un addr;
113 struct agent_connection *conn;
115 name = getenv("SSH_AUTH_SOCK");
119 sock = socket(PF_UNIX, SOCK_STREAM, 0);
121 perror("socket(PF_UNIX)");
127 addr.sun_family = AF_UNIX;
128 strncpy(addr.sun_path, name, sizeof(addr.sun_path));
129 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
134 for (done = 0; done < inlen ;) {
135 int ret = write(sock, (char *)in + done, inlen - done);
143 if (!agent_connections)
144 agent_connections = newtree234(agent_conncmp);
146 conn = snew(struct agent_connection);
148 conn->retbuf = conn->sizebuf;
151 conn->callback = callback;
152 conn->callback_ctx = callback_ctx;
153 add234(agent_connections, conn);
155 uxsel_set(sock, 1, agent_select_result);