]> diplodocus.org Git - nmh/blob - uip/imaptest.c
befedabbfb0b82f8fcc921820cbd58b6cc07a15e
[nmh] / uip / imaptest.c
1 /* imaptest.c -- A program to send commands to an IMAP server
2 *
3 * This code is Copyright (c) 2017, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include <h/netsec.h>
11 #include <stdarg.h>
12 #include <sys/time.h>
13 #include "h/done.h"
14 #include "sbr/base64.h"
15
16 #define IMAPTEST_SWITCHES \
17 X("host hostname", 0, HOSTSW) \
18 X("user username", 0, USERSW) \
19 X("port name/number", 0, PORTSW) \
20 X("snoop", 0, SNOOPSW) \
21 X("nosnoop", 0, NOSNOOPSW) \
22 X("sasl", 0, SASLSW) \
23 X("nosasl", 0, NOSASLSW) \
24 X("saslmech", 0, SASLMECHSW) \
25 X("authservice", 0, AUTHSERVICESW) \
26 X("tls", 0, TLSSW) \
27 X("notls", 0, NOTLSSW) \
28 X("initialtls", 0, INITIALTLSSW) \
29 X("append filename", 0, APPENDSW) \
30 X("timestamp", 0, TIMESTAMPSW) \
31 X("notimestamp", 0, NOTIMESTAMPSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(IMAPTEST);
37 #undef X
38
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(IMAPTEST, switches);
41 #undef X
42
43 struct imap_msg;
44
45 struct imap_msg {
46 char *command; /* Command to send */
47 bool queue; /* If true, queue for later delivery */
48 struct imap_msg *next; /* Next pointer */
49 };
50
51 struct imap_msg *msgqueue_head = NULL;
52 struct imap_msg *msgqueue_tail = NULL;
53
54 struct imap_cmd;
55
56 struct imap_cmd {
57 char tag[16]; /* Command tag */
58 struct timeval start; /* Time command was sent */
59 char prefix[64]; /* Command prefix */
60 struct imap_cmd *next; /* Next pointer */
61 };
62
63 static struct imap_cmd *cmdqueue = NULL;
64
65 static char *saslmechs = NULL;
66 svector_t imap_capabilities = NULL;
67
68 static int imap_sasl_callback(enum sasl_message_type, unsigned const char *,
69 unsigned int, unsigned char **, unsigned int *,
70 void *, char **);
71
72 static void parse_capability(const char *, unsigned int len);
73 static int capability_set(const char *);
74 static void clear_capability(void);
75 static int have_capability(void);
76 static int send_imap_command(netsec_context *, bool noflush, char **errstr,
77 const char *fmt, ...) CHECK_PRINTF(4, 5);
78 static int get_imap_response(netsec_context *, const char *token,
79 char **tokenresp, char **status, int failerr,
80 char **errstr);
81
82 static void ts_report(struct timeval *tv, const char *fmt, ...)
83 CHECK_PRINTF(2, 3);
84 static void imap_negotiate_tls(netsec_context *);
85
86 static void add_msg(bool queue, const char *fmt, ...) CHECK_PRINTF(2, 3);
87
88 static bool timestamp = false;
89
90 int
91 main (int argc, char **argv)
92 {
93 bool sasl = false, tls = false, initialtls = false;
94 bool snoop = false;
95 int fd;
96 char *saslmech = NULL, *host = NULL, *port = "143", *user = NULL;
97 char *cp, **argp, buf[BUFSIZ], *oauth_svc = NULL, *errstr, **arguments, *p;
98 netsec_context *nsc = NULL;
99 struct imap_msg *imsg;
100 size_t len;
101 struct timeval tv_start, tv_connect, tv_auth;
102
103 if (nmh_init(argv[0], true, true)) { return 1; }
104
105 arguments = getarguments (invo_name, argc, argv, 1);
106 argp = arguments;
107
108 while ((cp = *argp++)) {
109 if (*cp == '-') {
110 switch (smatch (++cp, switches)) {
111 case AMBIGSW:
112 ambigsw (cp, switches);
113 done (1);
114 case UNKWNSW:
115 die("-%s unknown", cp);
116
117 case HELPSW:
118 snprintf (buf, sizeof(buf), "%s [switches] +folder command "
119 "[command ...]", invo_name);
120 print_help (buf, switches, 1);
121 done (0);
122 case VERSIONSW:
123 print_version(invo_name);
124 done (0);
125
126 case HOSTSW:
127 if (!(host = *argp++) || *host == '-')
128 die("missing argument to %s", argp[-2]);
129 continue;
130 case PORTSW:
131 if (!(port = *argp++) || *port == '-')
132 die("missing argument to %s", argp[-2]);
133 continue;
134 case USERSW:
135 if (!(user = *argp++) || *user == '-')
136 die("missing argument to %s", argp[-2]);
137 continue;
138
139 case SNOOPSW:
140 snoop = true;
141 continue;
142 case NOSNOOPSW:
143 snoop = false;
144 continue;
145 case SASLSW:
146 sasl = true;
147 continue;
148 case NOSASLSW:
149 sasl = false;
150 continue;
151 case SASLMECHSW:
152 if (!(saslmech = *argp++) || *saslmech == '-')
153 die("missing argument to %s", argp[-2]);
154 continue;
155 case AUTHSERVICESW:
156 if (!(oauth_svc = *argp++) || *oauth_svc == '-')
157 die("missing argument to %s", argp[-2]);
158 continue;
159 case TLSSW:
160 tls = true;
161 initialtls = false;
162 continue;
163 case INITIALTLSSW:
164 tls = false;
165 initialtls = true;
166 continue;
167 case NOTLSSW:
168 tls = false;
169 initialtls = false;
170 continue;
171 case TIMESTAMPSW:
172 timestamp = true;
173 continue;
174 case NOTIMESTAMPSW:
175 timestamp = false;
176 continue;
177 }
178 } else if (*cp == '+') {
179 if (*(cp + 1) == '\0')
180 die("Invalid null folder name");
181 add_msg(0, "SELECT \"%s\"", cp + 1);
182 } else {
183 add_msg(0, "%s", cp);
184 }
185 }
186
187 if (! host)
188 die("A hostname must be given with -host");
189
190 nsc = netsec_init();
191
192 if (user)
193 netsec_set_userid(nsc, user);
194
195 netsec_set_hostname(nsc, host);
196
197 if (snoop)
198 netsec_set_snoop(nsc, 1);
199
200 if (oauth_svc) {
201 if (netsec_set_oauth_service(nsc, oauth_svc) != OK) {
202 die("OAuth2 not supported");
203 }
204 }
205
206 if (timestamp)
207 gettimeofday(&tv_start, NULL);
208
209 if ((fd = client(host, port, buf, sizeof(buf), snoop)) == NOTOK)
210 die("Connect failed: %s", buf);
211
212 if (timestamp) {
213 ts_report(&tv_start, "Connect time");
214 gettimeofday(&tv_connect, NULL);
215 }
216
217 netsec_set_fd(nsc, fd, fd);
218 netsec_set_snoop(nsc, snoop);
219
220 if (initialtls || tls) {
221 if (netsec_set_tls(nsc, 1, 0, &errstr) != OK)
222 die("%s", errstr);
223
224 if (initialtls)
225 imap_negotiate_tls(nsc);
226 }
227
228 if (sasl) {
229 if (netsec_set_sasl_params(nsc, "imap", saslmech, imap_sasl_callback,
230 nsc, &errstr) != OK)
231 die("%s", errstr);
232 }
233
234 if ((cp = netsec_readline(nsc, &len, &errstr)) == NULL) {
235 die("%s", errstr);
236 }
237
238 if (has_prefix(cp, "* BYE")) {
239 fprintf(stderr, "Connection rejected: %s\n", cp + 5);
240 goto finish;
241 }
242
243 if (!has_prefix(cp, "* OK") && !has_prefix(cp, "* PREAUTH")) {
244 fprintf(stderr, "Invalid server response: %s\n", cp);
245 goto finish;
246 }
247
248 if ((p = strchr(cp + 2, ' ')) && *(p + 1) != '\0' &&
249 has_prefix(p + 1, "[CAPABILITY ")) {
250 /*
251 * Parse the capability list out to the end
252 */
253 char *q;
254 p += 13; /* 1 + [CAPABILITY + space */
255
256 if (!(q = strchr(p, ']'))) {
257 fprintf(stderr, "Cannot find end of CAPABILITY announcement\n");
258 goto finish;
259 }
260
261 parse_capability(p, q - p);
262 } else {
263 char *capstring;
264
265 if (send_imap_command(nsc, false, &errstr, "CAPABILITY") != OK) {
266 fprintf(stderr, "Unable to send CAPABILITY command: %s\n", errstr);
267 goto finish;
268 }
269
270 if (get_imap_response(nsc, "CAPABILITY", &capstring, NULL, 1,
271 &errstr) != OK) {
272 fprintf(stderr, "Cannot get CAPABILITY response: %s\n", errstr);
273 goto finish;
274 }
275
276 if (! capstring) {
277 fprintf(stderr, "No CAPABILITY response seen\n");
278 goto finish;
279 }
280
281 parse_capability(capstring, strlen(capstring));
282 free(capstring);
283 }
284
285 if (tls) {
286 if (!capability_set("STARTTLS")) {
287 fprintf(stderr, "Requested STARTTLS with -tls, but IMAP server "
288 "has no support for STARTTLS\n");
289 goto finish;
290 }
291 if (send_imap_command(nsc, false, &errstr, "STARTTLS") != OK) {
292 fprintf(stderr, "Unable to issue STARTTLS: %s\n", errstr);
293 goto finish;
294 }
295
296 if (get_imap_response(nsc, NULL, NULL, NULL, 1, &errstr) != OK) {
297 fprintf(stderr, "STARTTLS failed: %s\n", errstr);
298 goto finish;
299 }
300
301 imap_negotiate_tls(nsc);
302 }
303
304 if (sasl) {
305 if (netsec_negotiate_sasl(nsc, saslmechs, &errstr) != OK) {
306 fprintf(stderr, "SASL negotiation failed: %s\n", errstr);
307 goto finish;
308 }
309 /*
310 * Sigh. If we negotiated a SSF AND we got a capability response
311 * as part of the AUTHENTICATE OK message, we can't actually trust
312 * it because it's not protected at that point. So discard the
313 * capability list and we will generate a fresh CAPABILITY command
314 * later.
315 */
316 if (netsec_get_sasl_ssf(nsc) > 0) {
317 clear_capability();
318 }
319 } else {
320 if (capability_set("LOGINDISABLED")) {
321 fprintf(stderr, "User did not request SASL, but LOGIN "
322 "is disabled\n");
323 goto finish;
324 }
325 }
326
327 if (!have_capability()) {
328 char *capstring;
329
330 if (send_imap_command(nsc, false, &errstr, "CAPABILITY") != OK) {
331 fprintf(stderr, "Unable to send CAPABILITY command: %s\n", errstr);
332 goto finish;
333 }
334
335 if (get_imap_response(nsc, "CAPABILITY", &capstring, NULL, 1,
336 &errstr) != OK) {
337 fprintf(stderr, "Cannot get CAPABILITY response: %s\n", errstr);
338 goto finish;
339 }
340
341 if (! capstring) {
342 fprintf(stderr, "No CAPABILITY response seen\n");
343 goto finish;
344 }
345
346 parse_capability(capstring, strlen(capstring));
347 free(capstring);
348 }
349
350 if (timestamp) {
351 ts_report(&tv_connect, "Authentication time");
352 gettimeofday(&tv_auth, NULL);
353 }
354
355 while (msgqueue_head != NULL) {
356 imsg = msgqueue_head;
357
358 if (send_imap_command(nsc, imsg->queue, &errstr, "%s",
359 imsg->command) != OK) {
360 fprintf(stderr, "Cannot send command \"%s\": %s\n", imsg->command,
361 errstr);
362 free(errstr);
363 goto finish;
364 }
365
366 if (! imsg->queue) {
367 if (get_imap_response(nsc, NULL, NULL, NULL, 0, &errstr) != OK) {
368 fprintf(stderr, "Unable to get response for command "
369 "\"%s\": %s\n", imsg->command, errstr);
370 goto finish;
371 }
372 }
373
374 msgqueue_head = imsg->next;
375
376 free(imsg->command);
377 free(imsg);
378 }
379
380 if (timestamp)
381 ts_report(&tv_auth, "Total command execution time");
382
383 send_imap_command(nsc, 0, NULL, "LOGOUT");
384 get_imap_response(nsc, NULL, NULL, NULL, 0, NULL);
385
386 finish:
387 netsec_shutdown(nsc);
388
389 if (timestamp)
390 ts_report(&tv_start, "Total elapsed time");
391
392 exit(0);
393 }
394
395 /*
396 * Parse a capability string
397 *
398 * Put these into an svector so we can check for stuff later. But special
399 * things, like AUTH, we parse now
400 */
401
402 static void
403 parse_capability(const char *cap, unsigned int len)
404 {
405 char *str = mh_xmalloc(len + 1);
406 char **caplist;
407 int i;
408
409 trunccpy(str, cap, len + 1);
410 caplist = brkstring(str, " ", NULL);
411
412 if (imap_capabilities) {
413 svector_free(imap_capabilities);
414 }
415
416 imap_capabilities = svector_create(32);
417
418 if (saslmechs) {
419 free(saslmechs);
420 saslmechs = NULL;
421 }
422
423 for (i = 0; caplist[i] != NULL; i++) {
424 if (has_prefix(caplist[i], "AUTH=") && *(caplist[i] + 5) != '\0') {
425 if (saslmechs) {
426 saslmechs = add(" ", saslmechs);
427 }
428 saslmechs = add(caplist[i] + 5, saslmechs);
429 } else {
430 svector_push_back(imap_capabilities, getcpy(caplist[i]));
431 }
432 }
433
434 free(str);
435 }
436
437 /*
438 * Return 1 if a particular capability is set
439 */
440
441 static int
442 capability_set(const char *capability)
443 {
444 if (!imap_capabilities)
445 return 0;
446
447 return svector_find(imap_capabilities, capability) != NULL;
448 }
449
450 /*
451 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
452 */
453
454 static void
455 clear_capability(void)
456 {
457 svector_free(imap_capabilities);
458 imap_capabilities = NULL;
459 }
460
461 static int
462 have_capability(void)
463 {
464 return imap_capabilities != NULL;
465 }
466
467 /*
468 * Our SASL callback, which handles the SASL authentication dialog
469 */
470
471 static int
472 imap_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
473 unsigned int indatalen, unsigned char **outdata,
474 unsigned int *outdatalen, void *context, char **errstr)
475 {
476 int rc, snoopoffset;
477 char *mech, *line;
478 size_t len;
479 netsec_context *nsc = (netsec_context *) context;
480
481 switch (mtype) {
482 case NETSEC_SASL_START:
483 /*
484 * Generate our AUTHENTICATE message.
485 *
486 * If SASL-IR capability is set, we can include any initial response
487 * in the AUTHENTICATE command. Otherwise we have to wait for
488 * the first server response (which should be blank).
489 */
490
491 mech = netsec_get_sasl_mechanism(nsc);
492
493 if (indatalen) {
494 char *b64data;
495 b64data = mh_xmalloc(BASE64SIZE(indatalen));
496 writeBase64raw(indata, indatalen, (unsigned char *) b64data);
497 if (capability_set("SASL-IR")) {
498 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder,
499 &snoopoffset);
500 /*
501 * Sigh. We're assuming a short tag length here. Really,
502 * I guess we should figure out a way to get the length
503 * of the next tag.
504 */
505 snoopoffset = 17 + strlen(mech);
506 rc = send_imap_command(nsc, 0, errstr, "AUTHENTICATE %s %s",
507 mech, b64data);
508 free(b64data);
509 netsec_set_snoop_callback(nsc, NULL, NULL);
510 if (rc)
511 return NOTOK;
512 } else {
513 rc = send_imap_command(nsc, 0, errstr, "AUTHENTICATE %s", mech);
514 line = netsec_readline(nsc, &len, errstr);
515 if (! line)
516 return NOTOK;
517 /*
518 * We should get a "+ ", nothing else.
519 */
520 if (len != 2 || strcmp(line, "+ ") != 0) {
521 netsec_err(errstr, "Did not get expected blank response "
522 "for initial challenge response");
523 return NOTOK;
524 }
525 rc = netsec_printf(nsc, errstr, "%s\r\n", b64data);
526 free(b64data);
527 if (rc != OK)
528 return NOTOK;
529 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
530 rc = netsec_flush(nsc, errstr);
531 netsec_set_snoop_callback(nsc, NULL, NULL);
532 if (rc != OK)
533 return NOTOK;
534 }
535 } else {
536 if (send_imap_command(nsc, 0, errstr, "AUTHENTICATE %s",
537 mech) != OK)
538 return NOTOK;
539 }
540
541 break;
542
543 /*
544 * Get a response, decode it and process it.
545 */
546
547 case NETSEC_SASL_READ:
548 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, &snoopoffset);
549 snoopoffset = 2;
550 line = netsec_readline(nsc, &len, errstr);
551 netsec_set_snoop_callback(nsc, NULL, NULL);
552
553 if (line == NULL)
554 return NOTOK;
555
556 if (len < 2 || (len == 2 && strcmp(line, "+ ") != 0)) {
557 netsec_err(errstr, "Invalid format for SASL response");
558 return NOTOK;
559 }
560
561 if (len == 2) {
562 *outdata = NULL;
563 *outdatalen = 0;
564 } else {
565 rc = decodeBase64(line + 2, outdata, &len, 0, NULL);
566 *outdatalen = len;
567 if (rc != OK) {
568 netsec_err(errstr, "Unable to decode base64 response");
569 return NOTOK;
570 }
571 }
572 break;
573
574 /*
575 * Simple request encoding
576 */
577
578 case NETSEC_SASL_WRITE:
579 if (indatalen > 0) {
580 unsigned char *b64data;
581 b64data = mh_xmalloc(BASE64SIZE(indatalen));
582 writeBase64raw(indata, indatalen, b64data);
583 rc = netsec_printf(nsc, errstr, "%s", b64data);
584 free(b64data);
585 if (rc != OK)
586 return NOTOK;
587 }
588
589 if (netsec_printf(nsc, errstr, "\r\n") != OK)
590 return NOTOK;
591
592 netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
593 rc = netsec_flush(nsc, errstr);
594 netsec_set_snoop_callback(nsc, NULL, NULL);
595 if (rc != OK)
596 return NOTOK;
597 break;
598
599 /*
600 * Finish protocol
601 */
602
603 case NETSEC_SASL_FINISH:
604 line = NULL;
605 if (get_imap_response(nsc, NULL, NULL, &line, 1, errstr) != OK)
606 return NOTOK;
607 /*
608 * We MIGHT get a capability response here. If so, be sure we
609 * parse it.
610 */
611
612 if (line && has_prefix(line, "OK [CAPABILITY ")) {
613 char *p = line + 15, *q;
614 q = strchr(p, ']');
615
616 if (q)
617 parse_capability(p, q - p);
618 }
619
620 break;
621
622 /*
623 * Cancel an authentication dialog
624 */
625
626 case NETSEC_SASL_CANCEL:
627 rc = netsec_printf(nsc, errstr, "*\r\n");
628 if (rc == OK)
629 rc = netsec_flush(nsc, errstr);
630 if (rc != OK)
631 return NOTOK;
632 break;
633 }
634 return OK;
635 }
636
637 /*
638 * Send a single command to the IMAP server.
639 */
640
641 static int
642 send_imap_command(netsec_context *nsc, bool noflush, char **errstr,
643 const char *fmt, ...)
644 {
645 static unsigned int seq = 0; /* Tag sequence number */
646 va_list ap;
647 int rc;
648 struct imap_cmd *cmd;
649
650 cmd = mh_xmalloc(sizeof(*cmd));
651
652 snprintf(cmd->tag, sizeof(cmd->tag), "A%u ", seq++);
653
654 if (timestamp) {
655 char *p;
656 va_start(ap, fmt);
657 vsnprintf(cmd->prefix, sizeof(cmd->prefix), fmt, ap);
658 va_end(ap);
659
660 p = strchr(cmd->prefix, ' ');
661
662 if (p)
663 *p = '\0';
664
665 gettimeofday(&cmd->start, NULL);
666 }
667
668 if (netsec_write(nsc, cmd->tag, strlen(cmd->tag), errstr) != OK) {
669 free(cmd);
670 return NOTOK;
671 }
672
673 va_start(ap, fmt);
674 rc = netsec_vprintf(nsc, errstr, fmt, ap);
675 va_end(ap);
676
677 if (rc == OK)
678 rc = netsec_write(nsc, "\r\n", 2, errstr);
679
680 if (rc != OK) {
681 free(cmd);
682 return NOTOK;
683 }
684
685 if (!noflush && netsec_flush(nsc, errstr) != OK) {
686 free(cmd);
687 return NOTOK;
688 }
689
690 cmd->next = cmdqueue;
691 cmdqueue = cmd;
692
693 return OK;
694 }
695
696 /*
697 * Get all outstanding responses. If we were passed in a token string
698 * to look for, return it.
699 */
700
701 static int
702 get_imap_response(netsec_context *nsc, const char *token, char **tokenresponse,
703 char **status, int failerr, char **errstr)
704 {
705 char *line;
706 struct imap_cmd *cmd;
707 bool numerrs = false;
708
709 if (tokenresponse)
710 *tokenresponse = NULL;
711
712 getline:
713 while (cmdqueue) {
714 if (!(line = netsec_readline(nsc, NULL, errstr)))
715 return NOTOK;
716 if (has_prefix(line, "* ") && *(line + 2) != '\0') {
717 if (token && tokenresponse && has_prefix(line + 2, token)) {
718 if (*tokenresponse)
719 free(*tokenresponse);
720 *tokenresponse = getcpy(line + 2);
721 }
722 } else {
723
724 if (has_prefix(line, cmdqueue->tag)) {
725 cmd = cmdqueue;
726 if (timestamp)
727 ts_report(&cmd->start, "Command (%s) execution time",
728 cmd->prefix);
729 cmdqueue = cmd->next;
730 free(cmd);
731 } else {
732 for (cmd = cmdqueue; cmd->next != NULL; cmd = cmd->next) {
733 if (has_prefix(line, cmd->next->tag)) {
734 struct imap_cmd *cmd2 = cmd->next;
735 cmd->next = cmd->next->next;
736 if (failerr && strncmp(line + strlen(cmd2->tag),
737 "OK ", 3) != 0) {
738 numerrs = true;
739 netsec_err(errstr, "%s", line + strlen(cmd2->tag));
740 }
741 free(cmd2);
742 if (status)
743 *status = getcpy(line);
744 goto getline;
745 }
746 netsec_err(errstr, "Non-matching response line: %s\n",
747 line);
748 return NOTOK;
749 }
750 }
751 }
752 }
753
754 return numerrs ? NOTOK : OK;
755 }
756
757 /*
758 * Add an IMAP command to the msg queue
759 */
760
761 static void
762 add_msg(bool queue, const char *fmt, ...)
763 {
764 struct imap_msg *imsg;
765 va_list ap;
766 size_t msgbufsize;
767 char *msg = NULL;
768 int rc = 63;
769
770 do {
771 msgbufsize = rc + 1;
772 msg = mh_xrealloc(msg, msgbufsize);
773 va_start(ap, fmt);
774 rc = vsnprintf(msg, msgbufsize, fmt, ap);
775 va_end(ap);
776 } while (rc >= (int) msgbufsize);
777
778 imsg = mh_xmalloc(sizeof(*imsg));
779
780 imsg->command = msg;
781 imsg->queue = queue;
782 imsg->next = NULL;
783
784 if (msgqueue_head == NULL) {
785 msgqueue_head = imsg;
786 msgqueue_tail = imsg;
787 } else {
788 msgqueue_tail->next = imsg;
789 msgqueue_tail = imsg;
790 }
791 }
792
793 /*
794 * Negotiate TLS connection, with optional timestamp
795 */
796
797 static void
798 imap_negotiate_tls(netsec_context *nsc)
799 {
800 char *errstr;
801 struct timeval tv;
802
803 if (timestamp)
804 gettimeofday(&tv, NULL);
805
806 if (netsec_negotiate_tls(nsc, &errstr) != OK)
807 die("%s", errstr);
808
809 if (timestamp)
810 ts_report(&tv, "TLS negotation time");
811 }
812
813 /*
814 * Give a timestamp report.
815 */
816
817 static void
818 ts_report(struct timeval *tv, const char *fmt, ...)
819 {
820 struct timeval now;
821 double delta;
822 va_list ap;
823
824 gettimeofday(&now, NULL);
825
826 delta = ((double) now.tv_sec) - ((double) tv->tv_sec) +
827 (now.tv_usec / 1E6 - tv->tv_usec / 1E6);
828
829 va_start(ap, fmt);
830 vfprintf(stdout, fmt, ap);
831 va_end(ap);
832
833 printf(": %f sec\n", delta);
834 }