unknown.c revision 462be471126495414a94f9fa35e16c02dc462c04
1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License, Version 1.0 only
1N/A * (the "License"). You may not use this file except in compliance
1N/A * with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A/*
1N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
1N/A/* All Rights Reserved */
1N/A
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A/*
1N/A * logs attempts by unknown remote machines to run uucico in FOREIGN
1N/A * ("/var/uucp/.Admin/Foreign"). if anything goes wrong,
1N/A * sends mail to login MAILTO ("uucp"). the executable should be
1N/A * placed in /usr/lib/uucp/remote.unknown, and should run setuid-uucp.
1N/A */
1N/A
1N/A#include <stdio.h>
1N/A#include <sys/types.h>
1N/A#include <time.h>
1N/A#include <errno.h>
1N/A#include "uucp.h"
1N/A
1N/A#define FOREIGN "/var/uucp/.Admin/Foreign"
1N/A#define MAILTO "uucp"
1N/A#define LOGLEN 256
1N/A
1N/Avoid fall_on_sword();
1N/A
1N/Aint
1N/Amain(argc, argv)
1N/Aint argc;
1N/Achar *argv[];
1N/A{
1N/A char buf[LOGLEN], *ctoday, *logname, tmpbuf[MAXBASENAME+1];
1N/A FILE *fp;
1N/A time_t today;
1N/A extern char *ctime();
1N/A extern FILE *fopen();
1N/A
1N/A if ( argc != 2 ) {
1N/A (void) fprintf(stderr, "USAGE: %s remotename\n", argv[0]);
1N/A exit(101);
1N/A }
1N/A
1N/A if ( time(&today) != -1 ) {
1N/A ctoday = ctime(&today);
1N/A *(ctoday + strlen(ctoday) - 1) = '\0'; /* no ending \n */
1N/A } else
1N/A ctoday = "NO DATE";
1N/A
1N/A logname = cuserid((char *) NULL);
1N/A (void) strncpy(tmpbuf, argv[1], MAXBASENAME);
1N/A tmpbuf[MAXBASENAME] = '\0';
1N/A (void) snprintf(buf, sizeof(buf), "%s: call from system %s login %s\n",
1N/A ctoday, tmpbuf, (logname == NULL ? "<unknown>" : logname));
1N/A
1N/A errno = 0;
1N/A if ( (fp = fopen(FOREIGN, "a+")) == (FILE *)NULL )
1N/A fall_on_sword("cannot open", buf);
1N/A if ( fputs(buf, fp) == EOF )
1N/A fall_on_sword("cannot write", buf);
1N/A if ( fclose(fp) != 0 )
1N/A fall_on_sword("cannot close", buf);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/* don't return from here */
1N/Avoid
1N/Afall_on_sword(errmsg, logmsg)
1N/Achar *errmsg, *logmsg;
1N/A{
1N/A char ebuf[BUFSIZ+1];
1N/A int fds[2];
1N/A size_t sz;
1N/A
1N/A (void) snprintf(ebuf, BUFSIZ,
1N/A "To: %s\nSubject: %s %s\n\n%s %s:\t%s (%d)\nlog msg:\t%s",
1N/A MAILTO, errmsg, FOREIGN, errmsg, FOREIGN,
1N/A strerror(errno), errno, logmsg);
1N/A sz = strlen(ebuf);
1N/A if (ebuf[sz-1] != '\n') {
1N/A ebuf[sz] = '\n';
1N/A ebuf[sz+1] = '\0';
1N/A }
1N/A
1N/A /* reset to real uid. get a pipe. put error message on */
1N/A /* "write end" of pipe, close it. dup "read end" to */
1N/A /* stdin and then execl mail (which will read the error */
1N/A /* message we just wrote). */
1N/A
1N/A if ( setuid(getuid()) == -1 || pipe(fds) != 0
1N/A || write(fds[1], ebuf, strlen(ebuf)) != strlen(ebuf)
1N/A || close(fds[1]) != 0 )
1N/A exit(errno);
1N/A
1N/A if ( fds[0] != 0 ) {
1N/A close(0);
1N/A if ( dup(fds[0]) != 0 )
1N/A exit(errno);
1N/A }
execl("/usr/bin/mail", "mail", MAILTO, (char *) 0);
exit(errno); /* shouldn't get here */
}