xref: /illumos-gate/usr/src/cmd/mail/mkdead.c (revision 3d7072f8bd27709dba14f6fe336f149d25d9e207)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"        /* SVr4.0 1.6 */
27 
28 #include "mail.h"
29 /*
30 	Routine creates dead.letter
31 */
32 void mkdead()
33 {
34 	static char pn[] = "mkdead";
35 	int aret;
36 	char *dotdead = &dead[1];
37 	gid_t egid = getegid();
38 	struct stat st;
39 
40 	malf = (FILE *)NULL;
41 
42 	/*
43 		Make certain that there's something to copy.
44 	*/
45 	if (!tmpf)
46 		return;
47 
48 	/*
49 		Try to create dead letter in current directory
50 		or in home directory
51 	*/
52 	umask(umsave);
53 	setgid(getgid());
54 	if ((aret = legal(dotdead)) && stat(dotdead, &st) == 0)
55 		malf = fopen(dotdead, "a");
56 	if ((malf == NULL) || (aret == 0)) {
57 		/*
58 			try to create in $HOME
59 		*/
60 		if((hmdead = malloc(strlen(home) + strlen(dead) + 1)) == NULL) {
61 			fprintf(stderr, "%s: Can't malloc\n",program);
62 			Dout(pn, 0, "Cannot malloc.\n");
63 			goto out;
64 		}
65 		cat(hmdead, home, dead);
66 		if ((aret=legal(hmdead)) && !(stat(hmdead, &st) < 0 &&
67 			errno == EOVERFLOW))
68 			malf = fopen(hmdead, "a");
69 		if ((malf == NULL) || (aret == 0)) {
70 			fprintf(stderr,
71 				"%s: Cannot create %s\n",
72 				program,dotdead);
73 			Dout(pn, 0, "Cannot create %s\n", dotdead);
74 		out:
75 			fclose(tmpf);
76 			error = E_FILE;
77 			Dout(pn, 0, "error set to %d\n", error);
78 			umask(7);
79 			setegid(egid);
80 			return;
81 		}  else {
82 			chmod(hmdead, DEADPERM);
83 			fprintf(stderr,"%s: Mail saved in %s\n",program,hmdead);
84 		}
85 	} else {
86 		chmod(dotdead, DEADPERM);
87 		fprintf(stderr,"%s: Mail saved in %s\n",program,dotdead);
88 	}
89 
90 	/*
91 		Copy letter into dead letter box
92 	*/
93 	umask(7);
94 	aret = fseek(tmpf,0L,0);
95 	if (aret)
96 		errmsg(E_DEAD,"");
97 	if (!copystream(tmpf, malf))
98 		errmsg(E_DEAD,"");
99 	fclose(malf);
100 	setegid(egid);
101 }
102 
103 void savdead()
104 {
105 	static char pn[] = "savdead";
106 	setsig(SIGINT, saveint);
107 	dflag = 2;	/* do not send back letter on interrupt */
108 	Dout(pn, 0, "dflag set to 2\n");
109 	if (!error) {
110 		error = E_REMOTE;
111 		Dout(pn, 0, "error set to %d\n", error);
112 	}
113 	maxerr = error;
114 	Dout(pn, 0, "maxerr set to %d\n", maxerr);
115 }
116