ncpfs: return proper error from NCP_IOC_SETROOT ioctl
[pandora-kernel.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/capability.h>
11 #include <linux/compat.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/ioctl.h>
15 #include <linux/time.h>
16 #include <linux/mm.h>
17 #include <linux/mount.h>
18 #include <linux/slab.h>
19 #include <linux/highuid.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sched.h>
22
23 #include <asm/uaccess.h>
24
25 #include "ncp_fs.h"
26
27 /* maximum limit for ncp_objectname_ioctl */
28 #define NCP_OBJECT_NAME_MAX_LEN 4096
29 /* maximum limit for ncp_privatedata_ioctl */
30 #define NCP_PRIVATE_DATA_MAX_LEN 8192
31 /* maximum negotiable packet size */
32 #define NCP_PACKET_SIZE_INTERNAL 65536
33
34 static int
35 ncp_get_fs_info(struct ncp_server * server, struct inode *inode,
36                 struct ncp_fs_info __user *arg)
37 {
38         struct ncp_fs_info info;
39
40         if (copy_from_user(&info, arg, sizeof(info)))
41                 return -EFAULT;
42
43         if (info.version != NCP_GET_FS_INFO_VERSION) {
44                 DPRINTK("info.version invalid: %d\n", info.version);
45                 return -EINVAL;
46         }
47         /* TODO: info.addr = server->m.serv_addr; */
48         SET_UID(info.mounted_uid, server->m.mounted_uid);
49         info.connection         = server->connection;
50         info.buffer_size        = server->buffer_size;
51         info.volume_number      = NCP_FINFO(inode)->volNumber;
52         info.directory_id       = NCP_FINFO(inode)->DosDirNum;
53
54         if (copy_to_user(arg, &info, sizeof(info)))
55                 return -EFAULT;
56         return 0;
57 }
58
59 static int
60 ncp_get_fs_info_v2(struct ncp_server * server, struct inode *inode,
61                    struct ncp_fs_info_v2 __user * arg)
62 {
63         struct ncp_fs_info_v2 info2;
64
65         if (copy_from_user(&info2, arg, sizeof(info2)))
66                 return -EFAULT;
67
68         if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
69                 DPRINTK("info.version invalid: %d\n", info2.version);
70                 return -EINVAL;
71         }
72         info2.mounted_uid   = server->m.mounted_uid;
73         info2.connection    = server->connection;
74         info2.buffer_size   = server->buffer_size;
75         info2.volume_number = NCP_FINFO(inode)->volNumber;
76         info2.directory_id  = NCP_FINFO(inode)->DosDirNum;
77         info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
78
79         if (copy_to_user(arg, &info2, sizeof(info2)))
80                 return -EFAULT;
81         return 0;
82 }
83
84 #ifdef CONFIG_COMPAT
85 struct compat_ncp_objectname_ioctl
86 {
87         s32             auth_type;
88         u32             object_name_len;
89         compat_caddr_t  object_name;    /* a userspace data, in most cases user name */
90 };
91
92 struct compat_ncp_fs_info_v2 {
93         s32 version;
94         u32 mounted_uid;
95         u32 connection;
96         u32 buffer_size;
97
98         u32 volume_number;
99         u32 directory_id;
100
101         u32 dummy1;
102         u32 dummy2;
103         u32 dummy3;
104 };
105
106 struct compat_ncp_ioctl_request {
107         u32 function;
108         u32 size;
109         compat_caddr_t data;
110 };
111
112 struct compat_ncp_privatedata_ioctl
113 {
114         u32             len;
115         compat_caddr_t  data;           /* ~1000 for NDS */
116 };
117
118 #define NCP_IOC_GET_FS_INFO_V2_32       _IOWR('n', 4, struct compat_ncp_fs_info_v2)
119 #define NCP_IOC_NCPREQUEST_32           _IOR('n', 1, struct compat_ncp_ioctl_request)
120 #define NCP_IOC_GETOBJECTNAME_32        _IOWR('n', 9, struct compat_ncp_objectname_ioctl)
121 #define NCP_IOC_SETOBJECTNAME_32        _IOR('n', 9, struct compat_ncp_objectname_ioctl)
122 #define NCP_IOC_GETPRIVATEDATA_32       _IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
123 #define NCP_IOC_SETPRIVATEDATA_32       _IOR('n', 10, struct compat_ncp_privatedata_ioctl)
124
125 static int
126 ncp_get_compat_fs_info_v2(struct ncp_server * server, struct inode *inode,
127                    struct compat_ncp_fs_info_v2 __user * arg)
128 {
129         struct compat_ncp_fs_info_v2 info2;
130
131         if (copy_from_user(&info2, arg, sizeof(info2)))
132                 return -EFAULT;
133
134         if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
135                 DPRINTK("info.version invalid: %d\n", info2.version);
136                 return -EINVAL;
137         }
138         info2.mounted_uid   = server->m.mounted_uid;
139         info2.connection    = server->connection;
140         info2.buffer_size   = server->buffer_size;
141         info2.volume_number = NCP_FINFO(inode)->volNumber;
142         info2.directory_id  = NCP_FINFO(inode)->DosDirNum;
143         info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
144
145         if (copy_to_user(arg, &info2, sizeof(info2)))
146                 return -EFAULT;
147         return 0;
148 }
149 #endif
150
151 #define NCP_IOC_GETMOUNTUID16           _IOW('n', 2, u16)
152 #define NCP_IOC_GETMOUNTUID32           _IOW('n', 2, u32)
153 #define NCP_IOC_GETMOUNTUID64           _IOW('n', 2, u64)
154
155 #ifdef CONFIG_NCPFS_NLS
156 /* Here we are select the iocharset and the codepage for NLS.
157  * Thanks Petr Vandrovec for idea and many hints.
158  */
159 static int
160 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
161 {
162         struct ncp_nls_ioctl user;
163         struct nls_table *codepage;
164         struct nls_table *iocharset;
165         struct nls_table *oldset_io;
166         struct nls_table *oldset_cp;
167         int utf8;
168         int err;
169
170         if (copy_from_user(&user, arg, sizeof(user)))
171                 return -EFAULT;
172
173         codepage = NULL;
174         user.codepage[NCP_IOCSNAME_LEN] = 0;
175         if (!user.codepage[0] || !strcmp(user.codepage, "default"))
176                 codepage = load_nls_default();
177         else {
178                 codepage = load_nls(user.codepage);
179                 if (!codepage) {
180                         return -EBADRQC;
181                 }
182         }
183
184         iocharset = NULL;
185         user.iocharset[NCP_IOCSNAME_LEN] = 0;
186         if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
187                 iocharset = load_nls_default();
188                 utf8 = 0;
189         } else if (!strcmp(user.iocharset, "utf8")) {
190                 iocharset = load_nls_default();
191                 utf8 = 1;
192         } else {
193                 iocharset = load_nls(user.iocharset);
194                 if (!iocharset) {
195                         unload_nls(codepage);
196                         return -EBADRQC;
197                 }
198                 utf8 = 0;
199         }
200
201         mutex_lock(&server->root_setup_lock);
202         if (server->root_setuped) {
203                 oldset_cp = codepage;
204                 oldset_io = iocharset;
205                 err = -EBUSY;
206         } else {
207                 if (utf8)
208                         NCP_SET_FLAG(server, NCP_FLAG_UTF8);
209                 else
210                         NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
211                 oldset_cp = server->nls_vol;
212                 server->nls_vol = codepage;
213                 oldset_io = server->nls_io;
214                 server->nls_io = iocharset;
215                 err = 0;
216         }
217         mutex_unlock(&server->root_setup_lock);
218         unload_nls(oldset_cp);
219         unload_nls(oldset_io);
220
221         return err;
222 }
223
224 static int
225 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
226 {
227         struct ncp_nls_ioctl user;
228         int len;
229
230         memset(&user, 0, sizeof(user));
231         mutex_lock(&server->root_setup_lock);
232         if (server->nls_vol && server->nls_vol->charset) {
233                 len = strlen(server->nls_vol->charset);
234                 if (len > NCP_IOCSNAME_LEN)
235                         len = NCP_IOCSNAME_LEN;
236                 strncpy(user.codepage, server->nls_vol->charset, len);
237                 user.codepage[len] = 0;
238         }
239
240         if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
241                 strcpy(user.iocharset, "utf8");
242         else if (server->nls_io && server->nls_io->charset) {
243                 len = strlen(server->nls_io->charset);
244                 if (len > NCP_IOCSNAME_LEN)
245                         len = NCP_IOCSNAME_LEN;
246                 strncpy(user.iocharset, server->nls_io->charset, len);
247                 user.iocharset[len] = 0;
248         }
249         mutex_unlock(&server->root_setup_lock);
250
251         if (copy_to_user(arg, &user, sizeof(user)))
252                 return -EFAULT;
253         return 0;
254 }
255 #endif /* CONFIG_NCPFS_NLS */
256
257 static long __ncp_ioctl(struct inode *inode, unsigned int cmd, unsigned long arg)
258 {
259         struct ncp_server *server = NCP_SERVER(inode);
260         int result;
261         struct ncp_ioctl_request request;
262         char* bouncebuffer;
263         void __user *argp = (void __user *)arg;
264
265         switch (cmd) {
266 #ifdef CONFIG_COMPAT
267         case NCP_IOC_NCPREQUEST_32:
268 #endif
269         case NCP_IOC_NCPREQUEST:
270 #ifdef CONFIG_COMPAT
271                 if (cmd == NCP_IOC_NCPREQUEST_32) {
272                         struct compat_ncp_ioctl_request request32;
273                         if (copy_from_user(&request32, argp, sizeof(request32)))
274                                 return -EFAULT;
275                         request.function = request32.function;
276                         request.size = request32.size;
277                         request.data = compat_ptr(request32.data);
278                 } else
279 #endif
280                 if (copy_from_user(&request, argp, sizeof(request)))
281                         return -EFAULT;
282
283                 if ((request.function > 255)
284                     || (request.size >
285                   NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
286                         return -EINVAL;
287                 }
288                 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
289                 if (!bouncebuffer)
290                         return -ENOMEM;
291                 if (copy_from_user(bouncebuffer, request.data, request.size)) {
292                         vfree(bouncebuffer);
293                         return -EFAULT;
294                 }
295                 ncp_lock_server(server);
296
297                 /* FIXME: We hack around in the server's structures
298                    here to be able to use ncp_request */
299
300                 server->has_subfunction = 0;
301                 server->current_size = request.size;
302                 memcpy(server->packet, bouncebuffer, request.size);
303
304                 result = ncp_request2(server, request.function,
305                         bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
306                 if (result < 0)
307                         result = -EIO;
308                 else
309                         result = server->reply_size;
310                 ncp_unlock_server(server);
311                 DPRINTK("ncp_ioctl: copy %d bytes\n",
312                         result);
313                 if (result >= 0)
314                         if (copy_to_user(request.data, bouncebuffer, result))
315                                 result = -EFAULT;
316                 vfree(bouncebuffer);
317                 return result;
318
319         case NCP_IOC_CONN_LOGGED_IN:
320
321                 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
322                         return -EINVAL;
323                 mutex_lock(&server->root_setup_lock);
324                 if (server->root_setuped)
325                         result = -EBUSY;
326                 else {
327                         result = ncp_conn_logged_in(inode->i_sb);
328                         if (result == 0)
329                                 server->root_setuped = 1;
330                 }
331                 mutex_unlock(&server->root_setup_lock);
332                 return result;
333
334         case NCP_IOC_GET_FS_INFO:
335                 return ncp_get_fs_info(server, inode, argp);
336
337         case NCP_IOC_GET_FS_INFO_V2:
338                 return ncp_get_fs_info_v2(server, inode, argp);
339
340 #ifdef CONFIG_COMPAT
341         case NCP_IOC_GET_FS_INFO_V2_32:
342                 return ncp_get_compat_fs_info_v2(server, inode, argp);
343 #endif
344         /* we have too many combinations of CONFIG_COMPAT,
345          * CONFIG_64BIT and CONFIG_UID16, so just handle
346          * any of the possible ioctls */
347         case NCP_IOC_GETMOUNTUID16:
348                 {
349                         u16 uid;
350
351                         SET_UID(uid, server->m.mounted_uid);
352                         if (put_user(uid, (u16 __user *)argp))
353                                 return -EFAULT;
354                         return 0;
355                 }
356         case NCP_IOC_GETMOUNTUID32:
357                 if (put_user(server->m.mounted_uid,
358                              (u32 __user *)argp))
359                         return -EFAULT;
360                 return 0;
361         case NCP_IOC_GETMOUNTUID64:
362                 if (put_user(server->m.mounted_uid,
363                              (u64 __user *)argp))
364                         return -EFAULT;
365                 return 0;
366
367         case NCP_IOC_GETROOT:
368                 {
369                         struct ncp_setroot_ioctl sr;
370
371                         result = -EACCES;
372                         mutex_lock(&server->root_setup_lock);
373                         if (server->m.mounted_vol[0]) {
374                                 struct dentry* dentry = inode->i_sb->s_root;
375
376                                 if (dentry) {
377                                         struct inode* s_inode = dentry->d_inode;
378
379                                         if (s_inode) {
380                                                 sr.volNumber = NCP_FINFO(s_inode)->volNumber;
381                                                 sr.dirEntNum = NCP_FINFO(s_inode)->dirEntNum;
382                                                 sr.namespace = server->name_space[sr.volNumber];
383                                                 result = 0;
384                                         } else
385                                                 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
386                                 } else
387                                         DPRINTK("ncpfs: s_root==NULL\n");
388                         } else {
389                                 sr.volNumber = -1;
390                                 sr.namespace = 0;
391                                 sr.dirEntNum = 0;
392                                 result = 0;
393                         }
394                         mutex_unlock(&server->root_setup_lock);
395                         if (!result && copy_to_user(argp, &sr, sizeof(sr)))
396                                 result = -EFAULT;
397                         return result;
398                 }
399
400         case NCP_IOC_SETROOT:
401                 {
402                         struct ncp_setroot_ioctl sr;
403                         __u32 vnum;
404                         __le32 de;
405                         __le32 dosde;
406                         struct dentry* dentry;
407
408                         if (copy_from_user(&sr, argp, sizeof(sr)))
409                                 return -EFAULT;
410                         mutex_lock(&server->root_setup_lock);
411                         if (server->root_setuped)
412                                 result = -EBUSY;
413                         else {
414                                 if (sr.volNumber < 0) {
415                                         server->m.mounted_vol[0] = 0;
416                                         vnum = NCP_NUMBER_OF_VOLUMES;
417                                         de = 0;
418                                         dosde = 0;
419                                         result = 0;
420                                 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
421                                         result = -EINVAL;
422                                 } else if (ncp_mount_subdir(server, sr.volNumber,
423                                                         sr.namespace, sr.dirEntNum,
424                                                         &vnum, &de, &dosde)) {
425                                         result = -ENOENT;
426                                 } else
427                                         result = 0;
428
429                                 if (result == 0) {
430                                         dentry = inode->i_sb->s_root;
431                                         if (dentry) {
432                                                 struct inode* s_inode = dentry->d_inode;
433
434                                                 if (s_inode) {
435                                                         NCP_FINFO(s_inode)->volNumber = vnum;
436                                                         NCP_FINFO(s_inode)->dirEntNum = de;
437                                                         NCP_FINFO(s_inode)->DosDirNum = dosde;
438                                                         server->root_setuped = 1;
439                                                 } else {
440                                                         DPRINTK("ncpfs: s_root->d_inode==NULL\n");
441                                                         result = -EIO;
442                                                 }
443                                         } else {
444                                                 DPRINTK("ncpfs: s_root==NULL\n");
445                                                 result = -EIO;
446                                         }
447                                 }
448                         }
449                         mutex_unlock(&server->root_setup_lock);
450
451                         return result;
452                 }
453
454 #ifdef CONFIG_NCPFS_PACKET_SIGNING
455         case NCP_IOC_SIGN_INIT:
456                 {
457                         struct ncp_sign_init sign;
458
459                         if (argp)
460                                 if (copy_from_user(&sign, argp, sizeof(sign)))
461                                         return -EFAULT;
462                         ncp_lock_server(server);
463                         mutex_lock(&server->rcv.creq_mutex);
464                         if (argp) {
465                                 if (server->sign_wanted) {
466                                         memcpy(server->sign_root,sign.sign_root,8);
467                                         memcpy(server->sign_last,sign.sign_last,16);
468                                         server->sign_active = 1;
469                                 }
470                                 /* ignore when signatures not wanted */
471                         } else {
472                                 server->sign_active = 0;
473                         }
474                         mutex_unlock(&server->rcv.creq_mutex);
475                         ncp_unlock_server(server);
476                         return 0;
477                 }
478
479         case NCP_IOC_SIGN_WANTED:
480                 {
481                         int state;
482
483                         ncp_lock_server(server);
484                         state = server->sign_wanted;
485                         ncp_unlock_server(server);
486                         if (put_user(state, (int __user *)argp))
487                                 return -EFAULT;
488                         return 0;
489                 }
490
491         case NCP_IOC_SET_SIGN_WANTED:
492                 {
493                         int newstate;
494
495                         /* get only low 8 bits... */
496                         if (get_user(newstate, (unsigned char __user *)argp))
497                                 return -EFAULT;
498                         result = 0;
499                         ncp_lock_server(server);
500                         if (server->sign_active) {
501                                 /* cannot turn signatures OFF when active */
502                                 if (!newstate)
503                                         result = -EINVAL;
504                         } else {
505                                 server->sign_wanted = newstate != 0;
506                         }
507                         ncp_unlock_server(server);
508                         return result;
509                 }
510
511 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
512
513 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
514         case NCP_IOC_LOCKUNLOCK:
515                 {
516                         struct ncp_lock_ioctl    rqdata;
517
518                         if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
519                                 return -EFAULT;
520                         if (rqdata.origin != 0)
521                                 return -EINVAL;
522                         /* check for cmd */
523                         switch (rqdata.cmd) {
524                                 case NCP_LOCK_EX:
525                                 case NCP_LOCK_SH:
526                                                 if (rqdata.timeout == 0)
527                                                         rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
528                                                 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
529                                                         rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
530                                                 break;
531                                 case NCP_LOCK_LOG:
532                                                 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;      /* has no effect */
533                                 case NCP_LOCK_CLEAR:
534                                                 break;
535                                 default:
536                                                 return -EINVAL;
537                         }
538                         /* locking needs both read and write access */
539                         if ((result = ncp_make_open(inode, O_RDWR)) != 0)
540                         {
541                                 return result;
542                         }
543                         result = -EISDIR;
544                         if (!S_ISREG(inode->i_mode))
545                                 goto outrel;
546                         if (rqdata.cmd == NCP_LOCK_CLEAR)
547                         {
548                                 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
549                                                         NCP_FINFO(inode)->file_handle,
550                                                         rqdata.offset,
551                                                         rqdata.length);
552                                 if (result > 0) result = 0;     /* no such lock */
553                         }
554                         else
555                         {
556                                 int lockcmd;
557
558                                 switch (rqdata.cmd)
559                                 {
560                                         case NCP_LOCK_EX:  lockcmd=1; break;
561                                         case NCP_LOCK_SH:  lockcmd=3; break;
562                                         default:           lockcmd=0; break;
563                                 }
564                                 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
565                                                         NCP_FINFO(inode)->file_handle,
566                                                         lockcmd,
567                                                         rqdata.offset,
568                                                         rqdata.length,
569                                                         rqdata.timeout);
570                                 if (result > 0) result = -EAGAIN;
571                         }
572 outrel:
573                         ncp_inode_close(inode);
574                         return result;
575                 }
576 #endif  /* CONFIG_NCPFS_IOCTL_LOCKING */
577
578 #ifdef CONFIG_COMPAT
579         case NCP_IOC_GETOBJECTNAME_32:
580                 {
581                         struct compat_ncp_objectname_ioctl user;
582                         size_t outl;
583
584                         if (copy_from_user(&user, argp, sizeof(user)))
585                                 return -EFAULT;
586                         down_read(&server->auth_rwsem);
587                         user.auth_type = server->auth.auth_type;
588                         outl = user.object_name_len;
589                         user.object_name_len = server->auth.object_name_len;
590                         if (outl > user.object_name_len)
591                                 outl = user.object_name_len;
592                         result = 0;
593                         if (outl) {
594                                 if (copy_to_user(compat_ptr(user.object_name),
595                                                  server->auth.object_name,
596                                                  outl))
597                                         result = -EFAULT;
598                         }
599                         up_read(&server->auth_rwsem);
600                         if (!result && copy_to_user(argp, &user, sizeof(user)))
601                                 result = -EFAULT;
602                         return result;
603                 }
604 #endif
605
606         case NCP_IOC_GETOBJECTNAME:
607                 {
608                         struct ncp_objectname_ioctl user;
609                         size_t outl;
610
611                         if (copy_from_user(&user, argp, sizeof(user)))
612                                 return -EFAULT;
613                         down_read(&server->auth_rwsem);
614                         user.auth_type = server->auth.auth_type;
615                         outl = user.object_name_len;
616                         user.object_name_len = server->auth.object_name_len;
617                         if (outl > user.object_name_len)
618                                 outl = user.object_name_len;
619                         result = 0;
620                         if (outl) {
621                                 if (copy_to_user(user.object_name,
622                                                  server->auth.object_name,
623                                                  outl))
624                                         result = -EFAULT;
625                         }
626                         up_read(&server->auth_rwsem);
627                         if (!result && copy_to_user(argp, &user, sizeof(user)))
628                                 result = -EFAULT;
629                         return result;
630                 }
631
632 #ifdef CONFIG_COMPAT
633         case NCP_IOC_SETOBJECTNAME_32:
634 #endif
635         case NCP_IOC_SETOBJECTNAME:
636                 {
637                         struct ncp_objectname_ioctl user;
638                         void* newname;
639                         void* oldname;
640                         size_t oldnamelen;
641                         void* oldprivate;
642                         size_t oldprivatelen;
643
644 #ifdef CONFIG_COMPAT
645                         if (cmd == NCP_IOC_SETOBJECTNAME_32) {
646                                 struct compat_ncp_objectname_ioctl user32;
647                                 if (copy_from_user(&user32, argp, sizeof(user32)))
648                                         return -EFAULT;
649                                 user.auth_type = user32.auth_type;
650                                 user.object_name_len = user32.object_name_len;
651                                 user.object_name = compat_ptr(user32.object_name);
652                         } else
653 #endif
654                         if (copy_from_user(&user, argp, sizeof(user)))
655                                 return -EFAULT;
656
657                         if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
658                                 return -ENOMEM;
659                         if (user.object_name_len) {
660                                 newname = memdup_user(user.object_name,
661                                                       user.object_name_len);
662                                 if (IS_ERR(newname))
663                                         return PTR_ERR(newname);
664                         } else {
665                                 newname = NULL;
666                         }
667                         down_write(&server->auth_rwsem);
668                         oldname = server->auth.object_name;
669                         oldnamelen = server->auth.object_name_len;
670                         oldprivate = server->priv.data;
671                         oldprivatelen = server->priv.len;
672                         server->auth.auth_type = user.auth_type;
673                         server->auth.object_name_len = user.object_name_len;
674                         server->auth.object_name = newname;
675                         server->priv.len = 0;
676                         server->priv.data = NULL;
677                         up_write(&server->auth_rwsem);
678                         kfree(oldprivate);
679                         kfree(oldname);
680                         return 0;
681                 }
682
683 #ifdef CONFIG_COMPAT
684         case NCP_IOC_GETPRIVATEDATA_32:
685 #endif
686         case NCP_IOC_GETPRIVATEDATA:
687                 {
688                         struct ncp_privatedata_ioctl user;
689                         size_t outl;
690
691 #ifdef CONFIG_COMPAT
692                         if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
693                                 struct compat_ncp_privatedata_ioctl user32;
694                                 if (copy_from_user(&user32, argp, sizeof(user32)))
695                                         return -EFAULT;
696                                 user.len = user32.len;
697                                 user.data = compat_ptr(user32.data);
698                         } else
699 #endif
700                         if (copy_from_user(&user, argp, sizeof(user)))
701                                 return -EFAULT;
702
703                         down_read(&server->auth_rwsem);
704                         outl = user.len;
705                         user.len = server->priv.len;
706                         if (outl > user.len) outl = user.len;
707                         result = 0;
708                         if (outl) {
709                                 if (copy_to_user(user.data,
710                                                  server->priv.data,
711                                                  outl))
712                                         result = -EFAULT;
713                         }
714                         up_read(&server->auth_rwsem);
715                         if (result)
716                                 return result;
717 #ifdef CONFIG_COMPAT
718                         if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
719                                 struct compat_ncp_privatedata_ioctl user32;
720                                 user32.len = user.len;
721                                 user32.data = (unsigned long) user.data;
722                                 if (copy_to_user(argp, &user32, sizeof(user32)))
723                                         return -EFAULT;
724                         } else
725 #endif
726                         if (copy_to_user(argp, &user, sizeof(user)))
727                                 return -EFAULT;
728
729                         return 0;
730                 }
731
732 #ifdef CONFIG_COMPAT
733         case NCP_IOC_SETPRIVATEDATA_32:
734 #endif
735         case NCP_IOC_SETPRIVATEDATA:
736                 {
737                         struct ncp_privatedata_ioctl user;
738                         void* new;
739                         void* old;
740                         size_t oldlen;
741
742 #ifdef CONFIG_COMPAT
743                         if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
744                                 struct compat_ncp_privatedata_ioctl user32;
745                                 if (copy_from_user(&user32, argp, sizeof(user32)))
746                                         return -EFAULT;
747                                 user.len = user32.len;
748                                 user.data = compat_ptr(user32.data);
749                         } else
750 #endif
751                         if (copy_from_user(&user, argp, sizeof(user)))
752                                 return -EFAULT;
753
754                         if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
755                                 return -ENOMEM;
756                         if (user.len) {
757                                 new = memdup_user(user.data, user.len);
758                                 if (IS_ERR(new))
759                                         return PTR_ERR(new);
760                         } else {
761                                 new = NULL;
762                         }
763                         down_write(&server->auth_rwsem);
764                         old = server->priv.data;
765                         oldlen = server->priv.len;
766                         server->priv.len = user.len;
767                         server->priv.data = new;
768                         up_write(&server->auth_rwsem);
769                         kfree(old);
770                         return 0;
771                 }
772
773 #ifdef CONFIG_NCPFS_NLS
774         case NCP_IOC_SETCHARSETS:
775                 return ncp_set_charsets(server, argp);
776
777         case NCP_IOC_GETCHARSETS:
778                 return ncp_get_charsets(server, argp);
779
780 #endif /* CONFIG_NCPFS_NLS */
781
782         case NCP_IOC_SETDENTRYTTL:
783                 {
784                         u_int32_t user;
785
786                         if (copy_from_user(&user, argp, sizeof(user)))
787                                 return -EFAULT;
788                         /* 20 secs at most... */
789                         if (user > 20000)
790                                 return -EINVAL;
791                         user = (user * HZ) / 1000;
792                         atomic_set(&server->dentry_ttl, user);
793                         return 0;
794                 }
795
796         case NCP_IOC_GETDENTRYTTL:
797                 {
798                         u_int32_t user = (atomic_read(&server->dentry_ttl) * 1000) / HZ;
799                         if (copy_to_user(argp, &user, sizeof(user)))
800                                 return -EFAULT;
801                         return 0;
802                 }
803
804         }
805         return -EINVAL;
806 }
807
808 long ncp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
809 {
810         struct inode *inode = filp->f_dentry->d_inode;
811         struct ncp_server *server = NCP_SERVER(inode);
812         uid_t uid = current_uid();
813         int need_drop_write = 0;
814         long ret;
815
816         switch (cmd) {
817         case NCP_IOC_SETCHARSETS:
818         case NCP_IOC_CONN_LOGGED_IN:
819         case NCP_IOC_SETROOT:
820                 if (!capable(CAP_SYS_ADMIN)) {
821                         ret = -EACCES;
822                         goto out;
823                 }
824                 break;
825         }
826         if (server->m.mounted_uid != uid) {
827                 switch (cmd) {
828                 /*
829                  * Only mount owner can issue these ioctls.  Information
830                  * necessary to authenticate to other NDS servers are
831                  * stored here.
832                  */
833                 case NCP_IOC_GETOBJECTNAME:
834                 case NCP_IOC_SETOBJECTNAME:
835                 case NCP_IOC_GETPRIVATEDATA:
836                 case NCP_IOC_SETPRIVATEDATA:
837 #ifdef CONFIG_COMPAT
838                 case NCP_IOC_GETOBJECTNAME_32:
839                 case NCP_IOC_SETOBJECTNAME_32:
840                 case NCP_IOC_GETPRIVATEDATA_32:
841                 case NCP_IOC_SETPRIVATEDATA_32:
842 #endif
843                         ret = -EACCES;
844                         goto out;
845                 /*
846                  * These require write access on the inode if user id
847                  * does not match.  Note that they do not write to the
848                  * file...  But old code did mnt_want_write, so I keep
849                  * it as is.  Of course not for mountpoint owner, as
850                  * that breaks read-only mounts altogether as ncpmount
851                  * needs working NCP_IOC_NCPREQUEST and
852                  * NCP_IOC_GET_FS_INFO.  Some of these codes (setdentryttl,
853                  * signinit, setsignwanted) should be probably restricted
854                  * to owner only, or even more to CAP_SYS_ADMIN).
855                  */
856                 case NCP_IOC_GET_FS_INFO:
857                 case NCP_IOC_GET_FS_INFO_V2:
858                 case NCP_IOC_NCPREQUEST:
859                 case NCP_IOC_SETDENTRYTTL:
860                 case NCP_IOC_SIGN_INIT:
861                 case NCP_IOC_LOCKUNLOCK:
862                 case NCP_IOC_SET_SIGN_WANTED:
863 #ifdef CONFIG_COMPAT
864                 case NCP_IOC_GET_FS_INFO_V2_32:
865                 case NCP_IOC_NCPREQUEST_32:
866 #endif
867                         ret = mnt_want_write_file(filp);
868                         if (ret)
869                                 goto out;
870                         need_drop_write = 1;
871                         ret = inode_permission(inode, MAY_WRITE);
872                         if (ret)
873                                 goto outDropWrite;
874                         break;
875                 /*
876                  * Read access required.
877                  */
878                 case NCP_IOC_GETMOUNTUID16:
879                 case NCP_IOC_GETMOUNTUID32:
880                 case NCP_IOC_GETMOUNTUID64:
881                 case NCP_IOC_GETROOT:
882                 case NCP_IOC_SIGN_WANTED:
883                         ret = inode_permission(inode, MAY_READ);
884                         if (ret)
885                                 goto out;
886                         break;
887                 /*
888                  * Anybody can read these.
889                  */
890                 case NCP_IOC_GETCHARSETS:
891                 case NCP_IOC_GETDENTRYTTL:
892                 default:
893                 /* Three codes below are protected by CAP_SYS_ADMIN above. */
894                 case NCP_IOC_SETCHARSETS:
895                 case NCP_IOC_CONN_LOGGED_IN:
896                 case NCP_IOC_SETROOT:
897                         break;
898                 }
899         }
900         ret = __ncp_ioctl(inode, cmd, arg);
901 outDropWrite:
902         if (need_drop_write)
903                 mnt_drop_write(filp->f_path.mnt);
904 out:
905         return ret;
906 }
907
908 #ifdef CONFIG_COMPAT
909 long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
910 {
911         long ret;
912
913         arg = (unsigned long) compat_ptr(arg);
914         ret = ncp_ioctl(file, cmd, arg);
915         return ret;
916 }
917 #endif