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