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