home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3249 / root.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-29  |  3.8 KB  |  151 lines

  1. /* root.c -    Execute a command as superuser
  2.  *
  3.  * SYNOPSIS
  4.  *    root <command> [<parameter> ...]
  5.  */
  6.  
  7. static char rcsid[] = "$Header: root.c 1.2 91/04/29 $" ;
  8.  
  9. #include <grp.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <sys/param.h>        /* For NGROUPS */
  13.  
  14. extern char *getenv () ;
  15. extern int setenv () ;
  16. extern int setuid () ;
  17. extern int setgid () ;
  18. extern int initgroups () ;
  19. extern char *search_path () ;
  20. extern char *malloc () ;
  21.  
  22. #ifndef TRUE
  23. #define TRUE        1
  24. #define FALSE        0
  25. #endif
  26. #define NULL_CP        (char *) 0
  27. #define EOS        '\0'
  28. #define fprint(f, s)    fputs (s, f)
  29.  
  30. static char *program_name ;
  31.  
  32. /*
  33.  * If this is defined, the user must be a member of AUTHORIZED_GROUP
  34.  * before they are allowed to execute their desired command as
  35.  * super-user.
  36.  */
  37. #ifndef    AUTHORIZED_GROUP
  38. #define    AUTHORIZED_GROUP    "root"
  39. #endif
  40.  
  41. /*
  42.  * These directories are added to the PATH environment variable before
  43.  * the specified command is executed.  This must be terminated by a
  44.  * null pointer.
  45.  */
  46. static char *dirs_for_path[] = { "/etc", "/usr/etc", NULL_CP } ;
  47.  
  48.  
  49. static void add_to_path (path, element)
  50.    char *path, *element ;
  51. {
  52.    char *path_p ;
  53.    char *elem_p ;
  54.    int elem_l = strlen (element) ;
  55.  
  56.    if ((path_p = malloc (strlen (path) + 3) + elem_l) == NULL_CP) {
  57.       perror2 (program_name, "malloc") ;
  58.       exit (1) ; }
  59.    if ((elem_p = malloc (elem_l + 3)) == NULL_CP) {
  60.       perror2 (program_name, "malloc") ;
  61.       exit (1) ; }
  62.  
  63.    *path_p++ = ':' ;
  64.    strcpy (path_p, path) ;
  65.    strcat (path_p, ":") ;
  66.    *elem_p++ = ':' ;
  67.    strcpy (elem_p, element) ;
  68.    strcat (elem_p, ":") ;
  69.    if (strstr (--path_p, --elem_p) == NULL_CP) {
  70.       /* Not already in path, so add it */
  71.       strcat (path_p, element) ;
  72.       strcpy (path, ++path_p) ; }
  73.    free (path_p) ;
  74.    free (elem_p) ;
  75.    }
  76.  
  77.  
  78. int main (argc, argv)
  79.    int argc ;
  80.    char *argv[] ;
  81.    {
  82.       static char path[256] ;        /* This may need to be bigger */
  83.       char *exec_this ;
  84.       char **dirs ;
  85. #ifdef AUTHORIZED_GROUP
  86.       int no_groups ;
  87.       int group_list[NGROUPS] ;
  88.       struct group *auth_group ;
  89.       int i ;
  90.       int member ;
  91. #endif
  92.  
  93.       program_name = argv[0] ;
  94.       if (argc < 2) {
  95.      fprint (stderr, "usage: ") ;
  96.      fprint (stderr, program_name) ;
  97.      fprint (stderr, " <command> [<parameter> ...]\n") ;
  98.          exit (2) ; }
  99.  
  100. #ifdef AUTHORIZED_GROUP
  101.       /* If this code is used, only members of the AUTHORIZED_GROUP are
  102.      given the privileges offered by this program. */
  103.  
  104.       if ((no_groups = getgroups (NGROUPS, group_list)) == -1) {
  105.      perror2 (program_name, "getgroups") ;
  106.      exit (1) ; }
  107.       if ((auth_group = getgrnam (AUTHORIZED_GROUP))
  108.             == (struct group *) NULL) {
  109.      perror2 (program_name, "getgrnam") ;
  110.      exit (1) ; }
  111.  
  112.       for (i = 0, member = FALSE ; i < no_groups && (! member) ; i++) {
  113.      member = group_list[i] == auth_group->gr_gid ; }
  114.  
  115.       if (! member) {
  116.      fprint (stderr, program_name) ;
  117. #ifdef NOT_SECRET
  118.      fprint (stderr, ": not a member of \"") ;
  119.      fprint (stderr, AUTHORIZED_GROUP) ;
  120.      fprint (stderr, "\" group\n") ;
  121. #else
  122.      /* Perhaps the method of authorization is privileged information? */
  123.      fprint (stderr, ": not authorized\n") ;
  124. #endif
  125.      exit (1) ; }
  126. #endif
  127.  
  128.       if (initgroups ("root", 0) == 1) {
  129.      fprint (stderr, program_name) ;
  130.      fprint (stderr, ": not super-user\n") ;
  131.      exit (1) ; }
  132.       if (setgid (0) == -1) {
  133.      perror2 (program_name, "setgid") ;
  134.          exit (1) ; }
  135.       if (setuid (0) == -1) {
  136.          perror2 (program_name, "setuid") ;
  137.          exit (1) ; }
  138.  
  139.       strcat (path, getenv ("PATH")) ;
  140.       for (dirs = dirs_for_path ; *dirs != NULL_CP ; dirs++)
  141.      add_to_path (path, *dirs) ;
  142.       if (setenv ("PATH", path, TRUE) == -1) {
  143.      perror2 (program_name, "setenv") ;
  144.      exit (1) ; }
  145.  
  146.       if (execvp (argv[1], &argv[1]) != 0) {
  147.          perror2 (program_name, argv[1]) ;
  148.          exit (1) ; }
  149.       exit (0) ;
  150.       }
  151.