Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.63.2.5
      1 /*	$NetBSD: kern_prot.c,v 1.63.2.5 2002/02/28 19:59:35 nathanw 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.63.2.5 2002/02/28 19:59:35 nathanw 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/lwp.h>
     57 #include <sys/proc.h>
     58 #include <sys/timeb.h>
     59 #include <sys/times.h>
     60 #include <sys/malloc.h>
     61 
     62 #include <sys/mount.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 	int ngrp;
    266 	int error;
    267 
    268 	ngrp = SCARG(uap, gidsetsize);
    269 	if (ngrp == 0) {
    270 		*retval = pc->pc_ucred->cr_ngroups;
    271 		return (0);
    272 	}
    273 	if (ngrp < pc->pc_ucred->cr_ngroups)
    274 		return (EINVAL);
    275 	ngrp = pc->pc_ucred->cr_ngroups;
    276 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    277 			(caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
    278 	if (error)
    279 		return (error);
    280 	*retval = ngrp;
    281 	return (0);
    282 }
    283 
    284 /* ARGSUSED */
    285 int
    286 sys_setsid(l, v, retval)
    287 	struct lwp *l;
    288 	void *v;
    289 	register_t *retval;
    290 {
    291 	struct proc *p = l->l_proc;
    292 
    293 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    294 		return (EPERM);
    295 	} else {
    296 		(void)enterpgrp(p, p->p_pid, 1);
    297 		*retval = p->p_pid;
    298 		return (0);
    299 	}
    300 }
    301 
    302 /*
    303  * set process group (setpgid/old setpgrp)
    304  *
    305  * caller does setpgid(targpid, targpgid)
    306  *
    307  * pgid must be in valid range (EINVAL)
    308  * pid must be caller or child of caller (ESRCH)
    309  * if a child
    310  *	pid must be in same session (EPERM)
    311  *	pid can't have done an exec (EACCES)
    312  * if pgid != pid
    313  * 	there must exist some pid in same session having pgid (EPERM)
    314  * pid must not be session leader (EPERM)
    315  */
    316 /* ARGSUSED */
    317 int
    318 sys_setpgid(l, v, retval)
    319 	struct lwp *l;
    320 	void *v;
    321 	register_t *retval;
    322 {
    323 	struct sys_setpgid_args /* {
    324 		syscallarg(int) pid;
    325 		syscallarg(int) pgid;
    326 	} */ *uap = v;
    327 	struct proc *curp = l->l_proc;
    328 	struct proc *targp;			/* target process */
    329 	struct pgrp *pgrp;			/* target pgrp */
    330 
    331 	if (SCARG(uap, pgid) < 0)
    332 		return (EINVAL);
    333 
    334 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
    335 		if ((targp = pfind(SCARG(uap, pid))) == 0
    336 		    || !inferior(targp, curp))
    337 			return (ESRCH);
    338 		if (targp->p_session != curp->p_session)
    339 			return (EPERM);
    340 		if (targp->p_flag & P_EXEC)
    341 			return (EACCES);
    342 	} else
    343 		targp = curp;
    344 	if (SESS_LEADER(targp))
    345 		return (EPERM);
    346 	if (SCARG(uap, pgid) == 0)
    347 		SCARG(uap, pgid) = targp->p_pid;
    348 	else if (SCARG(uap, pgid) != targp->p_pid)
    349 		if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
    350 	            pgrp->pg_session != curp->p_session)
    351 			return (EPERM);
    352 	return (enterpgrp(targp, SCARG(uap, pgid), 0));
    353 }
    354 
    355 /* ARGSUSED */
    356 int
    357 sys_setuid(l, v, retval)
    358 	struct lwp *l;
    359 	void *v;
    360 	register_t *retval;
    361 {
    362 	struct sys_setuid_args /* {
    363 		syscallarg(uid_t) uid;
    364 	} */ *uap = v;
    365 	struct proc *p = l->l_proc;
    366 	struct pcred *pc = p->p_cred;
    367 	uid_t uid;
    368 	int error;
    369 
    370 	uid = SCARG(uap, uid);
    371 	if (uid != pc->p_ruid &&
    372 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    373 		return (error);
    374 	/*
    375 	 * Check if we are all set, and this is a no-op.
    376 	 */
    377 	if (pc->p_ruid == uid && pc->p_svuid == uid &&
    378 	    pc->pc_ucred->cr_uid == uid)
    379 		return (0);
    380 	/*
    381 	 * Everything's okay, do it.
    382 	 * Transfer proc count to new user.
    383 	 * Copy credentials so other references do not see our changes.
    384 	 */
    385 	(void)chgproccnt(pc->p_ruid, -1);
    386 	(void)chgproccnt(uid, 1);
    387 	pc->pc_ucred = crcopy(pc->pc_ucred);
    388 	pc->pc_ucred->cr_uid = uid;
    389 	pc->p_ruid = uid;
    390 	pc->p_svuid = uid;
    391 	p_sugid(p);
    392 	return (0);
    393 }
    394 
    395 /* ARGSUSED */
    396 int
    397 sys_seteuid(l, v, retval)
    398 	struct lwp *l;
    399 	void *v;
    400 	register_t *retval;
    401 {
    402 	struct sys_seteuid_args /* {
    403 		syscallarg(uid_t) euid;
    404 	} */ *uap = v;
    405 	struct proc *p = l->l_proc;
    406 	struct pcred *pc = p->p_cred;
    407 	uid_t euid;
    408 	int error;
    409 
    410 	euid = SCARG(uap, euid);
    411 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    412 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    413 		return (error);
    414 	/*
    415 	 * Check if we are all set, and this is a no-op.
    416 	 */
    417 	if (pc->pc_ucred->cr_uid == euid)
    418 		return (0);
    419 
    420 	/*
    421 	 * Everything's okay, do it.  Copy credentials so other references do
    422 	 * not see our changes.
    423 	 */
    424 	pc->pc_ucred = crcopy(pc->pc_ucred);
    425 	pc->pc_ucred->cr_uid = euid;
    426 	p_sugid(p);
    427 	return (0);
    428 }
    429 
    430 int
    431 sys_setreuid(l, v, retval)
    432 	struct lwp *l;
    433 	void *v;
    434 	register_t *retval;
    435 {
    436 	struct sys_setreuid_args /* {
    437 		syscallarg(uid_t) ruid;
    438 		syscallarg(uid_t) euid;
    439 	} */ *uap = v;
    440 	struct proc *p = l->l_proc;
    441 	struct pcred *pc = p->p_cred;
    442 	uid_t ruid, euid;
    443 	int error, changed = 0;
    444 
    445 	ruid = SCARG(uap, ruid);
    446 	euid = SCARG(uap, euid);
    447 
    448 	if (ruid != (uid_t)-1 &&
    449 	    ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
    450 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    451 		return (error);
    452 
    453 	if (euid != (uid_t)-1 &&
    454 	    euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
    455 	    euid != pc->p_svuid &&
    456 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    457 		return (error);
    458 
    459 	if (euid != (uid_t)-1 && euid != pc->pc_ucred->cr_uid) {
    460 		pc->pc_ucred = crcopy(pc->pc_ucred);
    461 		pc->pc_ucred->cr_uid = euid;
    462 		changed++;
    463 	}
    464 
    465 	if (ruid != (uid_t)-1 &&
    466 	    (pc->p_ruid != ruid || pc->p_svuid != pc->pc_ucred->cr_uid)) {
    467 		(void)chgproccnt(pc->p_ruid, -1);
    468 		(void)chgproccnt(ruid, 1);
    469 		pc->p_ruid = ruid;
    470 		pc->p_svuid = pc->pc_ucred->cr_uid;
    471 		changed++;
    472 	}
    473 
    474 	if (changed)
    475 		p_sugid(p);
    476 	return (0);
    477 }
    478 
    479 /* ARGSUSED */
    480 int
    481 sys_setgid(l, v, retval)
    482 	struct lwp *l;
    483 	void *v;
    484 	register_t *retval;
    485 {
    486 	struct sys_setgid_args /* {
    487 		syscallarg(gid_t) gid;
    488 	} */ *uap = v;
    489 	struct proc *p = l->l_proc;
    490 	struct pcred *pc = p->p_cred;
    491 	gid_t gid;
    492 	int error;
    493 
    494 	gid = SCARG(uap, gid);
    495 	if (gid != pc->p_rgid &&
    496 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    497 		return (error);
    498 	/*
    499 	 * Check if we are all set, and this is a no-op.
    500 	 */
    501 	if (pc->pc_ucred->cr_gid == gid && pc->p_rgid == gid &&
    502 	    pc->p_svgid == gid)
    503 		return (0);
    504 
    505 	pc->pc_ucred = crcopy(pc->pc_ucred);
    506 	pc->pc_ucred->cr_gid = gid;
    507 	pc->p_rgid = gid;
    508 	pc->p_svgid = gid;
    509 	p_sugid(p);
    510 	return (0);
    511 }
    512 
    513 /* ARGSUSED */
    514 int
    515 sys_setegid(l, v, retval)
    516 	struct lwp *l;
    517 	void *v;
    518 	register_t *retval;
    519 {
    520 	struct sys_setegid_args /* {
    521 		syscallarg(gid_t) egid;
    522 	} */ *uap = v;
    523 	struct proc *p = l->l_proc;
    524 	struct pcred *pc = p->p_cred;
    525 	gid_t egid;
    526 	int error;
    527 
    528 	egid = SCARG(uap, egid);
    529 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    530 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    531 		return (error);
    532 	/*
    533 	 * Check if we are all set, and this is a no-op.
    534 	 */
    535 	if (pc->pc_ucred->cr_gid == egid)
    536 		return (0);
    537 
    538 	pc->pc_ucred = crcopy(pc->pc_ucred);
    539 	pc->pc_ucred->cr_gid = egid;
    540 	p_sugid(p);
    541 	return (0);
    542 }
    543 
    544 int
    545 sys_setregid(l, v, retval)
    546 	struct lwp *l;
    547 	void *v;
    548 	register_t *retval;
    549 {
    550 	struct sys_setregid_args /* {
    551 		syscallarg(gid_t) rgid;
    552 		syscallarg(gid_t) egid;
    553 	} */ *uap = v;
    554 	struct proc *p = l->l_proc;
    555 	struct pcred *pc = p->p_cred;
    556 	gid_t rgid, egid;
    557 	int error, changed = 0;
    558 
    559 	rgid = SCARG(uap, rgid);
    560 	egid = SCARG(uap, egid);
    561 
    562 	if (rgid != (gid_t)-1 &&
    563 	    rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
    564 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    565 		return (error);
    566 
    567 	if (egid != (gid_t)-1 &&
    568 	    egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
    569 	    egid != pc->p_svgid &&
    570 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    571 		return (error);
    572 
    573 	if (egid != (gid_t)-1 && pc->pc_ucred->cr_gid != egid) {
    574 		pc->pc_ucred = crcopy(pc->pc_ucred);
    575 		pc->pc_ucred->cr_gid = egid;
    576 		changed++;
    577 	}
    578 
    579 	if (rgid != (gid_t)-1 &&
    580 	    (pc->p_rgid != rgid || pc->p_svgid != pc->pc_ucred->cr_gid)) {
    581 		pc->p_rgid = rgid;
    582 		pc->p_svgid = pc->pc_ucred->cr_gid;
    583 		changed++;
    584 	}
    585 
    586 	if (changed)
    587 		p_sugid(p);
    588 	return (0);
    589 }
    590 
    591 int
    592 sys_issetugid(l, v, retval)
    593 	struct lwp *l;
    594 	void *v;
    595 	register_t *retval;
    596 {
    597 	struct proc *p = l->l_proc;
    598 	/*
    599 	 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
    600 	 * we use P_SUGID because we consider changing the owners as
    601 	 * "tainting" as well.
    602 	 * This is significant for procs that start as root and "become"
    603 	 * a user without an exec - programs cannot know *everything*
    604 	 * that libc *might* have put in their data segment.
    605 	 */
    606 	*retval = (p->p_flag & P_SUGID) != 0;
    607 	return (0);
    608 }
    609 
    610 /* ARGSUSED */
    611 int
    612 sys_setgroups(l, v, retval)
    613 	struct lwp *l;
    614 	void *v;
    615 	register_t *retval;
    616 {
    617 	struct sys_setgroups_args /* {
    618 		syscallarg(int) gidsetsize;
    619 		syscallarg(const gid_t *) gidset;
    620 	} */ *uap = v;
    621 	struct proc *p = l->l_proc;
    622 	struct pcred *pc = p->p_cred;
    623 	int ngrp;
    624 	int error;
    625 	gid_t grp[NGROUPS];
    626 	size_t grsize;
    627 
    628 	if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
    629 		return (error);
    630 
    631 	ngrp = SCARG(uap, gidsetsize);
    632 	if ((u_int)ngrp > NGROUPS)
    633 		return (EINVAL);
    634 
    635 	grsize = ngrp * sizeof(gid_t);
    636 	error = copyin(SCARG(uap, gidset), grp, grsize);
    637 	if (error)
    638 		return (error);
    639 	/*
    640 	 * Check if this is a no-op.
    641 	 */
    642 	if (pc->pc_ucred->cr_ngroups == ngrp &&
    643 	    memcmp(grp, pc->pc_ucred->cr_groups, grsize) == 0)
    644 		return (0);
    645 
    646 	pc->pc_ucred = crcopy(pc->pc_ucred);
    647 	(void)memcpy(pc->pc_ucred->cr_groups, grp, grsize);
    648 	pc->pc_ucred->cr_ngroups = ngrp;
    649 	p_sugid(p);
    650 	return (0);
    651 }
    652 
    653 /*
    654  * Check if gid is a member of the group set.
    655  */
    656 int
    657 groupmember(gid, cred)
    658 	gid_t gid;
    659 	struct ucred *cred;
    660 {
    661 	gid_t *gp;
    662 	gid_t *egp;
    663 
    664 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    665 	for (gp = cred->cr_groups; gp < egp; gp++)
    666 		if (*gp == gid)
    667 			return (1);
    668 	return (0);
    669 }
    670 
    671 /*
    672  * Test whether the specified credentials imply "super-user"
    673  * privilege; if so, and we have accounting info, set the flag
    674  * indicating use of super-powers.
    675  * Returns 0 or error.
    676  */
    677 int
    678 suser(cred, acflag)
    679 	struct ucred *cred;
    680 	u_short *acflag;
    681 {
    682 	if (cred->cr_uid == 0) {
    683 		if (acflag)
    684 			*acflag |= ASU;
    685 		return (0);
    686 	}
    687 	return (EPERM);
    688 }
    689 
    690 /*
    691  * Allocate a zeroed cred structure.
    692  */
    693 struct ucred *
    694 crget()
    695 {
    696 	struct ucred *cr;
    697 
    698 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    699 	memset((caddr_t)cr, 0, sizeof(*cr));
    700 	cr->cr_ref = 1;
    701 	return (cr);
    702 }
    703 
    704 /*
    705  * Free a cred structure.
    706  * Throws away space when ref count gets to 0.
    707  */
    708 void
    709 crfree(cr)
    710 	struct ucred *cr;
    711 {
    712 
    713 	if (--cr->cr_ref == 0)
    714 		FREE((caddr_t)cr, M_CRED);
    715 }
    716 
    717 /*
    718  * Copy cred structure to a new one and free the old one.
    719  */
    720 struct ucred *
    721 crcopy(cr)
    722 	struct ucred *cr;
    723 {
    724 	struct ucred *newcr;
    725 
    726 	if (cr->cr_ref == 1)
    727 		return (cr);
    728 	newcr = crget();
    729 	*newcr = *cr;
    730 	crfree(cr);
    731 	newcr->cr_ref = 1;
    732 	return (newcr);
    733 }
    734 
    735 /*
    736  * Dup cred struct to a new held one.
    737  */
    738 struct ucred *
    739 crdup(cr)
    740 	struct ucred *cr;
    741 {
    742 	struct ucred *newcr;
    743 
    744 	newcr = crget();
    745 	*newcr = *cr;
    746 	newcr->cr_ref = 1;
    747 	return (newcr);
    748 }
    749 
    750 /*
    751  * convert from userland credentials to kernel one
    752  */
    753 void
    754 crcvt(uc, uuc)
    755 	struct ucred *uc;
    756 	const struct uucred *uuc;
    757 {
    758 	uc->cr_ref = 0;
    759 	uc->cr_uid = uuc->cr_uid;
    760 	uc->cr_gid = uuc->cr_gid;
    761 	uc->cr_ngroups = uuc->cr_ngroups;
    762 	(void)memcpy(uc->cr_groups, uuc->cr_groups, sizeof(uuc->cr_groups));
    763 }
    764 
    765 /*
    766  * Get login name, if available.
    767  */
    768 /* ARGSUSED */
    769 int
    770 sys___getlogin(l, v, retval)
    771 	struct lwp *l;
    772 	void *v;
    773 	register_t *retval;
    774 {
    775 	struct sys___getlogin_args /* {
    776 		syscallarg(char *) namebuf;
    777 		syscallarg(size_t) namelen;
    778 	} */ *uap = v;
    779 	struct proc *p = l->l_proc;
    780 
    781 	if (SCARG(uap, namelen) > sizeof(p->p_pgrp->pg_session->s_login))
    782 		SCARG(uap, namelen) = sizeof(p->p_pgrp->pg_session->s_login);
    783 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    784 	    (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
    785 }
    786 
    787 /*
    788  * Set login name.
    789  */
    790 /* ARGSUSED */
    791 int
    792 sys___setlogin(l, v, retval)
    793 	struct lwp *l;
    794 	void *v;
    795 	register_t *retval;
    796 {
    797 	struct sys___setlogin_args /* {
    798 		syscallarg(const char *) namebuf;
    799 	} */ *uap = v;
    800 	struct proc *p = l->l_proc;
    801 	int error;
    802 
    803 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    804 		return (error);
    805 	error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
    806 	    sizeof(p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
    807 	if (error == ENAMETOOLONG)
    808 		error = EINVAL;
    809 	return (error);
    810 }
    811