/* Get the real user ID of the calling process. */ uid_t DEFUN_VOID(__getuid) { error_t err; uid_t uid; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); if (err = _hurd_check_ids ()) { errno = err; uid = -1; } else if (_hurd_id.aux.nuids >= 1) uid = _hurd_id.aux.uids[0]; else { /* We do not even have a real uid. */ errno = EGRATUITOUS; uid = -1; } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; return uid; }
/* Get the real group ID of the calling process. */ gid_t __getgid () { error_t err; gid_t gid; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); if (err = _hurd_check_ids ()) { errno = err; gid = -1; } else if (_hurd_id.aux.ngids >= 1) gid = _hurd_id.aux.gids[0]; else { /* We do not even have a real gid. */ errno = EGRATUITOUS; gid = -1; } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; return gid; }
/* Get the effective group ID of the calling process. */ gid_t DEFUN_VOID(__getegid) { error_t err; gid_t egid; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); if (err = _hurd_check_ids ()) { errno = err; egid = -1; } else if (_hurd_id.gen.ngids >= 1) egid = _hurd_id.gen.gids[0]; else if (_hurd_id.aux.ngids >= 1) /* We have no effective gids. Return the real gid. */ egid = _hurd_id.aux.gids[0]; else { /* We do not even have a real gid. */ errno = EGRATUITOUS; egid = -1; } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; return egid; }
/* Fetch the real user ID, effective user ID, and saved-set user ID, of the calling process. */ int __getresuid (uid_t *ruid, uid_t *euid, uid_t *suid) { error_t err; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); err = _hurd_check_ids (); if (!err) { if (_hurd_id.aux.nuids < 1) /* We do not even have a real UID. */ err = EGRATUITOUS; else { uid_t real = _hurd_id.aux.uids[0]; *ruid = real; *euid = _hurd_id.gen.nuids < 1 ? real : _hurd_id.gen.uids[0]; *suid = _hurd_id.aux.nuids < 2 ? real : _hurd_id.aux.uids[1]; } } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; return __hurd_fail (err); }
/* Fetch the real group ID, effective group ID, and saved-set group ID, of the calling process. */ int __getresgid (gid_t *rgid, gid_t *egid, gid_t *sgid) { error_t err; gid_t real, eff, saved; HURD_CRITICAL_BEGIN; __mutex_lock (&_hurd_id.lock); err = _hurd_check_ids (); if (!err) { if (_hurd_id.aux.ngids < 1) /* We do not even have a real GID. */ err = EGRATUITOUS; else { real = _hurd_id.aux.gids[0]; eff = _hurd_id.gen.ngids < 1 ? real : _hurd_id.gen.gids[0]; saved = _hurd_id.aux.ngids < 2 ? real : _hurd_id.aux.gids[1]; } } __mutex_unlock (&_hurd_id.lock); HURD_CRITICAL_END; if (err) return __hurd_fail (err); *rgid = real; *egid = eff; *sgid = saved; return 0; }
int __getgroups (int n, gid_t *gidset) { error_t err; int ngids; void *crit; if (n < 0) return __hurd_fail (EINVAL); crit = _hurd_critical_section_lock (); __mutex_lock (&_hurd_id.lock); if (err = _hurd_check_ids ()) { __mutex_unlock (&_hurd_id.lock); _hurd_critical_section_unlock (crit); return __hurd_fail (err); } ngids = _hurd_id.gen.ngids; if (n != 0) { /* Copy the gids onto stack storage and then release the idlock. */ gid_t gids[ngids]; memcpy (gids, _hurd_id.gen.gids, sizeof (gids)); __mutex_unlock (&_hurd_id.lock); _hurd_critical_section_unlock (crit); /* Now that the lock is released, we can safely copy the group set into the user's array, which might fault. */ if (ngids > n) return __hurd_fail (EINVAL); memcpy (gidset, gids, ngids * sizeof (gid_t)); } else { __mutex_unlock (&_hurd_id.lock); _hurd_critical_section_unlock (crit); } return ngids; }
int geteuids (int n, uid_t *uidset) { error_t err; int nuids; void *crit; crit = _hurd_critical_section_lock (); __mutex_lock (&_hurd_id.lock); if (err = _hurd_check_ids ()) { __mutex_unlock (&_hurd_id.lock); _hurd_critical_section_unlock (crit); return __hurd_fail (err); } nuids = _hurd_id.gen.nuids; if (n != 0) { /* Copy the uids onto stack storage and then release the idlock. */ uid_t uids[nuids]; memcpy (uids, _hurd_id.gen.uids, sizeof (uids)); __mutex_unlock (&_hurd_id.lock); _hurd_critical_section_unlock (crit); /* Now that the lock is released, we can safely copy the uid set into the user's array, which might fault. */ if (nuids > n) nuids = n; memcpy (uidset, uids, nuids * sizeof (uid_t)); } else { __mutex_unlock (&_hurd_id.lock); _hurd_critical_section_unlock (crit); } return nuids; }
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 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; }
/* 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; }