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