+
+ if ((uthread->uu_flag & UT_SETUID) != 0) {
+#if DEBUG_CRED
+ int my_cred_flags = uthread->uu_ucred->cr_flags;
+#endif /* DEBUG_CRED */
+ kauth_cred_unref(&my_cred);
+
+ /*
+ * If this thread is under an assumed identity, set the
+ * supplementary grouplist on the thread credential instead
+ * of the process one. If we were the only reference holder,
+ * the credential is updated in place, otherwise, our reference
+ * is dropped and we get back a different cred with a reference
+ * already held on it. Because this is per-thread, we don't
+ * need the referencing/locking/retry required for per-process.
+ */
+ my_cred = uthread->uu_ucred;
+ uthread->uu_ucred = kauth_cred_setgroups(my_cred, &newgroups[0], ngrp, gmuid);
+#if DEBUG_CRED
+ if (my_cred != uthread->uu_ucred) {
+ DEBUG_CRED_CHANGE("setgroups1(CH)%d: %p/0x%08x->%p/0x%08x\n", p->p_pid, my_cred, my_cred_flags, uthread->uu_ucred , uthread->uu_ucred ->cr_flags);
+ }
+#endif /* DEBUG_CRED */
+ } else {
+
+ /*
+ * get current credential and take a reference while we muck
+ * with it
+ */
+ for (;;) {
+ /*
+ * Set the credential with new info. If there is no
+ * change, we get back the same credential we passed
+ * in; if there is a change, we drop the reference on
+ * the credential we passed in. The subsequent
+ * compare is safe, because it is a pointer compare
+ * rather than a contents compare.
+ */
+ my_new_cred = kauth_cred_setgroups(my_cred, &newgroups[0], ngrp, gmuid);
+ if (my_cred != my_new_cred) {
+
+ DEBUG_CRED_CHANGE("setgroups1(CH)%d: %p/0x%08x->%p/0x%08x\n", p->p_pid, my_cred, my_cred->cr_flags, my_new_cred, my_new_cred->cr_flags);
+
+ proc_lock(p);
+ /*
+ * We need to protect for a race where another
+ * thread also changed the credential after we
+ * took our reference. If p_ucred has
+ * changed then we should restart this again
+ * with the new cred.
+ */
+ if (p->p_ucred != my_cred) {
+ proc_unlock(p);
+ kauth_cred_unref(&my_new_cred);
+ my_cred = kauth_cred_proc_ref(p);
+ /* try again */
+ continue;
+ }
+ p->p_ucred = my_new_cred;
+ OSBitOrAtomic(P_SUGID, &p->p_flag);
+ proc_unlock(p);
+ }
+ break;
+ }
+ /* Drop old proc reference or our extra reference */
+ AUDIT_ARG(groupset, my_cred->cr_groups, ngrp);
+ kauth_cred_unref(&my_cred);
+
+
+ set_security_token(p);
+ }
+
+ return (0);
+}
+
+
+/*
+ * initgroups
+ *
+ * Description: Initialize the default supplementary groups list and set the
+ * gmuid for use by the external group resolver (if any)
+ *
+ * Parameters: uap->gidsetsize Number of groups in set
+ * uap->gidset Pointer to group list
+ * uap->gmuid Base gid
+ *
+ * Returns: 0 Success
+ * setgroups1:EPERM Permision denied
+ * setgroups1:EINVAL Invalid gidsetsize value
+ * setgroups1:EFAULT Bad gidset or gidsetsize is
+ *
+ * Notes: This function opts *IN* to memberd participation
+ *
+ * The normal purpose of this function is for a privileged
+ * process to indicate supplementary groups and identity for
+ * participation in extended group membership resolution prior
+ * to dropping privilege by assuming a specific user identity.
+ *
+ * It is the first half of the primary mechanism whereby user
+ * identity is established to the system by programs such as
+ * /usr/bin/login. The second half is the drop of uid privilege
+ * for a specific uid corresponding to the user.
+ *
+ * See also: setgroups1()
+ */
+int
+initgroups(proc_t p, struct initgroups_args *uap, __unused int32_t *retval)
+{
+ DEBUG_CRED_ENTER("initgroups\n");
+
+ return(setgroups1(p, uap->gidsetsize, uap->gidset, uap->gmuid, retval));
+}
+
+
+/*
+ * setgroups
+ *
+ * Description: Initialize the default supplementary groups list
+ *
+ * Parameters: gidsetsize Number of groups in set
+ * gidset Pointer to group list
+ *
+ * Returns: 0 Success
+ * setgroups1:EPERM Permision denied
+ * setgroups1:EINVAL Invalid gidsetsize value
+ * setgroups1:EFAULT Bad gidset or gidsetsize is
+ *
+ * Notes: This functions opts *OUT* of memberd participation.
+ *
+ * This function exists for compatibility with POSIX. Most user
+ * programs should use initgroups() instead to ensure correct
+ * participation in group membership resolution when utilizing
+ * a directory service for authentication.
+ *
+ * It is identical to an initgroups() call with a gmuid argument
+ * of KAUTH_UID_NONE.
+ *
+ * See also: setgroups1()
+ */
+int
+setgroups(proc_t p, struct setgroups_args *uap, __unused int32_t *retval)
+{
+ DEBUG_CRED_ENTER("setgroups\n");
+
+ return(setgroups1(p, uap->gidsetsize, uap->gidset, KAUTH_UID_NONE, retval));
+}
+
+
+/*
+ * Set the per-thread/per-process supplementary groups list.
+ *
+ * XXX implement setsgroups
+ *
+ */
+
+int
+setsgroups(__unused proc_t p, __unused struct setsgroups_args *uap, __unused int32_t *retval)
+{
+ return(ENOTSUP);
+}
+
+/*
+ * Set the per-thread/per-process whiteout groups list.
+ *
+ * XXX implement setwgroups
+ *
+ */
+
+int
+setwgroups(__unused proc_t p, __unused struct setwgroups_args *uap, __unused int32_t *retval)
+{
+ return(ENOTSUP);