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