first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[bbaumbach/samba-autobuild/.git] / source3 / smbwrapper / smbw_dir.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    SMB wrapper directory functions
5    Copyright (C) Andrew Tridgell 1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "realcalls.h"
24
25 extern pstring smbw_cwd;
26 extern fstring smbw_prefix;
27
28 static struct smbw_dir *smbw_dirs;
29
30 extern struct bitmap *smbw_file_bmap;
31 extern int DEBUGLEVEL;
32
33 extern int smbw_busy;
34
35 /***************************************************** 
36 map a fd to a smbw_dir structure
37 *******************************************************/
38 struct smbw_dir *smbw_dir(int fd)
39 {
40         struct smbw_dir *dir;
41
42         for (dir=smbw_dirs;dir;dir=dir->next) {
43                 if (dir->fd == fd) return dir;
44         }
45         return NULL;
46 }
47
48 /***************************************************** 
49 check if a DIR* is one of ours
50 *******************************************************/
51 int smbw_dirp(DIR *dirp)
52 {
53         struct smbw_dir *d = (struct smbw_dir *)dirp;
54         struct smbw_dir *dir;
55
56         for (dir=smbw_dirs;dir;dir=dir->next) {
57                 if (dir == d) return 1;
58         }
59         return 0;
60 }
61
62 /***************************************************** 
63 free a smbw_dir structure and all entries
64 *******************************************************/
65 static void free_dir(struct smbw_dir *dir)
66 {
67         if (dir->list) {
68                 free(dir->list);
69         }
70         if (dir->path) free(dir->path);
71         ZERO_STRUCTP(dir);
72         free(dir);
73 }
74
75
76 static struct smbw_dir *cur_dir;
77
78 /***************************************************** 
79 add a entry to a directory listing
80 *******************************************************/
81 static void smbw_dir_add(struct file_info *finfo, const char *mask)
82 {
83         DEBUG(5,("%s\n", finfo->name));
84
85         if (cur_dir->malloced == cur_dir->count) {
86                 cur_dir->list = (struct file_info *)Realloc(cur_dir->list, 
87                                                             sizeof(cur_dir->list[0])*
88                                                             (cur_dir->count+100));
89                 if (!cur_dir->list) {
90                         /* oops */
91                         return;
92                 }
93                 cur_dir->malloced += 100;
94         }
95
96         cur_dir->list[cur_dir->count] = *finfo;
97         cur_dir->count++;
98 }
99
100 /***************************************************** 
101 add a entry to a directory listing
102 *******************************************************/
103 static void smbw_share_add(const char *share, uint32 type, const char *comment)
104 {
105         struct file_info finfo;
106
107         if (strcmp(share,"IPC$") == 0) return;
108
109         ZERO_STRUCT(finfo);
110
111         pstrcpy(finfo.name, share);
112         finfo.mode = aRONLY | aDIR;     
113
114         smbw_dir_add(&finfo, NULL);
115 }
116
117
118 /***************************************************** 
119 add a server to a directory listing
120 *******************************************************/
121 static void smbw_server_add(const char *name, uint32 type, 
122                             const char *comment)
123 {
124         struct file_info finfo;
125
126         ZERO_STRUCT(finfo);
127
128         pstrcpy(finfo.name, name);
129         finfo.mode = aRONLY | aDIR;     
130
131         smbw_dir_add(&finfo, NULL);
132 }
133
134
135 /***************************************************** 
136 add a entry to a directory listing
137 *******************************************************/
138 static void smbw_printjob_add(struct print_job_info *job)
139 {
140         struct file_info finfo;
141
142         ZERO_STRUCT(finfo);
143
144         pstrcpy(finfo.name, job->name);
145         finfo.mode = aRONLY | aDIR;     
146         finfo.mtime = job->t;
147         finfo.atime = job->t;
148         finfo.ctime = job->t;
149         finfo.uid = nametouid(job->user);
150         finfo.mode = aRONLY;
151         finfo.size = job->size;
152
153         smbw_dir_add(&finfo, NULL);
154 }
155
156
157 /***************************************************** 
158 open a directory on the server
159 *******************************************************/
160 int smbw_dir_open(const char *fname)
161 {
162         fstring server, share;
163         pstring path;
164         struct smbw_server *srv=NULL;
165         struct smbw_dir *dir=NULL;
166         pstring mask;
167         int fd;
168         char *s, *p;
169
170         if (!fname) {
171                 errno = EINVAL;
172                 return -1;
173         }
174
175         smbw_init();
176
177         /* work out what server they are after */
178         s = smbw_parse_path(fname, server, share, path);
179
180         DEBUG(4,("dir_open share=%s\n", share));
181
182         /* get a connection to the server */
183         srv = smbw_server(server, share);
184         if (!srv) {
185                 /* smbw_server sets errno */
186                 goto failed;
187         }
188
189         dir = (struct smbw_dir *)malloc(sizeof(*dir));
190         if (!dir) {
191                 errno = ENOMEM;
192                 goto failed;
193         }
194
195         ZERO_STRUCTP(dir);
196
197         cur_dir = dir;
198
199         slprintf(mask, sizeof(mask)-1, "%s\\*", path);
200         all_string_sub(mask,"\\\\","\\",0);
201
202         if ((p=strstr(srv->server_name,"#1D"))) {
203                 DEBUG(4,("doing NetServerEnum\n"));
204                 *p = 0;
205                 smbw_server_add(".",0,"");
206                 smbw_server_add("..",0,"");
207                 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
208                                   smbw_server_add);
209                 *p = '#';
210         } else if (strcmp(srv->cli.dev,"IPC") == 0) {
211                 DEBUG(4,("doing NetShareEnum\n"));
212                 smbw_share_add(".",0,"");
213                 smbw_share_add("..",0,"");
214                 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
215                         errno = smbw_errno(&srv->cli);
216                         goto failed;
217                 }
218         } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
219                 smbw_share_add(".",0,"");
220                 smbw_share_add("..",0,"");
221                 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
222                         errno = smbw_errno(&srv->cli);
223                         goto failed;
224                 }
225         } else {
226                 if (strcmp(path,"\\") == 0) {
227                         smbw_share_add(".",0,"");
228                         smbw_share_add("..",0,"");
229                 }
230                 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR, 
231                              smbw_dir_add) < 0) {
232                         errno = smbw_errno(&srv->cli);
233                         goto failed;
234                 }
235         }
236
237         cur_dir = NULL;
238         
239         fd = open(SMBW_DUMMY, O_WRONLY);
240         if (fd == -1) {
241                 errno = EMFILE;
242                 goto failed;
243         }
244
245         if (bitmap_query(smbw_file_bmap, fd)) {
246                 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
247                 errno = EIO;
248                 goto failed;
249         }
250
251         DLIST_ADD(smbw_dirs, dir);
252         
253         bitmap_set(smbw_file_bmap, fd);
254
255         dir->fd = fd;
256         dir->srv = srv;
257         dir->path = strdup(s);
258
259         DEBUG(4,("  -> %d\n", dir->count));
260
261         return dir->fd;
262
263  failed:
264         if (dir) {
265                 free_dir(dir);
266         }
267
268         return -1;
269 }
270
271 /***************************************************** 
272 a wrapper for fstat() on a directory
273 *******************************************************/
274 int smbw_dir_fstat(int fd, struct stat *st)
275 {
276         struct smbw_dir *dir;
277
278         dir = smbw_dir(fd);
279         if (!dir) {
280                 errno = EBADF;
281                 return -1;
282         }
283
284         ZERO_STRUCTP(st);
285
286         smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
287
288         st->st_dev = dir->srv->dev;
289
290         return 0;
291 }
292
293 /***************************************************** 
294 close a directory handle
295 *******************************************************/
296 int smbw_dir_close(int fd)
297 {
298         struct smbw_dir *dir;
299
300         dir = smbw_dir(fd);
301         if (!dir) {
302                 errno = EBADF;
303                 return -1;
304         }
305
306         bitmap_clear(smbw_file_bmap, dir->fd);
307         close(dir->fd);
308         
309         DLIST_REMOVE(smbw_dirs, dir);
310
311         free_dir(dir);
312
313         return 0;
314 }
315
316 /***************************************************** 
317 a wrapper for getdents()
318 *******************************************************/
319 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
320 {
321         struct smbw_dir *dir;
322         int n=0;
323
324         smbw_busy++;
325
326         dir = smbw_dir(fd);
327         if (!dir) {
328                 errno = EBADF;
329                 smbw_busy--;
330                 return -1;
331         }
332
333         while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
334 #if HAVE_DIRENT_D_OFF
335                 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
336 #endif
337                 dirp->d_reclen = DIRP_SIZE;
338                 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
339                 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
340                 dir->offset++;
341                 count -= dirp->d_reclen;
342 #if HAVE_DIRENT_D_OFF
343                 if (dir->offset == dir->count) {
344                         dirp->d_off = -1;
345                 }
346 #endif
347                 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
348                 n++;
349         }
350
351         smbw_busy--;
352         return n*DIRP_SIZE;
353 }
354
355
356 /***************************************************** 
357 a wrapper for chdir()
358 *******************************************************/
359 int smbw_chdir(const char *name)
360 {
361         struct smbw_server *srv;
362         fstring server, share;
363         pstring path;
364         uint16 mode = aDIR;
365         char *cwd;
366         int len;
367
368         smbw_init();
369
370         len = strlen(smbw_prefix);
371
372         if (smbw_busy) return real_chdir(name);
373
374         smbw_busy++;
375
376         if (!name) {
377                 errno = EINVAL;
378                 goto failed;
379         }
380
381         DEBUG(4,("smbw_chdir(%s)\n", name));
382
383         /* work out what server they are after */
384         cwd = smbw_parse_path(name, server, share, path);
385
386         /* a special case - accept cd to /smb */
387         if (strncmp(cwd, smbw_prefix, len-1) == 0 &&
388             cwd[len-1] == 0) {
389                 goto success1;
390         }
391
392         if (strncmp(cwd,smbw_prefix,strlen(smbw_prefix))) {
393                 if (real_chdir(cwd) == 0) {
394                         goto success2;
395                 }
396                 goto failed;
397         }
398
399         /* get a connection to the server */
400         srv = smbw_server(server, share);
401         if (!srv) {
402                 /* smbw_server sets errno */
403                 goto failed;
404         }
405
406         if (strncmp(srv->cli.dev,"IPC",3) &&
407             strncmp(srv->cli.dev,"LPT",3) &&
408             !smbw_getatr(srv, path, 
409                          &mode, NULL, NULL, NULL, NULL, NULL)) {
410                 errno = smbw_errno(&srv->cli);
411                 goto failed;
412         }
413
414         if (!(mode & aDIR)) {
415                 errno = ENOTDIR;
416                 goto failed;
417         }
418
419  success1:
420         /* we don't want the old directory to be busy */
421         real_chdir("/");
422
423  success2:
424
425         DEBUG(4,("set SMBW_CWD to %s\n", cwd));
426
427         pstrcpy(smbw_cwd, cwd);
428
429         smbw_busy--;
430         return 0;
431
432  failed:
433         smbw_busy--;
434         return -1;
435 }
436
437
438 /***************************************************** 
439 a wrapper for lseek() on directories
440 *******************************************************/
441 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
442 {
443         struct smbw_dir *dir;
444         off_t ret;
445
446         dir = smbw_dir(fd);
447         if (!dir) {
448                 errno = EBADF;
449                 return -1;
450         }
451
452         switch (whence) {
453         case SEEK_SET:
454                 dir->offset = offset/DIRP_SIZE;
455                 break;
456         case SEEK_CUR:
457                 dir->offset += offset/DIRP_SIZE;
458                 break;
459         case SEEK_END:
460                 dir->offset = (dir->count * DIRP_SIZE) + offset;
461                 dir->offset /= DIRP_SIZE;
462                 break;
463         }
464
465         ret = dir->offset * DIRP_SIZE;
466
467         DEBUG(4,("   -> %d\n", (int)ret));
468
469         return ret;
470 }
471
472
473 /***************************************************** 
474 a wrapper for mkdir()
475 *******************************************************/
476 int smbw_mkdir(const char *fname, mode_t mode)
477 {
478         struct smbw_server *srv;
479         fstring server, share;
480         pstring path;
481
482         if (!fname) {
483                 errno = EINVAL;
484                 return -1;
485         }
486
487         smbw_init();
488
489         smbw_busy++;
490
491         /* work out what server they are after */
492         smbw_parse_path(fname, server, share, path);
493
494         /* get a connection to the server */
495         srv = smbw_server(server, share);
496         if (!srv) {
497                 /* smbw_server sets errno */
498                 goto failed;
499         }
500
501         if (!cli_mkdir(&srv->cli, path)) {
502                 errno = smbw_errno(&srv->cli);
503                 goto failed;
504         }
505
506         smbw_busy--;
507         return 0;
508
509  failed:
510         smbw_busy--;
511         return -1;
512 }
513
514 /***************************************************** 
515 a wrapper for rmdir()
516 *******************************************************/
517 int smbw_rmdir(const char *fname)
518 {
519         struct smbw_server *srv;
520         fstring server, share;
521         pstring path;
522
523         if (!fname) {
524                 errno = EINVAL;
525                 return -1;
526         }
527
528         smbw_init();
529
530         smbw_busy++;
531
532         /* work out what server they are after */
533         smbw_parse_path(fname, server, share, path);
534
535         /* get a connection to the server */
536         srv = smbw_server(server, share);
537         if (!srv) {
538                 /* smbw_server sets errno */
539                 goto failed;
540         }
541
542         if (!cli_rmdir(&srv->cli, path)) {
543                 errno = smbw_errno(&srv->cli);
544                 goto failed;
545         }
546
547         smbw_busy--;
548         return 0;
549
550  failed:
551         smbw_busy--;
552         return -1;
553 }
554
555
556 /***************************************************** 
557 a wrapper for getcwd()
558 *******************************************************/
559 char *smbw_getcwd(char *buf, size_t size)
560 {
561         smbw_init();
562
563         if (smbw_busy) {
564                 return (char *)real_getcwd(buf, size);
565         }
566
567         smbw_busy++;
568
569         if (!buf) {
570                 if (size <= 0) size = strlen(smbw_cwd)+1;
571                 buf = (char *)malloc(size);
572                 if (!buf) {
573                         errno = ENOMEM;
574                         smbw_busy--;
575                         return NULL;
576                 }
577         }
578
579         if (strlen(smbw_cwd) > size-1) {
580                 errno = ERANGE;
581                 smbw_busy--;
582                 return NULL;
583         }
584
585         safe_strcpy(buf, smbw_cwd, size);
586
587         smbw_busy--;
588         return buf;
589 }
590
591 /***************************************************** 
592 a wrapper for fchdir()
593 *******************************************************/
594 int smbw_fchdir(unsigned int fd)
595 {
596         struct smbw_dir *dir;
597         int ret;
598
599         smbw_busy++;
600
601         dir = smbw_dir(fd);
602         if (dir) {
603                 smbw_busy--;
604                 return chdir(dir->path);
605         }       
606
607         ret = real_fchdir(fd);
608         if (ret == 0) {
609                 sys_getwd(smbw_cwd);            
610         }
611
612         smbw_busy--;
613         return ret;
614 }
615
616 /***************************************************** 
617 open a directory on the server
618 *******************************************************/
619 DIR *smbw_opendir(const char *fname)
620 {
621         int fd;
622
623         smbw_busy++;
624
625         fd = smbw_dir_open(fname);
626
627         if (fd == -1) {
628                 smbw_busy--;
629                 return NULL;
630         }
631
632         smbw_busy--;
633
634         return (DIR *)smbw_dir(fd);
635 }
636
637 /***************************************************** 
638 read one entry from a directory
639 *******************************************************/
640 struct dirent *smbw_readdir(DIR *dirp)
641 {
642         struct smbw_dir *d = (struct smbw_dir *)dirp;
643         static union {
644                 char buf[DIRP_SIZE];
645                 struct dirent de;
646         } dbuf;
647
648         if (smbw_getdents(d->fd, &dbuf.de, DIRP_SIZE) > 0) 
649                 return &dbuf.de;
650
651         return NULL;
652 }
653
654 /***************************************************** 
655 close a DIR*
656 *******************************************************/
657 int smbw_closedir(DIR *dirp)
658 {
659         struct smbw_dir *d = (struct smbw_dir *)dirp;
660         return smbw_close(d->fd);
661 }
662
663 /***************************************************** 
664 seek in a directory
665 *******************************************************/
666 void smbw_seekdir(DIR *dirp, off_t offset)
667 {
668         struct smbw_dir *d = (struct smbw_dir *)dirp;
669         smbw_dir_lseek(d->fd,offset, SEEK_SET);
670 }
671
672 /***************************************************** 
673 current loc in a directory
674 *******************************************************/
675 off_t smbw_telldir(DIR *dirp)
676 {
677         struct smbw_dir *d = (struct smbw_dir *)dirp;
678         return smbw_dir_lseek(d->fd,0,SEEK_CUR);
679 }
680