Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / emulation / linux / linux_uid16.c
1 /*-
2  * Copyright (c) 2001  The FreeBSD Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/compat/linux/linux_uid16.c,v 1.4.2.1 2001/10/21 03:57:35 marcel Exp $
27  * $DragonFly: src/sys/emulation/linux/linux_uid16.c,v 1.2 2003/06/17 04:28:19 dillon Exp $
28  */
29
30 #include "opt_compat.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
35 #include <sys/sysproto.h>
36
37 #include <machine/../linux/linux.h>
38 #include <machine/../linux/linux_proto.h>
39 #include <compat/linux/linux_util.h>
40
41 DUMMY(setfsuid16);
42 DUMMY(setfsgid16);
43 DUMMY(getresuid16);
44 DUMMY(getresgid16);
45
46 #define CAST_NOCHG(x)   (x == 0xFFFF) ? -1 : x;
47
48 int
49 linux_chown16(struct proc *p, struct linux_chown16_args *args)
50 {
51         struct chown_args bsd;
52         caddr_t sg;
53
54         sg = stackgap_init();
55         CHECKALTEXIST(p, &sg, args->path);
56
57 #ifdef DEBUG
58         if (ldebug(chown16))
59                 printf(ARGS(chown16, "%s, %d, %d"), args->path, args->uid,
60                     args->gid);
61 #endif
62
63         bsd.path = args->path;
64         bsd.uid = CAST_NOCHG(args->uid);
65         bsd.gid = CAST_NOCHG(args->gid);
66         return (chown(p, &bsd));
67 }
68
69 int
70 linux_lchown16(struct proc *p, struct linux_lchown16_args *args)
71 {
72         struct lchown_args bsd;
73         caddr_t sg;
74
75         sg = stackgap_init();
76         CHECKALTEXIST(p, &sg, args->path);
77
78 #ifdef DEBUG
79         if (ldebug(lchown16))
80                 printf(ARGS(lchown16, "%s, %d, %d"), args->path, args->uid,
81                     args->gid);
82 #endif
83
84         bsd.path = args->path;
85         bsd.uid = CAST_NOCHG(args->uid);
86         bsd.gid = CAST_NOCHG(args->gid);
87         return (lchown(p, &bsd));
88 }
89
90 int
91 linux_setgroups16(struct proc *p, struct linux_setgroups16_args *args)
92 {
93         struct ucred *newcred, *oldcred;
94         l_gid16_t linux_gidset[NGROUPS];
95         gid_t *bsd_gidset;
96         int ngrp, error;
97
98 #ifdef DEBUG
99         if (ldebug(setgroups16))
100                 printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
101 #endif
102
103         ngrp = args->gidsetsize;
104         oldcred = p->p_ucred;
105
106         /*
107          * cr_groups[0] holds egid. Setting the whole set from
108          * the supplied set will cause egid to be changed too.
109          * Keep cr_groups[0] unchanged to prevent that.
110          */
111
112         if ((error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
113                 return (error);
114
115         if (ngrp >= NGROUPS)
116                 return (EINVAL);
117
118         newcred = crdup(oldcred);
119         if (ngrp > 0) {
120                 error = copyin((caddr_t)args->gidset, linux_gidset,
121                     ngrp * sizeof(l_gid16_t));
122                 if (error)
123                         return (error);
124
125                 newcred->cr_ngroups = ngrp + 1;
126
127                 bsd_gidset = newcred->cr_groups;
128                 ngrp--;
129                 while (ngrp >= 0) {
130                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
131                         ngrp--;
132                 }
133         }
134         else
135                 newcred->cr_ngroups = 1;
136
137         setsugid(p);
138         p->p_ucred = newcred;
139         crfree(oldcred);
140         return (0);
141 }
142
143 int
144 linux_getgroups16(struct proc *p, struct linux_getgroups16_args *args)
145 {
146         struct ucred *cred;
147         l_gid16_t linux_gidset[NGROUPS];
148         gid_t *bsd_gidset;
149         int bsd_gidsetsz, ngrp, error;
150
151 #ifdef DEBUG
152         if (ldebug(getgroups16))
153                 printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
154 #endif
155
156         cred = p->p_ucred;
157         bsd_gidset = cred->cr_groups;
158         bsd_gidsetsz = cred->cr_ngroups - 1;
159
160         /*
161          * cr_groups[0] holds egid. Returning the whole set
162          * here will cause a duplicate. Exclude cr_groups[0]
163          * to prevent that.
164          */
165
166         if ((ngrp = args->gidsetsize) == 0) {
167                 p->p_retval[0] = bsd_gidsetsz;
168                 return (0);
169         }
170
171         if (ngrp < bsd_gidsetsz)
172                 return (EINVAL);
173
174         ngrp = 0;
175         while (ngrp < bsd_gidsetsz) {
176                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
177                 ngrp++;
178         }
179
180         error = copyout(linux_gidset, (caddr_t)args->gidset,
181             ngrp * sizeof(l_gid16_t));
182         if (error)
183                 return (error);
184
185         p->p_retval[0] = ngrp;
186         return (0);
187 }
188
189 /*
190  * The FreeBSD native getgid(2) and getuid(2) also modify p->p_retval[1]
191  * when COMPAT_43 or COMPAT_SUNOS is defined. This globbers registers that
192  * are assumed to be preserved. The following lightweight syscalls fixes
193  * this. See also linux_getpid(2), linux_getgid(2) and linux_getuid(2) in
194  * linux_misc.c
195  *
196  * linux_getgid16() - MP SAFE
197  * linux_getuid16() - MP SAFE
198  */
199
200 int
201 linux_getgid16(struct proc *p, struct linux_getgid16_args *args)
202 {
203
204         p->p_retval[0] = p->p_cred->p_rgid;
205         return (0);
206 }
207
208 int
209 linux_getuid16(struct proc *p, struct linux_getuid16_args *args)
210 {
211
212         p->p_retval[0] = p->p_cred->p_ruid;
213         return (0);
214 }
215
216 int
217 linux_getegid16(struct proc *p, struct linux_getegid16_args *args)
218 {
219         struct getegid_args bsd;
220
221         return (getegid(p, &bsd));
222 }
223
224 int
225 linux_geteuid16(struct proc *p, struct linux_geteuid16_args *args)
226 {
227         struct geteuid_args bsd;
228
229         return (geteuid(p, &bsd));
230 }
231
232 int
233 linux_setgid16(struct proc *p, struct linux_setgid16_args *args)
234 {
235         struct setgid_args bsd;
236
237         bsd.gid = args->gid;
238         return (setgid(p, &bsd));
239 }
240
241 int
242 linux_setuid16(struct proc *p, struct linux_setuid16_args *args)
243 {
244         struct setuid_args bsd;
245
246         bsd.uid = args->uid;
247         return (setuid(p, &bsd));
248 }
249
250 int
251 linux_setregid16(struct proc *p, struct linux_setregid16_args *args)
252 {
253         struct setregid_args bsd;
254
255         bsd.rgid = CAST_NOCHG(args->rgid);
256         bsd.egid = CAST_NOCHG(args->egid);
257         return (setregid(p, &bsd));
258 }
259
260 int
261 linux_setreuid16(struct proc *p, struct linux_setreuid16_args *args)
262 {
263         struct setreuid_args bsd;
264
265         bsd.ruid = CAST_NOCHG(args->ruid);
266         bsd.euid = CAST_NOCHG(args->euid);
267         return (setreuid(p, &bsd));
268 }
269
270 int
271 linux_setresgid16(struct proc *p, struct linux_setresgid16_args *args)
272 {
273         struct setresgid_args bsd;
274
275         bsd.rgid = CAST_NOCHG(args->rgid);
276         bsd.egid = CAST_NOCHG(args->egid);
277         bsd.sgid = CAST_NOCHG(args->sgid);
278         return (setresgid(p, &bsd));
279 }
280
281 int
282 linux_setresuid16(struct proc *p, struct linux_setresuid16_args *args)
283 {
284         struct setresuid_args bsd;
285
286         bsd.ruid = CAST_NOCHG(args->ruid);
287         bsd.euid = CAST_NOCHG(args->euid);
288         bsd.suid = CAST_NOCHG(args->suid);
289         return (setresuid(p, &bsd));
290 }