added lseek() support for directories
[Samba/gbeck.git] / source / smbwrapper / smbw.c
blobd77f33c97e1e58b7e2543fe1d6bd9bede6f993a2
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB wrapper functions
5 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
22 #include "includes.h"
23 #include "smbw.h"
24 #include "wrapper.h"
26 static pstring smb_cwd;
28 struct smbw_server {
29 struct smbw_server *next, *prev;
30 struct cli_state cli;
31 char *server_name;
32 char *share_name;
35 struct smbw_file {
36 struct smbw_file *next, *prev;
37 int cli_fd, fd;
38 char *fname;
39 off_t offset;
40 struct smbw_server *srv;
43 struct smbw_dir {
44 struct smbw_dir *next, *prev;
45 int fd;
46 int offset, count, malloced;
47 struct smbw_server *srv;
48 struct file_info *list;
51 static struct smbw_file *smbw_files;
52 static struct smbw_dir *smbw_dirs;
53 static struct smbw_server *smbw_srvs;
55 static struct bitmap *file_bmap;
56 static pstring local_machine;
57 extern int DEBUGLEVEL;
59 static int smbw_busy;
61 /*****************************************************
62 initialise structures
63 *******************************************************/
64 void smbw_init(void)
66 extern BOOL in_client;
67 static int initialised;
68 static pstring servicesf = CONFIGFILE;
69 extern FILE *dbf;
70 char *p;
72 if (initialised) return;
73 initialised = 1;
75 smbw_busy++;
77 DEBUGLEVEL = 0;
78 setup_logging("smbw",True);
80 dbf = stderr;
82 if ((p=getenv("SMBW_LOGFILE"))) {
83 dbf = fopen(p, "a");
86 file_bmap = bitmap_allocate(SMBW_MAX_OPEN);
87 if (!file_bmap) {
88 exit(1);
91 charset_initialise();
93 in_client = True;
95 if (!lp_load(servicesf,True,False,False)) {
96 exit(1);
99 get_myname(local_machine,NULL);
101 if ((p=getenv("SMBW_DEBUG"))) {
102 DEBUGLEVEL = atoi(p);
105 if ((p=getenv(SMBW_PWD_ENV))) {
106 pstrcpy(smb_cwd, p);
107 DEBUG(4,("Initial cwd from smb_cwd is %s\n", smb_cwd));
108 } else {
109 sys_getwd(smb_cwd);
110 DEBUG(4,("Initial cwd from getwd is %s\n", smb_cwd));
112 smbw_busy--;
115 /*****************************************************
116 determine if a file descriptor is a smb one
117 *******************************************************/
118 BOOL smbw_fd(int fd)
120 if (smbw_busy) return False;
121 return (fd >= SMBW_FD_OFFSET);
125 /*****************************************************
126 remove redundent stuff from a filename
127 *******************************************************/
128 void clean_fname(char *name)
130 char *p, *p2;
131 int l;
132 int modified = 1;
134 if (!name) return;
136 while (modified) {
137 modified = 0;
139 DEBUG(5,("cleaning %s\n", name));
141 if ((p=strstr(name,"/./"))) {
142 modified = 1;
143 while (*p) {
144 p[0] = p[2];
145 p++;
149 if ((p=strstr(name,"//"))) {
150 modified = 1;
151 while (*p) {
152 p[0] = p[1];
153 p++;
157 if (strcmp(name,"/../")==0) {
158 modified = 1;
159 name[1] = 0;
162 if ((p=strstr(name,"/../"))) {
163 modified = 1;
164 for (p2=(p>name?p-1:p);p2>name;p2--) {
165 if (p2[0] == '/') break;
167 while (*p2) {
168 p2[0] = p2[3];
169 p2++;
173 if (strcmp(name,"/..")==0) {
174 modified = 1;
175 name[1] = 0;
178 l = strlen(name);
179 p = l>=3?(name+l-3):name;
180 if (strcmp(p,"/..")==0) {
181 modified = 1;
182 for (p2=p-1;p2>name;p2--) {
183 if (p2[0] == '/') break;
185 if (p2==name) {
186 p[0] = '/';
187 p[1] = 0;
188 } else {
189 p2[0] = 0;
193 l = strlen(name);
194 p = l>=2?(name+l-2):name;
195 if (strcmp(p,"/.")==0) {
196 if (p == name) {
197 p[1] = 0;
198 } else {
199 p[0] = 0;
203 if (strncmp(p=name,"./",2) == 0) {
204 modified = 1;
205 do {
206 p[0] = p[2];
207 } while (*p++);
210 l = strlen(p=name);
211 if (l > 1 && p[l-1] == '/') {
212 modified = 1;
213 p[l-1] = 0;
219 /*****************************************************
220 parse a smb path into its components.
221 *******************************************************/
222 char *smbw_parse_path(const char *fname, char *server, char *share, char *path)
224 static pstring s;
225 char *p, *p2;
226 int len;
228 *server = *share = *path = 0;
230 if (fname[0] == '/') {
231 pstrcpy(s, fname);
232 } else {
233 slprintf(s,sizeof(s)-1, "%s/%s", smb_cwd, fname);
235 clean_fname(s);
237 DEBUG(5,("cleaned %s (fname=%s cwd=%s)\n",
238 s, fname, smb_cwd));
240 if (strncmp(s,SMBW_PREFIX,strlen(SMBW_PREFIX))) return s;
242 p = s + strlen(SMBW_PREFIX);
243 p2 = strchr(p,'/');
245 if (p2) {
246 len = (int)(p2-p);
247 } else {
248 len = strlen(p);
251 len = MIN(len,sizeof(fstring)-1);
253 strncpy(server, p, len);
254 server[len] = 0;
256 p = p2;
257 if (!p) {
258 fstrcpy(share,"IPC$");
259 pstrcpy(path,"");
260 goto ok;
263 p++;
264 p2 = strchr(p,'/');
266 if (p2) {
267 len = (int)(p2-p);
268 } else {
269 len = strlen(p);
272 len = MIN(len,sizeof(fstring)-1);
274 strncpy(share, p, len);
275 share[len] = 0;
277 p = p2;
278 if (!p) {
279 pstrcpy(path,"\\");
280 goto ok;
283 pstrcpy(path,p);
285 string_sub(path, "/", "\\");
288 DEBUG(5,("parsed path name=%s cwd=%s [%s] [%s] [%s]\n",
289 fname, smb_cwd,
290 server, share, path));
292 return s;
295 /*****************************************************
296 determine if a path name (possibly relative) is in the
297 smb name space
298 *******************************************************/
299 BOOL smbw_path(const char *path)
301 fstring server, share;
302 pstring s;
303 char *cwd;
304 int l=strlen(SMBW_PREFIX)-1;
306 if (path[0] == '/' && strncmp(path,SMBW_PREFIX,l)) {
307 return False;
310 if (smbw_busy) return False;
312 smbw_init();
314 DEBUG(3,("smbw_path(%s)\n", path));
316 cwd = smbw_parse_path(path, server, share, s);
318 if (strncmp(cwd,SMBW_PREFIX,l) == 0 &&
319 (cwd[l] == '/' || cwd[l] == 0)) {
320 return True;
323 return False;
326 /*****************************************************
327 return a unix errno from a SMB error pair
328 *******************************************************/
329 int smbw_errno(struct cli_state *c)
331 uint8 eclass;
332 uint32 ecode;
333 int ret;
335 ret = cli_error(c, &eclass, &ecode);
337 if (ret) {
338 DEBUG(3,("smbw_error %d %d (0x%x)\n",
339 (int)eclass, (int)ecode, (int)ecode));
341 return ret;
344 /*****************************************************
345 return a connection to a server (existing or new)
346 *******************************************************/
347 struct smbw_server *smbw_server(char *server, char *share)
349 struct smbw_server *srv=NULL;
350 static struct cli_state c;
351 char *username;
352 char *password;
353 char *workgroup;
354 struct nmb_name called, calling;
356 username = getenv("SMBW_USER");
357 if (!username) username = getenv("USER");
358 if (!username) username = "guest";
360 workgroup = getenv("SMBW_WORKGROUP");
361 if (!workgroup) workgroup = lp_workgroup();
363 password = getenv("SMBW_PASSWORD");
364 if (!password) password = "";
366 /* try to use an existing connection */
367 for (srv=smbw_srvs;srv;srv=srv->next) {
368 if (strcmp(server,srv->server_name)==0 &&
369 strcmp(share,srv->share_name)==0) return srv;
372 if (server[0] == 0) {
373 errno = EPERM;
374 return NULL;
377 /* have to open a new connection */
378 if (!cli_initialise(&c) || !cli_connect(&c, server, NULL)) {
379 errno = ENOENT;
380 return NULL;
383 make_nmb_name(&calling, local_machine, 0x0, "");
384 make_nmb_name(&called , server, 0x20, "");
386 if (!cli_session_request(&c, &calling, &called)) {
387 cli_shutdown(&c);
388 errno = ENOENT;
389 return NULL;
392 if (!cli_negprot(&c)) {
393 cli_shutdown(&c);
394 errno = ENOENT;
395 return NULL;
398 if (!cli_session_setup(&c, username,
399 password, strlen(password),
400 password, strlen(password),
401 workgroup)) {
402 cli_shutdown(&c);
403 errno = EPERM;
404 return NULL;
407 if (!cli_send_tconX(&c, share,
408 strstr(share,"IPC$")?"IPC":"A:",
409 password, strlen(password)+1)) {
410 errno = smbw_errno(&c);
411 cli_shutdown(&c);
412 return NULL;
415 srv = (struct smbw_server *)malloc(sizeof(*srv));
416 if (!srv) {
417 errno = ENOMEM;
418 goto failed;
421 ZERO_STRUCTP(srv);
423 srv->cli = c;
425 srv->server_name = strdup(server);
426 if (!srv->server_name) {
427 errno = ENOMEM;
428 goto failed;
431 srv->share_name = strdup(share);
432 if (!srv->share_name) {
433 errno = ENOMEM;
434 goto failed;
437 /* some programs play with file descriptors fairly intimately. We
438 try to get out of the way by duping to a high fd number */
439 if (fcntl(SMBW_CLI_FD + srv->cli.fd, F_GETFD) && errno == EBADF) {
440 if (dup2(srv->cli.fd,SMBW_CLI_FD+srv->cli.fd) ==
441 srv->cli.fd+SMBW_CLI_FD) {
442 close(srv->cli.fd);
443 srv->cli.fd += SMBW_CLI_FD;
447 DLIST_ADD(smbw_srvs, srv);
449 return srv;
451 failed:
452 cli_shutdown(&c);
453 if (!srv) return NULL;
455 if (srv->server_name) free(srv->server_name);
456 if (srv->share_name) free(srv->share_name);
457 free(srv);
458 return NULL;
462 /*****************************************************
463 map a fd to a smbw_file structure
464 *******************************************************/
465 struct smbw_file *smbw_file(int fd)
467 struct smbw_file *file;
469 for (file=smbw_files;file;file=file->next) {
470 if (file->fd == fd) return file;
472 return NULL;
475 /*****************************************************
476 map a fd to a smbw_dir structure
477 *******************************************************/
478 struct smbw_dir *smbw_dir(int fd)
480 struct smbw_dir *dir;
482 for (dir=smbw_dirs;dir;dir=dir->next) {
483 if (dir->fd == fd) return dir;
485 return NULL;
488 /*****************************************************
489 setup basic info in a stat structure
490 *******************************************************/
491 void smbw_setup_stat(struct stat *st, char *fname, size_t size, int mode)
493 ZERO_STRUCTP(st);
495 if (IS_DOS_DIR(mode)) {
496 st->st_mode = SMBW_DIR_MODE;
497 } else {
498 st->st_mode = SMBW_FILE_MODE;
501 st->st_size = size;
502 st->st_blksize = 512;
503 st->st_blocks = (size+511)/512;
504 st->st_uid = getuid();
505 st->st_gid = getgid();
509 /*****************************************************
510 try to do a QPATHINFO and if that fails then do a getatr
511 this is needed because win95 sometimes refuses the qpathinfo
512 *******************************************************/
513 static BOOL smbw_getatr(struct smbw_server *srv, char *path,
514 uint32 *mode, size_t *size,
515 time_t *c_time, time_t *a_time, time_t *m_time)
517 if (cli_qpathinfo(&srv->cli, path, c_time, a_time, m_time,
518 size, mode)) return True;
520 /* if this is NT then don't bother with the getatr */
521 if (srv->cli.capabilities & CAP_NT_SMBS) return False;
523 if (cli_getatr(&srv->cli, path, mode, size, m_time)) {
524 a_time = c_time = m_time;
525 return True;
527 return False;
530 /*****************************************************
531 free a smbw_dir structure and all entries
532 *******************************************************/
533 static void free_dir(struct smbw_dir *dir)
535 if (dir->list) {
536 free(dir->list);
538 ZERO_STRUCTP(dir);
539 free(dir);
543 static struct smbw_dir *cur_dir;
545 /*****************************************************
546 add a entry to a directory listing
547 *******************************************************/
548 void smbw_dir_add(struct file_info *finfo)
550 DEBUG(5,("%s\n", finfo->name));
552 if (cur_dir->malloced == cur_dir->count) {
553 cur_dir->list = (struct file_info *)Realloc(cur_dir->list,
554 sizeof(cur_dir->list[0])*
555 (cur_dir->count+100));
556 if (!cur_dir->list) {
557 /* oops */
558 return;
560 cur_dir->malloced += 100;
563 cur_dir->list[cur_dir->count] = *finfo;
564 cur_dir->count++;
567 /*****************************************************
568 add a entry to a directory listing
569 *******************************************************/
570 void smbw_share_add(const char *share, uint32 type, const char *comment)
572 struct file_info finfo;
574 ZERO_STRUCT(finfo);
576 pstrcpy(finfo.name, share);
577 finfo.mode = aRONLY | aDIR;
579 smbw_dir_add(&finfo);
583 /*****************************************************
584 open a directory on the server
585 *******************************************************/
586 int smbw_dir_open(const char *fname, int flags)
588 fstring server, share;
589 pstring path;
590 struct smbw_server *srv=NULL;
591 struct smbw_dir *dir=NULL;
592 pstring mask;
593 int fd;
595 DEBUG(4,("%s\n", __FUNCTION__));
597 if (!fname) {
598 errno = EINVAL;
599 return -1;
602 smbw_init();
604 /* work out what server they are after */
605 smbw_parse_path(fname, server, share, path);
607 DEBUG(4,("dir_open share=%s\n", share));
609 /* get a connection to the server */
610 srv = smbw_server(server, share);
611 if (!srv) {
612 /* smbw_server sets errno */
613 goto failed;
616 dir = (struct smbw_dir *)malloc(sizeof(*dir));
617 if (!dir) {
618 errno = ENOMEM;
619 goto failed;
622 ZERO_STRUCTP(dir);
624 cur_dir = dir;
626 slprintf(mask, sizeof(mask)-1, "%s\\*", path);
627 string_sub(mask,"\\\\","\\");
629 if (strcmp(share,"IPC$") == 0) {
630 DEBUG(4,("doing NetShareEnum\n"));
631 if (cli_RNetShareEnum(&srv->cli, smbw_share_add) <= 0) {
632 errno = smbw_errno(&srv->cli);
633 goto failed;
635 } else {
636 if (cli_list(&srv->cli, mask, aHIDDEN|aSYSTEM|aDIR,
637 smbw_dir_add) <= 0) {
638 errno = smbw_errno(&srv->cli);
639 goto failed;
643 cur_dir = NULL;
645 fd = bitmap_find(file_bmap, 0);
646 if (fd == -1) {
647 errno = EMFILE;
648 goto failed;
651 DLIST_ADD(smbw_dirs, dir);
653 bitmap_set(file_bmap, fd);
655 dir->fd = fd + SMBW_FD_OFFSET;
657 DEBUG(4,(" -> %d\n", dir->count));
659 return dir->fd;
661 failed:
662 if (dir) {
663 free_dir(dir);
666 return -1;
670 /*****************************************************
671 a wrapper for open()
672 *******************************************************/
673 int smbw_open(const char *fname, int flags, mode_t mode)
675 fstring server, share;
676 pstring path;
677 struct smbw_server *srv=NULL;
678 int eno, fd = -1;
679 struct smbw_file *file=NULL;
681 DEBUG(4,("%s\n", __FUNCTION__));
683 smbw_init();
685 if (!fname) {
686 errno = EINVAL;
687 return -1;
690 smbw_busy++;
692 /* work out what server they are after */
693 smbw_parse_path(fname, server, share, path);
695 /* get a connection to the server */
696 srv = smbw_server(server, share);
697 if (!srv) {
698 /* smbw_server sets errno */
699 goto failed;
702 if (path[strlen(path)-1] == '\\') {
703 fd = -1;
704 } else {
705 fd = cli_open(&srv->cli, path, flags, DENY_NONE);
707 if (fd == -1) {
708 /* it might be a directory. Maybe we should use chkpath? */
709 fd = smbw_dir_open(fname, flags);
710 smbw_busy--;
711 return fd;
713 if (fd == -1) {
714 errno = eno;
715 goto failed;
718 file = (struct smbw_file *)malloc(sizeof(*file));
719 if (!file) {
720 errno = ENOMEM;
721 goto failed;
724 ZERO_STRUCTP(file);
726 file->cli_fd = fd;
727 file->fname = strdup(path);
728 if (!file->fname) {
729 errno = ENOMEM;
730 goto failed;
732 file->srv = srv;
733 file->fd = bitmap_find(file_bmap, 0);
735 if (file->fd == -1) {
736 errno = EMFILE;
737 goto failed;
740 bitmap_set(file_bmap, file->fd);
742 file->fd += SMBW_FD_OFFSET;
744 DLIST_ADD(smbw_files, file);
746 DEBUG(4,("opened %s\n", fname));
748 smbw_busy--;
749 return file->fd;
751 failed:
752 if (fd != -1) {
753 cli_close(&srv->cli, fd);
755 if (file) {
756 if (file->fname) {
757 free(file->fname);
759 free(file);
761 smbw_busy--;
762 return -1;
766 /*****************************************************
767 a wrapper for fstat() on a directory
768 *******************************************************/
769 int smbw_dir_fstat(int fd, struct stat *st)
771 struct smbw_dir *dir;
773 DEBUG(4,("%s\n", __FUNCTION__));
775 dir = smbw_dir(fd);
776 if (!dir) {
777 errno = EBADF;
778 return -1;
781 ZERO_STRUCTP(st);
783 smbw_setup_stat(st, "", dir->count*sizeof(struct dirent), aDIR);
785 return 0;
788 /*****************************************************
789 a wrapper for fstat()
790 *******************************************************/
791 int smbw_fstat(int fd, struct stat *st)
793 struct smbw_file *file;
794 time_t c_time, a_time, m_time;
795 uint32 size;
796 int mode;
798 DEBUG(4,("%s\n", __FUNCTION__));
800 smbw_busy++;
802 file = smbw_file(fd);
803 if (!file) {
804 int ret = smbw_dir_fstat(fd, st);
805 smbw_busy--;
806 return ret;
809 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
810 &mode, &size, &c_time, &a_time, &m_time) &&
811 !cli_getattrE(&file->srv->cli, file->cli_fd,
812 &mode, &size, &c_time, &a_time, &m_time)) {
813 errno = EINVAL;
814 smbw_busy--;
815 return -1;
818 smbw_setup_stat(st, file->fname, size, mode);
820 st->st_atime = a_time;
821 st->st_ctime = c_time;
822 st->st_mtime = m_time;
824 DEBUG(4,("%s - OK\n", __FUNCTION__));
826 smbw_busy--;
827 return 0;
831 /*****************************************************
832 a wrapper for stat()
833 *******************************************************/
834 int smbw_stat(const char *fname, struct stat *st)
836 struct smbw_server *srv;
837 fstring server, share;
838 pstring path;
839 time_t m_time=0, a_time=0, c_time=0;
840 size_t size=0;
841 uint32 mode=0;
843 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
845 if (!fname) {
846 errno = EINVAL;
847 return -1;
850 smbw_init();
852 smbw_busy++;
854 /* work out what server they are after */
855 smbw_parse_path(fname, server, share, path);
857 /* get a connection to the server */
858 srv = smbw_server(server, share);
859 if (!srv) {
860 /* smbw_server sets errno */
861 goto failed;
864 if (strcmp(share,"IPC$") == 0) {
865 mode = aDIR | aRONLY;
866 } else {
867 if (!smbw_getatr(srv, path,
868 &mode, &size, &c_time, &a_time, &m_time)) {
869 errno = smbw_errno(&srv->cli);
870 goto failed;
874 smbw_setup_stat(st, path, size, mode);
876 st->st_atime = time(NULL);
877 st->st_ctime = m_time;
878 st->st_mtime = m_time;
880 smbw_busy--;
881 return 0;
883 failed:
884 smbw_busy--;
885 return -1;
888 /*****************************************************
889 a wrapper for read()
890 *******************************************************/
891 ssize_t smbw_read(int fd, void *buf, size_t count)
893 struct smbw_file *file;
894 int ret;
896 DEBUG(4,("%s\n", __FUNCTION__));
898 smbw_busy++;
900 file = smbw_file(fd);
901 if (!file) {
902 errno = EBADF;
903 smbw_busy--;
904 return -1;
907 ret = cli_read(&file->srv->cli, file->cli_fd, buf, file->offset, count);
909 if (ret == -1) {
910 errno = smbw_errno(&file->srv->cli);
911 smbw_busy--;
912 return -1;
915 file->offset += ret;
917 smbw_busy--;
918 return ret;
921 /*****************************************************
922 a wrapper for write()
923 *******************************************************/
924 ssize_t smbw_write(int fd, void *buf, size_t count)
926 struct smbw_file *file;
927 int ret;
929 DEBUG(4,("%s\n", __FUNCTION__));
931 smbw_busy++;
933 file = smbw_file(fd);
934 if (!file) {
935 DEBUG(3,("bad fd in read\n"));
936 errno = EBADF;
937 smbw_busy--;
938 return -1;
941 ret = cli_write(&file->srv->cli, file->cli_fd, buf, file->offset, count);
943 if (ret == -1) {
944 errno = smbw_errno(&file->srv->cli);
945 smbw_busy--;
946 return -1;
949 file->offset += ret;
951 smbw_busy--;
952 return ret;
955 /*****************************************************
956 close a directory handle
957 *******************************************************/
958 int smbw_dir_close(int fd)
960 struct smbw_dir *dir;
962 DEBUG(4,("%s\n", __FUNCTION__));
964 dir = smbw_dir(fd);
965 if (!dir) {
966 DEBUG(4,("%s(%d)\n", __FUNCTION__, __LINE__));
967 errno = EBADF;
968 return -1;
971 bitmap_clear(file_bmap, dir->fd - SMBW_FD_OFFSET);
973 DLIST_REMOVE(smbw_dirs, dir);
975 free_dir(dir);
977 return 0;
980 /*****************************************************
981 a wrapper for close()
982 *******************************************************/
983 int smbw_close(int fd)
985 struct smbw_file *file;
987 DEBUG(4,("%s\n", __FUNCTION__));
989 smbw_busy++;
991 file = smbw_file(fd);
992 if (!file) {
993 int ret = smbw_dir_close(fd);
994 smbw_busy--;
995 return ret;
998 if (!cli_close(&file->srv->cli, file->cli_fd)) {
999 errno = smbw_errno(&file->srv->cli);
1000 smbw_busy--;
1001 return -1;
1005 bitmap_clear(file_bmap, file->fd - SMBW_FD_OFFSET);
1007 DLIST_REMOVE(smbw_files, file);
1009 free(file->fname);
1010 ZERO_STRUCTP(file);
1011 free(file);
1013 smbw_busy--;
1015 return 0;
1019 /*****************************************************
1020 a wrapper for fcntl()
1021 *******************************************************/
1022 int smbw_fcntl(int fd, int cmd, long arg)
1024 DEBUG(4,("%s\n", __FUNCTION__));
1025 return 0;
1029 /*****************************************************
1030 a wrapper for getdents()
1031 *******************************************************/
1032 int smbw_getdents(unsigned int fd, struct dirent *dirp, int count)
1034 struct smbw_dir *dir;
1035 int n=0;
1037 DEBUG(4,("%s\n", __FUNCTION__));
1039 smbw_busy++;
1041 dir = smbw_dir(fd);
1042 if (!dir) {
1043 errno = EBADF;
1044 smbw_busy--;
1045 return -1;
1048 while (count>=sizeof(*dirp) && (dir->offset < dir->count)) {
1049 dirp->d_ino = dir->offset + 0x10000;
1050 dirp->d_off = (dir->offset+1)*sizeof(*dirp);
1051 dirp->d_reclen = sizeof(*dirp);
1052 /* what's going on with the -1 here? maybe d_type
1053 isn't really there? */
1054 safe_strcpy(&dirp->d_name[-1], dir->list[dir->offset].name,
1055 sizeof(dirp->d_name)-1);
1056 dir->offset++;
1057 count -= dirp->d_reclen;
1058 if (dir->offset == dir->count) {
1059 dirp->d_off = -1;
1061 dirp++;
1062 n++;
1065 smbw_busy--;
1066 return n*sizeof(*dirp);
1070 /*****************************************************
1071 a wrapper for access()
1072 *******************************************************/
1073 int smbw_access(const char *name, int mode)
1075 struct stat st;
1076 /* how do we map this properly ?? */
1077 return smbw_stat(name, &st) == 0;
1080 /*****************************************************
1081 a wrapper for realink() - needed for correct errno setting
1082 *******************************************************/
1083 int smbw_readlink(const char *path, char *buf, size_t bufsize)
1085 struct stat st;
1086 int ret;
1088 ret = smbw_stat(path, &st);
1089 if (ret != 0) {
1090 DEBUG(4,("readlink(%s) failed\n", path));
1091 return -1;
1094 /* it exists - say it isn't a link */
1095 DEBUG(4,("readlink(%s) not a link\n", path));
1097 errno = EINVAL;
1098 return -1;
1102 /*****************************************************
1103 a wrapper for chdir()
1104 *******************************************************/
1105 int smbw_chdir(const char *name)
1107 struct smbw_server *srv;
1108 fstring server, share;
1109 pstring path;
1110 uint32 mode = aDIR;
1111 char *cwd;
1113 smbw_init();
1115 if (smbw_busy) return real_chdir(cwd);
1117 smbw_busy++;
1119 if (!name) {
1120 errno = EINVAL;
1121 goto failed;
1124 DEBUG(4,("%s (%s)\n", __FUNCTION__, name));
1126 /* work out what server they are after */
1127 cwd = smbw_parse_path(name, server, share, path);
1129 if (strncmp(cwd,SMBW_PREFIX,strlen(SMBW_PREFIX))) {
1130 if (real_chdir(cwd) == 0) {
1131 DEBUG(4,("set SMBW_CWD to %s\n", cwd));
1132 pstrcpy(smb_cwd, cwd);
1133 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
1134 DEBUG(4,("setenv failed\n"));
1136 goto success;
1138 errno = ENOENT;
1139 goto failed;
1142 /* get a connection to the server */
1143 srv = smbw_server(server, share);
1144 if (!srv) {
1145 /* smbw_server sets errno */
1146 goto failed;
1149 if (strcmp(share,"IPC$") &&
1150 !smbw_getatr(srv, path,
1151 &mode, NULL, NULL, NULL, NULL)) {
1152 errno = smbw_errno(&srv->cli);
1153 goto failed;
1156 if (!(mode & aDIR)) {
1157 errno = ENOTDIR;
1158 goto failed;
1161 DEBUG(4,("set SMBW_CWD2 to %s\n", cwd));
1162 pstrcpy(smb_cwd, cwd);
1163 if (setenv(SMBW_PWD_ENV, smb_cwd, 1)) {
1164 DEBUG(4,("setenv failed\n"));
1167 /* we don't want the old directory to be busy */
1168 real_chdir("/");
1170 success:
1171 smbw_busy--;
1172 return 0;
1174 failed:
1175 smbw_busy--;
1176 return -1;
1180 /*****************************************************
1181 a wrapper for unlink()
1182 *******************************************************/
1183 int smbw_unlink(const char *fname)
1185 struct smbw_server *srv;
1186 fstring server, share;
1187 pstring path;
1189 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1191 if (!fname) {
1192 errno = EINVAL;
1193 return -1;
1196 smbw_init();
1198 smbw_busy++;
1200 /* work out what server they are after */
1201 smbw_parse_path(fname, server, share, path);
1203 /* get a connection to the server */
1204 srv = smbw_server(server, share);
1205 if (!srv) {
1206 /* smbw_server sets errno */
1207 goto failed;
1210 if (!cli_unlink(&srv->cli, path)) {
1211 errno = smbw_errno(&srv->cli);
1212 goto failed;
1215 smbw_busy--;
1216 return 0;
1218 failed:
1219 smbw_busy--;
1220 return -1;
1224 /*****************************************************
1225 a wrapper for rename()
1226 *******************************************************/
1227 int smbw_rename(const char *oldname, const char *newname)
1229 struct smbw_server *srv;
1230 fstring server1, share1;
1231 pstring path1;
1232 fstring server2, share2;
1233 pstring path2;
1235 DEBUG(4,("%s (%s, %s)\n", __FUNCTION__, oldname, newname));
1237 if (!oldname || !newname) {
1238 errno = EINVAL;
1239 return -1;
1242 smbw_init();
1244 smbw_busy++;
1246 /* work out what server they are after */
1247 smbw_parse_path(oldname, server1, share1, path1);
1248 smbw_parse_path(newname, server2, share2, path2);
1250 if (strcmp(server1, server2) || strcmp(share1, share2)) {
1251 /* can't cross filesystems */
1252 errno = EXDEV;
1253 return -1;
1256 /* get a connection to the server */
1257 srv = smbw_server(server1, share1);
1258 if (!srv) {
1259 /* smbw_server sets errno */
1260 goto failed;
1263 if (!cli_rename(&srv->cli, path1, path2)) {
1264 errno = smbw_errno(&srv->cli);
1265 goto failed;
1268 smbw_busy--;
1269 return 0;
1271 failed:
1272 smbw_busy--;
1273 return -1;
1277 /*****************************************************
1278 a wrapper for utime()
1279 *******************************************************/
1280 int smbw_utime(const char *fname, struct utimbuf *buf)
1282 struct smbw_server *srv;
1283 fstring server, share;
1284 pstring path;
1285 uint32 mode;
1287 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1289 if (!fname) {
1290 errno = EINVAL;
1291 return -1;
1294 smbw_init();
1296 smbw_busy++;
1298 /* work out what server they are after */
1299 smbw_parse_path(fname, server, share, path);
1301 /* get a connection to the server */
1302 srv = smbw_server(server, share);
1303 if (!srv) {
1304 /* smbw_server sets errno */
1305 goto failed;
1308 if (!cli_getatr(&srv->cli, path, &mode, NULL, NULL)) {
1309 errno = smbw_errno(&srv->cli);
1310 goto failed;
1313 if (!cli_setatr(&srv->cli, path, mode, buf->modtime)) {
1314 errno = smbw_errno(&srv->cli);
1315 goto failed;
1318 smbw_busy--;
1319 return 0;
1321 failed:
1322 smbw_busy--;
1323 return -1;
1326 /*****************************************************
1327 a wrapper for chown()
1328 *******************************************************/
1329 int smbw_chown(const char *fname, uid_t owner, gid_t group)
1331 struct smbw_server *srv;
1332 fstring server, share;
1333 pstring path;
1334 uint32 mode;
1336 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1338 if (!fname) {
1339 errno = EINVAL;
1340 return -1;
1343 smbw_init();
1345 smbw_busy++;
1347 /* work out what server they are after */
1348 smbw_parse_path(fname, server, share, path);
1350 /* get a connection to the server */
1351 srv = smbw_server(server, share);
1352 if (!srv) {
1353 /* smbw_server sets errno */
1354 goto failed;
1357 if (!cli_getatr(&srv->cli, path, &mode, NULL, NULL)) {
1358 errno = smbw_errno(&srv->cli);
1359 goto failed;
1362 /* assume success */
1364 smbw_busy--;
1365 return 0;
1367 failed:
1368 smbw_busy--;
1369 return -1;
1372 /*****************************************************
1373 a wrapper for chmod()
1374 *******************************************************/
1375 int smbw_chmod(const char *fname, mode_t newmode)
1377 struct smbw_server *srv;
1378 fstring server, share;
1379 pstring path;
1380 uint32 mode;
1382 DEBUG(4,("%s (%s)\n", __FUNCTION__, fname));
1384 if (!fname) {
1385 errno = EINVAL;
1386 return -1;
1389 smbw_init();
1391 smbw_busy++;
1393 /* work out what server they are after */
1394 smbw_parse_path(fname, server, share, path);
1396 /* get a connection to the server */
1397 srv = smbw_server(server, share);
1398 if (!srv) {
1399 /* smbw_server sets errno */
1400 goto failed;
1403 if (!cli_getatr(&srv->cli, path, &mode, NULL, NULL)) {
1404 errno = smbw_errno(&srv->cli);
1405 goto failed;
1408 /* assume success for the moment - need to add attribute mapping */
1410 smbw_busy--;
1411 return 0;
1413 failed:
1414 smbw_busy--;
1415 return -1;
1419 /*****************************************************
1420 a wrapper for lseek() on directories
1421 *******************************************************/
1422 off_t smbw_dir_lseek(int fd, off_t offset, int whence)
1424 struct smbw_dir *dir;
1425 off_t ret;
1427 DEBUG(4,("%s offset=%d whence=%d\n", __FUNCTION__,
1428 (int)offset, whence));
1430 dir = smbw_dir(fd);
1431 if (!dir) {
1432 errno = EBADF;
1433 return -1;
1436 switch (whence) {
1437 case SEEK_SET:
1438 dir->offset = offset/sizeof(struct dirent);
1439 break;
1440 case SEEK_CUR:
1441 dir->offset += offset/sizeof(struct dirent);
1442 break;
1443 case SEEK_END:
1444 dir->offset = (dir->count * sizeof(struct dirent)) + offset;
1445 dir->offset /= sizeof(struct dirent);
1446 break;
1449 ret = dir->offset * sizeof(struct dirent);
1451 DEBUG(4,(" -> %d\n", (int)ret));
1453 return ret;
1456 /*****************************************************
1457 a wrapper for lseek()
1458 *******************************************************/
1459 off_t smbw_lseek(int fd, off_t offset, int whence)
1461 struct smbw_file *file;
1462 uint32 size;
1464 DEBUG(4,("%s\n", __FUNCTION__));
1466 smbw_busy++;
1468 file = smbw_file(fd);
1469 if (!file) {
1470 off_t ret = smbw_dir_lseek(fd, offset, whence);
1471 smbw_busy--;
1472 return ret;
1475 switch (whence) {
1476 case SEEK_SET:
1477 file->offset = offset;
1478 break;
1479 case SEEK_CUR:
1480 file->offset += offset;
1481 break;
1482 case SEEK_END:
1483 if (!cli_qfileinfo(&file->srv->cli, file->cli_fd,
1484 NULL, &size, NULL, NULL, NULL) &&
1485 !cli_getattrE(&file->srv->cli, file->cli_fd,
1486 NULL, &size, NULL, NULL, NULL)) {
1487 errno = EINVAL;
1488 smbw_busy--;
1489 return -1;
1491 file->offset = size + offset;
1492 break;
1495 smbw_busy--;
1496 return file->offset;