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/config.h>
12 #include <asm/uaccess.h>
13 #include <linux/errno.h>
15 #include <linux/ioctl.h>
16 #include <linux/time.h>
18 #include <linux/highuid.h>
19 #include <linux/vmalloc.h>
21 #include <linux/ncp_fs.h>
23 #include "ncplib_kernel.h"
25 /* maximum limit for ncp_objectname_ioctl */
26 #define NCP_OBJECT_NAME_MAX_LEN 4096
27 /* maximum limit for ncp_privatedata_ioctl */
28 #define NCP_PRIVATE_DATA_MAX_LEN 8192
29 /* maximum negotiable packet size */
30 #define NCP_PACKET_SIZE_INTERNAL 65536
33 ncp_get_fs_info(struct ncp_server
* server
, struct inode
* inode
, struct ncp_fs_info __user
*arg
)
35 struct ncp_fs_info info
;
37 if ((permission(inode
, MAY_WRITE
, NULL
) != 0)
38 && (current
->uid
!= server
->m
.mounted_uid
)) {
41 if (copy_from_user(&info
, arg
, sizeof(info
)))
44 if (info
.version
!= NCP_GET_FS_INFO_VERSION
) {
45 DPRINTK("info.version invalid: %d\n", info
.version
);
48 /* TODO: info.addr = server->m.serv_addr; */
49 SET_UID(info
.mounted_uid
, server
->m
.mounted_uid
);
50 info
.connection
= server
->connection
;
51 info
.buffer_size
= server
->buffer_size
;
52 info
.volume_number
= NCP_FINFO(inode
)->volNumber
;
53 info
.directory_id
= NCP_FINFO(inode
)->DosDirNum
;
55 if (copy_to_user(arg
, &info
, sizeof(info
)))
61 ncp_get_fs_info_v2(struct ncp_server
* server
, struct inode
* inode
, struct ncp_fs_info_v2 __user
* arg
)
63 struct ncp_fs_info_v2 info2
;
65 if ((permission(inode
, MAY_WRITE
, NULL
) != 0)
66 && (current
->uid
!= server
->m
.mounted_uid
)) {
69 if (copy_from_user(&info2
, arg
, sizeof(info2
)))
72 if (info2
.version
!= NCP_GET_FS_INFO_VERSION_V2
) {
73 DPRINTK("info.version invalid: %d\n", info2
.version
);
76 info2
.mounted_uid
= server
->m
.mounted_uid
;
77 info2
.connection
= server
->connection
;
78 info2
.buffer_size
= server
->buffer_size
;
79 info2
.volume_number
= NCP_FINFO(inode
)->volNumber
;
80 info2
.directory_id
= NCP_FINFO(inode
)->DosDirNum
;
81 info2
.dummy1
= info2
.dummy2
= info2
.dummy3
= 0;
83 if (copy_to_user(arg
, &info2
, sizeof(info2
)))
88 #ifdef CONFIG_NCPFS_NLS
89 /* Here we are select the iocharset and the codepage for NLS.
90 * Thanks Petr Vandrovec for idea and many hints.
93 ncp_set_charsets(struct ncp_server
* server
, struct ncp_nls_ioctl __user
*arg
)
95 struct ncp_nls_ioctl user
;
96 struct nls_table
*codepage
;
97 struct nls_table
*iocharset
;
98 struct nls_table
*oldset_io
;
99 struct nls_table
*oldset_cp
;
101 if (!capable(CAP_SYS_ADMIN
))
103 if (server
->root_setuped
)
106 if (copy_from_user(&user
, arg
, sizeof(user
)))
110 user
.codepage
[NCP_IOCSNAME_LEN
] = 0;
111 if (!user
.codepage
[0] || !strcmp(user
.codepage
, "default"))
112 codepage
= load_nls_default();
114 codepage
= load_nls(user
.codepage
);
121 user
.iocharset
[NCP_IOCSNAME_LEN
] = 0;
122 if (!user
.iocharset
[0] || !strcmp(user
.iocharset
, "default")) {
123 iocharset
= load_nls_default();
124 NCP_CLR_FLAG(server
, NCP_FLAG_UTF8
);
125 } else if (!strcmp(user
.iocharset
, "utf8")) {
126 iocharset
= load_nls_default();
127 NCP_SET_FLAG(server
, NCP_FLAG_UTF8
);
129 iocharset
= load_nls(user
.iocharset
);
131 unload_nls(codepage
);
134 NCP_CLR_FLAG(server
, NCP_FLAG_UTF8
);
137 oldset_cp
= server
->nls_vol
;
138 server
->nls_vol
= codepage
;
139 oldset_io
= server
->nls_io
;
140 server
->nls_io
= iocharset
;
143 unload_nls(oldset_cp
);
145 unload_nls(oldset_io
);
151 ncp_get_charsets(struct ncp_server
* server
, struct ncp_nls_ioctl __user
*arg
)
153 struct ncp_nls_ioctl user
;
156 memset(&user
, 0, sizeof(user
));
157 if (server
->nls_vol
&& server
->nls_vol
->charset
) {
158 len
= strlen(server
->nls_vol
->charset
);
159 if (len
> NCP_IOCSNAME_LEN
)
160 len
= NCP_IOCSNAME_LEN
;
161 strncpy(user
.codepage
, server
->nls_vol
->charset
, len
);
162 user
.codepage
[len
] = 0;
165 if (NCP_IS_FLAG(server
, NCP_FLAG_UTF8
))
166 strcpy(user
.iocharset
, "utf8");
167 else if (server
->nls_io
&& server
->nls_io
->charset
) {
168 len
= strlen(server
->nls_io
->charset
);
169 if (len
> NCP_IOCSNAME_LEN
)
170 len
= NCP_IOCSNAME_LEN
;
171 strncpy(user
.iocharset
, server
->nls_io
->charset
, len
);
172 user
.iocharset
[len
] = 0;
175 if (copy_to_user(arg
, &user
, sizeof(user
)))
179 #endif /* CONFIG_NCPFS_NLS */
181 int ncp_ioctl(struct inode
*inode
, struct file
*filp
,
182 unsigned int cmd
, unsigned long arg
)
184 struct ncp_server
*server
= NCP_SERVER(inode
);
186 struct ncp_ioctl_request request
;
188 void __user
*argp
= (void __user
*)arg
;
191 case NCP_IOC_NCPREQUEST
:
193 if ((permission(inode
, MAY_WRITE
, NULL
) != 0)
194 && (current
->uid
!= server
->m
.mounted_uid
)) {
197 if (copy_from_user(&request
, argp
, sizeof(request
)))
200 if ((request
.function
> 255)
202 NCP_PACKET_SIZE
- sizeof(struct ncp_request_header
))) {
205 bouncebuffer
= vmalloc(NCP_PACKET_SIZE_INTERNAL
);
208 if (copy_from_user(bouncebuffer
, request
.data
, request
.size
)) {
212 ncp_lock_server(server
);
214 /* FIXME: We hack around in the server's structures
215 here to be able to use ncp_request */
217 server
->has_subfunction
= 0;
218 server
->current_size
= request
.size
;
219 memcpy(server
->packet
, bouncebuffer
, request
.size
);
221 result
= ncp_request2(server
, request
.function
,
222 bouncebuffer
, NCP_PACKET_SIZE_INTERNAL
);
226 result
= server
->reply_size
;
227 ncp_unlock_server(server
);
228 DPRINTK("ncp_ioctl: copy %d bytes\n",
231 if (copy_to_user(request
.data
, bouncebuffer
, result
))
236 case NCP_IOC_CONN_LOGGED_IN
:
238 if (!capable(CAP_SYS_ADMIN
))
240 if (!(server
->m
.int_flags
& NCP_IMOUNT_LOGGEDIN_POSSIBLE
))
242 if (server
->root_setuped
)
244 server
->root_setuped
= 1;
245 return ncp_conn_logged_in(inode
->i_sb
);
247 case NCP_IOC_GET_FS_INFO
:
248 return ncp_get_fs_info(server
, inode
, argp
);
250 case NCP_IOC_GET_FS_INFO_V2
:
251 return ncp_get_fs_info_v2(server
, inode
, argp
);
253 case NCP_IOC_GETMOUNTUID2
:
255 unsigned long tmp
= server
->m
.mounted_uid
;
257 if ( (permission(inode
, MAY_READ
, NULL
) != 0)
258 && (current
->uid
!= server
->m
.mounted_uid
))
262 if (put_user(tmp
, (unsigned long __user
*)argp
))
267 case NCP_IOC_GETROOT
:
269 struct ncp_setroot_ioctl sr
;
271 if ( (permission(inode
, MAY_READ
, NULL
) != 0)
272 && (current
->uid
!= server
->m
.mounted_uid
))
276 if (server
->m
.mounted_vol
[0]) {
277 struct dentry
* dentry
= inode
->i_sb
->s_root
;
280 struct inode
* inode
= dentry
->d_inode
;
283 sr
.volNumber
= NCP_FINFO(inode
)->volNumber
;
284 sr
.dirEntNum
= NCP_FINFO(inode
)->dirEntNum
;
285 sr
.namespace = server
->name_space
[sr
.volNumber
];
287 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
289 DPRINTK("ncpfs: s_root==NULL\n");
295 if (copy_to_user(argp
, &sr
, sizeof(sr
)))
299 case NCP_IOC_SETROOT
:
301 struct ncp_setroot_ioctl sr
;
305 struct dentry
* dentry
;
307 if (!capable(CAP_SYS_ADMIN
))
311 if (server
->root_setuped
) return -EBUSY
;
312 if (copy_from_user(&sr
, argp
, sizeof(sr
)))
314 if (sr
.volNumber
< 0) {
315 server
->m
.mounted_vol
[0] = 0;
316 vnum
= NCP_NUMBER_OF_VOLUMES
;
319 } else if (sr
.volNumber
>= NCP_NUMBER_OF_VOLUMES
) {
321 } else if (ncp_mount_subdir(server
, sr
.volNumber
,
322 sr
.namespace, sr
.dirEntNum
,
323 &vnum
, &de
, &dosde
)) {
327 dentry
= inode
->i_sb
->s_root
;
328 server
->root_setuped
= 1;
330 struct inode
* inode
= dentry
->d_inode
;
333 NCP_FINFO(inode
)->volNumber
= vnum
;
334 NCP_FINFO(inode
)->dirEntNum
= de
;
335 NCP_FINFO(inode
)->DosDirNum
= dosde
;
337 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
339 DPRINTK("ncpfs: s_root==NULL\n");
344 #ifdef CONFIG_NCPFS_PACKET_SIGNING
345 case NCP_IOC_SIGN_INIT
:
346 if ((permission(inode
, MAY_WRITE
, NULL
) != 0)
347 && (current
->uid
!= server
->m
.mounted_uid
))
352 if (server
->sign_wanted
)
354 struct ncp_sign_init sign
;
356 if (copy_from_user(&sign
, argp
, sizeof(sign
)))
358 memcpy(server
->sign_root
,sign
.sign_root
,8);
359 memcpy(server
->sign_last
,sign
.sign_last
,16);
360 server
->sign_active
= 1;
362 /* ignore when signatures not wanted */
364 server
->sign_active
= 0;
368 case NCP_IOC_SIGN_WANTED
:
369 if ( (permission(inode
, MAY_READ
, NULL
) != 0)
370 && (current
->uid
!= server
->m
.mounted_uid
))
375 if (put_user(server
->sign_wanted
, (int __user
*)argp
))
378 case NCP_IOC_SET_SIGN_WANTED
:
382 if ( (permission(inode
, MAY_WRITE
, NULL
) != 0)
383 && (current
->uid
!= server
->m
.mounted_uid
))
387 /* get only low 8 bits... */
388 if (get_user(newstate
, (unsigned char __user
*)argp
))
390 if (server
->sign_active
) {
391 /* cannot turn signatures OFF when active */
392 if (!newstate
) return -EINVAL
;
394 server
->sign_wanted
= newstate
!= 0;
399 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
401 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
402 case NCP_IOC_LOCKUNLOCK
:
403 if ( (permission(inode
, MAY_WRITE
, NULL
) != 0)
404 && (current
->uid
!= server
->m
.mounted_uid
))
409 struct ncp_lock_ioctl rqdata
;
412 if (copy_from_user(&rqdata
, argp
, sizeof(rqdata
)))
414 if (rqdata
.origin
!= 0)
417 switch (rqdata
.cmd
) {
420 if (rqdata
.timeout
== 0)
421 rqdata
.timeout
= NCP_LOCK_DEFAULT_TIMEOUT
;
422 else if (rqdata
.timeout
> NCP_LOCK_MAX_TIMEOUT
)
423 rqdata
.timeout
= NCP_LOCK_MAX_TIMEOUT
;
426 rqdata
.timeout
= NCP_LOCK_DEFAULT_TIMEOUT
; /* has no effect */
432 /* locking needs both read and write access */
433 if ((result
= ncp_make_open(inode
, O_RDWR
)) != 0)
438 if (!ncp_conn_valid(server
))
441 if (!S_ISREG(inode
->i_mode
))
443 if (rqdata
.cmd
== NCP_LOCK_CLEAR
)
445 result
= ncp_ClearPhysicalRecord(NCP_SERVER(inode
),
446 NCP_FINFO(inode
)->file_handle
,
449 if (result
> 0) result
= 0; /* no such lock */
457 case NCP_LOCK_EX
: lockcmd
=1; break;
458 case NCP_LOCK_SH
: lockcmd
=3; break;
459 default: lockcmd
=0; break;
461 result
= ncp_LogPhysicalRecord(NCP_SERVER(inode
),
462 NCP_FINFO(inode
)->file_handle
,
467 if (result
> 0) result
= -EAGAIN
;
470 ncp_inode_close(inode
);
473 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
475 case NCP_IOC_GETOBJECTNAME
:
476 if (current
->uid
!= server
->m
.mounted_uid
) {
480 struct ncp_objectname_ioctl user
;
483 if (copy_from_user(&user
, argp
, sizeof(user
)))
485 user
.auth_type
= server
->auth
.auth_type
;
486 outl
= user
.object_name_len
;
487 user
.object_name_len
= server
->auth
.object_name_len
;
488 if (outl
> user
.object_name_len
)
489 outl
= user
.object_name_len
;
491 if (copy_to_user(user
.object_name
,
492 server
->auth
.object_name
,
493 outl
)) return -EFAULT
;
495 if (copy_to_user(argp
, &user
, sizeof(user
)))
499 case NCP_IOC_SETOBJECTNAME
:
500 if (current
->uid
!= server
->m
.mounted_uid
) {
504 struct ncp_objectname_ioctl user
;
509 size_t oldprivatelen
;
511 if (copy_from_user(&user
, argp
, sizeof(user
)))
513 if (user
.object_name_len
> NCP_OBJECT_NAME_MAX_LEN
)
515 if (user
.object_name_len
) {
516 newname
= ncp_kmalloc(user
.object_name_len
, GFP_USER
);
517 if (!newname
) return -ENOMEM
;
518 if (copy_from_user(newname
, user
.object_name
, user
.object_name_len
)) {
519 ncp_kfree_s(newname
, user
.object_name_len
);
525 /* enter critical section */
526 /* maybe that kfree can sleep so do that this way */
527 /* it is at least more SMP friendly (in future...) */
528 oldname
= server
->auth
.object_name
;
529 oldnamelen
= server
->auth
.object_name_len
;
530 oldprivate
= server
->priv
.data
;
531 oldprivatelen
= server
->priv
.len
;
532 server
->auth
.auth_type
= user
.auth_type
;
533 server
->auth
.object_name_len
= user
.object_name_len
;
534 server
->auth
.object_name
= newname
;
535 server
->priv
.len
= 0;
536 server
->priv
.data
= NULL
;
537 /* leave critical section */
538 if (oldprivate
) ncp_kfree_s(oldprivate
, oldprivatelen
);
539 if (oldname
) ncp_kfree_s(oldname
, oldnamelen
);
542 case NCP_IOC_GETPRIVATEDATA
:
543 if (current
->uid
!= server
->m
.mounted_uid
) {
547 struct ncp_privatedata_ioctl user
;
550 if (copy_from_user(&user
, argp
, sizeof(user
)))
553 user
.len
= server
->priv
.len
;
554 if (outl
> user
.len
) outl
= user
.len
;
556 if (copy_to_user(user
.data
,
558 outl
)) return -EFAULT
;
560 if (copy_to_user(argp
, &user
, sizeof(user
)))
564 case NCP_IOC_SETPRIVATEDATA
:
565 if (current
->uid
!= server
->m
.mounted_uid
) {
569 struct ncp_privatedata_ioctl user
;
574 if (copy_from_user(&user
, argp
, sizeof(user
)))
576 if (user
.len
> NCP_PRIVATE_DATA_MAX_LEN
)
579 new = ncp_kmalloc(user
.len
, GFP_USER
);
580 if (!new) return -ENOMEM
;
581 if (copy_from_user(new, user
.data
, user
.len
)) {
582 ncp_kfree_s(new, user
.len
);
588 /* enter critical section */
589 old
= server
->priv
.data
;
590 oldlen
= server
->priv
.len
;
591 server
->priv
.len
= user
.len
;
592 server
->priv
.data
= new;
593 /* leave critical section */
594 if (old
) ncp_kfree_s(old
, oldlen
);
598 #ifdef CONFIG_NCPFS_NLS
599 case NCP_IOC_SETCHARSETS
:
600 return ncp_set_charsets(server
, argp
);
602 case NCP_IOC_GETCHARSETS
:
603 return ncp_get_charsets(server
, argp
);
605 #endif /* CONFIG_NCPFS_NLS */
607 case NCP_IOC_SETDENTRYTTL
:
608 if ((permission(inode
, MAY_WRITE
, NULL
) != 0) &&
609 (current
->uid
!= server
->m
.mounted_uid
))
614 if (copy_from_user(&user
, argp
, sizeof(user
)))
616 /* 20 secs at most... */
619 user
= (user
* HZ
) / 1000;
620 server
->dentry_ttl
= user
;
624 case NCP_IOC_GETDENTRYTTL
:
626 u_int32_t user
= (server
->dentry_ttl
* 1000) / HZ
;
627 if (copy_to_user(argp
, &user
, sizeof(user
)))
633 /* #ifdef CONFIG_UID16 */
634 /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
635 so we have this out of switch */
636 if (cmd
== NCP_IOC_GETMOUNTUID
) {
637 __kernel_uid_t uid
= 0;
638 if ((permission(inode
, MAY_READ
, NULL
) != 0)
639 && (current
->uid
!= server
->m
.mounted_uid
)) {
642 SET_UID(uid
, server
->m
.mounted_uid
);
643 if (put_user(uid
, (__kernel_uid_t __user
*)argp
))