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