r18236: fully autogenerated unix info files
[Samba/nascimento.git] / examples / libsmbclient / smbwrapper / smbw.c
bloba44f2f4046a2fe9524cbbb97447ebfd0889e76dc
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB wrapper functions
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include "smbw.h"
29 #include "bsd-strlfunc.h"
31 typedef enum StartupType
33 StartupType_Fake,
34 StartupType_Real
35 } StartupType;
37 int smbw_fd_map[__FD_SETSIZE];
38 int smbw_ref_count[__FD_SETSIZE];
39 char smbw_cwd[PATH_MAX];
40 char smbw_prefix[] = SMBW_PREFIX;
42 /* needs to be here because of dumb include files on some systems */
43 int creat_bits = O_WRONLY|O_CREAT|O_TRUNC;
45 int smbw_initialized = 0;
47 static int debug_level = 0;
49 static SMBCCTX *smbw_ctx;
51 extern int smbw_debug;
54 /*****************************************************
55 smbw_ref -- manipulate reference counts
56 ******************************************************/
57 int smbw_ref(int client_fd, Ref_Count_Type type, ...)
59 va_list ap;
61 /* client id values begin at SMBC_BASE_FC. */
62 client_fd -= SMBC_BASE_FD;
64 va_start(ap, type);
65 switch(type)
67 case SMBW_RCT_Increment:
68 return ++smbw_ref_count[client_fd];
70 case SMBW_RCT_Decrement:
71 return --smbw_ref_count[client_fd];
73 case SMBW_RCT_Get:
74 return smbw_ref_count[client_fd];
76 case SMBW_RCT_Set:
77 return (smbw_ref_count[client_fd] = va_arg(ap, int));
79 va_end(ap);
81 /* never gets here */
82 return -1;
87 * Return a username and password given a server and share name
89 * Returns 0 upon success;
90 * non-zero otherwise, and errno is set to indicate the error.
92 static void get_envvar_auth_data(const char *srv,
93 const char *shr,
94 char *wg, int wglen,
95 char *un, int unlen,
96 char *pw, int pwlen)
98 char *u;
99 char *p;
100 char *w;
102 /* Fall back to environment variables */
104 w = getenv("WORKGROUP");
105 if (w == NULL) w = "";
107 u = getenv("USER");
108 if (u == NULL) u = "";
110 p = getenv("PASSWORD");
111 if (p == NULL) p = "";
113 smbw_strlcpy(wg, w, wglen);
114 smbw_strlcpy(un, u, unlen);
115 smbw_strlcpy(pw, p, pwlen);
118 static smbc_get_auth_data_fn get_auth_data_fn = get_envvar_auth_data;
120 /*****************************************************
121 set the get auth data function
122 ******************************************************/
123 void smbw_set_auth_data_fn(smbc_get_auth_data_fn fn)
125 get_auth_data_fn = fn;
129 /*****************************************************
130 ensure that all connections are terminated upon exit
131 ******************************************************/
132 static void do_shutdown(void)
134 if (smbw_ctx != NULL) {
135 smbc_free_context(smbw_ctx, 1);
140 /*****************************************************
141 initialise structures
142 *******************************************************/
143 static void do_init(StartupType startupType)
145 int i;
146 char *p;
148 smbw_initialized = 1; /* this must be first to avoid recursion! */
150 smbw_ctx = NULL; /* don't free context until it's established */
152 /* initially, no file descriptors are mapped */
153 for (i = 0; i < __FD_SETSIZE; i++) {
154 smbw_fd_map[i] = -1;
155 smbw_ref_count[i] = 0;
158 /* See if we've been told to start in a particular directory */
159 if ((p=getenv("SMBW_DIR")) != NULL) {
160 smbw_strlcpy(smbw_cwd, p, PATH_MAX);
162 /* we don't want the old directory to be busy */
163 (* smbw_libc.chdir)("/");
165 } else {
166 *smbw_cwd = '\0';
169 if ((p=getenv("DEBUG"))) {
170 debug_level = atoi(p);
173 if ((smbw_ctx = smbc_new_context()) == NULL) {
174 fprintf(stderr, "Could not create a context.\n");
175 exit(1);
178 smbw_ctx->debug = debug_level;
179 smbw_ctx->callbacks.auth_fn = get_auth_data_fn;
180 smbw_ctx->options.browse_max_lmb_count = 0;
181 smbw_ctx->options.urlencode_readdir_entries = 1;
182 smbw_ctx->options.one_share_per_server = 1;
184 if (smbc_init_context(smbw_ctx) == NULL) {
185 fprintf(stderr, "Could not initialize context.\n");
186 exit(1);
189 smbc_set_context(smbw_ctx);
191 /* if not real startup, exit handler has already been established */
192 if (startupType == StartupType_Real) {
193 atexit(do_shutdown);
197 /*****************************************************
198 initialise structures, real start up vs a fork()
199 *******************************************************/
200 void smbw_init(void)
202 do_init(StartupType_Real);
206 /*****************************************************
207 determine if a file descriptor is a smb one
208 *******************************************************/
209 int smbw_fd(int smbw_fd)
211 SMBW_INIT();
213 return (smbw_fd >= 0 &&
214 smbw_fd < __FD_SETSIZE &&
215 smbw_fd_map[smbw_fd] >= SMBC_BASE_FD); /* minimum smbc_ fd */
219 /*****************************************************
220 determine if a path is a smb one
221 *******************************************************/
222 int smbw_path(const char *name)
224 int len;
225 int ret;
226 int saved_errno;
228 saved_errno = errno;
230 SMBW_INIT();
232 len = strlen(smbw_prefix);
234 ret = ((strncmp(name, smbw_prefix, len) == 0 &&
235 (name[len] == '\0' || name[len] == '/')) ||
236 (*name != '/' && *smbw_cwd != '\0'));
238 errno = saved_errno;
239 return ret;
243 /*****************************************************
244 remove redundent stuff from a filename
245 *******************************************************/
246 void smbw_clean_fname(char *name)
248 char *p, *p2;
249 int l;
250 int modified = 1;
252 if (!name) return;
254 DEBUG(10, ("Clean [%s]...\n", name));
256 while (modified) {
257 modified = 0;
259 if ((p=strstr(name,"/./"))) {
260 modified = 1;
261 while (*p) {
262 p[0] = p[2];
263 p++;
265 DEBUG(10, ("\tclean 1 (/./) produced [%s]\n", name));
268 if ((p=strstr(name,"//"))) {
269 modified = 1;
270 while (*p) {
271 p[0] = p[1];
272 p++;
274 DEBUG(10, ("\tclean 2 (//) produced [%s]\n", name));
277 if (strcmp(name,"/../")==0) {
278 modified = 1;
279 name[1] = 0;
280 DEBUG(10,("\tclean 3 (^/../$) produced [%s]\n", name));
283 if ((p=strstr(name,"/../"))) {
284 modified = 1;
285 for (p2 = (p > name ? p-1 : p); p2 > name; p2--) {
286 if (p2[0] == '/') break;
288 if (p2 > name) p2++;
289 while (*p2) {
290 p2[0] = p[3];
291 p2++;
292 p++;
294 DEBUG(10, ("\tclean 4 (/../) produced [%s]\n", name));
297 if (strcmp(name,"/..")==0) {
298 modified = 1;
299 name[1] = 0;
300 DEBUG(10, ("\tclean 5 (^/..$) produced [%s]\n", name));
303 l = strlen(name);
304 p = l>=3?(name+l-3):name;
305 if (strcmp(p,"/..")==0) {
306 modified = 1;
307 for (p2=p-1;p2>name;p2--) {
308 if (p2[0] == '/') break;
310 if (p2==name) {
311 p[0] = '/';
312 p[1] = 0;
313 } else {
314 p2[0] = 0;
316 DEBUG(10, ("\tclean 6 (/..) produced [%s]\n", name));
319 l = strlen(name);
320 p = l>=2?(name+l-2):name;
321 if (strcmp(p,"/.")==0) {
322 modified = 1;
323 if (p == name) {
324 p[1] = 0;
325 } else {
326 p[0] = 0;
328 DEBUG(10, ("\tclean 7 (/.) produced [%s]\n", name));
331 if (strncmp(p=name,"./",2) == 0) {
332 modified = 1;
333 do {
334 p[0] = p[2];
335 } while (*p++);
336 DEBUG(10, ("\tclean 8 (^./) produced [%s]\n", name));
339 l = strlen(p=name);
340 if (l > 1 && p[l-1] == '/') {
341 modified = 1;
342 p[l-1] = 0;
343 DEBUG(10, ("\tclean 9 (/) produced [%s]\n", name));
348 void smbw_fix_path(const char *src, char *dest)
350 const char *p;
351 int len = strlen(smbw_prefix);
353 if (*src == '/') {
354 for (p = src + len; *p == '/'; p++)
356 snprintf(dest, PATH_MAX, "smb://%s", p);
357 } else {
358 snprintf(dest, PATH_MAX, "%s/%s", smbw_cwd, src);
361 smbw_clean_fname(dest + 5);
363 DEBUG(10, ("smbw_fix_path(%s) returning [%s]\n", src, dest));
368 /*****************************************************
369 a wrapper for open()
370 *******************************************************/
371 int smbw_open(const char *fname, int flags, mode_t mode)
373 int client_fd;
374 int smbw_fd;
375 char path[PATH_MAX];
377 SMBW_INIT();
379 if (!fname) {
380 errno = EINVAL;
381 return -1;
384 smbw_fd = (smbw_libc.open)(SMBW_DUMMY, O_WRONLY, 0200);
385 if (smbw_fd == -1) {
386 errno = EMFILE;
387 return -1;
390 smbw_fix_path(fname, path);
391 if (flags == creat_bits) {
392 client_fd = smbc_creat(path, mode);
393 } else {
394 client_fd = smbc_open(path, flags, mode);
397 if (client_fd < 0) {
398 (* smbw_libc.close)(smbw_fd);
399 return -1;
402 smbw_fd_map[smbw_fd] = client_fd;
403 smbw_ref(client_fd, SMBW_RCT_Increment);
404 return smbw_fd;
408 /*****************************************************
409 a wrapper for pread()
411 there should really be an smbc_pread() to avoid the two
412 lseek()s required in this kludge.
413 *******************************************************/
414 ssize_t smbw_pread(int smbw_fd, void *buf, size_t count, SMBW_OFF_T ofs)
416 int client_fd;
417 ssize_t ret;
418 int saved_errno;
419 SMBW_OFF_T old_ofs;
421 if (count == 0) {
422 return 0;
425 client_fd = smbw_fd_map[smbw_fd];
427 if ((old_ofs = smbc_lseek(client_fd, 0, SEEK_CUR)) < 0 ||
428 smbc_lseek(client_fd, ofs, SEEK_SET) < 0) {
429 return -1;
432 if ((ret = smbc_read(client_fd, buf, count)) < 0) {
433 saved_errno = errno;
434 (void) smbc_lseek(client_fd, old_ofs, SEEK_SET);
435 errno = saved_errno;
436 return -1;
439 return ret;
442 /*****************************************************
443 a wrapper for read()
444 *******************************************************/
445 ssize_t smbw_read(int smbw_fd, void *buf, size_t count)
447 int client_fd;
449 client_fd = smbw_fd_map[smbw_fd];
451 return smbc_read(client_fd, buf, count);
456 /*****************************************************
457 a wrapper for write()
458 *******************************************************/
459 ssize_t smbw_write(int smbw_fd, void *buf, size_t count)
461 int client_fd;
463 client_fd = smbw_fd_map[smbw_fd];
465 return smbc_write(client_fd, buf, count);
468 /*****************************************************
469 a wrapper for pwrite()
470 *******************************************************/
471 ssize_t smbw_pwrite(int smbw_fd, void *buf, size_t count, SMBW_OFF_T ofs)
473 int saved_errno;
474 int client_fd;
475 ssize_t ret;
476 SMBW_OFF_T old_ofs;
478 if (count == 0) {
479 return 0;
482 client_fd = smbw_fd_map[smbw_fd];
484 if ((old_ofs = smbc_lseek(client_fd, 0, SEEK_CUR)) < 0 ||
485 smbc_lseek(client_fd, ofs, SEEK_SET) < 0) {
486 return -1;
489 if ((ret = smbc_write(client_fd, buf, count)) < 0) {
490 saved_errno = errno;
491 (void) smbc_lseek(client_fd, old_ofs, SEEK_SET);
492 errno = saved_errno;
493 return -1;
496 return ret;
499 /*****************************************************
500 a wrapper for close()
501 *******************************************************/
502 int smbw_close(int smbw_fd)
504 int client_fd;
506 client_fd = smbw_fd_map[smbw_fd];
508 if (smbw_ref(client_fd, SMBW_RCT_Decrement) > 0) {
509 return 0;
512 (* smbw_libc.close)(smbw_fd);
513 smbw_fd_map[smbw_fd] = -1;
514 return smbc_close(client_fd);
518 /*****************************************************
519 a wrapper for fcntl()
520 *******************************************************/
521 int smbw_fcntl(int smbw_fd, int cmd, long arg)
523 return 0;
527 /*****************************************************
528 a wrapper for access()
529 *******************************************************/
530 int smbw_access(const char *name, int mode)
532 struct SMBW_stat st;
534 SMBW_INIT();
536 if (smbw_stat(name, &st)) return -1;
538 if (((mode & R_OK) && !(st.s_mode & S_IRUSR)) ||
539 ((mode & W_OK) && !(st.s_mode & S_IWUSR)) ||
540 ((mode & X_OK) && !(st.s_mode & S_IXUSR))) {
541 errno = EACCES;
542 return -1;
545 return 0;
548 /*****************************************************
549 a wrapper for readlink() - needed for correct errno setting
550 *******************************************************/
551 int smbw_readlink(const char *fname, char *buf, size_t bufsize)
553 struct SMBW_stat st;
554 int ret;
556 SMBW_INIT();
558 ret = smbw_stat(fname, &st);
559 if (ret != 0) {
560 return -1;
563 /* it exists - say it isn't a link */
564 errno = EINVAL;
565 return -1;
569 /*****************************************************
570 a wrapper for unlink()
571 *******************************************************/
572 int smbw_unlink(const char *fname)
574 char path[PATH_MAX];
576 SMBW_INIT();
578 smbw_fix_path(fname, path);
579 return smbc_unlink(path);
583 /*****************************************************
584 a wrapper for rename()
585 *******************************************************/
586 int smbw_rename(const char *oldname, const char *newname)
588 char path_old[PATH_MAX];
589 char path_new[PATH_MAX];
591 SMBW_INIT();
593 smbw_fix_path(oldname, path_old);
594 smbw_fix_path(newname, path_new);
595 return smbc_rename(path_old, path_new);
599 /*****************************************************
600 a wrapper for utimes
601 *******************************************************/
602 int smbw_utimes(const char *fname, void *buf)
604 char path[PATH_MAX];
606 smbw_fix_path(fname, path);
607 return smbc_utimes(path, buf);
611 /*****************************************************
612 a wrapper for utime
613 *******************************************************/
614 int smbw_utime(const char *fname, void *buf)
616 char path[PATH_MAX];
618 smbw_fix_path(fname, path);
619 return smbc_utime(path, buf);
622 /*****************************************************
623 a wrapper for chown()
624 *******************************************************/
625 int smbw_chown(const char *fname, uid_t owner, gid_t group)
627 /* always indiciate that this is not supported. */
628 errno = ENOTSUP;
629 return -1;
632 /*****************************************************
633 a wrapper for chmod()
634 *******************************************************/
635 int smbw_chmod(const char *fname, mode_t newmode)
637 char path[PATH_MAX];
639 smbw_fix_path(fname, path);
640 return smbc_chmod(path, newmode);
644 /*****************************************************
645 a wrapper for lseek()
646 *******************************************************/
647 SMBW_OFF_T smbw_lseek(int smbw_fd,
648 SMBW_OFF_T offset,
649 int whence)
651 int client_fd;
652 SMBW_OFF_T ret;
654 client_fd = smbw_fd_map[smbw_fd];
656 ret = smbc_lseek(client_fd, offset, whence);
657 if (smbw_debug)
659 printf("smbw_lseek(%d/%d, 0x%llx) returned 0x%llx\n",
660 smbw_fd, client_fd,
661 (unsigned long long) offset,
662 (unsigned long long) ret);
664 return ret;
667 /*****************************************************
668 a wrapper for dup()
669 *******************************************************/
670 int smbw_dup(int smbw_fd)
672 int fd2;
674 fd2 = (smbw_libc.dup)(smbw_fd);
675 if (fd2 == -1) {
676 return -1;
679 smbw_fd_map[fd2] = smbw_fd_map[smbw_fd];
680 smbw_ref(smbw_fd_map[smbw_fd], SMBW_RCT_Increment);
681 return fd2;
685 /*****************************************************
686 a wrapper for dup2()
687 *******************************************************/
688 int smbw_dup2(int smbw_fd, int fd2)
690 if ((* smbw_libc.dup2)(smbw_fd, fd2) != fd2) {
691 return -1;
694 smbw_fd_map[fd2] = smbw_fd_map[smbw_fd];
695 smbw_ref(smbw_fd_map[smbw_fd], SMBW_RCT_Increment);
696 return fd2;
700 /*****************************************************
701 when we fork we have to close all connections and files
702 in the child
703 *******************************************************/
704 int smbw_fork(void)
706 int i;
707 pid_t child_pid;
708 int p[2];
709 char c = 0;
711 SMBW_INIT();
713 if (pipe(p)) return (* smbw_libc.fork)();
715 child_pid = (* smbw_libc.fork)();
717 if (child_pid) {
718 /* block the parent for a moment until the sockets are
719 closed */
720 (* smbw_libc.close)(p[1]);
721 (* smbw_libc.read)(p[0], &c, 1);
722 (* smbw_libc.close)(p[0]);
723 return child_pid;
726 (* smbw_libc.close)(p[0]);
728 /* close all server connections and locally-opened files */
729 for (i = 0; i < __FD_SETSIZE; i++) {
730 if (smbw_fd_map[i] > 0 &&
731 smbw_ref(smbw_fd_map[i], SMBW_RCT_Get) > 0) {
733 smbc_close(smbw_fd_map[i]);
734 smbw_ref(smbw_fd_map[i], SMBW_RCT_Set, 0);
735 (* smbw_libc.close)(i);
738 smbw_fd_map[i] = -1;
741 /* unblock the parent */
742 write(p[1], &c, 1);
743 (* smbw_libc.close)(p[1]);
745 /* specify directory to start in, if it's simulated smb */
746 if (*smbw_cwd != '\0') {
747 setenv("SMBW_DIR", smbw_cwd, 1);
748 } else {
749 unsetenv("SMBW_DIR");
752 /* Re-initialize this library for the child */
753 do_init(StartupType_Fake);
755 /* and continue in the child */
756 return 0;
759 int smbw_setxattr(const char *fname,
760 const char *name,
761 const void *value,
762 size_t size,
763 int flags)
765 char path[PATH_MAX];
767 if (strcmp(name, "system.posix_acl_access") == 0)
769 name = "system.*";
772 smbw_fix_path(fname, path);
773 return smbc_setxattr(path, name, value, size, flags);
776 int smbw_lsetxattr(const char *fname,
777 const char *name,
778 const void *value,
779 size_t size,
780 int flags)
782 char path[PATH_MAX];
784 if (strcmp(name, "system.posix_acl_access") == 0)
786 name = "system.*";
789 smbw_fix_path(fname, path);
790 return smbc_lsetxattr(path, name, value, size, flags);
793 int smbw_fsetxattr(int smbw_fd,
794 const char *name,
795 const void *value,
796 size_t size,
797 int flags)
799 int client_fd;
801 if (strcmp(name, "system.posix_acl_access") == 0)
803 name = "system.*";
806 client_fd = smbw_fd_map[smbw_fd];
807 return smbc_fsetxattr(client_fd, name, value, size, flags);
810 int smbw_getxattr(const char *fname,
811 const char *name,
812 const void *value,
813 size_t size)
815 char path[PATH_MAX];
817 if (strcmp(name, "system.posix_acl_access") == 0)
819 name = "system.*";
822 smbw_fix_path(fname, path);
824 return smbc_getxattr(path, name, value, size);
827 int smbw_lgetxattr(const char *fname,
828 const char *name,
829 const void *value,
830 size_t size)
832 char path[PATH_MAX];
834 if (strcmp(name, "system.posix_acl_access") == 0)
836 name = "system.*";
839 smbw_fix_path(fname, path);
840 return smbc_lgetxattr(path, name, value, size);
843 int smbw_fgetxattr(int smbw_fd,
844 const char *name,
845 const void *value,
846 size_t size)
848 int client_fd;
850 if (strcmp(name, "system.posix_acl_access") == 0)
852 name = "system.*";
855 client_fd = smbw_fd_map[smbw_fd];
856 return smbc_fgetxattr(client_fd, name, value, size);
859 int smbw_removexattr(const char *fname,
860 const char *name)
862 char path[PATH_MAX];
864 if (strcmp(name, "system.posix_acl_access") == 0)
866 name = "system.*";
869 smbw_fix_path(fname, path);
870 return smbc_removexattr(path, name);
873 int smbw_lremovexattr(const char *fname,
874 const char *name)
876 char path[PATH_MAX];
878 if (strcmp(name, "system.posix_acl_access") == 0)
880 name = "system.*";
883 smbw_fix_path(fname, path);
884 return smbc_lremovexattr(path, name);
887 int smbw_fremovexattr(int smbw_fd,
888 const char *name)
890 int client_fd;
892 if (strcmp(name, "system.posix_acl_access") == 0)
894 name = "system.*";
897 client_fd = smbw_fd_map[smbw_fd];
898 return smbc_fremovexattr(client_fd, name);
901 int smbw_listxattr(const char *fname,
902 char *list,
903 size_t size)
905 char path[PATH_MAX];
907 smbw_fix_path(fname, path);
908 return smbc_listxattr(path, list, size);
911 int smbw_llistxattr(const char *fname,
912 char *list,
913 size_t size)
915 char path[PATH_MAX];
917 smbw_fix_path(fname, path);
918 return smbc_llistxattr(path, list, size);
921 int smbw_flistxattr(int smbw_fd,
922 char *list,
923 size_t size)
925 int client_fd;
927 client_fd = smbw_fd_map[smbw_fd];
928 return smbc_flistxattr(client_fd, list, size);