2 * Unix implementation of SSH connection-sharing IPC setup.
13 #include <sys/types.h>
16 #define DEFINE_PLUG_METHOD_MACROS
23 #define CONNSHARE_SOCKETDIR_PREFIX "/tmp/putty-connshare"
24 #define SALT_FILENAME "salt"
28 * Functions provided by uxnet.c to help connection sharing.
30 SockAddr unix_sock_addr(const char *path);
31 Socket new_unix_listener(SockAddr listenaddr, Plug plug);
33 static char *make_parentdir_name(void)
35 char *username, *parent;
37 username = get_username();
38 parent = dupprintf("%s.%s", CONNSHARE_SOCKETDIR_PREFIX, username);
40 assert(*parent == '/');
45 static char *make_dirname(const char *pi_name, char **logtext)
47 char *name, *parentdirname, *dirname, *err;
50 * First, create the top-level directory for all shared PuTTY
51 * connections owned by this user.
53 parentdirname = make_parentdir_name();
54 if ((err = make_dir_and_check_ours(parentdirname)) != NULL) {
61 * Transform the platform-independent version of the connection
62 * identifier into the name we'll actually use for the directory
63 * containing the Unix socket.
65 * We do this by hashing the identifier with some user-specific
66 * secret information, to avoid the privacy leak of having
67 * "user@host" strings show up in 'netstat -x'. (Irritatingly, the
68 * full pathname of a Unix-domain socket _does_ show up in the
69 * 'netstat -x' output, at least on Linux, even if that socket is
70 * in a directory not readable to the user running netstat. You'd
71 * think putting things inside an 0700 directory would hide their
72 * names from other users, but no.)
74 * The secret information we use to salt the hash lives in a file
75 * inside the top-level directory we just created, so we must
76 * first create that file (with some fresh random data in it) if
77 * it's not already been done by a previous PuTTY.
80 unsigned char saltbuf[SALT_SIZE];
84 saltname = dupprintf("%s/%s", parentdirname, SALT_FILENAME);
85 saltfd = open(saltname, O_RDONLY);
90 if (errno != ENOENT) {
91 *logtext = dupprintf("%s: open: %s", saltname,
99 * The salt file doesn't already exist, so try to create
100 * it. Another process may be attempting the same thing
101 * simultaneously, so we must do this carefully: we write
102 * a salt file under a different name, then hard-link it
103 * into place, which guarantees that we won't change the
104 * contents of an existing salt file.
108 tmpname = dupprintf("%s/%s.tmp.%d.%d",
109 parentdirname, SALT_FILENAME, pid, i);
110 saltfd = open(tmpname, O_WRONLY | O_EXCL | O_CREAT, 0400);
113 if (errno != EEXIST) {
114 *logtext = dupprintf("%s: open: %s", tmpname,
118 sfree(parentdirname);
121 sfree(tmpname); /* go round and try again with i+1 */
124 * Invent some random data.
126 for (i = 0; i < SALT_SIZE; i++) {
127 saltbuf[i] = random_byte();
129 ret = write(saltfd, saltbuf, SALT_SIZE);
130 /* POSIX atomicity guarantee: because we wrote less than
131 * PIPE_BUF bytes, the write either completed in full or
133 assert(SALT_SIZE < PIPE_BUF);
134 assert(ret < 0 || ret == SALT_SIZE);
137 *logtext = dupprintf("%s: write: %s", tmpname,
141 sfree(parentdirname);
144 if (close(saltfd) < 0) {
145 *logtext = dupprintf("%s: close: %s", tmpname,
149 sfree(parentdirname);
154 * Now attempt to hard-link our temp file into place. We
155 * tolerate EEXIST as an outcome, because that just means
156 * another PuTTY got their attempt in before we did (and
157 * we only care that there is a valid salt file we can
158 * agree on, no matter who created it).
160 if (link(tmpname, saltname) < 0 && errno != EEXIST) {
161 *logtext = dupprintf("%s: link: %s", saltname,
165 sfree(parentdirname);
170 * Whether that succeeded or not, get rid of our temp file.
172 if (unlink(tmpname) < 0) {
173 *logtext = dupprintf("%s: unlink: %s", tmpname,
177 sfree(parentdirname);
182 * And now we've arranged for there to be a salt file, so
183 * we can try to open it for reading again and this time
188 saltfd = open(saltname, O_RDONLY);
190 *logtext = dupprintf("%s: open: %s", saltname,
193 sfree(parentdirname);
198 for (i = 0; i < SALT_SIZE; i++) {
199 ret = read(saltfd, saltbuf, SALT_SIZE);
202 *logtext = dupprintf("%s: read: %s", saltname,
203 ret == 0 ? "unexpected EOF" :
206 sfree(parentdirname);
209 assert(0 < ret && ret <= SALT_SIZE - i);
217 * Now we've got our salt, hash it with the connection
218 * identifier to produce our actual socket name.
223 unsigned char lenbuf[4];
224 unsigned char digest[32];
228 PUT_32BIT(lenbuf, SALT_SIZE);
229 SHA256_Bytes(&sha, lenbuf, 4);
230 SHA256_Bytes(&sha, saltbuf, SALT_SIZE);
231 len = strlen(pi_name);
232 PUT_32BIT(lenbuf, len);
233 SHA256_Bytes(&sha, lenbuf, 4);
234 SHA256_Bytes(&sha, pi_name, len);
235 SHA256_Final(&sha, digest);
238 * And make it printable.
240 for (i = 0; i < 32; i++) {
241 sprintf(retbuf + 2*i, "%02x", digest[i]);
242 /* the last of those will also write the trailing NUL */
245 name = dupstr(retbuf);
248 smemclr(saltbuf, sizeof(saltbuf));
251 dirname = dupprintf("%s/%s", parentdirname, name);
252 sfree(parentdirname);
258 int platform_ssh_share(const char *pi_name, Conf *conf,
259 Plug downplug, Plug upplug, Socket *sock,
260 char **logtext, char **ds_err, char **us_err,
261 int can_upstream, int can_downstream)
263 char *dirname, *lockname, *sockname, *err;
268 * Sort out what we're going to call the directory in which we
269 * keep the socket. This has the side effect of potentially
270 * creating its top-level containing dir and/or the salt file
271 * within that, if they don't already exist.
273 dirname = make_dirname(pi_name, logtext);
279 * Now make sure the subdirectory exists.
281 if ((err = make_dir_and_check_ours(dirname)) != NULL) {
288 * Acquire a lock on a file in that directory.
290 lockname = dupcat(dirname, "/lock", (char *)NULL);
291 lockfd = open(lockname, O_CREAT | O_RDWR | O_TRUNC, 0600);
293 *logtext = dupprintf("%s: open: %s", lockname, strerror(errno));
298 if (flock(lockfd, LOCK_EX) < 0) {
299 *logtext = dupprintf("%s: flock(LOCK_EX): %s",
300 lockname, strerror(errno));
307 sockname = dupprintf("%s/socket", dirname);
311 if (can_downstream) {
312 retsock = new_connection(unix_sock_addr(sockname),
313 "", 0, 0, 1, 0, 0, downplug, conf);
314 if (sk_socket_error(retsock) == NULL) {
321 return SHARE_DOWNSTREAM;
324 *ds_err = dupprintf("%s: %s", sockname, sk_socket_error(retsock));
329 retsock = new_unix_listener(unix_sock_addr(sockname), upplug);
330 if (sk_socket_error(retsock) == NULL) {
337 return SHARE_UPSTREAM;
340 *us_err = dupprintf("%s: %s", sockname, sk_socket_error(retsock));
344 /* One of the above clauses ought to have happened. */
345 assert(*logtext || *ds_err || *us_err);
354 void platform_ssh_share_cleanup(const char *name)
356 char *dirname, *filename, *logtext;
358 dirname = make_dirname(name, &logtext);
360 sfree(logtext); /* we can't do much with this */
364 filename = dupcat(dirname, "/socket", (char *)NULL);
368 filename = dupcat(dirname, "/lock", (char *)NULL);
375 * We deliberately _don't_ clean up the parent directory
376 * /tmp/putty-connshare.<username>, because if we leave it around
377 * then it reduces the ability for other users to be a nuisance by
378 * putting their own directory in the way of it. Also, the salt
379 * file in it can be reused.