Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.55
      1  1.55    bouyer /*	$NetBSD: kern_prot.c,v 1.55 1999/09/28 14:47:03 bouyer Exp $	*/
      2  1.16       cgd 
      3  1.14       cgd /*
      4  1.15       cgd  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
      5  1.15       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.14       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.14       cgd  * All or some portions of this file are derived from material licensed
      8  1.14       cgd  * to the University of California by American Telephone and Telegraph
      9  1.14       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.14       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.14       cgd  *
     12  1.14       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.14       cgd  * modification, are permitted provided that the following conditions
     14  1.14       cgd  * are met:
     15  1.14       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.14       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.14       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.14       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.14       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.14       cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.14       cgd  *    must display the following acknowledgement:
     22  1.14       cgd  *	This product includes software developed by the University of
     23  1.14       cgd  *	California, Berkeley and its contributors.
     24  1.14       cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.14       cgd  *    may be used to endorse or promote products derived from this software
     26  1.14       cgd  *    without specific prior written permission.
     27  1.14       cgd  *
     28  1.14       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.14       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.14       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.14       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.14       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.14       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.14       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.14       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.14       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.14       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.14       cgd  * SUCH DAMAGE.
     39  1.14       cgd  *
     40  1.44      fvdl  *	@(#)kern_prot.c	8.9 (Berkeley) 2/14/95
     41  1.14       cgd  */
     42  1.14       cgd 
     43  1.14       cgd /*
     44  1.14       cgd  * System calls related to processes and protection
     45  1.14       cgd  */
     46  1.45   thorpej 
     47  1.45   thorpej #include "opt_compat_freebsd.h"
     48  1.46   thorpej #include "opt_compat_ibcs2.h"
     49  1.47   thorpej #include "opt_compat_sunos.h"
     50  1.52  drochner #include "opt_compat_linux.h"
     51  1.51  christos #include "opt_compat_43.h"
     52  1.54       cgd #include "opt_compat_osf1.h"
     53  1.14       cgd 
     54  1.14       cgd #include <sys/param.h>
     55  1.14       cgd #include <sys/acct.h>
     56  1.14       cgd #include <sys/systm.h>
     57  1.14       cgd #include <sys/ucred.h>
     58  1.14       cgd #include <sys/proc.h>
     59  1.14       cgd #include <sys/timeb.h>
     60  1.14       cgd #include <sys/times.h>
     61  1.14       cgd #include <sys/malloc.h>
     62  1.14       cgd 
     63  1.19       cgd #include <sys/mount.h>
     64  1.19       cgd #include <sys/syscallargs.h>
     65  1.32  christos 
     66  1.14       cgd /* ARGSUSED */
     67  1.32  christos int
     68  1.30   mycroft sys_getpid(p, v, retval)
     69  1.14       cgd 	struct proc *p;
     70  1.30   mycroft 	void *v;
     71  1.19       cgd 	register_t *retval;
     72  1.14       cgd {
     73  1.14       cgd 
     74  1.14       cgd 	*retval = p->p_pid;
     75  1.31   mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
     76  1.54       cgd     defined(COMPAT_FREEBSD) || defined(COMPAT_OSF1) || \
     77  1.54       cgd     (defined(COMPAT_LINUX) && defined(__alpha__))
     78  1.14       cgd 	retval[1] = p->p_pptr->p_pid;
     79  1.14       cgd #endif
     80  1.14       cgd 	return (0);
     81  1.14       cgd }
     82  1.14       cgd 
     83  1.14       cgd /* ARGSUSED */
     84  1.32  christos int
     85  1.30   mycroft sys_getppid(p, v, retval)
     86  1.14       cgd 	struct proc *p;
     87  1.30   mycroft 	void *v;
     88  1.19       cgd 	register_t *retval;
     89  1.14       cgd {
     90  1.14       cgd 
     91  1.14       cgd 	*retval = p->p_pptr->p_pid;
     92  1.14       cgd 	return (0);
     93  1.14       cgd }
     94  1.14       cgd 
     95  1.14       cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
     96  1.32  christos int
     97  1.30   mycroft sys_getpgrp(p, v, retval)
     98  1.14       cgd 	struct proc *p;
     99  1.30   mycroft 	void *v;
    100  1.19       cgd 	register_t *retval;
    101  1.14       cgd {
    102  1.14       cgd 
    103  1.14       cgd 	*retval = p->p_pgrp->pg_id;
    104  1.14       cgd 	return (0);
    105  1.43   thorpej }
    106  1.43   thorpej 
    107  1.43   thorpej /*
    108  1.43   thorpej  * Return the process group ID of the session leader (session ID)
    109  1.43   thorpej  * for the specified process.
    110  1.43   thorpej  */
    111  1.43   thorpej int
    112  1.43   thorpej sys_getsid(p, v, retval)
    113  1.43   thorpej 	struct proc *p;
    114  1.43   thorpej 	void *v;
    115  1.43   thorpej 	register_t *retval;
    116  1.43   thorpej {
    117  1.43   thorpej 	struct sys_getsid_args /* {
    118  1.43   thorpej 		syscalldarg(pid_t) pid;
    119  1.43   thorpej 	} */ *uap = v;
    120  1.43   thorpej 
    121  1.43   thorpej 	if (SCARG(uap, pid) == 0)
    122  1.43   thorpej 		goto found;
    123  1.43   thorpej 	if ((p = pfind(SCARG(uap, pid))) == 0)
    124  1.43   thorpej 		return (ESRCH);
    125  1.43   thorpej found:
    126  1.43   thorpej 	*retval = p->p_session->s_sid;
    127  1.43   thorpej 	return 0;
    128  1.36       mrg }
    129  1.36       mrg 
    130  1.36       mrg int
    131  1.36       mrg sys_getpgid(p, v, retval)
    132  1.36       mrg 	struct proc *p;
    133  1.36       mrg 	void *v;
    134  1.36       mrg 	register_t *retval;
    135  1.36       mrg {
    136  1.36       mrg 	register struct sys_getpgid_args /* {
    137  1.36       mrg 		syscallarg(pid_t) pid;
    138  1.36       mrg 	} */ *uap = v;
    139  1.36       mrg 
    140  1.36       mrg 	if (SCARG(uap, pid) == 0)
    141  1.36       mrg 		goto found;
    142  1.36       mrg 	if ((p = pfind(SCARG(uap, pid))) == 0)
    143  1.36       mrg 		return (ESRCH);
    144  1.36       mrg found:
    145  1.36       mrg 	*retval = p->p_pgid;
    146  1.36       mrg 	return 0;
    147  1.14       cgd }
    148  1.14       cgd 
    149  1.14       cgd /* ARGSUSED */
    150  1.32  christos int
    151  1.30   mycroft sys_getuid(p, v, retval)
    152  1.14       cgd 	struct proc *p;
    153  1.30   mycroft 	void *v;
    154  1.19       cgd 	register_t *retval;
    155  1.14       cgd {
    156  1.14       cgd 
    157  1.14       cgd 	*retval = p->p_cred->p_ruid;
    158  1.31   mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
    159  1.54       cgd     defined(COMPAT_FREEBSD) || defined(COMPAT_OSF1) || \
    160  1.54       cgd     (defined(COMPAT_LINUX) && defined(__alpha__))
    161  1.14       cgd 	retval[1] = p->p_ucred->cr_uid;
    162  1.14       cgd #endif
    163  1.14       cgd 	return (0);
    164  1.14       cgd }
    165  1.14       cgd 
    166  1.14       cgd /* ARGSUSED */
    167  1.32  christos int
    168  1.30   mycroft sys_geteuid(p, v, retval)
    169  1.14       cgd 	struct proc *p;
    170  1.30   mycroft 	void *v;
    171  1.19       cgd 	register_t *retval;
    172  1.14       cgd {
    173  1.14       cgd 
    174  1.14       cgd 	*retval = p->p_ucred->cr_uid;
    175  1.14       cgd 	return (0);
    176  1.14       cgd }
    177  1.14       cgd 
    178  1.14       cgd /* ARGSUSED */
    179  1.32  christos int
    180  1.30   mycroft sys_getgid(p, v, retval)
    181  1.14       cgd 	struct proc *p;
    182  1.30   mycroft 	void *v;
    183  1.19       cgd 	register_t *retval;
    184  1.14       cgd {
    185  1.14       cgd 
    186  1.14       cgd 	*retval = p->p_cred->p_rgid;
    187  1.50       erh #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_FREEBSD) || \
    188  1.54       cgd     defined(COMPAT_OSF1) || (defined(COMPAT_LINUX) && defined(alpha))
    189  1.27       jtc 	retval[1] = p->p_ucred->cr_gid;
    190  1.14       cgd #endif
    191  1.14       cgd 	return (0);
    192  1.14       cgd }
    193  1.14       cgd 
    194  1.14       cgd /*
    195  1.14       cgd  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    196  1.14       cgd  * via getgroups.  This syscall exists because it is somewhat painful to do
    197  1.14       cgd  * correctly in a library function.
    198  1.14       cgd  */
    199  1.14       cgd /* ARGSUSED */
    200  1.32  christos int
    201  1.30   mycroft sys_getegid(p, v, retval)
    202  1.14       cgd 	struct proc *p;
    203  1.30   mycroft 	void *v;
    204  1.19       cgd 	register_t *retval;
    205  1.14       cgd {
    206  1.14       cgd 
    207  1.27       jtc 	*retval = p->p_ucred->cr_gid;
    208  1.14       cgd 	return (0);
    209  1.14       cgd }
    210  1.14       cgd 
    211  1.32  christos int
    212  1.30   mycroft sys_getgroups(p, v, retval)
    213  1.14       cgd 	struct proc *p;
    214  1.29   thorpej 	void *v;
    215  1.29   thorpej 	register_t *retval;
    216  1.29   thorpej {
    217  1.30   mycroft 	register struct sys_getgroups_args /* {
    218  1.41   thorpej 		syscallarg(int) gidsetsize;
    219  1.19       cgd 		syscallarg(gid_t *) gidset;
    220  1.29   thorpej 	} */ *uap = v;
    221  1.14       cgd 	register struct pcred *pc = p->p_cred;
    222  1.42   mycroft 	register int ngrp;
    223  1.14       cgd 	int error;
    224  1.14       cgd 
    225  1.42   mycroft 	ngrp = SCARG(uap, gidsetsize);
    226  1.42   mycroft 	if (ngrp == 0) {
    227  1.14       cgd 		*retval = pc->pc_ucred->cr_ngroups;
    228  1.14       cgd 		return (0);
    229  1.14       cgd 	}
    230  1.14       cgd 	if (ngrp < pc->pc_ucred->cr_ngroups)
    231  1.14       cgd 		return (EINVAL);
    232  1.14       cgd 	ngrp = pc->pc_ucred->cr_ngroups;
    233  1.32  christos 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    234  1.32  christos 			(caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    235  1.32  christos 	if (error)
    236  1.14       cgd 		return (error);
    237  1.14       cgd 	*retval = ngrp;
    238  1.14       cgd 	return (0);
    239  1.14       cgd }
    240  1.14       cgd 
    241  1.14       cgd /* ARGSUSED */
    242  1.32  christos int
    243  1.30   mycroft sys_setsid(p, v, retval)
    244  1.14       cgd 	register struct proc *p;
    245  1.30   mycroft 	void *v;
    246  1.19       cgd 	register_t *retval;
    247  1.14       cgd {
    248  1.14       cgd 
    249  1.14       cgd 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    250  1.14       cgd 		return (EPERM);
    251  1.14       cgd 	} else {
    252  1.15       cgd 		(void)enterpgrp(p, p->p_pid, 1);
    253  1.14       cgd 		*retval = p->p_pid;
    254  1.14       cgd 		return (0);
    255  1.14       cgd 	}
    256  1.14       cgd }
    257  1.14       cgd 
    258  1.14       cgd /*
    259  1.14       cgd  * set process group (setpgid/old setpgrp)
    260  1.14       cgd  *
    261  1.14       cgd  * caller does setpgid(targpid, targpgid)
    262  1.14       cgd  *
    263  1.39     mikel  * pgid must be in valid range (EINVAL)
    264  1.14       cgd  * pid must be caller or child of caller (ESRCH)
    265  1.14       cgd  * if a child
    266  1.14       cgd  *	pid must be in same session (EPERM)
    267  1.14       cgd  *	pid can't have done an exec (EACCES)
    268  1.14       cgd  * if pgid != pid
    269  1.14       cgd  * 	there must exist some pid in same session having pgid (EPERM)
    270  1.14       cgd  * pid must not be session leader (EPERM)
    271  1.14       cgd  */
    272  1.14       cgd /* ARGSUSED */
    273  1.32  christos int
    274  1.30   mycroft sys_setpgid(curp, v, retval)
    275  1.14       cgd 	struct proc *curp;
    276  1.29   thorpej 	void *v;
    277  1.29   thorpej 	register_t *retval;
    278  1.29   thorpej {
    279  1.30   mycroft 	register struct sys_setpgid_args /* {
    280  1.19       cgd 		syscallarg(int) pid;
    281  1.19       cgd 		syscallarg(int) pgid;
    282  1.29   thorpej 	} */ *uap = v;
    283  1.14       cgd 	register struct proc *targp;		/* target process */
    284  1.14       cgd 	register struct pgrp *pgrp;		/* target pgrp */
    285  1.14       cgd 
    286  1.39     mikel 	if (SCARG(uap, pgid) < 0)
    287  1.39     mikel 		return (EINVAL);
    288  1.14       cgd 
    289  1.19       cgd 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    290  1.19       cgd 		if ((targp = pfind(SCARG(uap, pid))) == 0 || !inferior(targp))
    291  1.14       cgd 			return (ESRCH);
    292  1.14       cgd 		if (targp->p_session != curp->p_session)
    293  1.14       cgd 			return (EPERM);
    294  1.15       cgd 		if (targp->p_flag & P_EXEC)
    295  1.14       cgd 			return (EACCES);
    296  1.14       cgd 	} else
    297  1.14       cgd 		targp = curp;
    298  1.14       cgd 	if (SESS_LEADER(targp))
    299  1.14       cgd 		return (EPERM);
    300  1.19       cgd 	if (SCARG(uap, pgid) == 0)
    301  1.19       cgd 		SCARG(uap, pgid) = targp->p_pid;
    302  1.19       cgd 	else if (SCARG(uap, pgid) != targp->p_pid)
    303  1.19       cgd 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    304  1.14       cgd 	            pgrp->pg_session != curp->p_session)
    305  1.14       cgd 			return (EPERM);
    306  1.19       cgd 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    307  1.14       cgd }
    308  1.14       cgd 
    309  1.14       cgd /* ARGSUSED */
    310  1.32  christos int
    311  1.30   mycroft sys_setuid(p, v, retval)
    312  1.14       cgd 	struct proc *p;
    313  1.29   thorpej 	void *v;
    314  1.29   thorpej 	register_t *retval;
    315  1.29   thorpej {
    316  1.30   mycroft 	struct sys_setuid_args /* {
    317  1.19       cgd 		syscallarg(uid_t) uid;
    318  1.29   thorpej 	} */ *uap = v;
    319  1.14       cgd 	register struct pcred *pc = p->p_cred;
    320  1.14       cgd 	register uid_t uid;
    321  1.14       cgd 	int error;
    322  1.14       cgd 
    323  1.19       cgd 	uid = SCARG(uap, uid);
    324  1.14       cgd 	if (uid != pc->p_ruid &&
    325  1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    326  1.14       cgd 		return (error);
    327  1.14       cgd 	/*
    328  1.15       cgd 	 * Everything's okay, do it.
    329  1.15       cgd 	 * Transfer proc count to new user.
    330  1.15       cgd 	 * Copy credentials so other references do not see our changes.
    331  1.14       cgd 	 */
    332  1.15       cgd 	(void)chgproccnt(pc->p_ruid, -1);
    333  1.15       cgd 	(void)chgproccnt(uid, 1);
    334  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    335  1.14       cgd 	pc->pc_ucred->cr_uid = uid;
    336  1.14       cgd 	pc->p_ruid = uid;
    337  1.14       cgd 	pc->p_svuid = uid;
    338  1.55    bouyer 	p_sugid(p);
    339  1.14       cgd 	return (0);
    340  1.14       cgd }
    341  1.14       cgd 
    342  1.14       cgd /* ARGSUSED */
    343  1.32  christos int
    344  1.30   mycroft sys_seteuid(p, v, retval)
    345  1.14       cgd 	struct proc *p;
    346  1.29   thorpej 	void *v;
    347  1.29   thorpej 	register_t *retval;
    348  1.29   thorpej {
    349  1.30   mycroft 	struct sys_seteuid_args /* {
    350  1.19       cgd 		syscallarg(uid_t) euid;
    351  1.29   thorpej 	} */ *uap = v;
    352  1.14       cgd 	register struct pcred *pc = p->p_cred;
    353  1.14       cgd 	register uid_t euid;
    354  1.14       cgd 	int error;
    355  1.14       cgd 
    356  1.19       cgd 	euid = SCARG(uap, euid);
    357  1.14       cgd 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    358  1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    359  1.14       cgd 		return (error);
    360  1.14       cgd 	/*
    361  1.14       cgd 	 * Everything's okay, do it.  Copy credentials so other references do
    362  1.14       cgd 	 * not see our changes.
    363  1.14       cgd 	 */
    364  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    365  1.14       cgd 	pc->pc_ucred->cr_uid = euid;
    366  1.55    bouyer 	p_sugid(p);
    367  1.14       cgd 	return (0);
    368  1.14       cgd }
    369  1.14       cgd 
    370  1.35   mycroft int
    371  1.35   mycroft sys_setreuid(p, v, retval)
    372  1.35   mycroft 	struct proc *p;
    373  1.35   mycroft 	void *v;
    374  1.35   mycroft 	register_t *retval;
    375  1.35   mycroft {
    376  1.35   mycroft 	struct sys_setreuid_args /* {
    377  1.35   mycroft 		syscallarg(uid_t) ruid;
    378  1.35   mycroft 		syscallarg(uid_t) euid;
    379  1.35   mycroft 	} */ *uap = v;
    380  1.35   mycroft 	register struct pcred *pc = p->p_cred;
    381  1.35   mycroft 	register uid_t ruid, euid;
    382  1.35   mycroft 	int error;
    383  1.35   mycroft 
    384  1.35   mycroft 	ruid = SCARG(uap, ruid);
    385  1.35   mycroft 	euid = SCARG(uap, euid);
    386  1.35   mycroft 
    387  1.35   mycroft 	if (ruid != (uid_t)-1 &&
    388  1.35   mycroft 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    389  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    390  1.35   mycroft 		return (error);
    391  1.35   mycroft 
    392  1.35   mycroft 	if (euid != (uid_t)-1 &&
    393  1.35   mycroft 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    394  1.35   mycroft 	    euid != pc->p_svuid &&
    395  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    396  1.35   mycroft 		return (error);
    397  1.35   mycroft 
    398  1.35   mycroft 	if (euid != (uid_t)-1) {
    399  1.35   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    400  1.35   mycroft 		pc->pc_ucred->cr_uid = euid;
    401  1.35   mycroft 	}
    402  1.35   mycroft 
    403  1.35   mycroft 	if (ruid != (uid_t)-1) {
    404  1.35   mycroft 		(void)chgproccnt(pc->p_ruid, -1);
    405  1.35   mycroft 		(void)chgproccnt(ruid, 1);
    406  1.35   mycroft 		pc->p_ruid = ruid;
    407  1.35   mycroft 		pc->p_svuid = pc->pc_ucred->cr_uid;
    408  1.35   mycroft 	}
    409  1.35   mycroft 
    410  1.35   mycroft 	if (euid != (uid_t)-1 && ruid != (uid_t)-1)
    411  1.55    bouyer 		p_sugid(p);
    412  1.35   mycroft 	return (0);
    413  1.35   mycroft }
    414  1.35   mycroft 
    415  1.14       cgd /* ARGSUSED */
    416  1.32  christos int
    417  1.30   mycroft sys_setgid(p, v, retval)
    418  1.14       cgd 	struct proc *p;
    419  1.29   thorpej 	void *v;
    420  1.29   thorpej 	register_t *retval;
    421  1.29   thorpej {
    422  1.30   mycroft 	struct sys_setgid_args /* {
    423  1.19       cgd 		syscallarg(gid_t) gid;
    424  1.29   thorpej 	} */ *uap = v;
    425  1.14       cgd 	register struct pcred *pc = p->p_cred;
    426  1.14       cgd 	register gid_t gid;
    427  1.14       cgd 	int error;
    428  1.14       cgd 
    429  1.19       cgd 	gid = SCARG(uap, gid);
    430  1.34   mycroft 	if (gid != pc->p_rgid &&
    431  1.34   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    432  1.14       cgd 		return (error);
    433  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    434  1.27       jtc 	pc->pc_ucred->cr_gid = gid;
    435  1.14       cgd 	pc->p_rgid = gid;
    436  1.34   mycroft 	pc->p_svgid = gid;
    437  1.55    bouyer 	p_sugid(p);
    438  1.14       cgd 	return (0);
    439  1.14       cgd }
    440  1.14       cgd 
    441  1.14       cgd /* ARGSUSED */
    442  1.32  christos int
    443  1.30   mycroft sys_setegid(p, v, retval)
    444  1.14       cgd 	struct proc *p;
    445  1.29   thorpej 	void *v;
    446  1.29   thorpej 	register_t *retval;
    447  1.29   thorpej {
    448  1.30   mycroft 	struct sys_setegid_args /* {
    449  1.19       cgd 		syscallarg(gid_t) egid;
    450  1.29   thorpej 	} */ *uap = v;
    451  1.14       cgd 	register struct pcred *pc = p->p_cred;
    452  1.14       cgd 	register gid_t egid;
    453  1.14       cgd 	int error;
    454  1.14       cgd 
    455  1.19       cgd 	egid = SCARG(uap, egid);
    456  1.14       cgd 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    457  1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    458  1.14       cgd 		return (error);
    459  1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    460  1.27       jtc 	pc->pc_ucred->cr_gid = egid;
    461  1.55    bouyer 	p_sugid(p);
    462  1.35   mycroft 	return (0);
    463  1.35   mycroft }
    464  1.35   mycroft 
    465  1.35   mycroft int
    466  1.35   mycroft sys_setregid(p, v, retval)
    467  1.35   mycroft 	struct proc *p;
    468  1.35   mycroft 	void *v;
    469  1.35   mycroft 	register_t *retval;
    470  1.35   mycroft {
    471  1.35   mycroft 	struct sys_setregid_args /* {
    472  1.35   mycroft 		syscallarg(gid_t) rgid;
    473  1.35   mycroft 		syscallarg(gid_t) egid;
    474  1.35   mycroft 	} */ *uap = v;
    475  1.35   mycroft 	register struct pcred *pc = p->p_cred;
    476  1.35   mycroft 	register gid_t rgid, egid;
    477  1.35   mycroft 	int error;
    478  1.35   mycroft 
    479  1.35   mycroft 	rgid = SCARG(uap, rgid);
    480  1.35   mycroft 	egid = SCARG(uap, egid);
    481  1.35   mycroft 
    482  1.35   mycroft 	if (rgid != (gid_t)-1 &&
    483  1.35   mycroft 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    484  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    485  1.35   mycroft 		return (error);
    486  1.35   mycroft 
    487  1.35   mycroft 	if (egid != (gid_t)-1 &&
    488  1.35   mycroft 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    489  1.35   mycroft 	    egid != pc->p_svgid &&
    490  1.35   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    491  1.35   mycroft 		return (error);
    492  1.35   mycroft 
    493  1.35   mycroft 	if (egid != (gid_t)-1) {
    494  1.35   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    495  1.35   mycroft 		pc->pc_ucred->cr_gid = egid;
    496  1.35   mycroft 	}
    497  1.35   mycroft 
    498  1.35   mycroft 	if (rgid != (gid_t)-1) {
    499  1.35   mycroft 		pc->p_rgid = rgid;
    500  1.35   mycroft 		pc->p_svgid = pc->pc_ucred->cr_gid;
    501  1.35   mycroft 	}
    502  1.35   mycroft 
    503  1.35   mycroft 	if (egid != (gid_t)-1 && rgid != (gid_t)-1)
    504  1.55    bouyer 		p_sugid(p);
    505  1.14       cgd 	return (0);
    506  1.14       cgd }
    507  1.14       cgd 
    508  1.15       cgd /* ARGSUSED */
    509  1.32  christos int
    510  1.30   mycroft sys_setgroups(p, v, retval)
    511  1.15       cgd 	struct proc *p;
    512  1.29   thorpej 	void *v;
    513  1.29   thorpej 	register_t *retval;
    514  1.29   thorpej {
    515  1.30   mycroft 	struct sys_setgroups_args /* {
    516  1.41   thorpej 		syscallarg(int) gidsetsize;
    517  1.38       cgd 		syscallarg(const gid_t *) gidset;
    518  1.29   thorpej 	} */ *uap = v;
    519  1.15       cgd 	register struct pcred *pc = p->p_cred;
    520  1.42   mycroft 	register int ngrp;
    521  1.15       cgd 	int error;
    522  1.15       cgd 
    523  1.32  christos 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    524  1.15       cgd 		return (error);
    525  1.19       cgd 	ngrp = SCARG(uap, gidsetsize);
    526  1.42   mycroft 	if ((u_int)ngrp > NGROUPS)
    527  1.15       cgd 		return (EINVAL);
    528  1.15       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    529  1.38       cgd 	error = copyin(SCARG(uap, gidset), pc->pc_ucred->cr_groups,
    530  1.38       cgd 	    ngrp * sizeof(gid_t));
    531  1.32  christos 	if (error)
    532  1.15       cgd 		return (error);
    533  1.15       cgd 	pc->pc_ucred->cr_ngroups = ngrp;
    534  1.55    bouyer 	p_sugid(p);
    535  1.15       cgd 	return (0);
    536  1.15       cgd }
    537  1.14       cgd 
    538  1.14       cgd /*
    539  1.14       cgd  * Check if gid is a member of the group set.
    540  1.14       cgd  */
    541  1.32  christos int
    542  1.14       cgd groupmember(gid, cred)
    543  1.14       cgd 	gid_t gid;
    544  1.14       cgd 	register struct ucred *cred;
    545  1.14       cgd {
    546  1.14       cgd 	register gid_t *gp;
    547  1.14       cgd 	gid_t *egp;
    548  1.14       cgd 
    549  1.14       cgd 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    550  1.14       cgd 	for (gp = cred->cr_groups; gp < egp; gp++)
    551  1.14       cgd 		if (*gp == gid)
    552  1.14       cgd 			return (1);
    553  1.14       cgd 	return (0);
    554  1.14       cgd }
    555  1.14       cgd 
    556  1.14       cgd /*
    557  1.14       cgd  * Test whether the specified credentials imply "super-user"
    558  1.14       cgd  * privilege; if so, and we have accounting info, set the flag
    559  1.14       cgd  * indicating use of super-powers.
    560  1.14       cgd  * Returns 0 or error.
    561  1.14       cgd  */
    562  1.32  christos int
    563  1.14       cgd suser(cred, acflag)
    564  1.14       cgd 	struct ucred *cred;
    565  1.14       cgd 	u_short *acflag;
    566  1.14       cgd {
    567  1.14       cgd 	if (cred->cr_uid == 0) {
    568  1.14       cgd 		if (acflag)
    569  1.14       cgd 			*acflag |= ASU;
    570  1.14       cgd 		return (0);
    571  1.14       cgd 	}
    572  1.14       cgd 	return (EPERM);
    573  1.14       cgd }
    574  1.14       cgd 
    575  1.14       cgd /*
    576  1.14       cgd  * Allocate a zeroed cred structure.
    577  1.14       cgd  */
    578  1.14       cgd struct ucred *
    579  1.14       cgd crget()
    580  1.14       cgd {
    581  1.14       cgd 	register struct ucred *cr;
    582  1.14       cgd 
    583  1.14       cgd 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    584  1.49     perry 	memset((caddr_t)cr, 0, sizeof(*cr));
    585  1.14       cgd 	cr->cr_ref = 1;
    586  1.14       cgd 	return (cr);
    587  1.14       cgd }
    588  1.14       cgd 
    589  1.14       cgd /*
    590  1.14       cgd  * Free a cred structure.
    591  1.14       cgd  * Throws away space when ref count gets to 0.
    592  1.14       cgd  */
    593  1.22       cgd void
    594  1.14       cgd crfree(cr)
    595  1.14       cgd 	struct ucred *cr;
    596  1.14       cgd {
    597  1.15       cgd 	int s;
    598  1.14       cgd 
    599  1.15       cgd 	s = splimp();				/* ??? */
    600  1.15       cgd 	if (--cr->cr_ref == 0)
    601  1.15       cgd 		FREE((caddr_t)cr, M_CRED);
    602  1.14       cgd 	(void) splx(s);
    603  1.14       cgd }
    604  1.14       cgd 
    605  1.14       cgd /*
    606  1.14       cgd  * Copy cred structure to a new one and free the old one.
    607  1.14       cgd  */
    608  1.14       cgd struct ucred *
    609  1.14       cgd crcopy(cr)
    610  1.14       cgd 	struct ucred *cr;
    611  1.14       cgd {
    612  1.14       cgd 	struct ucred *newcr;
    613  1.14       cgd 
    614  1.14       cgd 	if (cr->cr_ref == 1)
    615  1.14       cgd 		return (cr);
    616  1.14       cgd 	newcr = crget();
    617  1.14       cgd 	*newcr = *cr;
    618  1.14       cgd 	crfree(cr);
    619  1.14       cgd 	newcr->cr_ref = 1;
    620  1.14       cgd 	return (newcr);
    621  1.14       cgd }
    622  1.14       cgd 
    623  1.14       cgd /*
    624  1.14       cgd  * Dup cred struct to a new held one.
    625  1.14       cgd  */
    626  1.14       cgd struct ucred *
    627  1.14       cgd crdup(cr)
    628  1.14       cgd 	struct ucred *cr;
    629  1.14       cgd {
    630  1.14       cgd 	struct ucred *newcr;
    631  1.14       cgd 
    632  1.14       cgd 	newcr = crget();
    633  1.14       cgd 	*newcr = *cr;
    634  1.14       cgd 	newcr->cr_ref = 1;
    635  1.14       cgd 	return (newcr);
    636  1.14       cgd }
    637  1.14       cgd 
    638  1.14       cgd /*
    639  1.14       cgd  * Get login name, if available.
    640  1.14       cgd  */
    641  1.14       cgd /* ARGSUSED */
    642  1.32  christos int
    643  1.37       jtc sys___getlogin(p, v, retval)
    644  1.14       cgd 	struct proc *p;
    645  1.29   thorpej 	void *v;
    646  1.29   thorpej 	register_t *retval;
    647  1.29   thorpej {
    648  1.37       jtc 	struct sys___getlogin_args /* {
    649  1.19       cgd 		syscallarg(char *) namebuf;
    650  1.53    kleink 		syscallarg(size_t) namelen;
    651  1.29   thorpej 	} */ *uap = v;
    652  1.14       cgd 
    653  1.48     perry 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    654  1.48     perry 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    655  1.14       cgd 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    656  1.19       cgd 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    657  1.14       cgd }
    658  1.14       cgd 
    659  1.14       cgd /*
    660  1.14       cgd  * Set login name.
    661  1.14       cgd  */
    662  1.14       cgd /* ARGSUSED */
    663  1.32  christos int
    664  1.30   mycroft sys_setlogin(p, v, retval)
    665  1.14       cgd 	struct proc *p;
    666  1.29   thorpej 	void *v;
    667  1.29   thorpej 	register_t *retval;
    668  1.29   thorpej {
    669  1.30   mycroft 	struct sys_setlogin_args /* {
    670  1.38       cgd 		syscallarg(const char *) namebuf;
    671  1.29   thorpej 	} */ *uap = v;
    672  1.14       cgd 	int error;
    673  1.14       cgd 
    674  1.32  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    675  1.14       cgd 		return (error);
    676  1.38       cgd 	error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
    677  1.48     perry 	    sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
    678  1.14       cgd 	if (error == ENAMETOOLONG)
    679  1.14       cgd 		error = EINVAL;
    680  1.14       cgd 	return (error);
    681  1.14       cgd }
    682