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 int pageant_logging = FALSE;
75 void pageant_log(void *ctx, const char *fmt, ...)
83 vfprintf(stderr, fmt, ap);
84 fprintf(stderr, "\n");
89 * In Pageant our selects are synchronous, so these functions are
92 int uxsel_input_add(int fd, int rwx) { return 0; }
93 void uxsel_input_remove(int id) { }
98 void logevent(void *frontend, const char *string) {}
99 void random_save_seed(void) {}
100 void random_destroy_seed(void) {}
101 void noise_ultralight(unsigned long data) {}
102 char *platform_default_s(const char *name) { return NULL; }
103 int platform_default_i(const char *name, int def) { return def; }
104 FontSpec *platform_default_fontspec(const char *name) { return fontspec_new(""); }
105 Filename *platform_default_filename(const char *name) { return filename_from_str(""); }
106 char *x_get_default(const char *key) { return NULL; }
107 void old_keyfile_warning(void) {}
108 void timer_change_notify(unsigned long next) {}
111 * Short description of parameters.
113 static void usage(void)
115 printf("Pageant: SSH agent\n");
121 static void version(void)
123 printf("pageant: %s\n", ver);
127 void keylist_update(void)
129 /* Nothing needs doing in Unix Pageant */
132 #define PAGEANT_DIR_PREFIX "/tmp/pageant"
134 const char *const appname = "Pageant";
138 char *platform_get_x_display(void) {
139 return dupstr(getenv("DISPLAY"));
141 int sshfwd_write(struct ssh_channel *c, char *data, int len) { return 0; }
142 void sshfwd_write_eof(struct ssh_channel *c) { /* FIXME: notify main loop instead */ exit(0); }
143 void sshfwd_unclean_close(struct ssh_channel *c, const char *err) { /* FIXME: notify main loop instead */ exit(1); }
144 void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) {}
145 Conf *sshfwd_get_conf(struct ssh_channel *c) { return conf; }
146 void sshfwd_x11_sharing_handover(struct ssh_channel *c,
147 void *share_cs, void *share_chan,
148 const char *peer_addr, int peer_port,
149 int endian, int protomajor, int protominor,
150 const void *initial_data, int initial_len) {}
151 void sshfwd_x11_is_local(struct ssh_channel *c) {}
152 static void x11_log(Plug p, int type, SockAddr addr, int port,
153 const char *error_msg, int error_code) {}
154 static int x11_closing(Plug plug, const char *error_msg, int error_code,
155 int calling_back) { /* FIXME: notify main loop instead */ exit(0); }
156 static int x11_receive(Plug plug, int urgent, char *data, int len) { return 0; }
157 static void x11_sent(Plug plug, int bufsize) {}
158 struct X11Connection {
159 const struct plug_function_table *fn;
163 void pageant_print_env(int pid)
165 printf("SSH_AUTH_SOCK=%s; export SSH_AUTH_SOCK;\n"
166 "SSH_AGENT_PID=%d; export SSH_AGENT_PID;\n",
167 socketname, (int)pid);
170 void pageant_fork_and_print_env(void)
176 } else if (pid != 0) {
177 pageant_print_env(pid);
182 * Having forked off, we now daemonise ourselves as best we can.
183 * It's good practice in general to setsid() ourself out of any
184 * process group we didn't want to be part of, and to chdir("/")
185 * to avoid holding any directories open that we don't need in
186 * case someone wants to umount them; also, we should definitely
187 * close standard output (because it will very likely be pointing
188 * at a pipe from which some parent process is trying to read our
189 * environment variable dump, so if we hold open another copy of
190 * it then that process will never finish reading). We close
191 * standard input too on general principles, but not standard
192 * error, since we might need to shout a panicky error message
195 if (chdir("/") < 0) {
196 /* should there be an error condition, nothing we can do about
206 void sigchld(int signum)
208 if (write(signalpipe[1], "x", 1) <= 0)
209 /* not much we can do about it */;
212 int main(int argc, char **argv)
216 int i, fdcount, fdsize, fdstate;
219 char *username, *socketdir;
221 struct pageant_listen_state *pl;
224 LIFE_UNSPEC, LIFE_X11, LIFE_DEBUG, LIFE_PERM, LIFE_EXEC
225 } life = LIFE_UNSPEC;
226 const char *display = NULL;
227 int doing_opts = TRUE;
228 char **exec_args = NULL;
229 int termination_pid = -1;
232 fdcount = fdsize = 0;
236 * Process the command line.
240 if (*p == '-' && doing_opts) {
241 if (!strcmp(p, "-V") || !strcmp(p, "--version")) {
243 } else if (!strcmp(p, "--help")) {
246 } else if (!strcmp(p, "-X")) {
248 } else if (!strcmp(p, "--debug")) {
250 } else if (!strcmp(p, "--permanent")) {
252 } else if (!strcmp(p, "--exec")) {
254 } else if (!strcmp(p, "--")) {
258 if (life == LIFE_EXEC) {
260 break; /* everything else is now args to the exec command */
262 fprintf(stderr, "pageant: unexpected argument '%s'\n", p);
271 if (life == LIFE_UNSPEC) {
272 fprintf(stderr, "pageant: expected a lifetime option\n");
275 if (life == LIFE_EXEC && !exec_args) {
276 fprintf(stderr, "pageant: expected a command with --exec\n");
281 * Block SIGPIPE, so that we'll get EPIPE individually on
282 * particular network connections that go wrong.
284 putty_signal(SIGPIPE, SIG_IGN);
290 * Set up a listening socket and run Pageant on it.
292 username = get_username();
293 socketdir = dupprintf("%s.%s", PAGEANT_DIR_PREFIX, username);
295 assert(*socketdir == '/');
296 if ((err = make_dir_and_check_ours(socketdir)) != NULL) {
297 fprintf(stderr, "pageant: %s: %s\n", socketdir, err);
300 socketname = dupprintf("%s/pageant.%d", socketdir, (int)getpid());
303 pl = pageant_listener_new(NULL, pageant_log);
304 sock = new_unix_listener(unix_sock_addr(socketname), (Plug)pl);
305 if ((err = sk_socket_error(sock)) != NULL) {
306 fprintf(stderr, "pageant: %s: %s\n", socketname, err);
309 pageant_listener_got_socket(pl, sock);
312 conf_set_int(conf, CONF_proxy_type, PROXY_NONE);
315 * Lifetime preparations.
317 signalpipe[0] = signalpipe[1] = -1;
318 if (life == LIFE_X11) {
319 struct X11Display *disp;
323 struct X11Connection *conn;
325 static const struct plug_function_table fn_table = {
334 display = getenv("DISPLAY");
336 fprintf(stderr, "pageant: no DISPLAY for -X mode\n");
339 disp = x11_setup_display(display, conf);
341 conn = snew(struct X11Connection);
342 conn->fn = &fn_table;
343 s = new_connection(sk_addr_dup(disp->addr),
344 disp->realhost, disp->port,
345 0, 1, 0, 0, (Plug)conn, conf);
346 if ((err = sk_socket_error(s)) != NULL) {
347 fprintf(stderr, "pageant: unable to connect to X server: %s", err);
350 greeting = x11_make_greeting('B', 11, 0, disp->localauthproto,
352 disp->localauthdatalen,
353 NULL, 0, &greetinglen);
354 sk_write(s, greeting, greetinglen);
355 smemclr(greeting, greetinglen);
358 pageant_fork_and_print_env();
359 } else if (life == LIFE_PERM) {
360 pageant_fork_and_print_env();
361 } else if (life == LIFE_DEBUG) {
362 pageant_print_env(getpid());
363 pageant_logging = TRUE;
364 } else if (life == LIFE_EXEC) {
370 * Set up the pipe we'll use to tell us about SIGCHLD.
372 if (pipe(signalpipe) < 0) {
376 putty_signal(SIGCHLD, sigchld);
382 } else if (pid == 0) {
383 setenv("SSH_AUTH_SOCK", socketname, TRUE);
384 setenv("SSH_AGENT_PID", dupprintf("%d", (int)agentpid), TRUE);
385 execvp(exec_args[0], exec_args);
389 termination_pid = pid;
393 now = GETTICKCOUNT();
396 fd_set rset, wset, xset;
407 if (signalpipe[0] >= 0) {
408 FD_SET_MAX(signalpipe[0], maxfd, rset);
411 /* Count the currently active fds. */
413 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
414 fd = next_fd(&fdstate, &rwx)) i++;
416 /* Expand the fdlist buffer if necessary. */
419 fdlist = sresize(fdlist, fdsize, int);
423 * Add all currently open fds to the select sets, and store
424 * them in fdlist as well.
427 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
428 fd = next_fd(&fdstate, &rwx)) {
429 fdlist[fdcount++] = fd;
431 FD_SET_MAX(fd, maxfd, rset);
433 FD_SET_MAX(fd, maxfd, wset);
435 FD_SET_MAX(fd, maxfd, xset);
438 if (toplevel_callback_pending()) {
442 ret = select(maxfd, &rset, &wset, &xset, &tv);
443 } else if (run_timers(now, &next)) {
449 now = GETTICKCOUNT();
450 if (now - then > next - then)
454 tv.tv_sec = ticks / 1000;
455 tv.tv_usec = ticks % 1000 * 1000;
456 ret = select(maxfd, &rset, &wset, &xset, &tv);
460 now = GETTICKCOUNT();
462 ret = select(maxfd, &rset, &wset, &xset, NULL);
465 if (ret < 0 && errno == EINTR)
473 for (i = 0; i < fdcount; i++) {
476 * We must process exceptional notifications before
477 * ordinary readability ones, or we may go straight
478 * past the urgent marker.
480 if (FD_ISSET(fd, &xset))
481 select_result(fd, 4);
482 if (FD_ISSET(fd, &rset))
483 select_result(fd, 1);
484 if (FD_ISSET(fd, &wset))
485 select_result(fd, 2);
488 if (signalpipe[0] >= 0 && FD_ISSET(signalpipe[0], &rset)) {
490 if (read(signalpipe[0], c, 1) <= 0)
492 /* ignore its value; it'll be `x' */
496 pid = waitpid(-1, &status, WNOHANG);
499 if (pid == termination_pid)
504 run_toplevel_callbacks();