2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Derrell Lipman 2003-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "bsd-strlfunc.h"
30 typedef enum StartupType
36 int smbw_fd_map
[__FD_SETSIZE
];
37 int smbw_ref_count
[__FD_SETSIZE
];
38 char smbw_cwd
[PATH_MAX
];
39 char smbw_prefix
[] = SMBW_PREFIX
;
41 /* needs to be here because of dumb include files on some systems */
42 int creat_bits
= O_WRONLY
|O_CREAT
|O_TRUNC
;
44 int smbw_initialized
= 0;
46 static int debug_level
= 0;
48 static SMBCCTX
*smbw_ctx
;
50 extern int smbw_debug
;
53 /*****************************************************
54 smbw_ref -- manipulate reference counts
55 ******************************************************/
56 int smbw_ref(int client_fd
, Ref_Count_Type type
, ...)
58 /* client id values begin at SMBC_BASE_FC. */
59 client_fd
-= SMBC_BASE_FD
;
63 case SMBW_RCT_Increment
:
64 return ++smbw_ref_count
[client_fd
];
66 case SMBW_RCT_Decrement
:
67 return --smbw_ref_count
[client_fd
];
70 return smbw_ref_count
[client_fd
];
78 ret
= (smbw_ref_count
[client_fd
] = va_arg(ap
, int));
90 * Return a username and password given a server and share name
92 * Returns 0 upon success;
93 * non-zero otherwise, and errno is set to indicate the error.
95 static void get_envvar_auth_data(const char *srv
,
105 /* Fall back to environment variables */
107 w
= getenv("WORKGROUP");
108 if (w
== NULL
) w
= "";
111 if (u
== NULL
) u
= "";
113 p
= getenv("PASSWORD");
114 if (p
== NULL
) p
= "";
116 smbw_strlcpy(wg
, w
, wglen
);
117 smbw_strlcpy(un
, u
, unlen
);
118 smbw_strlcpy(pw
, p
, pwlen
);
121 static smbc_get_auth_data_fn get_auth_data_fn
= get_envvar_auth_data
;
123 /*****************************************************
124 set the get auth data function
125 ******************************************************/
126 void smbw_set_auth_data_fn(smbc_get_auth_data_fn fn
)
128 get_auth_data_fn
= fn
;
132 /*****************************************************
133 ensure that all connections are terminated upon exit
134 ******************************************************/
135 static void do_shutdown(void)
137 if (smbw_ctx
!= NULL
) {
138 smbc_free_context(smbw_ctx
, 1);
143 /*****************************************************
144 initialise structures
145 *******************************************************/
146 static void do_init(StartupType startupType
)
151 smbw_initialized
= 1; /* this must be first to avoid recursion! */
153 smbw_ctx
= NULL
; /* don't free context until it's established */
155 /* initially, no file descriptors are mapped */
156 for (i
= 0; i
< __FD_SETSIZE
; i
++) {
158 smbw_ref_count
[i
] = 0;
161 /* See if we've been told to start in a particular directory */
162 if ((p
=getenv("SMBW_DIR")) != NULL
) {
163 smbw_strlcpy(smbw_cwd
, p
, PATH_MAX
);
165 /* we don't want the old directory to be busy */
166 (* smbw_libc
.chdir
)("/");
172 if ((p
=getenv("DEBUG"))) {
173 debug_level
= atoi(p
);
176 if ((smbw_ctx
= smbc_new_context()) == NULL
) {
177 fprintf(stderr
, "Could not create a context.\n");
181 smbc_setDebug(smbw_ctx
, debug_level
);
182 smbc_setFunctionAuthData(smbw_ctx
, get_auth_data_fn
);
183 smbc_setOptionBrowseMaxLmbCount(smbw_ctx
, 0);
184 smbc_setOptionUrlEncodeReaddirEntries(smbw_ctx
, 1);
185 smbc_setOptionOneSharePerServer(smbw_ctx
, 1);
187 if (smbc_init_context(smbw_ctx
) == NULL
) {
188 fprintf(stderr
, "Could not initialize context.\n");
192 smbc_set_context(smbw_ctx
);
194 /* if not real startup, exit handler has already been established */
195 if (startupType
== StartupType_Real
) {
200 /*****************************************************
201 initialise structures, real start up vs a fork()
202 *******************************************************/
205 do_init(StartupType_Real
);
209 /*****************************************************
210 determine if a file descriptor is a smb one
211 *******************************************************/
212 int smbw_fd(int smbw_fd
)
216 return (smbw_fd
>= 0 &&
217 smbw_fd
< __FD_SETSIZE
&&
218 smbw_fd_map
[smbw_fd
] >= SMBC_BASE_FD
); /* minimum smbc_ fd */
222 /*****************************************************
223 determine if a path is a smb one
224 *******************************************************/
225 int smbw_path(const char *name
)
235 len
= strlen(smbw_prefix
);
237 ret
= ((strncmp(name
, smbw_prefix
, len
) == 0 &&
238 (name
[len
] == '\0' || name
[len
] == '/')) ||
239 (*name
!= '/' && *smbw_cwd
!= '\0'));
246 /*****************************************************
247 remove redundent stuff from a filename
248 *******************************************************/
249 void smbw_clean_fname(char *name
)
257 DEBUG(10, ("Clean [%s]...\n", name
));
262 if ((p
=strstr(name
,"/./"))) {
268 DEBUG(10, ("\tclean 1 (/./) produced [%s]\n", name
));
271 if ((p
=strstr(name
,"//"))) {
277 DEBUG(10, ("\tclean 2 (//) produced [%s]\n", name
));
280 if (strcmp(name
,"/../")==0) {
283 DEBUG(10,("\tclean 3 (^/../$) produced [%s]\n", name
));
286 if ((p
=strstr(name
,"/../"))) {
288 for (p2
= (p
> name
? p
-1 : p
); p2
> name
; p2
--) {
289 if (p2
[0] == '/') break;
297 DEBUG(10, ("\tclean 4 (/../) produced [%s]\n", name
));
300 if (strcmp(name
,"/..")==0) {
303 DEBUG(10, ("\tclean 5 (^/..$) produced [%s]\n", name
));
307 p
= l
>=3?(name
+l
-3):name
;
308 if (strcmp(p
,"/..")==0) {
310 for (p2
=p
-1;p2
>name
;p2
--) {
311 if (p2
[0] == '/') break;
319 DEBUG(10, ("\tclean 6 (/..) produced [%s]\n", name
));
323 p
= l
>=2?(name
+l
-2):name
;
324 if (strcmp(p
,"/.")==0) {
331 DEBUG(10, ("\tclean 7 (/.) produced [%s]\n", name
));
334 if (strncmp(p
=name
,"./",2) == 0) {
339 DEBUG(10, ("\tclean 8 (^./) produced [%s]\n", name
));
343 if (l
> 1 && p
[l
-1] == '/') {
346 DEBUG(10, ("\tclean 9 (/) produced [%s]\n", name
));
351 void smbw_fix_path(const char *src
, char *dest
)
354 int len
= strlen(smbw_prefix
);
357 for (p
= src
+ len
; *p
== '/'; p
++)
359 snprintf(dest
, PATH_MAX
, "smb://%s", p
);
361 snprintf(dest
, PATH_MAX
, "%s/%s", smbw_cwd
, src
);
364 smbw_clean_fname(dest
+ 5);
366 DEBUG(10, ("smbw_fix_path(%s) returning [%s]\n", src
, dest
));
371 /*****************************************************
373 *******************************************************/
374 int smbw_open(const char *fname
, int flags
, mode_t mode
)
387 smbw_fd
= (smbw_libc
.open
)(SMBW_DUMMY
, O_WRONLY
, 0200);
393 smbw_fix_path(fname
, path
);
394 if (flags
== creat_bits
) {
395 client_fd
= smbc_creat(path
, mode
);
397 client_fd
= smbc_open(path
, flags
, mode
);
401 (* smbw_libc
.close
)(smbw_fd
);
405 smbw_fd_map
[smbw_fd
] = client_fd
;
406 smbw_ref(client_fd
, SMBW_RCT_Increment
);
411 /*****************************************************
412 a wrapper for pread()
414 there should really be an smbc_pread() to avoid the two
415 lseek()s required in this kludge.
416 *******************************************************/
417 ssize_t
smbw_pread(int smbw_fd
, void *buf
, size_t count
, SMBW_OFF_T ofs
)
428 client_fd
= smbw_fd_map
[smbw_fd
];
430 if ((old_ofs
= smbc_lseek(client_fd
, 0, SEEK_CUR
)) < 0 ||
431 smbc_lseek(client_fd
, ofs
, SEEK_SET
) < 0) {
435 if ((ret
= smbc_read(client_fd
, buf
, count
)) < 0) {
437 (void) smbc_lseek(client_fd
, old_ofs
, SEEK_SET
);
445 /*****************************************************
447 *******************************************************/
448 ssize_t
smbw_read(int smbw_fd
, void *buf
, size_t count
)
452 client_fd
= smbw_fd_map
[smbw_fd
];
454 return smbc_read(client_fd
, buf
, count
);
459 /*****************************************************
460 a wrapper for write()
461 *******************************************************/
462 ssize_t
smbw_write(int smbw_fd
, void *buf
, size_t count
)
466 client_fd
= smbw_fd_map
[smbw_fd
];
468 return smbc_write(client_fd
, buf
, count
);
471 /*****************************************************
472 a wrapper for pwrite()
473 *******************************************************/
474 ssize_t
smbw_pwrite(int smbw_fd
, void *buf
, size_t count
, SMBW_OFF_T ofs
)
485 client_fd
= smbw_fd_map
[smbw_fd
];
487 if ((old_ofs
= smbc_lseek(client_fd
, 0, SEEK_CUR
)) < 0 ||
488 smbc_lseek(client_fd
, ofs
, SEEK_SET
) < 0) {
492 if ((ret
= smbc_write(client_fd
, buf
, count
)) < 0) {
494 (void) smbc_lseek(client_fd
, old_ofs
, SEEK_SET
);
502 /*****************************************************
503 a wrapper for close()
504 *******************************************************/
505 int smbw_close(int smbw_fd
)
509 client_fd
= smbw_fd_map
[smbw_fd
];
511 if (smbw_ref(client_fd
, SMBW_RCT_Decrement
) > 0) {
515 (* smbw_libc
.close
)(smbw_fd
);
516 smbw_fd_map
[smbw_fd
] = -1;
517 return smbc_close(client_fd
);
521 /*****************************************************
522 a wrapper for fcntl()
523 *******************************************************/
524 int smbw_fcntl(int smbw_fd
, int cmd
, long arg
)
530 /*****************************************************
531 a wrapper for access()
532 *******************************************************/
533 int smbw_access(const char *name
, int mode
)
539 if (smbw_stat(name
, &st
)) return -1;
541 if (((mode
& R_OK
) && !(st
.s_mode
& S_IRUSR
)) ||
542 ((mode
& W_OK
) && !(st
.s_mode
& S_IWUSR
)) ||
543 ((mode
& X_OK
) && !(st
.s_mode
& S_IXUSR
))) {
551 /*****************************************************
552 a wrapper for readlink() - needed for correct errno setting
553 *******************************************************/
554 int smbw_readlink(const char *fname
, char *buf
, size_t bufsize
)
561 ret
= smbw_stat(fname
, &st
);
566 /* it exists - say it isn't a link */
572 /*****************************************************
573 a wrapper for unlink()
574 *******************************************************/
575 int smbw_unlink(const char *fname
)
581 smbw_fix_path(fname
, path
);
582 return smbc_unlink(path
);
586 /*****************************************************
587 a wrapper for rename()
588 *******************************************************/
589 int smbw_rename(const char *oldname
, const char *newname
)
591 char path_old
[PATH_MAX
];
592 char path_new
[PATH_MAX
];
596 smbw_fix_path(oldname
, path_old
);
597 smbw_fix_path(newname
, path_new
);
598 return smbc_rename(path_old
, path_new
);
602 /*****************************************************
604 *******************************************************/
605 int smbw_utimes(const char *fname
, void *buf
)
609 smbw_fix_path(fname
, path
);
610 return smbc_utimes(path
, buf
);
614 /*****************************************************
616 *******************************************************/
617 int smbw_utime(const char *fname
, void *buf
)
621 smbw_fix_path(fname
, path
);
622 return smbc_utime(path
, buf
);
625 /*****************************************************
626 a wrapper for chown()
627 *******************************************************/
628 int smbw_chown(const char *fname
, uid_t owner
, gid_t group
)
630 /* always indiciate that this is not supported. */
635 /*****************************************************
636 a wrapper for chmod()
637 *******************************************************/
638 int smbw_chmod(const char *fname
, mode_t newmode
)
642 smbw_fix_path(fname
, path
);
643 return smbc_chmod(path
, newmode
);
647 /*****************************************************
648 a wrapper for lseek()
649 *******************************************************/
650 SMBW_OFF_T
smbw_lseek(int smbw_fd
,
657 client_fd
= smbw_fd_map
[smbw_fd
];
659 ret
= smbc_lseek(client_fd
, offset
, whence
);
662 printf("smbw_lseek(%d/%d, 0x%llx) returned 0x%llx\n",
664 (unsigned long long) offset
,
665 (unsigned long long) ret
);
670 /*****************************************************
672 *******************************************************/
673 int smbw_dup(int smbw_fd
)
677 fd2
= (smbw_libc
.dup
)(smbw_fd
);
682 smbw_fd_map
[fd2
] = smbw_fd_map
[smbw_fd
];
683 smbw_ref(smbw_fd_map
[smbw_fd
], SMBW_RCT_Increment
);
688 /*****************************************************
690 *******************************************************/
691 int smbw_dup2(int smbw_fd
, int fd2
)
693 if ((* smbw_libc
.dup2
)(smbw_fd
, fd2
) != fd2
) {
697 smbw_fd_map
[fd2
] = smbw_fd_map
[smbw_fd
];
698 smbw_ref(smbw_fd_map
[smbw_fd
], SMBW_RCT_Increment
);
703 /*****************************************************
704 when we fork we have to close all connections and files
706 *******************************************************/
716 if (pipe(p
)) return (* smbw_libc
.fork
)();
718 child_pid
= (* smbw_libc
.fork
)();
721 /* block the parent for a moment until the sockets are
723 (* smbw_libc
.close
)(p
[1]);
724 (* smbw_libc
.read
)(p
[0], &c
, 1);
725 (* smbw_libc
.close
)(p
[0]);
729 (* smbw_libc
.close
)(p
[0]);
731 /* close all server connections and locally-opened files */
732 for (i
= 0; i
< __FD_SETSIZE
; i
++) {
733 if (smbw_fd_map
[i
] > 0 &&
734 smbw_ref(smbw_fd_map
[i
], SMBW_RCT_Get
) > 0) {
736 smbc_close(smbw_fd_map
[i
]);
737 smbw_ref(smbw_fd_map
[i
], SMBW_RCT_Set
, 0);
738 (* smbw_libc
.close
)(i
);
744 /* unblock the parent */
746 (* smbw_libc
.close
)(p
[1]);
748 /* specify directory to start in, if it's simulated smb */
749 if (*smbw_cwd
!= '\0') {
750 setenv("SMBW_DIR", smbw_cwd
, 1);
752 unsetenv("SMBW_DIR");
755 /* Re-initialize this library for the child */
756 do_init(StartupType_Fake
);
758 /* and continue in the child */
762 int smbw_setxattr(const char *fname
,
770 if (strcmp(name
, "system.posix_acl_access") == 0)
775 smbw_fix_path(fname
, path
);
776 return smbc_setxattr(path
, name
, value
, size
, flags
);
779 int smbw_lsetxattr(const char *fname
,
787 if (strcmp(name
, "system.posix_acl_access") == 0)
792 smbw_fix_path(fname
, path
);
793 return smbc_lsetxattr(path
, name
, value
, size
, flags
);
796 int smbw_fsetxattr(int smbw_fd
,
804 if (strcmp(name
, "system.posix_acl_access") == 0)
809 client_fd
= smbw_fd_map
[smbw_fd
];
810 return smbc_fsetxattr(client_fd
, name
, value
, size
, flags
);
813 int smbw_getxattr(const char *fname
,
820 if (strcmp(name
, "system.posix_acl_access") == 0)
825 smbw_fix_path(fname
, path
);
827 return smbc_getxattr(path
, name
, value
, size
);
830 int smbw_lgetxattr(const char *fname
,
837 if (strcmp(name
, "system.posix_acl_access") == 0)
842 smbw_fix_path(fname
, path
);
843 return smbc_lgetxattr(path
, name
, value
, size
);
846 int smbw_fgetxattr(int smbw_fd
,
853 if (strcmp(name
, "system.posix_acl_access") == 0)
858 client_fd
= smbw_fd_map
[smbw_fd
];
859 return smbc_fgetxattr(client_fd
, name
, value
, size
);
862 int smbw_removexattr(const char *fname
,
867 if (strcmp(name
, "system.posix_acl_access") == 0)
872 smbw_fix_path(fname
, path
);
873 return smbc_removexattr(path
, name
);
876 int smbw_lremovexattr(const char *fname
,
881 if (strcmp(name
, "system.posix_acl_access") == 0)
886 smbw_fix_path(fname
, path
);
887 return smbc_lremovexattr(path
, name
);
890 int smbw_fremovexattr(int smbw_fd
,
895 if (strcmp(name
, "system.posix_acl_access") == 0)
900 client_fd
= smbw_fd_map
[smbw_fd
];
901 return smbc_fremovexattr(client_fd
, name
);
904 int smbw_listxattr(const char *fname
,
910 smbw_fix_path(fname
, path
);
911 return smbc_listxattr(path
, list
, size
);
914 int smbw_llistxattr(const char *fname
,
920 smbw_fix_path(fname
, path
);
921 return smbc_llistxattr(path
, list
, size
);
924 int smbw_flistxattr(int smbw_fd
,
930 client_fd
= smbw_fd_map
[smbw_fd
];
931 return smbc_flistxattr(client_fd
, list
, size
);