blob: 88df79356a1f333b75f079f6940dc9492709312d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ioctl.c
3 *
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
7 *
8 */
9
10#include <linux/config.h>
11
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>
20
21#include <linux/ncp_fs.h>
22
23#include "ncplib_kernel.h"
24
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
31
32static int
33ncp_get_fs_info(struct ncp_server* server, struct inode* inode, struct ncp_fs_info __user *arg)
34{
35 struct ncp_fs_info info;
36
37 if ((permission(inode, MAY_WRITE, NULL) != 0)
38 && (current->uid != server->m.mounted_uid)) {
39 return -EACCES;
40 }
41 if (copy_from_user(&info, arg, sizeof(info)))
42 return -EFAULT;
43
44 if (info.version != NCP_GET_FS_INFO_VERSION) {
45 DPRINTK("info.version invalid: %d\n", info.version);
46 return -EINVAL;
47 }
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;
54
55 if (copy_to_user(arg, &info, sizeof(info)))
56 return -EFAULT;
57 return 0;
58}
59
60static int
61ncp_get_fs_info_v2(struct ncp_server* server, struct inode* inode, struct ncp_fs_info_v2 __user * arg)
62{
63 struct ncp_fs_info_v2 info2;
64
65 if ((permission(inode, MAY_WRITE, NULL) != 0)
66 && (current->uid != server->m.mounted_uid)) {
67 return -EACCES;
68 }
69 if (copy_from_user(&info2, arg, sizeof(info2)))
70 return -EFAULT;
71
72 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
73 DPRINTK("info.version invalid: %d\n", info2.version);
74 return -EINVAL;
75 }
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;
82
83 if (copy_to_user(arg, &info2, sizeof(info2)))
84 return -EFAULT;
85 return 0;
86}
87
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.
91 */
92static int
93ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
94{
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;
100
101 if (!capable(CAP_SYS_ADMIN))
102 return -EACCES;
103 if (server->root_setuped)
104 return -EBUSY;
105
106 if (copy_from_user(&user, arg, sizeof(user)))
107 return -EFAULT;
108
109 codepage = NULL;
110 user.codepage[NCP_IOCSNAME_LEN] = 0;
111 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
112 codepage = load_nls_default();
113 else {
114 codepage = load_nls(user.codepage);
115 if (!codepage) {
116 return -EBADRQC;
117 }
118 }
119
120 iocharset = NULL;
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);
128 } else {
129 iocharset = load_nls(user.iocharset);
130 if (!iocharset) {
131 unload_nls(codepage);
132 return -EBADRQC;
133 }
134 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
135 }
136
137 oldset_cp = server->nls_vol;
138 server->nls_vol = codepage;
139 oldset_io = server->nls_io;
140 server->nls_io = iocharset;
141
142 if (oldset_cp)
143 unload_nls(oldset_cp);
144 if (oldset_io)
145 unload_nls(oldset_io);
146
147 return 0;
148}
149
150static int
151ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
152{
153 struct ncp_nls_ioctl user;
154 int len;
155
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;
163 }
164
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;
173 }
174
175 if (copy_to_user(arg, &user, sizeof(user)))
176 return -EFAULT;
177 return 0;
178}
179#endif /* CONFIG_NCPFS_NLS */
180
181int ncp_ioctl(struct inode *inode, struct file *filp,
182 unsigned int cmd, unsigned long arg)
183{
184 struct ncp_server *server = NCP_SERVER(inode);
185 int result;
186 struct ncp_ioctl_request request;
187 char* bouncebuffer;
188 void __user *argp = (void __user *)arg;
189
190 switch (cmd) {
191 case NCP_IOC_NCPREQUEST:
192
193 if ((permission(inode, MAY_WRITE, NULL) != 0)
194 && (current->uid != server->m.mounted_uid)) {
195 return -EACCES;
196 }
197 if (copy_from_user(&request, argp, sizeof(request)))
198 return -EFAULT;
199
200 if ((request.function > 255)
201 || (request.size >
202 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
203 return -EINVAL;
204 }
205 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
206 if (!bouncebuffer)
207 return -ENOMEM;
208 if (copy_from_user(bouncebuffer, request.data, request.size)) {
209 vfree(bouncebuffer);
210 return -EFAULT;
211 }
212 ncp_lock_server(server);
213
214 /* FIXME: We hack around in the server's structures
215 here to be able to use ncp_request */
216
217 server->has_subfunction = 0;
218 server->current_size = request.size;
219 memcpy(server->packet, bouncebuffer, request.size);
220
221 result = ncp_request2(server, request.function,
222 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
223 if (result < 0)
224 result = -EIO;
225 else
226 result = server->reply_size;
227 ncp_unlock_server(server);
228 DPRINTK("ncp_ioctl: copy %d bytes\n",
229 result);
230 if (result >= 0)
231 if (copy_to_user(request.data, bouncebuffer, result))
232 result = -EFAULT;
233 vfree(bouncebuffer);
234 return result;
235
236 case NCP_IOC_CONN_LOGGED_IN:
237
238 if (!capable(CAP_SYS_ADMIN))
239 return -EACCES;
240 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
241 return -EINVAL;
242 if (server->root_setuped)
243 return -EBUSY;
244 server->root_setuped = 1;
245 return ncp_conn_logged_in(inode->i_sb);
246
247 case NCP_IOC_GET_FS_INFO:
248 return ncp_get_fs_info(server, inode, argp);
249
250 case NCP_IOC_GET_FS_INFO_V2:
251 return ncp_get_fs_info_v2(server, inode, argp);
252
253 case NCP_IOC_GETMOUNTUID2:
254 {
255 unsigned long tmp = server->m.mounted_uid;
256
257 if ( (permission(inode, MAY_READ, NULL) != 0)
258 && (current->uid != server->m.mounted_uid))
259 {
260 return -EACCES;
261 }
262 if (put_user(tmp, (unsigned long __user *)argp))
263 return -EFAULT;
264 return 0;
265 }
266
267 case NCP_IOC_GETROOT:
268 {
269 struct ncp_setroot_ioctl sr;
270
271 if ( (permission(inode, MAY_READ, NULL) != 0)
272 && (current->uid != server->m.mounted_uid))
273 {
274 return -EACCES;
275 }
276 if (server->m.mounted_vol[0]) {
277 struct dentry* dentry = inode->i_sb->s_root;
278
279 if (dentry) {
280 struct inode* inode = dentry->d_inode;
281
282 if (inode) {
283 sr.volNumber = NCP_FINFO(inode)->volNumber;
284 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
285 sr.namespace = server->name_space[sr.volNumber];
286 } else
287 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
288 } else
289 DPRINTK("ncpfs: s_root==NULL\n");
290 } else {
291 sr.volNumber = -1;
292 sr.namespace = 0;
293 sr.dirEntNum = 0;
294 }
295 if (copy_to_user(argp, &sr, sizeof(sr)))
296 return -EFAULT;
297 return 0;
298 }
299 case NCP_IOC_SETROOT:
300 {
301 struct ncp_setroot_ioctl sr;
302 __u32 vnum;
303 __le32 de;
304 __le32 dosde;
305 struct dentry* dentry;
306
307 if (!capable(CAP_SYS_ADMIN))
308 {
309 return -EACCES;
310 }
311 if (server->root_setuped) return -EBUSY;
312 if (copy_from_user(&sr, argp, sizeof(sr)))
313 return -EFAULT;
314 if (sr.volNumber < 0) {
315 server->m.mounted_vol[0] = 0;
316 vnum = NCP_NUMBER_OF_VOLUMES;
317 de = 0;
318 dosde = 0;
319 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
320 return -EINVAL;
321 } else if (ncp_mount_subdir(server, sr.volNumber,
322 sr.namespace, sr.dirEntNum,
323 &vnum, &de, &dosde)) {
324 return -ENOENT;
325 }
326
327 dentry = inode->i_sb->s_root;
328 server->root_setuped = 1;
329 if (dentry) {
330 struct inode* inode = dentry->d_inode;
331
332 if (inode) {
333 NCP_FINFO(inode)->volNumber = vnum;
334 NCP_FINFO(inode)->dirEntNum = de;
335 NCP_FINFO(inode)->DosDirNum = dosde;
336 } else
337 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
338 } else
339 DPRINTK("ncpfs: s_root==NULL\n");
340
341 return 0;
342 }
343
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))
348 {
349 return -EACCES;
350 }
351 if (argp) {
352 if (server->sign_wanted)
353 {
354 struct ncp_sign_init sign;
355
356 if (copy_from_user(&sign, argp, sizeof(sign)))
357 return -EFAULT;
358 memcpy(server->sign_root,sign.sign_root,8);
359 memcpy(server->sign_last,sign.sign_last,16);
360 server->sign_active = 1;
361 }
362 /* ignore when signatures not wanted */
363 } else {
364 server->sign_active = 0;
365 }
366 return 0;
367
368 case NCP_IOC_SIGN_WANTED:
369 if ( (permission(inode, MAY_READ, NULL) != 0)
370 && (current->uid != server->m.mounted_uid))
371 {
372 return -EACCES;
373 }
374
375 if (put_user(server->sign_wanted, (int __user *)argp))
376 return -EFAULT;
377 return 0;
378 case NCP_IOC_SET_SIGN_WANTED:
379 {
380 int newstate;
381
382 if ( (permission(inode, MAY_WRITE, NULL) != 0)
383 && (current->uid != server->m.mounted_uid))
384 {
385 return -EACCES;
386 }
387 /* get only low 8 bits... */
388 if (get_user(newstate, (unsigned char __user *)argp))
389 return -EFAULT;
390 if (server->sign_active) {
391 /* cannot turn signatures OFF when active */
392 if (!newstate) return -EINVAL;
393 } else {
394 server->sign_wanted = newstate != 0;
395 }
396 return 0;
397 }
398
399#endif /* CONFIG_NCPFS_PACKET_SIGNING */
400
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))
405 {
406 return -EACCES;
407 }
408 {
409 struct ncp_lock_ioctl rqdata;
410 int result;
411
412 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
413 return -EFAULT;
414 if (rqdata.origin != 0)
415 return -EINVAL;
416 /* check for cmd */
417 switch (rqdata.cmd) {
418 case NCP_LOCK_EX:
419 case NCP_LOCK_SH:
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;
424 break;
425 case NCP_LOCK_LOG:
426 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
427 case NCP_LOCK_CLEAR:
428 break;
429 default:
430 return -EINVAL;
431 }
432 /* locking needs both read and write access */
433 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
434 {
435 return result;
436 }
437 result = -EIO;
438 if (!ncp_conn_valid(server))
439 goto outrel;
440 result = -EISDIR;
441 if (!S_ISREG(inode->i_mode))
442 goto outrel;
443 if (rqdata.cmd == NCP_LOCK_CLEAR)
444 {
445 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
446 NCP_FINFO(inode)->file_handle,
447 rqdata.offset,
448 rqdata.length);
449 if (result > 0) result = 0; /* no such lock */
450 }
451 else
452 {
453 int lockcmd;
454
455 switch (rqdata.cmd)
456 {
457 case NCP_LOCK_EX: lockcmd=1; break;
458 case NCP_LOCK_SH: lockcmd=3; break;
459 default: lockcmd=0; break;
460 }
461 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
462 NCP_FINFO(inode)->file_handle,
463 lockcmd,
464 rqdata.offset,
465 rqdata.length,
466 rqdata.timeout);
467 if (result > 0) result = -EAGAIN;
468 }
469outrel:
470 ncp_inode_close(inode);
471 return result;
472 }
473#endif /* CONFIG_NCPFS_IOCTL_LOCKING */
474
475 case NCP_IOC_GETOBJECTNAME:
476 if (current->uid != server->m.mounted_uid) {
477 return -EACCES;
478 }
479 {
480 struct ncp_objectname_ioctl user;
481 size_t outl;
482
483 if (copy_from_user(&user, argp, sizeof(user)))
484 return -EFAULT;
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;
490 if (outl) {
491 if (copy_to_user(user.object_name,
492 server->auth.object_name,
493 outl)) return -EFAULT;
494 }
495 if (copy_to_user(argp, &user, sizeof(user)))
496 return -EFAULT;
497 return 0;
498 }
499 case NCP_IOC_SETOBJECTNAME:
500 if (current->uid != server->m.mounted_uid) {
501 return -EACCES;
502 }
503 {
504 struct ncp_objectname_ioctl user;
505 void* newname;
506 void* oldname;
507 size_t oldnamelen;
508 void* oldprivate;
509 size_t oldprivatelen;
510
511 if (copy_from_user(&user, argp, sizeof(user)))
512 return -EFAULT;
513 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
514 return -ENOMEM;
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);
520 return -EFAULT;
521 }
522 } else {
523 newname = NULL;
524 }
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);
540 return 0;
541 }
542 case NCP_IOC_GETPRIVATEDATA:
543 if (current->uid != server->m.mounted_uid) {
544 return -EACCES;
545 }
546 {
547 struct ncp_privatedata_ioctl user;
548 size_t outl;
549
550 if (copy_from_user(&user, argp, sizeof(user)))
551 return -EFAULT;
552 outl = user.len;
553 user.len = server->priv.len;
554 if (outl > user.len) outl = user.len;
555 if (outl) {
556 if (copy_to_user(user.data,
557 server->priv.data,
558 outl)) return -EFAULT;
559 }
560 if (copy_to_user(argp, &user, sizeof(user)))
561 return -EFAULT;
562 return 0;
563 }
564 case NCP_IOC_SETPRIVATEDATA:
565 if (current->uid != server->m.mounted_uid) {
566 return -EACCES;
567 }
568 {
569 struct ncp_privatedata_ioctl user;
570 void* new;
571 void* old;
572 size_t oldlen;
573
574 if (copy_from_user(&user, argp, sizeof(user)))
575 return -EFAULT;
576 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
577 return -ENOMEM;
578 if (user.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);
583 return -EFAULT;
584 }
585 } else {
586 new = NULL;
587 }
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);
595 return 0;
596 }
597
598#ifdef CONFIG_NCPFS_NLS
599 case NCP_IOC_SETCHARSETS:
600 return ncp_set_charsets(server, argp);
601
602 case NCP_IOC_GETCHARSETS:
603 return ncp_get_charsets(server, argp);
604
605#endif /* CONFIG_NCPFS_NLS */
606
607 case NCP_IOC_SETDENTRYTTL:
608 if ((permission(inode, MAY_WRITE, NULL) != 0) &&
609 (current->uid != server->m.mounted_uid))
610 return -EACCES;
611 {
612 u_int32_t user;
613
614 if (copy_from_user(&user, argp, sizeof(user)))
615 return -EFAULT;
616 /* 20 secs at most... */
617 if (user > 20000)
618 return -EINVAL;
619 user = (user * HZ) / 1000;
620 server->dentry_ttl = user;
621 return 0;
622 }
623
624 case NCP_IOC_GETDENTRYTTL:
625 {
626 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
627 if (copy_to_user(argp, &user, sizeof(user)))
628 return -EFAULT;
629 return 0;
630 }
631
632 }
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)) {
640 return -EACCES;
641 }
642 SET_UID(uid, server->m.mounted_uid);
643 if (put_user(uid, (__kernel_uid_t __user *)argp))
644 return -EFAULT;
645 return 0;
646 }
647/* #endif */
648 return -EINVAL;
649}