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