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/highuid.h>
18 #include <linux/smp_lock.h>
19 #include <linux/vmalloc.h>
21 #include <linux/ncp_fs.h>
23 #include <asm/uaccess.h>
25 #include "ncplib_kernel.h"
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
35 ncp_get_fs_info(struct ncp_server
* server
, struct file
*file
,
36 struct ncp_fs_info __user
*arg
)
38 struct inode
*inode
= file
->f_dentry
->d_inode
;
39 struct ncp_fs_info info
;
41 if ((file_permission(file
, MAY_WRITE
) != 0)
42 && (current
->uid
!= server
->m
.mounted_uid
)) {
45 if (copy_from_user(&info
, arg
, sizeof(info
)))
48 if (info
.version
!= NCP_GET_FS_INFO_VERSION
) {
49 DPRINTK("info.version invalid: %d\n", info
.version
);
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
;
59 if (copy_to_user(arg
, &info
, sizeof(info
)))
65 ncp_get_fs_info_v2(struct ncp_server
* server
, struct file
*file
,
66 struct ncp_fs_info_v2 __user
* arg
)
68 struct inode
*inode
= file
->f_dentry
->d_inode
;
69 struct ncp_fs_info_v2 info2
;
71 if ((file_permission(file
, MAY_WRITE
) != 0)
72 && (current
->uid
!= server
->m
.mounted_uid
)) {
75 if (copy_from_user(&info2
, arg
, sizeof(info2
)))
78 if (info2
.version
!= NCP_GET_FS_INFO_VERSION_V2
) {
79 DPRINTK("info.version invalid: %d\n", info2
.version
);
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;
89 if (copy_to_user(arg
, &info2
, sizeof(info2
)))
95 struct compat_ncp_objectname_ioctl
99 compat_caddr_t object_name
; /* an userspace data, in most cases user name */
102 struct compat_ncp_fs_info_v2
{
116 struct compat_ncp_ioctl_request
{
122 struct compat_ncp_privatedata_ioctl
125 compat_caddr_t data
; /* ~1000 for NDS */
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)
136 ncp_get_compat_fs_info_v2(struct ncp_server
* server
, struct file
*file
,
137 struct compat_ncp_fs_info_v2 __user
* arg
)
139 struct inode
*inode
= file
->f_dentry
->d_inode
;
140 struct compat_ncp_fs_info_v2 info2
;
142 if ((file_permission(file
, MAY_WRITE
) != 0)
143 && (current
->uid
!= server
->m
.mounted_uid
)) {
146 if (copy_from_user(&info2
, arg
, sizeof(info2
)))
149 if (info2
.version
!= NCP_GET_FS_INFO_VERSION_V2
) {
150 DPRINTK("info.version invalid: %d\n", info2
.version
);
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;
160 if (copy_to_user(arg
, &info2
, sizeof(info2
)))
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)
170 #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.
175 ncp_set_charsets(struct ncp_server
* server
, struct ncp_nls_ioctl __user
*arg
)
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
;
183 if (!capable(CAP_SYS_ADMIN
))
185 if (server
->root_setuped
)
188 if (copy_from_user(&user
, arg
, sizeof(user
)))
192 user
.codepage
[NCP_IOCSNAME_LEN
] = 0;
193 if (!user
.codepage
[0] || !strcmp(user
.codepage
, "default"))
194 codepage
= load_nls_default();
196 codepage
= load_nls(user
.codepage
);
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
);
211 iocharset
= load_nls(user
.iocharset
);
213 unload_nls(codepage
);
216 NCP_CLR_FLAG(server
, NCP_FLAG_UTF8
);
219 oldset_cp
= server
->nls_vol
;
220 server
->nls_vol
= codepage
;
221 oldset_io
= server
->nls_io
;
222 server
->nls_io
= iocharset
;
225 unload_nls(oldset_cp
);
227 unload_nls(oldset_io
);
233 ncp_get_charsets(struct ncp_server
* server
, struct ncp_nls_ioctl __user
*arg
)
235 struct ncp_nls_ioctl user
;
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;
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;
257 if (copy_to_user(arg
, &user
, sizeof(user
)))
261 #endif /* CONFIG_NCPFS_NLS */
263 int ncp_ioctl(struct inode
*inode
, struct file
*filp
,
264 unsigned int cmd
, unsigned long arg
)
266 struct ncp_server
*server
= NCP_SERVER(inode
);
268 struct ncp_ioctl_request request
;
270 void __user
*argp
= (void __user
*)arg
;
274 case NCP_IOC_NCPREQUEST_32
:
276 case NCP_IOC_NCPREQUEST
:
277 if ((file_permission(filp
, MAY_WRITE
) != 0)
278 && (current
->uid
!= server
->m
.mounted_uid
)) {
282 if (cmd
== NCP_IOC_NCPREQUEST_32
) {
283 struct compat_ncp_ioctl_request request32
;
284 if (copy_from_user(&request32
, argp
, sizeof(request32
)))
286 request
.function
= request32
.function
;
287 request
.size
= request32
.size
;
288 request
.data
= compat_ptr(request32
.data
);
291 if (copy_from_user(&request
, argp
, sizeof(request
)))
294 if ((request
.function
> 255)
296 NCP_PACKET_SIZE
- sizeof(struct ncp_request_header
))) {
299 bouncebuffer
= vmalloc(NCP_PACKET_SIZE_INTERNAL
);
302 if (copy_from_user(bouncebuffer
, request
.data
, request
.size
)) {
306 ncp_lock_server(server
);
308 /* FIXME: We hack around in the server's structures
309 here to be able to use ncp_request */
311 server
->has_subfunction
= 0;
312 server
->current_size
= request
.size
;
313 memcpy(server
->packet
, bouncebuffer
, request
.size
);
315 result
= ncp_request2(server
, request
.function
,
316 bouncebuffer
, NCP_PACKET_SIZE_INTERNAL
);
320 result
= server
->reply_size
;
321 ncp_unlock_server(server
);
322 DPRINTK("ncp_ioctl: copy %d bytes\n",
325 if (copy_to_user(request
.data
, bouncebuffer
, result
))
330 case NCP_IOC_CONN_LOGGED_IN
:
332 if (!capable(CAP_SYS_ADMIN
))
334 if (!(server
->m
.int_flags
& NCP_IMOUNT_LOGGEDIN_POSSIBLE
))
336 if (server
->root_setuped
)
338 server
->root_setuped
= 1;
339 return ncp_conn_logged_in(inode
->i_sb
);
341 case NCP_IOC_GET_FS_INFO
:
342 return ncp_get_fs_info(server
, filp
, argp
);
344 case NCP_IOC_GET_FS_INFO_V2
:
345 return ncp_get_fs_info_v2(server
, filp
, argp
);
348 case NCP_IOC_GET_FS_INFO_V2_32
:
349 return ncp_get_compat_fs_info_v2(server
, filp
, argp
);
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
)) {
361 if (cmd
== NCP_IOC_GETMOUNTUID16
) {
363 SET_UID(uid
, server
->m
.mounted_uid
);
364 if (put_user(uid
, (u16 __user
*)argp
))
366 } else if (cmd
== NCP_IOC_GETMOUNTUID32
) {
367 if (put_user(server
->m
.mounted_uid
,
371 if (put_user(server
->m
.mounted_uid
,
377 case NCP_IOC_GETROOT
:
379 struct ncp_setroot_ioctl sr
;
381 if ((file_permission(filp
, MAY_READ
) != 0)
382 && (current
->uid
!= server
->m
.mounted_uid
))
386 if (server
->m
.mounted_vol
[0]) {
387 struct dentry
* dentry
= inode
->i_sb
->s_root
;
390 struct inode
* inode
= dentry
->d_inode
;
393 sr
.volNumber
= NCP_FINFO(inode
)->volNumber
;
394 sr
.dirEntNum
= NCP_FINFO(inode
)->dirEntNum
;
395 sr
.namespace = server
->name_space
[sr
.volNumber
];
397 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
399 DPRINTK("ncpfs: s_root==NULL\n");
405 if (copy_to_user(argp
, &sr
, sizeof(sr
)))
409 case NCP_IOC_SETROOT
:
411 struct ncp_setroot_ioctl sr
;
415 struct dentry
* dentry
;
417 if (!capable(CAP_SYS_ADMIN
))
421 if (server
->root_setuped
) return -EBUSY
;
422 if (copy_from_user(&sr
, argp
, sizeof(sr
)))
424 if (sr
.volNumber
< 0) {
425 server
->m
.mounted_vol
[0] = 0;
426 vnum
= NCP_NUMBER_OF_VOLUMES
;
429 } else if (sr
.volNumber
>= NCP_NUMBER_OF_VOLUMES
) {
431 } else if (ncp_mount_subdir(server
, sr
.volNumber
,
432 sr
.namespace, sr
.dirEntNum
,
433 &vnum
, &de
, &dosde
)) {
437 dentry
= inode
->i_sb
->s_root
;
438 server
->root_setuped
= 1;
440 struct inode
* inode
= dentry
->d_inode
;
443 NCP_FINFO(inode
)->volNumber
= vnum
;
444 NCP_FINFO(inode
)->dirEntNum
= de
;
445 NCP_FINFO(inode
)->DosDirNum
= dosde
;
447 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
449 DPRINTK("ncpfs: s_root==NULL\n");
454 #ifdef CONFIG_NCPFS_PACKET_SIGNING
455 case NCP_IOC_SIGN_INIT
:
456 if ((file_permission(filp
, MAY_WRITE
) != 0)
457 && (current
->uid
!= server
->m
.mounted_uid
))
462 if (server
->sign_wanted
)
464 struct ncp_sign_init sign
;
466 if (copy_from_user(&sign
, argp
, sizeof(sign
)))
468 memcpy(server
->sign_root
,sign
.sign_root
,8);
469 memcpy(server
->sign_last
,sign
.sign_last
,16);
470 server
->sign_active
= 1;
472 /* ignore when signatures not wanted */
474 server
->sign_active
= 0;
478 case NCP_IOC_SIGN_WANTED
:
479 if ((file_permission(filp
, MAY_READ
) != 0)
480 && (current
->uid
!= server
->m
.mounted_uid
))
485 if (put_user(server
->sign_wanted
, (int __user
*)argp
))
488 case NCP_IOC_SET_SIGN_WANTED
:
492 if ((file_permission(filp
, MAY_WRITE
) != 0)
493 && (current
->uid
!= server
->m
.mounted_uid
))
497 /* get only low 8 bits... */
498 if (get_user(newstate
, (unsigned char __user
*)argp
))
500 if (server
->sign_active
) {
501 /* cannot turn signatures OFF when active */
502 if (!newstate
) return -EINVAL
;
504 server
->sign_wanted
= newstate
!= 0;
509 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
511 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
512 case NCP_IOC_LOCKUNLOCK
:
513 if ((file_permission(filp
, MAY_WRITE
) != 0)
514 && (current
->uid
!= server
->m
.mounted_uid
))
519 struct ncp_lock_ioctl rqdata
;
522 if (copy_from_user(&rqdata
, argp
, sizeof(rqdata
)))
524 if (rqdata
.origin
!= 0)
527 switch (rqdata
.cmd
) {
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
;
536 rqdata
.timeout
= NCP_LOCK_DEFAULT_TIMEOUT
; /* has no effect */
542 /* locking needs both read and write access */
543 if ((result
= ncp_make_open(inode
, O_RDWR
)) != 0)
548 if (!ncp_conn_valid(server
))
551 if (!S_ISREG(inode
->i_mode
))
553 if (rqdata
.cmd
== NCP_LOCK_CLEAR
)
555 result
= ncp_ClearPhysicalRecord(NCP_SERVER(inode
),
556 NCP_FINFO(inode
)->file_handle
,
559 if (result
> 0) result
= 0; /* no such lock */
567 case NCP_LOCK_EX
: lockcmd
=1; break;
568 case NCP_LOCK_SH
: lockcmd
=3; break;
569 default: lockcmd
=0; break;
571 result
= ncp_LogPhysicalRecord(NCP_SERVER(inode
),
572 NCP_FINFO(inode
)->file_handle
,
577 if (result
> 0) result
= -EAGAIN
;
580 ncp_inode_close(inode
);
583 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
586 case NCP_IOC_GETOBJECTNAME_32
:
587 if (current
->uid
!= server
->m
.mounted_uid
) {
591 struct compat_ncp_objectname_ioctl user
;
594 if (copy_from_user(&user
, argp
, sizeof(user
)))
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
;
602 if (copy_to_user(compat_ptr(user
.object_name
),
603 server
->auth
.object_name
,
604 outl
)) return -EFAULT
;
606 if (copy_to_user(argp
, &user
, sizeof(user
)))
611 case NCP_IOC_GETOBJECTNAME
:
612 if (current
->uid
!= server
->m
.mounted_uid
) {
616 struct ncp_objectname_ioctl user
;
619 if (copy_from_user(&user
, argp
, sizeof(user
)))
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
;
627 if (copy_to_user(user
.object_name
,
628 server
->auth
.object_name
,
629 outl
)) return -EFAULT
;
631 if (copy_to_user(argp
, &user
, sizeof(user
)))
636 case NCP_IOC_SETOBJECTNAME_32
:
638 case NCP_IOC_SETOBJECTNAME
:
639 if (current
->uid
!= server
->m
.mounted_uid
) {
643 struct ncp_objectname_ioctl user
;
648 size_t oldprivatelen
;
651 if (cmd
== NCP_IOC_SETOBJECTNAME_32
) {
652 struct compat_ncp_objectname_ioctl user32
;
653 if (copy_from_user(&user32
, argp
, sizeof(user32
)))
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
);
660 if (copy_from_user(&user
, argp
, sizeof(user
)))
663 if (user
.object_name_len
> NCP_OBJECT_NAME_MAX_LEN
)
665 if (user
.object_name_len
) {
666 newname
= kmalloc(user
.object_name_len
, GFP_USER
);
669 if (copy_from_user(newname
, user
.object_name
, user
.object_name_len
)) {
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 */
694 case NCP_IOC_GETPRIVATEDATA_32
:
696 case NCP_IOC_GETPRIVATEDATA
:
697 if (current
->uid
!= server
->m
.mounted_uid
) {
701 struct ncp_privatedata_ioctl user
;
705 if (cmd
== NCP_IOC_GETPRIVATEDATA_32
) {
706 struct compat_ncp_privatedata_ioctl user32
;
707 if (copy_from_user(&user32
, argp
, sizeof(user32
)))
709 user
.len
= user32
.len
;
710 user
.data
= compat_ptr(user32
.data
);
713 if (copy_from_user(&user
, argp
, sizeof(user
)))
717 user
.len
= server
->priv
.len
;
718 if (outl
> user
.len
) outl
= user
.len
;
720 if (copy_to_user(user
.data
,
722 outl
)) return -EFAULT
;
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
;
729 if (copy_to_user(argp
, &user32
, sizeof(user32
)))
733 if (copy_to_user(argp
, &user
, sizeof(user
)))
739 case NCP_IOC_SETPRIVATEDATA_32
:
741 case NCP_IOC_SETPRIVATEDATA
:
742 if (current
->uid
!= server
->m
.mounted_uid
) {
746 struct ncp_privatedata_ioctl user
;
752 if (cmd
== NCP_IOC_SETPRIVATEDATA_32
) {
753 struct compat_ncp_privatedata_ioctl user32
;
754 if (copy_from_user(&user32
, argp
, sizeof(user32
)))
756 user
.len
= user32
.len
;
757 user
.data
= compat_ptr(user32
.data
);
760 if (copy_from_user(&user
, argp
, sizeof(user
)))
763 if (user
.len
> NCP_PRIVATE_DATA_MAX_LEN
)
766 new = kmalloc(user
.len
, GFP_USER
);
769 if (copy_from_user(new, user
.data
, user
.len
)) {
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 */
786 #ifdef CONFIG_NCPFS_NLS
787 case NCP_IOC_SETCHARSETS
:
788 return ncp_set_charsets(server
, argp
);
790 case NCP_IOC_GETCHARSETS
:
791 return ncp_get_charsets(server
, argp
);
793 #endif /* CONFIG_NCPFS_NLS */
795 case NCP_IOC_SETDENTRYTTL
:
796 if ((file_permission(filp
, MAY_WRITE
) != 0) &&
797 (current
->uid
!= server
->m
.mounted_uid
))
802 if (copy_from_user(&user
, argp
, sizeof(user
)))
804 /* 20 secs at most... */
807 user
= (user
* HZ
) / 1000;
808 server
->dentry_ttl
= user
;
812 case NCP_IOC_GETDENTRYTTL
:
814 u_int32_t user
= (server
->dentry_ttl
* 1000) / HZ
;
815 if (copy_to_user(argp
, &user
, sizeof(user
)))
825 long ncp_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
827 struct inode
*inode
= file
->f_dentry
->d_inode
;
831 arg
= (unsigned long) compat_ptr(arg
);
832 ret
= ncp_ioctl(inode
, file
, cmd
, arg
);