2 * Unix Pageant, more or less similar to ssh-agent.
11 #include <sys/types.h>
15 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
21 SockAddr unix_sock_addr(const char *path);
22 Socket new_unix_listener(SockAddr listenaddr, Plug plug);
24 void fatalbox(char *p, ...)
27 fprintf(stderr, "FATAL ERROR: ");
29 vfprintf(stderr, p, ap);
34 void modalfatalbox(char *p, ...)
37 fprintf(stderr, "FATAL ERROR: ");
39 vfprintf(stderr, p, ap);
44 void nonfatal(char *p, ...)
47 fprintf(stderr, "ERROR: ");
49 vfprintf(stderr, p, ap);
53 void connection_fatal(void *frontend, char *p, ...)
56 fprintf(stderr, "FATAL ERROR: ");
58 vfprintf(stderr, p, ap);
63 void cmdline_error(char *p, ...)
66 fprintf(stderr, "pageant: ");
68 vfprintf(stderr, p, ap);
74 FILE *pageant_logfp = NULL;
75 void pageant_log(void *ctx, const char *fmt, va_list ap)
80 fprintf(pageant_logfp, "pageant: ");
81 vfprintf(pageant_logfp, fmt, ap);
82 fprintf(pageant_logfp, "\n");
86 * In Pageant our selects are synchronous, so these functions are
89 int uxsel_input_add(int fd, int rwx) { return 0; }
90 void uxsel_input_remove(int id) { }
95 void logevent(void *frontend, const char *string) {}
96 void random_save_seed(void) {}
97 void random_destroy_seed(void) {}
98 void noise_ultralight(unsigned long data) {}
99 char *platform_default_s(const char *name) { return NULL; }
100 int platform_default_i(const char *name, int def) { return def; }
101 FontSpec *platform_default_fontspec(const char *name) { return fontspec_new(""); }
102 Filename *platform_default_filename(const char *name) { return filename_from_str(""); }
103 char *x_get_default(const char *key) { return NULL; }
104 void old_keyfile_warning(void) {}
105 void timer_change_notify(unsigned long next) {}
108 * Short description of parameters.
110 static void usage(void)
112 printf("Pageant: SSH agent\n");
118 static void version(void)
120 printf("pageant: %s\n", ver);
124 void keylist_update(void)
126 /* Nothing needs doing in Unix Pageant */
129 #define PAGEANT_DIR_PREFIX "/tmp/pageant"
131 const char *const appname = "Pageant";
133 char *platform_get_x_display(void) {
134 return dupstr(getenv("DISPLAY"));
137 static int time_to_die = FALSE;
139 /* Stub functions to permit linking against x11fwd.c. These never get
140 * used, because in LIFE_X11 mode we connect to the X server using a
141 * straightforward Socket and don't try to create an ersatz SSH
143 int sshfwd_write(struct ssh_channel *c, char *data, int len) { return 0; }
144 void sshfwd_write_eof(struct ssh_channel *c) { }
145 void sshfwd_unclean_close(struct ssh_channel *c, const char *err) { }
146 void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) {}
147 Conf *sshfwd_get_conf(struct ssh_channel *c) { return NULL; }
148 void sshfwd_x11_sharing_handover(struct ssh_channel *c,
149 void *share_cs, void *share_chan,
150 const char *peer_addr, int peer_port,
151 int endian, int protomajor, int protominor,
152 const void *initial_data, int initial_len) {}
153 void sshfwd_x11_is_local(struct ssh_channel *c) {}
156 * These functions are part of the plug for our connection to the X
157 * display, so they do get called. They needn't actually do anything,
158 * except that x11_closing has to signal back to the main loop that
159 * it's time to terminate.
161 static void x11_log(Plug p, int type, SockAddr addr, int port,
162 const char *error_msg, int error_code) {}
163 static int x11_receive(Plug plug, int urgent, char *data, int len) {return 0;}
164 static void x11_sent(Plug plug, int bufsize) {}
165 static int x11_closing(Plug plug, const char *error_msg, int error_code,
171 struct X11Connection {
172 const struct plug_function_table *fn;
176 void pageant_print_env(int pid)
178 printf("SSH_AUTH_SOCK=%s; export SSH_AUTH_SOCK;\n"
179 "SSH_AGENT_PID=%d; export SSH_AGENT_PID;\n",
180 socketname, (int)pid);
183 void pageant_fork_and_print_env(void)
189 } else if (pid != 0) {
190 pageant_print_env(pid);
195 * Having forked off, we now daemonise ourselves as best we can.
196 * It's good practice in general to setsid() ourself out of any
197 * process group we didn't want to be part of, and to chdir("/")
198 * to avoid holding any directories open that we don't need in
199 * case someone wants to umount them; also, we should definitely
200 * close standard output (because it will very likely be pointing
201 * at a pipe from which some parent process is trying to read our
202 * environment variable dump, so if we hold open another copy of
203 * it then that process will never finish reading). We close
204 * standard input too on general principles, but not standard
205 * error, since we might need to shout a panicky error message
208 if (chdir("/") < 0) {
209 /* should there be an error condition, nothing we can do about
219 void sigchld(int signum)
221 if (write(signalpipe[1], "x", 1) <= 0)
222 /* not much we can do about it */;
225 int main(int argc, char **argv)
229 int i, fdcount, fdsize, fdstate;
232 char *username, *socketdir;
234 struct pageant_listen_state *pl;
237 LIFE_UNSPEC, LIFE_X11, LIFE_DEBUG, LIFE_PERM, LIFE_EXEC
238 } life = LIFE_UNSPEC;
239 const char *display = NULL;
240 int doing_opts = TRUE;
241 char **exec_args = NULL;
242 int termination_pid = -1;
246 fdcount = fdsize = 0;
250 * Process the command line.
254 if (*p == '-' && doing_opts) {
255 if (!strcmp(p, "-V") || !strcmp(p, "--version")) {
257 } else if (!strcmp(p, "--help")) {
260 } else if (!strcmp(p, "-v")) {
261 pageant_logfp = stderr;
262 } else if (!strcmp(p, "-X")) {
264 } else if (!strcmp(p, "--debug")) {
266 } else if (!strcmp(p, "--permanent")) {
268 } else if (!strcmp(p, "--exec")) {
270 } else if (!strcmp(p, "--")) {
274 if (life == LIFE_EXEC) {
276 break; /* everything else is now args to the exec command */
278 fprintf(stderr, "pageant: unexpected argument '%s'\n", p);
287 if (life == LIFE_UNSPEC) {
288 fprintf(stderr, "pageant: expected a lifetime option\n");
291 if (life == LIFE_EXEC && !exec_args) {
292 fprintf(stderr, "pageant: expected a command with --exec\n");
297 * Block SIGPIPE, so that we'll get EPIPE individually on
298 * particular network connections that go wrong.
300 putty_signal(SIGPIPE, SIG_IGN);
306 * Set up a listening socket and run Pageant on it.
308 username = get_username();
309 socketdir = dupprintf("%s.%s", PAGEANT_DIR_PREFIX, username);
311 assert(*socketdir == '/');
312 if ((err = make_dir_and_check_ours(socketdir)) != NULL) {
313 fprintf(stderr, "pageant: %s: %s\n", socketdir, err);
316 socketname = dupprintf("%s/pageant.%d", socketdir, (int)getpid());
318 pl = pageant_listener_new();
319 sock = new_unix_listener(unix_sock_addr(socketname), (Plug)pl);
320 if ((err = sk_socket_error(sock)) != NULL) {
321 fprintf(stderr, "pageant: %s: %s\n", socketname, err);
324 pageant_listener_got_socket(pl, sock);
327 conf_set_int(conf, CONF_proxy_type, PROXY_NONE);
330 * Lifetime preparations.
332 signalpipe[0] = signalpipe[1] = -1;
333 if (life == LIFE_X11) {
334 struct X11Display *disp;
338 struct X11Connection *conn;
340 static const struct plug_function_table fn_table = {
349 display = getenv("DISPLAY");
351 fprintf(stderr, "pageant: no DISPLAY for -X mode\n");
354 disp = x11_setup_display(display, conf);
356 conn = snew(struct X11Connection);
357 conn->fn = &fn_table;
358 s = new_connection(sk_addr_dup(disp->addr),
359 disp->realhost, disp->port,
360 0, 1, 0, 0, (Plug)conn, conf);
361 if ((err = sk_socket_error(s)) != NULL) {
362 fprintf(stderr, "pageant: unable to connect to X server: %s", err);
365 greeting = x11_make_greeting('B', 11, 0, disp->localauthproto,
367 disp->localauthdatalen,
368 NULL, 0, &greetinglen);
369 sk_write(s, greeting, greetinglen);
370 smemclr(greeting, greetinglen);
373 pageant_fork_and_print_env();
374 } else if (life == LIFE_PERM) {
375 pageant_fork_and_print_env();
376 } else if (life == LIFE_DEBUG) {
377 pageant_print_env(getpid());
378 pageant_logfp = stdout;
379 } else if (life == LIFE_EXEC) {
385 * Set up the pipe we'll use to tell us about SIGCHLD.
387 if (pipe(signalpipe) < 0) {
391 putty_signal(SIGCHLD, sigchld);
397 } else if (pid == 0) {
398 setenv("SSH_AUTH_SOCK", socketname, TRUE);
399 setenv("SSH_AGENT_PID", dupprintf("%d", (int)agentpid), TRUE);
400 execvp(exec_args[0], exec_args);
404 termination_pid = pid;
409 * Now we've decided on our logging arrangements, pass them on to
412 pageant_listener_set_logfn(pl, NULL, pageant_logfp ? pageant_log : NULL);
414 now = GETTICKCOUNT();
416 while (!time_to_die) {
417 fd_set rset, wset, xset;
428 if (signalpipe[0] >= 0) {
429 FD_SET_MAX(signalpipe[0], maxfd, rset);
432 /* Count the currently active fds. */
434 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
435 fd = next_fd(&fdstate, &rwx)) i++;
437 /* Expand the fdlist buffer if necessary. */
440 fdlist = sresize(fdlist, fdsize, int);
444 * Add all currently open fds to the select sets, and store
445 * them in fdlist as well.
448 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
449 fd = next_fd(&fdstate, &rwx)) {
450 fdlist[fdcount++] = fd;
452 FD_SET_MAX(fd, maxfd, rset);
454 FD_SET_MAX(fd, maxfd, wset);
456 FD_SET_MAX(fd, maxfd, xset);
459 if (toplevel_callback_pending()) {
463 ret = select(maxfd, &rset, &wset, &xset, &tv);
464 } else if (run_timers(now, &next)) {
470 now = GETTICKCOUNT();
471 if (now - then > next - then)
475 tv.tv_sec = ticks / 1000;
476 tv.tv_usec = ticks % 1000 * 1000;
477 ret = select(maxfd, &rset, &wset, &xset, &tv);
481 now = GETTICKCOUNT();
483 ret = select(maxfd, &rset, &wset, &xset, NULL);
486 if (ret < 0 && errno == EINTR)
494 for (i = 0; i < fdcount; i++) {
497 * We must process exceptional notifications before
498 * ordinary readability ones, or we may go straight
499 * past the urgent marker.
501 if (FD_ISSET(fd, &xset))
502 select_result(fd, 4);
503 if (FD_ISSET(fd, &rset))
504 select_result(fd, 1);
505 if (FD_ISSET(fd, &wset))
506 select_result(fd, 2);
509 if (signalpipe[0] >= 0 && FD_ISSET(signalpipe[0], &rset)) {
511 if (read(signalpipe[0], c, 1) <= 0)
513 /* ignore its value; it'll be `x' */
517 pid = waitpid(-1, &status, WNOHANG);
520 if (pid == termination_pid)
525 run_toplevel_callbacks();
529 * When we come here, we're terminating, and should clean up our
530 * Unix socket file if possible.
532 if (unlink(socketname) < 0) {
533 fprintf(stderr, "pageant: %s: %s\n", socketname, strerror(errno));