1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "imap4d.h"
18
19 int
imap4d_bye(int reason)20 imap4d_bye (int reason)
21 {
22 return imap4d_bye_command (reason, NULL);
23 }
24
25 static jmp_buf pipejmp;
26
27 static RETSIGTYPE
sigpipe(int sig)28 sigpipe (int sig)
29 {
30 longjmp (pipejmp, 1);
31 }
32
33 int
imap4d_bye_command(int reason,struct imap4d_command * command)34 imap4d_bye_command (int reason, struct imap4d_command *command)
35 {
36 int status = EX_SOFTWARE;
37
38 /* Some clients may close the connection immediately after sending
39 LOGOUT. Do not treat this as error (RFC 2683).
40 To be on the safe side, ignore broken pipes for any command: at
41 this stage it is of no significance. */
42 static int sigtab[] = { SIGPIPE };
43 mu_set_signals (sigpipe, sigtab, MU_ARRAY_SIZE (sigtab));
44 if (setjmp (pipejmp))
45 {
46 mu_set_signals (SIG_IGN, sigtab, MU_ARRAY_SIZE (sigtab));
47 /* Invalidate iostream. This creates a mild memory leak, as the
48 stream is not destroyed by util_bye, but at least this avoids
49 endless loop which would happen when mu_stream_flush would
50 try to flush buffers on an already broken pipe.
51 FIXME: There must be a special function for that, I guess.
52 Something like mu_stream_invalidate. */
53 iostream = NULL;
54 }
55 else
56 {
57 if (mbox)
58 {
59 imap4d_enter_critical ();
60 mu_mailbox_flush (mbox, 0);
61 mu_mailbox_close (mbox);
62 manlock_unlock (mbox);
63 mu_mailbox_destroy (&mbox);
64 imap4d_leave_critical ();
65 }
66
67 switch (reason)
68 {
69 case ERR_NO_MEM:
70 io_untagged_response (RESP_BYE,
71 "Server terminating: no more resources.");
72 mu_diag_output (MU_DIAG_ERROR, _("not enough memory"));
73 break;
74
75 case ERR_TERMINATE:
76 status = EX_OK;
77 io_untagged_response (RESP_BYE, "Server terminating on request.");
78 mu_diag_output (MU_DIAG_NOTICE, _("terminating on request"));
79 break;
80
81 case ERR_SIGNAL:
82 mu_diag_output (MU_DIAG_ERROR, _("quitting on signal"));
83 exit (status);
84
85 case ERR_TIMEOUT:
86 status = EX_TEMPFAIL;
87 io_untagged_response (RESP_BYE, "Session timed out");
88 if (state == STATE_NONAUTH)
89 mu_diag_output (MU_DIAG_INFO, _("session timed out for no user"));
90 else
91 mu_diag_output (MU_DIAG_INFO, _("session timed out for user: %s"),
92 auth_data->name);
93 break;
94
95 case ERR_NO_OFILE:
96 status = EX_IOERR;
97 mu_diag_output (MU_DIAG_INFO, _("write error on control stream"));
98 break;
99
100 case ERR_NO_IFILE:
101 status = EX_IOERR;
102 mu_diag_output (MU_DIAG_INFO, _("read error on control stream"));
103 break;
104
105 case ERR_MAILBOX_CORRUPTED:
106 status = EX_OSERR;
107 mu_diag_output (MU_DIAG_ERROR, _("mailbox modified by third party"));
108 break;
109
110 case ERR_STREAM_CREATE:
111 status = EX_UNAVAILABLE;
112 mu_diag_output (MU_DIAG_ERROR, _("cannot create transport stream"));
113 break;
114
115 case OK:
116 status = EX_OK;
117 io_untagged_response (RESP_BYE, "Session terminating.");
118 if (state == STATE_NONAUTH)
119 mu_diag_output (MU_DIAG_INFO, _("session terminating"));
120 else
121 mu_diag_output (MU_DIAG_INFO,
122 _("session terminating for user: %s"),
123 auth_data->name);
124 break;
125
126 default:
127 io_untagged_response (RESP_BYE, "Quitting (reason unknown)");
128 mu_diag_output (MU_DIAG_ERROR, _("quitting (numeric reason %d)"),
129 reason);
130 break;
131 }
132
133 if (status == EX_OK && command)
134 io_completion_response (command, RESP_OK, "Completed");
135 }
136
137 util_bye ();
138
139 closelog ();
140 exit (status);
141 }
142
143