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