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