home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / samba-1.9.17p4 / source / uid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-30  |  14.5 KB  |  564 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    uid/user handling
  5.    Copyright (C) Andrew Tridgell 1992-1997
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "includes.h"
  23.  
  24. extern int DEBUGLEVEL;
  25.  
  26. extern connection_struct Connections[];
  27.  
  28. static int initial_uid;
  29. static int initial_gid;
  30.  
  31. /* what user is current? */
  32. struct current_user current_user;
  33.  
  34. extern pstring OriginalDir;
  35.  
  36. /****************************************************************************
  37. initialise the uid routines
  38. ****************************************************************************/
  39. void init_uid(void)
  40. {
  41.   initial_uid = current_user.uid = geteuid();
  42.   initial_gid = current_user.gid = getegid();
  43.  
  44.   if (initial_gid != 0 && initial_uid == 0)
  45.     {
  46. #ifdef HPUX
  47.       setresgid(0,0,0);
  48. #else
  49.       setgid(0);
  50.       setegid(0);
  51. #endif
  52.     }
  53.  
  54.   initial_uid = geteuid();
  55.   initial_gid = getegid();
  56.  
  57.   current_user.cnum = -1;
  58.  
  59.   ChDir(OriginalDir);
  60. }
  61.  
  62.  
  63. /****************************************************************************
  64.   become the specified uid 
  65. ****************************************************************************/
  66. static BOOL become_uid(int uid)
  67. {
  68.   if (initial_uid != 0)
  69.     return(True);
  70.  
  71.   if (uid == -1 || uid == 65535) {
  72.     DEBUG(1,("WARNING: using uid %d is a security risk\n",uid));    
  73.   }
  74.  
  75. #ifdef AIX
  76.   {
  77.     /* AIX 3 stuff - inspired by a code fragment in wu-ftpd */
  78.     priv_t priv;
  79.  
  80.     priv.pv_priv[0] = 0;
  81.     priv.pv_priv[1] = 0;
  82.     if (setpriv(PRIV_SET|PRIV_INHERITED|PRIV_EFFECTIVE|PRIV_BEQUEATH,
  83.         &priv, sizeof(priv_t)) < 0 ||
  84.     setuidx(ID_REAL|ID_EFFECTIVE, (uid_t)uid) < 0 ||
  85.     seteuid((uid_t)uid) < 0) 
  86.       DEBUG(1,("Can't set uid (AIX3)\n"));
  87.   }
  88. #endif
  89.  
  90. #ifdef USE_SETRES
  91.   if (setresuid(-1,uid,-1) != 0)
  92. #elif defined(USE_SETFS)
  93.     if (setfsuid(uid) != 0)
  94. #else
  95.     if ((seteuid(uid) != 0) && 
  96.     (setuid(uid) != 0))
  97. #endif
  98.       {
  99.     DEBUG(0,("Couldn't set uid %d currently set to (%d,%d)\n",
  100.          uid,getuid(), geteuid()));
  101.     if (uid > 32000)
  102.       DEBUG(0,("Looks like your OS doesn't like high uid values - try using a different account\n"));
  103.     return(False);
  104.       }
  105.  
  106.   if (((uid == -1) || (uid == 65535)) && geteuid() != uid) {
  107.     DEBUG(0,("Invalid uid -1. perhaps you have a account with uid 65535?\n"));
  108.     return(False);
  109.   }
  110.  
  111.   current_user.uid = uid;
  112.  
  113.   return(True);
  114. }
  115.  
  116.  
  117. /****************************************************************************
  118.   become the specified gid
  119. ****************************************************************************/
  120. static BOOL become_gid(int gid)
  121. {
  122.   if (initial_uid != 0)
  123.     return(True);
  124.  
  125.   if (gid == -1 || gid == 65535) {
  126.     DEBUG(1,("WARNING: using gid %d is a security risk\n",gid));    
  127.   }
  128.   
  129. #ifdef USE_SETRES 
  130.   if (setresgid(-1,gid,-1) != 0)
  131. #elif defined(USE_SETFS)
  132.   if (setfsgid(gid) != 0)
  133. #else
  134.   if (setgid(gid) != 0)
  135. #endif
  136.       {
  137.     DEBUG(0,("Couldn't set gid %d currently set to (%d,%d)\n",
  138.          gid,getgid(),getegid()));
  139.     if (gid > 32000)
  140.       DEBUG(0,("Looks like your OS doesn't like high gid values - try using a different account\n"));
  141.     return(False);
  142.       }
  143.  
  144.   current_user.gid = gid;
  145.  
  146.   return(True);
  147. }
  148.  
  149.  
  150. /****************************************************************************
  151.   become the specified uid and gid
  152. ****************************************************************************/
  153. static BOOL become_id(int uid,int gid)
  154. {
  155.     /* Note: must execute both on some systems! can't use single statement! */
  156.     BOOL u = become_uid(uid);
  157.     BOOL g = become_gid(gid);
  158.   return(u && g);
  159. }
  160.  
  161. /****************************************************************************
  162. become the guest user
  163. ****************************************************************************/
  164. BOOL become_guest(void)
  165. {
  166.   BOOL ret;
  167.   static struct passwd *pass=NULL;
  168.  
  169.   if (initial_uid != 0) 
  170.     return(True);
  171.  
  172.   if (!pass)
  173.     pass = Get_Pwnam(lp_guestaccount(-1),True);
  174.   if (!pass) return(False);
  175.  
  176.   ret = become_id(pass->pw_uid,pass->pw_gid);
  177.  
  178.   if (!ret)
  179.     DEBUG(1,("Failed to become guest. Invalid guest account?\n"));
  180.  
  181.   current_user.cnum = -2;
  182.  
  183.   return(ret);
  184. }
  185.  
  186. /*******************************************************************
  187. check if a username is OK
  188. ********************************************************************/
  189. static BOOL check_user_ok(int cnum,user_struct *vuser,int snum)
  190. {
  191.   int i;
  192.   for (i=0;i<Connections[cnum].uid_cache.entries;i++)
  193.     if (Connections[cnum].uid_cache.list[i] == vuser->uid) return(True);
  194.  
  195.   if (!user_ok(vuser->name,snum)) return(False);
  196.  
  197.   i = Connections[cnum].uid_cache.entries % UID_CACHE_SIZE;
  198.   Connections[cnum].uid_cache.list[i] = vuser->uid;
  199.  
  200.   if (Connections[cnum].uid_cache.entries < UID_CACHE_SIZE)
  201.     Connections[cnum].uid_cache.entries++;
  202.  
  203.   return(True);
  204. }
  205.  
  206.  
  207. /****************************************************************************
  208.   become the user of a connection number
  209. ****************************************************************************/
  210. BOOL become_user(int cnum, uint16 vuid)
  211. {
  212.   user_struct *vuser = get_valid_user_struct(vuid);
  213.   int snum,gid;
  214.   int uid;
  215.  
  216.   if (current_user.cnum == cnum && vuser != 0 && current_user.id == vuser->uid) {
  217.     DEBUG(4,("Skipping become_user - already user\n"));
  218.     return(True);
  219.   }
  220.  
  221.   unbecome_user();
  222.  
  223.   if (!OPEN_CNUM(cnum)) {
  224.     DEBUG(2,("Connection %d not open\n",cnum));
  225.     return(False);
  226.   }
  227.  
  228.   snum = Connections[cnum].service;
  229.  
  230.   if (Connections[cnum].force_user || 
  231.       lp_security() == SEC_SHARE ||
  232.       !(vuser) || (vuser->guest) ||
  233.       !check_user_ok(cnum,vuser,snum)) {
  234.     uid = Connections[cnum].uid;
  235.     gid = Connections[cnum].gid;
  236.     current_user.groups = Connections[cnum].groups;
  237.     current_user.igroups = Connections[cnum].igroups;
  238.     current_user.ngroups = Connections[cnum].ngroups;
  239.   } else {
  240.     if (!vuser) {
  241.       DEBUG(2,("Invalid vuid used %d\n",vuid));
  242.       return(False);
  243.     }
  244.     uid = vuser->uid;
  245.     if(!*lp_force_group(snum))
  246.       gid = vuser->gid;
  247.     else
  248.       gid = Connections[cnum].gid;
  249.     current_user.groups = vuser->user_groups;
  250.     current_user.igroups = vuser->user_igroups;
  251.     current_user.ngroups = vuser->user_ngroups;
  252.   }
  253.  
  254.   if (initial_uid == 0)
  255.     {
  256.       if (!become_gid(gid)) return(False);
  257.  
  258. #ifndef NO_SETGROUPS      
  259.       if (!IS_IPC(cnum)) {
  260.     /* groups stuff added by ih/wreu */
  261.     if (current_user.ngroups > 0)
  262.       if (setgroups(current_user.ngroups,current_user.groups)<0)
  263.         DEBUG(0,("setgroups call failed!\n"));
  264.       } else {
  265.         current_user.ngroups = 0;  
  266.       }
  267. #endif
  268.  
  269.       if (!Connections[cnum].admin_user && !become_uid(uid))
  270.     return(False);
  271.     }
  272.  
  273.   current_user.cnum = cnum;
  274.   current_user.id = uid;
  275.   
  276.   DEBUG(5,("become_user uid=(%d,%d) gid=(%d,%d)\n",
  277.        getuid(),geteuid(),getgid(),getegid()));
  278.   
  279.   return(True);
  280. }
  281.  
  282. /****************************************************************************
  283.   unbecome the user of a connection number
  284. ****************************************************************************/
  285. BOOL unbecome_user(void )
  286. {
  287.   if (current_user.cnum == -1)
  288.     return(False);
  289.  
  290.   ChDir(OriginalDir);
  291.  
  292.   if (initial_uid == 0)
  293.     {
  294. #ifdef USE_SETRES
  295.       setresuid(-1,getuid(),-1);
  296.       setresgid(-1,getgid(),-1);
  297. #elif defined(USE_SETFS)
  298.       setfsuid(initial_uid);
  299.       setfsgid(initial_gid);
  300. #else
  301.       if (seteuid(initial_uid) != 0) 
  302.     setuid(initial_uid);
  303.       setgid(initial_gid);
  304. #endif
  305.     }
  306. #ifdef NO_EID
  307.   if (initial_uid == 0)
  308.     DEBUG(2,("Running with no EID\n"));
  309.   initial_uid = getuid();
  310.   initial_gid = getgid();
  311. #else
  312.   if (geteuid() != initial_uid)
  313.     {
  314.       DEBUG(0,("Warning: You appear to have a trapdoor uid system\n"));
  315.       initial_uid = geteuid();
  316.     }
  317.   if (getegid() != initial_gid)
  318.     {
  319.       DEBUG(0,("Warning: You appear to have a trapdoor gid system\n"));
  320.       initial_gid = getegid();
  321.     }
  322. #endif
  323.  
  324.   current_user.uid = initial_uid;
  325.   current_user.gid = initial_gid;
  326.   current_user.ngroups = 0;
  327.  
  328.   if (ChDir(OriginalDir) != 0)
  329.     DEBUG(0,("%s chdir(%s) failed in unbecome_user\n",
  330.          timestring(),OriginalDir));
  331.  
  332.   DEBUG(5,("unbecome_user now uid=(%d,%d) gid=(%d,%d)\n",
  333.     getuid(),geteuid(),getgid(),getegid()));
  334.  
  335.   current_user.cnum = -1;
  336.  
  337.   return(True);
  338. }
  339.  
  340.  
  341. /****************************************************************************
  342. This is a utility function of smbrun(). It must be called only from
  343. the child as it may leave the caller in a privilaged state.
  344. ****************************************************************************/
  345. static BOOL setup_stdout_file(char *outfile,BOOL shared)
  346. {  
  347.   int fd;
  348.   struct stat st;
  349.   mode_t mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH;
  350.   int flags = O_RDWR|O_CREAT|O_TRUNC|O_EXCL;
  351.  
  352.   close(1);
  353.  
  354.   if (shared) {
  355.     /* become root - unprivilaged users can't delete these files */
  356. #ifdef USE_SETRES
  357.     setresgid(0,0,0);
  358.     setresuid(0,0,0);
  359. #else      
  360.     setuid(0);
  361.     seteuid(0);
  362. #endif
  363.   }
  364.  
  365.   if(stat(outfile, &st) == 0) {
  366.     /* Check we're not deleting a device file. */ 
  367.     if(st.st_mode & S_IFREG)
  368.       unlink(outfile);
  369.     else
  370.       flags = O_RDWR;
  371.   }
  372.   /* now create the file */
  373.   fd = open(outfile,flags,mode);
  374.  
  375.   if (fd == -1) return False;
  376.  
  377.   if (fd != 1) {
  378.     if (dup2(fd,1) != 0) {
  379.       DEBUG(2,("Failed to create stdout file descriptor\n"));
  380.       close(fd);
  381.       return False;
  382.     }
  383.     close(fd);
  384.   }
  385.   return True;
  386. }
  387.  
  388.  
  389. /****************************************************************************
  390. run a command being careful about uid/gid handling and putting the output in
  391. outfile (or discard it if outfile is NULL).
  392.  
  393. if shared is True then ensure the file will be writeable by all users
  394. but created such that its owned by root. This overcomes a security hole.
  395.  
  396. if shared is not set then open the file with O_EXCL set
  397. ****************************************************************************/
  398. int smbrun(char *cmd,char *outfile,BOOL shared)
  399. {
  400.   int fd,pid;
  401.   int uid = current_user.uid;
  402.   int gid = current_user.gid;
  403.  
  404. #if USE_SYSTEM
  405.   int ret;
  406.   pstring syscmd;  
  407.   char *path = lp_smbrun();
  408.  
  409.   /* in the old method we use system() to execute smbrun which then
  410.      executes the command (using system() again!). This involves lots
  411.      of shell launches and is very slow. It also suffers from a
  412.      potential security hole */
  413.   if (!file_exist(path,NULL))
  414.     {
  415.       DEBUG(0,("SMBRUN ERROR: Can't find %s. Installation problem?\n",path));
  416.       return(1);
  417.     }
  418.  
  419.   sprintf(syscmd,"%s %d %d \"(%s 2>&1) > %s\"",
  420.       path,uid,gid,cmd,
  421.       outfile?outfile:"/dev/null");
  422.  
  423.   DEBUG(5,("smbrun - running %s ",syscmd));
  424.   ret = system(syscmd);
  425.   DEBUG(5,("gave %d\n",ret));
  426.   return(ret);
  427. #else
  428.   /* in this newer method we will exec /bin/sh with the correct
  429.      arguments, after first setting stdout to point at the file */
  430.  
  431.   if ((pid=fork())) {
  432.     SYS_WAIT_TYPE status;
  433.     /* the parent just waits for the child to exit */
  434.     if (sys_waitpid(pid,&status,0) != pid) {
  435.       DEBUG(2,("waitpid(%d) : %s\n",pid,strerror(errno)));
  436.       return -1;
  437.     }
  438.     return SYS_WAIT_STAT(status);
  439.   }
  440.  
  441.  
  442.   /* we are in the child. we exec /bin/sh to do the work for us. we
  443.      don't directly exec the command we want because it may be a
  444.      pipeline or anything else the config file specifies */
  445.  
  446.   /* point our stdout at the file we want output to go into */
  447.   if (outfile && !setup_stdout_file(outfile,shared)) {
  448.     exit(80);
  449.   }
  450.  
  451.   /* now completely lose our privilages. This is a fairly paranoid
  452.      way of doing it, but it does work on all systems that I know of */
  453. #ifdef USE_SETRES
  454.   setresgid(0,0,0);
  455.   setresuid(0,0,0);
  456.   setresgid(gid,gid,gid);
  457.   setresuid(uid,uid,uid);      
  458. #else      
  459.   setuid(0);
  460.   seteuid(0);
  461.   setgid(gid);
  462.   setegid(gid);
  463.   setuid(uid);
  464.   seteuid(uid);
  465. #endif
  466.  
  467.   if (getuid() != uid || geteuid() != uid ||
  468.       getgid() != gid || getegid() != gid) {
  469.     /* we failed to lose our privilages - do not execute the command */
  470.     exit(81); /* we can't print stuff at this stage, instead use exit codes
  471.          for debugging */
  472.   }
  473.  
  474.   /* close all other file descriptors, leaving only 0, 1 and 2. 0 and
  475.      2 point to /dev/null from the startup code */
  476.   for (fd=3;fd<256;fd++) close(fd);
  477.  
  478.   execl("/bin/sh","sh","-c",cmd,NULL);  
  479.  
  480.   /* not reached */
  481.   exit(82);
  482. #endif
  483.   return 1;
  484. }
  485.  
  486.  
  487.  
  488. static struct current_user current_user_saved;
  489. static int become_root_depth;
  490. static pstring become_root_dir;
  491.  
  492. /****************************************************************************
  493. This is used when we need to do a privilaged operation (such as mucking
  494. with share mode files) and temporarily need root access to do it. This
  495. call should always be paired with an unbecome_root() call immediately
  496. after the operation
  497.  
  498. Set save_dir if you also need to save/restore the CWD 
  499. ****************************************************************************/
  500. void become_root(int save_dir) 
  501. {
  502.     if (become_root_depth) {
  503.         DEBUG(0,("ERROR: become root depth is non zero\n"));
  504.     }
  505.     if (save_dir)
  506.         GetWd(become_root_dir);
  507.  
  508.     current_user_saved = current_user;
  509.     become_root_depth = 1;
  510.  
  511.     become_uid(0);
  512.     become_gid(0);
  513. }
  514.  
  515. /****************************************************************************
  516. When the privilaged operation is over call this
  517.  
  518. Set save_dir if you also need to save/restore the CWD 
  519. ****************************************************************************/
  520. void unbecome_root(int restore_dir)
  521. {
  522.     if (become_root_depth != 1) {
  523.         DEBUG(0,("ERROR: unbecome root depth is %d\n",
  524.              become_root_depth));
  525.     }
  526.  
  527.     /* we might have done a become_user() while running as root,
  528.        if we have then become root again in order to become 
  529.        non root! */
  530.     if (current_user.uid != 0) {
  531.         become_uid(0);
  532.     }
  533.  
  534.     /* restore our gid first */
  535.     if (!become_gid(current_user_saved.gid)) {
  536.         DEBUG(0,("ERROR: Failed to restore gid\n"));
  537.         exit_server("Failed to restore gid");
  538.     }
  539.  
  540. #ifndef NO_SETGROUPS      
  541.     if (current_user_saved.ngroups > 0) {
  542.         if (setgroups(current_user_saved.ngroups,
  543.                   current_user_saved.groups)<0)
  544.             DEBUG(0,("ERROR: setgroups call failed!\n"));
  545.     }
  546. #endif
  547.  
  548.     /* now restore our uid */
  549.     if (!become_uid(current_user_saved.uid)) {
  550.         DEBUG(0,("ERROR: Failed to restore uid\n"));
  551.         exit_server("Failed to restore uid");
  552.     }
  553.  
  554.     if (restore_dir)
  555.         ChDir(become_root_dir);
  556.  
  557.     current_user = current_user_saved;
  558.  
  559.     become_root_depth = 0;
  560. }
  561.  
  562.  
  563.  
  564.