home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Utilities / LogHooks-1.0-MIHS / LogoutHook.m < prev    next >
Encoding:
Text File  |  1997-02-19  |  3.0 KB  |  99 lines

  1.  
  2. /*
  3.     Copyright 1993  Jeremy Slade.
  4.  
  5.     You are free to use all or any parts of the LogHook project
  6.     however you wish, just give credit where credit is due.
  7.     The author (Jeremy Slade) shall not be held responsible
  8.     for any damages that result out of use or misuse of any
  9.     part of this project.
  10.  
  11. */
  12.  
  13. /*
  14.     Project: LogHook
  15.  
  16.     File: LogoutHook.m
  17.  
  18.     Description:
  19.  
  20.     This is a simple program intended to be run as the LogoutHook of the loginWindow.  When set up as such, this program will be executed immediately after the user logs out, before the loginWindow comes up again.  The program will be rin 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.
  21.     When LogoutHook is run, it sets the owner of the process to the user who is logging out, and then executes the logout hook command, which is defined by LogoutHook's Hook preference setting.  This preference is "~/.LogoutHook" by default ("~" beging the home of the user logging out), but each user can specify their own by changing the setting in their own defaults database for the owner LogoutHook.
  22.     
  23.     Original Author: Jeremy Slade
  24.  
  25.     Revision History:
  26.         Created
  27.             V.101    JGS    Tue May 11 21:56:09 PDT 1993
  28.  
  29. */
  30.  
  31.  
  32. #import <defaults/defaults.h>
  33. #import <libc.h>
  34. #import <pwd.h>
  35. #import <stdio.h>
  36. #import <stdlib.h>
  37. #import <string.h>
  38. #import <sys/param.h>
  39. #import <sys/types.h>
  40.  
  41.  
  42. #define OPTIONS_OWNER    "LogoutHook"
  43. #define OPT_HOOK        "Hook"
  44.  
  45. NXDefaultsVector Options = {
  46.     { OPT_HOOK, ".LogoutHook" },    // Script to execute for Logout
  47.     { NULL, NULL }
  48. };
  49.  
  50.  
  51. void main ( int argc, char *argv[] )
  52. {
  53.     const char *hook;
  54.     char hookPath[MAXPATHLEN+1];
  55.     char theCommand[3001];
  56.     struct passwd *pwd;
  57.     
  58.     // Set up the Defaults registration table
  59.     NXRegisterDefaults ( OPTIONS_OWNER, Options );
  60.     NXUpdateDefaults ();
  61.  
  62.     // Get the name of the user who is logging in/out
  63.     // (should be first command-line parameter)
  64.     // Get the user's password information, set the user id
  65.     // of this process to that user
  66.     pwd = getpwnam ( argv[1] );
  67.     if ( !pwd ) {
  68.         // Unable to get passwd info for this user
  69.         exit ( 0 );
  70.     }
  71.     setuid ( pwd->pw_uid );
  72.     
  73.     // Change the Defaults user to the person logging in, so we can
  74.     // find out what command they want to run as their hook...
  75.     NXSetDefaultsUser ( argv[1] );
  76.     NXRegisterDefaults ( OPTIONS_OWNER, Options );
  77.     NXUpdateDefaults ();
  78.  
  79.     // read the OPT_HOOK to see what program we are supposed to run.
  80.     // If it is a relatively-specified filename, make it into a full path
  81.     hook = NXGetDefaultValue ( OPTIONS_OWNER, OPT_HOOK );
  82.     if ( !hook )
  83.         exit ( 0 );
  84.     else if ( hook[0] != '/' ) {
  85.         sprintf ( hookPath, "%s/%s", pwd->pw_dir, hook );
  86.         hook = hookPath;
  87.     }
  88.     
  89.     // Run the user's Logout hook script.  Make sure when this is done
  90.     // that it acts as if the user were logged in
  91.     // The call to system() does the following:
  92.     //    1.  Change to the user's home directory
  93.     //    2.  Execute the Logout hook
  94.     sprintf ( theCommand, "cd %s; %s", pwd->pw_dir, hook );
  95.     system ( theCommand );
  96.     
  97.     exit ( 0 );
  98. }
  99.