kbuild: tags file generation fixup
[linux-2.6/linux-2.6-openrd.git] / fs / ncpfs / ioctl.c
blobfd3efdca5ae39cc5d923392785ae2c03bbb88c5c
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/config.h>
12 #include <asm/uaccess.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/ioctl.h>
16 #include <linux/time.h>
17 #include <linux/mm.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
32 static int
33 ncp_get_fs_info(struct ncp_server * server, struct file *file,
34 struct ncp_fs_info __user *arg)
36 struct inode *inode = file->f_dentry->d_inode;
37 struct ncp_fs_info info;
39 if ((file_permission(file, MAY_WRITE) != 0)
40 && (current->uid != server->m.mounted_uid)) {
41 return -EACCES;
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 file *file,
64 struct ncp_fs_info_v2 __user * arg)
66 struct inode *inode = file->f_dentry->d_inode;
67 struct ncp_fs_info_v2 info2;
69 if ((file_permission(file, MAY_WRITE) != 0)
70 && (current->uid != server->m.mounted_uid)) {
71 return -EACCES;
73 if (copy_from_user(&info2, arg, sizeof(info2)))
74 return -EFAULT;
76 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
77 DPRINTK("info.version invalid: %d\n", info2.version);
78 return -EINVAL;
80 info2.mounted_uid = server->m.mounted_uid;
81 info2.connection = server->connection;
82 info2.buffer_size = server->buffer_size;
83 info2.volume_number = NCP_FINFO(inode)->volNumber;
84 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
85 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
87 if (copy_to_user(arg, &info2, sizeof(info2)))
88 return -EFAULT;
89 return 0;
92 #ifdef CONFIG_NCPFS_NLS
93 /* Here we are select the iocharset and the codepage for NLS.
94 * Thanks Petr Vandrovec for idea and many hints.
96 static int
97 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
99 struct ncp_nls_ioctl user;
100 struct nls_table *codepage;
101 struct nls_table *iocharset;
102 struct nls_table *oldset_io;
103 struct nls_table *oldset_cp;
105 if (!capable(CAP_SYS_ADMIN))
106 return -EACCES;
107 if (server->root_setuped)
108 return -EBUSY;
110 if (copy_from_user(&user, arg, sizeof(user)))
111 return -EFAULT;
113 codepage = NULL;
114 user.codepage[NCP_IOCSNAME_LEN] = 0;
115 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
116 codepage = load_nls_default();
117 else {
118 codepage = load_nls(user.codepage);
119 if (!codepage) {
120 return -EBADRQC;
124 iocharset = NULL;
125 user.iocharset[NCP_IOCSNAME_LEN] = 0;
126 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
127 iocharset = load_nls_default();
128 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
129 } else if (!strcmp(user.iocharset, "utf8")) {
130 iocharset = load_nls_default();
131 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
132 } else {
133 iocharset = load_nls(user.iocharset);
134 if (!iocharset) {
135 unload_nls(codepage);
136 return -EBADRQC;
138 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
141 oldset_cp = server->nls_vol;
142 server->nls_vol = codepage;
143 oldset_io = server->nls_io;
144 server->nls_io = iocharset;
146 if (oldset_cp)
147 unload_nls(oldset_cp);
148 if (oldset_io)
149 unload_nls(oldset_io);
151 return 0;
154 static int
155 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
157 struct ncp_nls_ioctl user;
158 int len;
160 memset(&user, 0, sizeof(user));
161 if (server->nls_vol && server->nls_vol->charset) {
162 len = strlen(server->nls_vol->charset);
163 if (len > NCP_IOCSNAME_LEN)
164 len = NCP_IOCSNAME_LEN;
165 strncpy(user.codepage, server->nls_vol->charset, len);
166 user.codepage[len] = 0;
169 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
170 strcpy(user.iocharset, "utf8");
171 else if (server->nls_io && server->nls_io->charset) {
172 len = strlen(server->nls_io->charset);
173 if (len > NCP_IOCSNAME_LEN)
174 len = NCP_IOCSNAME_LEN;
175 strncpy(user.iocharset, server->nls_io->charset, len);
176 user.iocharset[len] = 0;
179 if (copy_to_user(arg, &user, sizeof(user)))
180 return -EFAULT;
181 return 0;
183 #endif /* CONFIG_NCPFS_NLS */
185 int ncp_ioctl(struct inode *inode, struct file *filp,
186 unsigned int cmd, unsigned long arg)
188 struct ncp_server *server = NCP_SERVER(inode);
189 int result;
190 struct ncp_ioctl_request request;
191 char* bouncebuffer;
192 void __user *argp = (void __user *)arg;
194 switch (cmd) {
195 case NCP_IOC_NCPREQUEST:
197 if ((file_permission(filp, MAY_WRITE) != 0)
198 && (current->uid != server->m.mounted_uid)) {
199 return -EACCES;
201 if (copy_from_user(&request, argp, sizeof(request)))
202 return -EFAULT;
204 if ((request.function > 255)
205 || (request.size >
206 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
207 return -EINVAL;
209 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
210 if (!bouncebuffer)
211 return -ENOMEM;
212 if (copy_from_user(bouncebuffer, request.data, request.size)) {
213 vfree(bouncebuffer);
214 return -EFAULT;
216 ncp_lock_server(server);
218 /* FIXME: We hack around in the server's structures
219 here to be able to use ncp_request */
221 server->has_subfunction = 0;
222 server->current_size = request.size;
223 memcpy(server->packet, bouncebuffer, request.size);
225 result = ncp_request2(server, request.function,
226 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
227 if (result < 0)
228 result = -EIO;
229 else
230 result = server->reply_size;
231 ncp_unlock_server(server);
232 DPRINTK("ncp_ioctl: copy %d bytes\n",
233 result);
234 if (result >= 0)
235 if (copy_to_user(request.data, bouncebuffer, result))
236 result = -EFAULT;
237 vfree(bouncebuffer);
238 return result;
240 case NCP_IOC_CONN_LOGGED_IN:
242 if (!capable(CAP_SYS_ADMIN))
243 return -EACCES;
244 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
245 return -EINVAL;
246 if (server->root_setuped)
247 return -EBUSY;
248 server->root_setuped = 1;
249 return ncp_conn_logged_in(inode->i_sb);
251 case NCP_IOC_GET_FS_INFO:
252 return ncp_get_fs_info(server, filp, argp);
254 case NCP_IOC_GET_FS_INFO_V2:
255 return ncp_get_fs_info_v2(server, filp, argp);
257 case NCP_IOC_GETMOUNTUID2:
259 unsigned long tmp = server->m.mounted_uid;
261 if ((file_permission(filp, MAY_READ) != 0)
262 && (current->uid != server->m.mounted_uid))
264 return -EACCES;
266 if (put_user(tmp, (unsigned long __user *)argp))
267 return -EFAULT;
268 return 0;
271 case NCP_IOC_GETROOT:
273 struct ncp_setroot_ioctl sr;
275 if ((file_permission(filp, MAY_READ) != 0)
276 && (current->uid != server->m.mounted_uid))
278 return -EACCES;
280 if (server->m.mounted_vol[0]) {
281 struct dentry* dentry = inode->i_sb->s_root;
283 if (dentry) {
284 struct inode* inode = dentry->d_inode;
286 if (inode) {
287 sr.volNumber = NCP_FINFO(inode)->volNumber;
288 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
289 sr.namespace = server->name_space[sr.volNumber];
290 } else
291 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
292 } else
293 DPRINTK("ncpfs: s_root==NULL\n");
294 } else {
295 sr.volNumber = -1;
296 sr.namespace = 0;
297 sr.dirEntNum = 0;
299 if (copy_to_user(argp, &sr, sizeof(sr)))
300 return -EFAULT;
301 return 0;
303 case NCP_IOC_SETROOT:
305 struct ncp_setroot_ioctl sr;
306 __u32 vnum;
307 __le32 de;
308 __le32 dosde;
309 struct dentry* dentry;
311 if (!capable(CAP_SYS_ADMIN))
313 return -EACCES;
315 if (server->root_setuped) return -EBUSY;
316 if (copy_from_user(&sr, argp, sizeof(sr)))
317 return -EFAULT;
318 if (sr.volNumber < 0) {
319 server->m.mounted_vol[0] = 0;
320 vnum = NCP_NUMBER_OF_VOLUMES;
321 de = 0;
322 dosde = 0;
323 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
324 return -EINVAL;
325 } else if (ncp_mount_subdir(server, sr.volNumber,
326 sr.namespace, sr.dirEntNum,
327 &vnum, &de, &dosde)) {
328 return -ENOENT;
331 dentry = inode->i_sb->s_root;
332 server->root_setuped = 1;
333 if (dentry) {
334 struct inode* inode = dentry->d_inode;
336 if (inode) {
337 NCP_FINFO(inode)->volNumber = vnum;
338 NCP_FINFO(inode)->dirEntNum = de;
339 NCP_FINFO(inode)->DosDirNum = dosde;
340 } else
341 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
342 } else
343 DPRINTK("ncpfs: s_root==NULL\n");
345 return 0;
348 #ifdef CONFIG_NCPFS_PACKET_SIGNING
349 case NCP_IOC_SIGN_INIT:
350 if ((file_permission(filp, MAY_WRITE) != 0)
351 && (current->uid != server->m.mounted_uid))
353 return -EACCES;
355 if (argp) {
356 if (server->sign_wanted)
358 struct ncp_sign_init sign;
360 if (copy_from_user(&sign, argp, sizeof(sign)))
361 return -EFAULT;
362 memcpy(server->sign_root,sign.sign_root,8);
363 memcpy(server->sign_last,sign.sign_last,16);
364 server->sign_active = 1;
366 /* ignore when signatures not wanted */
367 } else {
368 server->sign_active = 0;
370 return 0;
372 case NCP_IOC_SIGN_WANTED:
373 if ((file_permission(filp, MAY_READ) != 0)
374 && (current->uid != server->m.mounted_uid))
376 return -EACCES;
379 if (put_user(server->sign_wanted, (int __user *)argp))
380 return -EFAULT;
381 return 0;
382 case NCP_IOC_SET_SIGN_WANTED:
384 int newstate;
386 if ((file_permission(filp, MAY_WRITE) != 0)
387 && (current->uid != server->m.mounted_uid))
389 return -EACCES;
391 /* get only low 8 bits... */
392 if (get_user(newstate, (unsigned char __user *)argp))
393 return -EFAULT;
394 if (server->sign_active) {
395 /* cannot turn signatures OFF when active */
396 if (!newstate) return -EINVAL;
397 } else {
398 server->sign_wanted = newstate != 0;
400 return 0;
403 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
405 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
406 case NCP_IOC_LOCKUNLOCK:
407 if ((file_permission(filp, MAY_WRITE) != 0)
408 && (current->uid != server->m.mounted_uid))
410 return -EACCES;
413 struct ncp_lock_ioctl rqdata;
414 int result;
416 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
417 return -EFAULT;
418 if (rqdata.origin != 0)
419 return -EINVAL;
420 /* check for cmd */
421 switch (rqdata.cmd) {
422 case NCP_LOCK_EX:
423 case NCP_LOCK_SH:
424 if (rqdata.timeout == 0)
425 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
426 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
427 rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
428 break;
429 case NCP_LOCK_LOG:
430 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
431 case NCP_LOCK_CLEAR:
432 break;
433 default:
434 return -EINVAL;
436 /* locking needs both read and write access */
437 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
439 return result;
441 result = -EIO;
442 if (!ncp_conn_valid(server))
443 goto outrel;
444 result = -EISDIR;
445 if (!S_ISREG(inode->i_mode))
446 goto outrel;
447 if (rqdata.cmd == NCP_LOCK_CLEAR)
449 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
450 NCP_FINFO(inode)->file_handle,
451 rqdata.offset,
452 rqdata.length);
453 if (result > 0) result = 0; /* no such lock */
455 else
457 int lockcmd;
459 switch (rqdata.cmd)
461 case NCP_LOCK_EX: lockcmd=1; break;
462 case NCP_LOCK_SH: lockcmd=3; break;
463 default: lockcmd=0; break;
465 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
466 NCP_FINFO(inode)->file_handle,
467 lockcmd,
468 rqdata.offset,
469 rqdata.length,
470 rqdata.timeout);
471 if (result > 0) result = -EAGAIN;
473 outrel:
474 ncp_inode_close(inode);
475 return result;
477 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
479 case NCP_IOC_GETOBJECTNAME:
480 if (current->uid != server->m.mounted_uid) {
481 return -EACCES;
484 struct ncp_objectname_ioctl user;
485 size_t outl;
487 if (copy_from_user(&user, argp, sizeof(user)))
488 return -EFAULT;
489 user.auth_type = server->auth.auth_type;
490 outl = user.object_name_len;
491 user.object_name_len = server->auth.object_name_len;
492 if (outl > user.object_name_len)
493 outl = user.object_name_len;
494 if (outl) {
495 if (copy_to_user(user.object_name,
496 server->auth.object_name,
497 outl)) return -EFAULT;
499 if (copy_to_user(argp, &user, sizeof(user)))
500 return -EFAULT;
501 return 0;
503 case NCP_IOC_SETOBJECTNAME:
504 if (current->uid != server->m.mounted_uid) {
505 return -EACCES;
508 struct ncp_objectname_ioctl user;
509 void* newname;
510 void* oldname;
511 size_t oldnamelen;
512 void* oldprivate;
513 size_t oldprivatelen;
515 if (copy_from_user(&user, argp, sizeof(user)))
516 return -EFAULT;
517 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
518 return -ENOMEM;
519 if (user.object_name_len) {
520 newname = ncp_kmalloc(user.object_name_len, GFP_USER);
521 if (!newname) return -ENOMEM;
522 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
523 ncp_kfree_s(newname, user.object_name_len);
524 return -EFAULT;
526 } else {
527 newname = NULL;
529 /* enter critical section */
530 /* maybe that kfree can sleep so do that this way */
531 /* it is at least more SMP friendly (in future...) */
532 oldname = server->auth.object_name;
533 oldnamelen = server->auth.object_name_len;
534 oldprivate = server->priv.data;
535 oldprivatelen = server->priv.len;
536 server->auth.auth_type = user.auth_type;
537 server->auth.object_name_len = user.object_name_len;
538 server->auth.object_name = newname;
539 server->priv.len = 0;
540 server->priv.data = NULL;
541 /* leave critical section */
542 if (oldprivate) ncp_kfree_s(oldprivate, oldprivatelen);
543 if (oldname) ncp_kfree_s(oldname, oldnamelen);
544 return 0;
546 case NCP_IOC_GETPRIVATEDATA:
547 if (current->uid != server->m.mounted_uid) {
548 return -EACCES;
551 struct ncp_privatedata_ioctl user;
552 size_t outl;
554 if (copy_from_user(&user, argp, sizeof(user)))
555 return -EFAULT;
556 outl = user.len;
557 user.len = server->priv.len;
558 if (outl > user.len) outl = user.len;
559 if (outl) {
560 if (copy_to_user(user.data,
561 server->priv.data,
562 outl)) return -EFAULT;
564 if (copy_to_user(argp, &user, sizeof(user)))
565 return -EFAULT;
566 return 0;
568 case NCP_IOC_SETPRIVATEDATA:
569 if (current->uid != server->m.mounted_uid) {
570 return -EACCES;
573 struct ncp_privatedata_ioctl user;
574 void* new;
575 void* old;
576 size_t oldlen;
578 if (copy_from_user(&user, argp, sizeof(user)))
579 return -EFAULT;
580 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
581 return -ENOMEM;
582 if (user.len) {
583 new = ncp_kmalloc(user.len, GFP_USER);
584 if (!new) return -ENOMEM;
585 if (copy_from_user(new, user.data, user.len)) {
586 ncp_kfree_s(new, user.len);
587 return -EFAULT;
589 } else {
590 new = NULL;
592 /* enter critical section */
593 old = server->priv.data;
594 oldlen = server->priv.len;
595 server->priv.len = user.len;
596 server->priv.data = new;
597 /* leave critical section */
598 if (old) ncp_kfree_s(old, oldlen);
599 return 0;
602 #ifdef CONFIG_NCPFS_NLS
603 case NCP_IOC_SETCHARSETS:
604 return ncp_set_charsets(server, argp);
606 case NCP_IOC_GETCHARSETS:
607 return ncp_get_charsets(server, argp);
609 #endif /* CONFIG_NCPFS_NLS */
611 case NCP_IOC_SETDENTRYTTL:
612 if ((file_permission(filp, MAY_WRITE) != 0) &&
613 (current->uid != server->m.mounted_uid))
614 return -EACCES;
616 u_int32_t user;
618 if (copy_from_user(&user, argp, sizeof(user)))
619 return -EFAULT;
620 /* 20 secs at most... */
621 if (user > 20000)
622 return -EINVAL;
623 user = (user * HZ) / 1000;
624 server->dentry_ttl = user;
625 return 0;
628 case NCP_IOC_GETDENTRYTTL:
630 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
631 if (copy_to_user(argp, &user, sizeof(user)))
632 return -EFAULT;
633 return 0;
637 /* #ifdef CONFIG_UID16 */
638 /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
639 so we have this out of switch */
640 if (cmd == NCP_IOC_GETMOUNTUID) {
641 __kernel_uid_t uid = 0;
642 if ((file_permission(filp, MAY_READ) != 0)
643 && (current->uid != server->m.mounted_uid)) {
644 return -EACCES;
646 SET_UID(uid, server->m.mounted_uid);
647 if (put_user(uid, (__kernel_uid_t __user *)argp))
648 return -EFAULT;
649 return 0;
651 /* #endif */
652 return -EINVAL;