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