VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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/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
32 static int
33 ncp_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
60 static int
61 ncp_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  */
92 static int
93 ncp_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
150 static int
151 ncp_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
181 int 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, de, dosde;
303                         struct dentry* dentry;
304
305                         if (!capable(CAP_SYS_ADMIN))
306                         {
307                                 return -EACCES;
308                         }
309                         if (server->root_setuped) return -EBUSY;
310                         if (copy_from_user(&sr, argp, sizeof(sr)))
311                                 return -EFAULT;
312                         if (sr.volNumber < 0) {
313                                 server->m.mounted_vol[0] = 0;
314                                 vnum = NCP_NUMBER_OF_VOLUMES;
315                                 de = 0;
316                                 dosde = 0;
317                         } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
318                                 return -EINVAL;
319                         } else if (ncp_mount_subdir(server, sr.volNumber,
320                                                 sr.namespace, sr.dirEntNum,
321                                                 &vnum, &de, &dosde)) {
322                                 return -ENOENT;
323                         }
324                         
325                         dentry = inode->i_sb->s_root;
326                         server->root_setuped = 1;
327                         if (dentry) {
328                                 struct inode* inode = dentry->d_inode;
329                                 
330                                 if (inode) {
331                                         NCP_FINFO(inode)->volNumber = vnum;
332                                         NCP_FINFO(inode)->dirEntNum = de;
333                                         NCP_FINFO(inode)->DosDirNum = dosde;
334                                 } else
335                                         DPRINTK("ncpfs: s_root->d_inode==NULL\n");
336                         } else
337                                 DPRINTK("ncpfs: s_root==NULL\n");
338
339                         return 0;
340                 }
341
342 #ifdef CONFIG_NCPFS_PACKET_SIGNING      
343         case NCP_IOC_SIGN_INIT:
344                 if ((permission(inode, MAY_WRITE, NULL) != 0)
345                     && (current->uid != server->m.mounted_uid))
346                 {
347                         return -EACCES;
348                 }
349                 if (argp) {
350                         if (server->sign_wanted)
351                         {
352                                 struct ncp_sign_init sign;
353
354                                 if (copy_from_user(&sign, argp, sizeof(sign)))
355                                         return -EFAULT;
356                                 memcpy(server->sign_root,sign.sign_root,8);
357                                 memcpy(server->sign_last,sign.sign_last,16);
358                                 server->sign_active = 1;
359                         }
360                         /* ignore when signatures not wanted */
361                 } else {
362                         server->sign_active = 0;
363                 }
364                 return 0;               
365                 
366         case NCP_IOC_SIGN_WANTED:
367                 if (   (permission(inode, MAY_READ, NULL) != 0)
368                     && (current->uid != server->m.mounted_uid))
369                 {
370                         return -EACCES;
371                 }
372                 
373                 if (put_user(server->sign_wanted, (int __user *)argp))
374                         return -EFAULT;
375                 return 0;
376         case NCP_IOC_SET_SIGN_WANTED:
377                 {
378                         int newstate;
379
380                         if (   (permission(inode, MAY_WRITE, NULL) != 0)
381                             && (current->uid != server->m.mounted_uid))
382                         {
383                                 return -EACCES;
384                         }
385                         /* get only low 8 bits... */
386                         if (get_user(newstate, (unsigned char __user *)argp))
387                                 return -EFAULT;
388                         if (server->sign_active) {
389                                 /* cannot turn signatures OFF when active */
390                                 if (!newstate) return -EINVAL;
391                         } else {
392                                 server->sign_wanted = newstate != 0;
393                         }
394                         return 0;
395                 }
396
397 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
398
399 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
400         case NCP_IOC_LOCKUNLOCK:
401                 if (   (permission(inode, MAY_WRITE, NULL) != 0)
402                     && (current->uid != server->m.mounted_uid))
403                 {
404                         return -EACCES;
405                 }
406                 {
407                         struct ncp_lock_ioctl    rqdata;
408                         int result;
409
410                         if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
411                                 return -EFAULT;
412                         if (rqdata.origin != 0)
413                                 return -EINVAL;
414                         /* check for cmd */
415                         switch (rqdata.cmd) {
416                                 case NCP_LOCK_EX:
417                                 case NCP_LOCK_SH:
418                                                 if (rqdata.timeout == 0)
419                                                         rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
420                                                 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
421                                                         rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
422                                                 break;
423                                 case NCP_LOCK_LOG:
424                                                 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;      /* has no effect */
425                                 case NCP_LOCK_CLEAR:
426                                                 break;
427                                 default:
428                                                 return -EINVAL;
429                         }
430                         /* locking needs both read and write access */
431                         if ((result = ncp_make_open(inode, O_RDWR)) != 0)
432                         {
433                                 return result;
434                         }
435                         result = -EIO;
436                         if (!ncp_conn_valid(server))
437                                 goto outrel;
438                         result = -EISDIR;
439                         if (!S_ISREG(inode->i_mode))
440                                 goto outrel;
441                         if (rqdata.cmd == NCP_LOCK_CLEAR)
442                         {
443                                 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
444                                                         NCP_FINFO(inode)->file_handle, 
445                                                         rqdata.offset,
446                                                         rqdata.length);
447                                 if (result > 0) result = 0;     /* no such lock */
448                         }
449                         else
450                         {
451                                 int lockcmd;
452
453                                 switch (rqdata.cmd)
454                                 {
455                                         case NCP_LOCK_EX:  lockcmd=1; break;
456                                         case NCP_LOCK_SH:  lockcmd=3; break;
457                                         default:           lockcmd=0; break;
458                                 }
459                                 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
460                                                         NCP_FINFO(inode)->file_handle,
461                                                         lockcmd,
462                                                         rqdata.offset,
463                                                         rqdata.length,
464                                                         rqdata.timeout);
465                                 if (result > 0) result = -EAGAIN;
466                         }
467 outrel:                 
468                         ncp_inode_close(inode);
469                         return result;
470                 }
471 #endif  /* CONFIG_NCPFS_IOCTL_LOCKING */
472
473         case NCP_IOC_GETOBJECTNAME:
474                 if (current->uid != server->m.mounted_uid) {
475                         return -EACCES;
476                 }
477                 {
478                         struct ncp_objectname_ioctl user;
479                         size_t outl;
480
481                         if (copy_from_user(&user, argp, sizeof(user)))
482                                 return -EFAULT;
483                         user.auth_type = server->auth.auth_type;
484                         outl = user.object_name_len;
485                         user.object_name_len = server->auth.object_name_len;
486                         if (outl > user.object_name_len)
487                                 outl = user.object_name_len;
488                         if (outl) {
489                                 if (copy_to_user(user.object_name,
490                                                  server->auth.object_name,
491                                                  outl)) return -EFAULT;
492                         }
493                         if (copy_to_user(argp, &user, sizeof(user)))
494                                 return -EFAULT;
495                         return 0;
496                 }
497         case NCP_IOC_SETOBJECTNAME:
498                 if (current->uid != server->m.mounted_uid) {
499                         return -EACCES;
500                 }
501                 {
502                         struct ncp_objectname_ioctl user;
503                         void* newname;
504                         void* oldname;
505                         size_t oldnamelen;
506                         void* oldprivate;
507                         size_t oldprivatelen;
508
509                         if (copy_from_user(&user, argp, sizeof(user)))
510                                 return -EFAULT;
511                         if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
512                                 return -ENOMEM;
513                         if (user.object_name_len) {
514                                 newname = ncp_kmalloc(user.object_name_len, GFP_USER);
515                                 if (!newname) return -ENOMEM;
516                                 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
517                                         ncp_kfree_s(newname, user.object_name_len);
518                                         return -EFAULT;
519                                 }
520                         } else {
521                                 newname = NULL;
522                         }
523                         /* enter critical section */
524                         /* maybe that kfree can sleep so do that this way */
525                         /* it is at least more SMP friendly (in future...) */
526                         oldname = server->auth.object_name;
527                         oldnamelen = server->auth.object_name_len;
528                         oldprivate = server->priv.data;
529                         oldprivatelen = server->priv.len;
530                         server->auth.auth_type = user.auth_type;
531                         server->auth.object_name_len = user.object_name_len;
532                         server->auth.object_name = newname;
533                         server->priv.len = 0;
534                         server->priv.data = NULL;
535                         /* leave critical section */
536                         if (oldprivate) ncp_kfree_s(oldprivate, oldprivatelen);
537                         if (oldname) ncp_kfree_s(oldname, oldnamelen);
538                         return 0;
539                 }
540         case NCP_IOC_GETPRIVATEDATA:
541                 if (current->uid != server->m.mounted_uid) {
542                         return -EACCES;
543                 }
544                 {
545                         struct ncp_privatedata_ioctl user;
546                         size_t outl;
547
548                         if (copy_from_user(&user, argp, sizeof(user)))
549                                 return -EFAULT;
550                         outl = user.len;
551                         user.len = server->priv.len;
552                         if (outl > user.len) outl = user.len;
553                         if (outl) {
554                                 if (copy_to_user(user.data,
555                                                  server->priv.data,
556                                                  outl)) return -EFAULT;
557                         }
558                         if (copy_to_user(argp, &user, sizeof(user)))
559                                 return -EFAULT;
560                         return 0;
561                 }
562         case NCP_IOC_SETPRIVATEDATA:
563                 if (current->uid != server->m.mounted_uid) {
564                         return -EACCES;
565                 }
566                 {
567                         struct ncp_privatedata_ioctl user;
568                         void* new;
569                         void* old;
570                         size_t oldlen;
571
572                         if (copy_from_user(&user, argp, sizeof(user)))
573                                 return -EFAULT;
574                         if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
575                                 return -ENOMEM;
576                         if (user.len) {
577                                 new = ncp_kmalloc(user.len, GFP_USER);
578                                 if (!new) return -ENOMEM;
579                                 if (copy_from_user(new, user.data, user.len)) {
580                                         ncp_kfree_s(new, user.len);
581                                         return -EFAULT;
582                                 }
583                         } else {
584                                 new = NULL;
585                         }
586                         /* enter critical section */
587                         old = server->priv.data;
588                         oldlen = server->priv.len;
589                         server->priv.len = user.len;
590                         server->priv.data = new;
591                         /* leave critical section */
592                         if (old) ncp_kfree_s(old, oldlen);
593                         return 0;
594                 }
595
596 #ifdef CONFIG_NCPFS_NLS
597         case NCP_IOC_SETCHARSETS:
598                 return ncp_set_charsets(server, argp);
599                 
600         case NCP_IOC_GETCHARSETS:
601                 return ncp_get_charsets(server, argp);
602
603 #endif /* CONFIG_NCPFS_NLS */
604
605         case NCP_IOC_SETDENTRYTTL:
606                 if ((permission(inode, MAY_WRITE, NULL) != 0) &&
607                                  (current->uid != server->m.mounted_uid))
608                         return -EACCES;
609                 {
610                         u_int32_t user;
611
612                         if (copy_from_user(&user, argp, sizeof(user)))
613                                 return -EFAULT;
614                         /* 20 secs at most... */
615                         if (user > 20000)
616                                 return -EINVAL;
617                         user = (user * HZ) / 1000;
618                         server->dentry_ttl = user;
619                         return 0;
620                 }
621                 
622         case NCP_IOC_GETDENTRYTTL:
623                 {
624                         u_int32_t user = (server->dentry_ttl * 1000) / HZ;
625                         if (copy_to_user(argp, &user, sizeof(user)))
626                                 return -EFAULT;
627                         return 0;
628                 }
629
630         }
631 /* #ifdef CONFIG_UID16 */
632         /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
633            so we have this out of switch */
634         if (cmd == NCP_IOC_GETMOUNTUID) {
635                 __kernel_uid_t uid = 0;
636                 if ((permission(inode, MAY_READ, NULL) != 0)
637                     && (current->uid != server->m.mounted_uid)) {
638                         return -EACCES;
639                 }
640                 SET_UID(uid, server->m.mounted_uid);
641                 if (put_user(uid, (__kernel_uid_t __user *)argp))
642                         return -EFAULT;
643                 return 0;
644         }
645 /* #endif */
646         return -EINVAL;
647 }