xref: /titanic_52/usr/src/cmd/bnu/mailst.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  * Copyright (c) 2000 by Sun Microsystems, Inc.
27*7c478bd9Sstevel@tonic-gate  * All rights reserved.
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:mailst.c 2.7 */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "uucp.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate extern int xfappend();
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  * fork and execute a mail command sending
38*7c478bd9Sstevel@tonic-gate  * string (str) to user (user).
39*7c478bd9Sstevel@tonic-gate  * If file is non-null, the file is also sent.
40*7c478bd9Sstevel@tonic-gate  * (this is used for mail returned to sender.)
41*7c478bd9Sstevel@tonic-gate  *	user	 -> user to send mail to
42*7c478bd9Sstevel@tonic-gate  *	subj	 -> subject for the mail
43*7c478bd9Sstevel@tonic-gate  *	str	 -> string mailed to user
44*7c478bd9Sstevel@tonic-gate  *	infile	 -> optional stdin mailed to user
45*7c478bd9Sstevel@tonic-gate  *	errfile	 -> optional stderr mailed to user
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate void
48*7c478bd9Sstevel@tonic-gate mailst(user, subj, str, infile, errfile)
49*7c478bd9Sstevel@tonic-gate char *user, *subj, *str, *infile, *errfile;
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	register FILE *fp, *fi;
52*7c478bd9Sstevel@tonic-gate 	char cmd[BUFSIZ];
53*7c478bd9Sstevel@tonic-gate 	char subject[BUFSIZ];
54*7c478bd9Sstevel@tonic-gate 	char *c;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	/* get rid of some stuff that could be dangerous */
57*7c478bd9Sstevel@tonic-gate 	if ( (c = strpbrk(user, Shchar)) != NULL) {
58*7c478bd9Sstevel@tonic-gate 		*c = NULLCHAR;
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	/* limit subject to one line */
62*7c478bd9Sstevel@tonic-gate 	if ((c = strchr(subj, '\n')) != NULL) {
63*7c478bd9Sstevel@tonic-gate 		strncpy(subject, subj, c-subj);
64*7c478bd9Sstevel@tonic-gate 		subject[c-subj] = NULLCHAR;
65*7c478bd9Sstevel@tonic-gate 		subj = subject;
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd, sizeof (cmd), "%s %s '%s'", PATH, MAIL, user);
69*7c478bd9Sstevel@tonic-gate 	if ((fp = popen(cmd, "w")) == NULL)
70*7c478bd9Sstevel@tonic-gate 		return;
71*7c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "To: %s\nSubject: %s\n\n%s\n", user, subj, str);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/* copy back stderr */
74*7c478bd9Sstevel@tonic-gate 	if (*errfile != '\0' && NOTEMPTY(errfile) && (fi = fopen(errfile, "r")) != NULL) {
75*7c478bd9Sstevel@tonic-gate 		fputs(gettext("\n\t===== stderr was =====\n"), fp);
76*7c478bd9Sstevel@tonic-gate 		if (xfappend(fi, fp) != SUCCESS)
77*7c478bd9Sstevel@tonic-gate 			fputs(gettext("\n\t===== well, i tried =====\n"), fp);
78*7c478bd9Sstevel@tonic-gate 		(void) fclose(fi);
79*7c478bd9Sstevel@tonic-gate 		fputc('\n', fp);
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	/* copy back stdin */
83*7c478bd9Sstevel@tonic-gate  	if (*infile) {
84*7c478bd9Sstevel@tonic-gate  		if (!NOTEMPTY(infile))
85*7c478bd9Sstevel@tonic-gate  			fputs(gettext("\n\t===== stdin was empty =====\n"), fp);
86*7c478bd9Sstevel@tonic-gate  		else if (chkpth(infile, CK_READ) == FAIL) {
87*7c478bd9Sstevel@tonic-gate  			fputs(gettext( "\n\t===== stdin was"
88*7c478bd9Sstevel@tonic-gate 			    " denied read permission =====\n"), fp);
89*7c478bd9Sstevel@tonic-gate 			snprintf(cmd, sizeof (cmd),
90*7c478bd9Sstevel@tonic-gate 				"user %s, stdin %s", user, infile);
91*7c478bd9Sstevel@tonic-gate 			logent(cmd, "DENIED");
92*7c478bd9Sstevel@tonic-gate 		}
93*7c478bd9Sstevel@tonic-gate  		else if ((fi = fopen(infile, "r")) == NULL) {
94*7c478bd9Sstevel@tonic-gate  			fputs(gettext(
95*7c478bd9Sstevel@tonic-gate 			    "\n\t===== stdin was unreadable =====\n"), fp);
96*7c478bd9Sstevel@tonic-gate 			snprintf(cmd, sizeof (cmd),
97*7c478bd9Sstevel@tonic-gate 				"user %s, stdin %s", user, infile);
98*7c478bd9Sstevel@tonic-gate 			logent(cmd, "UNREADABLE");
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate  		else {
101*7c478bd9Sstevel@tonic-gate  			fputs(gettext("\n\t===== stdin was =====\n"), fp);
102*7c478bd9Sstevel@tonic-gate  			if (xfappend(fi, fp) != SUCCESS)
103*7c478bd9Sstevel@tonic-gate  				fputs(gettext(
104*7c478bd9Sstevel@tonic-gate 				    "\n\t===== well, i tried =====\n"), fp);
105*7c478bd9Sstevel@tonic-gate  			(void) fclose(fi);
106*7c478bd9Sstevel@tonic-gate  		}
107*7c478bd9Sstevel@tonic-gate 		fputc('\n', fp);
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	(void) pclose(fp);
111*7c478bd9Sstevel@tonic-gate 	return;
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate #ifndef	V7
114*7c478bd9Sstevel@tonic-gate static char un[2*NAMESIZE];
115*7c478bd9Sstevel@tonic-gate void
116*7c478bd9Sstevel@tonic-gate setuucp(p)
117*7c478bd9Sstevel@tonic-gate char *p;
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate    char **envp;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate     envp = Env;
122*7c478bd9Sstevel@tonic-gate     for ( ; *envp; envp++) {
123*7c478bd9Sstevel@tonic-gate 	if(PREFIX("LOGNAME", *envp)) {
124*7c478bd9Sstevel@tonic-gate 	    (void) snprintf(un, sizeof (un), "LOGNAME=%s",p);
125*7c478bd9Sstevel@tonic-gate 	    envp[0] = &un[0];
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate     }
128*7c478bd9Sstevel@tonic-gate    return;
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate #else
131*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
132*7c478bd9Sstevel@tonic-gate void
133*7c478bd9Sstevel@tonic-gate setuucp(p) char	*p; {}
134*7c478bd9Sstevel@tonic-gate #endif
135