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 #pragma ident "%Z%%M% %I% %E% SMI" 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* 28*7c478bd9Sstevel@tonic-gate * NAME 29*7c478bd9Sstevel@tonic-gate * cksaved - check for an orphaned save file 30*7c478bd9Sstevel@tonic-gate * 31*7c478bd9Sstevel@tonic-gate * SYNOPSIS 32*7c478bd9Sstevel@tonic-gate * void cksaved(char *user) 33*7c478bd9Sstevel@tonic-gate * 34*7c478bd9Sstevel@tonic-gate * DESCRIPTION 35*7c478bd9Sstevel@tonic-gate * cksaved() looks to see if there is a saved-mail file sitting 36*7c478bd9Sstevel@tonic-gate * around which should be reinstated. These files should be sitting 37*7c478bd9Sstevel@tonic-gate * around only in the case of a crash during rewriting a mail message. 38*7c478bd9Sstevel@tonic-gate * 39*7c478bd9Sstevel@tonic-gate * The strategy is simple: if the file exists it is appended to 40*7c478bd9Sstevel@tonic-gate * the end of $MAIL. It is better that a user potentially sees the 41*7c478bd9Sstevel@tonic-gate * mail twice than to lose it. 42*7c478bd9Sstevel@tonic-gate * 43*7c478bd9Sstevel@tonic-gate * If $MAIL doesn't exist, then a simple rename() will suffice. 44*7c478bd9Sstevel@tonic-gate */ 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #include "mail.h" 47*7c478bd9Sstevel@tonic-gate void 48*7c478bd9Sstevel@tonic-gate cksaved(user) 49*7c478bd9Sstevel@tonic-gate char *user; 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate struct stat stbuf; 52*7c478bd9Sstevel@tonic-gate char command[512]; 53*7c478bd9Sstevel@tonic-gate char save[MAXFILENAME], mail[MAXFILENAME]; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate cat(mail, maildir, user); 56*7c478bd9Sstevel@tonic-gate cat(save, mailsave, user); 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate /* 59*7c478bd9Sstevel@tonic-gate * If no save file, or size is 0, return. 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate if ((stat(save, &stbuf) != 0) || (stbuf.st_size == 0)) 62*7c478bd9Sstevel@tonic-gate return; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* 65*7c478bd9Sstevel@tonic-gate * Ok, we have a savefile. If no mailfile exists, 66*7c478bd9Sstevel@tonic-gate * then we want to restore to the mailfile, 67*7c478bd9Sstevel@tonic-gate * else we append to the mailfile. 68*7c478bd9Sstevel@tonic-gate */ 69*7c478bd9Sstevel@tonic-gate lock(user); 70*7c478bd9Sstevel@tonic-gate if (stat(mail, &stbuf) != 0) { 71*7c478bd9Sstevel@tonic-gate /* 72*7c478bd9Sstevel@tonic-gate * Restore from the save file by linking 73*7c478bd9Sstevel@tonic-gate * it to $MAIL then unlinking save file 74*7c478bd9Sstevel@tonic-gate */ 75*7c478bd9Sstevel@tonic-gate chmod(save, MFMODE); 76*7c478bd9Sstevel@tonic-gate #ifdef SVR3 77*7c478bd9Sstevel@tonic-gate if (link(save, mail) != 0) { 78*7c478bd9Sstevel@tonic-gate unlock(); 79*7c478bd9Sstevel@tonic-gate perror("Restore failed to link to mailfile"); 80*7c478bd9Sstevel@tonic-gate return; 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate if (unlink(save) != 0) { 84*7c478bd9Sstevel@tonic-gate unlock(); 85*7c478bd9Sstevel@tonic-gate perror("Cannot unlink saved file"); 86*7c478bd9Sstevel@tonic-gate return; 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate #else 89*7c478bd9Sstevel@tonic-gate if (rename(save, mail) != 0) { 90*7c478bd9Sstevel@tonic-gate unlock(); 91*7c478bd9Sstevel@tonic-gate perror("Cannot rename saved file"); 92*7c478bd9Sstevel@tonic-gate return; 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate #endif 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate (void) snprintf(command, sizeof (command), 97*7c478bd9Sstevel@tonic-gate "echo \"Your mailfile was just restored by the mail " 98*7c478bd9Sstevel@tonic-gate "program.\nPermissions of your mailfile are set " 99*7c478bd9Sstevel@tonic-gate "to 0660.\"| mail %s", user); 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate else { 103*7c478bd9Sstevel@tonic-gate FILE *Istream, *Ostream; 104*7c478bd9Sstevel@tonic-gate if ((Ostream = fopen(mail, "a")) == NULL) { 105*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 106*7c478bd9Sstevel@tonic-gate "%s: Cannot open file '%s' for output\n", 107*7c478bd9Sstevel@tonic-gate program, mail); 108*7c478bd9Sstevel@tonic-gate unlock(); 109*7c478bd9Sstevel@tonic-gate return; 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate if ((Istream = fopen(save, "r")) == NULL) { 112*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot open saved " 113*7c478bd9Sstevel@tonic-gate "file '%s' for reading\n", program, save); 114*7c478bd9Sstevel@tonic-gate fclose(Ostream); 115*7c478bd9Sstevel@tonic-gate unlock(); 116*7c478bd9Sstevel@tonic-gate return; 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate copystream(Istream, Ostream); 119*7c478bd9Sstevel@tonic-gate fclose(Istream); 120*7c478bd9Sstevel@tonic-gate fclose(Ostream); 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate if (unlink(save) != 0) { 123*7c478bd9Sstevel@tonic-gate perror("Unlink of save file failed"); 124*7c478bd9Sstevel@tonic-gate return; 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate (void) snprintf(command, sizeof (command), 128*7c478bd9Sstevel@tonic-gate "echo \"Your mail save file has just been appended " 129*7c478bd9Sstevel@tonic-gate "to your mail box by the mail program.\" | mail %s", user); 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate /* 133*7c478bd9Sstevel@tonic-gate * Try to send mail to the user whose file 134*7c478bd9Sstevel@tonic-gate * is being restored. 135*7c478bd9Sstevel@tonic-gate */ 136*7c478bd9Sstevel@tonic-gate unlock(); 137*7c478bd9Sstevel@tonic-gate systm(command); 138*7c478bd9Sstevel@tonic-gate } 139