xref: /titanic_44/usr/src/cmd/bnu/unknown.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
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 int
52 main(argc, argv)
53 int	argc;
54 char	*argv[];
55 {
56 	char		buf[LOGLEN], *ctoday, *logname, tmpbuf[MAXBASENAME+1];
57 	FILE		*fp;
58 	time_t		today;
59 	extern char	*ctime();
60 	extern FILE	*fopen();
61 
62 	if ( argc != 2 ) {
63 		(void) fprintf(stderr, "USAGE: %s remotename\n", argv[0]);
64 		exit(101);
65 	}
66 
67 	if ( time(&today) != -1 ) {
68 		ctoday = ctime(&today);
69 		*(ctoday + strlen(ctoday) - 1) = '\0';	/* no ending \n */
70 	} else
71 		ctoday = "NO DATE";
72 
73 	logname = cuserid((char *) NULL);
74 	(void) strncpy(tmpbuf, argv[1], MAXBASENAME);
75 	tmpbuf[MAXBASENAME] = '\0';
76 	(void) snprintf(buf, sizeof(buf), "%s: call from system %s login %s\n",
77 		ctoday, tmpbuf, (logname == NULL ? "<unknown>" : logname));
78 
79 	errno = 0;
80 	if ( (fp = fopen(FOREIGN, "a+")) == (FILE *)NULL )
81 		fall_on_sword("cannot open", buf);
82 	if ( fputs(buf, fp) == EOF )
83 		fall_on_sword("cannot write", buf);
84 	if ( fclose(fp) != 0 )
85 		fall_on_sword("cannot close", buf);
86 
87 	return (0);
88 }
89 
90 /* don't return from here */
91 void
92 fall_on_sword(errmsg, logmsg)
93 char	*errmsg, *logmsg;
94 {
95 	char		ebuf[BUFSIZ+1];
96 	int		fds[2];
97 	size_t		sz;
98 
99 	(void) snprintf(ebuf, BUFSIZ,
100 		"To: %s\nSubject: %s %s\n\n%s %s:\t%s (%d)\nlog msg:\t%s",
101 		MAILTO, errmsg, FOREIGN, errmsg, FOREIGN,
102 		strerror(errno), errno, logmsg);
103 	sz = strlen(ebuf);
104 	if (ebuf[sz-1] != '\n') {
105 		ebuf[sz] = '\n';
106 		ebuf[sz+1] = '\0';
107 	}
108 
109 	/* reset to real uid. get a pipe. put error message on	*/
110 	/* "write end" of pipe, close it. dup "read end" to	*/
111 	/* stdin and then execl mail (which will read the error	*/
112 	/* message we just wrote).				*/
113 
114 	if ( setuid(getuid()) == -1 || pipe(fds) != 0
115 	|| write(fds[1], ebuf, strlen(ebuf)) != strlen(ebuf)
116 	|| close(fds[1]) != 0 )
117 		exit(errno);
118 
119 	if ( fds[0] != 0 ) {
120 		close(0);
121 		if ( dup(fds[0]) != 0 )
122 			exit(errno);
123 	}
124 
125 	execl("/usr/bin/mail", "mail", MAILTO, (char *) 0);
126 	exit(errno);	/* shouldn't get here */
127 }
128