/* Set the group ID of the calling process to UID. If the calling process is the super-user, the real and effective group IDs, and the saved set-group-ID to UID; if not, the effective group ID is set to GID. */ int __setgid (gid_t gid) { auth_t newauth; error_t err; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); err = _hurd_check_ids (); if (!err) { /* Make a new auth handle which has GID as the real gid, and as the first element in the list of effective gids. */ gid_t *newgen, *newaux, auxbuf[2]; size_t ngen, naux; if (_hurd_id.gen.ngids == 0) { /* No effective gids now. The new set will be just GID. */ newgen = &gid; ngen = 1; } else { _hurd_id.gen.gids[0] = gid; _hurd_id.valid = 0; newgen = _hurd_id.gen.gids; ngen = _hurd_id.gen.ngids; } newaux = _hurd_id.aux.gids; naux = _hurd_id.aux.ngids; if (_hurd_id.gen.nuids > 0 && _hurd_id.gen.uids[0] == 0) { /* We are root; set the real and saved IDs too. */ _hurd_id.valid = 0; if (_hurd_id.aux.ngids < 2) { newaux = auxbuf; naux = 2; } newaux[0] = newaux[1] = gid; } err = __USEPORT (AUTH, __auth_makeauth (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0, _hurd_id.gen.uids, _hurd_id.gen.nuids, _hurd_id.aux.uids, _hurd_id.aux.nuids, newgen, ngen, newaux, naux, &newauth)); } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; if (err) return __hurd_fail (err); /* Install the new handle and reauthenticate everything. */ err = __setauth (newauth); __mach_port_deallocate (__mach_task_self (), newauth); return err; }
int __setregid (gid_t rgid, gid_t egid) { auth_t newauth; error_t err; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); err = _hurd_check_ids (); if (!err) { /* Make a new auth handle which has RGID as the real gid, and EGID as the first element in the list of effective gids. */ gid_t *newgen, *newaux; size_t ngen, naux; newgen = _hurd_id.gen.gids; ngen = _hurd_id.gen.ngids; if (egid != -1) { if (_hurd_id.gen.ngids == 0) { /* No effective gids now. The new set will be just GID. */ newgen = &egid; ngen = 1; } else { _hurd_id.gen.gids[0] = egid; _hurd_id.valid = 0; } } newaux = _hurd_id.aux.gids; naux = _hurd_id.aux.ngids; if (rgid != -1) { if (_hurd_id.aux.ngids == 0) { newaux = &rgid; naux = 1; } else { _hurd_id.aux.gids[0] = rgid; _hurd_id.valid = 0; } } err = __USEPORT (AUTH, __auth_makeauth (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0, _hurd_id.gen.uids, _hurd_id.gen.nuids, _hurd_id.aux.uids, _hurd_id.aux.nuids, newgen, ngen, newaux, naux, &newauth)); } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; if (err) return __hurd_fail (err); /* Install the new handle and reauthenticate everything. */ err = __setauth (newauth); __mach_port_deallocate (__mach_task_self (), newauth); return err; }
/* Set the real user ID, effective user ID, and saved-set user ID, of the calling process to RUID, EUID, and SUID, respectively. */ int __setresuid (uid_t ruid, uid_t euid, uid_t suid) { auth_t newauth; error_t err; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); err = _hurd_check_ids (); if (!err) { /* Make a new auth handle which has EUID as the first element in the list of effective uids. */ uid_t *newgen, *newaux; uid_t auxs[2] = { ruid, suid }; size_t ngen, naux; newgen = _hurd_id.gen.uids; ngen = _hurd_id.gen.nuids; if (euid != -1) { if (_hurd_id.gen.nuids == 0) { /* No effective uids now. The new set will be just UID. */ newgen = &euid; ngen = 1; } else { _hurd_id.gen.uids[0] = euid; _hurd_id.valid = 0; } } newaux = _hurd_id.aux.uids; naux = _hurd_id.aux.nuids; if (ruid != -1) { if (_hurd_id.aux.nuids == 0) { newaux = &ruid; naux = 1; } else { _hurd_id.aux.uids[0] = ruid; _hurd_id.valid = 0; } } if (suid != -1) { if (ruid == -1) { if (_hurd_id.aux.nuids >= 1) auxs[0] = _hurd_id.aux.uids[0]; else if (_hurd_id.gen.nuids >= 1) auxs[0] = _hurd_id.gen.uids[0]; else /* Not even an effective UID. Fall back to the only UID we have. */ auxs[0] = suid; } if (_hurd_id.aux.nuids <= 1) { /* No saved uids now. The new set will be just UID. */ newaux = auxs; naux = 2; } else { _hurd_id.aux.uids[1] = suid; _hurd_id.valid = 0; } } err = __USEPORT (AUTH, __auth_makeauth (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0, newgen, ngen, newaux, naux, _hurd_id.gen.gids, _hurd_id.gen.ngids, _hurd_id.aux.gids, _hurd_id.aux.ngids, &newauth)); } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; if (err) return __hurd_fail (err); /* Install the new handle and reauthenticate everything. */ err = __setauth (newauth); __mach_port_deallocate (__mach_task_self (), newauth); return err; }