Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.33.4.1
      1  1.33.4.1   mycroft /*	$NetBSD: kern_prot.c,v 1.33.4.1 1996/12/10 08:04:30 mycroft 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.16       cgd  *	@(#)kern_prot.c	8.6 (Berkeley) 1/21/94
     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.14       cgd 
     47      1.14       cgd #include <sys/param.h>
     48      1.14       cgd #include <sys/acct.h>
     49      1.14       cgd #include <sys/systm.h>
     50      1.14       cgd #include <sys/ucred.h>
     51      1.14       cgd #include <sys/proc.h>
     52      1.14       cgd #include <sys/timeb.h>
     53      1.14       cgd #include <sys/times.h>
     54      1.14       cgd #include <sys/malloc.h>
     55      1.14       cgd 
     56      1.19       cgd #include <sys/mount.h>
     57      1.19       cgd #include <sys/syscallargs.h>
     58      1.32  christos 
     59      1.14       cgd /* ARGSUSED */
     60      1.32  christos int
     61      1.30   mycroft sys_getpid(p, v, retval)
     62      1.14       cgd 	struct proc *p;
     63      1.30   mycroft 	void *v;
     64      1.19       cgd 	register_t *retval;
     65      1.14       cgd {
     66      1.14       cgd 
     67      1.14       cgd 	*retval = p->p_pid;
     68      1.31   mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
     69      1.31   mycroft     defined(COMPAT_FREEBSD)
     70      1.14       cgd 	retval[1] = p->p_pptr->p_pid;
     71      1.14       cgd #endif
     72      1.14       cgd 	return (0);
     73      1.14       cgd }
     74      1.14       cgd 
     75      1.14       cgd /* ARGSUSED */
     76      1.32  christos int
     77      1.30   mycroft sys_getppid(p, v, retval)
     78      1.14       cgd 	struct proc *p;
     79      1.30   mycroft 	void *v;
     80      1.19       cgd 	register_t *retval;
     81      1.14       cgd {
     82      1.14       cgd 
     83      1.14       cgd 	*retval = p->p_pptr->p_pid;
     84      1.14       cgd 	return (0);
     85      1.14       cgd }
     86      1.14       cgd 
     87      1.14       cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
     88      1.32  christos int
     89      1.30   mycroft sys_getpgrp(p, v, retval)
     90      1.14       cgd 	struct proc *p;
     91      1.30   mycroft 	void *v;
     92      1.19       cgd 	register_t *retval;
     93      1.14       cgd {
     94      1.14       cgd 
     95      1.14       cgd 	*retval = p->p_pgrp->pg_id;
     96      1.14       cgd 	return (0);
     97      1.14       cgd }
     98      1.14       cgd 
     99      1.14       cgd /* ARGSUSED */
    100      1.32  christos int
    101      1.30   mycroft sys_getuid(p, v, retval)
    102      1.14       cgd 	struct proc *p;
    103      1.30   mycroft 	void *v;
    104      1.19       cgd 	register_t *retval;
    105      1.14       cgd {
    106      1.14       cgd 
    107      1.14       cgd 	*retval = p->p_cred->p_ruid;
    108      1.31   mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
    109      1.31   mycroft     defined(COMPAT_FREEBSD)
    110      1.14       cgd 	retval[1] = p->p_ucred->cr_uid;
    111      1.14       cgd #endif
    112      1.14       cgd 	return (0);
    113      1.14       cgd }
    114      1.14       cgd 
    115      1.14       cgd /* ARGSUSED */
    116      1.32  christos int
    117      1.30   mycroft sys_geteuid(p, v, retval)
    118      1.14       cgd 	struct proc *p;
    119      1.30   mycroft 	void *v;
    120      1.19       cgd 	register_t *retval;
    121      1.14       cgd {
    122      1.14       cgd 
    123      1.14       cgd 	*retval = p->p_ucred->cr_uid;
    124      1.14       cgd 	return (0);
    125      1.14       cgd }
    126      1.14       cgd 
    127      1.14       cgd /* ARGSUSED */
    128      1.32  christos int
    129      1.30   mycroft sys_getgid(p, v, retval)
    130      1.14       cgd 	struct proc *p;
    131      1.30   mycroft 	void *v;
    132      1.19       cgd 	register_t *retval;
    133      1.14       cgd {
    134      1.14       cgd 
    135      1.14       cgd 	*retval = p->p_cred->p_rgid;
    136      1.31   mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_FREEBSD)
    137      1.27       jtc 	retval[1] = p->p_ucred->cr_gid;
    138      1.14       cgd #endif
    139      1.14       cgd 	return (0);
    140      1.14       cgd }
    141      1.14       cgd 
    142      1.14       cgd /*
    143      1.14       cgd  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    144      1.14       cgd  * via getgroups.  This syscall exists because it is somewhat painful to do
    145      1.14       cgd  * correctly in a library function.
    146      1.14       cgd  */
    147      1.14       cgd /* ARGSUSED */
    148      1.32  christos int
    149      1.30   mycroft sys_getegid(p, v, retval)
    150      1.14       cgd 	struct proc *p;
    151      1.30   mycroft 	void *v;
    152      1.19       cgd 	register_t *retval;
    153      1.14       cgd {
    154      1.14       cgd 
    155      1.27       jtc 	*retval = p->p_ucred->cr_gid;
    156      1.14       cgd 	return (0);
    157      1.14       cgd }
    158      1.14       cgd 
    159      1.32  christos int
    160      1.30   mycroft sys_getgroups(p, v, retval)
    161      1.14       cgd 	struct proc *p;
    162      1.29   thorpej 	void *v;
    163      1.29   thorpej 	register_t *retval;
    164      1.29   thorpej {
    165      1.30   mycroft 	register struct sys_getgroups_args /* {
    166      1.19       cgd 		syscallarg(u_int) gidsetsize;
    167      1.19       cgd 		syscallarg(gid_t *) gidset;
    168      1.29   thorpej 	} */ *uap = v;
    169      1.14       cgd 	register struct pcred *pc = p->p_cred;
    170      1.14       cgd 	register u_int ngrp;
    171      1.14       cgd 	int error;
    172      1.14       cgd 
    173      1.19       cgd 	if ((ngrp = SCARG(uap, gidsetsize)) == 0) {
    174      1.14       cgd 		*retval = pc->pc_ucred->cr_ngroups;
    175      1.14       cgd 		return (0);
    176      1.14       cgd 	}
    177      1.14       cgd 	if (ngrp < pc->pc_ucred->cr_ngroups)
    178      1.14       cgd 		return (EINVAL);
    179      1.14       cgd 	ngrp = pc->pc_ucred->cr_ngroups;
    180      1.32  christos 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    181      1.32  christos 			(caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    182      1.32  christos 	if (error)
    183      1.14       cgd 		return (error);
    184      1.14       cgd 	*retval = ngrp;
    185      1.14       cgd 	return (0);
    186      1.14       cgd }
    187      1.14       cgd 
    188      1.14       cgd /* ARGSUSED */
    189      1.32  christos int
    190      1.30   mycroft sys_setsid(p, v, retval)
    191      1.14       cgd 	register struct proc *p;
    192      1.30   mycroft 	void *v;
    193      1.19       cgd 	register_t *retval;
    194      1.14       cgd {
    195      1.14       cgd 
    196      1.14       cgd 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    197      1.14       cgd 		return (EPERM);
    198      1.14       cgd 	} else {
    199      1.15       cgd 		(void)enterpgrp(p, p->p_pid, 1);
    200      1.14       cgd 		*retval = p->p_pid;
    201      1.14       cgd 		return (0);
    202      1.14       cgd 	}
    203      1.14       cgd }
    204      1.14       cgd 
    205      1.14       cgd /*
    206      1.14       cgd  * set process group (setpgid/old setpgrp)
    207      1.14       cgd  *
    208      1.14       cgd  * caller does setpgid(targpid, targpgid)
    209      1.14       cgd  *
    210      1.14       cgd  * pid must be caller or child of caller (ESRCH)
    211      1.14       cgd  * if a child
    212      1.14       cgd  *	pid must be in same session (EPERM)
    213      1.14       cgd  *	pid can't have done an exec (EACCES)
    214      1.14       cgd  * if pgid != pid
    215      1.14       cgd  * 	there must exist some pid in same session having pgid (EPERM)
    216      1.14       cgd  * pid must not be session leader (EPERM)
    217      1.14       cgd  */
    218      1.14       cgd /* ARGSUSED */
    219      1.32  christos int
    220      1.30   mycroft sys_setpgid(curp, v, retval)
    221      1.14       cgd 	struct proc *curp;
    222      1.29   thorpej 	void *v;
    223      1.29   thorpej 	register_t *retval;
    224      1.29   thorpej {
    225      1.30   mycroft 	register struct sys_setpgid_args /* {
    226      1.19       cgd 		syscallarg(int) pid;
    227      1.19       cgd 		syscallarg(int) pgid;
    228      1.29   thorpej 	} */ *uap = v;
    229      1.14       cgd 	register struct proc *targp;		/* target process */
    230      1.14       cgd 	register struct pgrp *pgrp;		/* target pgrp */
    231      1.14       cgd 
    232      1.14       cgd #ifdef COMPAT_09
    233      1.19       cgd 	SCARG(uap, pid)  = (short) SCARG(uap, pid);		/* XXX */
    234      1.19       cgd 	SCARG(uap, pgid) = (short) SCARG(uap, pgid);		/* XXX */
    235      1.14       cgd #endif
    236      1.14       cgd 
    237      1.19       cgd 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    238      1.19       cgd 		if ((targp = pfind(SCARG(uap, pid))) == 0 || !inferior(targp))
    239      1.14       cgd 			return (ESRCH);
    240      1.14       cgd 		if (targp->p_session != curp->p_session)
    241      1.14       cgd 			return (EPERM);
    242      1.15       cgd 		if (targp->p_flag & P_EXEC)
    243      1.14       cgd 			return (EACCES);
    244      1.14       cgd 	} else
    245      1.14       cgd 		targp = curp;
    246      1.14       cgd 	if (SESS_LEADER(targp))
    247      1.14       cgd 		return (EPERM);
    248      1.19       cgd 	if (SCARG(uap, pgid) == 0)
    249      1.19       cgd 		SCARG(uap, pgid) = targp->p_pid;
    250      1.19       cgd 	else if (SCARG(uap, pgid) != targp->p_pid)
    251      1.19       cgd 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    252      1.14       cgd 	            pgrp->pg_session != curp->p_session)
    253      1.14       cgd 			return (EPERM);
    254      1.19       cgd 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    255      1.14       cgd }
    256      1.14       cgd 
    257      1.14       cgd /* ARGSUSED */
    258      1.32  christos int
    259      1.30   mycroft sys_setuid(p, v, retval)
    260      1.14       cgd 	struct proc *p;
    261      1.29   thorpej 	void *v;
    262      1.29   thorpej 	register_t *retval;
    263      1.29   thorpej {
    264      1.30   mycroft 	struct sys_setuid_args /* {
    265      1.19       cgd 		syscallarg(uid_t) uid;
    266      1.29   thorpej 	} */ *uap = v;
    267      1.14       cgd 	register struct pcred *pc = p->p_cred;
    268      1.14       cgd 	register uid_t uid;
    269      1.14       cgd 	int error;
    270      1.14       cgd 
    271      1.14       cgd #ifdef COMPAT_09				/* XXX */
    272      1.19       cgd 	uid = (u_short)SCARG(uap, uid);
    273      1.14       cgd #else
    274      1.19       cgd 	uid = SCARG(uap, uid);
    275      1.14       cgd #endif
    276      1.14       cgd 	if (uid != pc->p_ruid &&
    277      1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    278      1.14       cgd 		return (error);
    279      1.14       cgd 	/*
    280      1.15       cgd 	 * Everything's okay, do it.
    281      1.15       cgd 	 * Transfer proc count to new user.
    282      1.15       cgd 	 * Copy credentials so other references do not see our changes.
    283      1.14       cgd 	 */
    284      1.15       cgd 	(void)chgproccnt(pc->p_ruid, -1);
    285      1.15       cgd 	(void)chgproccnt(uid, 1);
    286      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    287      1.14       cgd 	pc->pc_ucred->cr_uid = uid;
    288      1.14       cgd 	pc->p_ruid = uid;
    289      1.14       cgd 	pc->p_svuid = uid;
    290      1.14       cgd 	p->p_flag |= P_SUGID;
    291      1.14       cgd 	return (0);
    292      1.14       cgd }
    293      1.14       cgd 
    294      1.14       cgd /* ARGSUSED */
    295      1.32  christos int
    296      1.30   mycroft sys_seteuid(p, v, retval)
    297      1.14       cgd 	struct proc *p;
    298      1.29   thorpej 	void *v;
    299      1.29   thorpej 	register_t *retval;
    300      1.29   thorpej {
    301      1.30   mycroft 	struct sys_seteuid_args /* {
    302      1.19       cgd 		syscallarg(uid_t) euid;
    303      1.29   thorpej 	} */ *uap = v;
    304      1.14       cgd 	register struct pcred *pc = p->p_cred;
    305      1.14       cgd 	register uid_t euid;
    306      1.14       cgd 	int error;
    307      1.14       cgd 
    308      1.14       cgd #ifdef COMPAT_09				/* XXX */
    309      1.19       cgd 	euid = (u_short)SCARG(uap, euid);
    310      1.14       cgd #else
    311      1.19       cgd 	euid = SCARG(uap, euid);
    312      1.14       cgd #endif
    313      1.14       cgd 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    314      1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    315      1.14       cgd 		return (error);
    316      1.14       cgd 	/*
    317      1.14       cgd 	 * Everything's okay, do it.  Copy credentials so other references do
    318      1.14       cgd 	 * not see our changes.
    319      1.14       cgd 	 */
    320      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    321      1.14       cgd 	pc->pc_ucred->cr_uid = euid;
    322      1.14       cgd 	p->p_flag |= P_SUGID;
    323      1.14       cgd 	return (0);
    324      1.14       cgd }
    325      1.14       cgd 
    326  1.33.4.1   mycroft int
    327  1.33.4.1   mycroft sys_setreuid(p, v, retval)
    328  1.33.4.1   mycroft 	struct proc *p;
    329  1.33.4.1   mycroft 	void *v;
    330  1.33.4.1   mycroft 	register_t *retval;
    331  1.33.4.1   mycroft {
    332  1.33.4.1   mycroft 	struct sys_setreuid_args /* {
    333  1.33.4.1   mycroft 		syscallarg(uid_t) ruid;
    334  1.33.4.1   mycroft 		syscallarg(uid_t) euid;
    335  1.33.4.1   mycroft 	} */ *uap = v;
    336  1.33.4.1   mycroft 	register struct pcred *pc = p->p_cred;
    337  1.33.4.1   mycroft 	register uid_t ruid, euid;
    338  1.33.4.1   mycroft 	int error;
    339  1.33.4.1   mycroft 
    340  1.33.4.1   mycroft 	ruid = SCARG(uap, ruid);
    341  1.33.4.1   mycroft 	euid = SCARG(uap, euid);
    342  1.33.4.1   mycroft 
    343  1.33.4.1   mycroft 	if (ruid != (uid_t)-1 &&
    344  1.33.4.1   mycroft 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    345  1.33.4.1   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    346  1.33.4.1   mycroft 		return (error);
    347  1.33.4.1   mycroft 
    348  1.33.4.1   mycroft 	if (euid != (uid_t)-1 &&
    349  1.33.4.1   mycroft 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    350  1.33.4.1   mycroft 	    euid != pc->p_svuid &&
    351  1.33.4.1   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    352  1.33.4.1   mycroft 		return (error);
    353  1.33.4.1   mycroft 
    354  1.33.4.1   mycroft 	if (euid != (uid_t)-1) {
    355  1.33.4.1   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    356  1.33.4.1   mycroft 		pc->pc_ucred->cr_uid = euid;
    357  1.33.4.1   mycroft 	}
    358  1.33.4.1   mycroft 
    359  1.33.4.1   mycroft 	if (ruid != (uid_t)-1) {
    360  1.33.4.1   mycroft 		(void)chgproccnt(pc->p_ruid, -1);
    361  1.33.4.1   mycroft 		(void)chgproccnt(ruid, 1);
    362  1.33.4.1   mycroft 		pc->p_ruid = ruid;
    363  1.33.4.1   mycroft 		pc->p_svuid = pc->pc_ucred->cr_uid;
    364  1.33.4.1   mycroft 	}
    365  1.33.4.1   mycroft 
    366  1.33.4.1   mycroft 	if (euid != (uid_t)-1 && ruid != (uid_t)-1)
    367  1.33.4.1   mycroft 		p->p_flag |= P_SUGID;
    368  1.33.4.1   mycroft 	return (0);
    369  1.33.4.1   mycroft }
    370  1.33.4.1   mycroft 
    371      1.14       cgd /* ARGSUSED */
    372      1.32  christos int
    373      1.30   mycroft sys_setgid(p, v, retval)
    374      1.14       cgd 	struct proc *p;
    375      1.29   thorpej 	void *v;
    376      1.29   thorpej 	register_t *retval;
    377      1.29   thorpej {
    378      1.30   mycroft 	struct sys_setgid_args /* {
    379      1.19       cgd 		syscallarg(gid_t) gid;
    380      1.29   thorpej 	} */ *uap = v;
    381      1.14       cgd 	register struct pcred *pc = p->p_cred;
    382      1.14       cgd 	register gid_t gid;
    383      1.14       cgd 	int error;
    384      1.14       cgd 
    385      1.14       cgd #ifdef COMPAT_09				/* XXX */
    386      1.19       cgd 	gid = (u_short)SCARG(uap, gid);
    387      1.14       cgd #else
    388      1.19       cgd 	gid = SCARG(uap, gid);
    389      1.14       cgd #endif
    390  1.33.4.1   mycroft 	if (gid != pc->p_rgid &&
    391  1.33.4.1   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    392      1.14       cgd 		return (error);
    393      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    394      1.27       jtc 	pc->pc_ucred->cr_gid = gid;
    395      1.14       cgd 	pc->p_rgid = gid;
    396  1.33.4.1   mycroft 	pc->p_svgid = gid;
    397      1.14       cgd 	p->p_flag |= P_SUGID;
    398      1.14       cgd 	return (0);
    399      1.14       cgd }
    400      1.14       cgd 
    401      1.14       cgd /* ARGSUSED */
    402      1.32  christos int
    403      1.30   mycroft sys_setegid(p, v, retval)
    404      1.14       cgd 	struct proc *p;
    405      1.29   thorpej 	void *v;
    406      1.29   thorpej 	register_t *retval;
    407      1.29   thorpej {
    408      1.30   mycroft 	struct sys_setegid_args /* {
    409      1.19       cgd 		syscallarg(gid_t) egid;
    410      1.29   thorpej 	} */ *uap = v;
    411      1.14       cgd 	register struct pcred *pc = p->p_cred;
    412      1.14       cgd 	register gid_t egid;
    413      1.14       cgd 	int error;
    414      1.14       cgd 
    415      1.14       cgd #ifdef COMPAT_09				/* XXX */
    416      1.19       cgd 	egid = (u_short)SCARG(uap, egid);
    417      1.14       cgd #else
    418      1.19       cgd 	egid = SCARG(uap, egid);
    419      1.14       cgd #endif
    420      1.14       cgd 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    421      1.14       cgd 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    422      1.14       cgd 		return (error);
    423      1.14       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    424      1.27       jtc 	pc->pc_ucred->cr_gid = egid;
    425      1.14       cgd 	p->p_flag |= P_SUGID;
    426  1.33.4.1   mycroft 	return (0);
    427  1.33.4.1   mycroft }
    428  1.33.4.1   mycroft 
    429  1.33.4.1   mycroft int
    430  1.33.4.1   mycroft sys_setregid(p, v, retval)
    431  1.33.4.1   mycroft 	struct proc *p;
    432  1.33.4.1   mycroft 	void *v;
    433  1.33.4.1   mycroft 	register_t *retval;
    434  1.33.4.1   mycroft {
    435  1.33.4.1   mycroft 	struct sys_setregid_args /* {
    436  1.33.4.1   mycroft 		syscallarg(gid_t) rgid;
    437  1.33.4.1   mycroft 		syscallarg(gid_t) egid;
    438  1.33.4.1   mycroft 	} */ *uap = v;
    439  1.33.4.1   mycroft 	register struct pcred *pc = p->p_cred;
    440  1.33.4.1   mycroft 	register gid_t rgid, egid;
    441  1.33.4.1   mycroft 	int error;
    442  1.33.4.1   mycroft 
    443  1.33.4.1   mycroft 	rgid = SCARG(uap, rgid);
    444  1.33.4.1   mycroft 	egid = SCARG(uap, egid);
    445  1.33.4.1   mycroft 
    446  1.33.4.1   mycroft 	if (rgid != (gid_t)-1 &&
    447  1.33.4.1   mycroft 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    448  1.33.4.1   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    449  1.33.4.1   mycroft 		return (error);
    450  1.33.4.1   mycroft 
    451  1.33.4.1   mycroft 	if (egid != (gid_t)-1 &&
    452  1.33.4.1   mycroft 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    453  1.33.4.1   mycroft 	    egid != pc->p_svgid &&
    454  1.33.4.1   mycroft 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    455  1.33.4.1   mycroft 		return (error);
    456  1.33.4.1   mycroft 
    457  1.33.4.1   mycroft 	if (egid != (gid_t)-1) {
    458  1.33.4.1   mycroft 		pc->pc_ucred = crcopy(pc->pc_ucred);
    459  1.33.4.1   mycroft 		pc->pc_ucred->cr_gid = egid;
    460  1.33.4.1   mycroft 	}
    461  1.33.4.1   mycroft 
    462  1.33.4.1   mycroft 	if (rgid != (gid_t)-1) {
    463  1.33.4.1   mycroft 		pc->p_rgid = rgid;
    464  1.33.4.1   mycroft 		pc->p_svgid = pc->pc_ucred->cr_gid;
    465  1.33.4.1   mycroft 	}
    466  1.33.4.1   mycroft 
    467  1.33.4.1   mycroft 	if (egid != (gid_t)-1 && rgid != (gid_t)-1)
    468  1.33.4.1   mycroft 		p->p_flag |= P_SUGID;
    469      1.14       cgd 	return (0);
    470      1.14       cgd }
    471      1.14       cgd 
    472      1.15       cgd /* ARGSUSED */
    473      1.32  christos int
    474      1.30   mycroft sys_setgroups(p, v, retval)
    475      1.15       cgd 	struct proc *p;
    476      1.29   thorpej 	void *v;
    477      1.29   thorpej 	register_t *retval;
    478      1.29   thorpej {
    479      1.30   mycroft 	struct sys_setgroups_args /* {
    480      1.19       cgd 		syscallarg(u_int) gidsetsize;
    481      1.19       cgd 		syscallarg(gid_t *) gidset;
    482      1.29   thorpej 	} */ *uap = v;
    483      1.15       cgd 	register struct pcred *pc = p->p_cred;
    484      1.15       cgd 	register u_int ngrp;
    485      1.15       cgd 	int error;
    486      1.15       cgd 
    487      1.32  christos 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    488      1.15       cgd 		return (error);
    489      1.19       cgd 	ngrp = SCARG(uap, gidsetsize);
    490      1.27       jtc 	if (ngrp > NGROUPS)
    491      1.15       cgd 		return (EINVAL);
    492      1.15       cgd 	pc->pc_ucred = crcopy(pc->pc_ucred);
    493      1.32  christos 	error = copyin((caddr_t)SCARG(uap, gidset),
    494      1.32  christos 		       (caddr_t)pc->pc_ucred->cr_groups, ngrp * sizeof(gid_t));
    495      1.32  christos 	if (error)
    496      1.15       cgd 		return (error);
    497      1.15       cgd 	pc->pc_ucred->cr_ngroups = ngrp;
    498      1.15       cgd 	p->p_flag |= P_SUGID;
    499      1.15       cgd 	return (0);
    500      1.15       cgd }
    501      1.14       cgd 
    502      1.14       cgd /*
    503      1.14       cgd  * Check if gid is a member of the group set.
    504      1.14       cgd  */
    505      1.32  christos int
    506      1.14       cgd groupmember(gid, cred)
    507      1.14       cgd 	gid_t gid;
    508      1.14       cgd 	register struct ucred *cred;
    509      1.14       cgd {
    510      1.14       cgd 	register gid_t *gp;
    511      1.14       cgd 	gid_t *egp;
    512      1.14       cgd 
    513      1.14       cgd 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    514      1.14       cgd 	for (gp = cred->cr_groups; gp < egp; gp++)
    515      1.14       cgd 		if (*gp == gid)
    516      1.14       cgd 			return (1);
    517      1.14       cgd 	return (0);
    518      1.14       cgd }
    519      1.14       cgd 
    520      1.14       cgd /*
    521      1.14       cgd  * Test whether the specified credentials imply "super-user"
    522      1.14       cgd  * privilege; if so, and we have accounting info, set the flag
    523      1.14       cgd  * indicating use of super-powers.
    524      1.14       cgd  * Returns 0 or error.
    525      1.14       cgd  */
    526      1.32  christos int
    527      1.14       cgd suser(cred, acflag)
    528      1.14       cgd 	struct ucred *cred;
    529      1.14       cgd 	u_short *acflag;
    530      1.14       cgd {
    531      1.14       cgd 	if (cred->cr_uid == 0) {
    532      1.14       cgd 		if (acflag)
    533      1.14       cgd 			*acflag |= ASU;
    534      1.14       cgd 		return (0);
    535      1.14       cgd 	}
    536      1.14       cgd 	return (EPERM);
    537      1.14       cgd }
    538      1.14       cgd 
    539      1.14       cgd /*
    540      1.14       cgd  * Allocate a zeroed cred structure.
    541      1.14       cgd  */
    542      1.14       cgd struct ucred *
    543      1.14       cgd crget()
    544      1.14       cgd {
    545      1.14       cgd 	register struct ucred *cr;
    546      1.14       cgd 
    547      1.14       cgd 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    548      1.14       cgd 	bzero((caddr_t)cr, sizeof(*cr));
    549      1.14       cgd 	cr->cr_ref = 1;
    550      1.14       cgd 	return (cr);
    551      1.14       cgd }
    552      1.14       cgd 
    553      1.14       cgd /*
    554      1.14       cgd  * Free a cred structure.
    555      1.14       cgd  * Throws away space when ref count gets to 0.
    556      1.14       cgd  */
    557      1.22       cgd void
    558      1.14       cgd crfree(cr)
    559      1.14       cgd 	struct ucred *cr;
    560      1.14       cgd {
    561      1.15       cgd 	int s;
    562      1.14       cgd 
    563      1.15       cgd 	s = splimp();				/* ??? */
    564      1.15       cgd 	if (--cr->cr_ref == 0)
    565      1.15       cgd 		FREE((caddr_t)cr, M_CRED);
    566      1.14       cgd 	(void) splx(s);
    567      1.14       cgd }
    568      1.14       cgd 
    569      1.14       cgd /*
    570      1.14       cgd  * Copy cred structure to a new one and free the old one.
    571      1.14       cgd  */
    572      1.14       cgd struct ucred *
    573      1.14       cgd crcopy(cr)
    574      1.14       cgd 	struct ucred *cr;
    575      1.14       cgd {
    576      1.14       cgd 	struct ucred *newcr;
    577      1.14       cgd 
    578      1.14       cgd 	if (cr->cr_ref == 1)
    579      1.14       cgd 		return (cr);
    580      1.14       cgd 	newcr = crget();
    581      1.14       cgd 	*newcr = *cr;
    582      1.14       cgd 	crfree(cr);
    583      1.14       cgd 	newcr->cr_ref = 1;
    584      1.14       cgd 	return (newcr);
    585      1.14       cgd }
    586      1.14       cgd 
    587      1.14       cgd /*
    588      1.14       cgd  * Dup cred struct to a new held one.
    589      1.14       cgd  */
    590      1.14       cgd struct ucred *
    591      1.14       cgd crdup(cr)
    592      1.14       cgd 	struct ucred *cr;
    593      1.14       cgd {
    594      1.14       cgd 	struct ucred *newcr;
    595      1.14       cgd 
    596      1.14       cgd 	newcr = crget();
    597      1.14       cgd 	*newcr = *cr;
    598      1.14       cgd 	newcr->cr_ref = 1;
    599      1.14       cgd 	return (newcr);
    600      1.14       cgd }
    601      1.14       cgd 
    602      1.14       cgd /*
    603      1.14       cgd  * Get login name, if available.
    604      1.14       cgd  */
    605      1.14       cgd /* ARGSUSED */
    606      1.32  christos int
    607      1.30   mycroft sys_getlogin(p, v, retval)
    608      1.14       cgd 	struct proc *p;
    609      1.29   thorpej 	void *v;
    610      1.29   thorpej 	register_t *retval;
    611      1.29   thorpej {
    612      1.30   mycroft 	struct sys_getlogin_args /* {
    613      1.19       cgd 		syscallarg(char *) namebuf;
    614      1.19       cgd 		syscallarg(u_int) namelen;
    615      1.29   thorpej 	} */ *uap = v;
    616      1.14       cgd 
    617      1.19       cgd 	if (SCARG(uap, namelen) > sizeof (p->p_pgrp->pg_session->s_login))
    618      1.19       cgd 		SCARG(uap, namelen) = sizeof (p->p_pgrp->pg_session->s_login);
    619      1.14       cgd 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    620      1.19       cgd 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    621      1.14       cgd }
    622      1.14       cgd 
    623      1.14       cgd /*
    624      1.14       cgd  * Set login name.
    625      1.14       cgd  */
    626      1.14       cgd /* ARGSUSED */
    627      1.32  christos int
    628      1.30   mycroft sys_setlogin(p, v, retval)
    629      1.14       cgd 	struct proc *p;
    630      1.29   thorpej 	void *v;
    631      1.29   thorpej 	register_t *retval;
    632      1.29   thorpej {
    633      1.30   mycroft 	struct sys_setlogin_args /* {
    634      1.19       cgd 		syscallarg(char *) namebuf;
    635      1.29   thorpej 	} */ *uap = v;
    636      1.14       cgd 	int error;
    637      1.14       cgd 
    638      1.32  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    639      1.14       cgd 		return (error);
    640      1.19       cgd 	error = copyinstr((caddr_t) SCARG(uap, namebuf),
    641      1.14       cgd 	    (caddr_t) p->p_pgrp->pg_session->s_login,
    642      1.25   mycroft 	    sizeof (p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
    643      1.14       cgd 	if (error == ENAMETOOLONG)
    644      1.14       cgd 		error = EINVAL;
    645      1.14       cgd 	return (error);
    646      1.14       cgd }
    647