Merge branch 's5p-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ncpfs / ioctl.c
blobc2a1f9a155c3a6e8f82800334337b1b1b0aaa65a
1 /*
2 * ioctl.c
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
8 */
10 #include <linux/capability.h>
11 #include <linux/compat.h>
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>
17 #include <linux/mount.h>
18 #include <linux/slab.h>
19 #include <linux/highuid.h>
20 #include <linux/smp_lock.h>
21 #include <linux/vmalloc.h>
22 #include <linux/sched.h>
24 #include <linux/ncp_fs.h>
26 #include <asm/uaccess.h>
28 #include "ncplib_kernel.h"
30 /* maximum limit for ncp_objectname_ioctl */
31 #define NCP_OBJECT_NAME_MAX_LEN 4096
32 /* maximum limit for ncp_privatedata_ioctl */
33 #define NCP_PRIVATE_DATA_MAX_LEN 8192
34 /* maximum negotiable packet size */
35 #define NCP_PACKET_SIZE_INTERNAL 65536
37 static int
38 ncp_get_fs_info(struct ncp_server * server, struct inode *inode,
39 struct ncp_fs_info __user *arg)
41 struct ncp_fs_info info;
43 if (copy_from_user(&info, arg, sizeof(info)))
44 return -EFAULT;
46 if (info.version != NCP_GET_FS_INFO_VERSION) {
47 DPRINTK("info.version invalid: %d\n", info.version);
48 return -EINVAL;
50 /* TODO: info.addr = server->m.serv_addr; */
51 SET_UID(info.mounted_uid, server->m.mounted_uid);
52 info.connection = server->connection;
53 info.buffer_size = server->buffer_size;
54 info.volume_number = NCP_FINFO(inode)->volNumber;
55 info.directory_id = NCP_FINFO(inode)->DosDirNum;
57 if (copy_to_user(arg, &info, sizeof(info)))
58 return -EFAULT;
59 return 0;
62 static int
63 ncp_get_fs_info_v2(struct ncp_server * server, struct inode *inode,
64 struct ncp_fs_info_v2 __user * arg)
66 struct ncp_fs_info_v2 info2;
68 if (copy_from_user(&info2, arg, sizeof(info2)))
69 return -EFAULT;
71 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
72 DPRINTK("info.version invalid: %d\n", info2.version);
73 return -EINVAL;
75 info2.mounted_uid = server->m.mounted_uid;
76 info2.connection = server->connection;
77 info2.buffer_size = server->buffer_size;
78 info2.volume_number = NCP_FINFO(inode)->volNumber;
79 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
80 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
82 if (copy_to_user(arg, &info2, sizeof(info2)))
83 return -EFAULT;
84 return 0;
87 #ifdef CONFIG_COMPAT
88 struct compat_ncp_objectname_ioctl
90 s32 auth_type;
91 u32 object_name_len;
92 compat_caddr_t object_name; /* a userspace data, in most cases user name */
95 struct compat_ncp_fs_info_v2 {
96 s32 version;
97 u32 mounted_uid;
98 u32 connection;
99 u32 buffer_size;
101 u32 volume_number;
102 u32 directory_id;
104 u32 dummy1;
105 u32 dummy2;
106 u32 dummy3;
109 struct compat_ncp_ioctl_request {
110 u32 function;
111 u32 size;
112 compat_caddr_t data;
115 struct compat_ncp_privatedata_ioctl
117 u32 len;
118 compat_caddr_t data; /* ~1000 for NDS */
121 #define NCP_IOC_GET_FS_INFO_V2_32 _IOWR('n', 4, struct compat_ncp_fs_info_v2)
122 #define NCP_IOC_NCPREQUEST_32 _IOR('n', 1, struct compat_ncp_ioctl_request)
123 #define NCP_IOC_GETOBJECTNAME_32 _IOWR('n', 9, struct compat_ncp_objectname_ioctl)
124 #define NCP_IOC_SETOBJECTNAME_32 _IOR('n', 9, struct compat_ncp_objectname_ioctl)
125 #define NCP_IOC_GETPRIVATEDATA_32 _IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
126 #define NCP_IOC_SETPRIVATEDATA_32 _IOR('n', 10, struct compat_ncp_privatedata_ioctl)
128 static int
129 ncp_get_compat_fs_info_v2(struct ncp_server * server, struct inode *inode,
130 struct compat_ncp_fs_info_v2 __user * arg)
132 struct compat_ncp_fs_info_v2 info2;
134 if (copy_from_user(&info2, arg, sizeof(info2)))
135 return -EFAULT;
137 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
138 DPRINTK("info.version invalid: %d\n", info2.version);
139 return -EINVAL;
141 info2.mounted_uid = server->m.mounted_uid;
142 info2.connection = server->connection;
143 info2.buffer_size = server->buffer_size;
144 info2.volume_number = NCP_FINFO(inode)->volNumber;
145 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
146 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
148 if (copy_to_user(arg, &info2, sizeof(info2)))
149 return -EFAULT;
150 return 0;
152 #endif
154 #define NCP_IOC_GETMOUNTUID16 _IOW('n', 2, u16)
155 #define NCP_IOC_GETMOUNTUID32 _IOW('n', 2, u32)
156 #define NCP_IOC_GETMOUNTUID64 _IOW('n', 2, u64)
158 #ifdef CONFIG_NCPFS_NLS
159 /* Here we are select the iocharset and the codepage for NLS.
160 * Thanks Petr Vandrovec for idea and many hints.
162 static int
163 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
165 struct ncp_nls_ioctl user;
166 struct nls_table *codepage;
167 struct nls_table *iocharset;
168 struct nls_table *oldset_io;
169 struct nls_table *oldset_cp;
170 int utf8;
171 int err;
173 if (copy_from_user(&user, arg, sizeof(user)))
174 return -EFAULT;
176 codepage = NULL;
177 user.codepage[NCP_IOCSNAME_LEN] = 0;
178 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
179 codepage = load_nls_default();
180 else {
181 codepage = load_nls(user.codepage);
182 if (!codepage) {
183 return -EBADRQC;
187 iocharset = NULL;
188 user.iocharset[NCP_IOCSNAME_LEN] = 0;
189 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
190 iocharset = load_nls_default();
191 utf8 = 0;
192 } else if (!strcmp(user.iocharset, "utf8")) {
193 iocharset = load_nls_default();
194 utf8 = 1;
195 } else {
196 iocharset = load_nls(user.iocharset);
197 if (!iocharset) {
198 unload_nls(codepage);
199 return -EBADRQC;
201 utf8 = 0;
204 mutex_lock(&server->root_setup_lock);
205 if (server->root_setuped) {
206 oldset_cp = codepage;
207 oldset_io = iocharset;
208 err = -EBUSY;
209 } else {
210 if (utf8)
211 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
212 else
213 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
214 oldset_cp = server->nls_vol;
215 server->nls_vol = codepage;
216 oldset_io = server->nls_io;
217 server->nls_io = iocharset;
218 err = 0;
220 mutex_unlock(&server->root_setup_lock);
221 unload_nls(oldset_cp);
222 unload_nls(oldset_io);
224 return err;
227 static int
228 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
230 struct ncp_nls_ioctl user;
231 int len;
233 memset(&user, 0, sizeof(user));
234 mutex_lock(&server->root_setup_lock);
235 if (server->nls_vol && server->nls_vol->charset) {
236 len = strlen(server->nls_vol->charset);
237 if (len > NCP_IOCSNAME_LEN)
238 len = NCP_IOCSNAME_LEN;
239 strncpy(user.codepage, server->nls_vol->charset, len);
240 user.codepage[len] = 0;
243 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
244 strcpy(user.iocharset, "utf8");
245 else if (server->nls_io && server->nls_io->charset) {
246 len = strlen(server->nls_io->charset);
247 if (len > NCP_IOCSNAME_LEN)
248 len = NCP_IOCSNAME_LEN;
249 strncpy(user.iocharset, server->nls_io->charset, len);
250 user.iocharset[len] = 0;
252 mutex_unlock(&server->root_setup_lock);
254 if (copy_to_user(arg, &user, sizeof(user)))
255 return -EFAULT;
256 return 0;
258 #endif /* CONFIG_NCPFS_NLS */
260 static long __ncp_ioctl(struct inode *inode, unsigned int cmd, unsigned long arg)
262 struct ncp_server *server = NCP_SERVER(inode);
263 int result;
264 struct ncp_ioctl_request request;
265 char* bouncebuffer;
266 void __user *argp = (void __user *)arg;
268 switch (cmd) {
269 #ifdef CONFIG_COMPAT
270 case NCP_IOC_NCPREQUEST_32:
271 #endif
272 case NCP_IOC_NCPREQUEST:
273 #ifdef CONFIG_COMPAT
274 if (cmd == NCP_IOC_NCPREQUEST_32) {
275 struct compat_ncp_ioctl_request request32;
276 if (copy_from_user(&request32, argp, sizeof(request32)))
277 return -EFAULT;
278 request.function = request32.function;
279 request.size = request32.size;
280 request.data = compat_ptr(request32.data);
281 } else
282 #endif
283 if (copy_from_user(&request, argp, sizeof(request)))
284 return -EFAULT;
286 if ((request.function > 255)
287 || (request.size >
288 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
289 return -EINVAL;
291 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
292 if (!bouncebuffer)
293 return -ENOMEM;
294 if (copy_from_user(bouncebuffer, request.data, request.size)) {
295 vfree(bouncebuffer);
296 return -EFAULT;
298 ncp_lock_server(server);
300 /* FIXME: We hack around in the server's structures
301 here to be able to use ncp_request */
303 server->has_subfunction = 0;
304 server->current_size = request.size;
305 memcpy(server->packet, bouncebuffer, request.size);
307 result = ncp_request2(server, request.function,
308 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
309 if (result < 0)
310 result = -EIO;
311 else
312 result = server->reply_size;
313 ncp_unlock_server(server);
314 DPRINTK("ncp_ioctl: copy %d bytes\n",
315 result);
316 if (result >= 0)
317 if (copy_to_user(request.data, bouncebuffer, result))
318 result = -EFAULT;
319 vfree(bouncebuffer);
320 return result;
322 case NCP_IOC_CONN_LOGGED_IN:
324 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
325 return -EINVAL;
326 mutex_lock(&server->root_setup_lock);
327 if (server->root_setuped)
328 result = -EBUSY;
329 else {
330 result = ncp_conn_logged_in(inode->i_sb);
331 if (result == 0)
332 server->root_setuped = 1;
334 mutex_unlock(&server->root_setup_lock);
335 return result;
337 case NCP_IOC_GET_FS_INFO:
338 return ncp_get_fs_info(server, inode, argp);
340 case NCP_IOC_GET_FS_INFO_V2:
341 return ncp_get_fs_info_v2(server, inode, argp);
343 #ifdef CONFIG_COMPAT
344 case NCP_IOC_GET_FS_INFO_V2_32:
345 return ncp_get_compat_fs_info_v2(server, inode, argp);
346 #endif
347 /* we have too many combinations of CONFIG_COMPAT,
348 * CONFIG_64BIT and CONFIG_UID16, so just handle
349 * any of the possible ioctls */
350 case NCP_IOC_GETMOUNTUID16:
352 u16 uid;
354 SET_UID(uid, server->m.mounted_uid);
355 if (put_user(uid, (u16 __user *)argp))
356 return -EFAULT;
357 return 0;
359 case NCP_IOC_GETMOUNTUID32:
360 if (put_user(server->m.mounted_uid,
361 (u32 __user *)argp))
362 return -EFAULT;
363 return 0;
364 case NCP_IOC_GETMOUNTUID64:
365 if (put_user(server->m.mounted_uid,
366 (u64 __user *)argp))
367 return -EFAULT;
368 return 0;
370 case NCP_IOC_GETROOT:
372 struct ncp_setroot_ioctl sr;
374 result = -EACCES;
375 mutex_lock(&server->root_setup_lock);
376 if (server->m.mounted_vol[0]) {
377 struct dentry* dentry = inode->i_sb->s_root;
379 if (dentry) {
380 struct inode* s_inode = dentry->d_inode;
382 if (s_inode) {
383 sr.volNumber = NCP_FINFO(s_inode)->volNumber;
384 sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum;
385 sr.namespace = server->name_space[sr.volNumber];
386 result = 0;
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;
395 result = 0;
397 mutex_unlock(&server->root_setup_lock);
398 if (!result && copy_to_user(argp, &sr, sizeof(sr)))
399 result = -EFAULT;
400 return result;
403 case NCP_IOC_SETROOT:
405 struct ncp_setroot_ioctl sr;
406 __u32 vnum;
407 __le32 de;
408 __le32 dosde;
409 struct dentry* dentry;
411 if (copy_from_user(&sr, argp, sizeof(sr)))
412 return -EFAULT;
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;
429 } else
430 result = 0;
432 if (result == 0) {
433 dentry = inode->i_sb->s_root;
434 if (dentry) {
435 struct inode* s_inode = dentry->d_inode;
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;
446 } else {
447 DPRINTK("ncpfs: s_root==NULL\n");
448 result = -EIO;
451 result = 0;
453 mutex_unlock(&server->root_setup_lock);
455 return result;
458 #ifdef CONFIG_NCPFS_PACKET_SIGNING
459 case NCP_IOC_SIGN_INIT:
461 struct ncp_sign_init sign;
463 if (argp)
464 if (copy_from_user(&sign, argp, sizeof(sign)))
465 return -EFAULT;
466 ncp_lock_server(server);
467 mutex_lock(&server->rcv.creq_mutex);
468 if (argp) {
469 if (server->sign_wanted) {
470 memcpy(server->sign_root,sign.sign_root,8);
471 memcpy(server->sign_last,sign.sign_last,16);
472 server->sign_active = 1;
474 /* ignore when signatures not wanted */
475 } else {
476 server->sign_active = 0;
478 mutex_unlock(&server->rcv.creq_mutex);
479 ncp_unlock_server(server);
480 return 0;
483 case NCP_IOC_SIGN_WANTED:
485 int state;
487 ncp_lock_server(server);
488 state = server->sign_wanted;
489 ncp_unlock_server(server);
490 if (put_user(state, (int __user *)argp))
491 return -EFAULT;
492 return 0;
495 case NCP_IOC_SET_SIGN_WANTED:
497 int newstate;
499 /* get only low 8 bits... */
500 if (get_user(newstate, (unsigned char __user *)argp))
501 return -EFAULT;
502 result = 0;
503 ncp_lock_server(server);
504 if (server->sign_active) {
505 /* cannot turn signatures OFF when active */
506 if (!newstate)
507 result = -EINVAL;
508 } else {
509 server->sign_wanted = newstate != 0;
511 ncp_unlock_server(server);
512 return result;
515 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
517 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
518 case NCP_IOC_LOCKUNLOCK:
520 struct ncp_lock_ioctl rqdata;
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;
542 /* locking needs both read and write access */
543 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
545 return result;
547 result = -EISDIR;
548 if (!S_ISREG(inode->i_mode))
549 goto outrel;
550 if (rqdata.cmd == NCP_LOCK_CLEAR)
552 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
553 NCP_FINFO(inode)->file_handle,
554 rqdata.offset,
555 rqdata.length);
556 if (result > 0) result = 0; /* no such lock */
558 else
560 int lockcmd;
562 switch (rqdata.cmd)
564 case NCP_LOCK_EX: lockcmd=1; break;
565 case NCP_LOCK_SH: lockcmd=3; break;
566 default: lockcmd=0; break;
568 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
569 NCP_FINFO(inode)->file_handle,
570 lockcmd,
571 rqdata.offset,
572 rqdata.length,
573 rqdata.timeout);
574 if (result > 0) result = -EAGAIN;
576 outrel:
577 ncp_inode_close(inode);
578 return result;
580 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
582 #ifdef CONFIG_COMPAT
583 case NCP_IOC_GETOBJECTNAME_32:
585 struct compat_ncp_objectname_ioctl user;
586 size_t outl;
588 if (copy_from_user(&user, argp, sizeof(user)))
589 return -EFAULT;
590 down_read(&server->auth_rwsem);
591 user.auth_type = server->auth.auth_type;
592 outl = user.object_name_len;
593 user.object_name_len = server->auth.object_name_len;
594 if (outl > user.object_name_len)
595 outl = user.object_name_len;
596 result = 0;
597 if (outl) {
598 if (copy_to_user(compat_ptr(user.object_name),
599 server->auth.object_name,
600 outl))
601 result = -EFAULT;
603 up_read(&server->auth_rwsem);
604 if (!result && copy_to_user(argp, &user, sizeof(user)))
605 result = -EFAULT;
606 return result;
608 #endif
610 case NCP_IOC_GETOBJECTNAME:
612 struct ncp_objectname_ioctl user;
613 size_t outl;
615 if (copy_from_user(&user, argp, sizeof(user)))
616 return -EFAULT;
617 down_read(&server->auth_rwsem);
618 user.auth_type = server->auth.auth_type;
619 outl = user.object_name_len;
620 user.object_name_len = server->auth.object_name_len;
621 if (outl > user.object_name_len)
622 outl = user.object_name_len;
623 result = 0;
624 if (outl) {
625 if (copy_to_user(user.object_name,
626 server->auth.object_name,
627 outl))
628 result = -EFAULT;
630 up_read(&server->auth_rwsem);
631 if (!result && copy_to_user(argp, &user, sizeof(user)))
632 result = -EFAULT;
633 return result;
636 #ifdef CONFIG_COMPAT
637 case NCP_IOC_SETOBJECTNAME_32:
638 #endif
639 case NCP_IOC_SETOBJECTNAME:
641 struct ncp_objectname_ioctl user;
642 void* newname;
643 void* oldname;
644 size_t oldnamelen;
645 void* oldprivate;
646 size_t oldprivatelen;
648 #ifdef CONFIG_COMPAT
649 if (cmd == NCP_IOC_SETOBJECTNAME_32) {
650 struct compat_ncp_objectname_ioctl user32;
651 if (copy_from_user(&user32, argp, sizeof(user32)))
652 return -EFAULT;
653 user.auth_type = user32.auth_type;
654 user.object_name_len = user32.object_name_len;
655 user.object_name = compat_ptr(user32.object_name);
656 } else
657 #endif
658 if (copy_from_user(&user, argp, sizeof(user)))
659 return -EFAULT;
661 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
662 return -ENOMEM;
663 if (user.object_name_len) {
664 newname = memdup_user(user.object_name,
665 user.object_name_len);
666 if (IS_ERR(newname))
667 return PTR_ERR(newname);
668 } else {
669 newname = NULL;
671 down_write(&server->auth_rwsem);
672 oldname = server->auth.object_name;
673 oldnamelen = server->auth.object_name_len;
674 oldprivate = server->priv.data;
675 oldprivatelen = server->priv.len;
676 server->auth.auth_type = user.auth_type;
677 server->auth.object_name_len = user.object_name_len;
678 server->auth.object_name = newname;
679 server->priv.len = 0;
680 server->priv.data = NULL;
681 up_write(&server->auth_rwsem);
682 kfree(oldprivate);
683 kfree(oldname);
684 return 0;
687 #ifdef CONFIG_COMPAT
688 case NCP_IOC_GETPRIVATEDATA_32:
689 #endif
690 case NCP_IOC_GETPRIVATEDATA:
692 struct ncp_privatedata_ioctl user;
693 size_t outl;
695 #ifdef CONFIG_COMPAT
696 if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
697 struct compat_ncp_privatedata_ioctl user32;
698 if (copy_from_user(&user32, argp, sizeof(user32)))
699 return -EFAULT;
700 user.len = user32.len;
701 user.data = compat_ptr(user32.data);
702 } else
703 #endif
704 if (copy_from_user(&user, argp, sizeof(user)))
705 return -EFAULT;
707 down_read(&server->auth_rwsem);
708 outl = user.len;
709 user.len = server->priv.len;
710 if (outl > user.len) outl = user.len;
711 result = 0;
712 if (outl) {
713 if (copy_to_user(user.data,
714 server->priv.data,
715 outl))
716 result = -EFAULT;
718 up_read(&server->auth_rwsem);
719 if (result)
720 return result;
721 #ifdef CONFIG_COMPAT
722 if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
723 struct compat_ncp_privatedata_ioctl user32;
724 user32.len = user.len;
725 user32.data = (unsigned long) user.data;
726 if (copy_to_user(argp, &user32, sizeof(user32)))
727 return -EFAULT;
728 } else
729 #endif
730 if (copy_to_user(argp, &user, sizeof(user)))
731 return -EFAULT;
733 return 0;
736 #ifdef CONFIG_COMPAT
737 case NCP_IOC_SETPRIVATEDATA_32:
738 #endif
739 case NCP_IOC_SETPRIVATEDATA:
741 struct ncp_privatedata_ioctl user;
742 void* new;
743 void* old;
744 size_t oldlen;
746 #ifdef CONFIG_COMPAT
747 if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
748 struct compat_ncp_privatedata_ioctl user32;
749 if (copy_from_user(&user32, argp, sizeof(user32)))
750 return -EFAULT;
751 user.len = user32.len;
752 user.data = compat_ptr(user32.data);
753 } else
754 #endif
755 if (copy_from_user(&user, argp, sizeof(user)))
756 return -EFAULT;
758 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
759 return -ENOMEM;
760 if (user.len) {
761 new = memdup_user(user.data, user.len);
762 if (IS_ERR(new))
763 return PTR_ERR(new);
764 } else {
765 new = NULL;
767 down_write(&server->auth_rwsem);
768 old = server->priv.data;
769 oldlen = server->priv.len;
770 server->priv.len = user.len;
771 server->priv.data = new;
772 up_write(&server->auth_rwsem);
773 kfree(old);
774 return 0;
777 #ifdef CONFIG_NCPFS_NLS
778 case NCP_IOC_SETCHARSETS:
779 return ncp_set_charsets(server, argp);
781 case NCP_IOC_GETCHARSETS:
782 return ncp_get_charsets(server, argp);
784 #endif /* CONFIG_NCPFS_NLS */
786 case NCP_IOC_SETDENTRYTTL:
788 u_int32_t user;
790 if (copy_from_user(&user, argp, sizeof(user)))
791 return -EFAULT;
792 /* 20 secs at most... */
793 if (user > 20000)
794 return -EINVAL;
795 user = (user * HZ) / 1000;
796 atomic_set(&server->dentry_ttl, user);
797 return 0;
800 case NCP_IOC_GETDENTRYTTL:
802 u_int32_t user = (atomic_read(&server->dentry_ttl) * 1000) / HZ;
803 if (copy_to_user(argp, &user, sizeof(user)))
804 return -EFAULT;
805 return 0;
809 return -EINVAL;
812 long ncp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
814 struct inode *inode = filp->f_dentry->d_inode;
815 struct ncp_server *server = NCP_SERVER(inode);
816 uid_t uid = current_uid();
817 int need_drop_write = 0;
818 long ret;
820 switch (cmd) {
821 case NCP_IOC_SETCHARSETS:
822 case NCP_IOC_CONN_LOGGED_IN:
823 case NCP_IOC_SETROOT:
824 if (!capable(CAP_SYS_ADMIN)) {
825 ret = -EACCES;
826 goto out;
828 break;
830 if (server->m.mounted_uid != uid) {
831 switch (cmd) {
833 * Only mount owner can issue these ioctls. Information
834 * necessary to authenticate to other NDS servers are
835 * stored here.
837 case NCP_IOC_GETOBJECTNAME:
838 case NCP_IOC_SETOBJECTNAME:
839 case NCP_IOC_GETPRIVATEDATA:
840 case NCP_IOC_SETPRIVATEDATA:
841 #ifdef CONFIG_COMPAT
842 case NCP_IOC_GETOBJECTNAME_32:
843 case NCP_IOC_SETOBJECTNAME_32:
844 case NCP_IOC_GETPRIVATEDATA_32:
845 case NCP_IOC_SETPRIVATEDATA_32:
846 #endif
847 ret = -EACCES;
848 goto out;
850 * These require write access on the inode if user id
851 * does not match. Note that they do not write to the
852 * file... But old code did mnt_want_write, so I keep
853 * it as is. Of course not for mountpoint owner, as
854 * that breaks read-only mounts altogether as ncpmount
855 * needs working NCP_IOC_NCPREQUEST and
856 * NCP_IOC_GET_FS_INFO. Some of these codes (setdentryttl,
857 * signinit, setsignwanted) should be probably restricted
858 * to owner only, or even more to CAP_SYS_ADMIN).
860 case NCP_IOC_GET_FS_INFO:
861 case NCP_IOC_GET_FS_INFO_V2:
862 case NCP_IOC_NCPREQUEST:
863 case NCP_IOC_SETDENTRYTTL:
864 case NCP_IOC_SIGN_INIT:
865 case NCP_IOC_LOCKUNLOCK:
866 case NCP_IOC_SET_SIGN_WANTED:
867 #ifdef CONFIG_COMPAT
868 case NCP_IOC_GET_FS_INFO_V2_32:
869 case NCP_IOC_NCPREQUEST_32:
870 #endif
871 ret = mnt_want_write_file(filp);
872 if (ret)
873 goto out;
874 need_drop_write = 1;
875 ret = inode_permission(inode, MAY_WRITE);
876 if (ret)
877 goto outDropWrite;
878 break;
880 * Read access required.
882 case NCP_IOC_GETMOUNTUID16:
883 case NCP_IOC_GETMOUNTUID32:
884 case NCP_IOC_GETMOUNTUID64:
885 case NCP_IOC_GETROOT:
886 case NCP_IOC_SIGN_WANTED:
887 ret = inode_permission(inode, MAY_READ);
888 if (ret)
889 goto out;
890 break;
892 * Anybody can read these.
894 case NCP_IOC_GETCHARSETS:
895 case NCP_IOC_GETDENTRYTTL:
896 default:
897 /* Three codes below are protected by CAP_SYS_ADMIN above. */
898 case NCP_IOC_SETCHARSETS:
899 case NCP_IOC_CONN_LOGGED_IN:
900 case NCP_IOC_SETROOT:
901 break;
904 ret = __ncp_ioctl(inode, cmd, arg);
905 outDropWrite:
906 if (need_drop_write)
907 mnt_drop_write(filp->f_path.mnt);
908 out:
909 return ret;
912 #ifdef CONFIG_COMPAT
913 long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
915 long ret;
917 arg = (unsigned long) compat_ptr(arg);
918 ret = ncp_ioctl(file, cmd, arg);
919 return ret;
921 #endif