VirtualBox

source: vbox/trunk/src/VBox/RDP/client/disk.c@ 33656

Last change on this file since 33656 was 33656, checked in by vboxsync, 14 years ago

*: rebrand Sun (L)GPL disclaimers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.8 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Disk Redirection
4 Copyright (C) Jeroen Meijer 2003-2007
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
22 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
24 * the General Public License version 2 (GPLv2) at this time for any software where
25 * a choice of GPL license versions is made available with the language indicating
26 * that GPLv2 or any later version may be used, or where a choice of which version
27 * of the GPL is applied is otherwise unspecified.
28 */
29
30#include "disk.h"
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <unistd.h>
35#include <fcntl.h> /* open, close */
36#include <dirent.h> /* opendir, closedir, readdir */
37#include <fnmatch.h>
38#include <errno.h> /* errno */
39#include <stdio.h>
40
41#include <utime.h>
42#include <time.h> /* ctime */
43
44#if (defined(HAVE_DIRFD) || (HAVE_DECL_DIRFD == 1))
45#define DIRFD(a) (dirfd(a))
46#else
47#define DIRFD(a) ((a)->DIR_FD_MEMBER_NAME)
48#endif
49
50/* TODO: Fix mntent-handling for solaris
51 * #include <sys/mntent.h> */
52#if (defined(HAVE_MNTENT_H) && defined(HAVE_SETMNTENT))
53#include <mntent.h>
54#define MNTENT_PATH "/etc/mtab"
55#define USE_SETMNTENT
56#endif
57
58#ifdef HAVE_SYS_VFS_H
59#include <sys/vfs.h>
60#endif
61
62#ifdef HAVE_SYS_STATVFS_H
63#include <sys/statvfs.h>
64#endif
65
66#ifdef HAVE_SYS_STATFS_H
67#include <sys/statfs.h>
68#endif
69
70#ifdef HAVE_SYS_PARAM_H
71#include <sys/param.h>
72#endif
73
74#ifdef HAVE_SYS_MOUNT_H
75#include <sys/mount.h>
76#endif
77
78#include "rdesktop.h"
79
80#ifdef STAT_STATFS3_OSF1
81#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf)))
82#define STATFS_T statfs
83#define USE_STATFS
84#endif
85
86#ifdef STAT_STATVFS
87#define STATFS_FN(path, buf) (statvfs(path,buf))
88#define STATFS_T statvfs
89#define USE_STATVFS
90#endif
91
92#ifdef STAT_STATVFS64
93#define STATFS_FN(path, buf) (statvfs64(path,buf))
94#define STATFS_T statvfs64
95#define USE_STATVFS
96#endif
97
98#if (defined(STAT_STATFS2_FS_DATA) || defined(STAT_STATFS2_BSIZE) || defined(STAT_STATFS2_FSIZE))
99#define STATFS_FN(path, buf) (statfs(path,buf))
100#define STATFS_T statfs
101#define USE_STATFS
102#endif
103
104#ifdef STAT_STATFS4
105#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf),0))
106#define STATFS_T statfs
107#define USE_STATFS
108#endif
109
110#if ((defined(USE_STATFS) && defined(HAVE_STRUCT_STATFS_F_NAMEMAX)) || (defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_NAMEMAX)))
111#define F_NAMELEN(buf) ((buf).f_namemax)
112#endif
113
114#if ((defined(USE_STATFS) && defined(HAVE_STRUCT_STATFS_F_NAMELEN)) || (defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_NAMELEN)))
115#define F_NAMELEN(buf) ((buf).f_namelen)
116#endif
117
118#ifndef F_NAMELEN
119#define F_NAMELEN(buf) (255)
120#endif
121
122/* Dummy statfs fallback */
123#ifndef STATFS_T
124struct dummy_statfs_t
125{
126 long f_bfree;
127 long f_bsize;
128 long f_blocks;
129 int f_namelen;
130 int f_namemax;
131};
132
133static int
134dummy_statfs(struct dummy_statfs_t *buf)
135{
136 buf->f_blocks = 262144;
137 buf->f_bfree = 131072;
138 buf->f_bsize = 512;
139 buf->f_namelen = 255;
140 buf->f_namemax = 255;
141
142 return 0;
143}
144
145#define STATFS_T dummy_statfs_t
146#define STATFS_FN(path,buf) (dummy_statfs(buf))
147#endif
148
149extern RDPDR_DEVICE g_rdpdr_device[];
150
151FILEINFO g_fileinfo[MAX_OPEN_FILES];
152RD_BOOL g_notify_stamp = False;
153
154typedef struct
155{
156 char name[PATH_MAX];
157 char label[PATH_MAX];
158 unsigned long serial;
159 char type[PATH_MAX];
160} FsInfoType;
161
162static RD_NTSTATUS NotifyInfo(RD_NTHANDLE handle, uint32 info_class, NOTIFY * p);
163
164static time_t
165get_create_time(struct stat *filestat)
166{
167 time_t ret, ret1;
168
169 ret = MIN(filestat->st_ctime, filestat->st_mtime);
170 ret1 = MIN(ret, filestat->st_atime);
171
172 if (ret1 != (time_t) 0)
173 return ret1;
174
175 return ret;
176}
177
178/* Convert seconds since 1970 to a filetime */
179static void
180seconds_since_1970_to_filetime(time_t seconds, uint32 * high, uint32 * low)
181{
182 unsigned long long ticks;
183
184 ticks = (seconds + 11644473600LL) * 10000000;
185 *low = (uint32) ticks;
186 *high = (uint32) (ticks >> 32);
187}
188
189/* Convert seconds since 1970 back to filetime */
190static time_t
191convert_1970_to_filetime(uint32 high, uint32 low)
192{
193 unsigned long long ticks;
194 time_t val;
195
196 ticks = low + (((unsigned long long) high) << 32);
197 ticks /= 10000000;
198 ticks -= 11644473600LL;
199
200 val = (time_t) ticks;
201 return (val);
202
203}
204
205/* A wrapper for ftruncate which supports growing files, even if the
206 native ftruncate doesn't. This is needed on Linux FAT filesystems,
207 for example. */
208static int
209ftruncate_growable(int fd, off_t length)
210{
211 int ret;
212 off_t pos;
213 static const char zero = 0;
214
215 /* Try the simple method first */
216 if ((ret = ftruncate(fd, length)) != -1)
217 {
218 return ret;
219 }
220
221 /*
222 * Some kind of error. Perhaps we were trying to grow. Retry
223 * in a safe way.
224 */
225
226 /* Get current position */
227 if ((pos = lseek(fd, 0, SEEK_CUR)) == -1)
228 {
229 perror("lseek");
230 return -1;
231 }
232
233 /* Seek to new size */
234 if (lseek(fd, length, SEEK_SET) == -1)
235 {
236 perror("lseek");
237 return -1;
238 }
239
240 /* Write a zero */
241 if (write(fd, &zero, 1) == -1)
242 {
243 perror("write");
244 return -1;
245 }
246
247 /* Truncate. This shouldn't fail. */
248 if (ftruncate(fd, length) == -1)
249 {
250 perror("ftruncate");
251 return -1;
252 }
253
254 /* Restore position */
255 if (lseek(fd, pos, SEEK_SET) == -1)
256 {
257 perror("lseek");
258 return -1;
259 }
260
261 return 0;
262}
263
264/* Just like open(2), but if a open with O_EXCL fails, retry with
265 GUARDED semantics. This might be necessary because some filesystems
266 (such as NFS filesystems mounted from a unfsd server) doesn't
267 support O_EXCL. GUARDED semantics are subject to race conditions,
268 but we can live with that.
269*/
270static int
271open_weak_exclusive(const char *pathname, int flags, mode_t mode)
272{
273 int ret;
274 struct stat filestat;
275
276 ret = open(pathname, flags, mode);
277 if (ret != -1 || !(flags & O_EXCL))
278 {
279 /* Success, or not using O_EXCL */
280 return ret;
281 }
282
283 /* An error occured, and we are using O_EXCL. In case the FS
284 doesn't support O_EXCL, some kind of error will be
285 returned. Unfortunately, we don't know which one. Linux
286 2.6.8 seems to return 524, but I cannot find a documented
287 #define for this case. So, we'll return only on errors that
288 we know aren't related to O_EXCL. */
289 switch (errno)
290 {
291 case EACCES:
292 case EEXIST:
293 case EINTR:
294 case EISDIR:
295 case ELOOP:
296 case ENAMETOOLONG:
297 case ENOENT:
298 case ENOTDIR:
299 return ret;
300 }
301
302 /* Retry with GUARDED semantics */
303 if (stat(pathname, &filestat) != -1)
304 {
305 /* File exists */
306 errno = EEXIST;
307 return -1;
308 }
309 else
310 {
311 return open(pathname, flags & ~O_EXCL, mode);
312 }
313}
314
315/* Enumeration of devices from rdesktop.c */
316/* returns numer of units found and initialized. */
317/* optarg looks like ':h=/mnt/floppy,b=/mnt/usbdevice1' */
318/* when it arrives to this function. */
319int
320disk_enum_devices(uint32 * id, char *optarg)
321{
322 char *pos = optarg;
323 char *pos2;
324 int count = 0;
325
326 /* skip the first colon */
327 optarg++;
328 while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
329 {
330 pos2 = next_arg(optarg, '=');
331
332 strncpy(g_rdpdr_device[*id].name, optarg, sizeof(g_rdpdr_device[*id].name) - 1);
333 if (strlen(optarg) > (sizeof(g_rdpdr_device[*id].name) - 1))
334 fprintf(stderr, "share name %s truncated to %s\n", optarg,
335 g_rdpdr_device[*id].name);
336
337 g_rdpdr_device[*id].local_path = (char *) xmalloc(strlen(pos2) + 1);
338 strcpy(g_rdpdr_device[*id].local_path, pos2);
339 g_rdpdr_device[*id].device_type = DEVICE_TYPE_DISK;
340 count++;
341 (*id)++;
342
343 optarg = pos;
344 }
345 return count;
346}
347
348/* Opens or creates a file or directory */
349static RD_NTSTATUS
350disk_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition,
351 uint32 flags_and_attributes, char *filename, RD_NTHANDLE * phandle)
352{
353 RD_NTHANDLE handle;
354 DIR *dirp;
355 int flags, mode;
356 char path[PATH_MAX];
357 struct stat filestat;
358
359 handle = 0;
360 dirp = NULL;
361 flags = 0;
362 mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
363
364 if (*filename && filename[strlen(filename) - 1] == '/')
365 filename[strlen(filename) - 1] = 0;
366 sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename);
367
368 switch (create_disposition)
369 {
370 case CREATE_ALWAYS:
371
372 /* Delete existing file/link. */
373 unlink(path);
374 flags |= O_CREAT;
375 break;
376
377 case CREATE_NEW:
378
379 /* If the file already exists, then fail. */
380 flags |= O_CREAT | O_EXCL;
381 break;
382
383 case OPEN_ALWAYS:
384
385 /* Create if not already exists. */
386 flags |= O_CREAT;
387 break;
388
389 case OPEN_EXISTING:
390
391 /* Default behaviour */
392 break;
393
394 case TRUNCATE_EXISTING:
395
396 /* If the file does not exist, then fail. */
397 flags |= O_TRUNC;
398 break;
399 }
400
401 /*printf("Open: \"%s\" flags: %X, accessmask: %X sharemode: %X create disp: %X\n", path, flags_and_attributes, accessmask, sharemode, create_disposition); */
402
403 /* Get information about file and set that flag ourselfs */
404 if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode)))
405 {
406 if (flags_and_attributes & FILE_NON_DIRECTORY_FILE)
407 return RD_STATUS_FILE_IS_A_DIRECTORY;
408 else
409 flags_and_attributes |= FILE_DIRECTORY_FILE;
410 }
411
412 if (flags_and_attributes & FILE_DIRECTORY_FILE)
413 {
414 if (flags & O_CREAT)
415 {
416 mkdir(path, mode);
417 }
418
419 dirp = opendir(path);
420 if (!dirp)
421 {
422 switch (errno)
423 {
424 case EACCES:
425
426 return RD_STATUS_ACCESS_DENIED;
427
428 case ENOENT:
429
430 return RD_STATUS_NO_SUCH_FILE;
431
432 default:
433
434 perror("opendir");
435 return RD_STATUS_NO_SUCH_FILE;
436 }
437 }
438 handle = DIRFD(dirp);
439 }
440 else
441 {
442
443 if (accessmask & GENERIC_ALL
444 || (accessmask & GENERIC_READ && accessmask & GENERIC_WRITE))
445 {
446 flags |= O_RDWR;
447 }
448 else if ((accessmask & GENERIC_WRITE) && !(accessmask & GENERIC_READ))
449 {
450 flags |= O_WRONLY;
451 }
452 else
453 {
454 flags |= O_RDONLY;
455 }
456
457 handle = open_weak_exclusive(path, flags, mode);
458 if (handle == -1)
459 {
460 switch (errno)
461 {
462 case EISDIR:
463
464 return RD_STATUS_FILE_IS_A_DIRECTORY;
465
466 case EACCES:
467
468 return RD_STATUS_ACCESS_DENIED;
469
470 case ENOENT:
471
472 return RD_STATUS_NO_SUCH_FILE;
473 case EEXIST:
474
475 return RD_STATUS_OBJECT_NAME_COLLISION;
476 default:
477
478 perror("open");
479 return RD_STATUS_NO_SUCH_FILE;
480 }
481 }
482
483 /* all read and writes of files should be non blocking */
484 if (fcntl(handle, F_SETFL, O_NONBLOCK) == -1)
485 perror("fcntl");
486 }
487
488 if (handle >= MAX_OPEN_FILES)
489 {
490 error("Maximum number of open files (%s) reached. Increase MAX_OPEN_FILES!\n",
491 handle);
492 exit(1);
493 }
494
495 if (dirp)
496 g_fileinfo[handle].pdir = dirp;
497 else
498 g_fileinfo[handle].pdir = NULL;
499
500 g_fileinfo[handle].device_id = device_id;
501 g_fileinfo[handle].flags_and_attributes = flags_and_attributes;
502 g_fileinfo[handle].accessmask = accessmask;
503 strncpy(g_fileinfo[handle].path, path, PATH_MAX - 1);
504 g_fileinfo[handle].delete_on_close = False;
505
506 if (accessmask & GENERIC_ALL || accessmask & GENERIC_WRITE)
507 g_notify_stamp = True;
508
509 *phandle = handle;
510 return RD_STATUS_SUCCESS;
511}
512
513static RD_NTSTATUS
514disk_close(RD_NTHANDLE handle)
515{
516 struct fileinfo *pfinfo;
517
518 pfinfo = &(g_fileinfo[handle]);
519
520 if (pfinfo->accessmask & GENERIC_ALL || pfinfo->accessmask & GENERIC_WRITE)
521 g_notify_stamp = True;
522
523 rdpdr_abort_io(handle, 0, RD_STATUS_CANCELLED);
524
525 if (pfinfo->pdir)
526 {
527 if (closedir(pfinfo->pdir) < 0)
528 {
529 perror("closedir");
530 return RD_STATUS_INVALID_HANDLE;
531 }
532
533 if (pfinfo->delete_on_close)
534 if (rmdir(pfinfo->path) < 0)
535 {
536 perror(pfinfo->path);
537 return RD_STATUS_ACCESS_DENIED;
538 }
539 pfinfo->delete_on_close = False;
540 }
541 else
542 {
543 if (close(handle) < 0)
544 {
545 perror("close");
546 return RD_STATUS_INVALID_HANDLE;
547 }
548 if (pfinfo->delete_on_close)
549 if (unlink(pfinfo->path) < 0)
550 {
551 perror(pfinfo->path);
552 return RD_STATUS_ACCESS_DENIED;
553 }
554
555 pfinfo->delete_on_close = False;
556 }
557
558 return RD_STATUS_SUCCESS;
559}
560
561static RD_NTSTATUS
562disk_read(RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
563{
564 int n;
565
566#if 0
567 /* browsing dir ???? */
568 /* each request is 24 bytes */
569 if (g_fileinfo[handle].flags_and_attributes & FILE_DIRECTORY_FILE)
570 {
571 *result = 0;
572 return STATUS_SUCCESS;
573 }
574#endif
575
576 lseek(handle, offset, SEEK_SET);
577
578 n = read(handle, data, length);
579
580 if (n < 0)
581 {
582 *result = 0;
583 switch (errno)
584 {
585 case EISDIR:
586 /* Implement 24 Byte directory read ??
587 with STATUS_NOT_IMPLEMENTED server doesn't read again */
588 /* return STATUS_FILE_IS_A_DIRECTORY; */
589 return RD_STATUS_NOT_IMPLEMENTED;
590 default:
591 perror("read");
592 return RD_STATUS_INVALID_PARAMETER;
593 }
594 }
595
596 *result = n;
597
598 return RD_STATUS_SUCCESS;
599}
600
601static RD_NTSTATUS
602disk_write(RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
603{
604 int n;
605
606 lseek(handle, offset, SEEK_SET);
607
608 n = write(handle, data, length);
609
610 if (n < 0)
611 {
612 perror("write");
613 *result = 0;
614 switch (errno)
615 {
616 case ENOSPC:
617 return RD_STATUS_DISK_FULL;
618 default:
619 return RD_STATUS_ACCESS_DENIED;
620 }
621 }
622
623 *result = n;
624
625 return RD_STATUS_SUCCESS;
626}
627
628RD_NTSTATUS
629disk_query_information(RD_NTHANDLE handle, uint32 info_class, STREAM out)
630{
631 uint32 file_attributes, ft_high, ft_low;
632 struct stat filestat;
633 char *path, *filename;
634
635 path = g_fileinfo[handle].path;
636
637 /* Get information about file */
638 if (fstat(handle, &filestat) != 0)
639 {
640 perror("stat");
641 out_uint8(out, 0);
642 return RD_STATUS_ACCESS_DENIED;
643 }
644
645 /* Set file attributes */
646 file_attributes = 0;
647 if (S_ISDIR(filestat.st_mode))
648 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
649
650 filename = 1 + strrchr(path, '/');
651 if (filename && filename[0] == '.')
652 file_attributes |= FILE_ATTRIBUTE_HIDDEN;
653
654 if (!file_attributes)
655 file_attributes |= FILE_ATTRIBUTE_NORMAL;
656
657 if (!(filestat.st_mode & S_IWUSR))
658 file_attributes |= FILE_ATTRIBUTE_READONLY;
659
660 /* Return requested data */
661 switch (info_class)
662 {
663 case FileBasicInformation:
664 seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high,
665 &ft_low);
666 out_uint32_le(out, ft_low); /* create_access_time */
667 out_uint32_le(out, ft_high);
668
669 seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);
670 out_uint32_le(out, ft_low); /* last_access_time */
671 out_uint32_le(out, ft_high);
672
673 seconds_since_1970_to_filetime(filestat.st_mtime, &ft_high, &ft_low);
674 out_uint32_le(out, ft_low); /* last_write_time */
675 out_uint32_le(out, ft_high);
676
677 seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low);
678 out_uint32_le(out, ft_low); /* last_change_time */
679 out_uint32_le(out, ft_high);
680
681 out_uint32_le(out, file_attributes);
682 break;
683
684 case FileStandardInformation:
685
686 out_uint32_le(out, filestat.st_size); /* Allocation size */
687 out_uint32_le(out, 0);
688 out_uint32_le(out, filestat.st_size); /* End of file */
689 out_uint32_le(out, 0);
690 out_uint32_le(out, filestat.st_nlink); /* Number of links */
691 out_uint8(out, 0); /* Delete pending */
692 out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); /* Directory */
693 break;
694
695 case FileObjectIdInformation:
696
697 out_uint32_le(out, file_attributes); /* File Attributes */
698 out_uint32_le(out, 0); /* Reparse Tag */
699 break;
700
701 default:
702
703 unimpl("IRP Query (File) Information class: 0x%x\n", info_class);
704 return RD_STATUS_INVALID_PARAMETER;
705 }
706 return RD_STATUS_SUCCESS;
707}
708
709RD_NTSTATUS
710disk_set_information(RD_NTHANDLE handle, uint32 info_class, STREAM in, STREAM out)
711{
712 uint32 length, file_attributes, ft_high, ft_low, delete_on_close;
713 char newname[PATH_MAX], fullpath[PATH_MAX];
714 struct fileinfo *pfinfo;
715 int mode;
716 struct stat filestat;
717 time_t write_time, change_time, access_time, mod_time;
718 struct utimbuf tvs;
719 struct STATFS_T stat_fs;
720
721 pfinfo = &(g_fileinfo[handle]);
722 g_notify_stamp = True;
723
724 switch (info_class)
725 {
726 case FileBasicInformation:
727 write_time = change_time = access_time = 0;
728
729 in_uint8s(in, 4); /* Handle of root dir? */
730 in_uint8s(in, 24); /* unknown */
731
732 /* CreationTime */
733 in_uint32_le(in, ft_low);
734 in_uint32_le(in, ft_high);
735
736 /* AccessTime */
737 in_uint32_le(in, ft_low);
738 in_uint32_le(in, ft_high);
739 if (ft_low || ft_high)
740 access_time = convert_1970_to_filetime(ft_high, ft_low);
741
742 /* WriteTime */
743 in_uint32_le(in, ft_low);
744 in_uint32_le(in, ft_high);
745 if (ft_low || ft_high)
746 write_time = convert_1970_to_filetime(ft_high, ft_low);
747
748 /* ChangeTime */
749 in_uint32_le(in, ft_low);
750 in_uint32_le(in, ft_high);
751 if (ft_low || ft_high)
752 change_time = convert_1970_to_filetime(ft_high, ft_low);
753
754 in_uint32_le(in, file_attributes);
755
756 if (fstat(handle, &filestat))
757 return RD_STATUS_ACCESS_DENIED;
758
759 tvs.modtime = filestat.st_mtime;
760 tvs.actime = filestat.st_atime;
761 if (access_time)
762 tvs.actime = access_time;
763
764
765 if (write_time || change_time)
766 mod_time = MIN(write_time, change_time);
767 else
768 mod_time = write_time ? write_time : change_time;
769
770 if (mod_time)
771 tvs.modtime = mod_time;
772
773
774 if (access_time || write_time || change_time)
775 {
776#if WITH_DEBUG_RDP5
777 printf("FileBasicInformation access time %s",
778 ctime(&tvs.actime));
779 printf("FileBasicInformation modification time %s",
780 ctime(&tvs.modtime));
781#endif
782 if (utime(pfinfo->path, &tvs) && errno != EPERM)
783 return RD_STATUS_ACCESS_DENIED;
784 }
785
786 if (!file_attributes)
787 break; /* not valid */
788
789 mode = filestat.st_mode;
790
791 if (file_attributes & FILE_ATTRIBUTE_READONLY)
792 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
793 else
794 mode |= S_IWUSR;
795
796 mode &= 0777;
797#if WITH_DEBUG_RDP5
798 printf("FileBasicInformation set access mode 0%o", mode);
799#endif
800
801 if (fchmod(handle, mode))
802 return RD_STATUS_ACCESS_DENIED;
803
804 break;
805
806 case FileRenameInformation:
807
808 in_uint8s(in, 4); /* Handle of root dir? */
809 in_uint8s(in, 0x1a); /* unknown */
810 in_uint32_le(in, length);
811
812 if (length && (length / 2) < 256)
813 {
814 rdp_in_unistr(in, newname, sizeof(newname), length);
815 convert_to_unix_filename(newname);
816 }
817 else
818 {
819 return RD_STATUS_INVALID_PARAMETER;
820 }
821
822 sprintf(fullpath, "%s%s", g_rdpdr_device[pfinfo->device_id].local_path,
823 newname);
824
825 if (rename(pfinfo->path, fullpath) != 0)
826 {
827 perror("rename");
828 return RD_STATUS_ACCESS_DENIED;
829 }
830 break;
831
832 case FileDispositionInformation:
833 /* As far as I understand it, the correct
834 thing to do here is to *schedule* a delete,
835 so it will be deleted when the file is
836 closed. Subsequent
837 FileDispositionInformation requests with
838 DeleteFile set to FALSE should unschedule
839 the delete. See
840 http://www.osronline.com/article.cfm?article=245. */
841
842 in_uint32_le(in, delete_on_close);
843
844 if (delete_on_close ||
845 (pfinfo->
846 accessmask & (FILE_DELETE_ON_CLOSE | FILE_COMPLETE_IF_OPLOCKED)))
847 {
848 pfinfo->delete_on_close = True;
849 }
850
851 break;
852
853 case FileAllocationInformation:
854 /* Fall through to FileEndOfFileInformation,
855 which uses ftrunc. This is like Samba with
856 "strict allocation = false", and means that
857 we won't detect out-of-quota errors, for
858 example. */
859
860 case FileEndOfFileInformation:
861 in_uint8s(in, 28); /* unknown */
862 in_uint32_le(in, length); /* file size */
863
864 /* prevents start of writing if not enough space left on device */
865 if (STATFS_FN(pfinfo->path, &stat_fs) == 0)
866 if (stat_fs.f_bfree * stat_fs.f_bsize < length)
867 return RD_STATUS_DISK_FULL;
868
869 if (ftruncate_growable(handle, length) != 0)
870 {
871 return RD_STATUS_DISK_FULL;
872 }
873
874 break;
875 default:
876
877 unimpl("IRP Set File Information class: 0x%x\n", info_class);
878 return RD_STATUS_INVALID_PARAMETER;
879 }
880 return RD_STATUS_SUCCESS;
881}
882
883RD_NTSTATUS
884disk_check_notify(RD_NTHANDLE handle)
885{
886 struct fileinfo *pfinfo;
887 RD_NTSTATUS status = RD_STATUS_PENDING;
888
889 NOTIFY notify;
890
891 pfinfo = &(g_fileinfo[handle]);
892 if (!pfinfo->pdir)
893 return RD_STATUS_INVALID_DEVICE_REQUEST;
894
895
896
897 status = NotifyInfo(handle, pfinfo->info_class, &notify);
898
899 if (status != RD_STATUS_PENDING)
900 return status;
901
902 if (memcmp(&pfinfo->notify, &notify, sizeof(NOTIFY)))
903 {
904 /*printf("disk_check_notify found changed event\n"); */
905 memcpy(&pfinfo->notify, &notify, sizeof(NOTIFY));
906 status = RD_STATUS_NOTIFY_ENUM_DIR;
907 }
908
909 return status;
910
911
912}
913
914RD_NTSTATUS
915disk_create_notify(RD_NTHANDLE handle, uint32 info_class)
916{
917
918 struct fileinfo *pfinfo;
919 RD_NTSTATUS ret = RD_STATUS_PENDING;
920
921 /* printf("start disk_create_notify info_class %X\n", info_class); */
922
923 pfinfo = &(g_fileinfo[handle]);
924 pfinfo->info_class = info_class;
925
926 ret = NotifyInfo(handle, info_class, &pfinfo->notify);
927
928 if (info_class & 0x1000)
929 { /* ???? */
930 if (ret == RD_STATUS_PENDING)
931 return RD_STATUS_SUCCESS;
932 }
933
934 /* printf("disk_create_notify: num_entries %d\n", pfinfo->notify.num_entries); */
935
936
937 return ret;
938
939}
940
941static RD_NTSTATUS
942NotifyInfo(RD_NTHANDLE handle, uint32 info_class, NOTIFY * p)
943{
944 struct fileinfo *pfinfo;
945 struct stat filestat;
946 struct dirent *dp;
947 char *fullname;
948 DIR *dpr;
949
950 pfinfo = &(g_fileinfo[handle]);
951 if (fstat(handle, &filestat) < 0)
952 {
953 perror("NotifyInfo");
954 return RD_STATUS_ACCESS_DENIED;
955 }
956 p->modify_time = filestat.st_mtime;
957 p->status_time = filestat.st_ctime;
958 p->num_entries = 0;
959 p->total_time = 0;
960
961
962 dpr = opendir(pfinfo->path);
963 if (!dpr)
964 {
965 perror("NotifyInfo");
966 return RD_STATUS_ACCESS_DENIED;
967 }
968
969
970 while ((dp = readdir(dpr)))
971 {
972 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
973 continue;
974 p->num_entries++;
975 fullname = (char *) xmalloc(strlen(pfinfo->path) + strlen(dp->d_name) + 2);
976 sprintf(fullname, "%s/%s", pfinfo->path, dp->d_name);
977
978 if (!stat(fullname, &filestat))
979 {
980 p->total_time += (filestat.st_mtime + filestat.st_ctime);
981 }
982
983 xfree(fullname);
984 }
985 closedir(dpr);
986
987 return RD_STATUS_PENDING;
988}
989
990static FsInfoType *
991FsVolumeInfo(char *fpath)
992{
993
994 static FsInfoType info;
995#ifdef USE_SETMNTENT
996 FILE *fdfs;
997 struct mntent *e;
998#endif
999
1000 /* initialize */
1001 memset(&info, 0, sizeof(info));
1002 strcpy(info.label, "RDESKTOP");
1003 strcpy(info.type, "RDPFS");
1004
1005#ifdef USE_SETMNTENT
1006 fdfs = setmntent(MNTENT_PATH, "r");
1007 if (!fdfs)
1008 return &info;
1009
1010 while ((e = getmntent(fdfs)))
1011 {
1012 if (str_startswith(e->mnt_dir, fpath))
1013 {
1014 strcpy(info.type, e->mnt_type);
1015 strcpy(info.name, e->mnt_fsname);
1016 if (strstr(e->mnt_opts, "vfat") || strstr(e->mnt_opts, "iso9660"))
1017 {
1018 int fd = open(e->mnt_fsname, O_RDONLY);
1019 if (fd >= 0)
1020 {
1021 unsigned char buf[512];
1022 memset(buf, 0, sizeof(buf));
1023 if (strstr(e->mnt_opts, "vfat"))
1024 /*FAT*/
1025 {
1026 strcpy(info.type, "vfat");
1027 read(fd, buf, sizeof(buf));
1028 info.serial =
1029 (buf[42] << 24) + (buf[41] << 16) +
1030 (buf[40] << 8) + buf[39];
1031 strncpy(info.label, (char *) buf + 43, 10);
1032 info.label[10] = '\0';
1033 }
1034 else if (lseek(fd, 32767, SEEK_SET) >= 0) /* ISO9660 */
1035 {
1036 read(fd, buf, sizeof(buf));
1037 strncpy(info.label, (char *) buf + 41, 32);
1038 info.label[32] = '\0';
1039 /* info.Serial = (buf[128]<<24)+(buf[127]<<16)+(buf[126]<<8)+buf[125]; */
1040 }
1041 close(fd);
1042 }
1043 }
1044 }
1045 }
1046 endmntent(fdfs);
1047#else
1048 /* initialize */
1049 memset(&info, 0, sizeof(info));
1050 strcpy(info.label, "RDESKTOP");
1051 strcpy(info.type, "RDPFS");
1052
1053#endif
1054 return &info;
1055}
1056
1057
1058RD_NTSTATUS
1059disk_query_volume_information(RD_NTHANDLE handle, uint32 info_class, STREAM out)
1060{
1061 struct STATFS_T stat_fs;
1062 struct fileinfo *pfinfo;
1063 FsInfoType *fsinfo;
1064
1065 pfinfo = &(g_fileinfo[handle]);
1066
1067 if (STATFS_FN(pfinfo->path, &stat_fs) != 0)
1068 {
1069 perror("statfs");
1070 return RD_STATUS_ACCESS_DENIED;
1071 }
1072
1073 fsinfo = FsVolumeInfo(pfinfo->path);
1074
1075 switch (info_class)
1076 {
1077 case FileFsVolumeInformation:
1078
1079 out_uint32_le(out, 0); /* volume creation time low */
1080 out_uint32_le(out, 0); /* volume creation time high */
1081 out_uint32_le(out, fsinfo->serial); /* serial */
1082
1083 out_uint32_le(out, 2 * strlen(fsinfo->label)); /* length of string */
1084
1085 out_uint8(out, 0); /* support objects? */
1086 rdp_out_unistr(out, fsinfo->label, 2 * strlen(fsinfo->label) - 2);
1087 break;
1088
1089 case FileFsSizeInformation:
1090
1091 out_uint32_le(out, stat_fs.f_blocks); /* Total allocation units low */
1092 out_uint32_le(out, 0); /* Total allocation high units */
1093 out_uint32_le(out, stat_fs.f_bfree); /* Available allocation units */
1094 out_uint32_le(out, 0); /* Available allowcation units */
1095 out_uint32_le(out, stat_fs.f_bsize / 0x200); /* Sectors per allocation unit */
1096 out_uint32_le(out, 0x200); /* Bytes per sector */
1097 break;
1098
1099 case FileFsAttributeInformation:
1100
1101 out_uint32_le(out, FS_CASE_SENSITIVE | FS_CASE_IS_PRESERVED); /* fs attributes */
1102 out_uint32_le(out, F_NAMELEN(stat_fs)); /* max length of filename */
1103
1104 out_uint32_le(out, 2 * strlen(fsinfo->type)); /* length of fs_type */
1105 rdp_out_unistr(out, fsinfo->type, 2 * strlen(fsinfo->type) - 2);
1106 break;
1107
1108 case FileFsLabelInformation:
1109 case FileFsDeviceInformation:
1110 case FileFsControlInformation:
1111 case FileFsFullSizeInformation:
1112 case FileFsObjectIdInformation:
1113 case FileFsMaximumInformation:
1114
1115 default:
1116
1117 unimpl("IRP Query Volume Information class: 0x%x\n", info_class);
1118 return RD_STATUS_INVALID_PARAMETER;
1119 }
1120 return RD_STATUS_SUCCESS;
1121}
1122
1123RD_NTSTATUS
1124disk_query_directory(RD_NTHANDLE handle, uint32 info_class, char *pattern, STREAM out)
1125{
1126 uint32 file_attributes, ft_low, ft_high;
1127 char *dirname, fullpath[PATH_MAX];
1128 DIR *pdir;
1129 struct dirent *pdirent;
1130 struct stat filestat;
1131 struct fileinfo *pfinfo;
1132
1133 pfinfo = &(g_fileinfo[handle]);
1134 pdir = pfinfo->pdir;
1135 dirname = pfinfo->path;
1136 file_attributes = 0;
1137
1138 switch (info_class)
1139 {
1140 case FileBothDirectoryInformation:
1141
1142 /* If a search pattern is received, remember this pattern, and restart search */
1143 if (pattern[0] != 0)
1144 {
1145 strncpy(pfinfo->pattern, 1 + strrchr(pattern, '/'), PATH_MAX - 1);
1146 rewinddir(pdir);
1147 }
1148
1149 /* find next dirent matching pattern */
1150 pdirent = readdir(pdir);
1151 while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0)
1152 pdirent = readdir(pdir);
1153
1154 if (pdirent == NULL)
1155 return RD_STATUS_NO_MORE_FILES;
1156
1157 /* Get information for directory entry */
1158 sprintf(fullpath, "%s/%s", dirname, pdirent->d_name);
1159
1160 if (stat(fullpath, &filestat))
1161 {
1162 switch (errno)
1163 {
1164 case ENOENT:
1165 case ELOOP:
1166 case EACCES:
1167 /* These are non-fatal errors. */
1168 memset(&filestat, 0, sizeof(filestat));
1169 break;
1170 default:
1171 /* Fatal error. By returning STATUS_NO_SUCH_FILE,
1172 the directory list operation will be aborted */
1173 perror(fullpath);
1174 out_uint8(out, 0);
1175 return RD_STATUS_NO_SUCH_FILE;
1176 }
1177 }
1178
1179 if (S_ISDIR(filestat.st_mode))
1180 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
1181 if (pdirent->d_name[0] == '.')
1182 file_attributes |= FILE_ATTRIBUTE_HIDDEN;
1183 if (!file_attributes)
1184 file_attributes |= FILE_ATTRIBUTE_NORMAL;
1185 if (!(filestat.st_mode & S_IWUSR))
1186 file_attributes |= FILE_ATTRIBUTE_READONLY;
1187
1188 /* Return requested information */
1189 out_uint8s(out, 8); /* unknown zero */
1190
1191 seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high,
1192 &ft_low);
1193 out_uint32_le(out, ft_low); /* create time */
1194 out_uint32_le(out, ft_high);
1195
1196 seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);
1197 out_uint32_le(out, ft_low); /* last_access_time */
1198 out_uint32_le(out, ft_high);
1199
1200 seconds_since_1970_to_filetime(filestat.st_mtime, &ft_high, &ft_low);
1201 out_uint32_le(out, ft_low); /* last_write_time */
1202 out_uint32_le(out, ft_high);
1203
1204 seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low);
1205 out_uint32_le(out, ft_low); /* change_write_time */
1206 out_uint32_le(out, ft_high);
1207
1208 out_uint32_le(out, filestat.st_size); /* filesize low */
1209 out_uint32_le(out, 0); /* filesize high */
1210 out_uint32_le(out, filestat.st_size); /* filesize low */
1211 out_uint32_le(out, 0); /* filesize high */
1212 out_uint32_le(out, file_attributes);
1213 out_uint8(out, 2 * strlen(pdirent->d_name) + 2); /* unicode length */
1214 out_uint8s(out, 7); /* pad? */
1215 out_uint8(out, 0); /* 8.3 file length */
1216 out_uint8s(out, 2 * 12); /* 8.3 unicode length */
1217 rdp_out_unistr(out, pdirent->d_name, 2 * strlen(pdirent->d_name));
1218 break;
1219
1220 default:
1221 /* FIXME: Support FileDirectoryInformation,
1222 FileFullDirectoryInformation, and
1223 FileNamesInformation */
1224
1225 unimpl("IRP Query Directory sub: 0x%x\n", info_class);
1226 return RD_STATUS_INVALID_PARAMETER;
1227 }
1228
1229 return RD_STATUS_SUCCESS;
1230}
1231
1232
1233
1234static RD_NTSTATUS
1235disk_device_control(RD_NTHANDLE handle, uint32 request, STREAM in, STREAM out)
1236{
1237 if (((request >> 16) != 20) || ((request >> 16) != 9))
1238 return RD_STATUS_INVALID_PARAMETER;
1239
1240 /* extract operation */
1241 request >>= 2;
1242 request &= 0xfff;
1243
1244 printf("DISK IOCTL %d\n", request);
1245
1246 switch (request)
1247 {
1248 case 25: /* ? */
1249 case 42: /* ? */
1250 default:
1251 unimpl("DISK IOCTL %d\n", request);
1252 return RD_STATUS_INVALID_PARAMETER;
1253 }
1254
1255 return RD_STATUS_SUCCESS;
1256}
1257
1258DEVICE_FNS disk_fns = {
1259 disk_create,
1260 disk_close,
1261 disk_read,
1262 disk_write,
1263 disk_device_control /* device_control */
1264};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette