Advertisement
shiftdot515

asid.c

Jul 25th, 2021
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. /* asid.c
  2.  * for root, run a process as a particular user:group combo
  3.  * sets up minimal enviroment
  4.  * compile w/gcc -g -fwritable-strings -Wall asid.c -o asid
  5.  */
  6.  
  7. #include <sys/types.h>
  8. #include <pwd.h>
  9. #include <grp.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14.  
  15.  
  16. #define ASID_PATH "/bin:/usr/bin:/usr/local/bin"
  17. #define ASID_SHELL "/bin/bash"
  18. #define ASID_TERM "vt100"      /* if unset */
  19. #define ASID_LINES "25"        /* if unset */
  20. #define ASID_HOME "/tmp"       /* if not in passwd */
  21.  
  22. int main( int argc, char * argv[] , char **environ)
  23. {
  24.   uid_t uid,tmpuid;
  25.   gid_t gid,tmpgid;
  26.   struct passwd * pwuid;
  27.   char * cptr;
  28.   char * ptr;
  29.   char * usrstr;
  30.   char * grstr;
  31.   int i;
  32.   char * envp;
  33.   char ** newargv;
  34.  
  35.   /* new env */
  36.   char * newenv[9] = {
  37.     "PATH=" ASID_PATH ,
  38.     "TERM=ABCDEFGHIZKLMNOPQRSTUVWXYZ0123456789",
  39.     "LINES=1234567890",
  40.     "SHELL=" ASID_SHELL,
  41.     "HOME=/home/user1234567890ABCDEFGHIZKLMNOPQRSTUVWXYZ0123456789XXXXXXXXXXX",
  42.     "LOGNAME=USER1234567890XXXXXXXXXXXXXXX",
  43.     "USER=USER1234567890XXXXXXXXXXXXXXX",
  44.     "MAIL=/var/spool/mail/USER1234567890XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  45.     NULL };
  46.   enum { newenv_path , newenv_term, newenv_lines,
  47.          newenv_shell, newenv_home, newenv_logname,
  48.          newenv_user, newenv_mail };
  49.  
  50.    
  51.   if  ( argc < 2 )   {
  52.       puts("Usage: asid user[:group] command [arg1] ... [argn]");
  53.       return 2;
  54.   }
  55.   newargv=argv; /* argv[0] */
  56.   newargv++;    /* argv[1] */
  57.   newargv++;    /* argv[2] */
  58.  
  59.   cptr=index(argv[1], ':' );
  60.   if ( cptr ) {
  61.     cptr[0]=0;
  62.     grstr=1+cptr;
  63.   } else
  64.     grstr="";
  65.   usrstr=argv[1];                       /* usrstr:grstr */
  66.   tmpuid=tmpgid=99;             /* default to nobody */
  67.   tmpuid=strtol(usrstr, &ptr, 0);
  68.   if ( *ptr != 0 ) { /* try looking up ustr*/
  69.     struct passwd * pptr;
  70.     if ((pptr=getpwnam(usrstr))) {
  71.      tmpuid=pptr->pw_uid;
  72.      gid=pptr->pw_gid;
  73.     }
  74.   }
  75.   uid=tmpuid;
  76.   tmpgid=strtol(grstr, &ptr, 0);
  77.   if ( *ptr != 0 ) {    /* try looking up */
  78.     struct group *gptr  ;
  79.     if ((gptr=getgrnam(grstr)))
  80.       gid=gptr->gr_gid;
  81.   } else if ( tmpgid )
  82.     gid=tmpgid;
  83.  
  84.   /*
  85.    * setup newenv
  86.    */
  87.   envp=getenv("TERM");
  88.   snprintf(newenv[newenv_term], 41, "TERM=%s", envp ? envp : ASID_TERM );
  89.  
  90.   envp=getenv("LINES");
  91.   snprintf(newenv[newenv_lines], 16, "LINES=%s", envp ? envp : ASID_LINES);
  92.  
  93.   pwuid=getpwuid(uid);
  94.   if ( pwuid && pwuid->pw_dir && pwuid->pw_dir[0] )
  95.       snprintf(newenv[newenv_home],72,"HOME=%s", pwuid->pw_dir );
  96.     else
  97.       snprintf(newenv[newenv_home],72,"HOME=%s", ASID_HOME );
  98.  
  99.   if ( pwuid && pwuid->pw_name && pwuid->pw_name[0] ) {
  100.       snprintf(newenv[newenv_logname],37,"LOGNAME=%s", pwuid->pw_name );
  101.       snprintf(newenv[newenv_user],34,"USER=%s", pwuid->pw_name );
  102.       snprintf(newenv[newenv_mail],72,"MAIL=/var/mail/%s",
  103.                pwuid->pw_name );
  104.      
  105.   } else {
  106.     snprintf(newenv[newenv_logname],37,"LOGNAME=%i", uid );
  107.     snprintf(newenv[newenv_user],34,"USER=%i", uid );
  108.     snprintf(newenv[newenv_mail],72,"MAIL=/var/mail/%i", uid );
  109.   }
  110.   /*
  111.    * set ids, clear out groups( only use gid ) , and exec
  112.    */
  113.   fprintf(stderr,"%i:%i trying\n",uid,gid);
  114.   if ( setgroups( 0, NULL ) ) {
  115.     perror("asid:setgroups()");
  116.     return 3;
  117.   }
  118.  
  119.   if ( setregid( gid, gid) ) {
  120.         perror("asid:setregid():");
  121.         return 4;
  122.   }
  123.   if ( setreuid( uid, uid ) ){
  124.       perror("asid:setreuid():");
  125.       return 3;
  126.   }
  127.   if ( argc < 3 ) {
  128.     execle("/bin/bash", "-bash", NULL, newenv);
  129.     perror("execle:/bin/bash");
  130.     return 4;
  131.   } else {
  132.     for ( i=0 ; i<9 ; i++  )
  133.       environ[i]=newenv[i];     /* seems to work better than clearenv(), */
  134.     environ[9]=NULL;            /* putenv() */
  135.     execvp(newargv[0], newargv);
  136.     perror("execve:newargv[0]");
  137.     return 5;
  138.   }
  139.  
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement