[PATCH] ncpfs: remove kmalloc wrapper
[linux-2.6.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 = kmalloc(user.object_name_len, GFP_USER);
522                                 if (!newname)
523                                         return -ENOMEM;
524                                 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
525                                         kfree(newname);
526                                         return -EFAULT;
527                                 }
528                         } else {
529                                 newname = NULL;
530                         }
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 */
544                         kfree(oldprivate);
545                         kfree(oldname);
546                         return 0;
547                 }
548         case NCP_IOC_GETPRIVATEDATA:
549                 if (current->uid != server->m.mounted_uid) {
550                         return -EACCES;
551                 }
552                 {
553                         struct ncp_privatedata_ioctl user;
554                         size_t outl;
555
556                         if (copy_from_user(&user, argp, sizeof(user)))
557                                 return -EFAULT;
558                         outl = user.len;
559                         user.len = server->priv.len;
560                         if (outl > user.len) outl = user.len;
561                         if (outl) {
562                                 if (copy_to_user(user.data,
563                                                  server->priv.data,
564                                                  outl)) return -EFAULT;
565                         }
566                         if (copy_to_user(argp, &user, sizeof(user)))
567                                 return -EFAULT;
568                         return 0;
569                 }
570         case NCP_IOC_SETPRIVATEDATA:
571                 if (current->uid != server->m.mounted_uid) {
572                         return -EACCES;
573                 }
574                 {
575                         struct ncp_privatedata_ioctl user;
576                         void* new;
577                         void* old;
578                         size_t oldlen;
579
580                         if (copy_from_user(&user, argp, sizeof(user)))
581                                 return -EFAULT;
582                         if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
583                                 return -ENOMEM;
584                         if (user.len) {
585                                 new = kmalloc(user.len, GFP_USER);
586                                 if (!new)
587                                         return -ENOMEM;
588                                 if (copy_from_user(new, user.data, user.len)) {
589                                         kfree(new);
590                                         return -EFAULT;
591                                 }
592                         } else {
593                                 new = NULL;
594                         }
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 */
601                         kfree(old);
602                         return 0;
603                 }
604
605 #ifdef CONFIG_NCPFS_NLS
606         case NCP_IOC_SETCHARSETS:
607                 return ncp_set_charsets(server, argp);
608                 
609         case NCP_IOC_GETCHARSETS:
610                 return ncp_get_charsets(server, argp);
611
612 #endif /* CONFIG_NCPFS_NLS */
613
614         case NCP_IOC_SETDENTRYTTL:
615                 if ((file_permission(filp, MAY_WRITE) != 0) &&
616                                  (current->uid != server->m.mounted_uid))
617                         return -EACCES;
618                 {
619                         u_int32_t user;
620
621                         if (copy_from_user(&user, argp, sizeof(user)))
622                                 return -EFAULT;
623                         /* 20 secs at most... */
624                         if (user > 20000)
625                                 return -EINVAL;
626                         user = (user * HZ) / 1000;
627                         server->dentry_ttl = user;
628                         return 0;
629                 }
630                 
631         case NCP_IOC_GETDENTRYTTL:
632                 {
633                         u_int32_t user = (server->dentry_ttl * 1000) / HZ;
634                         if (copy_to_user(argp, &user, sizeof(user)))
635                                 return -EFAULT;
636                         return 0;
637                 }
638
639         }
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)) {
647                         return -EACCES;
648                 }
649                 SET_UID(uid, server->m.mounted_uid);
650                 if (put_user(uid, (__kernel_uid_t __user *)argp))
651                         return -EFAULT;
652                 return 0;
653         }
654 /* #endif */
655         return -EINVAL;
656 }