Commit 2a0e6368 2a0e6368a2bdcc324c2255e8f671fad26b6e0236 by Sergey Poznyakoff

(ifile, ofile, sfile): Removed.

(istream, ostream): New statics.
(util_send_lowlevel): Removed
(util_send, util_out, util_finish, imap4d_readline)
(util_flush_output): Rewritten using streams.
(util_setio): Changed argument types
(util_is_ofile): Removed.
(util_is_master): New function.
(imap4d_init_tls_server): Rewritten using TLS streams.
(imap4d_deinit_tls_server): Removed.
(util_bye): New function.
1 parent 8c64eaf8
...@@ -17,11 +17,8 @@ ...@@ -17,11 +17,8 @@
17 17
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 static FILE *ifile; 20 static stream_t istream;
21 static FILE *ofile; 21 static stream_t ostream;
22 #ifdef WITH_TLS
23 static gnutls_session sfile;
24 #endif /* WITH_TLS */
25 22
26 static int add2set __P ((size_t **, int *, unsigned long)); 23 static int add2set __P ((size_t **, int *, unsigned long));
27 static const char *sc2string __P ((int)); 24 static const char *sc2string __P ((int));
...@@ -318,24 +315,6 @@ util_msgset (char *s, size_t ** set, int *n, int isuid) ...@@ -318,24 +315,6 @@ util_msgset (char *s, size_t ** set, int *n, int isuid)
318 return 0; 315 return 0;
319 } 316 }
320 317
321 static int
322 util_send_lowlevel (char *buf)
323 {
324 int status;
325
326 if (buf)
327 {
328 #ifdef WITH_TLS
329 if (tls_done)
330 status = gnutls_record_send (sfile, buf, strlen (buf));
331 else
332 #endif /* WITH_TLS */
333 status = fprintf (ofile, buf);
334 }
335
336 return status;
337 }
338
339 int 318 int
340 util_send (const char *format, ...) 319 util_send (const char *format, ...)
341 { 320 {
...@@ -345,17 +324,16 @@ util_send (const char *format, ...) ...@@ -345,17 +324,16 @@ util_send (const char *format, ...)
345 324
346 va_start (ap, format); 325 va_start (ap, format);
347 vasprintf (&buf, format, ap); 326 vasprintf (&buf, format, ap);
327 va_end (ap);
328 if (!buf)
329 imap4d_bye (ERR_NO_MEM);
348 330
349 if (buf)
350 {
351 if (daemon_param.transcript) 331 if (daemon_param.transcript)
352 syslog (LOG_DEBUG, "sent: %s", buf); 332 syslog (LOG_DEBUG, "sent: %s", buf);
353 333
354 status = util_send_lowlevel (buf); 334 status = stream_sequential_write (ostream, buf, strlen (buf));
355 free (buf); 335 free (buf);
356 }
357 336
358 va_end (ap);
359 return status; 337 return status;
360 } 338 }
361 339
...@@ -400,17 +378,15 @@ util_out (int rc, const char *format, ...) ...@@ -400,17 +378,15 @@ util_out (int rc, const char *format, ...)
400 asprintf (&tempbuf, "* %s%s\r\n", sc2string (rc), format); 378 asprintf (&tempbuf, "* %s%s\r\n", sc2string (rc), format);
401 va_start (ap, format); 379 va_start (ap, format);
402 vasprintf (&buf, tempbuf, ap); 380 vasprintf (&buf, tempbuf, ap);
381 va_end (ap);
382 if (!buf)
383 imap4d_bye (ERR_NO_MEM);
403 384
404 if (buf)
405 {
406 if (daemon_param.transcript) 385 if (daemon_param.transcript)
407 syslog (LOG_DEBUG, "sent: %s", buf); 386 syslog (LOG_DEBUG, "sent: %s", buf);
408 387
409 status = util_send_lowlevel (buf); 388 status = stream_sequential_write (ostream, buf, strlen (buf));
410 free (buf); 389 free (buf);
411 }
412
413 va_end (ap);
414 free (tempbuf); 390 free (tempbuf);
415 return status; 391 return status;
416 } 392 }
...@@ -429,17 +405,15 @@ util_finish (struct imap4d_command *command, int rc, const char *format, ...) ...@@ -429,17 +405,15 @@ util_finish (struct imap4d_command *command, int rc, const char *format, ...)
429 command->name, format); 405 command->name, format);
430 va_start (ap, format); 406 va_start (ap, format);
431 vasprintf (&buf, tempbuf, ap); 407 vasprintf (&buf, tempbuf, ap);
408 va_end (ap);
409 if (!buf)
410 imap4d_bye (ERR_NO_MEM);
432 411
433 if (buf)
434 {
435 if (daemon_param.transcript) 412 if (daemon_param.transcript)
436 syslog (LOG_DEBUG, "sent: %s", buf); 413 syslog (LOG_DEBUG, "sent: %s", buf);
437 414
438 status = util_send_lowlevel (buf); 415 status = stream_sequential_write (ostream, buf, strlen (buf));
439 free (buf); 416 free (buf);
440 }
441
442 va_end (ap);
443 free (tempbuf); 417 free (tempbuf);
444 418
445 /* Reset the state. */ 419 /* Reset the state. */
...@@ -477,30 +451,23 @@ imap4d_readline (void) ...@@ -477,30 +451,23 @@ imap4d_readline (void)
477 line[0] = '\0'; /* start with a empty string. */ 451 line[0] = '\0'; /* start with a empty string. */
478 do 452 do
479 { 453 {
454 size_t sz;
455 int rc;
456
480 alarm (daemon_param.timeout); 457 alarm (daemon_param.timeout);
481 #ifdef WITH_TLS 458 rc = stream_sequential_readline (istream, buffer, sizeof (buffer), &sz);
482 if (tls_done) 459 if (sz == 0)
483 { 460 {
484 len = gnutls_record_recv (sfile, buffer, sizeof (buffer) - 1); 461 syslog (LOG_INFO, _("unexpected eof on input"));
485 if (len < 0) 462 imap4d_bye (ERR_NO_OFILE);
486 {
487 syslog (LOG_INFO, _("TLS error on read: %s"),
488 gnutls_strerror (len));
489 imap4d_bye (ERR_TLS);
490 }
491 else
492 buffer[len] = 0;
493 } 463 }
494 else 464 else if (rc)
495 #endif /* WITH_TLS */
496 if (fgets (buffer, sizeof (buffer), ifile) == NULL)
497 { 465 {
498 if (feof (ifile)) 466 const char *p;
499 syslog (LOG_INFO, _("unexpected eof on input")); 467 if (stream_strerror (istream, &p))
500 else if (errno) 468 p = strerror (errno);
501 syslog (LOG_INFO, _("error reading from input file: %m")); 469
502 else 470 syslog (LOG_INFO, _("error reading from input file: %s"), p);
503 continue;
504 imap4d_bye (ERR_NO_OFILE); 471 imap4d_bye (ERR_NO_OFILE);
505 } 472 }
506 alarm (0); 473 alarm (0);
...@@ -799,24 +766,13 @@ struct ...@@ -799,24 +766,13 @@ struct
799 } 766 }
800 _imap4d_attrlist[] = 767 _imap4d_attrlist[] =
801 { 768 {
802 { 769 { "\\Answered", MU_ATTRIBUTE_ANSWERED },
803 "\\Answered", MU_ATTRIBUTE_ANSWERED} 770 { "\\Flagged", MU_ATTRIBUTE_FLAGGED },
804 , 771 { "\\Deleted", MU_ATTRIBUTE_DELETED },
805 { 772 { "\\Draft", MU_ATTRIBUTE_DRAFT },
806 "\\Flagged", MU_ATTRIBUTE_FLAGGED} 773 { "\\Seen", MU_ATTRIBUTE_READ },
807 , 774 { "\\Recent", MU_ATTRIBUTE_RECENT },
808 { 775 };
809 "\\Deleted", MU_ATTRIBUTE_DELETED}
810 ,
811 {
812 "\\Draft", MU_ATTRIBUTE_DRAFT}
813 ,
814 {
815 "\\Seen", MU_ATTRIBUTE_READ}
816 ,
817 {
818 "\\Recent", MU_ATTRIBUTE_RECENT}
819 ,};
820 776
821 #define NATTR sizeof(_imap4d_attrlist)/sizeof(_imap4d_attrlist[0]) 777 #define NATTR sizeof(_imap4d_attrlist)/sizeof(_imap4d_attrlist[0])
822 778
...@@ -1138,45 +1094,73 @@ util_uidvalidity (mailbox_t smbox, unsigned long *uidvp) ...@@ -1138,45 +1094,73 @@ util_uidvalidity (mailbox_t smbox, unsigned long *uidvp)
1138 1094
1139 1095
1140 void 1096 void
1141 util_setio (int infile, int outfile) 1097 util_setio (FILE *in, FILE *out)
1142 { 1098 {
1143 ifile = fdopen (infile, "r"); 1099 if (!out || !in)
1144 ofile = fdopen (outfile, "w");
1145 if (!ofile || !ifile)
1146 imap4d_bye (ERR_NO_OFILE); 1100 imap4d_bye (ERR_NO_OFILE);
1147 1101
1148 setvbuf (ofile, NULL, _IOLBF, 0); 1102 if (stdio_stream_create (&istream, in, MU_STREAM_NO_CLOSE)
1103 || stdio_stream_create (&ostream, out, MU_STREAM_NO_CLOSE))
1104 imap4d_bye (ERR_NO_OFILE);
1149 } 1105 }
1150 1106
1151 void 1107 void
1152 util_flush_output () 1108 util_flush_output ()
1153 { 1109 {
1154 if (!tls_done) 1110 stream_flush (ostream);
1155 fflush (ofile);
1156 } 1111 }
1157 1112
1158 FILE * 1113 int
1159 util_is_ofile () 1114 util_is_master ()
1160 { 1115 {
1161 return ofile; 1116 return ostream == NULL;
1162 } 1117 }
1163 1118
1164 #ifdef WITH_TLS 1119 #ifdef WITH_TLS
1165
1166 int 1120 int
1167 imap4d_init_tls_server () 1121 imap4d_init_tls_server ()
1168 { 1122 {
1169 sfile = 1123 stream_t stream;
1170 (gnutls_session) mu_init_tls_server (fileno (ifile), fileno (ofile)); 1124 int in_fd;
1171 if (!sfile) 1125 int out_fd;
1126 int rc;
1127
1128 if (stream_get_fd (istream, &in_fd)
1129 || stream_get_fd (ostream, &out_fd))
1130 return 0;
1131 rc = tls_stream_create (&stream, in_fd, out_fd, 0);
1132 if (rc)
1172 return 0; 1133 return 0;
1134
1135 if (stream_open (stream))
1136 {
1137 const char *p;
1138 stream_strerror (stream, &p);
1139 syslog (LOG_ERR, _("cannot open TLS stream: %s"), p);
1140 return 0;
1141 }
1142
1143 stream_destroy (&istream, stream_get_owner (istream));
1144 stream_destroy (&ostream, stream_get_owner (ostream));
1145 istream = ostream = stream;
1173 return 1; 1146 return 1;
1174 } 1147 }
1148 #endif /* WITH_TLS */
1175 1149
1176 void 1150 void
1177 imap4d_deinit_tls_server () 1151 util_bye ()
1178 { 1152 {
1179 mu_deinit_tls_server (sfile); 1153 if (istream == ostream)
1154 {
1155 stream_close (istream);
1156 stream_destroy (&istream, stream_get_owner (istream));
1157 }
1158 /* There's no reason closing in/out streams otherwise */
1159 #ifdef WITH_TLS
1160 mu_deinit_tls_libs ();
1161 #endif /* WITH_TLS */
1180 } 1162 }
1181 1163
1182 #endif /* WITH_TLS */ 1164
1165
1166
......