Back to index

glibc  2.9
setreuid.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <sys/types.h>
00021 #include <unistd.h>
00022 #include <hurd.h>
00023 #include <hurd/id.h>
00024 #include <string.h>
00025 
00026 int
00027 __setreuid (ruid, euid)
00028      uid_t ruid;
00029      uid_t euid;
00030 {
00031   auth_t newauth;
00032   error_t err;
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 RUID as the real uid,
00041         and EUID as the first element in the list of effective uids.  */
00042 
00043       uid_t *newgen, *newaux;
00044       size_t ngen, naux;
00045 
00046       newgen = _hurd_id.gen.uids;
00047       ngen = _hurd_id.gen.nuids;
00048       if (euid != -1)
00049        {
00050          if (_hurd_id.gen.nuids == 0)
00051            {
00052              /* No effective uids now.  The new set will be just UID.  */
00053              newgen = &euid;
00054              ngen = 1;
00055            }
00056          else
00057            {
00058              _hurd_id.gen.uids[0] = euid;
00059              _hurd_id.valid = 0;
00060            }
00061        }
00062 
00063       newaux = _hurd_id.aux.uids;
00064       naux = _hurd_id.aux.nuids;
00065       if (ruid != -1)
00066        {
00067          if (_hurd_id.aux.nuids == 0)
00068            {
00069              newaux = &ruid;
00070              naux = 1;
00071            }
00072          else
00073            {
00074              _hurd_id.aux.uids[0] = ruid;
00075              _hurd_id.valid = 0;
00076            }
00077        }
00078 
00079       err = __USEPORT (AUTH, __auth_makeauth
00080                      (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0,
00081                      newgen, ngen, newaux, naux,
00082                      _hurd_id.gen.gids, _hurd_id.gen.ngids,
00083                      _hurd_id.aux.gids, _hurd_id.aux.ngids,
00084                      &newauth));
00085     }
00086   __mutex_unlock (&_hurd_id.lock);
00087   HURD_CRITICAL_END;
00088 
00089   if (err)
00090     return __hurd_fail (err);
00091 
00092   /* Install the new handle and reauthenticate everything.  */
00093   err = __setauth (newauth);
00094   __mach_port_deallocate (__mach_task_self (), newauth);
00095   return err;
00096 }
00097 
00098 weak_alias (__setreuid, setreuid)