Fix false resource release <-> double free (Bob Tennent)..
[s-mailx.git] / quit.c
blob65185a74ae8f52844456ae48e3c03118c15a14cb
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Termination processing.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 #undef n_FILE
40 #define n_FILE quit
42 #ifndef HAVE_AMALGAMATION
43 # include "nail.h"
44 #endif
46 #include <utime.h>
48 enum quitflags {
49 QUITFLAG_HOLD = 1<<0,
50 QUITFLAG_KEEP = 1<<1,
51 QUITFLAG_KEEPSAVE = 1<<2,
52 QUITFLAG_APPEND = 1<<3,
53 QUITFLAG_EMPTYBOX = 1<<4
56 struct quitnames {
57 enum quitflags flag;
58 enum okeys okey;
61 static struct quitnames const _quitnames[] = {
62 {QUITFLAG_HOLD, ok_b_hold},
63 {QUITFLAG_KEEP, ok_b_keep},
64 {QUITFLAG_KEEPSAVE, ok_b_keepsave},
65 {QUITFLAG_APPEND, ok_b_append},
66 {QUITFLAG_EMPTYBOX, ok_b_emptybox} /* TODO obsolete emptybox */
69 static char _mboxname[PATH_MAX]; /* Name of mbox */
71 /* Touch the indicated file */
72 static void _alter(char const *name);
74 /* Preserve all the appropriate messages back in the system mailbox, and print
75 * a nice message indicated how many were saved. On any error, just return -1.
76 * Else return 0. Incorporate the any new mail that we found */
77 static int writeback(FILE *res, FILE *obuf);
79 /* Terminate an editing session by attempting to write out the user's file from
80 * the temporary. Save any new stuff appended to the file */
81 static void edstop(void);
83 /* Remove "mailname", unless *keep* says otherwise; force truncation, then */
84 static void _demail(void);
86 static void
87 _alter(char const *name)
89 struct stat sb;
90 struct utimbuf utb;
91 NYD_ENTER;
93 if (!stat(name, &sb)) {
94 utb.actime = time(NULL) + 1;
95 utb.modtime = sb.st_mtime;
96 utime(name, &utb);
98 NYD_LEAVE;
101 static int
102 writeback(FILE *res, FILE *obuf)
104 struct message *mp;
105 int rv = -1, p, c;
106 NYD_ENTER;
108 if (fseek(obuf, 0L, SEEK_SET) == -1)
109 goto jleave;
111 #ifndef APPEND
112 if (res != NULL)
113 while ((c = getc(res)) != EOF)
114 putc(c, obuf);
115 #endif
116 srelax_hold();
117 for (p = 0, mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
118 if ((mp->m_flag & MPRESERVE) || !(mp->m_flag & MTOUCH)) {
119 ++p;
120 if (sendmp(mp, obuf, NULL, NULL, SEND_MBOX, NULL) < 0) {
121 perror(mailname);
122 srelax_rele();
123 goto jerror;
125 srelax();
127 srelax_rele();
128 #ifdef APPEND
129 if (res != NULL)
130 while ((c = getc(res)) != EOF)
131 putc(c, obuf);
132 #endif
133 ftrunc(obuf);
135 if (ferror(obuf)) {
136 perror(mailname);
137 jerror:
138 fseek(obuf, 0L, SEEK_SET);
139 goto jleave;
141 if (fseek(obuf, 0L, SEEK_SET) == -1)
142 goto jleave;
144 _alter(mailname);
145 if (p == 1)
146 printf(_("Held 1 message in %s\n"), displayname);
147 else
148 printf(_("Held %d messages in %s\n"), p, displayname);
149 rv = 0;
150 jleave:
151 if (res != NULL)
152 Fclose(res);
153 NYD_LEAVE;
154 return rv;
157 static void
158 edstop(void) /* TODO oh my god - and REMOVE that CRAPPY reset(0) jump!! */
160 int gotcha, c;
161 struct message *mp;
162 FILE *obuf = NULL, *ibuf = NULL;
163 struct stat statb;
164 bool_t doreset;
165 NYD_ENTER;
167 hold_sigs();
168 doreset = FAL0;
170 if (mb.mb_perm == 0)
171 goto jleave;
173 for (mp = message, gotcha = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
174 if (mp->m_flag & MNEW) {
175 mp->m_flag &= ~MNEW;
176 mp->m_flag |= MSTATUS;
178 if (mp->m_flag & (MODIFY | MDELETED | MSTATUS | MFLAG | MUNFLAG |
179 MANSWER | MUNANSWER | MDRAFT | MUNDRAFT))
180 ++gotcha;
182 if (!gotcha)
183 goto jleave;
185 doreset = TRU1;
187 if (!stat(mailname, &statb) && statb.st_size > mailsize) {
188 if ((obuf = Ftmp(NULL, "edstop", OF_RDWR | OF_UNLINK | OF_REGISTER,
189 0600)) == NULL) {
190 perror(_("tmpfile"));
191 goto jleave;
193 if ((ibuf = Zopen(mailname, "r")) == NULL) {
194 perror(mailname);
195 Fclose(obuf);
196 goto jleave;
198 fseek(ibuf, (long)mailsize, SEEK_SET);
199 while ((c = getc(ibuf)) != EOF)
200 putc(c, obuf);
201 Fclose(ibuf);
202 ibuf = obuf;
203 fflush_rewind(obuf);
206 printf(_("\"%s\" "), displayname);
207 fflush(stdout);
208 if ((obuf = Zopen(mailname, "r+")) == NULL) {
209 perror(mailname);
210 goto jleave;
212 ftrunc(obuf);
214 srelax_hold();
215 c = 0;
216 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
217 if (mp->m_flag & MDELETED)
218 continue;
219 ++c;
220 if (sendmp(mp, obuf, NULL, NULL, SEND_MBOX, NULL) < 0) {
221 perror(mailname);
222 srelax_rele();
223 goto jleave;
225 srelax();
227 srelax_rele();
229 gotcha = (c == 0 && ibuf == NULL);
230 if (ibuf != NULL) {
231 while ((c = getc(ibuf)) != EOF)
232 putc(c, obuf);
234 fflush(obuf);
235 if (ferror(obuf)) {
236 perror(mailname);
237 goto jleave;
239 Fclose(obuf);
241 doreset = FAL0;
243 if (gotcha && !ok_blook(keep) && !ok_blook(emptybox)/* TODO obsolete eb*/) {
244 rm(mailname);
245 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
246 ? _("removed\n") : _("removed.\n"));
247 } else
248 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
249 ? _("complete\n") : _("updated.\n"));
250 fflush(stdout);
251 jleave:
252 if (ibuf != NULL)
253 Fclose(ibuf);
254 rele_sigs();
255 NYD_LEAVE;
256 if (doreset)
257 reset(0);
260 static void
261 _demail(void)
263 NYD2_ENTER;
264 if (ok_blook(keep) || rm(mailname) < 0) {
265 /* TODO demail(): try use f?truncate(2) instead?! */
266 int fd = open(mailname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
267 if (fd >= 0)
268 close(fd);
270 NYD2_LEAVE;
273 FL void
274 quit(void)
276 int p, modify, anystat, c;
277 FILE *fbuf = NULL, *rbuf, *abuf;
278 struct message *mp;
279 struct stat minfo;
280 NYD_ENTER;
282 temporary_localopts_folder_hook_unroll();
284 /* If we are read only, we can't do anything, so just return quickly */
285 /* TODO yet we cannot return quickly if resources have to be released!
286 * TODO somewhen it'll be mailbox->quit() anyway, for now do it by hand
287 *if (mb.mb_perm == 0)
288 * goto jleave;*/
289 p = (mb.mb_perm == 0);
291 /* TODO lex.c:setfile() has just called hold_sigs(); before it called
292 * TODO us, but this causes uninterruptible hangs due to blocked sigs
293 * TODO anywhere except for MB_FILE (all others install their own
294 * TODO handlers, as it seems, properly); marked YYY */
295 switch (mb.mb_type) {
296 case MB_FILE:
297 break;
298 case MB_MAILDIR:
299 rele_sigs(); /* YYY */
300 maildir_quit();
301 hold_sigs(); /* YYY */
302 goto jleave;
303 #ifdef HAVE_POP3
304 case MB_POP3:
305 rele_sigs(); /* YYY */
306 pop3_quit();
307 hold_sigs(); /* YYY */
308 goto jleave;
309 #endif
310 #ifdef HAVE_IMAP
311 case MB_IMAP:
312 case MB_CACHE:
313 rele_sigs(); /* YYY */
314 imap_quit();
315 hold_sigs(); /* YYY */
316 goto jleave;
317 #endif
318 case MB_VOID:
319 default:
320 goto jleave;
322 if (p) goto jleave; /* TODO */
324 /* If editing (not reading system mail box), then do the work in edstop() */
325 if (pstate & PS_EDIT) {
326 edstop();
327 goto jleave;
330 /* See if there any messages to save in mbox. If no, we
331 * can save copying mbox to /tmp and back.
333 * Check also to see if any files need to be preserved.
334 * Delete all untouched messages to keep them out of mbox.
335 * If all the messages are to be preserved, just exit with
336 * a message */
337 fbuf = Zopen(mailname, "r+");
338 if (fbuf == NULL) {
339 if (errno == ENOENT)
340 goto jleave;
341 jnewmail:
342 printf(_("Thou hast new mail.\n"));
343 goto jleave;
346 if (!dot_lock(mailname, fileno(fbuf), 1)) {
347 perror(_("Unable to lock mailbox"));
348 Fclose(fbuf);
349 fbuf = NULL;
350 goto jleave;
353 rbuf = NULL;
354 if (!fstat(fileno(fbuf), &minfo) && minfo.st_size > mailsize) {
355 printf(_("New mail has arrived.\n"));
356 rbuf = Ftmp(NULL, "quit", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600);
357 if (rbuf == NULL || fbuf == NULL)
358 goto jnewmail;
359 #ifdef APPEND
360 fseek(fbuf, (long)mailsize, SEEK_SET);
361 while ((c = getc(fbuf)) != EOF)
362 putc(c, rbuf);
363 #else
364 p = minfo.st_size - mailsize;
365 while (p-- > 0) {
366 c = getc(fbuf);
367 if (c == EOF)
368 goto jnewmail;
369 putc(c, rbuf);
371 #endif
372 fflush_rewind(rbuf);
375 anystat = holdbits();
376 modify = 0;
377 for (c = 0, p = 0, mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
378 if (mp->m_flag & MBOX)
379 c++;
380 if (mp->m_flag & MPRESERVE)
381 p++;
382 if (mp->m_flag & MODIFY)
383 modify++;
385 if (p == msgCount && !modify && !anystat) {
386 if (p == 1)
387 printf(_("Held 1 message in %s\n"), displayname);
388 else if (p > 1)
389 printf(_("Held %d messages in %s\n"), p, displayname);
390 goto jleave;
392 if (c == 0) {
393 if (p != 0) {
394 writeback(rbuf, fbuf);
395 goto jleave;
397 goto jcream;
400 if (makembox() == STOP)
401 goto jleave;
403 /* Now we are ready to copy back preserved files to the system mailbox, if
404 * any were requested */
405 if (p != 0) {
406 writeback(rbuf, fbuf);
407 goto jleave;
410 /* Finally, remove his file. If new mail has arrived, copy it back */
411 jcream:
412 if (rbuf != NULL) {
413 abuf = fbuf;
414 fseek(abuf, 0L, SEEK_SET);
415 while ((c = getc(rbuf)) != EOF)
416 putc(c, abuf);
417 Fclose(rbuf);
418 ftrunc(abuf);
419 _alter(mailname);
420 goto jleave;
422 _demail();
423 jleave:
424 if (fbuf != NULL) {
425 Fclose(fbuf);
426 dot_unlock(mailname);
428 NYD_LEAVE;
431 FL int
432 holdbits(void)
434 struct message *mp;
435 int anystat, autohold, holdbit, nohold;
436 NYD_ENTER;
438 anystat = 0;
439 autohold = ok_blook(hold);
440 holdbit = autohold ? MPRESERVE : MBOX;
441 nohold = MBOX | MSAVED | MDELETED | MPRESERVE;
442 if (ok_blook(keepsave))
443 nohold &= ~MSAVED;
444 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
445 if (mp->m_flag & MNEW) {
446 mp->m_flag &= ~MNEW;
447 mp->m_flag |= MSTATUS;
449 if (mp->m_flag & (MSTATUS | MFLAG | MUNFLAG | MANSWER | MUNANSWER |
450 MDRAFT | MUNDRAFT))
451 ++anystat;
452 if (!(mp->m_flag & MTOUCH))
453 mp->m_flag |= MPRESERVE;
454 if (!(mp->m_flag & nohold))
455 mp->m_flag |= holdbit;
457 NYD_LEAVE;
458 return anystat;
461 FL enum okay
462 makembox(void) /* TODO oh my god */
464 struct message *mp;
465 char *mbox, *tempQuit;
466 int mcount, c;
467 FILE *ibuf = NULL, *obuf, *abuf;
468 enum protocol prot;
469 enum okay rv = STOP;
470 NYD_ENTER;
472 mbox = _mboxname;
473 mcount = 0;
474 if (!ok_blook(append)) {
475 if ((obuf = Ftmp(&tempQuit, "makembox",
476 OF_WRONLY | OF_HOLDSIGS | OF_REGISTER, 0600)) == NULL) {
477 perror(_("temporary mail quit file"));
478 goto jleave;
480 if ((ibuf = Fopen(tempQuit, "r")) == NULL)
481 perror(tempQuit);
482 Ftmp_release(&tempQuit);
483 if (ibuf == NULL) {
484 Fclose(obuf);
485 goto jleave;
488 if ((abuf = Zopen(mbox, "r")) != NULL) {
489 while ((c = getc(abuf)) != EOF)
490 putc(c, obuf);
491 Fclose(abuf);
493 if (ferror(obuf)) {
494 perror(_("temporary mail quit file"));
495 Fclose(ibuf);
496 Fclose(obuf);
497 goto jleave;
499 Fclose(obuf);
501 if ((c = open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)) != -1)
502 close(c);
503 if ((obuf = Zopen(mbox, "r+")) == NULL) {
504 perror(mbox);
505 Fclose(ibuf);
506 goto jleave;
508 } else {
509 if ((obuf = Zopen(mbox, "a")) == NULL) {
510 perror(mbox);
511 goto jleave;
513 fchmod(fileno(obuf), 0600);
516 srelax_hold();
517 prot = which_protocol(mbox);
518 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp) {
519 if (mp->m_flag & MBOX) {
520 ++mcount;
521 if (prot == PROTO_IMAP &&
522 saveignore[0].i_count == 0 && saveignore[1].i_count == 0
523 #ifdef HAVE_IMAP /* TODO revisit */
524 && imap_thisaccount(mbox)
525 #endif
527 #ifdef HAVE_IMAP
528 if (imap_copy(mp, PTR2SIZE(mp - message + 1), mbox) == STOP)
529 #endif
530 goto jerr;
531 } else if (sendmp(mp, obuf, saveignore, NULL, SEND_MBOX, NULL) < 0) {
532 perror(mbox);
533 jerr:
534 srelax_rele();
535 if (ibuf != NULL)
536 Fclose(ibuf);
537 Fclose(obuf);
538 goto jleave;
540 mp->m_flag |= MBOXED;
541 srelax();
544 srelax_rele();
546 /* Copy the user's old mbox contents back to the end of the stuff we just
547 * saved. If we are appending, this is unnecessary */
548 if (!ok_blook(append)) {
549 rewind(ibuf);
550 c = getc(ibuf);
551 while (c != EOF) {
552 putc(c, obuf);
553 if (ferror(obuf))
554 break;
555 c = getc(ibuf);
557 Fclose(ibuf);
558 fflush(obuf);
560 ftrunc(obuf);
561 if (ferror(obuf)) {
562 perror(mbox);
563 Fclose(obuf);
564 goto jleave;
566 if (Fclose(obuf) != 0) {
567 if (prot != PROTO_IMAP)
568 perror(mbox);
569 goto jleave;
571 if (mcount == 1)
572 printf(_("Saved 1 message in mbox\n"));
573 else
574 printf(_("Saved %d messages in mbox\n"), mcount);
575 rv = OKAY;
576 jleave:
577 NYD_LEAVE;
578 return rv;
581 FL void
582 save_mbox_for_possible_quitstuff(void) /* TODO try to get rid of that */
584 char const *cp;
585 NYD_ENTER;
587 if ((cp = expand("&")) == NULL)
588 cp = "";
589 n_strlcpy(_mboxname, cp, sizeof _mboxname);
590 NYD_LEAVE;
593 FL int
594 savequitflags(void)
596 enum quitflags qf = 0;
597 size_t i;
598 NYD_ENTER;
600 for (i = 0; i < NELEM(_quitnames); ++i)
601 if (_var_oklook(_quitnames[i].okey) != NULL)
602 qf |= _quitnames[i].flag;
603 NYD_LEAVE;
604 return qf;
607 FL void
608 restorequitflags(int qf)
610 size_t i;
611 NYD_ENTER;
613 for (i = 0; i < NELEM(_quitnames); ++i) {
614 char *x = _var_oklook(_quitnames[i].okey);
615 if (qf & _quitnames[i].flag) {
616 if (x == NULL)
617 _var_okset(_quitnames[i].okey, TRU1);
618 } else if (x != NULL)
619 _var_okclear(_quitnames[i].okey);
621 NYD_LEAVE;
624 /* s-it-mode */