home *** CD-ROM | disk | FTP | other *** search
- /* root.c - Execute a command as superuser
- *
- * SYNOPSIS
- * root <command> [<parameter> ...]
- */
-
- static char rcsid[] = "$Header: root.c 1.2 91/04/29 $" ;
-
- #include <grp.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/param.h> /* For NGROUPS */
-
- extern char *getenv () ;
- extern int setenv () ;
- extern int setuid () ;
- extern int setgid () ;
- extern int initgroups () ;
- extern char *search_path () ;
- extern char *malloc () ;
-
- #ifndef TRUE
- #define TRUE 1
- #define FALSE 0
- #endif
- #define NULL_CP (char *) 0
- #define EOS '\0'
- #define fprint(f, s) fputs (s, f)
-
- static char *program_name ;
-
- /*
- * If this is defined, the user must be a member of AUTHORIZED_GROUP
- * before they are allowed to execute their desired command as
- * super-user.
- */
- #ifndef AUTHORIZED_GROUP
- #define AUTHORIZED_GROUP "root"
- #endif
-
- /*
- * These directories are added to the PATH environment variable before
- * the specified command is executed. This must be terminated by a
- * null pointer.
- */
- static char *dirs_for_path[] = { "/etc", "/usr/etc", NULL_CP } ;
-
-
- static void add_to_path (path, element)
- char *path, *element ;
- {
- char *path_p ;
- char *elem_p ;
- int elem_l = strlen (element) ;
-
- if ((path_p = malloc (strlen (path) + 3) + elem_l) == NULL_CP) {
- perror2 (program_name, "malloc") ;
- exit (1) ; }
- if ((elem_p = malloc (elem_l + 3)) == NULL_CP) {
- perror2 (program_name, "malloc") ;
- exit (1) ; }
-
- *path_p++ = ':' ;
- strcpy (path_p, path) ;
- strcat (path_p, ":") ;
- *elem_p++ = ':' ;
- strcpy (elem_p, element) ;
- strcat (elem_p, ":") ;
- if (strstr (--path_p, --elem_p) == NULL_CP) {
- /* Not already in path, so add it */
- strcat (path_p, element) ;
- strcpy (path, ++path_p) ; }
- free (path_p) ;
- free (elem_p) ;
- }
-
-
- int main (argc, argv)
- int argc ;
- char *argv[] ;
- {
- static char path[256] ; /* This may need to be bigger */
- char *exec_this ;
- char **dirs ;
- #ifdef AUTHORIZED_GROUP
- int no_groups ;
- int group_list[NGROUPS] ;
- struct group *auth_group ;
- int i ;
- int member ;
- #endif
-
- program_name = argv[0] ;
- if (argc < 2) {
- fprint (stderr, "usage: ") ;
- fprint (stderr, program_name) ;
- fprint (stderr, " <command> [<parameter> ...]\n") ;
- exit (2) ; }
-
- #ifdef AUTHORIZED_GROUP
- /* If this code is used, only members of the AUTHORIZED_GROUP are
- given the privileges offered by this program. */
-
- if ((no_groups = getgroups (NGROUPS, group_list)) == -1) {
- perror2 (program_name, "getgroups") ;
- exit (1) ; }
- if ((auth_group = getgrnam (AUTHORIZED_GROUP))
- == (struct group *) NULL) {
- perror2 (program_name, "getgrnam") ;
- exit (1) ; }
-
- for (i = 0, member = FALSE ; i < no_groups && (! member) ; i++) {
- member = group_list[i] == auth_group->gr_gid ; }
-
- if (! member) {
- fprint (stderr, program_name) ;
- #ifdef NOT_SECRET
- fprint (stderr, ": not a member of \"") ;
- fprint (stderr, AUTHORIZED_GROUP) ;
- fprint (stderr, "\" group\n") ;
- #else
- /* Perhaps the method of authorization is privileged information? */
- fprint (stderr, ": not authorized\n") ;
- #endif
- exit (1) ; }
- #endif
-
- if (initgroups ("root", 0) == 1) {
- fprint (stderr, program_name) ;
- fprint (stderr, ": not super-user\n") ;
- exit (1) ; }
- if (setgid (0) == -1) {
- perror2 (program_name, "setgid") ;
- exit (1) ; }
- if (setuid (0) == -1) {
- perror2 (program_name, "setuid") ;
- exit (1) ; }
-
- strcat (path, getenv ("PATH")) ;
- for (dirs = dirs_for_path ; *dirs != NULL_CP ; dirs++)
- add_to_path (path, *dirs) ;
- if (setenv ("PATH", path, TRUE) == -1) {
- perror2 (program_name, "setenv") ;
- exit (1) ; }
-
- if (execvp (argv[1], &argv[1]) != 0) {
- perror2 (program_name, argv[1]) ;
- exit (1) ; }
- exit (0) ;
- }
-