/[rdesktop]/sourceforge.net/trunk/rdesktop/disk.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/disk.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 576 - (hide annotations)
Thu Jan 22 20:31:59 2004 UTC (20 years, 4 months ago) by stargo
File MIME type: text/plain
File size: 15909 byte(s)
fix g_null_pointer memleak
fixes for compiles on NetBSD & FreeBSD

1 n-ki 569 /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.
3     Disk Redirection
4     Copyright (C) Jeroen Meijer 2003
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     #define FILE_ATTRIBUTE_READONLY 0x00000001
22     #define FILE_ATTRIBUTE_HIDDEN 0x00000002
23     #define FILE_ATTRIBUTE_SYSTEM 0x00000004
24     #define FILE_ATTRIBUTE_DIRECTORY 0x00000010
25     #define FILE_ATTRIBUTE_ARCHIVE 0x00000020
26     #define FILE_ATTRIBUTE_DEVICE 0x00000040
27     #define FILE_ATTRIBUTE_NORMAL 0x00000080
28     #define FILE_ATTRIBUTE_TEMPORARY 0x00000100
29     #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
30     #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
31     #define FILE_ATTRIBUTE_COMPRESSED 0x00000800
32     #define FILE_ATTRIBUTE_OFFLINE 0x00001000
33     #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
34     #define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
35    
36     #define FILE_BASIC_INFORMATION 0x04
37     #define FILE_STANDARD_INFORMATION 0x05
38    
39     #define FS_CASE_SENSITIVE 0x00000001
40     #define FS_CASE_IS_PRESERVED 0x00000002
41     #define FS_UNICODE_STORED_ON_DISK 0x00000004
42     #define FS_PERSISTENT_ACLS 0x00000008
43     #define FS_FILE_COMPRESSION 0x00000010
44     #define FS_VOLUME_QUOTAS 0x00000020
45     #define FS_SUPPORTS_SPARSE_FILES 0x00000040
46     #define FS_SUPPORTS_REPARSE_POINTS 0x00000080
47     #define FS_SUPPORTS_REMOTE_STORAGE 0X00000100
48     #define FS_VOL_IS_COMPRESSED 0x00008000
49     #define FILE_READ_ONLY_VOLUME 0x00080000
50    
51     #define OPEN_EXISTING 1
52     #define CREATE_NEW 2
53     #define OPEN_ALWAYS 3
54     #define TRUNCATE_EXISTING 4
55     #define CREATE_ALWAYS 5
56    
57     #define GENERIC_READ 0x80000000
58     #define GENERIC_WRITE 0x40000000
59     #define GENERIC_EXECUTE 0x20000000
60     #define GENERIC_ALL 0x10000000
61    
62     #define ERROR_FILE_NOT_FOUND 2L
63     #define ERROR_ALREADY_EXISTS 183L
64    
65     #define MAX_OPEN_FILES 0x100
66    
67 stargo 572 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
68     #define SOLARIS
69     #endif
70    
71     #ifdef SOLARIS
72     #define DIRFD(a) ((a)->dd_fd)
73     #else
74     #define DIRFD(a) (dirfd(a))
75     #endif
76    
77 n-ki 569 #include <sys/types.h>
78     #include <sys/stat.h>
79     #include <unistd.h>
80     #include <fcntl.h> /* open, close */
81     #include <dirent.h> /* opendir, closedir, readdir */
82     #include <fnmatch.h>
83     #include <errno.h> /* errno */
84 stargo 572
85 stargo 573 #if defined(SOLARIS)
86 stargo 572 #include <sys/statvfs.h> /* solaris statvfs */
87 stargo 574 #define STATFS_FN(path, buf) (statvfs(path,buf))
88     #define STATFS_T statvfs
89 stargo 573 #define F_NAMELEN(buf) ((buf).f_namemax)
90    
91 stargo 576 #elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__))
92 stargo 573 #include <sys/param.h>
93     #include <sys/mount.h>
94 stargo 574 #define STATFS_FN(path, buf) (statfs(path,buf))
95     #define STATFS_T statfs
96 stargo 573 #define F_NAMELEN(buf) (NAME_MAX)
97    
98 stargo 572 #else
99     #include <sys/vfs.h> /* linux statfs */
100 stargo 574 #define STATFS_FN(path, buf) (statfs(path,buf))
101     #define STATFS_T statfs
102 stargo 573 #define F_NAMELEN(buf) ((buf).f_namelen)
103 stargo 572 #endif
104    
105 n-ki 569 #include "rdesktop.h"
106    
107     extern RDPDR_DEVICE g_rdpdr_device[];
108    
109     struct fileinfo
110     {
111     uint32 device_id, flags_and_attributes;
112     char path[256];
113     DIR *pdir;
114     struct dirent *pdirent;
115     char pattern[64];
116     BOOL delete_on_close;
117     }
118     g_fileinfo[MAX_OPEN_FILES];
119    
120     /* Convert seconds since 1970 to a filetime */
121     void
122     seconds_since_1970_to_filetime(time_t seconds, uint32 * high, uint32 * low)
123     {
124     unsigned long long ticks;
125    
126     ticks = (seconds + 11644473600LL) * 10000000;
127     *low = (uint32) ticks;
128     *high = (uint32) (ticks >> 32);
129     }
130    
131     /* Enumeration of devices from rdesktop.c */
132     /* returns numer of units found and initialized. */
133     /* optarg looks like ':h:=/mnt/floppy,b:=/mnt/usbdevice1' */
134     /* when it arrives to this function. */
135     int
136     disk_enum_devices(int *id, char *optarg)
137     {
138     char *pos = optarg;
139     char *pos2;
140     int count = 0;
141    
142     // skip the first colon
143     optarg++;
144     while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
145     {
146     pos2 = next_arg(optarg, '=');
147     strcpy(g_rdpdr_device[*id].name, optarg);
148    
149 stargo 570 toupper_str(g_rdpdr_device[*id].name);
150 n-ki 569
151     /* add trailing colon to name. */
152     strcat(g_rdpdr_device[*id].name, ":");
153    
154     g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
155     strcpy(g_rdpdr_device[*id].local_path, pos2);
156     printf("DISK %s to %s\n", g_rdpdr_device[*id].name, g_rdpdr_device[*id].local_path);
157     g_rdpdr_device[*id].device_type = DEVICE_TYPE_DISK;
158     count++;
159     (*id)++;
160    
161     optarg = pos;
162     }
163     return count;
164     }
165    
166     /* Opens of creates a file or directory */
167     NTSTATUS
168     disk_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition,
169     uint32 flags_and_attributes, char *filename, HANDLE * phandle)
170     {
171     HANDLE handle;
172     DIR *dirp;
173     int flags, mode;
174     char path[256];
175    
176     handle = 0;
177     dirp = NULL;
178     flags = 0;
179     mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
180    
181     if (filename[strlen(filename) - 1] == '/')
182     filename[strlen(filename) - 1] = 0;
183     sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename);
184     //printf("Open: %s\n", path);
185    
186     switch (create_disposition)
187     {
188     case CREATE_ALWAYS:
189    
190     // Delete existing file/link.
191     unlink(path);
192     flags |= O_CREAT;
193     break;
194    
195     case CREATE_NEW:
196    
197     // If the file already exists, then fail.
198     flags |= O_CREAT | O_EXCL;
199     break;
200    
201     case OPEN_ALWAYS:
202    
203     // Create if not already exists.
204     flags |= O_CREAT;
205     break;
206    
207     case OPEN_EXISTING:
208    
209     // Default behaviour
210     break;
211    
212     case TRUNCATE_EXISTING:
213    
214     // If the file does not exist, then fail.
215     flags |= O_TRUNC;
216     break;
217     }
218    
219     if (flags_and_attributes & FILE_DIRECTORY_FILE)
220     {
221     if (flags & O_CREAT)
222     {
223     mkdir(path, mode);
224     }
225    
226     dirp = opendir(path);
227     if (!dirp)
228     {
229     switch (errno)
230     {
231     case EACCES:
232    
233     return STATUS_ACCESS_DENIED;
234    
235     case ENOENT:
236    
237     return STATUS_NO_SUCH_FILE;
238    
239     default:
240    
241     perror("opendir");
242     return STATUS_NO_SUCH_FILE;
243     }
244     }
245 stargo 572 handle = DIRFD(dirp);
246 n-ki 569 }
247     else
248     {
249     if (accessmask & GENERIC_ALL
250     || (accessmask & GENERIC_READ && accessmask & GENERIC_WRITE))
251     {
252     flags |= O_RDWR;
253     }
254     else if ((accessmask & GENERIC_WRITE) && !(accessmask & GENERIC_READ))
255     {
256     flags |= O_WRONLY;
257     }
258     else
259     {
260     flags |= O_RDONLY;
261     }
262    
263     handle = open(path, flags, mode);
264     if (handle == -1)
265     {
266     switch (errno)
267     {
268     case EACCES:
269    
270     return STATUS_ACCESS_DENIED;
271    
272     case ENOENT:
273    
274     return STATUS_NO_SUCH_FILE;
275    
276     default:
277    
278     perror("open");
279     return STATUS_NO_SUCH_FILE;
280     }
281     }
282     }
283    
284     if (handle >= MAX_OPEN_FILES)
285     {
286     error("Maximum number of open files (%s) reached. Increase MAX_OPEN_FILES!\n",
287     handle);
288     exit(1);
289     }
290    
291     if (dirp)
292     g_fileinfo[handle].pdir = dirp;
293     g_fileinfo[handle].device_id = device_id;
294     g_fileinfo[handle].flags_and_attributes = flags_and_attributes;
295     strncpy(g_fileinfo[handle].path, path, 255);
296    
297     *phandle = handle;
298     return STATUS_SUCCESS;
299     }
300    
301     NTSTATUS
302     disk_close(HANDLE handle)
303     {
304     struct fileinfo *pfinfo;
305    
306     pfinfo = &(g_fileinfo[handle]);
307    
308     if (pfinfo->flags_and_attributes & FILE_DIRECTORY_FILE)
309     {
310     closedir(pfinfo->pdir);
311     //FIXME: Should check exit code
312     }
313     else
314     {
315     close(handle);
316     }
317    
318     return STATUS_SUCCESS;
319     }
320    
321     NTSTATUS
322     disk_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
323     {
324     int n;
325    
326     if (offset)
327     lseek(handle, offset, SEEK_SET);
328     n = read(handle, data, length);
329    
330     if (n < 0)
331     {
332     perror("read");
333     *result = 0;
334     return STATUS_INVALID_PARAMETER;
335     }
336    
337     *result = n;
338    
339     return STATUS_SUCCESS;
340     }
341    
342     NTSTATUS
343     disk_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
344     {
345     int n;
346    
347     if (offset)
348     lseek(handle, offset, SEEK_SET);
349    
350     n = write(handle, data, length);
351    
352     if (n < 0)
353     {
354     perror("write");
355     *result = 0;
356     return STATUS_ACCESS_DENIED;
357     }
358    
359     *result = n;
360    
361     return STATUS_SUCCESS;
362     }
363    
364     NTSTATUS
365     disk_query_information(HANDLE handle, uint32 info_class, STREAM out)
366     {
367     uint32 file_attributes, ft_high, ft_low;
368     struct stat filestat;
369     char *path, *filename;
370    
371     path = g_fileinfo[handle].path;
372    
373     // Get information about file
374     if (fstat(handle, &filestat) != 0)
375     {
376     perror("stat");
377     out_uint8(out, 0);
378     return STATUS_ACCESS_DENIED;
379     }
380    
381     // Set file attributes
382     file_attributes = 0;
383     if (S_ISDIR(filestat.st_mode))
384     {
385     file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
386     }
387     filename = 1 + strrchr(path, '/');
388     if (filename && filename[0] == '.')
389     {
390     file_attributes |= FILE_ATTRIBUTE_HIDDEN;
391     }
392    
393     // Return requested data
394     switch (info_class)
395     {
396     case 4: /* FileBasicInformation */
397    
398     out_uint8s(out, 8); //create_time not available;
399    
400     seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);
401     out_uint32_le(out, ft_low); //last_access_time
402     out_uint32_le(out, ft_high);
403    
404     seconds_since_1970_to_filetime(filestat.st_mtime, &ft_high, &ft_low);
405     out_uint32_le(out, ft_low); //last_write_time
406     out_uint32_le(out, ft_high);
407    
408     out_uint8s(out, 8); //unknown zero
409     out_uint32_le(out, file_attributes);
410     break;
411    
412     case 5: /* FileStandardInformation */
413    
414     out_uint32_le(out, filestat.st_size); //Allocation size
415     out_uint32_le(out, 0);
416     out_uint32_le(out, filestat.st_size); //End of file
417     out_uint32_le(out, 0);
418     out_uint32_le(out, filestat.st_nlink); //Number of links
419     out_uint8(out, 0); //Delete pending
420     out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); //Directory
421     break;
422    
423     case 35: /* FileObjectIdInformation */
424    
425     out_uint32_le(out, file_attributes); /* File Attributes */
426     out_uint32_le(out, 0); /* Reparse Tag */
427     break;
428    
429     default:
430    
431     unimpl("IRP Query (File) Information class: 0x%x\n", info_class);
432     return STATUS_INVALID_PARAMETER;
433     }
434     return STATUS_SUCCESS;
435     }
436    
437     NTSTATUS
438     disk_set_information(HANDLE handle, uint32 info_class, STREAM in, STREAM out)
439     {
440     uint32 device_id, length, file_attributes, ft_high, ft_low;
441     char newname[256], fullpath[256];
442     struct fileinfo *pfinfo;
443    
444     pfinfo = &(g_fileinfo[handle]);
445    
446     switch (info_class)
447     {
448     case 4: /* FileBasicInformation */
449    
450     // Probably safe to ignore
451     break;
452    
453     case 10: /* FileRenameInformation */
454    
455     in_uint8s(in, 4); /* Handle of root dir? */
456     in_uint8s(in, 0x1a); /* unknown */
457     in_uint32_le(in, length);
458    
459     if (length && (length / 2) < 256)
460     {
461     rdp_in_unistr(in, newname, length);
462     convert_to_unix_filename(newname);
463     }
464     else
465     {
466     return STATUS_INVALID_PARAMETER;
467     }
468    
469     sprintf(fullpath, "%s%s", g_rdpdr_device[pfinfo->device_id].local_path,
470     newname);
471    
472     if (rename(pfinfo->path, fullpath) != 0)
473     {
474     perror("rename");
475     return STATUS_ACCESS_DENIED;
476     }
477     break;
478    
479     case 13: /* FileDispositionInformation */
480    
481     //unimpl("IRP Set File Information class: FileDispositionInformation\n");
482     // in_uint32_le(in, delete_on_close);
483     // disk_close(handle);
484     unlink(pfinfo->path);
485     break;
486    
487     case 19: /* FileAllocationInformation */
488    
489     unimpl("IRP Set File Information class: FileAllocationInformation\n");
490     break;
491    
492     case 20: /* FileEndOfFileInformation */
493    
494     unimpl("IRP Set File Information class: FileEndOfFileInformation\n");
495     break;
496    
497     default:
498    
499     unimpl("IRP Set File Information class: 0x%x\n", info_class);
500     return STATUS_INVALID_PARAMETER;
501     }
502     return STATUS_SUCCESS;
503     }
504    
505     NTSTATUS
506     disk_query_volume_information(HANDLE handle, uint32 info_class, STREAM out)
507     {
508     char *volume, *fs_type;
509 stargo 574 struct STATFS_T stat_fs;
510 n-ki 569 struct fileinfo *pfinfo;
511    
512     pfinfo = &(g_fileinfo[handle]);
513     volume = "RDESKTOP";
514     fs_type = "RDPFS";
515    
516 stargo 574 if (STATFS_FN(pfinfo->path, &stat_fs) != 0) /* FIXME: statfs is not portable */
517 n-ki 569 {
518     perror("statfs");
519     return STATUS_ACCESS_DENIED;
520     }
521    
522     switch (info_class)
523     {
524     case 1: /* FileFsVolumeInformation */
525    
526     out_uint32_le(out, 0); /* volume creation time low */
527     out_uint32_le(out, 0); /* volume creation time high */
528     out_uint32_le(out, 0); /* serial */
529     out_uint32_le(out, 2 * strlen(volume)); /* length of string */
530     out_uint8(out, 0); /* support objects? */
531     rdp_out_unistr(out, volume, 2 * strlen(volume) - 2);
532     break;
533    
534     case 3: /* FileFsSizeInformation */
535    
536     out_uint32_le(out, stat_fs.f_blocks); /* Total allocation units low */
537     out_uint32_le(out, 0); /* Total allocation high units */
538     out_uint32_le(out, stat_fs.f_bfree); /* Available allocation units */
539     out_uint32_le(out, 0); /* Available allowcation units */
540     out_uint32_le(out, stat_fs.f_bsize / 0x200); /* Sectors per allocation unit */
541     out_uint32_le(out, 0x200); /* Bytes per sector */
542     break;
543    
544     case 5: /* FileFsAttributeInformation */
545    
546     out_uint32_le(out, FS_CASE_SENSITIVE | FS_CASE_IS_PRESERVED); /* fs attributes */
547 stargo 574 out_uint32_le(out, F_NAMELEN(stat_fs)); /* max length of filename */
548 n-ki 569 out_uint32_le(out, 2 * strlen(fs_type)); /* length of fs_type */
549     rdp_out_unistr(out, fs_type, 2 * strlen(fs_type) - 2);
550     break;
551    
552     case 2: /* FileFsLabelInformation */
553     case 4: /* FileFsDeviceInformation */
554     case 6: /* FileFsControlInformation */
555     case 7: /* FileFsFullSizeInformation */
556     case 8: /* FileFsObjectIdInformation */
557     case 9: /* FileFsMaximumInformation */
558     default:
559    
560     unimpl("IRP Query Volume Information class: 0x%x\n", info_class);
561     return STATUS_INVALID_PARAMETER;
562     }
563     return STATUS_SUCCESS;
564     }
565    
566     NTSTATUS
567     disk_query_directory(HANDLE handle, uint32 info_class, char *pattern, STREAM out)
568     {
569     uint32 file_attributes, ft_low, ft_high;
570     char *dirname, fullpath[256];
571     DIR *pdir;
572     struct dirent *pdirent;
573     struct stat fstat;
574     struct fileinfo *pfinfo;
575    
576     pfinfo = &(g_fileinfo[handle]);
577     pdir = pfinfo->pdir;
578     dirname = pfinfo->path;
579     file_attributes = 0;
580    
581     switch (info_class)
582     {
583     case 3: //FIXME: Why 3?
584    
585     // If a search pattern is received, remember this pattern, and restart search
586     if (pattern[0] != 0)
587     {
588     strncpy(pfinfo->pattern, 1 + strrchr(pattern, '/'), 64);
589     rewinddir(pdir);
590     }
591    
592     // find next dirent matching pattern
593     pdirent = readdir(pdir);
594     while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0)
595     {
596     pdirent = readdir(pdir);
597     }
598    
599     if (pdirent == NULL)
600     {
601     return STATUS_NO_MORE_FILES;
602     }
603    
604     // Get information for directory entry
605     sprintf(fullpath, "%s/%s", dirname, pdirent->d_name);
606     /* JIF
607     printf("Stat: %s\n", fullpath); */
608     if (stat(fullpath, &fstat))
609     {
610     perror("stat");
611     out_uint8(out, 0);
612     return STATUS_ACCESS_DENIED;
613     }
614    
615     if (S_ISDIR(fstat.st_mode))
616     file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
617     if (pdirent->d_name[0] == '.')
618     file_attributes |= FILE_ATTRIBUTE_HIDDEN;
619    
620     // Return requested information
621     out_uint8s(out, 8); //unknown zero
622     out_uint8s(out, 8); //create_time not available in posix;
623    
624     seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low);
625     out_uint32_le(out, ft_low); //last_access_time
626     out_uint32_le(out, ft_high);
627    
628     seconds_since_1970_to_filetime(fstat.st_mtime, &ft_high, &ft_low);
629     out_uint32_le(out, ft_low); //last_write_time
630     out_uint32_le(out, ft_high);
631    
632     out_uint8s(out, 8); //unknown zero
633     out_uint32_le(out, fstat.st_size); //filesize low
634     out_uint32_le(out, 0); //filesize high
635     out_uint32_le(out, fstat.st_size); //filesize low
636     out_uint32_le(out, 0); //filesize high
637     out_uint32_le(out, file_attributes);
638     out_uint8(out, 2 * strlen(pdirent->d_name) + 2); //unicode length
639     out_uint8s(out, 7); //pad?
640     out_uint8(out, 0); //8.3 file length
641     out_uint8s(out, 2 * 12); //8.3 unicode length
642     rdp_out_unistr(out, pdirent->d_name, 2 * strlen(pdirent->d_name));
643     break;
644    
645     default:
646    
647     unimpl("IRP Query Directory sub: 0x%x\n", info_class);
648     return STATUS_INVALID_PARAMETER;
649     }
650    
651     return STATUS_SUCCESS;
652     }
653    
654     DEVICE_FNS disk_fns = {
655     disk_create,
656     disk_close,
657     disk_read,
658     disk_write,
659     NULL /* device_control */
660     };

  ViewVC Help
Powered by ViewVC 1.1.26