home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / admin / 4321 < prev    next >
Encoding:
Text File  |  1992-07-25  |  5.4 KB  |  159 lines

  1. Path: sparky!uunet!cedb!dan
  2. From: dan@cedb.dpcsys.org (Dan Busarow)
  3. Newsgroups: comp.unix.admin
  4. Subject: Re: allowing users root access to specific commands
  5. Summary: try this
  6. Message-ID: <276@cedb.dpcsys.org>
  7. Date: 25 Jul 92 18:59:52 GMT
  8. References: <1992Jul24.130911.6009@dragon.acadiau.ca>
  9. Organization: DPC Systems, Monrovia, CA
  10. Lines: 147
  11.  
  12. In article <1992Jul24.130911.6009@dragon.acadiau.ca>, alan@dragon.acadiau.ca (Alan McKay) writes:
  13. > Does anyone know of a way I can give root access to specific users for
  14. > specific commands?  A while ago I pulled a program from the net which
  15. > did this, but I seem to have lost the program before installing it.
  16.  
  17. I got this from Unix World a few years ago and modified it slightly
  18. to add access lists to restrict it in the manner you describe.
  19.  
  20. The use of this program is controlled by two files, /etc/.priv.list
  21. which is a list of authorized users and /usr/lib/priv/login_name
  22. which is a list of the commands login_name is allowed to run as root.
  23. If the second file does not exist then the user has UNLIMITED access
  24. which was the original behaviour of the program (priv sh will give you
  25. a root shell).  Also, if you remove the define for NEWPATH you are 
  26. leaving your system wide open to trojan horses with the name of an 
  27. authorized program.
  28.  
  29. This was also posted in biz.sco.general a few days ago in response to
  30. a different request, my apologies if you get both groups.
  31.  
  32. Dan
  33. ------------------- priv.c -----------------------------------
  34. /*
  35.  * priv  Run a command as superuser
  36.  * by Ron Kuris, December 1988
  37.  */
  38. /* 
  39.     access list added by Dan Busarow, DPC Systems, 11/22/91
  40.  
  41.     files: /etc/.priv.list 
  42.                 a list of authorized user login names, one per line
  43.                 should be mode 400
  44.               
  45.            /usr/lib/priv/login_name 
  46.                 a list of authorized commands, one per line
  47.                 this should also be 400
  48.               
  49.     priv should be mode 4111, owned by root
  50. */
  51.  
  52. #include <stdio.h>
  53. #include <pwd.h>
  54.  
  55. #define PRIVLIST "/etc/.priv.list"
  56. #define ACCESSDIR "/usr/lib/priv/"
  57. #define LONGESTNAME 64
  58. #define ERREXIT 1
  59. /* If NEWPATH isn't defined, then PATH is taken from calling program */
  60. #define NEWPATH "PATH=/bin:/etc:/usr/bin"
  61.  
  62. extern unsigned short getuid();
  63. extern char *malloc();
  64.  
  65. main(argc,argv,envp)
  66. char **argv, **envp;
  67. int argc;
  68. {
  69.     struct passwd *getpwuid(), *pw;
  70.     extern void exit();
  71.     FILE *fp;
  72.     char aList[64], buffer[LONGESTNAME+1], *lname, *prog;
  73.     short i, j, ok;
  74.  
  75.     prog = argv[0]; /* store program name */
  76.     if (argc < 2) 
  77.     {   /***
  78.         (void)fprintf(stderr,"Usage: %s command args\n", prog);
  79.         no error messages, this program is not intended for use
  80.         by the general public, authorized users will know how to
  81.         run it
  82.         ***/
  83.         exit(ERREXIT);
  84.     }
  85.     pw = getpwuid((int)getuid());
  86.     lname = pw->pw_name;
  87.     if ((fp = fopen(PRIVLIST, "r")) == NULL) 
  88.     {   (void)fprintf(stderr, "Can't open database\n");
  89.         exit(ERREXIT);
  90.     }
  91.     while (fgets(buffer, LONGESTNAME, fp) != NULL) 
  92.     {   buffer[strlen(buffer)-1] = '\0'; /* zap newline */
  93.         if (!strcmp(lname, buffer)) 
  94.         {   (void)fclose(fp);
  95.             ok = 0;
  96.             strcpy(aList,ACCESSDIR);
  97.             strcat(aList,lname);
  98.             if ((fp = fopen(aList, "r")) == NULL)/* default, no restriction */
  99.                 ok = 1;  /* this user is now root so you better trust them! */
  100.             else
  101.             {   while(fgets(buffer,LONGESTNAME,fp) != NULL)
  102.                 {   buffer[strlen(buffer)-1] = 0;
  103.                     if(!strcmp(buffer,argv[1]))
  104.                     {   ok = 1;
  105.                         break;
  106.                     }
  107.                 }
  108.             }
  109.             if(!ok)             /* failed access list test */
  110.                 break;
  111. #ifndef NEWPATH
  112.             if (getenv("PATH") == NULL) 
  113.             {   (void)fprintf(stderr,"%s: No path.\n", prog);
  114.                 exit(ERREXIT);
  115.             }
  116. #else
  117.             for (i=0; envp[i]; i++) 
  118.             {   if (!strncmp("PATH=", envp[i], 5)) 
  119.                 {   envp[i] = NEWPATH;
  120.                     break;
  121.                 }
  122.             }
  123.             if (!envp[i])               /* no PATH, add it to environ */ 
  124.             {   extern char **environ;
  125.                 char **newenv = (char **)malloc((i+2)*sizeof(char *));
  126.                 for (j = 0; j < i; j++)
  127.                     newenv[j] = envp[j];
  128.                 newenv[j] = NEWPATH;
  129.                 newenv[j+1] = NULL;
  130.                 environ = newenv;
  131.             }
  132. #endif
  133.             (void)setuid(0);
  134.             (void)setgid(0);
  135.             (void)execvp(argv[1], argv+1);
  136.             (void)fprintf(stderr,"%s: can't execute %s\n",prog,argv[1]);
  137.             exit(ERREXIT);
  138.         }
  139.     }
  140.     /* failed authorization test */
  141.     /* originally there was an error message here saying that the
  142.        user is not authorized to run priv.  I removed it on the
  143.        assumption that a program which seems to do nothing is a lot
  144.        less likely to get hacked on than one which tells you that you
  145.        are not authorized to run it.
  146.     */
  147.     (void)fclose(fp);
  148.     exit(ERREXIT);
  149.     /* NOTREACHED */
  150. }
  151.  
  152. ---------------- end priv.c ------------------------------------
  153. -- 
  154. +                                                                            -
  155.    Dan Busarow             dan@cedb.dpcsys.org              uunet!cedb!dan
  156.    DPC SYSTEMS                Monrovia, CA                  (818) 305-5733
  157. -                                                                            +
  158.