home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / secure / secure.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  1.2 KB  |  42 lines

  1. #include    <stdio.h>
  2.  
  3. #define        SECURE        "/etc/secure"
  4.  
  5. main(n, a)
  6. char **a;
  7.  {
  8.     int uid;
  9.     FILE *fp;
  10.     char program[100];
  11.  
  12.     if (strcmp(a[0], "SEC-URE"))/* I'm damned if I know why this is */
  13.      {                /* necessary, but it is */
  14.     (void) strcpy(program, a[0]);
  15.     a[0] = "SEC-URE";
  16.     execv(program, a);    /* re exec ourselves so setuid bits work */
  17.     exit(1);        /* this should never happen */
  18.      }
  19.     if ((fp = fopen(a[1], "r")) == (FILE *) NULL)
  20.       exit(1);            /* file not found */
  21.     (void) fclose(fp);
  22.     if (a[1][0] != '/')
  23.       exit(1);            /* only pass an absolute pathname to /bin/sh */
  24.     if ((fp = fopen(SECURE, "r")) == (FILE *) NULL)
  25.       exit(1);            /* can't find the file of secure programs */
  26.     while (fscanf(fp, "%s %d", program, &uid) == 2)
  27.      {
  28.     if (strcmp(program, a[1]) == 0)
  29.      {            /* aha ..... we found our program */
  30.         (void) fclose(fp);
  31.         (void) unsetenv("IFS");
  32.                 /* tweak the environment for added safety */
  33.         (void) setenv("PATH", "/bin:/usr/bin", 1);
  34.         (void) setuid(uid);    /* set the uid */
  35.         a[0] = "-sh";
  36.         execv("/bin/sh", a);
  37.         exit(1);        /* this should never happen */
  38.      }
  39.      }
  40.     exit(1);            /* come here if we didn't find the program */
  41.  }
  42.