[PATCH] capable/capability.h (fs/)
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ncpfs / ioctl.c
1 /*
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/capability.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/ioctl.h>
17 #include <linux/time.h>
18 #include <linux/mm.h>
19 #include <linux/highuid.h>
20 #include <linux/vmalloc.h>
21
22 #include <linux/ncp_fs.h>
23
24 #include "ncplib_kernel.h"
25
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
32
33 static int
34 ncp_get_fs_info(struct ncp_server * server, struct file *file,
35 struct ncp_fs_info __user *arg)
36 {
37 struct inode *inode = file->f_dentry->d_inode;
38 struct ncp_fs_info info;
39
40 if ((file_permission(file, MAY_WRITE) != 0)
41 && (current->uid != server->m.mounted_uid)) {
42 return -EACCES;
43 }
44 if (copy_from_user(&info, arg, sizeof(info)))
45 return -EFAULT;
46
47 if (info.version != NCP_GET_FS_INFO_VERSION) {
48 DPRINTK("info.version invalid: %d\n", info.version);
49 return -EINVAL;
50 }
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;
57
58 if (copy_to_user(arg, &info, sizeof(info)))
59 return -EFAULT;
60 return 0;
61 }
62
63 static int
64 ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
65 struct ncp_fs_info_v2 __user * arg)
66 {
67 struct inode *inode = file->f_dentry->d_inode;
68 struct ncp_fs_info_v2 info2;
69
70 if ((file_permission(file, MAY_WRITE) != 0)
71 && (current->uid != server->m.mounted_uid)) {
72 return -EACCES;
73 }
74 if (copy_from_user(&info2, arg, sizeof(info2)))
75 return -EFAULT;
76
77 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
78 DPRINTK("info.version invalid: %d\n", info2.version);
79 return -EINVAL;
80 }
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;
87
88 if (copy_to_user(arg, &info2, sizeof(info2)))
89 return -EFAULT;
90 return 0;
91 }
92
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.
96 */
97 static int
98 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
99 {
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;
105
106 if (!capable(CAP_SYS_ADMIN))
107 return -EACCES;
108 if (server->root_setuped)
109 return -EBUSY;
110
111 if (copy_from_user(&user, arg, sizeof(user)))
112 return -EFAULT;
113
114 codepage = NULL;
115 user.codepage[NCP_IOCSNAME_LEN] = 0;
116 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
117 codepage = load_nls_default();
118 else {
119 codepage = load_nls(user.codepage);
120 if (!codepage) {
121 return -EBADRQC;
122 }
123 }
124
125 iocharset = NULL;
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);
133 } else {
134 iocharset = load_nls(user.iocharset);
135 if (!iocharset) {
136 unload_nls(codepage);
137 return -EBADRQC;
138 }
139 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
140 }
141
142 oldset_cp = server->nls_vol;
143 server->nls_vol = codepage;
144 oldset_io = server->nls_io;
145 server->nls_io = iocharset;
146
147 if (oldset_cp)
148 unload_nls(oldset_cp);
149 if (oldset_io)
150 unload_nls(oldset_io);
151
152 return 0;
153 }
154
155 static int
156 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
157 {
158 struct ncp_nls_ioctl user;
159 int len;
160
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;
168 }
169
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;
178 }
179
180 if (copy_to_user(arg, &user, sizeof(user)))
181 return -EFAULT;
182 return 0;
183 }
184 #endif /* CONFIG_NCPFS_NLS */
185
186 int ncp_ioctl(struct inode *inode, struct file *filp,
187 unsigned int cmd, unsigned long arg)
188 {
189 struct ncp_server *server = NCP_SERVER(inode);
190 int result;
191 struct ncp_ioctl_request request;
192 char* bouncebuffer;
193 void __user *argp = (void __user *)arg;
194
195 switch (cmd) {
196 case NCP_IOC_NCPREQUEST:
197
198 if ((file_permission(filp, MAY_WRITE) != 0)
199 && (current->uid != server->m.mounted_uid)) {
200 return -EACCES;
201 }
202 if (copy_from_user(&request, argp, sizeof(request)))
203 return -EFAULT;
204
205 if ((request.function > 255)
206 || (request.size >
207 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
208 return -EINVAL;
209 }
210 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
211 if (!bouncebuffer)
212 return -ENOMEM;
213 if (copy_from_user(bouncebuffer, request.data, request.size)) {
214 vfree(bouncebuffer);
215 return -EFAULT;
216 }
217 ncp_lock_server(server);
218
219 /* FIXME: We hack around in the server's structures
220 here to be able to use ncp_request */
221
222 server->has_subfunction = 0;
223 server->current_size = request.size;
224 memcpy(server->packet, bouncebuffer, request.size);
225
226 result = ncp_request2(server, request.function,
227 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
228 if (result < 0)
229 result = -EIO;
230 else
231 result = server->reply_size;
232 ncp_unlock_server(server);
233 DPRINTK("ncp_ioctl: copy %d bytes\n",
234 result);
235 if (result >= 0)
236 if (copy_to_user(request.data, bouncebuffer, result))
237 result = -EFAULT;
238 vfree(bouncebuffer);
239 return result;
240
241 case NCP_IOC_CONN_LOGGED_IN:
242
243 if (!capable(CAP_SYS_ADMIN))
244 return -EACCES;
245 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
246 return -EINVAL;
247 if (server->root_setuped)
248 return -EBUSY;
249 server->root_setuped = 1;
250 return ncp_conn_logged_in(inode->i_sb);
251
252 case NCP_IOC_GET_FS_INFO:
253 return ncp_get_fs_info(server, filp, argp);
254
255 case NCP_IOC_GET_FS_INFO_V2:
256 return ncp_get_fs_info_v2(server, filp, argp);
257
258 case NCP_IOC_GETMOUNTUID2:
259 {
260 unsigned long tmp = server->m.mounted_uid;
261
262 if ((file_permission(filp, MAY_READ) != 0)
263 && (current->uid != server->m.mounted_uid))
264 {
265 return -EACCES;
266 }
267 if (put_user(tmp, (unsigned long __user *)argp))
268 return -EFAULT;
269 return 0;
270 }
271
272 case NCP_IOC_GETROOT:
273 {
274 struct ncp_setroot_ioctl sr;
275
276 if ((file_permission(filp, MAY_READ) != 0)
277 && (current->uid != server->m.mounted_uid))
278 {
279 return -EACCES;
280 }
281 if (server->m.mounted_vol[0]) {
282 struct dentry* dentry = inode->i_sb->s_root;
283
284 if (dentry) {
285 struct inode* inode = dentry->d_inode;
286
287 if (inode) {
288 sr.volNumber = NCP_FINFO(inode)->volNumber;
289 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
290 sr.namespace = server->name_space[sr.volNumber];
291 } else
292 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
293 } else
294 DPRINTK("ncpfs: s_root==NULL\n");
295 } else {
296 sr.volNumber = -1;
297 sr.namespace = 0;
298 sr.dirEntNum = 0;
299 }
300 if (copy_to_user(argp, &sr, sizeof(sr)))
301 return -EFAULT;
302 return 0;
303 }
304 case NCP_IOC_SETROOT:
305 {
306 struct ncp_setroot_ioctl sr;
307 __u32 vnum;
308 __le32 de;
309 __le32 dosde;
310 struct dentry* dentry;
311
312 if (!capable(CAP_SYS_ADMIN))
313 {
314 return -EACCES;
315 }
316 if (server->root_setuped) return -EBUSY;
317 if (copy_from_user(&sr, argp, sizeof(sr)))
318 return -EFAULT;
319 if (sr.volNumber < 0) {
320 server->m.mounted_vol[0] = 0;
321 vnum = NCP_NUMBER_OF_VOLUMES;
322 de = 0;
323 dosde = 0;
324 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
325 return -EINVAL;
326 } else if (ncp_mount_subdir(server, sr.volNumber,
327 sr.namespace, sr.dirEntNum,
328 &vnum, &de, &dosde)) {
329 return -ENOENT;
330 }
331
332 dentry = inode->i_sb->s_root;
333 server->root_setuped = 1;
334 if (dentry) {
335 struct inode* inode = dentry->d_inode;
336
337 if (inode) {
338 NCP_FINFO(inode)->volNumber = vnum;
339 NCP_FINFO(inode)->dirEntNum = de;
340 NCP_FINFO(inode)->DosDirNum = dosde;
341 } else
342 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
343 } else
344 DPRINTK("ncpfs: s_root==NULL\n");
345
346 return 0;
347 }
348
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))
353 {
354 return -EACCES;
355 }
356 if (argp) {
357 if (server->sign_wanted)
358 {
359 struct ncp_sign_init sign;
360
361 if (copy_from_user(&sign, argp, sizeof(sign)))
362 return -EFAULT;
363 memcpy(server->sign_root,sign.sign_root,8);
364 memcpy(server->sign_last,sign.sign_last,16);
365 server->sign_active = 1;
366 }
367 /* ignore when signatures not wanted */
368 } else {
369 server->sign_active = 0;
370 }
371 return 0;
372
373 case NCP_IOC_SIGN_WANTED:
374 if ((file_permission(filp, MAY_READ) != 0)
375 && (current->uid != server->m.mounted_uid))
376 {
377 return -EACCES;
378 }
379
380 if (put_user(server->sign_wanted, (int __user *)argp))
381 return -EFAULT;
382 return 0;
383 case NCP_IOC_SET_SIGN_WANTED:
384 {
385 int newstate;
386
387 if ((file_permission(filp, MAY_WRITE) != 0)
388 && (current->uid != server->m.mounted_uid))
389 {
390 return -EACCES;
391 }
392 /* get only low 8 bits... */
393 if (get_user(newstate, (unsigned char __user *)argp))
394 return -EFAULT;
395 if (server->sign_active) {
396 /* cannot turn signatures OFF when active */
397 if (!newstate) return -EINVAL;
398 } else {
399 server->sign_wanted = newstate != 0;
400 }
401 return 0;
402 }
403
404 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
405
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))
410 {
411 return -EACCES;
412 }
413 {
414 struct ncp_lock_ioctl rqdata;
415 int result;
416
417 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
418 return -EFAULT;
419 if (rqdata.origin != 0)
420 return -EINVAL;
421 /* check for cmd */
422 switch (rqdata.cmd) {
423 case NCP_LOCK_EX:
424 case NCP_LOCK_SH:
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;
429 break;
430 case NCP_LOCK_LOG:
431 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
432 case NCP_LOCK_CLEAR:
433 break;
434 default:
435 return -EINVAL;
436 }
437 /* locking needs both read and write access */
438 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
439 {
440 return result;
441 }
442 result = -EIO;
443 if (!ncp_conn_valid(server))
444 goto outrel;
445 result = -EISDIR;
446 if (!S_ISREG(inode->i_mode))
447 goto outrel;
448 if (rqdata.cmd == NCP_LOCK_CLEAR)
449 {
450 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
451 NCP_FINFO(inode)->file_handle,
452 rqdata.offset,
453 rqdata.length);
454 if (result > 0) result = 0; /* no such lock */
455 }
456 else
457 {
458 int lockcmd;
459
460 switch (rqdata.cmd)
461 {
462 case NCP_LOCK_EX: lockcmd=1; break;
463 case NCP_LOCK_SH: lockcmd=3; break;
464 default: lockcmd=0; break;
465 }
466 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
467 NCP_FINFO(inode)->file_handle,
468 lockcmd,
469 rqdata.offset,
470 rqdata.length,
471 rqdata.timeout);
472 if (result > 0) result = -EAGAIN;
473 }
474 outrel:
475 ncp_inode_close(inode);
476 return result;
477 }
478 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
479
480 case NCP_IOC_GETOBJECTNAME:
481 if (current->uid != server->m.mounted_uid) {
482 return -EACCES;
483 }
484 {
485 struct ncp_objectname_ioctl user;
486 size_t outl;
487
488 if (copy_from_user(&user, argp, sizeof(user)))
489 return -EFAULT;
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;
495 if (outl) {
496 if (copy_to_user(user.object_name,
497 server->auth.object_name,
498 outl)) return -EFAULT;
499 }
500 if (copy_to_user(argp, &user, sizeof(user)))
501 return -EFAULT;
502 return 0;
503 }
504 case NCP_IOC_SETOBJECTNAME:
505 if (current->uid != server->m.mounted_uid) {
506 return -EACCES;
507 }
508 {
509 struct ncp_objectname_ioctl user;
510 void* newname;
511 void* oldname;
512 size_t oldnamelen;
513 void* oldprivate;
514 size_t oldprivatelen;
515
516 if (copy_from_user(&user, argp, sizeof(user)))
517 return -EFAULT;
518 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
519 return -ENOMEM;
520 if (user.object_name_len) {
521 newname = ncp_kmalloc(user.object_name_len, GFP_USER);
522 if (!newname) return -ENOMEM;
523 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
524 ncp_kfree_s(newname, user.object_name_len);
525 return -EFAULT;
526 }
527 } else {
528 newname = NULL;
529 }
530 /* enter critical section */
531 /* maybe that kfree can sleep so do that this way */
532 /* it is at least more SMP friendly (in future...) */
533 oldname = server->auth.object_name;
534 oldnamelen = server->auth.object_name_len;
535 oldprivate = server->priv.data;
536 oldprivatelen = server->priv.len;
537 server->auth.auth_type = user.auth_type;
538 server->auth.object_name_len = user.object_name_len;
539 server->auth.object_name = newname;
540 server->priv.len = 0;
541 server->priv.data = NULL;
542 /* leave critical section */
543 if (oldprivate) ncp_kfree_s(oldprivate, oldprivatelen);
544 if (oldname) ncp_kfree_s(oldname, oldnamelen);
545 return 0;
546 }
547 case NCP_IOC_GETPRIVATEDATA:
548 if (current->uid != server->m.mounted_uid) {
549 return -EACCES;
550 }
551 {
552 struct ncp_privatedata_ioctl user;
553 size_t outl;
554
555 if (copy_from_user(&user, argp, sizeof(user)))
556 return -EFAULT;
557 outl = user.len;
558 user.len = server->priv.len;
559 if (outl > user.len) outl = user.len;
560 if (outl) {
561 if (copy_to_user(user.data,
562 server->priv.data,
563 outl)) return -EFAULT;
564 }
565 if (copy_to_user(argp, &user, sizeof(user)))
566 return -EFAULT;
567 return 0;
568 }
569 case NCP_IOC_SETPRIVATEDATA:
570 if (current->uid != server->m.mounted_uid) {
571 return -EACCES;
572 }
573 {
574 struct ncp_privatedata_ioctl user;
575 void* new;
576 void* old;
577 size_t oldlen;
578
579 if (copy_from_user(&user, argp, sizeof(user)))
580 return -EFAULT;
581 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
582 return -ENOMEM;
583 if (user.len) {
584 new = ncp_kmalloc(user.len, GFP_USER);
585 if (!new) return -ENOMEM;
586 if (copy_from_user(new, user.data, user.len)) {
587 ncp_kfree_s(new, user.len);
588 return -EFAULT;
589 }
590 } else {
591 new = NULL;
592 }
593 /* enter critical section */
594 old = server->priv.data;
595 oldlen = server->priv.len;
596 server->priv.len = user.len;
597 server->priv.data = new;
598 /* leave critical section */
599 if (old) ncp_kfree_s(old, oldlen);
600 return 0;
601 }
602
603 #ifdef CONFIG_NCPFS_NLS
604 case NCP_IOC_SETCHARSETS:
605 return ncp_set_charsets(server, argp);
606
607 case NCP_IOC_GETCHARSETS:
608 return ncp_get_charsets(server, argp);
609
610 #endif /* CONFIG_NCPFS_NLS */
611
612 case NCP_IOC_SETDENTRYTTL:
613 if ((file_permission(filp, MAY_WRITE) != 0) &&
614 (current->uid != server->m.mounted_uid))
615 return -EACCES;
616 {
617 u_int32_t user;
618
619 if (copy_from_user(&user, argp, sizeof(user)))
620 return -EFAULT;
621 /* 20 secs at most... */
622 if (user > 20000)
623 return -EINVAL;
624 user = (user * HZ) / 1000;
625 server->dentry_ttl = user;
626 return 0;
627 }
628
629 case NCP_IOC_GETDENTRYTTL:
630 {
631 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
632 if (copy_to_user(argp, &user, sizeof(user)))
633 return -EFAULT;
634 return 0;
635 }
636
637 }
638 /* #ifdef CONFIG_UID16 */
639 /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
640 so we have this out of switch */
641 if (cmd == NCP_IOC_GETMOUNTUID) {
642 __kernel_uid_t uid = 0;
643 if ((file_permission(filp, MAY_READ) != 0)
644 && (current->uid != server->m.mounted_uid)) {
645 return -EACCES;
646 }
647 SET_UID(uid, server->m.mounted_uid);
648 if (put_user(uid, (__kernel_uid_t __user *)argp))
649 return -EFAULT;
650 return 0;
651 }
652 /* #endif */
653 return -EINVAL;
654 }