blob: 8843a83d4ef03dc4242dd11646c78480736375cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080010#include <linux/capability.h>
Petr Vandrovec54f67f62006-09-30 23:27:55 -070011#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/fs.h>
14#include <linux/ioctl.h>
15#include <linux/time.h>
16#include <linux/mm.h>
17#include <linux/highuid.h>
Petr Vandrovec54f67f62006-09-30 23:27:55 -070018#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/vmalloc.h>
20
21#include <linux/ncp_fs.h>
22
Petr Vandrovec54f67f62006-09-30 23:27:55 -070023#include <asm/uaccess.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "ncplib_kernel.h"
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
34static int
Christoph Hellwig8c744fb2005-11-08 21:35:04 -080035ncp_get_fs_info(struct ncp_server * server, struct file *file,
36 struct ncp_fs_info __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Josef Sipek92e5bae2006-12-08 02:37:22 -080038 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct ncp_fs_info info;
40
Christoph Hellwig8c744fb2005-11-08 21:35:04 -080041 if ((file_permission(file, MAY_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 && (current->uid != server->m.mounted_uid)) {
43 return -EACCES;
44 }
45 if (copy_from_user(&info, arg, sizeof(info)))
46 return -EFAULT;
47
48 if (info.version != NCP_GET_FS_INFO_VERSION) {
49 DPRINTK("info.version invalid: %d\n", info.version);
50 return -EINVAL;
51 }
52 /* TODO: info.addr = server->m.serv_addr; */
53 SET_UID(info.mounted_uid, server->m.mounted_uid);
54 info.connection = server->connection;
55 info.buffer_size = server->buffer_size;
56 info.volume_number = NCP_FINFO(inode)->volNumber;
57 info.directory_id = NCP_FINFO(inode)->DosDirNum;
58
59 if (copy_to_user(arg, &info, sizeof(info)))
60 return -EFAULT;
61 return 0;
62}
63
64static int
Christoph Hellwig8c744fb2005-11-08 21:35:04 -080065ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
66 struct ncp_fs_info_v2 __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Josef Sipek92e5bae2006-12-08 02:37:22 -080068 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct ncp_fs_info_v2 info2;
70
Christoph Hellwig8c744fb2005-11-08 21:35:04 -080071 if ((file_permission(file, MAY_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 && (current->uid != server->m.mounted_uid)) {
73 return -EACCES;
74 }
75 if (copy_from_user(&info2, arg, sizeof(info2)))
76 return -EFAULT;
77
78 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
79 DPRINTK("info.version invalid: %d\n", info2.version);
80 return -EINVAL;
81 }
82 info2.mounted_uid = server->m.mounted_uid;
83 info2.connection = server->connection;
84 info2.buffer_size = server->buffer_size;
85 info2.volume_number = NCP_FINFO(inode)->volNumber;
86 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
87 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
88
89 if (copy_to_user(arg, &info2, sizeof(info2)))
90 return -EFAULT;
91 return 0;
92}
93
Petr Vandrovec54f67f62006-09-30 23:27:55 -070094#ifdef CONFIG_COMPAT
95struct compat_ncp_objectname_ioctl
96{
97 s32 auth_type;
98 u32 object_name_len;
99 compat_caddr_t object_name; /* an userspace data, in most cases user name */
100};
101
102struct compat_ncp_fs_info_v2 {
103 s32 version;
104 u32 mounted_uid;
105 u32 connection;
106 u32 buffer_size;
107
108 u32 volume_number;
109 u32 directory_id;
110
111 u32 dummy1;
112 u32 dummy2;
113 u32 dummy3;
114};
115
116struct compat_ncp_ioctl_request {
117 u32 function;
118 u32 size;
119 compat_caddr_t data;
120};
121
122struct compat_ncp_privatedata_ioctl
123{
124 u32 len;
125 compat_caddr_t data; /* ~1000 for NDS */
126};
127
128#define NCP_IOC_GET_FS_INFO_V2_32 _IOWR('n', 4, struct compat_ncp_fs_info_v2)
129#define NCP_IOC_NCPREQUEST_32 _IOR('n', 1, struct compat_ncp_ioctl_request)
130#define NCP_IOC_GETOBJECTNAME_32 _IOWR('n', 9, struct compat_ncp_objectname_ioctl)
131#define NCP_IOC_SETOBJECTNAME_32 _IOR('n', 9, struct compat_ncp_objectname_ioctl)
132#define NCP_IOC_GETPRIVATEDATA_32 _IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
133#define NCP_IOC_SETPRIVATEDATA_32 _IOR('n', 10, struct compat_ncp_privatedata_ioctl)
134
135static int
136ncp_get_compat_fs_info_v2(struct ncp_server * server, struct file *file,
137 struct compat_ncp_fs_info_v2 __user * arg)
138{
Josef Sipek92e5bae2006-12-08 02:37:22 -0800139 struct inode *inode = file->f_path.dentry->d_inode;
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700140 struct compat_ncp_fs_info_v2 info2;
141
142 if ((file_permission(file, MAY_WRITE) != 0)
143 && (current->uid != server->m.mounted_uid)) {
144 return -EACCES;
145 }
146 if (copy_from_user(&info2, arg, sizeof(info2)))
147 return -EFAULT;
148
149 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
150 DPRINTK("info.version invalid: %d\n", info2.version);
151 return -EINVAL;
152 }
153 info2.mounted_uid = server->m.mounted_uid;
154 info2.connection = server->connection;
155 info2.buffer_size = server->buffer_size;
156 info2.volume_number = NCP_FINFO(inode)->volNumber;
157 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
158 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
159
160 if (copy_to_user(arg, &info2, sizeof(info2)))
161 return -EFAULT;
162 return 0;
163}
164#endif
165
166#define NCP_IOC_GETMOUNTUID16 _IOW('n', 2, u16)
167#define NCP_IOC_GETMOUNTUID32 _IOW('n', 2, u32)
168#define NCP_IOC_GETMOUNTUID64 _IOW('n', 2, u64)
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#ifdef CONFIG_NCPFS_NLS
171/* Here we are select the iocharset and the codepage for NLS.
172 * Thanks Petr Vandrovec for idea and many hints.
173 */
174static int
175ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
176{
177 struct ncp_nls_ioctl user;
178 struct nls_table *codepage;
179 struct nls_table *iocharset;
180 struct nls_table *oldset_io;
181 struct nls_table *oldset_cp;
182
183 if (!capable(CAP_SYS_ADMIN))
184 return -EACCES;
185 if (server->root_setuped)
186 return -EBUSY;
187
188 if (copy_from_user(&user, arg, sizeof(user)))
189 return -EFAULT;
190
191 codepage = NULL;
192 user.codepage[NCP_IOCSNAME_LEN] = 0;
193 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
194 codepage = load_nls_default();
195 else {
196 codepage = load_nls(user.codepage);
197 if (!codepage) {
198 return -EBADRQC;
199 }
200 }
201
202 iocharset = NULL;
203 user.iocharset[NCP_IOCSNAME_LEN] = 0;
204 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
205 iocharset = load_nls_default();
206 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
207 } else if (!strcmp(user.iocharset, "utf8")) {
208 iocharset = load_nls_default();
209 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
210 } else {
211 iocharset = load_nls(user.iocharset);
212 if (!iocharset) {
213 unload_nls(codepage);
214 return -EBADRQC;
215 }
216 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
217 }
218
219 oldset_cp = server->nls_vol;
220 server->nls_vol = codepage;
221 oldset_io = server->nls_io;
222 server->nls_io = iocharset;
223
224 if (oldset_cp)
225 unload_nls(oldset_cp);
226 if (oldset_io)
227 unload_nls(oldset_io);
228
229 return 0;
230}
231
232static int
233ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
234{
235 struct ncp_nls_ioctl user;
236 int len;
237
238 memset(&user, 0, sizeof(user));
239 if (server->nls_vol && server->nls_vol->charset) {
240 len = strlen(server->nls_vol->charset);
241 if (len > NCP_IOCSNAME_LEN)
242 len = NCP_IOCSNAME_LEN;
243 strncpy(user.codepage, server->nls_vol->charset, len);
244 user.codepage[len] = 0;
245 }
246
247 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
248 strcpy(user.iocharset, "utf8");
249 else if (server->nls_io && server->nls_io->charset) {
250 len = strlen(server->nls_io->charset);
251 if (len > NCP_IOCSNAME_LEN)
252 len = NCP_IOCSNAME_LEN;
253 strncpy(user.iocharset, server->nls_io->charset, len);
254 user.iocharset[len] = 0;
255 }
256
257 if (copy_to_user(arg, &user, sizeof(user)))
258 return -EFAULT;
259 return 0;
260}
261#endif /* CONFIG_NCPFS_NLS */
262
263int ncp_ioctl(struct inode *inode, struct file *filp,
264 unsigned int cmd, unsigned long arg)
265{
266 struct ncp_server *server = NCP_SERVER(inode);
267 int result;
268 struct ncp_ioctl_request request;
269 char* bouncebuffer;
270 void __user *argp = (void __user *)arg;
271
272 switch (cmd) {
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700273#ifdef CONFIG_COMPAT
274 case NCP_IOC_NCPREQUEST_32:
275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case NCP_IOC_NCPREQUEST:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800277 if ((file_permission(filp, MAY_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 && (current->uid != server->m.mounted_uid)) {
279 return -EACCES;
280 }
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700281#ifdef CONFIG_COMPAT
282 if (cmd == NCP_IOC_NCPREQUEST_32) {
283 struct compat_ncp_ioctl_request request32;
284 if (copy_from_user(&request32, argp, sizeof(request32)))
285 return -EFAULT;
286 request.function = request32.function;
287 request.size = request32.size;
288 request.data = compat_ptr(request32.data);
289 } else
290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (copy_from_user(&request, argp, sizeof(request)))
292 return -EFAULT;
293
294 if ((request.function > 255)
295 || (request.size >
296 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
297 return -EINVAL;
298 }
299 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
300 if (!bouncebuffer)
301 return -ENOMEM;
302 if (copy_from_user(bouncebuffer, request.data, request.size)) {
303 vfree(bouncebuffer);
304 return -EFAULT;
305 }
306 ncp_lock_server(server);
307
308 /* FIXME: We hack around in the server's structures
309 here to be able to use ncp_request */
310
311 server->has_subfunction = 0;
312 server->current_size = request.size;
313 memcpy(server->packet, bouncebuffer, request.size);
314
315 result = ncp_request2(server, request.function,
316 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
317 if (result < 0)
318 result = -EIO;
319 else
320 result = server->reply_size;
321 ncp_unlock_server(server);
322 DPRINTK("ncp_ioctl: copy %d bytes\n",
323 result);
324 if (result >= 0)
325 if (copy_to_user(request.data, bouncebuffer, result))
326 result = -EFAULT;
327 vfree(bouncebuffer);
328 return result;
329
330 case NCP_IOC_CONN_LOGGED_IN:
331
332 if (!capable(CAP_SYS_ADMIN))
333 return -EACCES;
334 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
335 return -EINVAL;
336 if (server->root_setuped)
337 return -EBUSY;
338 server->root_setuped = 1;
339 return ncp_conn_logged_in(inode->i_sb);
340
341 case NCP_IOC_GET_FS_INFO:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800342 return ncp_get_fs_info(server, filp, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 case NCP_IOC_GET_FS_INFO_V2:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800345 return ncp_get_fs_info_v2(server, filp, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700347#ifdef CONFIG_COMPAT
348 case NCP_IOC_GET_FS_INFO_V2_32:
349 return ncp_get_compat_fs_info_v2(server, filp, argp);
350#endif
351 /* we have too many combinations of CONFIG_COMPAT,
352 * CONFIG_64BIT and CONFIG_UID16, so just handle
353 * any of the possible ioctls */
354 case NCP_IOC_GETMOUNTUID16:
355 case NCP_IOC_GETMOUNTUID32:
356 case NCP_IOC_GETMOUNTUID64:
357 if ((file_permission(filp, MAY_READ) != 0)
358 && (current->uid != server->m.mounted_uid)) {
359 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700361 if (cmd == NCP_IOC_GETMOUNTUID16) {
362 u16 uid;
363 SET_UID(uid, server->m.mounted_uid);
364 if (put_user(uid, (u16 __user *)argp))
365 return -EFAULT;
366 } else if (cmd == NCP_IOC_GETMOUNTUID32) {
367 if (put_user(server->m.mounted_uid,
368 (u32 __user *)argp))
369 return -EFAULT;
370 } else {
371 if (put_user(server->m.mounted_uid,
372 (u64 __user *)argp))
373 return -EFAULT;
374 }
375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 case NCP_IOC_GETROOT:
378 {
379 struct ncp_setroot_ioctl sr;
380
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800381 if ((file_permission(filp, MAY_READ) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 && (current->uid != server->m.mounted_uid))
383 {
384 return -EACCES;
385 }
386 if (server->m.mounted_vol[0]) {
387 struct dentry* dentry = inode->i_sb->s_root;
388
389 if (dentry) {
390 struct inode* inode = dentry->d_inode;
391
392 if (inode) {
393 sr.volNumber = NCP_FINFO(inode)->volNumber;
394 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
395 sr.namespace = server->name_space[sr.volNumber];
396 } else
397 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
398 } else
399 DPRINTK("ncpfs: s_root==NULL\n");
400 } else {
401 sr.volNumber = -1;
402 sr.namespace = 0;
403 sr.dirEntNum = 0;
404 }
405 if (copy_to_user(argp, &sr, sizeof(sr)))
406 return -EFAULT;
407 return 0;
408 }
409 case NCP_IOC_SETROOT:
410 {
411 struct ncp_setroot_ioctl sr;
412 __u32 vnum;
413 __le32 de;
414 __le32 dosde;
415 struct dentry* dentry;
416
417 if (!capable(CAP_SYS_ADMIN))
418 {
419 return -EACCES;
420 }
421 if (server->root_setuped) return -EBUSY;
422 if (copy_from_user(&sr, argp, sizeof(sr)))
423 return -EFAULT;
424 if (sr.volNumber < 0) {
425 server->m.mounted_vol[0] = 0;
426 vnum = NCP_NUMBER_OF_VOLUMES;
427 de = 0;
428 dosde = 0;
429 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
430 return -EINVAL;
431 } else if (ncp_mount_subdir(server, sr.volNumber,
432 sr.namespace, sr.dirEntNum,
433 &vnum, &de, &dosde)) {
434 return -ENOENT;
435 }
436
437 dentry = inode->i_sb->s_root;
438 server->root_setuped = 1;
439 if (dentry) {
440 struct inode* inode = dentry->d_inode;
441
442 if (inode) {
443 NCP_FINFO(inode)->volNumber = vnum;
444 NCP_FINFO(inode)->dirEntNum = de;
445 NCP_FINFO(inode)->DosDirNum = dosde;
446 } else
447 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
448 } else
449 DPRINTK("ncpfs: s_root==NULL\n");
450
451 return 0;
452 }
453
454#ifdef CONFIG_NCPFS_PACKET_SIGNING
455 case NCP_IOC_SIGN_INIT:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800456 if ((file_permission(filp, MAY_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 && (current->uid != server->m.mounted_uid))
458 {
459 return -EACCES;
460 }
461 if (argp) {
462 if (server->sign_wanted)
463 {
464 struct ncp_sign_init sign;
465
466 if (copy_from_user(&sign, argp, sizeof(sign)))
467 return -EFAULT;
468 memcpy(server->sign_root,sign.sign_root,8);
469 memcpy(server->sign_last,sign.sign_last,16);
470 server->sign_active = 1;
471 }
472 /* ignore when signatures not wanted */
473 } else {
474 server->sign_active = 0;
475 }
476 return 0;
477
478 case NCP_IOC_SIGN_WANTED:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800479 if ((file_permission(filp, MAY_READ) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 && (current->uid != server->m.mounted_uid))
481 {
482 return -EACCES;
483 }
484
485 if (put_user(server->sign_wanted, (int __user *)argp))
486 return -EFAULT;
487 return 0;
488 case NCP_IOC_SET_SIGN_WANTED:
489 {
490 int newstate;
491
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800492 if ((file_permission(filp, MAY_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 && (current->uid != server->m.mounted_uid))
494 {
495 return -EACCES;
496 }
497 /* get only low 8 bits... */
498 if (get_user(newstate, (unsigned char __user *)argp))
499 return -EFAULT;
500 if (server->sign_active) {
501 /* cannot turn signatures OFF when active */
502 if (!newstate) return -EINVAL;
503 } else {
504 server->sign_wanted = newstate != 0;
505 }
506 return 0;
507 }
508
509#endif /* CONFIG_NCPFS_PACKET_SIGNING */
510
511#ifdef CONFIG_NCPFS_IOCTL_LOCKING
512 case NCP_IOC_LOCKUNLOCK:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800513 if ((file_permission(filp, MAY_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 && (current->uid != server->m.mounted_uid))
515 {
516 return -EACCES;
517 }
518 {
519 struct ncp_lock_ioctl rqdata;
520 int result;
521
522 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
523 return -EFAULT;
524 if (rqdata.origin != 0)
525 return -EINVAL;
526 /* check for cmd */
527 switch (rqdata.cmd) {
528 case NCP_LOCK_EX:
529 case NCP_LOCK_SH:
530 if (rqdata.timeout == 0)
531 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
532 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
533 rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
534 break;
535 case NCP_LOCK_LOG:
536 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
537 case NCP_LOCK_CLEAR:
538 break;
539 default:
540 return -EINVAL;
541 }
542 /* locking needs both read and write access */
543 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
544 {
545 return result;
546 }
547 result = -EIO;
548 if (!ncp_conn_valid(server))
549 goto outrel;
550 result = -EISDIR;
551 if (!S_ISREG(inode->i_mode))
552 goto outrel;
553 if (rqdata.cmd == NCP_LOCK_CLEAR)
554 {
555 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
556 NCP_FINFO(inode)->file_handle,
557 rqdata.offset,
558 rqdata.length);
559 if (result > 0) result = 0; /* no such lock */
560 }
561 else
562 {
563 int lockcmd;
564
565 switch (rqdata.cmd)
566 {
567 case NCP_LOCK_EX: lockcmd=1; break;
568 case NCP_LOCK_SH: lockcmd=3; break;
569 default: lockcmd=0; break;
570 }
571 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
572 NCP_FINFO(inode)->file_handle,
573 lockcmd,
574 rqdata.offset,
575 rqdata.length,
576 rqdata.timeout);
577 if (result > 0) result = -EAGAIN;
578 }
579outrel:
580 ncp_inode_close(inode);
581 return result;
582 }
583#endif /* CONFIG_NCPFS_IOCTL_LOCKING */
584
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700585#ifdef CONFIG_COMPAT
586 case NCP_IOC_GETOBJECTNAME_32:
587 if (current->uid != server->m.mounted_uid) {
588 return -EACCES;
589 }
590 {
591 struct compat_ncp_objectname_ioctl user;
592 size_t outl;
593
594 if (copy_from_user(&user, argp, sizeof(user)))
595 return -EFAULT;
596 user.auth_type = server->auth.auth_type;
597 outl = user.object_name_len;
598 user.object_name_len = server->auth.object_name_len;
599 if (outl > user.object_name_len)
600 outl = user.object_name_len;
601 if (outl) {
602 if (copy_to_user(compat_ptr(user.object_name),
603 server->auth.object_name,
604 outl)) return -EFAULT;
605 }
606 if (copy_to_user(argp, &user, sizeof(user)))
607 return -EFAULT;
608 return 0;
609 }
610#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 case NCP_IOC_GETOBJECTNAME:
612 if (current->uid != server->m.mounted_uid) {
613 return -EACCES;
614 }
615 {
616 struct ncp_objectname_ioctl user;
617 size_t outl;
618
619 if (copy_from_user(&user, argp, sizeof(user)))
620 return -EFAULT;
621 user.auth_type = server->auth.auth_type;
622 outl = user.object_name_len;
623 user.object_name_len = server->auth.object_name_len;
624 if (outl > user.object_name_len)
625 outl = user.object_name_len;
626 if (outl) {
627 if (copy_to_user(user.object_name,
628 server->auth.object_name,
629 outl)) return -EFAULT;
630 }
631 if (copy_to_user(argp, &user, sizeof(user)))
632 return -EFAULT;
633 return 0;
634 }
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700635#ifdef CONFIG_COMPAT
636 case NCP_IOC_SETOBJECTNAME_32:
637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 case NCP_IOC_SETOBJECTNAME:
639 if (current->uid != server->m.mounted_uid) {
640 return -EACCES;
641 }
642 {
643 struct ncp_objectname_ioctl user;
644 void* newname;
645 void* oldname;
646 size_t oldnamelen;
647 void* oldprivate;
648 size_t oldprivatelen;
649
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700650#ifdef CONFIG_COMPAT
651 if (cmd == NCP_IOC_SETOBJECTNAME_32) {
652 struct compat_ncp_objectname_ioctl user32;
653 if (copy_from_user(&user32, argp, sizeof(user32)))
654 return -EFAULT;
655 user.auth_type = user32.auth_type;
656 user.object_name_len = user32.object_name_len;
657 user.object_name = compat_ptr(user32.object_name);
658 } else
659#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (copy_from_user(&user, argp, sizeof(user)))
661 return -EFAULT;
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
664 return -ENOMEM;
665 if (user.object_name_len) {
Pekka Enberg44db77f2006-01-14 13:21:12 -0800666 newname = kmalloc(user.object_name_len, GFP_USER);
667 if (!newname)
668 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
Pekka Enberg44db77f2006-01-14 13:21:12 -0800670 kfree(newname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -EFAULT;
672 }
673 } else {
674 newname = NULL;
675 }
676 /* enter critical section */
677 /* maybe that kfree can sleep so do that this way */
678 /* it is at least more SMP friendly (in future...) */
679 oldname = server->auth.object_name;
680 oldnamelen = server->auth.object_name_len;
681 oldprivate = server->priv.data;
682 oldprivatelen = server->priv.len;
683 server->auth.auth_type = user.auth_type;
684 server->auth.object_name_len = user.object_name_len;
685 server->auth.object_name = newname;
686 server->priv.len = 0;
687 server->priv.data = NULL;
688 /* leave critical section */
Pekka Enberg44db77f2006-01-14 13:21:12 -0800689 kfree(oldprivate);
690 kfree(oldname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692 }
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700693#ifdef CONFIG_COMPAT
694 case NCP_IOC_GETPRIVATEDATA_32:
695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case NCP_IOC_GETPRIVATEDATA:
697 if (current->uid != server->m.mounted_uid) {
698 return -EACCES;
699 }
700 {
701 struct ncp_privatedata_ioctl user;
702 size_t outl;
703
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700704#ifdef CONFIG_COMPAT
705 if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
706 struct compat_ncp_privatedata_ioctl user32;
707 if (copy_from_user(&user32, argp, sizeof(user32)))
708 return -EFAULT;
709 user.len = user32.len;
710 user.data = compat_ptr(user32.data);
711 } else
712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (copy_from_user(&user, argp, sizeof(user)))
714 return -EFAULT;
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 outl = user.len;
717 user.len = server->priv.len;
718 if (outl > user.len) outl = user.len;
719 if (outl) {
720 if (copy_to_user(user.data,
721 server->priv.data,
722 outl)) return -EFAULT;
723 }
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700724#ifdef CONFIG_COMPAT
725 if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
726 struct compat_ncp_privatedata_ioctl user32;
727 user32.len = user.len;
728 user32.data = (unsigned long) user.data;
Al Viro5c09d962006-10-09 20:24:49 +0100729 if (copy_to_user(argp, &user32, sizeof(user32)))
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700730 return -EFAULT;
731 } else
732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (copy_to_user(argp, &user, sizeof(user)))
734 return -EFAULT;
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737 }
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700738#ifdef CONFIG_COMPAT
739 case NCP_IOC_SETPRIVATEDATA_32:
740#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case NCP_IOC_SETPRIVATEDATA:
742 if (current->uid != server->m.mounted_uid) {
743 return -EACCES;
744 }
745 {
746 struct ncp_privatedata_ioctl user;
747 void* new;
748 void* old;
749 size_t oldlen;
750
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700751#ifdef CONFIG_COMPAT
752 if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
753 struct compat_ncp_privatedata_ioctl user32;
754 if (copy_from_user(&user32, argp, sizeof(user32)))
755 return -EFAULT;
756 user.len = user32.len;
757 user.data = compat_ptr(user32.data);
758 } else
759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (copy_from_user(&user, argp, sizeof(user)))
761 return -EFAULT;
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
764 return -ENOMEM;
765 if (user.len) {
Pekka Enberg44db77f2006-01-14 13:21:12 -0800766 new = kmalloc(user.len, GFP_USER);
767 if (!new)
768 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (copy_from_user(new, user.data, user.len)) {
Pekka Enberg44db77f2006-01-14 13:21:12 -0800770 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -EFAULT;
772 }
773 } else {
774 new = NULL;
775 }
776 /* enter critical section */
777 old = server->priv.data;
778 oldlen = server->priv.len;
779 server->priv.len = user.len;
780 server->priv.data = new;
781 /* leave critical section */
Pekka Enberg44db77f2006-01-14 13:21:12 -0800782 kfree(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784 }
785
786#ifdef CONFIG_NCPFS_NLS
787 case NCP_IOC_SETCHARSETS:
788 return ncp_set_charsets(server, argp);
789
790 case NCP_IOC_GETCHARSETS:
791 return ncp_get_charsets(server, argp);
792
793#endif /* CONFIG_NCPFS_NLS */
794
795 case NCP_IOC_SETDENTRYTTL:
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800796 if ((file_permission(filp, MAY_WRITE) != 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 (current->uid != server->m.mounted_uid))
798 return -EACCES;
799 {
800 u_int32_t user;
801
802 if (copy_from_user(&user, argp, sizeof(user)))
803 return -EFAULT;
804 /* 20 secs at most... */
805 if (user > 20000)
806 return -EINVAL;
807 user = (user * HZ) / 1000;
808 server->dentry_ttl = user;
809 return 0;
810 }
811
812 case NCP_IOC_GETDENTRYTTL:
813 {
814 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
815 if (copy_to_user(argp, &user, sizeof(user)))
816 return -EFAULT;
817 return 0;
818 }
819
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return -EINVAL;
822}
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700823
824#ifdef CONFIG_COMPAT
825long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
826{
Josef Sipek92e5bae2006-12-08 02:37:22 -0800827 struct inode *inode = file->f_path.dentry->d_inode;
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700828 int ret;
829
830 lock_kernel();
831 arg = (unsigned long) compat_ptr(arg);
832 ret = ncp_ioctl(inode, file, cmd, arg);
833 unlock_kernel();
834 return ret;
835}
836#endif