]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxpgnt.c
4d2aa08eb1a5ba587d23c215240d887d5edc16bf
[PuTTY.git] / unix / uxpgnt.c
1 /*
2  * Unix Pageant, more or less similar to ssh-agent.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <signal.h>
10
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15
16 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
17 #include "putty.h"
18 #include "ssh.h"
19 #include "misc.h"
20 #include "pageant.h"
21
22 SockAddr unix_sock_addr(const char *path);
23 Socket new_unix_listener(SockAddr listenaddr, Plug plug);
24
25 void fatalbox(char *p, ...)
26 {
27     va_list ap;
28     fprintf(stderr, "FATAL ERROR: ");
29     va_start(ap, p);
30     vfprintf(stderr, p, ap);
31     va_end(ap);
32     fputc('\n', stderr);
33     exit(1);
34 }
35 void modalfatalbox(char *p, ...)
36 {
37     va_list ap;
38     fprintf(stderr, "FATAL ERROR: ");
39     va_start(ap, p);
40     vfprintf(stderr, p, ap);
41     va_end(ap);
42     fputc('\n', stderr);
43     exit(1);
44 }
45 void nonfatal(char *p, ...)
46 {
47     va_list ap;
48     fprintf(stderr, "ERROR: ");
49     va_start(ap, p);
50     vfprintf(stderr, p, ap);
51     va_end(ap);
52     fputc('\n', stderr);
53 }
54 void connection_fatal(void *frontend, char *p, ...)
55 {
56     va_list ap;
57     fprintf(stderr, "FATAL ERROR: ");
58     va_start(ap, p);
59     vfprintf(stderr, p, ap);
60     va_end(ap);
61     fputc('\n', stderr);
62     exit(1);
63 }
64 void cmdline_error(char *p, ...)
65 {
66     va_list ap;
67     fprintf(stderr, "pageant: ");
68     va_start(ap, p);
69     vfprintf(stderr, p, ap);
70     va_end(ap);
71     fputc('\n', stderr);
72     exit(1);
73 }
74
75 FILE *pageant_logfp = NULL;
76 void pageant_log(void *ctx, const char *fmt, va_list ap)
77 {
78     if (!pageant_logfp)
79         return;
80
81     fprintf(pageant_logfp, "pageant: ");
82     vfprintf(pageant_logfp, fmt, ap);
83     fprintf(pageant_logfp, "\n");
84 }
85
86 /*
87  * In Pageant our selects are synchronous, so these functions are
88  * empty stubs.
89  */
90 int uxsel_input_add(int fd, int rwx) { return 0; }
91 void uxsel_input_remove(int id) { }
92
93 /*
94  * More stubs.
95  */
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 log_eventlog(void *handle, const char *event) {}
105
106 /*
107  * Short description of parameters.
108  */
109 static void usage(void)
110 {
111     printf("Pageant: SSH agent\n");
112     printf("%s\n", ver);
113     printf("FIXME\n");
114     exit(1);
115 }
116
117 static void version(void)
118 {
119     printf("pageant: %s\n", ver);
120     exit(1);
121 }
122
123 void keylist_update(void)
124 {
125     /* Nothing needs doing in Unix Pageant */
126 }
127
128 #define PAGEANT_DIR_PREFIX "/tmp/pageant"
129
130 const char *const appname = "Pageant";
131
132 static int time_to_die = FALSE;
133
134 /* Stub functions to permit linking against x11fwd.c. These never get
135  * used, because in LIFE_X11 mode we connect to the X server using a
136  * straightforward Socket and don't try to create an ersatz SSH
137  * forwarding too. */
138 int sshfwd_write(struct ssh_channel *c, char *data, int len) { return 0; }
139 void sshfwd_write_eof(struct ssh_channel *c) { }
140 void sshfwd_unclean_close(struct ssh_channel *c, const char *err) { }
141 void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) {}
142 Conf *sshfwd_get_conf(struct ssh_channel *c) { return NULL; }
143 void sshfwd_x11_sharing_handover(struct ssh_channel *c,
144                                  void *share_cs, void *share_chan,
145                                  const char *peer_addr, int peer_port,
146                                  int endian, int protomajor, int protominor,
147                                  const void *initial_data, int initial_len) {}
148 void sshfwd_x11_is_local(struct ssh_channel *c) {}
149
150 /*
151  * These functions are part of the plug for our connection to the X
152  * display, so they do get called. They needn't actually do anything,
153  * except that x11_closing has to signal back to the main loop that
154  * it's time to terminate.
155  */
156 static void x11_log(Plug p, int type, SockAddr addr, int port,
157                     const char *error_msg, int error_code) {}
158 static int x11_receive(Plug plug, int urgent, char *data, int len) {return 0;}
159 static void x11_sent(Plug plug, int bufsize) {}
160 static int x11_closing(Plug plug, const char *error_msg, int error_code,
161                        int calling_back)
162 {
163     time_to_die = TRUE;
164     return 1;
165 }
166 struct X11Connection {
167     const struct plug_function_table *fn;
168 };
169
170 char *socketname;
171 void pageant_print_env(int pid)
172 {
173     printf("SSH_AUTH_SOCK=%s; export SSH_AUTH_SOCK;\n"
174            "SSH_AGENT_PID=%d; export SSH_AGENT_PID;\n",
175            socketname, (int)pid);
176 }
177
178 void pageant_fork_and_print_env(int retain_tty)
179 {
180     pid_t pid = fork();
181     if (pid == -1) {
182         perror("fork");
183         exit(1);
184     } else if (pid != 0) {
185         pageant_print_env(pid);
186         exit(0);
187     }
188
189     /*
190      * Having forked off, we now daemonise ourselves as best we can.
191      * It's good practice in general to setsid() ourself out of any
192      * process group we didn't want to be part of, and to chdir("/")
193      * to avoid holding any directories open that we don't need in
194      * case someone wants to umount them; also, we should definitely
195      * close standard output (because it will very likely be pointing
196      * at a pipe from which some parent process is trying to read our
197      * environment variable dump, so if we hold open another copy of
198      * it then that process will never finish reading). We close
199      * standard input too on general principles, but not standard
200      * error, since we might need to shout a panicky error message
201      * down that one.
202      */
203     if (chdir("/") < 0) {
204         /* should there be an error condition, nothing we can do about
205          * it anyway */
206     }
207     close(0);
208     close(1);
209     if (retain_tty) {
210         /* Get out of our previous process group, to avoid being
211          * blasted by passing signals. But keep our controlling tty,
212          * so we can keep checking to see if we still have one. */
213         setpgrp();
214     } else {
215         /* Do that, but also leave our entire session and detach from
216          * the controlling tty (if any). */
217         setsid();
218     }
219 }
220
221 int signalpipe[2];
222
223 void sigchld(int signum)
224 {
225     if (write(signalpipe[1], "x", 1) <= 0)
226         /* not much we can do about it */;
227 }
228
229 #define TTY_LIFE_POLL_INTERVAL (TICKSPERSEC * 30)
230 void *dummy_timer_ctx;
231 static void tty_life_timer(void *ctx, unsigned long now)
232 {
233     schedule_timer(TTY_LIFE_POLL_INTERVAL, tty_life_timer, &dummy_timer_ctx);
234 }
235
236 typedef enum {
237     KEYACT_AGENT_LOAD,
238     KEYACT_CLIENT_ADD,
239     KEYACT_CLIENT_DEL,
240     KEYACT_CLIENT_DEL_ALL,
241     KEYACT_CLIENT_LIST,
242     KEYACT_CLIENT_LIST_FULL,
243 } keyact;
244 struct cmdline_key_action {
245     struct cmdline_key_action *next;
246     keyact action;
247     const char *filename;
248 };
249
250 int is_agent_action(keyact action)
251 {
252     return action == KEYACT_AGENT_LOAD;
253 }
254
255 struct cmdline_key_action *keyact_head = NULL, *keyact_tail = NULL;
256
257 void add_keyact(keyact action, const char *filename)
258 {
259     struct cmdline_key_action *a = snew(struct cmdline_key_action);
260     a->action = action;
261     a->filename = filename;
262     a->next = NULL;
263     if (keyact_tail)
264         keyact_tail->next = a;
265     else
266         keyact_head = a;
267     keyact_tail = a;
268 }
269
270 char **exec_args = NULL;
271 enum {
272     LIFE_UNSPEC, LIFE_X11, LIFE_TTY, LIFE_DEBUG, LIFE_PERM, LIFE_EXEC
273 } life = LIFE_UNSPEC;
274 const char *display = NULL;
275
276 static char *askpass(const char *comment)
277 {
278     prompts_t *p = new_prompts(NULL);
279     int ret;
280
281     /*
282      * FIXME: if we don't have a terminal, and have to do this by X11,
283      * there's a big missing piece.
284      */
285
286     p->to_server = FALSE;
287     p->name = dupstr("Pageant passphrase prompt");
288     add_prompt(p,
289                dupprintf("Enter passphrase to load key '%s': ", comment),
290                FALSE);
291     ret = console_get_userpass_input(p, NULL, 0);
292     assert(ret >= 0);
293
294     if (!ret) {
295         perror("pageant: unable to read passphrase");
296         free_prompts(p);
297         return NULL;
298     } else {
299         char *passphrase = dupstr(p->prompts[0]->result);
300         free_prompts(p);
301         return passphrase;
302     }
303 }
304
305 static int unix_add_keyfile(const char *filename_str)
306 {
307     Filename *filename = filename_from_str(filename_str);
308     int status, ret;
309     char *err;
310
311     ret = TRUE;
312
313     /*
314      * Try without a passphrase.
315      */
316     status = pageant_add_keyfile(filename, NULL, &err);
317     if (status == PAGEANT_ACTION_OK) {
318         goto cleanup;
319     } else if (status == PAGEANT_ACTION_FAILURE) {
320         fprintf(stderr, "pageant: %s: %s\n", filename_str, err);
321         sfree(err);
322         ret = FALSE;
323         goto cleanup;
324     }
325
326     /*
327      * And now try prompting for a passphrase.
328      */
329     while (1) {
330         char *passphrase = askpass(err);
331         sfree(err);
332         if (!passphrase)
333             break;
334
335         status = pageant_add_keyfile(filename, passphrase, &err);
336
337         smemclr(passphrase, strlen(passphrase));
338         sfree(passphrase);
339         passphrase = NULL;
340
341         if (status == PAGEANT_ACTION_OK) {
342             goto cleanup;
343         } else if (status == PAGEANT_ACTION_FAILURE) {
344             fprintf(stderr, "pageant: %s: %s\n", filename_str, err);
345             sfree(err);
346             ret = FALSE;
347             goto cleanup;
348         }
349     }
350
351   cleanup:
352     sfree(err);
353     filename_free(filename);
354     return ret;
355 }
356
357 void key_list_callback(void *ctx, const char *fingerprint, const char *comment)
358 {
359     printf("%s %s\n", fingerprint, comment);
360 }
361
362 void run_client(void)
363 {
364     const struct cmdline_key_action *act;
365     int errors = FALSE;
366     char *retstr;
367
368     if (!agent_exists()) {
369         fprintf(stderr, "pageant: no agent running to talk to\n");
370         exit(1);
371     }
372
373     for (act = keyact_head; act; act = act->next) {
374         switch (act->action) {
375           case KEYACT_CLIENT_ADD:
376             if (!unix_add_keyfile(act->filename))
377                 errors = TRUE;
378             break;
379           case KEYACT_CLIENT_LIST:
380             if (pageant_enum_keys(key_list_callback, NULL, &retstr) ==
381                 PAGEANT_ACTION_FAILURE) {
382                 fprintf(stderr, "pageant: listing keys: %s\n", retstr);
383                 sfree(retstr);
384                 errors = TRUE;
385             }
386             break;
387           case KEYACT_CLIENT_DEL:
388           case KEYACT_CLIENT_DEL_ALL:
389           case KEYACT_CLIENT_LIST_FULL:
390             fprintf(stderr, "NYI\n");
391             errors = TRUE;
392             break;
393           default:
394             assert(0 && "Invalid client action found");
395         }
396     }
397
398     if (errors)
399         exit(1);
400 }
401
402 void run_agent(void)
403 {
404     const char *err;
405     char *username, *socketdir;
406     struct pageant_listen_state *pl;
407     Socket sock;
408     unsigned long now;
409     int *fdlist;
410     int fd;
411     int i, fdcount, fdsize, fdstate;
412     int termination_pid = -1;
413     int errors = FALSE;
414     Conf *conf;
415     const struct cmdline_key_action *act;
416
417     fdlist = NULL;
418     fdcount = fdsize = 0;
419
420     pageant_init();
421
422     /*
423      * Start by loading any keys provided on the command line.
424      */
425     for (act = keyact_head; act; act = act->next) {
426         assert(act->action == KEYACT_AGENT_LOAD);
427         if (!unix_add_keyfile(act->filename))
428             errors = TRUE;
429     }
430     if (errors)
431         exit(1);
432
433     /*
434      * Set up a listening socket and run Pageant on it.
435      */
436     username = get_username();
437     socketdir = dupprintf("%s.%s", PAGEANT_DIR_PREFIX, username);
438     sfree(username);
439     assert(*socketdir == '/');
440     if ((err = make_dir_and_check_ours(socketdir)) != NULL) {
441         fprintf(stderr, "pageant: %s: %s\n", socketdir, err);
442         exit(1);
443     }
444     socketname = dupprintf("%s/pageant.%d", socketdir, (int)getpid());
445     pl = pageant_listener_new();
446     sock = new_unix_listener(unix_sock_addr(socketname), (Plug)pl);
447     if ((err = sk_socket_error(sock)) != NULL) {
448         fprintf(stderr, "pageant: %s: %s\n", socketname, err);
449         exit(1);
450     }
451     pageant_listener_got_socket(pl, sock);
452
453     conf = conf_new();
454     conf_set_int(conf, CONF_proxy_type, PROXY_NONE);
455
456     /*
457      * Lifetime preparations.
458      */
459     signalpipe[0] = signalpipe[1] = -1;
460     if (life == LIFE_X11) {
461         struct X11Display *disp;
462         void *greeting;
463         int greetinglen;
464         Socket s;
465         struct X11Connection *conn;
466
467         static const struct plug_function_table fn_table = {
468             x11_log,
469             x11_closing,
470             x11_receive,
471             x11_sent,
472             NULL
473         };
474
475         if (!display)
476             display = getenv("DISPLAY");
477         if (!display) {
478             fprintf(stderr, "pageant: no DISPLAY for -X mode\n");
479             exit(1);
480         }
481         disp = x11_setup_display(display, conf);
482
483         conn = snew(struct X11Connection);
484         conn->fn = &fn_table;
485         s = new_connection(sk_addr_dup(disp->addr),
486                            disp->realhost, disp->port,
487                            0, 1, 0, 0, (Plug)conn, conf);
488         if ((err = sk_socket_error(s)) != NULL) {
489             fprintf(stderr, "pageant: unable to connect to X server: %s", err);
490             exit(1);
491         }
492         greeting = x11_make_greeting('B', 11, 0, disp->localauthproto,
493                                      disp->localauthdata,
494                                      disp->localauthdatalen,
495                                      NULL, 0, &greetinglen);
496         sk_write(s, greeting, greetinglen);
497         smemclr(greeting, greetinglen);
498         sfree(greeting);
499
500         pageant_fork_and_print_env(FALSE);
501     } else if (life == LIFE_TTY) {
502         schedule_timer(TTY_LIFE_POLL_INTERVAL,
503                        tty_life_timer, &dummy_timer_ctx);
504         pageant_fork_and_print_env(TRUE);
505     } else if (life == LIFE_PERM) {
506         pageant_fork_and_print_env(FALSE);
507     } else if (life == LIFE_DEBUG) {
508         pageant_print_env(getpid());
509         pageant_logfp = stdout;
510     } else if (life == LIFE_EXEC) {
511         pid_t agentpid, pid;
512
513         agentpid = getpid();
514
515         /*
516          * Set up the pipe we'll use to tell us about SIGCHLD.
517          */
518         if (pipe(signalpipe) < 0) {
519             perror("pipe");
520             exit(1);
521         }
522         putty_signal(SIGCHLD, sigchld);
523
524         pid = fork();
525         if (pid < 0) {
526             perror("fork");
527             exit(1);
528         } else if (pid == 0) {
529             setenv("SSH_AUTH_SOCK", socketname, TRUE);
530             setenv("SSH_AGENT_PID", dupprintf("%d", (int)agentpid), TRUE);
531             execvp(exec_args[0], exec_args);
532             perror("exec");
533             _exit(127);
534         } else {
535             termination_pid = pid;
536         }
537     }
538
539     /*
540      * Now we've decided on our logging arrangements, pass them on to
541      * pageant.c.
542      */
543     pageant_listener_set_logfn(pl, NULL, pageant_logfp ? pageant_log : NULL);
544
545     now = GETTICKCOUNT();
546
547     while (!time_to_die) {
548         fd_set rset, wset, xset;
549         int maxfd;
550         int rwx;
551         int ret;
552         unsigned long next;
553
554         FD_ZERO(&rset);
555         FD_ZERO(&wset);
556         FD_ZERO(&xset);
557         maxfd = 0;
558
559         if (signalpipe[0] >= 0) {
560             FD_SET_MAX(signalpipe[0], maxfd, rset);
561         }
562
563         /* Count the currently active fds. */
564         i = 0;
565         for (fd = first_fd(&fdstate, &rwx); fd >= 0;
566              fd = next_fd(&fdstate, &rwx)) i++;
567
568         /* Expand the fdlist buffer if necessary. */
569         if (i > fdsize) {
570             fdsize = i + 16;
571             fdlist = sresize(fdlist, fdsize, int);
572         }
573
574         /*
575          * Add all currently open fds to the select sets, and store
576          * them in fdlist as well.
577          */
578         fdcount = 0;
579         for (fd = first_fd(&fdstate, &rwx); fd >= 0;
580              fd = next_fd(&fdstate, &rwx)) {
581             fdlist[fdcount++] = fd;
582             if (rwx & 1)
583                 FD_SET_MAX(fd, maxfd, rset);
584             if (rwx & 2)
585                 FD_SET_MAX(fd, maxfd, wset);
586             if (rwx & 4)
587                 FD_SET_MAX(fd, maxfd, xset);
588         }
589
590         if (toplevel_callback_pending()) {
591             struct timeval tv;
592             tv.tv_sec = 0;
593             tv.tv_usec = 0;
594             ret = select(maxfd, &rset, &wset, &xset, &tv);
595         } else if (run_timers(now, &next)) {
596             unsigned long then;
597             long ticks;
598             struct timeval tv;
599
600             then = now;
601             now = GETTICKCOUNT();
602             if (now - then > next - then)
603                 ticks = 0;
604             else
605                 ticks = next - now;
606             tv.tv_sec = ticks / 1000;
607             tv.tv_usec = ticks % 1000 * 1000;
608             ret = select(maxfd, &rset, &wset, &xset, &tv);
609             if (ret == 0)
610                 now = next;
611             else
612                 now = GETTICKCOUNT();
613         } else {
614             ret = select(maxfd, &rset, &wset, &xset, NULL);
615         }
616
617         if (ret < 0 && errno == EINTR)
618             continue;
619
620         if (ret < 0) {
621             perror("select");
622             exit(1);
623         }
624
625         if (life == LIFE_TTY) {
626             /*
627              * Every time we wake up (whether it was due to tty_timer
628              * elapsing or for any other reason), poll to see if we
629              * still have a controlling terminal. If we don't, then
630              * our containing tty session has ended, so it's time to
631              * clean up and leave.
632              */
633             int fd = open("/dev/tty", O_RDONLY);
634             if (fd < 0) {
635                 if (errno != ENXIO) {
636                     perror("/dev/tty: open");
637                     exit(1);
638                 }
639                 time_to_die = TRUE;
640                 break;
641             } else {
642                 close(fd);
643             }
644         }
645
646         for (i = 0; i < fdcount; i++) {
647             fd = fdlist[i];
648             /*
649              * We must process exceptional notifications before
650              * ordinary readability ones, or we may go straight
651              * past the urgent marker.
652              */
653             if (FD_ISSET(fd, &xset))
654                 select_result(fd, 4);
655             if (FD_ISSET(fd, &rset))
656                 select_result(fd, 1);
657             if (FD_ISSET(fd, &wset))
658                 select_result(fd, 2);
659         }
660
661         if (signalpipe[0] >= 0 && FD_ISSET(signalpipe[0], &rset)) {
662             char c[1];
663             if (read(signalpipe[0], c, 1) <= 0)
664                 /* ignore error */;
665             /* ignore its value; it'll be `x' */
666             while (1) {
667                 int status;
668                 pid_t pid;
669                 pid = waitpid(-1, &status, WNOHANG);
670                 if (pid <= 0)
671                     break;
672                 if (pid == termination_pid)
673                     time_to_die = TRUE;
674             }
675         }
676
677         run_toplevel_callbacks();
678     }
679
680     /*
681      * When we come here, we're terminating, and should clean up our
682      * Unix socket file if possible.
683      */
684     if (unlink(socketname) < 0) {
685         fprintf(stderr, "pageant: %s: %s\n", socketname, strerror(errno));
686         exit(1);
687     }
688 }
689
690 int main(int argc, char **argv)
691 {
692     int doing_opts = TRUE;
693     keyact curr_keyact = KEYACT_AGENT_LOAD;
694
695     /*
696      * Process the command line.
697      */
698     while (--argc > 0) {
699         char *p = *++argv;
700         if (*p == '-' && doing_opts) {
701             if (!strcmp(p, "-V") || !strcmp(p, "--version")) {
702                 version();
703             } else if (!strcmp(p, "--help")) {
704                 usage();
705                 exit(0);
706             } else if (!strcmp(p, "-v")) {
707                 pageant_logfp = stderr;
708             } else if (!strcmp(p, "-a")) {
709                 curr_keyact = KEYACT_CLIENT_ADD;
710             } else if (!strcmp(p, "-d")) {
711                 curr_keyact = KEYACT_CLIENT_DEL;
712             } else if (!strcmp(p, "-D")) {
713                 add_keyact(KEYACT_CLIENT_DEL_ALL, NULL);
714             } else if (!strcmp(p, "-l")) {
715                 add_keyact(KEYACT_CLIENT_LIST, NULL);
716             } else if (!strcmp(p, "-L")) {
717                 add_keyact(KEYACT_CLIENT_LIST_FULL, NULL);
718             } else if (!strcmp(p, "-X")) {
719                 life = LIFE_X11;
720             } else if (!strcmp(p, "-T")) {
721                 life = LIFE_TTY;
722             } else if (!strcmp(p, "--debug")) {
723                 life = LIFE_DEBUG;
724             } else if (!strcmp(p, "--permanent")) {
725                 life = LIFE_PERM;
726             } else if (!strcmp(p, "--exec")) {
727                 life = LIFE_EXEC;
728                 /* Now all subsequent arguments go to the exec command. */
729                 if (--argc > 0) {
730                     exec_args = ++argv;
731                     argc = 0;          /* force end of option processing */
732                 } else {
733                     fprintf(stderr, "pageant: expected a command "
734                             "after --exec\n");
735                     exit(1);
736                 }
737             } else if (!strcmp(p, "--")) {
738                 doing_opts = FALSE;
739             }
740         } else {
741             /*
742              * Non-option arguments (apart from those after --exec,
743              * which are treated specially above) are interpreted as
744              * the names of private key files to either add or delete
745              * from an agent.
746              */
747             add_keyact(curr_keyact, p);
748         }
749     }
750
751     if (life == LIFE_EXEC && !exec_args) {
752         fprintf(stderr, "pageant: expected a command with --exec\n");
753         exit(1);
754     }
755
756     /*
757      * Block SIGPIPE, so that we'll get EPIPE individually on
758      * particular network connections that go wrong.
759      */
760     putty_signal(SIGPIPE, SIG_IGN);
761
762     sk_init();
763     uxsel_init();
764
765     /*
766      * Now distinguish our two main running modes. Either we're
767      * actually starting up an agent, in which case we should have a
768      * lifetime mode, and no key actions of KEYACT_CLIENT_* type; or
769      * else we're contacting an existing agent to add or remove keys,
770      * in which case we should have no lifetime mode, and no key
771      * actions of KEYACT_AGENT_* type.
772      */
773     {
774         int has_agent_actions = FALSE;
775         int has_client_actions = FALSE;
776         int has_lifetime = FALSE;
777         const struct cmdline_key_action *act;
778
779         for (act = keyact_head; act; act = act->next) {
780             if (is_agent_action(act->action))
781                 has_agent_actions = TRUE;
782             else
783                 has_client_actions = TRUE;
784         }
785         if (life != LIFE_UNSPEC)
786             has_lifetime = TRUE;
787
788         if (has_lifetime && has_client_actions) {
789             fprintf(stderr, "pageant: client key actions (-a, -d, -D, -l, -L)"
790                     " do not go with an agent lifetime option\n");
791             exit(1);
792         }
793         if (!has_lifetime && has_agent_actions) {
794             fprintf(stderr, "pageant: expected an agent lifetime option with"
795                     " bare key file arguments\n");
796             exit(1);
797         }
798         if (!has_lifetime && !has_client_actions) {
799             fprintf(stderr, "pageant: expected an agent lifetime option"
800                     " or a client key action\n");
801             exit(1);
802         }
803
804         if (has_lifetime) {
805             run_agent();
806         } else if (has_client_actions) {
807             run_client();
808         }
809     }
810
811     return 0;
812 }