started basic support for solaris 2.5 in smbwrapper.
[kai/samba.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 "wrapper.h"
24
25 extern pstring smb_cwd;
26
27 static struct smbw_dir *smbw_dirs;
28
29 extern struct bitmap *smbw_file_bmap;
30 extern int DEBUGLEVEL;
31
32 extern int smbw_busy;
33
34 #define DIRP_SIZE (sizeof(fstring) + 12)
35
36
37 /***************************************************** 
38 map a fd to a smbw_dir structure
39 *******************************************************/
40 struct smbw_dir *smbw_dir(int fd)
41 {
42         struct smbw_dir *dir;
43
44         for (dir=smbw_dirs;dir;dir=dir->next) {
45                 if (dir->fd == fd) return dir;
46         }
47         return NULL;
48 }
49
50 /***************************************************** 
51 check if a DIR* is one of ours
52 *******************************************************/
53 int smbw_dirp(DIR *dirp)
54 {
55         struct smbw_dir *d = (struct smbw_dir *)dirp;
56         struct smbw_dir *dir;
57
58         for (dir=smbw_dirs;dir;dir=dir->next) {
59                 if (dir == d) return 1;
60         }
61         return 0;
62 }
63
64 /***************************************************** 
65 free a smbw_dir structure and all entries
66 *******************************************************/
67 static void free_dir(struct smbw_dir *dir)
68 {
69         if (dir->list) {
70                 free(dir->list);
71         }
72         if (dir->path) free(dir->path);
73         ZERO_STRUCTP(dir);
74         free(dir);
75 }
76
77
78 static struct smbw_dir *cur_dir;
79
80 /***************************************************** 
81 add a entry to a directory listing
82 *******************************************************/
83 static void smbw_dir_add(struct file_info *finfo)
84 {
85         DEBUG(5,("%s\n", finfo->name));
86
87         if (cur_dir->malloced == cur_dir->count) {
88                 cur_dir->list = (struct file_info *)Realloc(cur_dir->list, 
89                                                             sizeof(cur_dir->list[0])*
90                                                             (cur_dir->count+100));
91                 if (!cur_dir->list) {
92                         /* oops */
93                         return;
94                 }
95                 cur_dir->malloced += 100;
96         }
97
98         cur_dir->list[cur_dir->count] = *finfo;
99         cur_dir->count++;
100 }
101
102 /***************************************************** 
103 add a entry to a directory listing
104 *******************************************************/
105 static void smbw_share_add(const char *share, uint32 type, const char *comment)
106 {
107         struct file_info finfo;
108
109         ZERO_STRUCT(finfo);
110
111         pstrcpy(finfo.name, share);
112         finfo.mode = aRONLY | aDIR;     
113
114         smbw_dir_add(&finfo);
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);
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);
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         DEBUG(4,("%s\n", __FUNCTION__));
171
172         if (!fname) {
173                 errno = EINVAL;
174                 return -1;
175         }
176
177         smbw_init();
178
179         /* work out what server they are after */
180         s = smbw_parse_path(fname, server, share, path);
181
182         DEBUG(4,("dir_open share=%s\n", share));
183
184         /* get a connection to the server */
185         srv = smbw_server(server, share);
186         if (!srv) {
187                 /* smbw_server sets errno */
188                 goto failed;
189         }
190
191         dir = (struct smbw_dir *)malloc(sizeof(*dir));
192         if (!dir) {
193                 errno = ENOMEM;
194                 goto failed;
195         }
196
197         ZERO_STRUCTP(dir);
198
199         cur_dir = dir;
200
201         slprintf(mask, sizeof(mask)-1, "%s\\*", path);
202         string_sub(mask,"\\\\","\\");
203
204         if ((p=strstr(srv->server_name,"#1D"))) {
205                 DEBUG(4,("doing NetServerEnum\n"));
206                 *p = 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                 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
213                         errno = smbw_errno(&srv->cli);
214                         goto failed;
215                 }
216         } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
217                 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
218                         errno = smbw_errno(&srv->cli);
219                         goto failed;
220                 }
221         } else {
222                 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR, 
223                              smbw_dir_add) < 0) {
224                         errno = smbw_errno(&srv->cli);
225                         goto failed;
226                 }
227         }
228
229         cur_dir = NULL;
230         
231         fd = open(SMBW_DUMMY, O_WRONLY);
232         if (fd == -1) {
233                 errno = EMFILE;
234                 goto failed;
235         }
236
237         if (bitmap_query(smbw_file_bmap, fd)) {
238                 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
239                 errno = EIO;
240                 goto failed;
241         }
242
243         DLIST_ADD(smbw_dirs, dir);
244         
245         bitmap_set(smbw_file_bmap, fd);
246
247         dir->fd = fd;
248         dir->srv = srv;
249         dir->path = strdup(s);
250
251         DEBUG(4,("  -> %d\n", dir->count));
252
253         return dir->fd;
254
255  failed:
256         if (dir) {
257                 free_dir(dir);
258         }
259
260         return -1;
261 }
262
263 /***************************************************** 
264 a wrapper for fstat() on a directory
265 *******************************************************/
266 int smbw_dir_fstat(int fd, struct stat *st)
267 {
268         struct smbw_dir *dir;
269
270         DEBUG(4,("%s\n", __FUNCTION__));
271
272         dir = smbw_dir(fd);
273         if (!dir) {
274                 errno = EBADF;
275                 return -1;
276         }
277
278         ZERO_STRUCTP(st);
279
280         smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
281
282         st->st_dev = dir->srv->dev;
283
284         return 0;
285 }
286
287 /***************************************************** 
288 close a directory handle
289 *******************************************************/
290 int smbw_dir_close(int fd)
291 {
292         struct smbw_dir *dir;
293
294         DEBUG(4,("%s\n", __FUNCTION__));
295
296         dir = smbw_dir(fd);
297         if (!dir) {
298                 DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
299                 errno = EBADF;
300                 return -1;
301         }
302
303         bitmap_clear(smbw_file_bmap, dir->fd);
304         close(dir->fd);
305         
306         DLIST_REMOVE(smbw_dirs, dir);
307
308         free_dir(dir);
309
310         return 0;
311 }
312
313 /***************************************************** 
314 a wrapper for getdents()
315 *******************************************************/
316 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
317 {
318         struct smbw_dir *dir;
319         int n=0;
320
321         DEBUG(4,("%s\n", __FUNCTION__));
322
323         smbw_busy++;
324
325         dir = smbw_dir(fd);
326         if (!dir) {
327                 errno = EBADF;
328                 smbw_busy--;
329                 return -1;
330         }
331
332         DEBUG(4,("sizeof(*dirp)=%d\n", sizeof(*dirp)));
333         
334         while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
335                 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
336                 dirp->d_reclen = DIRP_SIZE;
337                 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
338                 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
339                 dir->offset++;
340                 count -= dirp->d_reclen;
341                 if (dir->offset == dir->count) {
342                         dirp->d_off = -1;
343                 }
344                 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
345                 n++;
346         }
347
348         smbw_busy--;
349         return n*DIRP_SIZE;
350 }
351
352
353 /***************************************************** 
354 a wrapper for chdir()
355 *******************************************************/
356 int smbw_chdir(const char *name)
357 {
358         struct smbw_server *srv;
359         fstring server, share;
360         pstring path;
361         uint32 mode = aDIR;
362         char *cwd;
363
364         smbw_init();
365
366         if (smbw_busy) return real_chdir(cwd);
367
368         smbw_busy++;
369
370         if (!name) {
371                 errno = EINVAL;
372                 goto failed;
373         }
374
375         DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
376
377         /* work out what server they are after */
378         cwd = smbw_parse_path(name, server, share, path);
379
380         if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
381                 if (real_chdir(cwd) == 0) {
382                         DEBUG(4,("set SMBW_CWD to %s\n", cwd));
383                         pstrcpy(smb_cwd, cwd);
384                         if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
385                                 DEBUG(4,("setenv failed\n"));
386                         }
387                         goto success;
388                 }
389                 errno = ENOENT;
390                 goto failed;
391         }
392
393         /* get a connection to the server */
394         srv = smbw_server(server, share);
395         if (!srv) {
396                 /* smbw_server sets errno */
397                 goto failed;
398         }
399
400         if (strncmp(srv->cli.dev,"IPC",3) &&
401             strncmp(srv->cli.dev,"LPT",3) &&
402             !smbw_getatr(srv, path, 
403                          &mode, NULL, NULL, NULL, NULL)) {
404                 errno = smbw_errno(&srv->cli);
405                 goto failed;
406         }
407
408         if (!(mode & aDIR)) {
409                 errno = ENOTDIR;
410                 goto failed;
411         }
412
413         DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
414         pstrcpy(smb_cwd, cwd);
415         if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
416                 DEBUG(4,("setenv failed\n"));
417         }
418
419         /* we don't want the old directory to be busy */
420         real_chdir("/");
421
422  success:
423         smbw_busy--;
424         return 0;
425
426  failed:
427         smbw_busy--;
428         return -1;
429 }
430
431
432 /***************************************************** 
433 a wrapper for lseek() on directories
434 *******************************************************/
435 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
436 {
437         struct smbw_dir *dir;
438         off_t ret;
439
440         DEBUG(4,("%s offset=%d whence=%d\n", __FUNCTION__, 
441                  (int)offset, whence));
442
443         dir = smbw_dir(fd);
444         if (!dir) {
445                 errno = EBADF;
446                 return -1;
447         }
448
449         switch (whence) {
450         case SEEK_SET:
451                 dir->offset = offset/DIRP_SIZE;
452                 break;
453         case SEEK_CUR:
454                 dir->offset += offset/DIRP_SIZE;
455                 break;
456         case SEEK_END:
457                 dir->offset = (dir->count * DIRP_SIZE) + offset;
458                 dir->offset /= DIRP_SIZE;
459                 break;
460         }
461
462         ret = dir->offset * DIRP_SIZE;
463
464         DEBUG(4,("   -> %d\n", (int)ret));
465
466         return ret;
467 }
468
469
470 /***************************************************** 
471 a wrapper for mkdir()
472 *******************************************************/
473 int smbw_mkdir(const char *fname, mode_t mode)
474 {
475         struct smbw_server *srv;
476         fstring server, share;
477         pstring path;
478
479         DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
480
481         if (!fname) {
482                 errno = EINVAL;
483                 return -1;
484         }
485
486         smbw_init();
487
488         smbw_busy++;
489
490         /* work out what server they are after */
491         smbw_parse_path(fname, server, share, path);
492
493         /* get a connection to the server */
494         srv = smbw_server(server, share);
495         if (!srv) {
496                 /* smbw_server sets errno */
497                 goto failed;
498         }
499
500         if (!cli_mkdir(&srv->cli, path)) {
501                 errno = smbw_errno(&srv->cli);
502                 goto failed;
503         }
504
505         smbw_busy--;
506         return 0;
507
508  failed:
509         smbw_busy--;
510         return -1;
511 }
512
513 /***************************************************** 
514 a wrapper for rmdir()
515 *******************************************************/
516 int smbw_rmdir(const char *fname)
517 {
518         struct smbw_server *srv;
519         fstring server, share;
520         pstring path;
521
522         DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
523
524         if (!fname) {
525                 errno = EINVAL;
526                 return -1;
527         }
528
529         smbw_init();
530
531         smbw_busy++;
532
533         /* work out what server they are after */
534         smbw_parse_path(fname, server, share, path);
535
536         /* get a connection to the server */
537         srv = smbw_server(server, share);
538         if (!srv) {
539                 /* smbw_server sets errno */
540                 goto failed;
541         }
542
543         if (!cli_rmdir(&srv->cli, path)) {
544                 errno = smbw_errno(&srv->cli);
545                 goto failed;
546         }
547
548         smbw_busy--;
549         return 0;
550
551  failed:
552         smbw_busy--;
553         return -1;
554 }
555
556
557 /***************************************************** 
558 a wrapper for getcwd()
559 *******************************************************/
560 char *smbw_getcwd(char *buf, size_t size)
561 {
562         smbw_init();
563
564         if (smbw_busy) {
565                 return real_getcwd(buf, size);
566         }
567
568         smbw_busy++;
569
570         if (!buf) {
571                 if (size <= 0) size = strlen(smb_cwd)+1;
572                 buf = (char *)malloc(size);
573                 if (!buf) {
574                         errno = ENOMEM;
575                         smbw_busy--;
576                         return NULL;
577                 }
578         }
579
580         if (strlen(smb_cwd) > size-1) {
581                 errno = ERANGE;
582                 smbw_busy--;
583                 return NULL;
584         }
585
586         safe_strcpy(buf, smb_cwd, size);
587
588         smbw_busy--;
589         return buf;
590 }
591
592 /***************************************************** 
593 a wrapper for fchdir()
594 *******************************************************/
595 int smbw_fchdir(unsigned int fd)
596 {
597         struct smbw_dir *dir;
598
599         DEBUG(4,("%s\n", __FUNCTION__));
600
601         smbw_busy++;
602
603         dir = smbw_dir(fd);
604         if (!dir) {
605                 errno = EBADF;
606                 smbw_busy--;
607                 return -1;
608         }       
609
610         smbw_busy--;
611         
612         return chdir(dir->path);
613 }
614
615 /***************************************************** 
616 open a directory on the server
617 *******************************************************/
618 DIR *smbw_opendir(const char *fname)
619 {
620         int fd;
621
622         smbw_busy++;
623
624         fd = smbw_dir_open(fname);
625
626         if (fd == -1) {
627                 smbw_busy--;
628                 return NULL;
629         }
630
631         smbw_busy--;
632
633         return (DIR *)smbw_dir(fd);
634 }
635
636 /***************************************************** 
637 read one entry from a directory
638 *******************************************************/
639 struct dirent *smbw_readdir(DIR *dirp)
640 {
641         struct smbw_dir *d = (struct smbw_dir *)dirp;
642         static char buf[DIRP_SIZE];
643         struct dirent *de = (struct dirent *)buf;
644
645         if (smbw_getdents(d->fd, de, DIRP_SIZE) > 0) 
646                 return de;
647
648         return NULL;
649 }
650
651 /***************************************************** 
652 close a DIR*
653 *******************************************************/
654 int smbw_closedir(DIR *dirp)
655 {
656         struct smbw_dir *d = (struct smbw_dir *)dirp;
657         return smbw_close(d->fd);
658 }
659
660 /***************************************************** 
661 seek in a directory
662 *******************************************************/
663 void smbw_seekdir(DIR *dirp, off_t offset)
664 {
665         struct smbw_dir *d = (struct smbw_dir *)dirp;
666         smbw_dir_lseek(d->fd,offset, SEEK_SET);
667 }
668
669 /***************************************************** 
670 current loc in a directory
671 *******************************************************/
672 off_t smbw_telldir(DIR *dirp)
673 {
674         struct smbw_dir *d = (struct smbw_dir *)dirp;
675         return smbw_dir_lseek(d->fd,0,SEEK_CUR);
676 }