xref: /titanic_44/usr/src/cmd/bnu/unknown.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
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 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma	ident	"%Z%%M%	%I%	%E% SMI" /* from SVR4 bnu:unknown.c 1.8 */
32 /*
33  *	logs attempts by unknown remote machines to run uucico in FOREIGN
34  *	("/var/uucp/.Admin/Foreign").  if anything goes wrong,
35  *	sends mail to login MAILTO ("uucp").  the executable should be
36  *	placed in /usr/lib/uucp/remote.unknown, and should run setuid-uucp.
37  */
38 
39 #include	<stdio.h>
40 #include	<sys/types.h>
41 #include	<time.h>
42 #include	<errno.h>
43 #include	"uucp.h"
44 
45 #define	FOREIGN	"/var/uucp/.Admin/Foreign"
46 #define	MAILTO	"uucp"
47 #define	LOGLEN	256
48 
49 void fall_on_sword();
50 
51 main(argc, argv)
52 int	argc;
53 char	*argv[];
54 {
55 	char		buf[LOGLEN], *ctoday, *logname, tmpbuf[MAXBASENAME+1];
56 	FILE		*fp;
57 	time_t		today;
58 	extern char	*ctime();
59 	extern FILE	*fopen();
60 
61 	if ( argc != 2 ) {
62 		(void) fprintf(stderr, "USAGE: %s remotename\n", argv[0]);
63 		exit(101);
64 	}
65 
66 	if ( time(&today) != -1 ) {
67 		ctoday = ctime(&today);
68 		*(ctoday + strlen(ctoday) - 1) = '\0';	/* no ending \n */
69 	} else
70 		ctoday = "NO DATE";
71 
72 	logname = cuserid((char *) NULL);
73 	(void) strncpy(tmpbuf, argv[1], MAXBASENAME);
74 	tmpbuf[MAXBASENAME] = '\0';
75 	(void) snprintf(buf, sizeof(buf), "%s: call from system %s login %s\n",
76 		ctoday, tmpbuf, (logname == NULL ? "<unknown>" : logname));
77 
78 	errno = 0;
79 	if ( (fp = fopen(FOREIGN, "a+")) == (FILE *)NULL )
80 		fall_on_sword("cannot open", buf);
81 	if ( fputs(buf, fp) == EOF )
82 		fall_on_sword("cannot write", buf);
83 	if ( fclose(fp) != 0 )
84 		fall_on_sword("cannot close", buf);
85 
86 	exit(0);
87 
88 	/* NOTREACHED */
89 }
90 
91 /* don't return from here */
92 void
93 fall_on_sword(errmsg, logmsg)
94 char	*errmsg, *logmsg;
95 {
96 	char		ebuf[BUFSIZ+1];
97 	int		fds[2];
98 	size_t		sz;
99 	extern int	sys_nerr;
100 	extern char	*sys_errlist[];
101 
102 
103 	(void) snprintf(ebuf, BUFSIZ,
104 		"To: %s\nSubject: %s %s\n\n%s %s:\t%s (%d)\nlog msg:\t%s",
105 		MAILTO, errmsg, FOREIGN, errmsg, FOREIGN,
106 		(errno < sys_nerr ? sys_errlist[errno] : "Unknown error "),
107 		errno, logmsg);
108 	sz = strlen(ebuf);
109 	if (ebuf[sz-1] != '\n') {
110 		ebuf[sz] = '\n';
111 		ebuf[sz+1] = '\0';
112 	}
113 
114 	/* reset to real uid. get a pipe. put error message on	*/
115 	/* "write end" of pipe, close it. dup "read end" to	*/
116 	/* stdin and then execl mail (which will read the error	*/
117 	/* message we just wrote).				*/
118 
119 	if ( setuid(getuid()) == -1 || pipe(fds) != 0
120 	|| write(fds[1], ebuf, strlen(ebuf)) != strlen(ebuf)
121 	|| close(fds[1]) != 0 )
122 		exit(errno);
123 
124 	if ( fds[0] != 0 ) {
125 		close(0);
126 		if ( dup(fds[0]) != 0 )
127 			exit(errno);
128 	}
129 
130 	execl("/usr/bin/mail", "mail", MAILTO, (char *) 0);
131 	exit(errno);	/* shouldn't get here */
132 }
133