kern_prot.c revision 1.43 1 1.43 thorpej /* $NetBSD: kern_prot.c,v 1.43 1998/02/14 01:17:51 thorpej Exp $ */
2 1.16 cgd
3 1.14 cgd /*
4 1.15 cgd * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
5 1.15 cgd * The Regents of the University of California. All rights reserved.
6 1.14 cgd * (c) UNIX System Laboratories, Inc.
7 1.14 cgd * All or some portions of this file are derived from material licensed
8 1.14 cgd * to the University of California by American Telephone and Telegraph
9 1.14 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.14 cgd * the permission of UNIX System Laboratories, Inc.
11 1.14 cgd *
12 1.14 cgd * Redistribution and use in source and binary forms, with or without
13 1.14 cgd * modification, are permitted provided that the following conditions
14 1.14 cgd * are met:
15 1.14 cgd * 1. Redistributions of source code must retain the above copyright
16 1.14 cgd * notice, this list of conditions and the following disclaimer.
17 1.14 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.14 cgd * notice, this list of conditions and the following disclaimer in the
19 1.14 cgd * documentation and/or other materials provided with the distribution.
20 1.14 cgd * 3. All advertising materials mentioning features or use of this software
21 1.14 cgd * must display the following acknowledgement:
22 1.14 cgd * This product includes software developed by the University of
23 1.14 cgd * California, Berkeley and its contributors.
24 1.14 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.14 cgd * may be used to endorse or promote products derived from this software
26 1.14 cgd * without specific prior written permission.
27 1.14 cgd *
28 1.14 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.14 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.14 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.14 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.14 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.14 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.14 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.14 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.14 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.14 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.14 cgd * SUCH DAMAGE.
39 1.14 cgd *
40 1.16 cgd * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
41 1.14 cgd */
42 1.14 cgd
43 1.14 cgd /*
44 1.14 cgd * System calls related to processes and protection
45 1.14 cgd */
46 1.14 cgd
47 1.14 cgd #include <sys/param.h>
48 1.14 cgd #include <sys/acct.h>
49 1.14 cgd #include <sys/systm.h>
50 1.14 cgd #include <sys/ucred.h>
51 1.14 cgd #include <sys/proc.h>
52 1.14 cgd #include <sys/timeb.h>
53 1.14 cgd #include <sys/times.h>
54 1.14 cgd #include <sys/malloc.h>
55 1.14 cgd
56 1.19 cgd #include <sys/mount.h>
57 1.19 cgd #include <sys/syscallargs.h>
58 1.32 christos
59 1.14 cgd /* ARGSUSED */
60 1.32 christos int
61 1.30 mycroft sys_getpid(p, v, retval)
62 1.14 cgd struct proc *p;
63 1.30 mycroft void *v;
64 1.19 cgd register_t *retval;
65 1.14 cgd {
66 1.14 cgd
67 1.14 cgd *retval = p->p_pid;
68 1.31 mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
69 1.31 mycroft defined(COMPAT_FREEBSD)
70 1.14 cgd retval[1] = p->p_pptr->p_pid;
71 1.14 cgd #endif
72 1.14 cgd return (0);
73 1.14 cgd }
74 1.14 cgd
75 1.14 cgd /* ARGSUSED */
76 1.32 christos int
77 1.30 mycroft sys_getppid(p, v, retval)
78 1.14 cgd struct proc *p;
79 1.30 mycroft void *v;
80 1.19 cgd register_t *retval;
81 1.14 cgd {
82 1.14 cgd
83 1.14 cgd *retval = p->p_pptr->p_pid;
84 1.14 cgd return (0);
85 1.14 cgd }
86 1.14 cgd
87 1.14 cgd /* Get process group ID; note that POSIX getpgrp takes no parameter */
88 1.32 christos int
89 1.30 mycroft sys_getpgrp(p, v, retval)
90 1.14 cgd struct proc *p;
91 1.30 mycroft void *v;
92 1.19 cgd register_t *retval;
93 1.14 cgd {
94 1.14 cgd
95 1.14 cgd *retval = p->p_pgrp->pg_id;
96 1.14 cgd return (0);
97 1.43 thorpej }
98 1.43 thorpej
99 1.43 thorpej /*
100 1.43 thorpej * Return the process group ID of the session leader (session ID)
101 1.43 thorpej * for the specified process.
102 1.43 thorpej */
103 1.43 thorpej int
104 1.43 thorpej sys_getsid(p, v, retval)
105 1.43 thorpej struct proc *p;
106 1.43 thorpej void *v;
107 1.43 thorpej register_t *retval;
108 1.43 thorpej {
109 1.43 thorpej struct sys_getsid_args /* {
110 1.43 thorpej syscalldarg(pid_t) pid;
111 1.43 thorpej } */ *uap = v;
112 1.43 thorpej
113 1.43 thorpej if (SCARG(uap, pid) == 0)
114 1.43 thorpej goto found;
115 1.43 thorpej if ((p = pfind(SCARG(uap, pid))) == 0)
116 1.43 thorpej return (ESRCH);
117 1.43 thorpej found:
118 1.43 thorpej *retval = p->p_session->s_sid;
119 1.43 thorpej return 0;
120 1.36 mrg }
121 1.36 mrg
122 1.36 mrg int
123 1.36 mrg sys_getpgid(p, v, retval)
124 1.36 mrg struct proc *p;
125 1.36 mrg void *v;
126 1.36 mrg register_t *retval;
127 1.36 mrg {
128 1.36 mrg register struct sys_getpgid_args /* {
129 1.36 mrg syscallarg(pid_t) pid;
130 1.36 mrg } */ *uap = v;
131 1.36 mrg
132 1.36 mrg if (SCARG(uap, pid) == 0)
133 1.36 mrg goto found;
134 1.36 mrg if ((p = pfind(SCARG(uap, pid))) == 0)
135 1.36 mrg return (ESRCH);
136 1.36 mrg found:
137 1.36 mrg *retval = p->p_pgid;
138 1.36 mrg return 0;
139 1.14 cgd }
140 1.14 cgd
141 1.14 cgd /* ARGSUSED */
142 1.32 christos int
143 1.30 mycroft sys_getuid(p, v, retval)
144 1.14 cgd struct proc *p;
145 1.30 mycroft void *v;
146 1.19 cgd register_t *retval;
147 1.14 cgd {
148 1.14 cgd
149 1.14 cgd *retval = p->p_cred->p_ruid;
150 1.31 mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_IBCS2) || \
151 1.31 mycroft defined(COMPAT_FREEBSD)
152 1.14 cgd retval[1] = p->p_ucred->cr_uid;
153 1.14 cgd #endif
154 1.14 cgd return (0);
155 1.14 cgd }
156 1.14 cgd
157 1.14 cgd /* ARGSUSED */
158 1.32 christos int
159 1.30 mycroft sys_geteuid(p, v, retval)
160 1.14 cgd struct proc *p;
161 1.30 mycroft void *v;
162 1.19 cgd register_t *retval;
163 1.14 cgd {
164 1.14 cgd
165 1.14 cgd *retval = p->p_ucred->cr_uid;
166 1.14 cgd return (0);
167 1.14 cgd }
168 1.14 cgd
169 1.14 cgd /* ARGSUSED */
170 1.32 christos int
171 1.30 mycroft sys_getgid(p, v, retval)
172 1.14 cgd struct proc *p;
173 1.30 mycroft void *v;
174 1.19 cgd register_t *retval;
175 1.14 cgd {
176 1.14 cgd
177 1.14 cgd *retval = p->p_cred->p_rgid;
178 1.31 mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_FREEBSD)
179 1.27 jtc retval[1] = p->p_ucred->cr_gid;
180 1.14 cgd #endif
181 1.14 cgd return (0);
182 1.14 cgd }
183 1.14 cgd
184 1.14 cgd /*
185 1.14 cgd * Get effective group ID. The "egid" is groups[0], and could be obtained
186 1.14 cgd * via getgroups. This syscall exists because it is somewhat painful to do
187 1.14 cgd * correctly in a library function.
188 1.14 cgd */
189 1.14 cgd /* ARGSUSED */
190 1.32 christos int
191 1.30 mycroft sys_getegid(p, v, retval)
192 1.14 cgd struct proc *p;
193 1.30 mycroft void *v;
194 1.19 cgd register_t *retval;
195 1.14 cgd {
196 1.14 cgd
197 1.27 jtc *retval = p->p_ucred->cr_gid;
198 1.14 cgd return (0);
199 1.14 cgd }
200 1.14 cgd
201 1.32 christos int
202 1.30 mycroft sys_getgroups(p, v, retval)
203 1.14 cgd struct proc *p;
204 1.29 thorpej void *v;
205 1.29 thorpej register_t *retval;
206 1.29 thorpej {
207 1.30 mycroft register struct sys_getgroups_args /* {
208 1.41 thorpej syscallarg(int) gidsetsize;
209 1.19 cgd syscallarg(gid_t *) gidset;
210 1.29 thorpej } */ *uap = v;
211 1.14 cgd register struct pcred *pc = p->p_cred;
212 1.42 mycroft register int ngrp;
213 1.14 cgd int error;
214 1.14 cgd
215 1.42 mycroft ngrp = SCARG(uap, gidsetsize);
216 1.42 mycroft if (ngrp == 0) {
217 1.14 cgd *retval = pc->pc_ucred->cr_ngroups;
218 1.14 cgd return (0);
219 1.14 cgd }
220 1.14 cgd if (ngrp < pc->pc_ucred->cr_ngroups)
221 1.14 cgd return (EINVAL);
222 1.14 cgd ngrp = pc->pc_ucred->cr_ngroups;
223 1.32 christos error = copyout((caddr_t)pc->pc_ucred->cr_groups,
224 1.32 christos (caddr_t)SCARG(uap, gidset), ngrp * sizeof(gid_t));
225 1.32 christos if (error)
226 1.14 cgd return (error);
227 1.14 cgd *retval = ngrp;
228 1.14 cgd return (0);
229 1.14 cgd }
230 1.14 cgd
231 1.14 cgd /* ARGSUSED */
232 1.32 christos int
233 1.30 mycroft sys_setsid(p, v, retval)
234 1.14 cgd register struct proc *p;
235 1.30 mycroft void *v;
236 1.19 cgd register_t *retval;
237 1.14 cgd {
238 1.14 cgd
239 1.14 cgd if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
240 1.14 cgd return (EPERM);
241 1.14 cgd } else {
242 1.15 cgd (void)enterpgrp(p, p->p_pid, 1);
243 1.14 cgd *retval = p->p_pid;
244 1.14 cgd return (0);
245 1.14 cgd }
246 1.14 cgd }
247 1.14 cgd
248 1.14 cgd /*
249 1.14 cgd * set process group (setpgid/old setpgrp)
250 1.14 cgd *
251 1.14 cgd * caller does setpgid(targpid, targpgid)
252 1.14 cgd *
253 1.39 mikel * pgid must be in valid range (EINVAL)
254 1.14 cgd * pid must be caller or child of caller (ESRCH)
255 1.14 cgd * if a child
256 1.14 cgd * pid must be in same session (EPERM)
257 1.14 cgd * pid can't have done an exec (EACCES)
258 1.14 cgd * if pgid != pid
259 1.14 cgd * there must exist some pid in same session having pgid (EPERM)
260 1.14 cgd * pid must not be session leader (EPERM)
261 1.14 cgd */
262 1.14 cgd /* ARGSUSED */
263 1.32 christos int
264 1.30 mycroft sys_setpgid(curp, v, retval)
265 1.14 cgd struct proc *curp;
266 1.29 thorpej void *v;
267 1.29 thorpej register_t *retval;
268 1.29 thorpej {
269 1.30 mycroft register struct sys_setpgid_args /* {
270 1.19 cgd syscallarg(int) pid;
271 1.19 cgd syscallarg(int) pgid;
272 1.29 thorpej } */ *uap = v;
273 1.14 cgd register struct proc *targp; /* target process */
274 1.14 cgd register struct pgrp *pgrp; /* target pgrp */
275 1.14 cgd
276 1.39 mikel if (SCARG(uap, pgid) < 0)
277 1.39 mikel return (EINVAL);
278 1.14 cgd
279 1.19 cgd if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != curp->p_pid) {
280 1.19 cgd if ((targp = pfind(SCARG(uap, pid))) == 0 || !inferior(targp))
281 1.14 cgd return (ESRCH);
282 1.14 cgd if (targp->p_session != curp->p_session)
283 1.14 cgd return (EPERM);
284 1.15 cgd if (targp->p_flag & P_EXEC)
285 1.14 cgd return (EACCES);
286 1.14 cgd } else
287 1.14 cgd targp = curp;
288 1.14 cgd if (SESS_LEADER(targp))
289 1.14 cgd return (EPERM);
290 1.19 cgd if (SCARG(uap, pgid) == 0)
291 1.19 cgd SCARG(uap, pgid) = targp->p_pid;
292 1.19 cgd else if (SCARG(uap, pgid) != targp->p_pid)
293 1.19 cgd if ((pgrp = pgfind(SCARG(uap, pgid))) == 0 ||
294 1.14 cgd pgrp->pg_session != curp->p_session)
295 1.14 cgd return (EPERM);
296 1.19 cgd return (enterpgrp(targp, SCARG(uap, pgid), 0));
297 1.14 cgd }
298 1.14 cgd
299 1.14 cgd /* ARGSUSED */
300 1.32 christos int
301 1.30 mycroft sys_setuid(p, v, retval)
302 1.14 cgd struct proc *p;
303 1.29 thorpej void *v;
304 1.29 thorpej register_t *retval;
305 1.29 thorpej {
306 1.30 mycroft struct sys_setuid_args /* {
307 1.19 cgd syscallarg(uid_t) uid;
308 1.29 thorpej } */ *uap = v;
309 1.14 cgd register struct pcred *pc = p->p_cred;
310 1.14 cgd register uid_t uid;
311 1.14 cgd int error;
312 1.14 cgd
313 1.19 cgd uid = SCARG(uap, uid);
314 1.14 cgd if (uid != pc->p_ruid &&
315 1.14 cgd (error = suser(pc->pc_ucred, &p->p_acflag)))
316 1.14 cgd return (error);
317 1.14 cgd /*
318 1.15 cgd * Everything's okay, do it.
319 1.15 cgd * Transfer proc count to new user.
320 1.15 cgd * Copy credentials so other references do not see our changes.
321 1.14 cgd */
322 1.15 cgd (void)chgproccnt(pc->p_ruid, -1);
323 1.15 cgd (void)chgproccnt(uid, 1);
324 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
325 1.14 cgd pc->pc_ucred->cr_uid = uid;
326 1.14 cgd pc->p_ruid = uid;
327 1.14 cgd pc->p_svuid = uid;
328 1.14 cgd p->p_flag |= P_SUGID;
329 1.14 cgd return (0);
330 1.14 cgd }
331 1.14 cgd
332 1.14 cgd /* ARGSUSED */
333 1.32 christos int
334 1.30 mycroft sys_seteuid(p, v, retval)
335 1.14 cgd struct proc *p;
336 1.29 thorpej void *v;
337 1.29 thorpej register_t *retval;
338 1.29 thorpej {
339 1.30 mycroft struct sys_seteuid_args /* {
340 1.19 cgd syscallarg(uid_t) euid;
341 1.29 thorpej } */ *uap = v;
342 1.14 cgd register struct pcred *pc = p->p_cred;
343 1.14 cgd register uid_t euid;
344 1.14 cgd int error;
345 1.14 cgd
346 1.19 cgd euid = SCARG(uap, euid);
347 1.14 cgd if (euid != pc->p_ruid && euid != pc->p_svuid &&
348 1.14 cgd (error = suser(pc->pc_ucred, &p->p_acflag)))
349 1.14 cgd return (error);
350 1.14 cgd /*
351 1.14 cgd * Everything's okay, do it. Copy credentials so other references do
352 1.14 cgd * not see our changes.
353 1.14 cgd */
354 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
355 1.14 cgd pc->pc_ucred->cr_uid = euid;
356 1.14 cgd p->p_flag |= P_SUGID;
357 1.14 cgd return (0);
358 1.14 cgd }
359 1.14 cgd
360 1.35 mycroft int
361 1.35 mycroft sys_setreuid(p, v, retval)
362 1.35 mycroft struct proc *p;
363 1.35 mycroft void *v;
364 1.35 mycroft register_t *retval;
365 1.35 mycroft {
366 1.35 mycroft struct sys_setreuid_args /* {
367 1.35 mycroft syscallarg(uid_t) ruid;
368 1.35 mycroft syscallarg(uid_t) euid;
369 1.35 mycroft } */ *uap = v;
370 1.35 mycroft register struct pcred *pc = p->p_cred;
371 1.35 mycroft register uid_t ruid, euid;
372 1.35 mycroft int error;
373 1.35 mycroft
374 1.35 mycroft ruid = SCARG(uap, ruid);
375 1.35 mycroft euid = SCARG(uap, euid);
376 1.35 mycroft
377 1.35 mycroft if (ruid != (uid_t)-1 &&
378 1.35 mycroft ruid != pc->p_ruid && ruid != pc->pc_ucred->cr_uid &&
379 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
380 1.35 mycroft return (error);
381 1.35 mycroft
382 1.35 mycroft if (euid != (uid_t)-1 &&
383 1.35 mycroft euid != pc->p_ruid && euid != pc->pc_ucred->cr_uid &&
384 1.35 mycroft euid != pc->p_svuid &&
385 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
386 1.35 mycroft return (error);
387 1.35 mycroft
388 1.35 mycroft if (euid != (uid_t)-1) {
389 1.35 mycroft pc->pc_ucred = crcopy(pc->pc_ucred);
390 1.35 mycroft pc->pc_ucred->cr_uid = euid;
391 1.35 mycroft }
392 1.35 mycroft
393 1.35 mycroft if (ruid != (uid_t)-1) {
394 1.35 mycroft (void)chgproccnt(pc->p_ruid, -1);
395 1.35 mycroft (void)chgproccnt(ruid, 1);
396 1.35 mycroft pc->p_ruid = ruid;
397 1.35 mycroft pc->p_svuid = pc->pc_ucred->cr_uid;
398 1.35 mycroft }
399 1.35 mycroft
400 1.35 mycroft if (euid != (uid_t)-1 && ruid != (uid_t)-1)
401 1.35 mycroft p->p_flag |= P_SUGID;
402 1.35 mycroft return (0);
403 1.35 mycroft }
404 1.35 mycroft
405 1.14 cgd /* ARGSUSED */
406 1.32 christos int
407 1.30 mycroft sys_setgid(p, v, retval)
408 1.14 cgd struct proc *p;
409 1.29 thorpej void *v;
410 1.29 thorpej register_t *retval;
411 1.29 thorpej {
412 1.30 mycroft struct sys_setgid_args /* {
413 1.19 cgd syscallarg(gid_t) gid;
414 1.29 thorpej } */ *uap = v;
415 1.14 cgd register struct pcred *pc = p->p_cred;
416 1.14 cgd register gid_t gid;
417 1.14 cgd int error;
418 1.14 cgd
419 1.19 cgd gid = SCARG(uap, gid);
420 1.34 mycroft if (gid != pc->p_rgid &&
421 1.34 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
422 1.14 cgd return (error);
423 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
424 1.27 jtc pc->pc_ucred->cr_gid = gid;
425 1.14 cgd pc->p_rgid = gid;
426 1.34 mycroft pc->p_svgid = gid;
427 1.14 cgd p->p_flag |= P_SUGID;
428 1.14 cgd return (0);
429 1.14 cgd }
430 1.14 cgd
431 1.14 cgd /* ARGSUSED */
432 1.32 christos int
433 1.30 mycroft sys_setegid(p, v, retval)
434 1.14 cgd struct proc *p;
435 1.29 thorpej void *v;
436 1.29 thorpej register_t *retval;
437 1.29 thorpej {
438 1.30 mycroft struct sys_setegid_args /* {
439 1.19 cgd syscallarg(gid_t) egid;
440 1.29 thorpej } */ *uap = v;
441 1.14 cgd register struct pcred *pc = p->p_cred;
442 1.14 cgd register gid_t egid;
443 1.14 cgd int error;
444 1.14 cgd
445 1.19 cgd egid = SCARG(uap, egid);
446 1.14 cgd if (egid != pc->p_rgid && egid != pc->p_svgid &&
447 1.14 cgd (error = suser(pc->pc_ucred, &p->p_acflag)))
448 1.14 cgd return (error);
449 1.14 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
450 1.27 jtc pc->pc_ucred->cr_gid = egid;
451 1.14 cgd p->p_flag |= P_SUGID;
452 1.35 mycroft return (0);
453 1.35 mycroft }
454 1.35 mycroft
455 1.35 mycroft int
456 1.35 mycroft sys_setregid(p, v, retval)
457 1.35 mycroft struct proc *p;
458 1.35 mycroft void *v;
459 1.35 mycroft register_t *retval;
460 1.35 mycroft {
461 1.35 mycroft struct sys_setregid_args /* {
462 1.35 mycroft syscallarg(gid_t) rgid;
463 1.35 mycroft syscallarg(gid_t) egid;
464 1.35 mycroft } */ *uap = v;
465 1.35 mycroft register struct pcred *pc = p->p_cred;
466 1.35 mycroft register gid_t rgid, egid;
467 1.35 mycroft int error;
468 1.35 mycroft
469 1.35 mycroft rgid = SCARG(uap, rgid);
470 1.35 mycroft egid = SCARG(uap, egid);
471 1.35 mycroft
472 1.35 mycroft if (rgid != (gid_t)-1 &&
473 1.35 mycroft rgid != pc->p_rgid && rgid != pc->pc_ucred->cr_gid &&
474 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
475 1.35 mycroft return (error);
476 1.35 mycroft
477 1.35 mycroft if (egid != (gid_t)-1 &&
478 1.35 mycroft egid != pc->p_rgid && egid != pc->pc_ucred->cr_gid &&
479 1.35 mycroft egid != pc->p_svgid &&
480 1.35 mycroft (error = suser(pc->pc_ucred, &p->p_acflag)))
481 1.35 mycroft return (error);
482 1.35 mycroft
483 1.35 mycroft if (egid != (gid_t)-1) {
484 1.35 mycroft pc->pc_ucred = crcopy(pc->pc_ucred);
485 1.35 mycroft pc->pc_ucred->cr_gid = egid;
486 1.35 mycroft }
487 1.35 mycroft
488 1.35 mycroft if (rgid != (gid_t)-1) {
489 1.35 mycroft pc->p_rgid = rgid;
490 1.35 mycroft pc->p_svgid = pc->pc_ucred->cr_gid;
491 1.35 mycroft }
492 1.35 mycroft
493 1.35 mycroft if (egid != (gid_t)-1 && rgid != (gid_t)-1)
494 1.35 mycroft p->p_flag |= P_SUGID;
495 1.14 cgd return (0);
496 1.14 cgd }
497 1.14 cgd
498 1.15 cgd /* ARGSUSED */
499 1.32 christos int
500 1.30 mycroft sys_setgroups(p, v, retval)
501 1.15 cgd struct proc *p;
502 1.29 thorpej void *v;
503 1.29 thorpej register_t *retval;
504 1.29 thorpej {
505 1.30 mycroft struct sys_setgroups_args /* {
506 1.41 thorpej syscallarg(int) gidsetsize;
507 1.38 cgd syscallarg(const gid_t *) gidset;
508 1.29 thorpej } */ *uap = v;
509 1.15 cgd register struct pcred *pc = p->p_cred;
510 1.42 mycroft register int ngrp;
511 1.15 cgd int error;
512 1.15 cgd
513 1.32 christos if ((error = suser(pc->pc_ucred, &p->p_acflag)) != 0)
514 1.15 cgd return (error);
515 1.19 cgd ngrp = SCARG(uap, gidsetsize);
516 1.42 mycroft if ((u_int)ngrp > NGROUPS)
517 1.15 cgd return (EINVAL);
518 1.15 cgd pc->pc_ucred = crcopy(pc->pc_ucred);
519 1.38 cgd error = copyin(SCARG(uap, gidset), pc->pc_ucred->cr_groups,
520 1.38 cgd ngrp * sizeof(gid_t));
521 1.32 christos if (error)
522 1.15 cgd return (error);
523 1.15 cgd pc->pc_ucred->cr_ngroups = ngrp;
524 1.15 cgd p->p_flag |= P_SUGID;
525 1.15 cgd return (0);
526 1.15 cgd }
527 1.14 cgd
528 1.14 cgd /*
529 1.14 cgd * Check if gid is a member of the group set.
530 1.14 cgd */
531 1.32 christos int
532 1.14 cgd groupmember(gid, cred)
533 1.14 cgd gid_t gid;
534 1.14 cgd register struct ucred *cred;
535 1.14 cgd {
536 1.14 cgd register gid_t *gp;
537 1.14 cgd gid_t *egp;
538 1.14 cgd
539 1.14 cgd egp = &(cred->cr_groups[cred->cr_ngroups]);
540 1.14 cgd for (gp = cred->cr_groups; gp < egp; gp++)
541 1.14 cgd if (*gp == gid)
542 1.14 cgd return (1);
543 1.14 cgd return (0);
544 1.14 cgd }
545 1.14 cgd
546 1.14 cgd /*
547 1.14 cgd * Test whether the specified credentials imply "super-user"
548 1.14 cgd * privilege; if so, and we have accounting info, set the flag
549 1.14 cgd * indicating use of super-powers.
550 1.14 cgd * Returns 0 or error.
551 1.14 cgd */
552 1.32 christos int
553 1.14 cgd suser(cred, acflag)
554 1.14 cgd struct ucred *cred;
555 1.14 cgd u_short *acflag;
556 1.14 cgd {
557 1.14 cgd if (cred->cr_uid == 0) {
558 1.14 cgd if (acflag)
559 1.14 cgd *acflag |= ASU;
560 1.14 cgd return (0);
561 1.14 cgd }
562 1.14 cgd return (EPERM);
563 1.14 cgd }
564 1.14 cgd
565 1.14 cgd /*
566 1.14 cgd * Allocate a zeroed cred structure.
567 1.14 cgd */
568 1.14 cgd struct ucred *
569 1.14 cgd crget()
570 1.14 cgd {
571 1.14 cgd register struct ucred *cr;
572 1.14 cgd
573 1.14 cgd MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
574 1.14 cgd bzero((caddr_t)cr, sizeof(*cr));
575 1.14 cgd cr->cr_ref = 1;
576 1.14 cgd return (cr);
577 1.14 cgd }
578 1.14 cgd
579 1.14 cgd /*
580 1.14 cgd * Free a cred structure.
581 1.14 cgd * Throws away space when ref count gets to 0.
582 1.14 cgd */
583 1.22 cgd void
584 1.14 cgd crfree(cr)
585 1.14 cgd struct ucred *cr;
586 1.14 cgd {
587 1.15 cgd int s;
588 1.14 cgd
589 1.15 cgd s = splimp(); /* ??? */
590 1.15 cgd if (--cr->cr_ref == 0)
591 1.15 cgd FREE((caddr_t)cr, M_CRED);
592 1.14 cgd (void) splx(s);
593 1.14 cgd }
594 1.14 cgd
595 1.14 cgd /*
596 1.14 cgd * Copy cred structure to a new one and free the old one.
597 1.14 cgd */
598 1.14 cgd struct ucred *
599 1.14 cgd crcopy(cr)
600 1.14 cgd struct ucred *cr;
601 1.14 cgd {
602 1.14 cgd struct ucred *newcr;
603 1.14 cgd
604 1.14 cgd if (cr->cr_ref == 1)
605 1.14 cgd return (cr);
606 1.14 cgd newcr = crget();
607 1.14 cgd *newcr = *cr;
608 1.14 cgd crfree(cr);
609 1.14 cgd newcr->cr_ref = 1;
610 1.14 cgd return (newcr);
611 1.14 cgd }
612 1.14 cgd
613 1.14 cgd /*
614 1.14 cgd * Dup cred struct to a new held one.
615 1.14 cgd */
616 1.14 cgd struct ucred *
617 1.14 cgd crdup(cr)
618 1.14 cgd struct ucred *cr;
619 1.14 cgd {
620 1.14 cgd struct ucred *newcr;
621 1.14 cgd
622 1.14 cgd newcr = crget();
623 1.14 cgd *newcr = *cr;
624 1.14 cgd newcr->cr_ref = 1;
625 1.14 cgd return (newcr);
626 1.14 cgd }
627 1.14 cgd
628 1.14 cgd /*
629 1.14 cgd * Get login name, if available.
630 1.14 cgd */
631 1.14 cgd /* ARGSUSED */
632 1.32 christos int
633 1.37 jtc sys___getlogin(p, v, retval)
634 1.14 cgd struct proc *p;
635 1.29 thorpej void *v;
636 1.29 thorpej register_t *retval;
637 1.29 thorpej {
638 1.37 jtc struct sys___getlogin_args /* {
639 1.19 cgd syscallarg(char *) namebuf;
640 1.19 cgd syscallarg(u_int) namelen;
641 1.29 thorpej } */ *uap = v;
642 1.14 cgd
643 1.19 cgd if (SCARG(uap, namelen) > sizeof (p->p_pgrp->pg_session->s_login))
644 1.19 cgd SCARG(uap, namelen) = sizeof (p->p_pgrp->pg_session->s_login);
645 1.14 cgd return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
646 1.19 cgd (caddr_t) SCARG(uap, namebuf), SCARG(uap, namelen)));
647 1.14 cgd }
648 1.14 cgd
649 1.14 cgd /*
650 1.14 cgd * Set login name.
651 1.14 cgd */
652 1.14 cgd /* ARGSUSED */
653 1.32 christos int
654 1.30 mycroft sys_setlogin(p, v, retval)
655 1.14 cgd struct proc *p;
656 1.29 thorpej void *v;
657 1.29 thorpej register_t *retval;
658 1.29 thorpej {
659 1.30 mycroft struct sys_setlogin_args /* {
660 1.38 cgd syscallarg(const char *) namebuf;
661 1.29 thorpej } */ *uap = v;
662 1.14 cgd int error;
663 1.14 cgd
664 1.32 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
665 1.14 cgd return (error);
666 1.38 cgd error = copyinstr(SCARG(uap, namebuf), p->p_pgrp->pg_session->s_login,
667 1.25 mycroft sizeof (p->p_pgrp->pg_session->s_login) - 1, (size_t *)0);
668 1.14 cgd if (error == ENAMETOOLONG)
669 1.14 cgd error = EINVAL;
670 1.14 cgd return (error);
671 1.14 cgd }
672