
/*
	Copyright 1993  Jeremy Slade.

	You are free to use all or any parts of the LogHook project
	however you wish, just give credit where credit is due.
	The author (Jeremy Slade) shall not be held responsible
	for any damages that result out of use or misuse of any
	part of this project.

*/

/*
	Project: LogHook

	File: LoginHook.m

	Description:

	This is a simple program intended to be run as the LoginHook of the loginWindow.  When set up as such, this program will be executed immediately after the user types their password, but before they are logged in.  This means that the program will be run with the same owner as the loginWindow, which is generally root.  A single command line parameter is specified for this program, and that is the name of the user who is logging in.
	When LoginHook is run, it sets the owner of the process to the user who is logging in, and then executes the login hook command, which is defined by LoginHook's Hook preference setting.  This preference is "~/.LoginHook" by default ("~" being the home of the user logging in), but each user can specify their own by changing the setting in their own defaults database for the owner LoginHook.
	
	Original Author: Jeremy Slade

	Revision History:
		Created
			V.101	JGS	Tue May 11 21:56:09 PDT 1993

*/


#import <defaults/defaults.h>
#import <libc.h>
#import <pwd.h>
#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import <sys/param.h>
#import <sys/types.h>


#define OPTIONS_OWNER	"LoginHook"
#define OPT_HOOK		"Hook"

NXDefaultsVector Options = {
	{ OPT_HOOK, ".LoginHook" },	// Script to execute for Login
	{ NULL, NULL }
};


void main ( int argc, char *argv[] )
{
    const char *hook;
	char hookPath[MAXPATHLEN+1];
	char theCommand[3001];
	struct passwd *pwd;
	
    // Set up the Defaults registration table
    NXRegisterDefaults ( OPTIONS_OWNER, Options );
    NXUpdateDefaults ();

    // Get the name of the user who is logging in/out
	// (should be first command-line parameter)
	// Get the user's password information, set the user id
	// of this process to that user
	pwd = getpwnam ( argv[1] );
	if ( !pwd ) {
		// Unable to get passwd info for this user
		exit ( 0 );
	}
	setuid ( pwd->pw_uid );
	
	// Change the Defaults user to the person logging in, so we can
	// find out what command they want to run as their hook...
	NXSetDefaultsUser ( argv[1] );
	NXRegisterDefaults ( OPTIONS_OWNER, Options );
	NXUpdateDefaults ();
 
    // read the OPT_HOOK to see what program we are supposed to run.
	// If it is a relatively-specified filename, make it into a full path
    hook = NXGetDefaultValue ( OPTIONS_OWNER, OPT_HOOK );
	if ( !hook )
		exit ( 0 );
	else if ( hook[0] != '/' ) {
		sprintf ( hookPath, "%s/%s", pwd->pw_dir, hook );
		hook = hookPath;
	}
	
    // Run the user's login hook script.  Make sure when this is done
	// that it acts as if the user were logged in
	// The call to system() does the following:
	//    1.  Change to the user's home directory
	//    2.  Execute the login hook
	sprintf ( theCommand, "cd %s; %s", pwd->pw_dir, hook );
	system ( theCommand );
	
	exit ( 0 );
}
