xref: /titanic_51/usr/src/cmd/bnu/unknown.c (revision 23961e2ba54bde2ae83edb46c7c951bf9baa6496)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
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"
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 
100 	(void) snprintf(ebuf, BUFSIZ,
101 		"To: %s\nSubject: %s %s\n\n%s %s:\t%s (%d)\nlog msg:\t%s",
102 		MAILTO, errmsg, FOREIGN, errmsg, FOREIGN,
103 		strerror(errno), errno, logmsg);
104 	sz = strlen(ebuf);
105 	if (ebuf[sz-1] != '\n') {
106 		ebuf[sz] = '\n';
107 		ebuf[sz+1] = '\0';
108 	}
109 
110 	/* reset to real uid. get a pipe. put error message on	*/
111 	/* "write end" of pipe, close it. dup "read end" to	*/
112 	/* stdin and then execl mail (which will read the error	*/
113 	/* message we just wrote).				*/
114 
115 	if ( setuid(getuid()) == -1 || pipe(fds) != 0
116 	|| write(fds[1], ebuf, strlen(ebuf)) != strlen(ebuf)
117 	|| close(fds[1]) != 0 )
118 		exit(errno);
119 
120 	if ( fds[0] != 0 ) {
121 		close(0);
122 		if ( dup(fds[0]) != 0 )
123 			exit(errno);
124 	}
125 
126 	execl("/usr/bin/mail", "mail", MAILTO, (char *) 0);
127 	exit(errno);	/* shouldn't get here */
128 }
129