Back to index

glibc  2.9
setresuid.c
Go to the documentation of this file.
00001 /* setresuid -- set real user ID, effective user ID, and saved-set user ID
00002    Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <unistd.h>
00022 #include <hurd.h>
00023 #include <hurd/id.h>
00024 
00025 /* Set the real user ID, effective user ID, and saved-set user ID,
00026    of the calling process to RUID, EUID, and SUID, respectively.  */
00027 int
00028 __setresuid (uid_t ruid, uid_t euid, uid_t suid)
00029 {
00030   auth_t newauth;
00031   error_t err;
00032   uid_t auids[2] = { ruid, suid };
00033 
00034   HURD_CRITICAL_BEGIN;
00035   __mutex_lock (&_hurd_id.lock);
00036   err = _hurd_check_ids ();
00037 
00038   if (!err)
00039     {
00040       /* Make a new auth handle which has EUID as the first element in the
00041          list of effective uids.  */
00042 
00043       if (_hurd_id.gen.nuids > 0)
00044        {
00045          _hurd_id.gen.uids[0] = euid;
00046          _hurd_id.valid = 0;
00047        }
00048       if (_hurd_id.aux.nuids > 1)
00049        {
00050          _hurd_id.aux.uids[0] = ruid;
00051          _hurd_id.aux.uids[1] = suid;
00052          _hurd_id.valid = 0;
00053        }
00054 
00055       err = __USEPORT (AUTH, __auth_makeauth
00056                      (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0,
00057                      _hurd_id.gen.nuids ? _hurd_id.gen.uids : &euid,
00058                      _hurd_id.gen.nuids ?: 1,
00059                      _hurd_id.aux.nuids > 1 ? _hurd_id.aux.uids : auids,
00060                      _hurd_id.aux.nuids > 1 ? _hurd_id.aux.nuids : 2,
00061                      _hurd_id.gen.gids, _hurd_id.gen.ngids,
00062                      _hurd_id.aux.gids, _hurd_id.aux.ngids,
00063                      &newauth));
00064     }
00065 
00066   __mutex_unlock (&_hurd_id.lock);
00067   HURD_CRITICAL_END;
00068 
00069   if (err)
00070     return __hurd_fail (err);
00071 
00072   /* Install the new handle and reauthenticate everything.  */
00073   err = __setauth (newauth);
00074   __mach_port_deallocate (__mach_task_self (), newauth);
00075   return err;
00076 }
00077 libc_hidden_def (__setresuid)
00078 weak_alias (__setresuid, setresuid)