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/capability.h>
14 #include <linux/errno.h>
16 #include <linux/ioctl.h>
17 #include <linux/time.h>
19 #include <linux/highuid.h>
20 #include <linux/vmalloc.h>
22 #include <linux/ncp_fs.h>
24 #include "ncplib_kernel.h"
26 /* maximum limit for ncp_objectname_ioctl */
27 #define NCP_OBJECT_NAME_MAX_LEN 4096
28 /* maximum limit for ncp_privatedata_ioctl */
29 #define NCP_PRIVATE_DATA_MAX_LEN 8192
30 /* maximum negotiable packet size */
31 #define NCP_PACKET_SIZE_INTERNAL 65536
34 ncp_get_fs_info(struct ncp_server * server, struct file *file,
35 struct ncp_fs_info __user *arg)
37 struct inode *inode = file->f_dentry->d_inode;
38 struct ncp_fs_info info;
40 if ((file_permission(file, MAY_WRITE) != 0)
41 && (current->uid != server->m.mounted_uid)) {
44 if (copy_from_user(&info, arg, sizeof(info)))
47 if (info.version != NCP_GET_FS_INFO_VERSION) {
48 DPRINTK("info.version invalid: %d\n", info.version);
51 /* TODO: info.addr = server->m.serv_addr; */
52 SET_UID(info.mounted_uid, server->m.mounted_uid);
53 info.connection = server->connection;
54 info.buffer_size = server->buffer_size;
55 info.volume_number = NCP_FINFO(inode)->volNumber;
56 info.directory_id = NCP_FINFO(inode)->DosDirNum;
58 if (copy_to_user(arg, &info, sizeof(info)))
64 ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
65 struct ncp_fs_info_v2 __user * arg)
67 struct inode *inode = file->f_dentry->d_inode;
68 struct ncp_fs_info_v2 info2;
70 if ((file_permission(file, MAY_WRITE) != 0)
71 && (current->uid != server->m.mounted_uid)) {
74 if (copy_from_user(&info2, arg, sizeof(info2)))
77 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
78 DPRINTK("info.version invalid: %d\n", info2.version);
81 info2.mounted_uid = server->m.mounted_uid;
82 info2.connection = server->connection;
83 info2.buffer_size = server->buffer_size;
84 info2.volume_number = NCP_FINFO(inode)->volNumber;
85 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
86 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
88 if (copy_to_user(arg, &info2, sizeof(info2)))
93 #ifdef CONFIG_NCPFS_NLS
94 /* Here we are select the iocharset and the codepage for NLS.
95 * Thanks Petr Vandrovec for idea and many hints.
98 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
100 struct ncp_nls_ioctl user;
101 struct nls_table *codepage;
102 struct nls_table *iocharset;
103 struct nls_table *oldset_io;
104 struct nls_table *oldset_cp;
106 if (!capable(CAP_SYS_ADMIN))
108 if (server->root_setuped)
111 if (copy_from_user(&user, arg, sizeof(user)))
115 user.codepage[NCP_IOCSNAME_LEN] = 0;
116 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
117 codepage = load_nls_default();
119 codepage = load_nls(user.codepage);
126 user.iocharset[NCP_IOCSNAME_LEN] = 0;
127 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
128 iocharset = load_nls_default();
129 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
130 } else if (!strcmp(user.iocharset, "utf8")) {
131 iocharset = load_nls_default();
132 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
134 iocharset = load_nls(user.iocharset);
136 unload_nls(codepage);
139 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
142 oldset_cp = server->nls_vol;
143 server->nls_vol = codepage;
144 oldset_io = server->nls_io;
145 server->nls_io = iocharset;
148 unload_nls(oldset_cp);
150 unload_nls(oldset_io);
156 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
158 struct ncp_nls_ioctl user;
161 memset(&user, 0, sizeof(user));
162 if (server->nls_vol && server->nls_vol->charset) {
163 len = strlen(server->nls_vol->charset);
164 if (len > NCP_IOCSNAME_LEN)
165 len = NCP_IOCSNAME_LEN;
166 strncpy(user.codepage, server->nls_vol->charset, len);
167 user.codepage[len] = 0;
170 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
171 strcpy(user.iocharset, "utf8");
172 else if (server->nls_io && server->nls_io->charset) {
173 len = strlen(server->nls_io->charset);
174 if (len > NCP_IOCSNAME_LEN)
175 len = NCP_IOCSNAME_LEN;
176 strncpy(user.iocharset, server->nls_io->charset, len);
177 user.iocharset[len] = 0;
180 if (copy_to_user(arg, &user, sizeof(user)))
184 #endif /* CONFIG_NCPFS_NLS */
186 int ncp_ioctl(struct inode *inode, struct file *filp,
187 unsigned int cmd, unsigned long arg)
189 struct ncp_server *server = NCP_SERVER(inode);
191 struct ncp_ioctl_request request;
193 void __user *argp = (void __user *)arg;
196 case NCP_IOC_NCPREQUEST:
198 if ((file_permission(filp, MAY_WRITE) != 0)
199 && (current->uid != server->m.mounted_uid)) {
202 if (copy_from_user(&request, argp, sizeof(request)))
205 if ((request.function > 255)
207 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
210 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
213 if (copy_from_user(bouncebuffer, request.data, request.size)) {
217 ncp_lock_server(server);
219 /* FIXME: We hack around in the server's structures
220 here to be able to use ncp_request */
222 server->has_subfunction = 0;
223 server->current_size = request.size;
224 memcpy(server->packet, bouncebuffer, request.size);
226 result = ncp_request2(server, request.function,
227 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
231 result = server->reply_size;
232 ncp_unlock_server(server);
233 DPRINTK("ncp_ioctl: copy %d bytes\n",
236 if (copy_to_user(request.data, bouncebuffer, result))
241 case NCP_IOC_CONN_LOGGED_IN:
243 if (!capable(CAP_SYS_ADMIN))
245 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
247 if (server->root_setuped)
249 server->root_setuped = 1;
250 return ncp_conn_logged_in(inode->i_sb);
252 case NCP_IOC_GET_FS_INFO:
253 return ncp_get_fs_info(server, filp, argp);
255 case NCP_IOC_GET_FS_INFO_V2:
256 return ncp_get_fs_info_v2(server, filp, argp);
258 case NCP_IOC_GETMOUNTUID2:
260 unsigned long tmp = server->m.mounted_uid;
262 if ((file_permission(filp, MAY_READ) != 0)
263 && (current->uid != server->m.mounted_uid))
267 if (put_user(tmp, (unsigned long __user *)argp))
272 case NCP_IOC_GETROOT:
274 struct ncp_setroot_ioctl sr;
276 if ((file_permission(filp, MAY_READ) != 0)
277 && (current->uid != server->m.mounted_uid))
281 if (server->m.mounted_vol[0]) {
282 struct dentry* dentry = inode->i_sb->s_root;
285 struct inode* inode = dentry->d_inode;
288 sr.volNumber = NCP_FINFO(inode)->volNumber;
289 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
290 sr.namespace = server->name_space[sr.volNumber];
292 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
294 DPRINTK("ncpfs: s_root==NULL\n");
300 if (copy_to_user(argp, &sr, sizeof(sr)))
304 case NCP_IOC_SETROOT:
306 struct ncp_setroot_ioctl sr;
310 struct dentry* dentry;
312 if (!capable(CAP_SYS_ADMIN))
316 if (server->root_setuped) return -EBUSY;
317 if (copy_from_user(&sr, argp, sizeof(sr)))
319 if (sr.volNumber < 0) {
320 server->m.mounted_vol[0] = 0;
321 vnum = NCP_NUMBER_OF_VOLUMES;
324 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
326 } else if (ncp_mount_subdir(server, sr.volNumber,
327 sr.namespace, sr.dirEntNum,
328 &vnum, &de, &dosde)) {
332 dentry = inode->i_sb->s_root;
333 server->root_setuped = 1;
335 struct inode* inode = dentry->d_inode;
338 NCP_FINFO(inode)->volNumber = vnum;
339 NCP_FINFO(inode)->dirEntNum = de;
340 NCP_FINFO(inode)->DosDirNum = dosde;
342 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
344 DPRINTK("ncpfs: s_root==NULL\n");
349 #ifdef CONFIG_NCPFS_PACKET_SIGNING
350 case NCP_IOC_SIGN_INIT:
351 if ((file_permission(filp, MAY_WRITE) != 0)
352 && (current->uid != server->m.mounted_uid))
357 if (server->sign_wanted)
359 struct ncp_sign_init sign;
361 if (copy_from_user(&sign, argp, sizeof(sign)))
363 memcpy(server->sign_root,sign.sign_root,8);
364 memcpy(server->sign_last,sign.sign_last,16);
365 server->sign_active = 1;
367 /* ignore when signatures not wanted */
369 server->sign_active = 0;
373 case NCP_IOC_SIGN_WANTED:
374 if ((file_permission(filp, MAY_READ) != 0)
375 && (current->uid != server->m.mounted_uid))
380 if (put_user(server->sign_wanted, (int __user *)argp))
383 case NCP_IOC_SET_SIGN_WANTED:
387 if ((file_permission(filp, MAY_WRITE) != 0)
388 && (current->uid != server->m.mounted_uid))
392 /* get only low 8 bits... */
393 if (get_user(newstate, (unsigned char __user *)argp))
395 if (server->sign_active) {
396 /* cannot turn signatures OFF when active */
397 if (!newstate) return -EINVAL;
399 server->sign_wanted = newstate != 0;
404 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
406 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
407 case NCP_IOC_LOCKUNLOCK:
408 if ((file_permission(filp, MAY_WRITE) != 0)
409 && (current->uid != server->m.mounted_uid))
414 struct ncp_lock_ioctl rqdata;
417 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
419 if (rqdata.origin != 0)
422 switch (rqdata.cmd) {
425 if (rqdata.timeout == 0)
426 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
427 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
428 rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
431 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
437 /* locking needs both read and write access */
438 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
443 if (!ncp_conn_valid(server))
446 if (!S_ISREG(inode->i_mode))
448 if (rqdata.cmd == NCP_LOCK_CLEAR)
450 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
451 NCP_FINFO(inode)->file_handle,
454 if (result > 0) result = 0; /* no such lock */
462 case NCP_LOCK_EX: lockcmd=1; break;
463 case NCP_LOCK_SH: lockcmd=3; break;
464 default: lockcmd=0; break;
466 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
467 NCP_FINFO(inode)->file_handle,
472 if (result > 0) result = -EAGAIN;
475 ncp_inode_close(inode);
478 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
480 case NCP_IOC_GETOBJECTNAME:
481 if (current->uid != server->m.mounted_uid) {
485 struct ncp_objectname_ioctl user;
488 if (copy_from_user(&user, argp, sizeof(user)))
490 user.auth_type = server->auth.auth_type;
491 outl = user.object_name_len;
492 user.object_name_len = server->auth.object_name_len;
493 if (outl > user.object_name_len)
494 outl = user.object_name_len;
496 if (copy_to_user(user.object_name,
497 server->auth.object_name,
498 outl)) return -EFAULT;
500 if (copy_to_user(argp, &user, sizeof(user)))
504 case NCP_IOC_SETOBJECTNAME:
505 if (current->uid != server->m.mounted_uid) {
509 struct ncp_objectname_ioctl user;
514 size_t oldprivatelen;
516 if (copy_from_user(&user, argp, sizeof(user)))
518 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
520 if (user.object_name_len) {
521 newname = kmalloc(user.object_name_len, GFP_USER);
524 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
531 /* enter critical section */
532 /* maybe that kfree can sleep so do that this way */
533 /* it is at least more SMP friendly (in future...) */
534 oldname = server->auth.object_name;
535 oldnamelen = server->auth.object_name_len;
536 oldprivate = server->priv.data;
537 oldprivatelen = server->priv.len;
538 server->auth.auth_type = user.auth_type;
539 server->auth.object_name_len = user.object_name_len;
540 server->auth.object_name = newname;
541 server->priv.len = 0;
542 server->priv.data = NULL;
543 /* leave critical section */
548 case NCP_IOC_GETPRIVATEDATA:
549 if (current->uid != server->m.mounted_uid) {
553 struct ncp_privatedata_ioctl user;
556 if (copy_from_user(&user, argp, sizeof(user)))
559 user.len = server->priv.len;
560 if (outl > user.len) outl = user.len;
562 if (copy_to_user(user.data,
564 outl)) return -EFAULT;
566 if (copy_to_user(argp, &user, sizeof(user)))
570 case NCP_IOC_SETPRIVATEDATA:
571 if (current->uid != server->m.mounted_uid) {
575 struct ncp_privatedata_ioctl user;
580 if (copy_from_user(&user, argp, sizeof(user)))
582 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
585 new = kmalloc(user.len, GFP_USER);
588 if (copy_from_user(new, user.data, user.len)) {
595 /* enter critical section */
596 old = server->priv.data;
597 oldlen = server->priv.len;
598 server->priv.len = user.len;
599 server->priv.data = new;
600 /* leave critical section */
605 #ifdef CONFIG_NCPFS_NLS
606 case NCP_IOC_SETCHARSETS:
607 return ncp_set_charsets(server, argp);
609 case NCP_IOC_GETCHARSETS:
610 return ncp_get_charsets(server, argp);
612 #endif /* CONFIG_NCPFS_NLS */
614 case NCP_IOC_SETDENTRYTTL:
615 if ((file_permission(filp, MAY_WRITE) != 0) &&
616 (current->uid != server->m.mounted_uid))
621 if (copy_from_user(&user, argp, sizeof(user)))
623 /* 20 secs at most... */
626 user = (user * HZ) / 1000;
627 server->dentry_ttl = user;
631 case NCP_IOC_GETDENTRYTTL:
633 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
634 if (copy_to_user(argp, &user, sizeof(user)))
640 /* #ifdef CONFIG_UID16 */
641 /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
642 so we have this out of switch */
643 if (cmd == NCP_IOC_GETMOUNTUID) {
644 __kernel_uid_t uid = 0;
645 if ((file_permission(filp, MAY_READ) != 0)
646 && (current->uid != server->m.mounted_uid)) {
649 SET_UID(uid, server->m.mounted_uid);
650 if (put_user(uid, (__kernel_uid_t __user *)argp))