xref: /illumos-gate/usr/src/cmd/bnu/unknown.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22462be471Sceastha /*
23ace1a5f1Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24ace1a5f1Sdp  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	logs attempts by unknown remote machines to run uucico in FOREIGN
327c478bd9Sstevel@tonic-gate  *	("/var/uucp/.Admin/Foreign").  if anything goes wrong,
33*2a8bcb4eSToomas Soome  *	sends mail to login MAILTO ("uucp").  the executable should be
347c478bd9Sstevel@tonic-gate  *	placed in /usr/lib/uucp/remote.unknown, and should run setuid-uucp.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<sys/types.h>
397c478bd9Sstevel@tonic-gate #include	<time.h>
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	"uucp.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	FOREIGN	"/var/uucp/.Admin/Foreign"
447c478bd9Sstevel@tonic-gate #define	MAILTO	"uucp"
457c478bd9Sstevel@tonic-gate #define	LOGLEN	256
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate void fall_on_sword();
487c478bd9Sstevel@tonic-gate 
49462be471Sceastha int
main(argc,argv)507c478bd9Sstevel@tonic-gate main(argc, argv)
517c478bd9Sstevel@tonic-gate int	argc;
527c478bd9Sstevel@tonic-gate char	*argv[];
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	char		buf[LOGLEN], *ctoday, *logname, tmpbuf[MAXBASENAME+1];
557c478bd9Sstevel@tonic-gate 	FILE		*fp;
567c478bd9Sstevel@tonic-gate 	time_t		today;
577c478bd9Sstevel@tonic-gate 	extern char	*ctime();
587c478bd9Sstevel@tonic-gate 	extern FILE	*fopen();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if ( argc != 2 ) {
617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "USAGE: %s remotename\n", argv[0]);
627c478bd9Sstevel@tonic-gate 		exit(101);
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if ( time(&today) != -1 ) {
667c478bd9Sstevel@tonic-gate 		ctoday = ctime(&today);
677c478bd9Sstevel@tonic-gate 		*(ctoday + strlen(ctoday) - 1) = '\0';	/* no ending \n */
687c478bd9Sstevel@tonic-gate 	} else
697c478bd9Sstevel@tonic-gate 		ctoday = "NO DATE";
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	logname = cuserid((char *) NULL);
727c478bd9Sstevel@tonic-gate 	(void) strncpy(tmpbuf, argv[1], MAXBASENAME);
737c478bd9Sstevel@tonic-gate 	tmpbuf[MAXBASENAME] = '\0';
747c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof(buf), "%s: call from system %s login %s\n",
757c478bd9Sstevel@tonic-gate 		ctoday, tmpbuf, (logname == NULL ? "<unknown>" : logname));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	errno = 0;
787c478bd9Sstevel@tonic-gate 	if ( (fp = fopen(FOREIGN, "a+")) == (FILE *)NULL )
797c478bd9Sstevel@tonic-gate 		fall_on_sword("cannot open", buf);
807c478bd9Sstevel@tonic-gate 	if ( fputs(buf, fp) == EOF )
817c478bd9Sstevel@tonic-gate 		fall_on_sword("cannot write", buf);
827c478bd9Sstevel@tonic-gate 	if ( fclose(fp) != 0 )
837c478bd9Sstevel@tonic-gate 		fall_on_sword("cannot close", buf);
847c478bd9Sstevel@tonic-gate 
85462be471Sceastha 	return (0);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* don't return from here */
897c478bd9Sstevel@tonic-gate void
fall_on_sword(errmsg,logmsg)907c478bd9Sstevel@tonic-gate fall_on_sword(errmsg, logmsg)
917c478bd9Sstevel@tonic-gate char	*errmsg, *logmsg;
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	char		ebuf[BUFSIZ+1];
947c478bd9Sstevel@tonic-gate 	int		fds[2];
957c478bd9Sstevel@tonic-gate 	size_t		sz;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	(void) snprintf(ebuf, BUFSIZ,
987c478bd9Sstevel@tonic-gate 		"To: %s\nSubject: %s %s\n\n%s %s:\t%s (%d)\nlog msg:\t%s",
997c478bd9Sstevel@tonic-gate 		MAILTO, errmsg, FOREIGN, errmsg, FOREIGN,
100ace1a5f1Sdp 		strerror(errno), errno, logmsg);
1017c478bd9Sstevel@tonic-gate 	sz = strlen(ebuf);
1027c478bd9Sstevel@tonic-gate 	if (ebuf[sz-1] != '\n') {
1037c478bd9Sstevel@tonic-gate 		ebuf[sz] = '\n';
1047c478bd9Sstevel@tonic-gate 		ebuf[sz+1] = '\0';
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/* reset to real uid. get a pipe. put error message on	*/
1087c478bd9Sstevel@tonic-gate 	/* "write end" of pipe, close it. dup "read end" to	*/
1097c478bd9Sstevel@tonic-gate 	/* stdin and then execl mail (which will read the error	*/
1107c478bd9Sstevel@tonic-gate 	/* message we just wrote).				*/
1117c478bd9Sstevel@tonic-gate 
112*2a8bcb4eSToomas Soome 	if ( setuid(getuid()) == -1 || pipe(fds) != 0
1137c478bd9Sstevel@tonic-gate 	|| write(fds[1], ebuf, strlen(ebuf)) != strlen(ebuf)
1147c478bd9Sstevel@tonic-gate 	|| close(fds[1]) != 0 )
1157c478bd9Sstevel@tonic-gate 		exit(errno);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ( fds[0] != 0 ) {
1187c478bd9Sstevel@tonic-gate 		close(0);
1197c478bd9Sstevel@tonic-gate 		if ( dup(fds[0]) != 0 )
1207c478bd9Sstevel@tonic-gate 			exit(errno);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	execl("/usr/bin/mail", "mail", MAILTO, (char *) 0);
1247c478bd9Sstevel@tonic-gate 	exit(errno);	/* shouldn't get here */
1257c478bd9Sstevel@tonic-gate }
126