[comp.sources.misc] v06i072: tallow --- add entry to ~/.rhosts and later remove it with at

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (03/22/89)

Posting-number: Volume 6, Issue 72
Submitted-by: tcjones@watdragon.waterloo.edu (speedboat jones)
Archive-name: tallow

[In the days following the Great Internet Worm Scare, this is a darned good
idea....  ++bsa]

tallow - temporary allow. Put someone in your ~/.rhosts and arrange for them to
		 be taken out automatically (using at(1)).

Usage: tallow [-c] user host [minutes]
       tallow [-c] host user [minutes]

The -c (confirm) option sends you mail when the removal is done.
Minutes is the number of minutes you want the person to be added for.
The default is ten minutes.  The granularity of at(1) (usually 15
minutes) should be kept in mind.


Terry Jones

    Department Of Computer Science,  University Of Waterloo
    Waterloo Ontario Canada N2L 3G1. Phone: 1-519-8884674
    UUCP:                    ...!watmath!watdragon!tcjones
    CSNET, Internet, CDNnet: tcjones@dragon.waterloo.{cdn,edu}
    BITNET:                  tcjones@WATER.bitnet
    Canadian domain:         tcjones@dragon.uwaterloo.ca

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 1 (of 1)."
# Contents:  tallow tallow/Makefile tallow/README tallow/tallow.c
# Wrapped by tcjones@watdragon on Wed Mar 15 13:38:09 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test ! -d 'tallow' ; then
    echo shar: Creating directory \"'tallow'\"
    mkdir 'tallow'
fi
if test -f 'tallow/Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'tallow/Makefile'\"
else
echo shar: Extracting \"'tallow/Makefile'\" \(63 characters\)
sed "s/^X//" >'tallow/Makefile' <<'END_OF_FILE'
CFLAGS = -O
X
tallow: tallow.o
X	cc $(CFLAGS) -o tallow tallow.o
END_OF_FILE
if test 63 -ne `wc -c <'tallow/Makefile'`; then
    echo shar: \"'tallow/Makefile'\" unpacked with wrong size!
fi
# end of 'tallow/Makefile'
fi
if test -f 'tallow/README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'tallow/README'\"
else
echo shar: Extracting \"'tallow/README'\" \(801 characters\)
sed "s/^X//" >'tallow/README' <<'END_OF_FILE'
tallow - temporary allow. Put someone in your ~/.rhosts and arrange for them to
X		 be taken out automatically (using at(1)).
X
Usage: tallow [-c] user host [minutes]
X       tallow [-c] host user [minutes]
X
The -c (confirm) option sends you mail when the removal is done.
Minutes is the number of minutes you want the person to be added for.
The default is ten minutes.  The granularity of at(1) (usually 15
minutes) should be kept in mind.
X
X
Terry Jones
X
X    Department Of Computer Science,  University Of Waterloo
X    Waterloo Ontario Canada N2L 3G1. Phone: 1-519-8884674
X    UUCP:                    ...!watmath!watdragon!tcjones
X    CSNET, Internet, CDNnet: tcjones@dragon.waterloo.{cdn,edu}
X    BITNET:                  tcjones@WATER.bitnet
X    Canadian domain:         tcjones@dragon.uwaterloo.ca
END_OF_FILE
if test 801 -ne `wc -c <'tallow/README'`; then
    echo shar: \"'tallow/README'\" unpacked with wrong size!
fi
# end of 'tallow/README'
fi
if test -f 'tallow/tallow.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'tallow/tallow.c'\"
else
echo shar: Extracting \"'tallow/tallow.c'\" \(5422 characters\)
sed "s/^X//" >'tallow/tallow.c' <<'END_OF_FILE'
X/*
X * tallow.c - temporarily add someone to your .rhosts file.
X *
X * usage: tallow [-c] user host [minutes]
X *        tallow [-c] host user [minutes
X *
X *
X * tcjones@watdragon
X *
X */
X
X#include <stdio.h>
X#include <stdlib.h>
X#include <sys/types.h>
X#include <sys/time.h>
X#include <netdb.h>
X#include <pwd.h>
X
X#define DEF_AT_TIME 600			/* seconds */
X#define EDIT "/usr/ucb/edit"
X#define MAIL "/usr/ucb/mail"
X#define ECHO "/bin/echo"
X#define AT "/usr/bin/at -s"
X
main(argc, argv)
int argc;
char **argv;
X{
X	extern char *getenv();
X	extern char *mktemp();
X	extern char *index();
X	extern struct passwd *getpwuid();
X
X	FILE *rfp, *tfp, *fopen(), *fclose();
X
X	char *progname;
X	char *tmp = mktemp("/tmp/tallow.XXXXXXXXX");
X	register char *runtime;
X	register char *host;
X	register char *user;
X	char date[26];
X	char rhosts[40];
X	struct passwd *pwd;
X	register int seconds = DEF_AT_TIME;
X	int	confirm = 0;
X	struct timeval v;
X
X	/* 
X	 * This program's name.
X	 *
X	 */
X	progname = *argv++;
X
X	/*
X	 * Find out if we found out who we are etc etc.
X	 *
X	 */
X	if ((pwd = getpwuid(getuid())) == NULL){
X		fprintf(stderr, "%s: Could not getpwuid\n", progname);
X	}
X
X
X	/*
X	 * Open the temporary file that we will write the at(1) commands to
X	 * for the removal of the .rhosts entry.
X	 *
X	 */
X	if ((tfp = fopen(tmp, "w")) == NULL) {
X		printf("Could not open %s, .rhosts untouched\n", tmp);
X		diy();
X		exit(1);
X	}
X
X
X	/*
X	 * If the -c option is present, record that and move on through the
X	 * command line options as normal.
X	 *
X	 */
X	if (argc > 1 && !strcmp(*argv, "-c")) {
X		confirm = 1;
X		argc--;
X		argv++;
X	}
X
X
X	/* 
X	 * If there are 3 arguments only (e.g. tallow fred watmath) then
X	 * no time has been given so use the default. Otherwise if there
X	 * are not exactly 4 arguments (e.g. tallow fred watmath 30) we
X	 * have an error. 
X	 *
X	 */
X	if (argc != 4 && argc != 3) usage(progname);
X
X
X	/*
X	 * The host name and the user name can appear in either order. If
X	 * we can't recognise a hostname in either of them then get out.
X	 * Set up the host and user pointers appropriately...
X	 *
X	 */
X	if (gethostbyname(*argv) == NULL) {
X		if (gethostbyname(*(argv+1)) == NULL) {
X			printf("%s: neither %s nor %s are valid machine names.\n",
X				*argv, *(argv+1));
X			usage(progname);
X		}
X		else {
X			user = *argv;
X			host = *(argv+1);
X		}
X	}
X	else {
X		host = *argv;
X		user = *(argv+1);
X	}
X
X	/*
X	 * Skip over the host and user names.
X	 *
X	 */
X	argv+=2;
X
X
X	/*
X	 * Calculate the number of seconds required until the .rhosts entry
X	 * should be removed. This is either the default or 60 * whatever 
X	 * they entered.
X	 *
X	 */
X	seconds = (*argv) ?  60*(atoi(*argv)) : DEF_AT_TIME;
X
X
X	/*
X	 * Make sure everything looks ok.
X	 *
X	 */
X	if (seconds <= 0) {
X		printf("%s: Invalid time %d\n", progname, seconds);
X		exit(1);
X	}
X
X
X	/*
X	 * Try to open their .rhosts file.
X	 *
X	 */
X	sprintf(rhosts, "/u/%s/.rhosts", pwd->pw_name);
X	if ((rfp = fopen(rhosts, "a")) == NULL) {
X		printf("%s: Could not open %s\n", progname, rhosts);
X		exit(1);
X	}
X
X	/*
X	 * Add the new entry at the end and close the file.
X	 *
X	 */
X	fprintf(rfp, "%s %s\n", host, user);
X	fclose(rfp);
X
X
X	/*
X	 * Start to produce the file for at. The first thing it must do is
X	 * edit the .rhosts and remove the line. Use a here document.
X	 *
X	 */
X	fprintf(tfp, "%s>/dev/null %s<<*\ng/^%s %s$/d\nw\n*\n",
X		EDIT, rhosts, host, user);
X
X
X	/*
X	 * If they want confirmation, arrange to send them some mail
X	 * when it is done. Could send them the .rhosts file too.
X	 *
X	 */
X	if (confirm) {
X		fprintf(tfp,"%s \\\"%s %s\\\" removed from %s|%s -s %s %s\n",
X			ECHO,host,user,rhosts,MAIL,progname,pwd->pw_name);
X	}
X
X
X	/*
X	 * Close the file for at.
X	 *
X	 */
X	fclose(tfp);
X
X	/*
X	 * Get the time of day so we can work out when to schedule at for.
X	 *
X	 */
X	if (gettimeofday(&v, NULL) == -1){
X		fprintf(stderr, "%s: Could not get time of day.\n", progname);
X		diy();
X		exit(1);
X	}
X
X	/* 
X	 * Add on the required number of seconds and convert the time to a string
X	 * like "Sun Sep 16 01:03:52 1973\n\0"
X	 *
X	 */
X	v.tv_sec += seconds;
X	strcpy(date, ctime((time_t *)(&(v.tv_sec))));
X
X
X	/*
X	 * Find the first colon in the above, set the next colon (3 chars on)
X	 * to '\0', and move back a couple of charaters to the start of the 
X	 * time. Thus with the above string we'd end up with
X	 *
X	 * "Sun Sep 16 01:03'\0'52 1973\n\0"
X	 *             ^
X	 *             |
X	 *     runtime--
X	 *
X	 * With runtime pointing at the null-terminated time.
X	 *
X	 */
X	if ((runtime = index(date, ':')) == NULL){
X		fprintf(stderr, "%s: Could not get the time correctly,\n", progname);
X		diy();
X		exit(1);
X	}
X
X	*(runtime+3) = '\0';
X	runtime -= 2;
X	
X
X
X	/*
X	 * System an at command to run the file at the right time.
X	 * You need to use at -s since we are using a here document and
X	 * csh (and its friends) won't like that.
X	 *
X	 */
X	{char cmd[100];  /* Can't possibly be this long. */
X		sprintf(cmd, "%s %s %s", AT, runtime, tmp);
X		if (system(cmd)) {
X			printf("%s: %s fails!\n", progname, cmd);
X			diy();
X		}
X	}
X
X
X	/*
X	 * Get rid of the temporary.
X	 *
X	 */
X	if (unlink(tmp) == -1) {
X		printf("%s: Warning, could not remove %s\n", progname, tmp);
X		exit(1);
X	}
X
X	return 0;
X}
X
diy()
X{
X	/*
X	 * Complain.
X	 *
X	 */
X	fprintf(stderr, "Cannot arrange for automatic .rhosts removal!\n");
X	fprintf(stderr, "Do it yourself.\n");
X}
X
usage(me)
char *me;
X{
X	/*
X	 * Complain bitterly.
X	 *
X	 */
X	fprintf(stderr, "Usage: %s [-c] host user [minutes]\n", me);
X	exit(1);
X}
END_OF_FILE
if test 5422 -ne `wc -c <'tallow/tallow.c'`; then
    echo shar: \"'tallow/tallow.c'\" unpacked with wrong size!
fi
# end of 'tallow/tallow.c'
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have the archive.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0