enhanced cron.c, anyone?

dpk at brl-vgr dpk at brl-vgr
Sat Jul 23 02:42:33 AEST 1983


From:      Doug Kingston <dpk at brl-vgr>

	On our pdp-11's we modified cron to have a username field prior
to the command.  In the interest of compatability we did not do this on
the Vaxes.  Instead, we wrote a program called "alias" which is used
like nohup or nice (e.g.  "alias user programs args ...") which if run
by the superuser (like from crontab) will run the given program as the
specified user.  All you need to is add "alias user " to the fron of the
command string in the crontab entry.

						Cheers,
							-Doug-

PS.  The following is alias.c.  Enjoy!

/*
 *			A L I A S . C
 *
 *	To compile:  cc alias.c -O -o alias
 *
 *	This program allows the superuser to run a program with
 *	arbitrary arguments with the uid/gid of any account on
 *	the system.  If no program is specified,   /bin/sh  is
 *	assumed.
 *
 *	% alias <account-name> [<program>[<args...>]]
 *
 *	R E V I S I O N   H I S T O R Y
 *
 *	05/20/78  RNJ	Any program may be run, not just shell.
 *			New /etc/passwd lookup function.
 *
 *	10/25/81  DPK	Converted to integer UIDs and GIDs & LIB7.
 *
 *	12/23/81  RSM	Fixed to not "blow away" V7 environment.
 *			Default case now sets argv[0] to "-Alias(account)"
 */

#include <stdio.h>
#include <pwd.h>

char	namebuf[30];		/* Argument 0 to new shell */

main(argc, argv)
char **argv;
{
	register char *program;	/* name of program to be exec ed   */
	register struct passwd *pw;

	if( argc <2 )  {
		printf("Usage:  %s account [<program> [<args>]]\n", argv[0]);
		exit(1);
	}
	if ((pw = getpwnam(argv[1])) == 0)  {
		printf("%s: account not found\n", argv[1]);
		exit(2);
	}

	if (setgid( pw->pw_gid ) < 0)
		perror ("setgid");
	if (setuid( pw->pw_uid ) < 0)
		perror ("setuid");

	if( argc <= 2 )  {
		program = "/bin/sh";
		printf("UID = %d; GID = %d\n", pw->pw_uid, pw->pw_gid);
		sprintf(namebuf, "-Alias(%s)", argv[1]);
		execl(program, namebuf, 0);
	} else {
		program = argv[2];
		argv[argc] = 0;
		execv(program, &argv[2]);
	}

	perror(program);
	exit(3);
}



More information about the Comp.unix.wizards mailing list