1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
24*7c478bd9Sstevel@tonic-gate
25*7c478bd9Sstevel@tonic-gate /*
26*7c478bd9Sstevel@tonic-gate * NAME
27*7c478bd9Sstevel@tonic-gate * cksaved - check for an orphaned save file
28*7c478bd9Sstevel@tonic-gate *
29*7c478bd9Sstevel@tonic-gate * SYNOPSIS
30*7c478bd9Sstevel@tonic-gate * void cksaved(char *user)
31*7c478bd9Sstevel@tonic-gate *
32*7c478bd9Sstevel@tonic-gate * DESCRIPTION
33*7c478bd9Sstevel@tonic-gate * cksaved() looks to see if there is a saved-mail file sitting
34*7c478bd9Sstevel@tonic-gate * around which should be reinstated. These files should be sitting
35*7c478bd9Sstevel@tonic-gate * around only in the case of a crash during rewriting a mail message.
36*7c478bd9Sstevel@tonic-gate *
37*7c478bd9Sstevel@tonic-gate * The strategy is simple: if the file exists it is appended to
38*7c478bd9Sstevel@tonic-gate * the end of $MAIL. It is better that a user potentially sees the
39*7c478bd9Sstevel@tonic-gate * mail twice than to lose it.
40*7c478bd9Sstevel@tonic-gate *
41*7c478bd9Sstevel@tonic-gate * If $MAIL doesn't exist, then a simple rename() will suffice.
42*7c478bd9Sstevel@tonic-gate */
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #include "mail.h"
45*7c478bd9Sstevel@tonic-gate void
cksaved(user)46*7c478bd9Sstevel@tonic-gate cksaved(user)
47*7c478bd9Sstevel@tonic-gate char *user;
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate struct stat stbuf;
50*7c478bd9Sstevel@tonic-gate char command[512];
51*7c478bd9Sstevel@tonic-gate char save[MAXFILENAME], mail[MAXFILENAME];
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate cat(mail, maildir, user);
54*7c478bd9Sstevel@tonic-gate cat(save, mailsave, user);
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate * If no save file, or size is 0, return.
58*7c478bd9Sstevel@tonic-gate */
59*7c478bd9Sstevel@tonic-gate if ((stat(save, &stbuf) != 0) || (stbuf.st_size == 0))
60*7c478bd9Sstevel@tonic-gate return;
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate * Ok, we have a savefile. If no mailfile exists,
64*7c478bd9Sstevel@tonic-gate * then we want to restore to the mailfile,
65*7c478bd9Sstevel@tonic-gate * else we append to the mailfile.
66*7c478bd9Sstevel@tonic-gate */
67*7c478bd9Sstevel@tonic-gate lock(user);
68*7c478bd9Sstevel@tonic-gate if (stat(mail, &stbuf) != 0) {
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate * Restore from the save file by linking
71*7c478bd9Sstevel@tonic-gate * it to $MAIL then unlinking save file
72*7c478bd9Sstevel@tonic-gate */
73*7c478bd9Sstevel@tonic-gate chmod(save, MFMODE);
74*7c478bd9Sstevel@tonic-gate #ifdef SVR3
75*7c478bd9Sstevel@tonic-gate if (link(save, mail) != 0) {
76*7c478bd9Sstevel@tonic-gate unlock();
77*7c478bd9Sstevel@tonic-gate perror("Restore failed to link to mailfile");
78*7c478bd9Sstevel@tonic-gate return;
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate if (unlink(save) != 0) {
82*7c478bd9Sstevel@tonic-gate unlock();
83*7c478bd9Sstevel@tonic-gate perror("Cannot unlink saved file");
84*7c478bd9Sstevel@tonic-gate return;
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate #else
87*7c478bd9Sstevel@tonic-gate if (rename(save, mail) != 0) {
88*7c478bd9Sstevel@tonic-gate unlock();
89*7c478bd9Sstevel@tonic-gate perror("Cannot rename saved file");
90*7c478bd9Sstevel@tonic-gate return;
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate #endif
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate (void) snprintf(command, sizeof (command),
95*7c478bd9Sstevel@tonic-gate "echo \"Your mailfile was just restored by the mail "
96*7c478bd9Sstevel@tonic-gate "program.\nPermissions of your mailfile are set "
97*7c478bd9Sstevel@tonic-gate "to 0660.\"| mail %s", user);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate else {
101*7c478bd9Sstevel@tonic-gate FILE *Istream, *Ostream;
102*7c478bd9Sstevel@tonic-gate if ((Ostream = fopen(mail, "a")) == NULL) {
103*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
104*7c478bd9Sstevel@tonic-gate "%s: Cannot open file '%s' for output\n",
105*7c478bd9Sstevel@tonic-gate program, mail);
106*7c478bd9Sstevel@tonic-gate unlock();
107*7c478bd9Sstevel@tonic-gate return;
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate if ((Istream = fopen(save, "r")) == NULL) {
110*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot open saved "
111*7c478bd9Sstevel@tonic-gate "file '%s' for reading\n", program, save);
112*7c478bd9Sstevel@tonic-gate fclose(Ostream);
113*7c478bd9Sstevel@tonic-gate unlock();
114*7c478bd9Sstevel@tonic-gate return;
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate copystream(Istream, Ostream);
117*7c478bd9Sstevel@tonic-gate fclose(Istream);
118*7c478bd9Sstevel@tonic-gate fclose(Ostream);
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate if (unlink(save) != 0) {
121*7c478bd9Sstevel@tonic-gate perror("Unlink of save file failed");
122*7c478bd9Sstevel@tonic-gate return;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate (void) snprintf(command, sizeof (command),
126*7c478bd9Sstevel@tonic-gate "echo \"Your mail save file has just been appended "
127*7c478bd9Sstevel@tonic-gate "to your mail box by the mail program.\" | mail %s", user);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate * Try to send mail to the user whose file
132*7c478bd9Sstevel@tonic-gate * is being restored.
133*7c478bd9Sstevel@tonic-gate */
134*7c478bd9Sstevel@tonic-gate unlock();
135*7c478bd9Sstevel@tonic-gate systm(command);
136*7c478bd9Sstevel@tonic-gate }
137