Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ioctl.c | |
3 | * | |
4 | * Copyright (C) 1995, 1996 by Volker Lendecke | |
5 | * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache | |
6 | * Modified 1998, 1999 Wolfram Pienkoss for NLS | |
7 | * | |
8 | */ | |
9 | ||
16f7e0fe | 10 | #include <linux/capability.h> |
54f67f63 | 11 | #include <linux/compat.h> |
1da177e4 LT |
12 | #include <linux/errno.h> |
13 | #include <linux/fs.h> | |
14 | #include <linux/ioctl.h> | |
15 | #include <linux/time.h> | |
16 | #include <linux/mm.h> | |
a761a1c0 | 17 | #include <linux/mount.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
1da177e4 LT |
19 | #include <linux/highuid.h> |
20 | #include <linux/vmalloc.h> | |
e8edc6e0 | 21 | #include <linux/sched.h> |
1da177e4 | 22 | |
54f67f63 PV |
23 | #include <asm/uaccess.h> |
24 | ||
32c419d9 | 25 | #include "ncp_fs.h" |
1da177e4 LT |
26 | |
27 | /* maximum limit for ncp_objectname_ioctl */ | |
28 | #define NCP_OBJECT_NAME_MAX_LEN 4096 | |
29 | /* maximum limit for ncp_privatedata_ioctl */ | |
30 | #define NCP_PRIVATE_DATA_MAX_LEN 8192 | |
31 | /* maximum negotiable packet size */ | |
32 | #define NCP_PACKET_SIZE_INTERNAL 65536 | |
33 | ||
34 | static int | |
2e54eb96 | 35 | ncp_get_fs_info(struct ncp_server * server, struct inode *inode, |
8c744fb8 | 36 | struct ncp_fs_info __user *arg) |
1da177e4 LT |
37 | { |
38 | struct ncp_fs_info info; | |
39 | ||
1da177e4 LT |
40 | if (copy_from_user(&info, arg, sizeof(info))) |
41 | return -EFAULT; | |
42 | ||
43 | if (info.version != NCP_GET_FS_INFO_VERSION) { | |
44 | DPRINTK("info.version invalid: %d\n", info.version); | |
45 | return -EINVAL; | |
46 | } | |
47 | /* TODO: info.addr = server->m.serv_addr; */ | |
1ac7fd81 | 48 | SET_UID(info.mounted_uid, from_kuid_munged(current_user_ns(), server->m.mounted_uid)); |
1da177e4 LT |
49 | info.connection = server->connection; |
50 | info.buffer_size = server->buffer_size; | |
51 | info.volume_number = NCP_FINFO(inode)->volNumber; | |
52 | info.directory_id = NCP_FINFO(inode)->DosDirNum; | |
53 | ||
54 | if (copy_to_user(arg, &info, sizeof(info))) | |
55 | return -EFAULT; | |
56 | return 0; | |
57 | } | |
58 | ||
59 | static int | |
2e54eb96 | 60 | ncp_get_fs_info_v2(struct ncp_server * server, struct inode *inode, |
8c744fb8 | 61 | struct ncp_fs_info_v2 __user * arg) |
1da177e4 LT |
62 | { |
63 | struct ncp_fs_info_v2 info2; | |
64 | ||
1da177e4 LT |
65 | if (copy_from_user(&info2, arg, sizeof(info2))) |
66 | return -EFAULT; | |
67 | ||
68 | if (info2.version != NCP_GET_FS_INFO_VERSION_V2) { | |
69 | DPRINTK("info.version invalid: %d\n", info2.version); | |
70 | return -EINVAL; | |
71 | } | |
1ac7fd81 | 72 | info2.mounted_uid = from_kuid_munged(current_user_ns(), server->m.mounted_uid); |
1da177e4 LT |
73 | info2.connection = server->connection; |
74 | info2.buffer_size = server->buffer_size; | |
75 | info2.volume_number = NCP_FINFO(inode)->volNumber; | |
76 | info2.directory_id = NCP_FINFO(inode)->DosDirNum; | |
77 | info2.dummy1 = info2.dummy2 = info2.dummy3 = 0; | |
78 | ||
79 | if (copy_to_user(arg, &info2, sizeof(info2))) | |
80 | return -EFAULT; | |
81 | return 0; | |
82 | } | |
83 | ||
54f67f63 PV |
84 | #ifdef CONFIG_COMPAT |
85 | struct compat_ncp_objectname_ioctl | |
86 | { | |
87 | s32 auth_type; | |
88 | u32 object_name_len; | |
0211a9c8 | 89 | compat_caddr_t object_name; /* a userspace data, in most cases user name */ |
54f67f63 PV |
90 | }; |
91 | ||
92 | struct compat_ncp_fs_info_v2 { | |
93 | s32 version; | |
94 | u32 mounted_uid; | |
95 | u32 connection; | |
96 | u32 buffer_size; | |
97 | ||
98 | u32 volume_number; | |
99 | u32 directory_id; | |
100 | ||
101 | u32 dummy1; | |
102 | u32 dummy2; | |
103 | u32 dummy3; | |
104 | }; | |
105 | ||
106 | struct compat_ncp_ioctl_request { | |
107 | u32 function; | |
108 | u32 size; | |
109 | compat_caddr_t data; | |
110 | }; | |
111 | ||
112 | struct compat_ncp_privatedata_ioctl | |
113 | { | |
114 | u32 len; | |
115 | compat_caddr_t data; /* ~1000 for NDS */ | |
116 | }; | |
117 | ||
118 | #define NCP_IOC_GET_FS_INFO_V2_32 _IOWR('n', 4, struct compat_ncp_fs_info_v2) | |
119 | #define NCP_IOC_NCPREQUEST_32 _IOR('n', 1, struct compat_ncp_ioctl_request) | |
120 | #define NCP_IOC_GETOBJECTNAME_32 _IOWR('n', 9, struct compat_ncp_objectname_ioctl) | |
121 | #define NCP_IOC_SETOBJECTNAME_32 _IOR('n', 9, struct compat_ncp_objectname_ioctl) | |
122 | #define NCP_IOC_GETPRIVATEDATA_32 _IOWR('n', 10, struct compat_ncp_privatedata_ioctl) | |
123 | #define NCP_IOC_SETPRIVATEDATA_32 _IOR('n', 10, struct compat_ncp_privatedata_ioctl) | |
124 | ||
125 | static int | |
2e54eb96 | 126 | ncp_get_compat_fs_info_v2(struct ncp_server * server, struct inode *inode, |
54f67f63 PV |
127 | struct compat_ncp_fs_info_v2 __user * arg) |
128 | { | |
54f67f63 PV |
129 | struct compat_ncp_fs_info_v2 info2; |
130 | ||
54f67f63 PV |
131 | if (copy_from_user(&info2, arg, sizeof(info2))) |
132 | return -EFAULT; | |
133 | ||
134 | if (info2.version != NCP_GET_FS_INFO_VERSION_V2) { | |
135 | DPRINTK("info.version invalid: %d\n", info2.version); | |
136 | return -EINVAL; | |
137 | } | |
1ac7fd81 | 138 | info2.mounted_uid = from_kuid_munged(current_user_ns(), server->m.mounted_uid); |
54f67f63 PV |
139 | info2.connection = server->connection; |
140 | info2.buffer_size = server->buffer_size; | |
141 | info2.volume_number = NCP_FINFO(inode)->volNumber; | |
142 | info2.directory_id = NCP_FINFO(inode)->DosDirNum; | |
143 | info2.dummy1 = info2.dummy2 = info2.dummy3 = 0; | |
144 | ||
145 | if (copy_to_user(arg, &info2, sizeof(info2))) | |
146 | return -EFAULT; | |
147 | return 0; | |
148 | } | |
149 | #endif | |
150 | ||
151 | #define NCP_IOC_GETMOUNTUID16 _IOW('n', 2, u16) | |
152 | #define NCP_IOC_GETMOUNTUID32 _IOW('n', 2, u32) | |
153 | #define NCP_IOC_GETMOUNTUID64 _IOW('n', 2, u64) | |
154 | ||
1da177e4 LT |
155 | #ifdef CONFIG_NCPFS_NLS |
156 | /* Here we are select the iocharset and the codepage for NLS. | |
157 | * Thanks Petr Vandrovec for idea and many hints. | |
158 | */ | |
159 | static int | |
160 | ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg) | |
161 | { | |
162 | struct ncp_nls_ioctl user; | |
163 | struct nls_table *codepage; | |
164 | struct nls_table *iocharset; | |
165 | struct nls_table *oldset_io; | |
166 | struct nls_table *oldset_cp; | |
2e54eb96 PV |
167 | int utf8; |
168 | int err; | |
1da177e4 LT |
169 | |
170 | if (copy_from_user(&user, arg, sizeof(user))) | |
171 | return -EFAULT; | |
172 | ||
173 | codepage = NULL; | |
174 | user.codepage[NCP_IOCSNAME_LEN] = 0; | |
175 | if (!user.codepage[0] || !strcmp(user.codepage, "default")) | |
176 | codepage = load_nls_default(); | |
177 | else { | |
178 | codepage = load_nls(user.codepage); | |
179 | if (!codepage) { | |
180 | return -EBADRQC; | |
181 | } | |
182 | } | |
183 | ||
184 | iocharset = NULL; | |
185 | user.iocharset[NCP_IOCSNAME_LEN] = 0; | |
186 | if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) { | |
187 | iocharset = load_nls_default(); | |
2e54eb96 | 188 | utf8 = 0; |
1da177e4 LT |
189 | } else if (!strcmp(user.iocharset, "utf8")) { |
190 | iocharset = load_nls_default(); | |
2e54eb96 | 191 | utf8 = 1; |
1da177e4 LT |
192 | } else { |
193 | iocharset = load_nls(user.iocharset); | |
194 | if (!iocharset) { | |
195 | unload_nls(codepage); | |
196 | return -EBADRQC; | |
197 | } | |
2e54eb96 | 198 | utf8 = 0; |
1da177e4 LT |
199 | } |
200 | ||
2e54eb96 PV |
201 | mutex_lock(&server->root_setup_lock); |
202 | if (server->root_setuped) { | |
203 | oldset_cp = codepage; | |
204 | oldset_io = iocharset; | |
205 | err = -EBUSY; | |
206 | } else { | |
207 | if (utf8) | |
208 | NCP_SET_FLAG(server, NCP_FLAG_UTF8); | |
209 | else | |
210 | NCP_CLR_FLAG(server, NCP_FLAG_UTF8); | |
211 | oldset_cp = server->nls_vol; | |
212 | server->nls_vol = codepage; | |
213 | oldset_io = server->nls_io; | |
214 | server->nls_io = iocharset; | |
215 | err = 0; | |
216 | } | |
217 | mutex_unlock(&server->root_setup_lock); | |
6d729e44 TG |
218 | unload_nls(oldset_cp); |
219 | unload_nls(oldset_io); | |
1da177e4 | 220 | |
2e54eb96 | 221 | return err; |
1da177e4 LT |
222 | } |
223 | ||
224 | static int | |
225 | ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg) | |
226 | { | |
227 | struct ncp_nls_ioctl user; | |
228 | int len; | |
229 | ||
230 | memset(&user, 0, sizeof(user)); | |
2e54eb96 | 231 | mutex_lock(&server->root_setup_lock); |
1da177e4 LT |
232 | if (server->nls_vol && server->nls_vol->charset) { |
233 | len = strlen(server->nls_vol->charset); | |
234 | if (len > NCP_IOCSNAME_LEN) | |
235 | len = NCP_IOCSNAME_LEN; | |
236 | strncpy(user.codepage, server->nls_vol->charset, len); | |
237 | user.codepage[len] = 0; | |
238 | } | |
239 | ||
240 | if (NCP_IS_FLAG(server, NCP_FLAG_UTF8)) | |
241 | strcpy(user.iocharset, "utf8"); | |
242 | else if (server->nls_io && server->nls_io->charset) { | |
243 | len = strlen(server->nls_io->charset); | |
244 | if (len > NCP_IOCSNAME_LEN) | |
245 | len = NCP_IOCSNAME_LEN; | |
246 | strncpy(user.iocharset, server->nls_io->charset, len); | |
247 | user.iocharset[len] = 0; | |
248 | } | |
2e54eb96 | 249 | mutex_unlock(&server->root_setup_lock); |
1da177e4 LT |
250 | |
251 | if (copy_to_user(arg, &user, sizeof(user))) | |
252 | return -EFAULT; | |
253 | return 0; | |
254 | } | |
255 | #endif /* CONFIG_NCPFS_NLS */ | |
256 | ||
2e54eb96 | 257 | static long __ncp_ioctl(struct inode *inode, unsigned int cmd, unsigned long arg) |
1da177e4 LT |
258 | { |
259 | struct ncp_server *server = NCP_SERVER(inode); | |
260 | int result; | |
261 | struct ncp_ioctl_request request; | |
262 | char* bouncebuffer; | |
263 | void __user *argp = (void __user *)arg; | |
264 | ||
265 | switch (cmd) { | |
54f67f63 PV |
266 | #ifdef CONFIG_COMPAT |
267 | case NCP_IOC_NCPREQUEST_32: | |
268 | #endif | |
1da177e4 | 269 | case NCP_IOC_NCPREQUEST: |
54f67f63 PV |
270 | #ifdef CONFIG_COMPAT |
271 | if (cmd == NCP_IOC_NCPREQUEST_32) { | |
272 | struct compat_ncp_ioctl_request request32; | |
273 | if (copy_from_user(&request32, argp, sizeof(request32))) | |
274 | return -EFAULT; | |
275 | request.function = request32.function; | |
276 | request.size = request32.size; | |
277 | request.data = compat_ptr(request32.data); | |
278 | } else | |
279 | #endif | |
1da177e4 LT |
280 | if (copy_from_user(&request, argp, sizeof(request))) |
281 | return -EFAULT; | |
282 | ||
283 | if ((request.function > 255) | |
284 | || (request.size > | |
285 | NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) { | |
286 | return -EINVAL; | |
287 | } | |
288 | bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL); | |
289 | if (!bouncebuffer) | |
290 | return -ENOMEM; | |
291 | if (copy_from_user(bouncebuffer, request.data, request.size)) { | |
292 | vfree(bouncebuffer); | |
293 | return -EFAULT; | |
294 | } | |
295 | ncp_lock_server(server); | |
296 | ||
297 | /* FIXME: We hack around in the server's structures | |
298 | here to be able to use ncp_request */ | |
299 | ||
300 | server->has_subfunction = 0; | |
301 | server->current_size = request.size; | |
302 | memcpy(server->packet, bouncebuffer, request.size); | |
303 | ||
2e54eb96 | 304 | result = ncp_request2(server, request.function, |
1da177e4 LT |
305 | bouncebuffer, NCP_PACKET_SIZE_INTERNAL); |
306 | if (result < 0) | |
307 | result = -EIO; | |
308 | else | |
309 | result = server->reply_size; | |
310 | ncp_unlock_server(server); | |
311 | DPRINTK("ncp_ioctl: copy %d bytes\n", | |
312 | result); | |
313 | if (result >= 0) | |
314 | if (copy_to_user(request.data, bouncebuffer, result)) | |
315 | result = -EFAULT; | |
316 | vfree(bouncebuffer); | |
317 | return result; | |
318 | ||
319 | case NCP_IOC_CONN_LOGGED_IN: | |
320 | ||
1da177e4 LT |
321 | if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE)) |
322 | return -EINVAL; | |
2e54eb96 | 323 | mutex_lock(&server->root_setup_lock); |
1da177e4 | 324 | if (server->root_setuped) |
2e54eb96 PV |
325 | result = -EBUSY; |
326 | else { | |
327 | result = ncp_conn_logged_in(inode->i_sb); | |
328 | if (result == 0) | |
329 | server->root_setuped = 1; | |
330 | } | |
331 | mutex_unlock(&server->root_setup_lock); | |
332 | return result; | |
1da177e4 LT |
333 | |
334 | case NCP_IOC_GET_FS_INFO: | |
2e54eb96 | 335 | return ncp_get_fs_info(server, inode, argp); |
1da177e4 LT |
336 | |
337 | case NCP_IOC_GET_FS_INFO_V2: | |
2e54eb96 | 338 | return ncp_get_fs_info_v2(server, inode, argp); |
1da177e4 | 339 | |
54f67f63 PV |
340 | #ifdef CONFIG_COMPAT |
341 | case NCP_IOC_GET_FS_INFO_V2_32: | |
2e54eb96 | 342 | return ncp_get_compat_fs_info_v2(server, inode, argp); |
54f67f63 PV |
343 | #endif |
344 | /* we have too many combinations of CONFIG_COMPAT, | |
345 | * CONFIG_64BIT and CONFIG_UID16, so just handle | |
346 | * any of the possible ioctls */ | |
347 | case NCP_IOC_GETMOUNTUID16: | |
2e54eb96 | 348 | { |
54f67f63 | 349 | u16 uid; |
2e54eb96 | 350 | |
1ac7fd81 | 351 | SET_UID(uid, from_kuid_munged(current_user_ns(), server->m.mounted_uid)); |
54f67f63 PV |
352 | if (put_user(uid, (u16 __user *)argp)) |
353 | return -EFAULT; | |
2e54eb96 | 354 | return 0; |
1da177e4 | 355 | } |
2e54eb96 | 356 | case NCP_IOC_GETMOUNTUID32: |
1ac7fd81 EB |
357 | { |
358 | uid_t uid = from_kuid_munged(current_user_ns(), server->m.mounted_uid); | |
359 | if (put_user(uid, (u32 __user *)argp)) | |
2e54eb96 PV |
360 | return -EFAULT; |
361 | return 0; | |
1ac7fd81 | 362 | } |
2e54eb96 | 363 | case NCP_IOC_GETMOUNTUID64: |
1ac7fd81 EB |
364 | { |
365 | uid_t uid = from_kuid_munged(current_user_ns(), server->m.mounted_uid); | |
366 | if (put_user(uid, (u64 __user *)argp)) | |
2e54eb96 | 367 | return -EFAULT; |
54f67f63 | 368 | return 0; |
1ac7fd81 | 369 | } |
1da177e4 LT |
370 | case NCP_IOC_GETROOT: |
371 | { | |
372 | struct ncp_setroot_ioctl sr; | |
373 | ||
2e54eb96 PV |
374 | result = -EACCES; |
375 | mutex_lock(&server->root_setup_lock); | |
1da177e4 LT |
376 | if (server->m.mounted_vol[0]) { |
377 | struct dentry* dentry = inode->i_sb->s_root; | |
378 | ||
379 | if (dentry) { | |
305787e4 | 380 | struct inode* s_inode = dentry->d_inode; |
2e54eb96 | 381 | |
305787e4 HH |
382 | if (s_inode) { |
383 | sr.volNumber = NCP_FINFO(s_inode)->volNumber; | |
384 | sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum; | |
1da177e4 | 385 | sr.namespace = server->name_space[sr.volNumber]; |
2e54eb96 | 386 | result = 0; |
1da177e4 LT |
387 | } else |
388 | DPRINTK("ncpfs: s_root->d_inode==NULL\n"); | |
389 | } else | |
390 | DPRINTK("ncpfs: s_root==NULL\n"); | |
391 | } else { | |
392 | sr.volNumber = -1; | |
393 | sr.namespace = 0; | |
394 | sr.dirEntNum = 0; | |
2e54eb96 | 395 | result = 0; |
1da177e4 | 396 | } |
2e54eb96 PV |
397 | mutex_unlock(&server->root_setup_lock); |
398 | if (!result && copy_to_user(argp, &sr, sizeof(sr))) | |
399 | result = -EFAULT; | |
400 | return result; | |
1da177e4 | 401 | } |
48937024 | 402 | |
1da177e4 LT |
403 | case NCP_IOC_SETROOT: |
404 | { | |
405 | struct ncp_setroot_ioctl sr; | |
406 | __u32 vnum; | |
407 | __le32 de; | |
408 | __le32 dosde; | |
409 | struct dentry* dentry; | |
410 | ||
1da177e4 LT |
411 | if (copy_from_user(&sr, argp, sizeof(sr))) |
412 | return -EFAULT; | |
2e54eb96 PV |
413 | mutex_lock(&server->root_setup_lock); |
414 | if (server->root_setuped) | |
415 | result = -EBUSY; | |
416 | else { | |
417 | if (sr.volNumber < 0) { | |
418 | server->m.mounted_vol[0] = 0; | |
419 | vnum = NCP_NUMBER_OF_VOLUMES; | |
420 | de = 0; | |
421 | dosde = 0; | |
422 | result = 0; | |
423 | } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) { | |
424 | result = -EINVAL; | |
425 | } else if (ncp_mount_subdir(server, sr.volNumber, | |
426 | sr.namespace, sr.dirEntNum, | |
427 | &vnum, &de, &dosde)) { | |
428 | result = -ENOENT; | |
1da177e4 | 429 | } else |
2e54eb96 PV |
430 | result = 0; |
431 | ||
432 | if (result == 0) { | |
433 | dentry = inode->i_sb->s_root; | |
434 | if (dentry) { | |
435 | struct inode* s_inode = dentry->d_inode; | |
436 | ||
437 | if (s_inode) { | |
438 | NCP_FINFO(s_inode)->volNumber = vnum; | |
439 | NCP_FINFO(s_inode)->dirEntNum = de; | |
440 | NCP_FINFO(s_inode)->DosDirNum = dosde; | |
441 | server->root_setuped = 1; | |
442 | } else { | |
443 | DPRINTK("ncpfs: s_root->d_inode==NULL\n"); | |
444 | result = -EIO; | |
445 | } | |
446 | } else { | |
447 | DPRINTK("ncpfs: s_root==NULL\n"); | |
448 | result = -EIO; | |
449 | } | |
450 | } | |
2e54eb96 PV |
451 | } |
452 | mutex_unlock(&server->root_setup_lock); | |
1da177e4 | 453 | |
2e54eb96 | 454 | return result; |
1da177e4 LT |
455 | } |
456 | ||
2e54eb96 | 457 | #ifdef CONFIG_NCPFS_PACKET_SIGNING |
1da177e4 | 458 | case NCP_IOC_SIGN_INIT: |
2e54eb96 PV |
459 | { |
460 | struct ncp_sign_init sign; | |
1da177e4 | 461 | |
2e54eb96 | 462 | if (argp) |
1da177e4 LT |
463 | if (copy_from_user(&sign, argp, sizeof(sign))) |
464 | return -EFAULT; | |
2e54eb96 PV |
465 | ncp_lock_server(server); |
466 | mutex_lock(&server->rcv.creq_mutex); | |
467 | if (argp) { | |
468 | if (server->sign_wanted) { | |
469 | memcpy(server->sign_root,sign.sign_root,8); | |
470 | memcpy(server->sign_last,sign.sign_last,16); | |
471 | server->sign_active = 1; | |
472 | } | |
473 | /* ignore when signatures not wanted */ | |
474 | } else { | |
475 | server->sign_active = 0; | |
1da177e4 | 476 | } |
2e54eb96 PV |
477 | mutex_unlock(&server->rcv.creq_mutex); |
478 | ncp_unlock_server(server); | |
479 | return 0; | |
1da177e4 | 480 | } |
2e54eb96 | 481 | |
1da177e4 | 482 | case NCP_IOC_SIGN_WANTED: |
2e54eb96 PV |
483 | { |
484 | int state; | |
485 | ||
486 | ncp_lock_server(server); | |
487 | state = server->sign_wanted; | |
488 | ncp_unlock_server(server); | |
489 | if (put_user(state, (int __user *)argp)) | |
490 | return -EFAULT; | |
491 | return 0; | |
492 | } | |
48937024 | 493 | |
1da177e4 LT |
494 | case NCP_IOC_SET_SIGN_WANTED: |
495 | { | |
496 | int newstate; | |
497 | ||
1da177e4 LT |
498 | /* get only low 8 bits... */ |
499 | if (get_user(newstate, (unsigned char __user *)argp)) | |
500 | return -EFAULT; | |
2e54eb96 PV |
501 | result = 0; |
502 | ncp_lock_server(server); | |
1da177e4 LT |
503 | if (server->sign_active) { |
504 | /* cannot turn signatures OFF when active */ | |
2e54eb96 PV |
505 | if (!newstate) |
506 | result = -EINVAL; | |
1da177e4 LT |
507 | } else { |
508 | server->sign_wanted = newstate != 0; | |
509 | } | |
2e54eb96 PV |
510 | ncp_unlock_server(server); |
511 | return result; | |
1da177e4 LT |
512 | } |
513 | ||
514 | #endif /* CONFIG_NCPFS_PACKET_SIGNING */ | |
515 | ||
516 | #ifdef CONFIG_NCPFS_IOCTL_LOCKING | |
517 | case NCP_IOC_LOCKUNLOCK: | |
1da177e4 LT |
518 | { |
519 | struct ncp_lock_ioctl rqdata; | |
1da177e4 LT |
520 | |
521 | if (copy_from_user(&rqdata, argp, sizeof(rqdata))) | |
522 | return -EFAULT; | |
523 | if (rqdata.origin != 0) | |
524 | return -EINVAL; | |
525 | /* check for cmd */ | |
526 | switch (rqdata.cmd) { | |
527 | case NCP_LOCK_EX: | |
528 | case NCP_LOCK_SH: | |
529 | if (rqdata.timeout == 0) | |
530 | rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; | |
531 | else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT) | |
532 | rqdata.timeout = NCP_LOCK_MAX_TIMEOUT; | |
533 | break; | |
534 | case NCP_LOCK_LOG: | |
535 | rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */ | |
536 | case NCP_LOCK_CLEAR: | |
537 | break; | |
538 | default: | |
539 | return -EINVAL; | |
540 | } | |
541 | /* locking needs both read and write access */ | |
542 | if ((result = ncp_make_open(inode, O_RDWR)) != 0) | |
543 | { | |
544 | return result; | |
545 | } | |
1da177e4 LT |
546 | result = -EISDIR; |
547 | if (!S_ISREG(inode->i_mode)) | |
548 | goto outrel; | |
549 | if (rqdata.cmd == NCP_LOCK_CLEAR) | |
550 | { | |
551 | result = ncp_ClearPhysicalRecord(NCP_SERVER(inode), | |
2e54eb96 | 552 | NCP_FINFO(inode)->file_handle, |
1da177e4 LT |
553 | rqdata.offset, |
554 | rqdata.length); | |
555 | if (result > 0) result = 0; /* no such lock */ | |
556 | } | |
557 | else | |
558 | { | |
559 | int lockcmd; | |
560 | ||
561 | switch (rqdata.cmd) | |
562 | { | |
563 | case NCP_LOCK_EX: lockcmd=1; break; | |
564 | case NCP_LOCK_SH: lockcmd=3; break; | |
565 | default: lockcmd=0; break; | |
566 | } | |
567 | result = ncp_LogPhysicalRecord(NCP_SERVER(inode), | |
568 | NCP_FINFO(inode)->file_handle, | |
569 | lockcmd, | |
570 | rqdata.offset, | |
571 | rqdata.length, | |
572 | rqdata.timeout); | |
573 | if (result > 0) result = -EAGAIN; | |
574 | } | |
2e54eb96 | 575 | outrel: |
1da177e4 LT |
576 | ncp_inode_close(inode); |
577 | return result; | |
578 | } | |
579 | #endif /* CONFIG_NCPFS_IOCTL_LOCKING */ | |
580 | ||
54f67f63 PV |
581 | #ifdef CONFIG_COMPAT |
582 | case NCP_IOC_GETOBJECTNAME_32: | |
54f67f63 PV |
583 | { |
584 | struct compat_ncp_objectname_ioctl user; | |
585 | size_t outl; | |
586 | ||
587 | if (copy_from_user(&user, argp, sizeof(user))) | |
588 | return -EFAULT; | |
2e54eb96 | 589 | down_read(&server->auth_rwsem); |
54f67f63 PV |
590 | user.auth_type = server->auth.auth_type; |
591 | outl = user.object_name_len; | |
592 | user.object_name_len = server->auth.object_name_len; | |
593 | if (outl > user.object_name_len) | |
594 | outl = user.object_name_len; | |
2e54eb96 | 595 | result = 0; |
54f67f63 PV |
596 | if (outl) { |
597 | if (copy_to_user(compat_ptr(user.object_name), | |
598 | server->auth.object_name, | |
2e54eb96 PV |
599 | outl)) |
600 | result = -EFAULT; | |
54f67f63 | 601 | } |
2e54eb96 PV |
602 | up_read(&server->auth_rwsem); |
603 | if (!result && copy_to_user(argp, &user, sizeof(user))) | |
604 | result = -EFAULT; | |
605 | return result; | |
54f67f63 PV |
606 | } |
607 | #endif | |
48937024 | 608 | |
1da177e4 | 609 | case NCP_IOC_GETOBJECTNAME: |
1da177e4 LT |
610 | { |
611 | struct ncp_objectname_ioctl user; | |
612 | size_t outl; | |
613 | ||
614 | if (copy_from_user(&user, argp, sizeof(user))) | |
615 | return -EFAULT; | |
2e54eb96 | 616 | down_read(&server->auth_rwsem); |
1da177e4 LT |
617 | user.auth_type = server->auth.auth_type; |
618 | outl = user.object_name_len; | |
619 | user.object_name_len = server->auth.object_name_len; | |
620 | if (outl > user.object_name_len) | |
621 | outl = user.object_name_len; | |
2e54eb96 | 622 | result = 0; |
1da177e4 LT |
623 | if (outl) { |
624 | if (copy_to_user(user.object_name, | |
625 | server->auth.object_name, | |
2e54eb96 PV |
626 | outl)) |
627 | result = -EFAULT; | |
1da177e4 | 628 | } |
2e54eb96 PV |
629 | up_read(&server->auth_rwsem); |
630 | if (!result && copy_to_user(argp, &user, sizeof(user))) | |
631 | result = -EFAULT; | |
632 | return result; | |
1da177e4 | 633 | } |
48937024 | 634 | |
54f67f63 PV |
635 | #ifdef CONFIG_COMPAT |
636 | case NCP_IOC_SETOBJECTNAME_32: | |
637 | #endif | |
1da177e4 | 638 | case NCP_IOC_SETOBJECTNAME: |
1da177e4 LT |
639 | { |
640 | struct ncp_objectname_ioctl user; | |
641 | void* newname; | |
642 | void* oldname; | |
643 | size_t oldnamelen; | |
644 | void* oldprivate; | |
645 | size_t oldprivatelen; | |
646 | ||
54f67f63 PV |
647 | #ifdef CONFIG_COMPAT |
648 | if (cmd == NCP_IOC_SETOBJECTNAME_32) { | |
649 | struct compat_ncp_objectname_ioctl user32; | |
650 | if (copy_from_user(&user32, argp, sizeof(user32))) | |
651 | return -EFAULT; | |
652 | user.auth_type = user32.auth_type; | |
653 | user.object_name_len = user32.object_name_len; | |
654 | user.object_name = compat_ptr(user32.object_name); | |
655 | } else | |
656 | #endif | |
1da177e4 LT |
657 | if (copy_from_user(&user, argp, sizeof(user))) |
658 | return -EFAULT; | |
54f67f63 | 659 | |
1da177e4 LT |
660 | if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN) |
661 | return -ENOMEM; | |
662 | if (user.object_name_len) { | |
a9482ebc LZ |
663 | newname = memdup_user(user.object_name, |
664 | user.object_name_len); | |
665 | if (IS_ERR(newname)) | |
666 | return PTR_ERR(newname); | |
1da177e4 LT |
667 | } else { |
668 | newname = NULL; | |
669 | } | |
2e54eb96 | 670 | down_write(&server->auth_rwsem); |
1da177e4 LT |
671 | oldname = server->auth.object_name; |
672 | oldnamelen = server->auth.object_name_len; | |
673 | oldprivate = server->priv.data; | |
674 | oldprivatelen = server->priv.len; | |
675 | server->auth.auth_type = user.auth_type; | |
676 | server->auth.object_name_len = user.object_name_len; | |
677 | server->auth.object_name = newname; | |
678 | server->priv.len = 0; | |
679 | server->priv.data = NULL; | |
2e54eb96 | 680 | up_write(&server->auth_rwsem); |
44db77f3 PE |
681 | kfree(oldprivate); |
682 | kfree(oldname); | |
1da177e4 LT |
683 | return 0; |
684 | } | |
48937024 | 685 | |
54f67f63 PV |
686 | #ifdef CONFIG_COMPAT |
687 | case NCP_IOC_GETPRIVATEDATA_32: | |
688 | #endif | |
1da177e4 | 689 | case NCP_IOC_GETPRIVATEDATA: |
1da177e4 LT |
690 | { |
691 | struct ncp_privatedata_ioctl user; | |
692 | size_t outl; | |
693 | ||
54f67f63 PV |
694 | #ifdef CONFIG_COMPAT |
695 | if (cmd == NCP_IOC_GETPRIVATEDATA_32) { | |
696 | struct compat_ncp_privatedata_ioctl user32; | |
697 | if (copy_from_user(&user32, argp, sizeof(user32))) | |
698 | return -EFAULT; | |
699 | user.len = user32.len; | |
700 | user.data = compat_ptr(user32.data); | |
701 | } else | |
702 | #endif | |
1da177e4 LT |
703 | if (copy_from_user(&user, argp, sizeof(user))) |
704 | return -EFAULT; | |
54f67f63 | 705 | |
2e54eb96 | 706 | down_read(&server->auth_rwsem); |
1da177e4 LT |
707 | outl = user.len; |
708 | user.len = server->priv.len; | |
709 | if (outl > user.len) outl = user.len; | |
2e54eb96 | 710 | result = 0; |
1da177e4 LT |
711 | if (outl) { |
712 | if (copy_to_user(user.data, | |
713 | server->priv.data, | |
2e54eb96 PV |
714 | outl)) |
715 | result = -EFAULT; | |
1da177e4 | 716 | } |
2e54eb96 PV |
717 | up_read(&server->auth_rwsem); |
718 | if (result) | |
719 | return result; | |
54f67f63 PV |
720 | #ifdef CONFIG_COMPAT |
721 | if (cmd == NCP_IOC_GETPRIVATEDATA_32) { | |
722 | struct compat_ncp_privatedata_ioctl user32; | |
723 | user32.len = user.len; | |
724 | user32.data = (unsigned long) user.data; | |
5c09d96b | 725 | if (copy_to_user(argp, &user32, sizeof(user32))) |
54f67f63 PV |
726 | return -EFAULT; |
727 | } else | |
728 | #endif | |
1da177e4 LT |
729 | if (copy_to_user(argp, &user, sizeof(user))) |
730 | return -EFAULT; | |
54f67f63 | 731 | |
1da177e4 LT |
732 | return 0; |
733 | } | |
48937024 | 734 | |
54f67f63 PV |
735 | #ifdef CONFIG_COMPAT |
736 | case NCP_IOC_SETPRIVATEDATA_32: | |
737 | #endif | |
1da177e4 | 738 | case NCP_IOC_SETPRIVATEDATA: |
1da177e4 LT |
739 | { |
740 | struct ncp_privatedata_ioctl user; | |
741 | void* new; | |
742 | void* old; | |
743 | size_t oldlen; | |
744 | ||
54f67f63 PV |
745 | #ifdef CONFIG_COMPAT |
746 | if (cmd == NCP_IOC_SETPRIVATEDATA_32) { | |
747 | struct compat_ncp_privatedata_ioctl user32; | |
748 | if (copy_from_user(&user32, argp, sizeof(user32))) | |
749 | return -EFAULT; | |
750 | user.len = user32.len; | |
751 | user.data = compat_ptr(user32.data); | |
752 | } else | |
753 | #endif | |
1da177e4 LT |
754 | if (copy_from_user(&user, argp, sizeof(user))) |
755 | return -EFAULT; | |
54f67f63 | 756 | |
1da177e4 LT |
757 | if (user.len > NCP_PRIVATE_DATA_MAX_LEN) |
758 | return -ENOMEM; | |
759 | if (user.len) { | |
a9482ebc LZ |
760 | new = memdup_user(user.data, user.len); |
761 | if (IS_ERR(new)) | |
762 | return PTR_ERR(new); | |
1da177e4 LT |
763 | } else { |
764 | new = NULL; | |
765 | } | |
2e54eb96 | 766 | down_write(&server->auth_rwsem); |
1da177e4 LT |
767 | old = server->priv.data; |
768 | oldlen = server->priv.len; | |
769 | server->priv.len = user.len; | |
770 | server->priv.data = new; | |
2e54eb96 | 771 | up_write(&server->auth_rwsem); |
44db77f3 | 772 | kfree(old); |
1da177e4 LT |
773 | return 0; |
774 | } | |
775 | ||
776 | #ifdef CONFIG_NCPFS_NLS | |
777 | case NCP_IOC_SETCHARSETS: | |
778 | return ncp_set_charsets(server, argp); | |
2e54eb96 | 779 | |
1da177e4 LT |
780 | case NCP_IOC_GETCHARSETS: |
781 | return ncp_get_charsets(server, argp); | |
782 | ||
783 | #endif /* CONFIG_NCPFS_NLS */ | |
784 | ||
785 | case NCP_IOC_SETDENTRYTTL: | |
1da177e4 LT |
786 | { |
787 | u_int32_t user; | |
788 | ||
789 | if (copy_from_user(&user, argp, sizeof(user))) | |
790 | return -EFAULT; | |
791 | /* 20 secs at most... */ | |
792 | if (user > 20000) | |
793 | return -EINVAL; | |
794 | user = (user * HZ) / 1000; | |
2e54eb96 | 795 | atomic_set(&server->dentry_ttl, user); |
1da177e4 LT |
796 | return 0; |
797 | } | |
2e54eb96 | 798 | |
1da177e4 LT |
799 | case NCP_IOC_GETDENTRYTTL: |
800 | { | |
2e54eb96 | 801 | u_int32_t user = (atomic_read(&server->dentry_ttl) * 1000) / HZ; |
1da177e4 LT |
802 | if (copy_to_user(argp, &user, sizeof(user))) |
803 | return -EFAULT; | |
804 | return 0; | |
805 | } | |
806 | ||
807 | } | |
1da177e4 LT |
808 | return -EINVAL; |
809 | } | |
54f67f63 | 810 | |
2e54eb96 | 811 | long ncp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
a761a1c0 | 812 | { |
496ad9aa | 813 | struct inode *inode = file_inode(filp); |
2e54eb96 | 814 | struct ncp_server *server = NCP_SERVER(inode); |
1ac7fd81 | 815 | kuid_t uid = current_uid(); |
2e54eb96 PV |
816 | int need_drop_write = 0; |
817 | long ret; | |
818 | ||
a761a1c0 | 819 | switch (cmd) { |
a761a1c0 | 820 | case NCP_IOC_SETCHARSETS: |
a761a1c0 | 821 | case NCP_IOC_CONN_LOGGED_IN: |
a761a1c0 | 822 | case NCP_IOC_SETROOT: |
2e54eb96 | 823 | if (!capable(CAP_SYS_ADMIN)) { |
41735818 | 824 | ret = -EPERM; |
2e54eb96 PV |
825 | goto out; |
826 | } | |
827 | break; | |
a761a1c0 | 828 | } |
1ac7fd81 | 829 | if (!uid_eq(server->m.mounted_uid, uid)) { |
2e54eb96 | 830 | switch (cmd) { |
a761a1c0 | 831 | /* |
2e54eb96 PV |
832 | * Only mount owner can issue these ioctls. Information |
833 | * necessary to authenticate to other NDS servers are | |
834 | * stored here. | |
a761a1c0 | 835 | */ |
2e54eb96 PV |
836 | case NCP_IOC_GETOBJECTNAME: |
837 | case NCP_IOC_SETOBJECTNAME: | |
838 | case NCP_IOC_GETPRIVATEDATA: | |
839 | case NCP_IOC_SETPRIVATEDATA: | |
840 | #ifdef CONFIG_COMPAT | |
841 | case NCP_IOC_GETOBJECTNAME_32: | |
842 | case NCP_IOC_SETOBJECTNAME_32: | |
843 | case NCP_IOC_GETPRIVATEDATA_32: | |
844 | case NCP_IOC_SETPRIVATEDATA_32: | |
845 | #endif | |
93d84b6d JK |
846 | ret = -EACCES; |
847 | goto out; | |
2e54eb96 PV |
848 | /* |
849 | * These require write access on the inode if user id | |
850 | * does not match. Note that they do not write to the | |
851 | * file... But old code did mnt_want_write, so I keep | |
852 | * it as is. Of course not for mountpoint owner, as | |
853 | * that breaks read-only mounts altogether as ncpmount | |
854 | * needs working NCP_IOC_NCPREQUEST and | |
855 | * NCP_IOC_GET_FS_INFO. Some of these codes (setdentryttl, | |
856 | * signinit, setsignwanted) should be probably restricted | |
857 | * to owner only, or even more to CAP_SYS_ADMIN). | |
858 | */ | |
859 | case NCP_IOC_GET_FS_INFO: | |
860 | case NCP_IOC_GET_FS_INFO_V2: | |
861 | case NCP_IOC_NCPREQUEST: | |
862 | case NCP_IOC_SETDENTRYTTL: | |
863 | case NCP_IOC_SIGN_INIT: | |
864 | case NCP_IOC_LOCKUNLOCK: | |
865 | case NCP_IOC_SET_SIGN_WANTED: | |
866 | #ifdef CONFIG_COMPAT | |
867 | case NCP_IOC_GET_FS_INFO_V2_32: | |
868 | case NCP_IOC_NCPREQUEST_32: | |
869 | #endif | |
870 | ret = mnt_want_write_file(filp); | |
871 | if (ret) | |
872 | goto out; | |
873 | need_drop_write = 1; | |
874 | ret = inode_permission(inode, MAY_WRITE); | |
875 | if (ret) | |
876 | goto outDropWrite; | |
877 | break; | |
878 | /* | |
879 | * Read access required. | |
880 | */ | |
881 | case NCP_IOC_GETMOUNTUID16: | |
882 | case NCP_IOC_GETMOUNTUID32: | |
883 | case NCP_IOC_GETMOUNTUID64: | |
884 | case NCP_IOC_GETROOT: | |
885 | case NCP_IOC_SIGN_WANTED: | |
886 | ret = inode_permission(inode, MAY_READ); | |
887 | if (ret) | |
888 | goto out; | |
889 | break; | |
890 | /* | |
891 | * Anybody can read these. | |
892 | */ | |
893 | case NCP_IOC_GETCHARSETS: | |
894 | case NCP_IOC_GETDENTRYTTL: | |
895 | default: | |
896 | /* Three codes below are protected by CAP_SYS_ADMIN above. */ | |
897 | case NCP_IOC_SETCHARSETS: | |
898 | case NCP_IOC_CONN_LOGGED_IN: | |
899 | case NCP_IOC_SETROOT: | |
900 | break; | |
93d84b6d | 901 | } |
a761a1c0 | 902 | } |
2e54eb96 PV |
903 | ret = __ncp_ioctl(inode, cmd, arg); |
904 | outDropWrite: | |
905 | if (need_drop_write) | |
2a79f17e | 906 | mnt_drop_write_file(filp); |
93d84b6d | 907 | out: |
a761a1c0 DH |
908 | return ret; |
909 | } | |
910 | ||
54f67f63 PV |
911 | #ifdef CONFIG_COMPAT |
912 | long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
913 | { | |
93d84b6d | 914 | long ret; |
54f67f63 | 915 | |
54f67f63 | 916 | arg = (unsigned long) compat_ptr(arg); |
93d84b6d | 917 | ret = ncp_ioctl(file, cmd, arg); |
54f67f63 PV |
918 | return ret; |
919 | } | |
920 | #endif |