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
10 #include <linux/capability.h>
11 #include <linux/compat.h>
12 #include <linux/errno.h>
14 #include <linux/ioctl.h>
15 #include <linux/time.h>
17 #include <linux/mount.h>
18 #include <linux/highuid.h>
19 #include <linux/smp_lock.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sched.h>
23 #include <linux/ncp_fs.h>
25 #include <asm/uaccess.h>
27 #include "ncplib_kernel.h"
29 /* maximum limit for ncp_objectname_ioctl */
30 #define NCP_OBJECT_NAME_MAX_LEN 4096
31 /* maximum limit for ncp_privatedata_ioctl */
32 #define NCP_PRIVATE_DATA_MAX_LEN 8192
33 /* maximum negotiable packet size */
34 #define NCP_PACKET_SIZE_INTERNAL 65536
37 ncp_get_fs_info(struct ncp_server * server, struct file *file,
38 struct ncp_fs_info __user *arg)
40 struct inode *inode = file->f_path.dentry->d_inode;
41 struct ncp_fs_info info;
43 if ((file_permission(file, MAY_WRITE) != 0)
44 && (current->uid != server->m.mounted_uid)) {
47 if (copy_from_user(&info, arg, sizeof(info)))
50 if (info.version != NCP_GET_FS_INFO_VERSION) {
51 DPRINTK("info.version invalid: %d\n", info.version);
54 /* TODO: info.addr = server->m.serv_addr; */
55 SET_UID(info.mounted_uid, server->m.mounted_uid);
56 info.connection = server->connection;
57 info.buffer_size = server->buffer_size;
58 info.volume_number = NCP_FINFO(inode)->volNumber;
59 info.directory_id = NCP_FINFO(inode)->DosDirNum;
61 if (copy_to_user(arg, &info, sizeof(info)))
67 ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
68 struct ncp_fs_info_v2 __user * arg)
70 struct inode *inode = file->f_path.dentry->d_inode;
71 struct ncp_fs_info_v2 info2;
73 if ((file_permission(file, MAY_WRITE) != 0)
74 && (current->uid != server->m.mounted_uid)) {
77 if (copy_from_user(&info2, arg, sizeof(info2)))
80 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
81 DPRINTK("info.version invalid: %d\n", info2.version);
84 info2.mounted_uid = server->m.mounted_uid;
85 info2.connection = server->connection;
86 info2.buffer_size = server->buffer_size;
87 info2.volume_number = NCP_FINFO(inode)->volNumber;
88 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
89 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
91 if (copy_to_user(arg, &info2, sizeof(info2)))
97 struct compat_ncp_objectname_ioctl
101 compat_caddr_t object_name; /* an userspace data, in most cases user name */
104 struct compat_ncp_fs_info_v2 {
118 struct compat_ncp_ioctl_request {
124 struct compat_ncp_privatedata_ioctl
127 compat_caddr_t data; /* ~1000 for NDS */
130 #define NCP_IOC_GET_FS_INFO_V2_32 _IOWR('n', 4, struct compat_ncp_fs_info_v2)
131 #define NCP_IOC_NCPREQUEST_32 _IOR('n', 1, struct compat_ncp_ioctl_request)
132 #define NCP_IOC_GETOBJECTNAME_32 _IOWR('n', 9, struct compat_ncp_objectname_ioctl)
133 #define NCP_IOC_SETOBJECTNAME_32 _IOR('n', 9, struct compat_ncp_objectname_ioctl)
134 #define NCP_IOC_GETPRIVATEDATA_32 _IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
135 #define NCP_IOC_SETPRIVATEDATA_32 _IOR('n', 10, struct compat_ncp_privatedata_ioctl)
138 ncp_get_compat_fs_info_v2(struct ncp_server * server, struct file *file,
139 struct compat_ncp_fs_info_v2 __user * arg)
141 struct inode *inode = file->f_path.dentry->d_inode;
142 struct compat_ncp_fs_info_v2 info2;
144 if ((file_permission(file, MAY_WRITE) != 0)
145 && (current->uid != server->m.mounted_uid)) {
148 if (copy_from_user(&info2, arg, sizeof(info2)))
151 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
152 DPRINTK("info.version invalid: %d\n", info2.version);
155 info2.mounted_uid = server->m.mounted_uid;
156 info2.connection = server->connection;
157 info2.buffer_size = server->buffer_size;
158 info2.volume_number = NCP_FINFO(inode)->volNumber;
159 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
160 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
162 if (copy_to_user(arg, &info2, sizeof(info2)))
168 #define NCP_IOC_GETMOUNTUID16 _IOW('n', 2, u16)
169 #define NCP_IOC_GETMOUNTUID32 _IOW('n', 2, u32)
170 #define NCP_IOC_GETMOUNTUID64 _IOW('n', 2, u64)
172 #ifdef CONFIG_NCPFS_NLS
173 /* Here we are select the iocharset and the codepage for NLS.
174 * Thanks Petr Vandrovec for idea and many hints.
177 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
179 struct ncp_nls_ioctl user;
180 struct nls_table *codepage;
181 struct nls_table *iocharset;
182 struct nls_table *oldset_io;
183 struct nls_table *oldset_cp;
185 if (!capable(CAP_SYS_ADMIN))
187 if (server->root_setuped)
190 if (copy_from_user(&user, arg, sizeof(user)))
194 user.codepage[NCP_IOCSNAME_LEN] = 0;
195 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
196 codepage = load_nls_default();
198 codepage = load_nls(user.codepage);
205 user.iocharset[NCP_IOCSNAME_LEN] = 0;
206 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
207 iocharset = load_nls_default();
208 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
209 } else if (!strcmp(user.iocharset, "utf8")) {
210 iocharset = load_nls_default();
211 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
213 iocharset = load_nls(user.iocharset);
215 unload_nls(codepage);
218 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
221 oldset_cp = server->nls_vol;
222 server->nls_vol = codepage;
223 oldset_io = server->nls_io;
224 server->nls_io = iocharset;
227 unload_nls(oldset_cp);
229 unload_nls(oldset_io);
235 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
237 struct ncp_nls_ioctl user;
240 memset(&user, 0, sizeof(user));
241 if (server->nls_vol && server->nls_vol->charset) {
242 len = strlen(server->nls_vol->charset);
243 if (len > NCP_IOCSNAME_LEN)
244 len = NCP_IOCSNAME_LEN;
245 strncpy(user.codepage, server->nls_vol->charset, len);
246 user.codepage[len] = 0;
249 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
250 strcpy(user.iocharset, "utf8");
251 else if (server->nls_io && server->nls_io->charset) {
252 len = strlen(server->nls_io->charset);
253 if (len > NCP_IOCSNAME_LEN)
254 len = NCP_IOCSNAME_LEN;
255 strncpy(user.iocharset, server->nls_io->charset, len);
256 user.iocharset[len] = 0;
259 if (copy_to_user(arg, &user, sizeof(user)))
263 #endif /* CONFIG_NCPFS_NLS */
265 static int __ncp_ioctl(struct inode *inode, struct file *filp,
266 unsigned int cmd, unsigned long arg)
268 struct ncp_server *server = NCP_SERVER(inode);
270 struct ncp_ioctl_request request;
272 void __user *argp = (void __user *)arg;
276 case NCP_IOC_NCPREQUEST_32:
278 case NCP_IOC_NCPREQUEST:
279 if ((file_permission(filp, MAY_WRITE) != 0)
280 && (current->uid != server->m.mounted_uid)) {
284 if (cmd == NCP_IOC_NCPREQUEST_32) {
285 struct compat_ncp_ioctl_request request32;
286 if (copy_from_user(&request32, argp, sizeof(request32)))
288 request.function = request32.function;
289 request.size = request32.size;
290 request.data = compat_ptr(request32.data);
293 if (copy_from_user(&request, argp, sizeof(request)))
296 if ((request.function > 255)
298 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
301 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
304 if (copy_from_user(bouncebuffer, request.data, request.size)) {
308 ncp_lock_server(server);
310 /* FIXME: We hack around in the server's structures
311 here to be able to use ncp_request */
313 server->has_subfunction = 0;
314 server->current_size = request.size;
315 memcpy(server->packet, bouncebuffer, request.size);
317 result = ncp_request2(server, request.function,
318 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
322 result = server->reply_size;
323 ncp_unlock_server(server);
324 DPRINTK("ncp_ioctl: copy %d bytes\n",
327 if (copy_to_user(request.data, bouncebuffer, result))
332 case NCP_IOC_CONN_LOGGED_IN:
334 if (!capable(CAP_SYS_ADMIN))
336 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
338 if (server->root_setuped)
340 server->root_setuped = 1;
341 return ncp_conn_logged_in(inode->i_sb);
343 case NCP_IOC_GET_FS_INFO:
344 return ncp_get_fs_info(server, filp, argp);
346 case NCP_IOC_GET_FS_INFO_V2:
347 return ncp_get_fs_info_v2(server, filp, argp);
350 case NCP_IOC_GET_FS_INFO_V2_32:
351 return ncp_get_compat_fs_info_v2(server, filp, argp);
353 /* we have too many combinations of CONFIG_COMPAT,
354 * CONFIG_64BIT and CONFIG_UID16, so just handle
355 * any of the possible ioctls */
356 case NCP_IOC_GETMOUNTUID16:
357 case NCP_IOC_GETMOUNTUID32:
358 case NCP_IOC_GETMOUNTUID64:
359 if ((file_permission(filp, MAY_READ) != 0)
360 && (current->uid != server->m.mounted_uid)) {
363 if (cmd == NCP_IOC_GETMOUNTUID16) {
365 SET_UID(uid, server->m.mounted_uid);
366 if (put_user(uid, (u16 __user *)argp))
368 } else if (cmd == NCP_IOC_GETMOUNTUID32) {
369 if (put_user(server->m.mounted_uid,
373 if (put_user(server->m.mounted_uid,
379 case NCP_IOC_GETROOT:
381 struct ncp_setroot_ioctl sr;
383 if ((file_permission(filp, MAY_READ) != 0)
384 && (current->uid != server->m.mounted_uid))
388 if (server->m.mounted_vol[0]) {
389 struct dentry* dentry = inode->i_sb->s_root;
392 struct inode* s_inode = dentry->d_inode;
395 sr.volNumber = NCP_FINFO(s_inode)->volNumber;
396 sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum;
397 sr.namespace = server->name_space[sr.volNumber];
399 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
401 DPRINTK("ncpfs: s_root==NULL\n");
407 if (copy_to_user(argp, &sr, sizeof(sr)))
411 case NCP_IOC_SETROOT:
413 struct ncp_setroot_ioctl sr;
417 struct dentry* dentry;
419 if (!capable(CAP_SYS_ADMIN))
423 if (server->root_setuped) return -EBUSY;
424 if (copy_from_user(&sr, argp, sizeof(sr)))
426 if (sr.volNumber < 0) {
427 server->m.mounted_vol[0] = 0;
428 vnum = NCP_NUMBER_OF_VOLUMES;
431 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
433 } else if (ncp_mount_subdir(server, sr.volNumber,
434 sr.namespace, sr.dirEntNum,
435 &vnum, &de, &dosde)) {
439 dentry = inode->i_sb->s_root;
440 server->root_setuped = 1;
442 struct inode* s_inode = dentry->d_inode;
445 NCP_FINFO(s_inode)->volNumber = vnum;
446 NCP_FINFO(s_inode)->dirEntNum = de;
447 NCP_FINFO(s_inode)->DosDirNum = dosde;
449 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
451 DPRINTK("ncpfs: s_root==NULL\n");
456 #ifdef CONFIG_NCPFS_PACKET_SIGNING
457 case NCP_IOC_SIGN_INIT:
458 if ((file_permission(filp, MAY_WRITE) != 0)
459 && (current->uid != server->m.mounted_uid))
464 if (server->sign_wanted)
466 struct ncp_sign_init sign;
468 if (copy_from_user(&sign, argp, sizeof(sign)))
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 */
476 server->sign_active = 0;
480 case NCP_IOC_SIGN_WANTED:
481 if ((file_permission(filp, MAY_READ) != 0)
482 && (current->uid != server->m.mounted_uid))
487 if (put_user(server->sign_wanted, (int __user *)argp))
490 case NCP_IOC_SET_SIGN_WANTED:
494 if ((file_permission(filp, MAY_WRITE) != 0)
495 && (current->uid != server->m.mounted_uid))
499 /* get only low 8 bits... */
500 if (get_user(newstate, (unsigned char __user *)argp))
502 if (server->sign_active) {
503 /* cannot turn signatures OFF when active */
504 if (!newstate) return -EINVAL;
506 server->sign_wanted = newstate != 0;
511 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
513 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
514 case NCP_IOC_LOCKUNLOCK:
515 if ((file_permission(filp, MAY_WRITE) != 0)
516 && (current->uid != server->m.mounted_uid))
521 struct ncp_lock_ioctl rqdata;
523 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
525 if (rqdata.origin != 0)
528 switch (rqdata.cmd) {
531 if (rqdata.timeout == 0)
532 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
533 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
534 rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
537 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
543 /* locking needs both read and write access */
544 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
549 if (!ncp_conn_valid(server))
552 if (!S_ISREG(inode->i_mode))
554 if (rqdata.cmd == NCP_LOCK_CLEAR)
556 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
557 NCP_FINFO(inode)->file_handle,
560 if (result > 0) result = 0; /* no such lock */
568 case NCP_LOCK_EX: lockcmd=1; break;
569 case NCP_LOCK_SH: lockcmd=3; break;
570 default: lockcmd=0; break;
572 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
573 NCP_FINFO(inode)->file_handle,
578 if (result > 0) result = -EAGAIN;
581 ncp_inode_close(inode);
584 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
587 case NCP_IOC_GETOBJECTNAME_32:
588 if (current->uid != server->m.mounted_uid) {
592 struct compat_ncp_objectname_ioctl user;
595 if (copy_from_user(&user, argp, sizeof(user)))
597 user.auth_type = server->auth.auth_type;
598 outl = user.object_name_len;
599 user.object_name_len = server->auth.object_name_len;
600 if (outl > user.object_name_len)
601 outl = user.object_name_len;
603 if (copy_to_user(compat_ptr(user.object_name),
604 server->auth.object_name,
605 outl)) return -EFAULT;
607 if (copy_to_user(argp, &user, sizeof(user)))
612 case NCP_IOC_GETOBJECTNAME:
613 if (current->uid != server->m.mounted_uid) {
617 struct ncp_objectname_ioctl user;
620 if (copy_from_user(&user, argp, sizeof(user)))
622 user.auth_type = server->auth.auth_type;
623 outl = user.object_name_len;
624 user.object_name_len = server->auth.object_name_len;
625 if (outl > user.object_name_len)
626 outl = user.object_name_len;
628 if (copy_to_user(user.object_name,
629 server->auth.object_name,
630 outl)) return -EFAULT;
632 if (copy_to_user(argp, &user, sizeof(user)))
637 case NCP_IOC_SETOBJECTNAME_32:
639 case NCP_IOC_SETOBJECTNAME:
640 if (current->uid != server->m.mounted_uid) {
644 struct ncp_objectname_ioctl user;
649 size_t oldprivatelen;
652 if (cmd == NCP_IOC_SETOBJECTNAME_32) {
653 struct compat_ncp_objectname_ioctl user32;
654 if (copy_from_user(&user32, argp, sizeof(user32)))
656 user.auth_type = user32.auth_type;
657 user.object_name_len = user32.object_name_len;
658 user.object_name = compat_ptr(user32.object_name);
661 if (copy_from_user(&user, argp, sizeof(user)))
664 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
666 if (user.object_name_len) {
667 newname = kmalloc(user.object_name_len, GFP_USER);
670 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
677 /* enter critical section */
678 /* maybe that kfree can sleep so do that this way */
679 /* it is at least more SMP friendly (in future...) */
680 oldname = server->auth.object_name;
681 oldnamelen = server->auth.object_name_len;
682 oldprivate = server->priv.data;
683 oldprivatelen = server->priv.len;
684 server->auth.auth_type = user.auth_type;
685 server->auth.object_name_len = user.object_name_len;
686 server->auth.object_name = newname;
687 server->priv.len = 0;
688 server->priv.data = NULL;
689 /* leave critical section */
695 case NCP_IOC_GETPRIVATEDATA_32:
697 case NCP_IOC_GETPRIVATEDATA:
698 if (current->uid != server->m.mounted_uid) {
702 struct ncp_privatedata_ioctl user;
706 if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
707 struct compat_ncp_privatedata_ioctl user32;
708 if (copy_from_user(&user32, argp, sizeof(user32)))
710 user.len = user32.len;
711 user.data = compat_ptr(user32.data);
714 if (copy_from_user(&user, argp, sizeof(user)))
718 user.len = server->priv.len;
719 if (outl > user.len) outl = user.len;
721 if (copy_to_user(user.data,
723 outl)) return -EFAULT;
726 if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
727 struct compat_ncp_privatedata_ioctl user32;
728 user32.len = user.len;
729 user32.data = (unsigned long) user.data;
730 if (copy_to_user(argp, &user32, sizeof(user32)))
734 if (copy_to_user(argp, &user, sizeof(user)))
740 case NCP_IOC_SETPRIVATEDATA_32:
742 case NCP_IOC_SETPRIVATEDATA:
743 if (current->uid != server->m.mounted_uid) {
747 struct ncp_privatedata_ioctl user;
753 if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
754 struct compat_ncp_privatedata_ioctl user32;
755 if (copy_from_user(&user32, argp, sizeof(user32)))
757 user.len = user32.len;
758 user.data = compat_ptr(user32.data);
761 if (copy_from_user(&user, argp, sizeof(user)))
764 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
767 new = kmalloc(user.len, GFP_USER);
770 if (copy_from_user(new, user.data, user.len)) {
777 /* enter critical section */
778 old = server->priv.data;
779 oldlen = server->priv.len;
780 server->priv.len = user.len;
781 server->priv.data = new;
782 /* leave critical section */
787 #ifdef CONFIG_NCPFS_NLS
788 case NCP_IOC_SETCHARSETS:
789 return ncp_set_charsets(server, argp);
791 case NCP_IOC_GETCHARSETS:
792 return ncp_get_charsets(server, argp);
794 #endif /* CONFIG_NCPFS_NLS */
796 case NCP_IOC_SETDENTRYTTL:
797 if ((file_permission(filp, MAY_WRITE) != 0) &&
798 (current->uid != server->m.mounted_uid))
803 if (copy_from_user(&user, argp, sizeof(user)))
805 /* 20 secs at most... */
808 user = (user * HZ) / 1000;
809 server->dentry_ttl = user;
813 case NCP_IOC_GETDENTRYTTL:
815 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
816 if (copy_to_user(argp, &user, sizeof(user)))
825 static int ncp_ioctl_need_write(unsigned int cmd)
828 case NCP_IOC_GET_FS_INFO:
829 case NCP_IOC_GET_FS_INFO_V2:
830 case NCP_IOC_NCPREQUEST:
831 case NCP_IOC_SETDENTRYTTL:
832 case NCP_IOC_SIGN_INIT:
833 case NCP_IOC_LOCKUNLOCK:
834 case NCP_IOC_SET_SIGN_WANTED:
836 case NCP_IOC_GETOBJECTNAME:
837 case NCP_IOC_SETOBJECTNAME:
838 case NCP_IOC_GETPRIVATEDATA:
839 case NCP_IOC_SETPRIVATEDATA:
840 case NCP_IOC_SETCHARSETS:
841 case NCP_IOC_GETCHARSETS:
842 case NCP_IOC_CONN_LOGGED_IN:
843 case NCP_IOC_GETDENTRYTTL:
844 case NCP_IOC_GETMOUNTUID2:
845 case NCP_IOC_SIGN_WANTED:
846 case NCP_IOC_GETROOT:
847 case NCP_IOC_SETROOT:
850 /* unkown IOCTL command, assume write */
855 int ncp_ioctl(struct inode *inode, struct file *filp,
856 unsigned int cmd, unsigned long arg)
860 if (ncp_ioctl_need_write(cmd)) {
862 * inside the ioctl(), any failures which
863 * are because of file_permission() are
864 * -EACCESS, so it seems consistent to keep
867 if (mnt_want_write(filp->f_path.mnt))
870 ret = __ncp_ioctl(inode, filp, cmd, arg);
871 if (ncp_ioctl_need_write(cmd))
872 mnt_drop_write(filp->f_path.mnt);
877 long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
879 struct inode *inode = file->f_path.dentry->d_inode;
883 arg = (unsigned long) compat_ptr(arg);
884 ret = ncp_ioctl(inode, file, cmd, arg);