xref: /titanic_53/usr/src/cmd/mail/mkdead.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #ident	"%Z%%M%	%I%	%E% SMI"        /* SVr4.0 1.6 */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "mail.h"
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate 	Routine creates dead.letter
31*7c478bd9Sstevel@tonic-gate */
mkdead()32*7c478bd9Sstevel@tonic-gate void mkdead()
33*7c478bd9Sstevel@tonic-gate {
34*7c478bd9Sstevel@tonic-gate 	static char pn[] = "mkdead";
35*7c478bd9Sstevel@tonic-gate 	int aret;
36*7c478bd9Sstevel@tonic-gate 	char *dotdead = &dead[1];
37*7c478bd9Sstevel@tonic-gate 	gid_t egid = getegid();
38*7c478bd9Sstevel@tonic-gate 	struct stat st;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 	malf = (FILE *)NULL;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 	/*
43*7c478bd9Sstevel@tonic-gate 		Make certain that there's something to copy.
44*7c478bd9Sstevel@tonic-gate 	*/
45*7c478bd9Sstevel@tonic-gate 	if (!tmpf)
46*7c478bd9Sstevel@tonic-gate 		return;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	/*
49*7c478bd9Sstevel@tonic-gate 		Try to create dead letter in current directory
50*7c478bd9Sstevel@tonic-gate 		or in home directory
51*7c478bd9Sstevel@tonic-gate 	*/
52*7c478bd9Sstevel@tonic-gate 	umask(umsave);
53*7c478bd9Sstevel@tonic-gate 	setgid(getgid());
54*7c478bd9Sstevel@tonic-gate 	if ((aret = legal(dotdead)) && stat(dotdead, &st) == 0)
55*7c478bd9Sstevel@tonic-gate 		malf = fopen(dotdead, "a");
56*7c478bd9Sstevel@tonic-gate 	if ((malf == NULL) || (aret == 0)) {
57*7c478bd9Sstevel@tonic-gate 		/*
58*7c478bd9Sstevel@tonic-gate 			try to create in $HOME
59*7c478bd9Sstevel@tonic-gate 		*/
60*7c478bd9Sstevel@tonic-gate 		if((hmdead = malloc(strlen(home) + strlen(dead) + 1)) == NULL) {
61*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: Can't malloc\n",program);
62*7c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "Cannot malloc.\n");
63*7c478bd9Sstevel@tonic-gate 			goto out;
64*7c478bd9Sstevel@tonic-gate 		}
65*7c478bd9Sstevel@tonic-gate 		cat(hmdead, home, dead);
66*7c478bd9Sstevel@tonic-gate 		if ((aret=legal(hmdead)) && !(stat(hmdead, &st) < 0 &&
67*7c478bd9Sstevel@tonic-gate 			errno == EOVERFLOW))
68*7c478bd9Sstevel@tonic-gate 			malf = fopen(hmdead, "a");
69*7c478bd9Sstevel@tonic-gate 		if ((malf == NULL) || (aret == 0)) {
70*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
71*7c478bd9Sstevel@tonic-gate 				"%s: Cannot create %s\n",
72*7c478bd9Sstevel@tonic-gate 				program,dotdead);
73*7c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "Cannot create %s\n", dotdead);
74*7c478bd9Sstevel@tonic-gate 		out:
75*7c478bd9Sstevel@tonic-gate 			fclose(tmpf);
76*7c478bd9Sstevel@tonic-gate 			error = E_FILE;
77*7c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "error set to %d\n", error);
78*7c478bd9Sstevel@tonic-gate 			umask(7);
79*7c478bd9Sstevel@tonic-gate 			setegid(egid);
80*7c478bd9Sstevel@tonic-gate 			return;
81*7c478bd9Sstevel@tonic-gate 		}  else {
82*7c478bd9Sstevel@tonic-gate 			chmod(hmdead, DEADPERM);
83*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,"%s: Mail saved in %s\n",program,hmdead);
84*7c478bd9Sstevel@tonic-gate 		}
85*7c478bd9Sstevel@tonic-gate 	} else {
86*7c478bd9Sstevel@tonic-gate 		chmod(dotdead, DEADPERM);
87*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,"%s: Mail saved in %s\n",program,dotdead);
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	/*
91*7c478bd9Sstevel@tonic-gate 		Copy letter into dead letter box
92*7c478bd9Sstevel@tonic-gate 	*/
93*7c478bd9Sstevel@tonic-gate 	umask(7);
94*7c478bd9Sstevel@tonic-gate 	aret = fseek(tmpf,0L,0);
95*7c478bd9Sstevel@tonic-gate 	if (aret)
96*7c478bd9Sstevel@tonic-gate 		errmsg(E_DEAD,"");
97*7c478bd9Sstevel@tonic-gate 	if (!copystream(tmpf, malf))
98*7c478bd9Sstevel@tonic-gate 		errmsg(E_DEAD,"");
99*7c478bd9Sstevel@tonic-gate 	fclose(malf);
100*7c478bd9Sstevel@tonic-gate 	setegid(egid);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
savdead()103*7c478bd9Sstevel@tonic-gate void savdead()
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	static char pn[] = "savdead";
106*7c478bd9Sstevel@tonic-gate 	setsig(SIGINT, saveint);
107*7c478bd9Sstevel@tonic-gate 	dflag = 2;	/* do not send back letter on interrupt */
108*7c478bd9Sstevel@tonic-gate 	Dout(pn, 0, "dflag set to 2\n");
109*7c478bd9Sstevel@tonic-gate 	if (!error) {
110*7c478bd9Sstevel@tonic-gate 		error = E_REMOTE;
111*7c478bd9Sstevel@tonic-gate 		Dout(pn, 0, "error set to %d\n", error);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	maxerr = error;
114*7c478bd9Sstevel@tonic-gate 	Dout(pn, 0, "maxerr set to %d\n", maxerr);
115*7c478bd9Sstevel@tonic-gate }
116