weekend work. user / group database API.
[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 "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   = (uid_t)-1;
150         nametouid(job->user, finfo.uid);
151         finfo.mode = aRONLY;
152         finfo.size = job->size;
153
154         smbw_dir_add(&finfo, NULL);
155 }
156
157
158 /***************************************************** 
159 open a directory on the server
160 *******************************************************/
161 int smbw_dir_open(const char *fname)
162 {
163         fstring server, share;
164         pstring path;
165         struct smbw_server *srv=NULL;
166         struct smbw_dir *dir=NULL;
167         pstring mask;
168         int fd;
169         char *s, *p;
170
171         if (!fname) {
172                 errno = EINVAL;
173                 return -1;
174         }
175
176         smbw_init();
177
178         /* work out what server they are after */
179         s = smbw_parse_path(fname, server, share, path);
180
181         DEBUG(4,("dir_open share=%s\n", share));
182
183         /* get a connection to the server */
184         srv = smbw_server(server, share);
185         if (!srv) {
186                 /* smbw_server sets errno */
187                 goto failed;
188         }
189
190         dir = (struct smbw_dir *)malloc(sizeof(*dir));
191         if (!dir) {
192                 errno = ENOMEM;
193                 goto failed;
194         }
195
196         ZERO_STRUCTP(dir);
197
198         cur_dir = dir;
199
200         slprintf(mask, sizeof(mask)-1, "%s\\*", path);
201         string_sub(mask,"\\\\","\\");
202
203         if ((p=strstr(srv->server_name,"#1D"))) {
204                 DEBUG(4,("doing NetServerEnum\n"));
205                 *p = 0;
206                 smbw_server_add(".",0,"");
207                 smbw_server_add("..",0,"");
208                 cli_NetServerEnum(&srv->cli, srv->server_name, SV_TYPE_ALL,
209                                   smbw_server_add);
210                 *p = '#';
211         } else if (strcmp(srv->cli.dev,"IPC") == 0) {
212                 DEBUG(4,("doing NetShareEnum\n"));
213                 smbw_share_add(".",0,"");
214                 smbw_share_add("..",0,"");
215                 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) < 0) {
216                         errno = smbw_errno(&srv->cli);
217                         goto failed;
218                 }
219         } else if (strncmp(srv->cli.dev,"LPT",3) == 0) {
220                 smbw_share_add(".",0,"");
221                 smbw_share_add("..",0,"");
222                 if (cli_print_queue(&srv->cli, smbw_printjob_add) < 0) {
223                         errno = smbw_errno(&srv->cli);
224                         goto failed;
225                 }
226         } else {
227                 if (strcmp(path,"\\") == 0) {
228                         smbw_share_add(".",0,"");
229                         smbw_share_add("..",0,"");
230                 }
231                 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR, 
232                              smbw_dir_add) < 0) {
233                         errno = smbw_errno(&srv->cli);
234                         goto failed;
235                 }
236         }
237
238         cur_dir = NULL;
239         
240         fd = open(SMBW_DUMMY, O_WRONLY);
241         if (fd == -1) {
242                 errno = EMFILE;
243                 goto failed;
244         }
245
246         if (bitmap_query(smbw_file_bmap, fd)) {
247                 DEBUG(0,("ERROR: fd used in smbw_dir_open\n"));
248                 errno = EIO;
249                 goto failed;
250         }
251
252         DLIST_ADD(smbw_dirs, dir);
253         
254         bitmap_set(smbw_file_bmap, fd);
255
256         dir->fd = fd;
257         dir->srv = srv;
258         dir->path = strdup(s);
259
260         DEBUG(4,("  -> %d\n", dir->count));
261
262         return dir->fd;
263
264  failed:
265         if (dir) {
266                 free_dir(dir);
267         }
268
269         return -1;
270 }
271
272 /***************************************************** 
273 a wrapper for fstat() on a directory
274 *******************************************************/
275 int smbw_dir_fstat(int fd, struct stat *st)
276 {
277         struct smbw_dir *dir;
278
279         dir = smbw_dir(fd);
280         if (!dir) {
281                 errno = EBADF;
282                 return -1;
283         }
284
285         ZERO_STRUCTP(st);
286
287         smbw_setup_stat(st, "", dir->count*DIRP_SIZE, aDIR);
288
289         st->st_dev = dir->srv->dev;
290
291         return 0;
292 }
293
294 /***************************************************** 
295 close a directory handle
296 *******************************************************/
297 int smbw_dir_close(int fd)
298 {
299         struct smbw_dir *dir;
300
301         dir = smbw_dir(fd);
302         if (!dir) {
303                 errno = EBADF;
304                 return -1;
305         }
306
307         bitmap_clear(smbw_file_bmap, dir->fd);
308         close(dir->fd);
309         
310         DLIST_REMOVE(smbw_dirs, dir);
311
312         free_dir(dir);
313
314         return 0;
315 }
316
317 /***************************************************** 
318 a wrapper for getdents()
319 *******************************************************/
320 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
321 {
322         struct smbw_dir *dir;
323         int n=0;
324
325         smbw_busy++;
326
327         dir = smbw_dir(fd);
328         if (!dir) {
329                 errno = EBADF;
330                 smbw_busy--;
331                 return -1;
332         }
333
334         while (count>=DIRP_SIZE && (dir->offset < dir->count)) {
335 #if HAVE_DIRENT_D_OFF
336                 dirp->d_off = (dir->offset+1)*DIRP_SIZE;
337 #endif
338                 dirp->d_reclen = DIRP_SIZE;
339                 fstrcpy(&dirp->d_name[0], dir->list[dir->offset].name);
340                 dirp->d_ino = smbw_inode(dir->list[dir->offset].name);
341                 dir->offset++;
342                 count -= dirp->d_reclen;
343 #if HAVE_DIRENT_D_OFF
344                 if (dir->offset == dir->count) {
345                         dirp->d_off = -1;
346                 }
347 #endif
348                 dirp = (struct dirent *)(((char *)dirp) + DIRP_SIZE);
349                 n++;
350         }
351
352         smbw_busy--;
353         return n*DIRP_SIZE;
354 }
355
356
357 /***************************************************** 
358 a wrapper for chdir()
359 *******************************************************/
360 int smbw_chdir(const char *name)
361 {
362         struct smbw_server *srv;
363         fstring server, share;
364         pstring path;
365         uint16 mode = aDIR;
366         char *cwd;
367         int len;
368
369         smbw_init();
370
371         len = strlen(smbw_prefix);
372
373         if (smbw_busy) return real_chdir(name);
374
375         smbw_busy++;
376
377         if (!name) {
378                 errno = EINVAL;
379                 goto failed;
380         }
381
382         DEBUG(4,("smbw_chdir(%s)\n", name));
383
384         /* work out what server they are after */
385         cwd = smbw_parse_path(name, server, share, path);
386
387         /* a special case - accept cd to /smb */
388         if (strncmp(cwd, smbw_prefix, len-1) == 0 &&
389             cwd[len-1] == 0) {
390                 goto success1;
391         }
392
393         if (strncmp(cwd,smbw_prefix,strlen(smbw_prefix))) {
394                 if (real_chdir(cwd) == 0) {
395                         goto success2;
396                 }
397                 goto failed;
398         }
399
400         /* get a connection to the server */
401         srv = smbw_server(server, share);
402         if (!srv) {
403                 /* smbw_server sets errno */
404                 goto failed;
405         }
406
407         if (strncmp(srv->cli.dev,"IPC",3) &&
408             strncmp(srv->cli.dev,"LPT",3) &&
409             !smbw_getatr(srv, path, 
410                          &mode, NULL, NULL, NULL, NULL, NULL)) {
411                 errno = smbw_errno(&srv->cli);
412                 goto failed;
413         }
414
415         if (!(mode & aDIR)) {
416                 errno = ENOTDIR;
417                 goto failed;
418         }
419
420  success1:
421         /* we don't want the old directory to be busy */
422         real_chdir("/");
423
424  success2:
425
426         DEBUG(4,("set SMBW_CWD to %s\n", cwd));
427
428         pstrcpy(smbw_cwd, cwd);
429
430         smbw_busy--;
431         return 0;
432
433  failed:
434         smbw_busy--;
435         return -1;
436 }
437
438
439 /***************************************************** 
440 a wrapper for lseek() on directories
441 *******************************************************/
442 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
443 {
444         struct smbw_dir *dir;
445         off_t ret;
446
447         dir = smbw_dir(fd);
448         if (!dir) {
449                 errno = EBADF;
450                 return -1;
451         }
452
453         switch (whence) {
454         case SEEK_SET:
455                 dir->offset = offset/DIRP_SIZE;
456                 break;
457         case SEEK_CUR:
458                 dir->offset += offset/DIRP_SIZE;
459                 break;
460         case SEEK_END:
461                 dir->offset = (dir->count * DIRP_SIZE) + offset;
462                 dir->offset /= DIRP_SIZE;
463                 break;
464         }
465
466         ret = dir->offset * DIRP_SIZE;
467
468         DEBUG(4,("   -> %d\n", (int)ret));
469
470         return ret;
471 }
472
473
474 /***************************************************** 
475 a wrapper for mkdir()
476 *******************************************************/
477 int smbw_mkdir(const char *fname, mode_t mode)
478 {
479         struct smbw_server *srv;
480         fstring server, share;
481         pstring path;
482
483         if (!fname) {
484                 errno = EINVAL;
485                 return -1;
486         }
487
488         smbw_init();
489
490         smbw_busy++;
491
492         /* work out what server they are after */
493         smbw_parse_path(fname, server, share, path);
494
495         /* get a connection to the server */
496         srv = smbw_server(server, share);
497         if (!srv) {
498                 /* smbw_server sets errno */
499                 goto failed;
500         }
501
502         if (!cli_mkdir(&srv->cli, path)) {
503                 errno = smbw_errno(&srv->cli);
504                 goto failed;
505         }
506
507         smbw_busy--;
508         return 0;
509
510  failed:
511         smbw_busy--;
512         return -1;
513 }
514
515 /***************************************************** 
516 a wrapper for rmdir()
517 *******************************************************/
518 int smbw_rmdir(const char *fname)
519 {
520         struct smbw_server *srv;
521         fstring server, share;
522         pstring path;
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 (char *)real_getcwd(buf, size);
566         }
567
568         smbw_busy++;
569
570         if (!buf) {
571                 if (size <= 0) size = strlen(smbw_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(smbw_cwd) > size-1) {
581                 errno = ERANGE;
582                 smbw_busy--;
583                 return NULL;
584         }
585
586         safe_strcpy(buf, smbw_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         int ret;
599
600         smbw_busy++;
601
602         dir = smbw_dir(fd);
603         if (dir) {
604                 smbw_busy--;
605                 return chdir(dir->path);
606         }       
607
608         ret = real_fchdir(fd);
609         if (ret == 0) {
610                 sys_getwd(smbw_cwd);            
611         }
612
613         smbw_busy--;
614         return ret;
615 }
616
617 /***************************************************** 
618 open a directory on the server
619 *******************************************************/
620 DIR *smbw_opendir(const char *fname)
621 {
622         int fd;
623
624         smbw_busy++;
625
626         fd = smbw_dir_open(fname);
627
628         if (fd == -1) {
629                 smbw_busy--;
630                 return NULL;
631         }
632
633         smbw_busy--;
634
635         return (DIR *)smbw_dir(fd);
636 }
637
638 /***************************************************** 
639 read one entry from a directory
640 *******************************************************/
641 struct dirent *smbw_readdir(DIR *dirp)
642 {
643         struct smbw_dir *d = (struct smbw_dir *)dirp;
644         static union {
645                 char buf[DIRP_SIZE];
646                 struct dirent de;
647         } dbuf;
648
649         if (smbw_getdents(d->fd, &dbuf.de, DIRP_SIZE) > 0) 
650                 return &dbuf.de;
651
652         return NULL;
653 }
654
655 /***************************************************** 
656 close a DIR*
657 *******************************************************/
658 int smbw_closedir(DIR *dirp)
659 {
660         struct smbw_dir *d = (struct smbw_dir *)dirp;
661         return smbw_close(d->fd);
662 }
663
664 /***************************************************** 
665 seek in a directory
666 *******************************************************/
667 void smbw_seekdir(DIR *dirp, off_t offset)
668 {
669         struct smbw_dir *d = (struct smbw_dir *)dirp;
670         smbw_dir_lseek(d->fd,offset, SEEK_SET);
671 }
672
673 /***************************************************** 
674 current loc in a directory
675 *******************************************************/
676 off_t smbw_telldir(DIR *dirp)
677 {
678         struct smbw_dir *d = (struct smbw_dir *)dirp;
679         return smbw_dir_lseek(d->fd,0,SEEK_CUR);
680 }
681