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