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