/* $DBA/dbakill.c

   Executes the Unix "kill" command with root privileges.

   The username executing this program must begin with "ora".

   The one required parameter must be the Unix PID to be killed.

   Compile using:  cc dbakill.c -o dbakill
         -- or (if "Unexpected symbol" errors are displayed) --
                   c89 dbakill.c -o dbakill
   Set the required ownership and protection as follows:
       chown root dbakill
       chmod 4711 dbakill

   <<<<<<<< MODIFICATION HISTORY >>>>>>>>
     Date	Programmer	Description
   --------	---------------	-----------------------------------------------
   02/19/97	Brian Lomasky	Original
*/

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
	/* Change "strings" in the following line to "string" if on PYRAMID */
#include <string.h>

main (argc, argv)
int argc;
char *argv[];
{
  uid_t getuid(void);			/* Get uid of current user */
  struct passwd *getpwuid(uid_t uid);	/* Get passwd info for a uid */
  char *strstr(const char *s1, const char *s2); /* Search for substring pos */
  void exit(int status);		/* Exit with status */

  char cmd[99];				/* Variable to hold command string */
  struct passwd pw;			/* Structure to hold passwd info */
  char ora3[4];				/* Store char string to search for */
  char *o3;				/* String pointer from strstr funct */

  strcpy(ora3, "ora");
  if (argc = 1) {			/* If 1 argument was specified: */

    /* Ensure that the username executing this program begins with "ora" */
    pw = *getpwuid(getuid());		/* Get pointer to our passwd struct */
    /* printf("%s\n", pw.pw_name); */
    o3 = strstr(pw.pw_name, ora3);	/* Search username for ora position */
    if (pw.pw_name != o3) {		/* If pointer to "ora" not name */
      printf("%s\n","dbakill can only be executed by the ORACLE username\007");
      exit(2);				/* Exit with error status */
    }

    sprintf(cmd, "kill -9 %s\n", argv[1]); /* Format the system command */
    setuid (0);				/* Set effective UID to root user */
    system (cmd);			/* Execute the "kill" command */
  } else {
    printf("HELP for dbakill command\n");
    printf("========================\n\n");
    sprintf(cmd, "dbakill <Unix-pid-to-be-killed>\n"); 
    system (cmd);
  }
  exit(0);				/* Exit with success status */
}
