2 * SSH agent client code.
9 #include <sys/socket.h>
15 #define GET_32BIT(cp) \
16 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
17 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
18 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
19 ((unsigned long)(unsigned char)(cp)[3]))
21 int agent_exists(void)
23 if (getenv("SSH_AUTH_SOCK") != NULL)
28 void agent_query(void *in, int inlen, void **out, int *outlen)
32 struct sockaddr_un addr;
35 char sizebuf[4], *retbuf;
37 name = getenv("SSH_AUTH_SOCK");
41 sock = socket(PF_UNIX, SOCK_STREAM, 0);
43 perror("socket(PF_UNIX)");
47 addr.sun_family = AF_UNIX;
48 strncpy(addr.sun_path, name, sizeof(addr.sun_path));
49 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
54 for (done = 0; done < inlen ;) {
55 int ret = write(sock, (char *)in + done, inlen - done);
67 while (retlen < retsize) {
68 int ret = read(sock, retbuf + retlen, retsize - retlen);
71 if (retbuf != sizebuf) sfree(retbuf);
75 if (retsize == 4 && retlen == 4) {
76 retsize = GET_32BIT(retbuf);
82 assert(retbuf == sizebuf);
83 retbuf = snewn(retsize, char);
84 memcpy(retbuf, sizebuf, 4);
88 assert(retbuf != sizebuf);