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