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