Back to index

glibc  2.9
setresgid.c
Go to the documentation of this file.
00001 /* setresgid -- set real group ID, effective group ID, and saved-set group 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 group ID, effective group ID, and saved-set group ID,
00026    of the calling process to RGID, EGID, and SGID, respectively.  */
00027 int
00028 __setresgid (gid_t rgid, gid_t egid, gid_t sgid)
00029 {
00030   auth_t newauth;
00031   error_t err;
00032   gid_t agids[2] = { rgid, sgid };
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 EGID as the first element in the
00041          list of effective gids.  */
00042 
00043       if (_hurd_id.gen.ngids > 0)
00044        {
00045          _hurd_id.gen.gids[0] = egid;
00046          _hurd_id.valid = 0;
00047        }
00048       if (_hurd_id.aux.ngids > 1)
00049        {
00050          _hurd_id.aux.gids[0] = rgid;
00051          _hurd_id.aux.gids[1] = sgid;
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.uids, _hurd_id.gen.nuids,
00058                      _hurd_id.aux.uids, _hurd_id.aux.nuids,
00059                      _hurd_id.gen.ngids ? _hurd_id.gen.gids : &egid,
00060                      _hurd_id.gen.ngids ?: 1,
00061                      _hurd_id.aux.ngids > 1 ? _hurd_id.aux.gids : agids,
00062                      _hurd_id.aux.ngids > 1 ? _hurd_id.aux.ngids : 2,
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 (__setresgid)
00078 weak_alias (__setresgid, setresgid)