2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "include/libsmb_internal.h"
31 * DOS Attribute values (used internally)
33 typedef struct DOS_ATTR_DESC
{
44 * Internal flags for extended attributes
47 /* internal mode values */
48 #define SMBC_XATTR_MODE_ADD 1
49 #define SMBC_XATTR_MODE_REMOVE 2
50 #define SMBC_XATTR_MODE_REMOVE_ALL 3
51 #define SMBC_XATTR_MODE_SET 4
52 #define SMBC_XATTR_MODE_CHOWN 5
53 #define SMBC_XATTR_MODE_CHGRP 6
55 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
57 /*We should test for this in configure ... */
59 #define ENOTSUP EOPNOTSUPP
63 * Functions exported by libsmb_cache.c that we need here
65 int smbc_default_cache_functions(SMBCCTX
*context
);
68 * check if an element is part of the list.
69 * FIXME: Does not belong here !
70 * Can anyone put this in a macro in dlinklist.h ?
73 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
74 if (!p
|| !list
) return False
;
76 if (p
== list
) return True
;
83 * Find an lsa pipe handle associated with a cli struct.
86 static struct rpc_pipe_client
*find_lsa_pipe_hnd(struct cli_state
*ipc_cli
)
88 struct rpc_pipe_client
*pipe_hnd
;
90 for (pipe_hnd
= ipc_cli
->pipe_list
; pipe_hnd
; pipe_hnd
= pipe_hnd
->next
) {
91 if (pipe_hnd
->pipe_idx
== PI_LSARPC
) {
99 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
);
100 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
);
102 extern BOOL in_client
;
105 * Is the logging working / configfile read ?
107 static int smbc_initialized
= 0;
110 hex2int( unsigned int _char
)
112 if ( _char
>= 'A' && _char
<='F')
113 return _char
- 'A' + 10;
114 if ( _char
>= 'a' && _char
<='f')
115 return _char
- 'a' + 10;
116 if ( _char
>= '0' && _char
<='9')
124 * Convert strings of %xx to their single character equivalent. Each 'x' must
125 * be a valid hexadecimal digit, or that % sequence is left undecoded.
127 * dest may, but need not be, the same pointer as src.
129 * Returns the number of % sequences which could not be converted due to lack
130 * of two following hexadecimal digits.
133 smbc_urldecode(char *dest
, char * src
, size_t max_dest_len
)
135 int old_length
= strlen(src
);
141 if ( old_length
== 0 ) {
146 while ( i
< old_length
) {
147 unsigned char character
= src
[ i
++ ];
149 if (character
== '%') {
150 int a
= i
+1 < old_length
? hex2int( src
[i
] ) : -1;
151 int b
= i
+1 < old_length
? hex2int( src
[i
+1] ) : -1;
153 /* Replace valid sequence */
154 if (a
!= -1 && b
!= -1) {
156 /* Replace valid %xx sequence with %dd */
157 character
= (a
* 16) + b
;
159 if (character
== '\0') {
160 break; /* Stop at %00 */
175 strncpy(dest
, temp
, max_dest_len
);
183 * Convert any characters not specifically allowed in a URL into their %xx
186 * Returns the remaining buffer length.
189 smbc_urlencode(char * dest
, char * src
, int max_dest_len
)
191 char hex
[] = "0123456789ABCDEF";
193 for (; *src
!= '\0' && max_dest_len
>= 3; src
++) {
205 *dest
++ = hex
[(*src
>> 4) & 0x0f];
206 *dest
++ = hex
[*src
& 0x0f];
221 * Function to parse a path and turn it into components
223 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
224 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
225 * general format ("smb:" only; we do not look for "cifs:").
229 * smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]][?options]
233 * smb:// Show all workgroups.
235 * The method of locating the list of workgroups varies
236 * depending upon the setting of the context variable
237 * context->options.browse_max_lmb_count. This value
238 * determine the maximum number of local master browsers to
239 * query for the list of workgroups. In order to ensure that
240 * a complete list of workgroups is obtained, all master
241 * browsers must be queried, but if there are many
242 * workgroups, the time spent querying can begin to add up.
243 * For small networks (not many workgroups), it is suggested
244 * that this variable be set to 0, indicating query all local
245 * master browsers. When the network has many workgroups, a
246 * reasonable setting for this variable might be around 3.
248 * smb://name/ if name<1D> or name<1B> exists, list servers in
249 * workgroup, else, if name<20> exists, list all shares
252 * If "options" are provided, this function returns the entire option list as a
253 * string, for later parsing by the caller. Note that currently, no options
257 static const char *smbc_prefix
= "smb:";
260 smbc_parse_path(SMBCCTX
*context
,
262 char *server
, int server_len
,
263 char *share
, int share_len
,
264 char *path
, int path_len
,
265 char *user
, int user_len
,
266 char *password
, int password_len
,
267 char *options
, int options_len
)
275 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
276 if (options
!= NULL
&& options_len
> 0) {
277 options
[0] = (char)0;
281 /* see if it has the right prefix */
282 len
= strlen(smbc_prefix
);
283 if (strncmp(s
,smbc_prefix
,len
) || (s
[len
] != '/' && s
[len
] != 0)) {
284 return -1; /* What about no smb: ? */
289 /* Watch the test below, we are testing to see if we should exit */
291 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
293 DEBUG(1, ("Invalid path (does not begin with smb://"));
298 p
+= 2; /* Skip the double slash */
300 /* See if any options were specified */
301 if ((q
= strrchr(p
, '?')) != NULL
) {
302 /* There are options. Null terminate here and point to them */
305 DEBUG(4, ("Found options '%s'", q
));
307 /* Copy the options */
308 if (options
!= NULL
&& options_len
> 0) {
309 safe_strcpy(options
, q
, options_len
- 1);
318 strncpy(server
, context
->workgroup
,
319 (strlen(context
->workgroup
) < 16)?strlen(context
->workgroup
):16);
325 * ok, its for us. Now parse out the server, share etc.
327 * However, we want to parse out [[domain;]user[:password]@] if it
331 /* check that '@' occurs before '/', if '/' exists at all */
332 q
= strchr_m(p
, '@');
333 r
= strchr_m(p
, '/');
334 if (q
&& (!r
|| q
< r
)) {
335 pstring username
, passwd
, domain
;
336 const char *u
= userinfo
;
338 next_token(&p
, userinfo
, "@", sizeof(fstring
));
340 username
[0] = passwd
[0] = domain
[0] = 0;
342 if (strchr_m(u
, ';')) {
344 next_token(&u
, domain
, ";", sizeof(fstring
));
348 if (strchr_m(u
, ':')) {
350 next_token(&u
, username
, ":", sizeof(fstring
));
357 pstrcpy(username
, u
);
362 strncpy(user
, username
, user_len
); /* FIXME, domain */
365 strncpy(password
, passwd
, password_len
);
369 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
375 if (*p
== (char)0) goto decoding
; /* That's it ... */
377 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
383 safe_strcpy(path
, p
, path_len
- 1);
385 all_string_sub(path
, "/", "\\", 0);
388 (void) smbc_urldecode(path
, path
, path_len
);
389 (void) smbc_urldecode(server
, server
, server_len
);
390 (void) smbc_urldecode(share
, share
, share_len
);
391 (void) smbc_urldecode(user
, user
, user_len
);
392 (void) smbc_urldecode(password
, password
, password_len
);
398 * Verify that the options specified in a URL are valid
400 static int smbc_check_options(char *server
, char *share
, char *path
, char *options
)
402 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' path='%s' options='%s'\n", server
, share
, path
, options
));
404 /* No options at all is always ok */
405 if (! *options
) return 0;
407 /* Currently, we don't support any options. */
412 * Convert an SMB error into a UNIX error ...
414 static int smbc_errno(SMBCCTX
*context
, struct cli_state
*c
)
416 int ret
= cli_errno(c
);
418 if (cli_is_dos_error(c
)) {
422 cli_dos_error(c
, &eclass
, &ecode
);
424 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
425 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
429 status
= cli_nt_error(c
);
431 DEBUG(3,("smbc errno %s -> %d\n",
432 nt_errstr(status
), ret
));
439 * Check a server for being alive and well.
440 * returns 0 if the server is in shape. Returns 1 on error
442 * Also useable outside libsmbclient to enable external cache
443 * to do some checks too.
445 int smbc_check_server(SMBCCTX
* context
, SMBCSRV
* server
)
447 if ( send_keepalive(server
->cli
.fd
) == False
)
450 /* connection is ok */
455 * Remove a server from the cached server list it's unused.
456 * On success, 0 is returned. 1 is returned if the server could not be removed.
458 * Also useable outside libsmbclient
460 int smbc_remove_unused_server(SMBCCTX
* context
, SMBCSRV
* srv
)
464 /* are we being fooled ? */
465 if (!context
|| !context
->internal
||
466 !context
->internal
->_initialized
|| !srv
) return 1;
469 /* Check all open files/directories for a relation with this server */
470 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
471 if (file
->srv
== srv
) {
473 DEBUG(3, ("smbc_remove_usused_server: %p still used by %p.\n",
479 DLIST_REMOVE(context
->internal
->_servers
, srv
);
481 cli_shutdown(&srv
->cli
);
483 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
485 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
490 SMBCSRV
*find_server(SMBCCTX
*context
,
502 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
503 workgroup
, username
);
505 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
506 context
->callbacks
.auth_fn(server
, share
,
507 workgroup
, sizeof(fstring
),
508 username
, sizeof(fstring
),
509 password
, sizeof(fstring
));
511 * However, smbc_auth_fn may have picked up info relating to
512 * an existing connection, so try for an existing connection
516 goto check_server_cache
;
521 if (context
->callbacks
.check_server_fn(context
, srv
)) {
523 * This server is no good anymore
524 * Try to remove it and check for more possible
525 * servers in the cache
527 if (context
->callbacks
.remove_unused_server_fn(context
,
530 * We could not remove the server completely,
531 * remove it from the cache so we will not get
532 * it again. It will be removed when the last
533 * file/dir is closed.
535 context
->callbacks
.remove_cached_srv_fn(context
,
540 * Maybe there are more cached connections to this
543 goto check_server_cache
;
553 * Connect to a server, possibly on an existing connection
555 * Here, what we want to do is: If the server and username
556 * match an existing connection, reuse that, otherwise, establish a
559 * If we have to create a new connection, call the auth_fn to get the
560 * info we need, unless the username and password were passed in.
563 SMBCSRV
*smbc_server(SMBCCTX
*context
,
564 const char *server
, const char *share
,
565 fstring workgroup
, fstring username
,
570 struct nmb_name called
, calling
;
571 const char *server_n
= server
;
574 int tried_reverse
= 0;
577 const char *username_used
;
582 if (server
[0] == 0) {
587 srv
= find_server(context
, server
, share
,
588 workgroup
, username
, password
);
591 * If we found a connection and we're only allowed one share per
594 if (srv
&& *share
!= '\0' && context
->options
.one_share_per_server
) {
597 * ... then if there's no current connection to the share,
598 * connect to it. find_server(), or rather the function
599 * pointed to by context->callbacks.get_cached_srv_fn which
600 * was called by find_server(), will have issued a tree
601 * disconnect if the requested share is not the same as the
602 * one that was already connected.
604 if (srv
->cli
.cnum
== (uint16
) -1) {
605 /* Ensure we have accurate auth info */
606 context
->callbacks
.auth_fn(server
, share
,
607 workgroup
, sizeof(fstring
),
608 username
, sizeof(fstring
),
609 password
, sizeof(fstring
));
611 if (! cli_send_tconX(&srv
->cli
, share
, "?????",
612 password
, strlen(password
)+1)) {
614 errno
= smbc_errno(context
, &srv
->cli
);
615 cli_shutdown(&srv
->cli
);
616 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
620 /* Regenerate the dev value since it's based on both server and share */
622 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
627 /* If we have a connection... */
630 /* ... then we're done here. Give 'em what they came for. */
634 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
635 make_nmb_name(&called
, server
, 0x20);
637 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
639 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
642 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
646 /* have to open a new connection */
647 if (!cli_initialise(&c
)) {
652 if (context
->flags
& SMB_CTX_FLAG_USE_KERBEROS
) {
653 c
.use_kerberos
= True
;
655 if (context
->flags
& SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS
) {
656 c
.fallback_after_kerberos
= True
;
659 c
.timeout
= context
->timeout
;
662 * Force use of port 139 for first try if share is $IPC, empty, or
663 * null, so browse lists can work
665 if (share
== NULL
|| *share
== '\0' || strcmp(share
, "IPC$") == 0) {
666 port_try_first
= 139;
669 port_try_first
= 445;
673 c
.port
= port_try_first
;
675 if (!cli_connect(&c
, server_n
, &ip
)) {
677 /* First connection attempt failed. Try alternate port. */
678 c
.port
= port_try_next
;
680 if (!cli_connect(&c
, server_n
, &ip
)) {
687 if (!cli_session_request(&c
, &calling
, &called
)) {
689 if (strcmp(called
.name
, "*SMBSERVER")) {
690 make_nmb_name(&called
, "*SMBSERVER", 0x20);
693 else { /* Try one more time, but ensure we don't loop */
695 /* Only try this if server is an IP address ... */
697 if (is_ipaddress(server
) && !tried_reverse
) {
699 struct in_addr rem_ip
;
701 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
702 DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server
));
707 tried_reverse
++; /* Yuck */
709 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
710 make_nmb_name(&called
, remote_name
, 0x20);
721 DEBUG(4,(" session request ok\n"));
723 if (!cli_negprot(&c
)) {
729 username_used
= username
;
731 if (!cli_session_setup(&c
, username_used
,
732 password
, strlen(password
),
733 password
, strlen(password
),
736 /* Failed. Try an anonymous login, if allowed by flags. */
739 if ((context
->flags
& SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON
) ||
740 !cli_session_setup(&c
, username_used
,
751 DEBUG(4,(" session setup ok\n"));
753 if (!cli_send_tconX(&c
, share
, "?????",
754 password
, strlen(password
)+1)) {
755 errno
= smbc_errno(context
, &c
);
760 DEBUG(4,(" tconx ok\n"));
763 * Ok, we have got a nice connection
764 * Let's allocate a server structure.
767 srv
= SMB_MALLOC_P(SMBCSRV
);
775 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
776 srv
->no_pathinfo
= False
;
777 srv
->no_pathinfo2
= False
;
778 srv
->no_nt_session
= False
;
780 /* now add it to the cache (internal or external) */
781 /* Let the cache function set errno if it wants to */
783 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username_used
)) {
784 int saved_errno
= errno
;
785 DEBUG(3, (" Failed to add server to cache\n"));
793 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
794 server
, share
, srv
));
796 DLIST_ADD(context
->internal
->_servers
, srv
);
801 if (!srv
) return NULL
;
808 * Connect to a server for getting/setting attributes, possibly on an existing
809 * connection. This works similarly to smbc_server().
811 SMBCSRV
*smbc_attr_server(SMBCCTX
*context
,
812 const char *server
, const char *share
,
814 fstring username
, fstring password
,
818 struct cli_state
*ipc_cli
;
819 struct rpc_pipe_client
*pipe_hnd
;
821 SMBCSRV
*ipc_srv
=NULL
;
824 * See if we've already created this special connection. Reference our
825 * "special" share name '*IPC$', which is an impossible real share name
826 * due to the leading asterisk.
828 ipc_srv
= find_server(context
, server
, "*IPC$",
829 workgroup
, username
, password
);
832 /* We didn't find a cached connection. Get the password */
833 if (*password
== '\0') {
834 /* ... then retrieve it now. */
835 context
->callbacks
.auth_fn(server
, share
,
836 workgroup
, sizeof(fstring
),
837 username
, sizeof(fstring
),
838 password
, sizeof(fstring
));
842 nt_status
= cli_full_connection(&ipc_cli
,
843 global_myname(), server
,
844 &ip
, 0, "IPC$", "?????",
848 if (! NT_STATUS_IS_OK(nt_status
)) {
849 DEBUG(1,("cli_full_connection failed! (%s)\n",
850 nt_errstr(nt_status
)));
856 pipe_hnd
= cli_rpc_pipe_open_noauth(ipc_cli
, PI_LSARPC
, &nt_status
);
858 DEBUG(1, ("cli_nt_session_open fail!\n"));
860 cli_shutdown(ipc_cli
);
864 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
865 but NT sends 0x2000000 so we might as well do it too. */
867 nt_status
= rpccli_lsa_open_policy(pipe_hnd
,
870 GENERIC_EXECUTE_ACCESS
,
873 if (!NT_STATUS_IS_OK(nt_status
)) {
874 errno
= smbc_errno(context
, ipc_cli
);
875 cli_shutdown(ipc_cli
);
880 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
883 cli_shutdown(ipc_cli
);
887 ZERO_STRUCTP(ipc_srv
);
888 ipc_srv
->cli
= *ipc_cli
;
892 /* now add it to the cache (internal or external) */
894 errno
= 0; /* let cache function set errno if it likes */
895 if (context
->callbacks
.add_cached_srv_fn(context
, ipc_srv
,
900 DEBUG(3, (" Failed to add server to cache\n"));
904 cli_shutdown(&ipc_srv
->cli
);
909 DLIST_ADD(context
->internal
->_servers
, ipc_srv
);
916 * Routine to open() a file ...
919 static SMBCFILE
*smbc_open_ctx(SMBCCTX
*context
, const char *fname
, int flags
, mode_t mode
)
921 fstring server
, share
, user
, password
, workgroup
;
924 struct cli_state
*targetcli
;
926 SMBCFILE
*file
= NULL
;
929 if (!context
|| !context
->internal
||
930 !context
->internal
->_initialized
) {
932 errno
= EINVAL
; /* Best I can think of ... */
944 if (smbc_parse_path(context
, fname
,
945 server
, sizeof(server
),
946 share
, sizeof(share
),
949 password
, sizeof(password
),
955 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
957 fstrcpy(workgroup
, context
->workgroup
);
959 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
963 if (errno
== EPERM
) errno
= EACCES
;
964 return NULL
; /* smbc_server sets errno */
968 /* Hmmm, the test for a directory is suspect here ... FIXME */
970 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
977 file
= SMB_MALLOC_P(SMBCFILE
);
988 /*d_printf(">>>open: resolving %s\n", path);*/
989 if (!cli_resolve_path( "", &srv
->cli
, path
, &targetcli
, targetpath
))
991 d_printf("Could not resolve %s\n", path
);
994 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
996 if ( targetcli
->dfsroot
)
999 pstrcpy(temppath
, targetpath
);
1000 cli_dfs_make_full_path( targetpath
, targetcli
->desthost
, targetcli
->share
, temppath
);
1003 if ((fd
= cli_open(targetcli
, targetpath
, flags
, DENY_NONE
)) < 0) {
1005 /* Handle the error ... */
1008 errno
= smbc_errno(context
, targetcli
);
1013 /* Fill in file struct */
1016 file
->fname
= SMB_STRDUP(fname
);
1021 DLIST_ADD(context
->internal
->_files
, file
);
1024 * If the file was opened in O_APPEND mode, all write
1025 * operations should be appended to the file. To do that,
1026 * though, using this protocol, would require a getattrE()
1027 * call for each and every write, to determine where the end
1028 * of the file is. (There does not appear to be an append flag
1029 * in the protocol.) Rather than add all of that overhead of
1030 * retrieving the current end-of-file offset prior to each
1031 * write operation, we'll assume that most append operations
1032 * will continuously write, so we'll just set the offset to
1033 * the end of the file now and hope that's adequate.
1035 * Note to self: If this proves inadequate, and O_APPEND
1036 * should, in some cases, be forced for each write, add a
1037 * field in the context options structure, for
1038 * "strict_append_mode" which would select between the current
1039 * behavior (if FALSE) or issuing a getattrE() prior to each
1040 * write and forcing the write to the end of the file (if
1041 * TRUE). Adding that capability will likely require adding
1042 * an "append" flag into the _SMBCFILE structure to track
1043 * whether a file was opened in O_APPEND mode. -- djl
1045 if (flags
& O_APPEND
) {
1046 if (smbc_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
1047 (void) smbc_close_ctx(context
, file
);
1057 /* Check if opendir needed ... */
1062 eno
= smbc_errno(context
, &srv
->cli
);
1063 file
= context
->opendir(context
, fname
);
1064 if (!file
) errno
= eno
;
1069 errno
= EINVAL
; /* FIXME, correct errno ? */
1075 * Routine to create a file
1078 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
1080 static SMBCFILE
*smbc_creat_ctx(SMBCCTX
*context
, const char *path
, mode_t mode
)
1083 if (!context
|| !context
->internal
||
1084 !context
->internal
->_initialized
) {
1091 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
1095 * Routine to read() a file ...
1098 static ssize_t
smbc_read_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
1101 fstring server
, share
, user
, password
;
1102 pstring path
, targetpath
;
1103 struct cli_state
*targetcli
;
1108 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1109 * appears to pass file->offset (which is type off_t) differently than
1110 * a local variable of type off_t. Using local variable "offset" in
1111 * the call to cli_read() instead of file->offset fixes a problem
1112 * retrieving data at an offset greater than 4GB.
1114 off_t offset
= file
->offset
;
1116 if (!context
|| !context
->internal
||
1117 !context
->internal
->_initialized
) {
1124 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
1126 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1133 /* Check that the buffer exists ... */
1142 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1143 if (smbc_parse_path(context
, file
->fname
,
1144 server
, sizeof(server
),
1145 share
, sizeof(share
),
1148 password
, sizeof(password
),
1154 /*d_printf(">>>read: resolving %s\n", path);*/
1155 if (!cli_resolve_path( "", &file
->srv
->cli
, path
, &targetcli
, targetpath
))
1157 d_printf("Could not resolve %s\n", path
);
1160 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1162 ret
= cli_read(targetcli
, file
->cli_fd
, buf
, offset
, count
);
1166 errno
= smbc_errno(context
, targetcli
);
1171 file
->offset
+= ret
;
1173 DEBUG(4, (" --> %d\n", ret
));
1175 return ret
; /* Success, ret bytes of data ... */
1180 * Routine to write() a file ...
1183 static ssize_t
smbc_write_ctx(SMBCCTX
*context
, SMBCFILE
*file
, void *buf
, size_t count
)
1186 off_t offset
= file
->offset
; /* See "offset" comment in smbc_read_ctx() */
1187 fstring server
, share
, user
, password
;
1188 pstring path
, targetpath
;
1189 struct cli_state
*targetcli
;
1191 if (!context
|| !context
->internal
||
1192 !context
->internal
->_initialized
) {
1199 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1206 /* Check that the buffer exists ... */
1215 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1216 if (smbc_parse_path(context
, file
->fname
,
1217 server
, sizeof(server
),
1218 share
, sizeof(share
),
1221 password
, sizeof(password
),
1227 /*d_printf(">>>write: resolving %s\n", path);*/
1228 if (!cli_resolve_path( "", &file
->srv
->cli
, path
, &targetcli
, targetpath
))
1230 d_printf("Could not resolve %s\n", path
);
1233 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1236 ret
= cli_write(targetcli
, file
->cli_fd
, 0, buf
, offset
, count
);
1240 errno
= smbc_errno(context
, targetcli
);
1245 file
->offset
+= ret
;
1247 return ret
; /* Success, 0 bytes of data ... */
1251 * Routine to close() a file ...
1254 static int smbc_close_ctx(SMBCCTX
*context
, SMBCFILE
*file
)
1257 fstring server
, share
, user
, password
;
1258 pstring path
, targetpath
;
1259 struct cli_state
*targetcli
;
1261 if (!context
|| !context
->internal
||
1262 !context
->internal
->_initialized
) {
1269 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1279 return context
->closedir(context
, file
);
1283 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1284 if (smbc_parse_path(context
, file
->fname
,
1285 server
, sizeof(server
),
1286 share
, sizeof(share
),
1289 password
, sizeof(password
),
1295 /*d_printf(">>>close: resolving %s\n", path);*/
1296 if (!cli_resolve_path( "", &file
->srv
->cli
, path
, &targetcli
, targetpath
))
1298 d_printf("Could not resolve %s\n", path
);
1301 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1303 if (!cli_close(targetcli
, file
->cli_fd
)) {
1305 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1307 /* Deallocate slot and remove the server
1308 * from the server cache if unused */
1309 errno
= smbc_errno(context
, targetcli
);
1311 DLIST_REMOVE(context
->internal
->_files
, file
);
1312 SAFE_FREE(file
->fname
);
1314 context
->callbacks
.remove_unused_server_fn(context
, srv
);
1320 DLIST_REMOVE(context
->internal
->_files
, file
);
1321 SAFE_FREE(file
->fname
);
1328 * Get info from an SMB server on a file. Use a qpathinfo call first
1329 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1331 static BOOL
smbc_getatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
1332 uint16
*mode
, SMB_OFF_T
*size
,
1333 time_t *c_time
, time_t *a_time
, time_t *m_time
,
1338 struct cli_state
*targetcli
;
1339 if (!context
|| !context
->internal
||
1340 !context
->internal
->_initialized
) {
1347 /* path fixup for . and .. */
1348 if (strequal(path
, ".") || strequal(path
, ".."))
1349 pstrcpy(fixedpath
, "\\");
1352 pstrcpy(fixedpath
, path
);
1353 trim_string(fixedpath
, NULL
, "\\..");
1354 trim_string(fixedpath
, NULL
, "\\.");
1356 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1358 if (!cli_resolve_path( "", &srv
->cli
, fixedpath
, &targetcli
, targetpath
))
1360 d_printf("Couldn't resolve %s\n", path
);
1364 if ( targetcli
->dfsroot
)
1367 pstrcpy(temppath
, targetpath
);
1368 cli_dfs_make_full_path( targetpath
, targetcli
->desthost
, targetcli
->share
, temppath
);
1371 if (!srv
->no_pathinfo2
&&
1372 cli_qpathinfo2(targetcli
, targetpath
, c_time
, a_time
, m_time
, NULL
,
1373 size
, mode
, ino
)) return True
;
1375 /* if this is NT then don't bother with the getatr */
1376 if (targetcli
->capabilities
& CAP_NT_SMBS
) {
1381 if (cli_getatr(targetcli
, targetpath
, mode
, size
, m_time
)) {
1382 if (m_time
!= NULL
) {
1383 if (a_time
!= NULL
) *a_time
= *m_time
;
1384 if (c_time
!= NULL
) *c_time
= *m_time
;
1386 srv
->no_pathinfo2
= True
;
1396 * Set file info on an SMB server. Use setpathinfo call first. If that
1397 * fails, use setattrE..
1399 * Access and modification time parameters are always used and must be
1400 * provided. Create time, if zero, will be determined from the actual create
1401 * time of the file. If non-zero, the create time will be set as well.
1403 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1405 static BOOL
smbc_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
1406 time_t c_time
, time_t a_time
, time_t m_time
,
1413 * Get the create time of the file (if not provided); we'll need it in
1416 if (! srv
->no_pathinfo
&& c_time
== 0) {
1417 if (! cli_qpathinfo(&srv
->cli
, path
,
1418 &c_time
, NULL
, NULL
, NULL
, NULL
)) {
1419 /* qpathinfo not available */
1420 srv
->no_pathinfo
= True
;
1423 * We got a creation time. Some OS versions don't
1424 * return a valid create time, though. If we got an
1425 * invalid time, start with the current time instead.
1427 if (c_time
== 0 || c_time
== (time_t) -1) {
1428 c_time
= time(NULL
);
1432 * We got a creation time. For sanity sake, since
1433 * there is no POSIX function to set the create time
1434 * of a file, if the existing create time is greater
1435 * than either of access time or modification time,
1436 * set create time to the smallest of those. This
1437 * ensure that the create time of a file is never
1438 * greater than its last access or modification time.
1440 if (c_time
> a_time
) c_time
= a_time
;
1441 if (c_time
> m_time
) c_time
= m_time
;
1446 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1447 * modern function for "new code" to be using, and it works given a
1448 * filename rather than requiring that the file be opened to have its
1449 * attributes manipulated.
1451 if (srv
->no_pathinfo
||
1452 ! cli_setpathinfo(&srv
->cli
, path
, c_time
, a_time
, m_time
, mode
)) {
1455 * setpathinfo is not supported; go to plan B.
1457 * cli_setatr() does not work on win98, and it also doesn't
1458 * support setting the access time (only the modification
1459 * time), so in all cases, we open the specified file and use
1460 * cli_setattrE() which should work on all OS versions, and
1461 * supports both times.
1464 /* Don't try {q,set}pathinfo() again, with this server */
1465 srv
->no_pathinfo
= True
;
1468 if ((fd
= cli_open(&srv
->cli
, path
, O_RDWR
, DENY_NONE
)) < 0) {
1470 errno
= smbc_errno(context
, &srv
->cli
);
1475 * Get the creat time of the file (if it wasn't provided).
1476 * We'll need it in the set call
1479 ret
= cli_getattrE(&srv
->cli
, fd
,
1481 &c_time
, NULL
, NULL
);
1486 /* If we got create time, set times */
1488 /* Some OS versions don't support create time */
1489 if (c_time
== 0 || c_time
== -1) {
1490 c_time
= time(NULL
);
1494 * For sanity sake, since there is no POSIX function
1495 * to set the create time of a file, if the existing
1496 * create time is greater than either of access time
1497 * or modification time, set create time to the
1498 * smallest of those. This ensure that the create
1499 * time of a file is never greater than its last
1500 * access or modification time.
1502 if (c_time
> a_time
) c_time
= a_time
;
1503 if (c_time
> m_time
) c_time
= m_time
;
1505 /* Set the new attributes */
1506 ret
= cli_setattrE(&srv
->cli
, fd
,
1507 c_time
, a_time
, m_time
);
1508 cli_close(&srv
->cli
, fd
);
1512 * Unfortunately, setattrE() doesn't have a provision for
1513 * setting the access mode (attributes). We'll have to try
1514 * cli_setatr() for that, and with only this parameter, it
1515 * seems to work on win98.
1517 if (ret
&& mode
!= (uint16
) -1) {
1518 ret
= cli_setatr(&srv
->cli
, path
, mode
, 0);
1522 errno
= smbc_errno(context
, &srv
->cli
);
1531 * Routine to unlink() a file
1534 static int smbc_unlink_ctx(SMBCCTX
*context
, const char *fname
)
1536 fstring server
, share
, user
, password
, workgroup
;
1537 pstring path
, targetpath
;
1538 struct cli_state
*targetcli
;
1539 SMBCSRV
*srv
= NULL
;
1541 if (!context
|| !context
->internal
||
1542 !context
->internal
->_initialized
) {
1544 errno
= EINVAL
; /* Best I can think of ... */
1556 if (smbc_parse_path(context
, fname
,
1557 server
, sizeof(server
),
1558 share
, sizeof(share
),
1561 password
, sizeof(password
),
1567 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1569 fstrcpy(workgroup
, context
->workgroup
);
1571 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1575 return -1; /* smbc_server sets errno */
1579 /*d_printf(">>>unlink: resolving %s\n", path);*/
1580 if (!cli_resolve_path( "", &srv
->cli
, path
, &targetcli
, targetpath
))
1582 d_printf("Could not resolve %s\n", path
);
1585 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1587 if (!cli_unlink(targetcli
, targetpath
)) {
1589 errno
= smbc_errno(context
, targetcli
);
1591 if (errno
== EACCES
) { /* Check if the file is a directory */
1596 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1599 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1600 &c_time
, &a_time
, &m_time
, &ino
)) {
1602 /* Hmmm, bad error ... What? */
1604 errno
= smbc_errno(context
, targetcli
);
1610 if (IS_DOS_DIR(mode
))
1613 errno
= saverr
; /* Restore this */
1622 return 0; /* Success ... */
1627 * Routine to rename() a file
1630 static int smbc_rename_ctx(SMBCCTX
*ocontext
, const char *oname
,
1631 SMBCCTX
*ncontext
, const char *nname
)
1633 fstring server1
, share1
, server2
, share2
, user1
, user2
, password1
, password2
, workgroup
;
1634 pstring path1
, path2
, targetpath1
, targetpath2
;
1635 struct cli_state
*targetcli1
, *targetcli2
;
1636 SMBCSRV
*srv
= NULL
;
1638 if (!ocontext
|| !ncontext
||
1639 !ocontext
->internal
|| !ncontext
->internal
||
1640 !ocontext
->internal
->_initialized
||
1641 !ncontext
->internal
->_initialized
) {
1643 errno
= EINVAL
; /* Best I can think of ... */
1648 if (!oname
|| !nname
) {
1655 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1657 smbc_parse_path(ocontext
, oname
,
1658 server1
, sizeof(server1
),
1659 share1
, sizeof(share1
),
1660 path1
, sizeof(path1
),
1661 user1
, sizeof(user1
),
1662 password1
, sizeof(password1
),
1665 if (user1
[0] == (char)0) fstrcpy(user1
, ocontext
->user
);
1667 smbc_parse_path(ncontext
, nname
,
1668 server2
, sizeof(server2
),
1669 share2
, sizeof(share2
),
1670 path2
, sizeof(path2
),
1671 user2
, sizeof(user2
),
1672 password2
, sizeof(password2
),
1675 if (user2
[0] == (char)0) fstrcpy(user2
, ncontext
->user
);
1677 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1678 strcmp(user1
, user2
)) {
1680 /* Can't rename across file systems, or users?? */
1687 fstrcpy(workgroup
, ocontext
->workgroup
);
1688 /* HELP !!! Which workgroup should I use ? Or are they always the same -- Tom */
1689 srv
= smbc_server(ocontext
, server1
, share1
, workgroup
, user1
, password1
);
1696 /*d_printf(">>>rename: resolving %s\n", path1);*/
1697 if (!cli_resolve_path( "", &srv
->cli
, path1
, &targetcli1
, targetpath1
))
1699 d_printf("Could not resolve %s\n", path1
);
1702 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1703 /*d_printf(">>>rename: resolving %s\n", path2);*/
1704 if (!cli_resolve_path( "", &srv
->cli
, path2
, &targetcli2
, targetpath2
))
1706 d_printf("Could not resolve %s\n", path2
);
1709 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1711 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) || strcmp(targetcli1
->share
, targetcli2
->share
))
1713 /* can't rename across file systems */
1719 if (!cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1720 int eno
= smbc_errno(ocontext
, targetcli1
);
1722 if (eno
!= EEXIST
||
1723 !cli_unlink(targetcli1
, targetpath2
) ||
1724 !cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1732 return 0; /* Success */
1737 * A routine to lseek() a file
1740 static off_t
smbc_lseek_ctx(SMBCCTX
*context
, SMBCFILE
*file
, off_t offset
, int whence
)
1743 fstring server
, share
, user
, password
;
1744 pstring path
, targetpath
;
1745 struct cli_state
*targetcli
;
1747 if (!context
|| !context
->internal
||
1748 !context
->internal
->_initialized
) {
1755 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1765 return -1; /* Can't lseek a dir ... */
1771 file
->offset
= offset
;
1775 file
->offset
+= offset
;
1779 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1780 if (smbc_parse_path(context
, file
->fname
,
1781 server
, sizeof(server
),
1782 share
, sizeof(share
),
1785 password
, sizeof(password
),
1791 /*d_printf(">>>lseek: resolving %s\n", path);*/
1792 if (!cli_resolve_path( "", &file
->srv
->cli
, path
, &targetcli
, targetpath
))
1794 d_printf("Could not resolve %s\n", path
);
1797 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1799 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, NULL
, &size
, NULL
, NULL
,
1802 SMB_OFF_T b_size
= size
;
1803 if (!cli_getattrE(targetcli
, file
->cli_fd
, NULL
, &b_size
, NULL
, NULL
,
1811 file
->offset
= size
+ offset
;
1820 return file
->offset
;
1825 * Generate an inode number from file name for those things that need it
1829 ino_t
smbc_inode(SMBCCTX
*context
, const char *name
)
1832 if (!context
|| !context
->internal
||
1833 !context
->internal
->_initialized
) {
1840 if (!*name
) return 2; /* FIXME, why 2 ??? */
1841 return (ino_t
)str_checksum(name
);
1846 * Routine to put basic stat info into a stat structure ... Used by stat and
1851 int smbc_setup_stat(SMBCCTX
*context
, struct stat
*st
, char *fname
,
1852 SMB_OFF_T size
, int mode
)
1857 if (IS_DOS_DIR(mode
)) {
1858 st
->st_mode
= SMBC_DIR_MODE
;
1860 st
->st_mode
= SMBC_FILE_MODE
;
1863 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
1864 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
1865 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
1866 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
1869 #ifdef HAVE_STAT_ST_BLKSIZE
1870 st
->st_blksize
= 512;
1872 #ifdef HAVE_STAT_ST_BLOCKS
1873 st
->st_blocks
= (size
+511)/512;
1875 st
->st_uid
= getuid();
1876 st
->st_gid
= getgid();
1878 if (IS_DOS_DIR(mode
)) {
1884 if (st
->st_ino
== 0) {
1885 st
->st_ino
= smbc_inode(context
, fname
);
1888 return True
; /* FIXME: Is this needed ? */
1893 * Routine to stat a file given a name
1896 static int smbc_stat_ctx(SMBCCTX
*context
, const char *fname
, struct stat
*st
)
1899 fstring server
, share
, user
, password
, workgroup
;
1901 time_t m_time
= 0, a_time
= 0, c_time
= 0;
1906 if (!context
|| !context
->internal
||
1907 !context
->internal
->_initialized
) {
1909 errno
= EINVAL
; /* Best I can think of ... */
1921 DEBUG(4, ("smbc_stat(%s)\n", fname
));
1923 if (smbc_parse_path(context
, fname
,
1924 server
, sizeof(server
),
1925 share
, sizeof(share
),
1928 password
, sizeof(password
),
1934 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1936 fstrcpy(workgroup
, context
->workgroup
);
1938 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
1941 return -1; /* errno set by smbc_server */
1944 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1945 &c_time
, &a_time
, &m_time
, &ino
)) {
1947 errno
= smbc_errno(context
, &srv
->cli
);
1954 smbc_setup_stat(context
, st
, path
, size
, mode
);
1956 st
->st_atime
= a_time
;
1957 st
->st_ctime
= c_time
;
1958 st
->st_mtime
= m_time
;
1959 st
->st_dev
= srv
->dev
;
1966 * Routine to stat a file given an fd
1969 static int smbc_fstat_ctx(SMBCCTX
*context
, SMBCFILE
*file
, struct stat
*st
)
1971 time_t c_time
, a_time
, m_time
;
1974 fstring server
, share
, user
, password
;
1975 pstring path
, targetpath
;
1976 struct cli_state
*targetcli
;
1979 if (!context
|| !context
->internal
||
1980 !context
->internal
->_initialized
) {
1987 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1996 return context
->fstatdir(context
, file
, st
);
2000 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2001 if (smbc_parse_path(context
, file
->fname
,
2002 server
, sizeof(server
),
2003 share
, sizeof(share
),
2006 password
, sizeof(password
),
2012 /*d_printf(">>>fstat: resolving %s\n", path);*/
2013 if (!cli_resolve_path( "", &file
->srv
->cli
, path
, &targetcli
, targetpath
))
2015 d_printf("Could not resolve %s\n", path
);
2018 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2020 if (!cli_qfileinfo(targetcli
, file
->cli_fd
,
2021 &mode
, &size
, &c_time
, &a_time
, &m_time
, NULL
, &ino
)) {
2022 if (!cli_getattrE(targetcli
, file
->cli_fd
,
2023 &mode
, &size
, &c_time
, &a_time
, &m_time
)) {
2032 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
2034 st
->st_atime
= a_time
;
2035 st
->st_ctime
= c_time
;
2036 st
->st_mtime
= m_time
;
2037 st
->st_dev
= file
->srv
->dev
;
2044 * Routine to open a directory
2045 * We accept the URL syntax explained in smbc_parse_path(), above.
2048 static void smbc_remove_dir(SMBCFILE
*dir
)
2050 struct smbc_dir_list
*d
,*f
;
2057 SAFE_FREE(f
->dirent
);
2062 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
2066 static int add_dirent(SMBCFILE
*dir
, const char *name
, const char *comment
, uint32 type
)
2068 struct smbc_dirent
*dirent
;
2070 int name_length
= (name
== NULL
? 0 : strlen(name
));
2071 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
2074 * Allocate space for the dirent, which must be increased by the
2075 * size of the name and the comment and 1 each for the null terminator.
2078 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
2080 dirent
= SMB_MALLOC(size
);
2084 dir
->dir_error
= ENOMEM
;
2089 ZERO_STRUCTP(dirent
);
2091 if (dir
->dir_list
== NULL
) {
2093 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
2094 if (!dir
->dir_list
) {
2097 dir
->dir_error
= ENOMEM
;
2101 ZERO_STRUCTP(dir
->dir_list
);
2103 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
2107 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
2109 if (!dir
->dir_end
->next
) {
2112 dir
->dir_error
= ENOMEM
;
2116 ZERO_STRUCTP(dir
->dir_end
->next
);
2118 dir
->dir_end
= dir
->dir_end
->next
;
2121 dir
->dir_end
->next
= NULL
;
2122 dir
->dir_end
->dirent
= dirent
;
2124 dirent
->smbc_type
= type
;
2125 dirent
->namelen
= name_length
;
2126 dirent
->commentlen
= comment_len
;
2127 dirent
->dirlen
= size
;
2129 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
2131 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
2132 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
2139 list_unique_wg_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
2141 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2142 struct smbc_dir_list
*dir_list
;
2143 struct smbc_dirent
*dirent
;
2147 dirent_type
= dir
->dir_type
;
2149 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2151 /* An error occurred, what do we do? */
2152 /* FIXME: Add some code here */
2155 /* Point to the one just added */
2156 dirent
= dir
->dir_end
->dirent
;
2158 /* See if this was a duplicate */
2159 for (dir_list
= dir
->dir_list
;
2160 dir_list
!= dir
->dir_end
;
2161 dir_list
= dir_list
->next
) {
2163 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
2164 /* Duplicate. End end of list need to be removed. */
2168 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
2169 /* Found the end of the list. Remove it. */
2170 dir
->dir_end
= dir_list
;
2171 free(dir_list
->next
);
2172 dir_list
->next
= NULL
;
2179 list_fn(const char *name
, uint32 type
, const char *comment
, void *state
)
2181 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2184 /* We need to process the type a little ... */
2186 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
2189 case 0: /* Directory tree */
2190 dirent_type
= SMBC_FILE_SHARE
;
2194 dirent_type
= SMBC_PRINTER_SHARE
;
2198 dirent_type
= SMBC_COMMS_SHARE
;
2202 dirent_type
= SMBC_IPC_SHARE
;
2206 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
2210 else dirent_type
= dir
->dir_type
;
2212 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2214 /* An error occurred, what do we do? */
2215 /* FIXME: Add some code here */
2221 dir_list_fn(const char *mnt
, file_info
*finfo
, const char *mask
, void *state
)
2224 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
2225 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
2227 /* Handle an error ... */
2229 /* FIXME: Add some code ... */
2235 static SMBCFILE
*smbc_opendir_ctx(SMBCCTX
*context
, const char *fname
)
2237 fstring server
, share
, user
, password
, options
;
2242 SMBCSRV
*srv
= NULL
;
2243 SMBCFILE
*dir
= NULL
;
2244 struct in_addr rem_ip
;
2246 if (!context
|| !context
->internal
||
2247 !context
->internal
->_initialized
) {
2248 DEBUG(4, ("no valid context\n"));
2249 errno
= EINVAL
+ 8192;
2255 DEBUG(4, ("no valid fname\n"));
2256 errno
= EINVAL
+ 8193;
2260 if (smbc_parse_path(context
, fname
,
2261 server
, sizeof(server
),
2262 share
, sizeof(share
),
2265 password
, sizeof(password
),
2266 options
, sizeof(options
))) {
2267 DEBUG(4, ("no valid path\n"));
2268 errno
= EINVAL
+ 8194;
2272 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' path='%s' options='%s'\n", fname
, server
, share
, path
, options
));
2274 /* Ensure the options are valid */
2275 if (smbc_check_options(server
, share
, path
, options
)) {
2276 DEBUG(4, ("unacceptable options (%s)\n", options
));
2277 errno
= EINVAL
+ 8195;
2281 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2283 pstrcpy(workgroup
, context
->workgroup
);
2285 dir
= SMB_MALLOC_P(SMBCFILE
);
2297 dir
->fname
= SMB_STRDUP(fname
);
2301 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
2303 if (server
[0] == (char)0) {
2308 struct ip_service
*ip_list
;
2309 struct ip_service server_addr
;
2310 struct user_auth_info u_info
;
2311 struct cli_state
*cli
;
2313 if (share
[0] != (char)0 || path
[0] != (char)0) {
2315 errno
= EINVAL
+ 8196;
2317 SAFE_FREE(dir
->fname
);
2323 /* Determine how many local master browsers to query */
2324 max_lmb_count
= (context
->options
.browse_max_lmb_count
== 0
2326 : context
->options
.browse_max_lmb_count
);
2328 pstrcpy(u_info
.username
, user
);
2329 pstrcpy(u_info
.password
, password
);
2332 * We have server and share and path empty but options
2333 * requesting that we scan all master browsers for their list
2334 * of workgroups/domains. This implies that we must first try
2335 * broadcast queries to find all master browsers, and if that
2336 * doesn't work, then try our other methods which return only
2337 * a single master browser.
2340 if (!name_resolve_bcast(MSBROWSE
, 1, &ip_list
, &count
)) {
2341 if (!find_master_ip(workgroup
, &server_addr
.ip
)) {
2347 ip_list
= &server_addr
;
2351 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
2352 DEBUG(99, ("Found master browser %d of %d: %s\n", i
+1, MAX(count
, max_lmb_count
), inet_ntoa(ip_list
[i
].ip
)));
2354 cli
= get_ipc_connect_master_ip(&ip_list
[i
], workgroup
, &u_info
);
2355 /* cli == NULL is the master browser refused to talk or
2356 could not be found */
2360 fstrcpy(server
, cli
->desthost
);
2363 DEBUG(4, ("using workgroup %s %s\n", workgroup
, server
));
2366 * For each returned master browser IP address, get a
2367 * connection to IPC$ on the server if we do not
2368 * already have one, and determine the
2369 * workgroups/domains that it knows about.
2372 srv
= smbc_server(context
, server
,
2373 "IPC$", workgroup
, user
, password
);
2379 dir
->dir_type
= SMBC_WORKGROUP
;
2381 /* Now, list the stuff ... */
2383 if (!cli_NetServerEnum(&srv
->cli
, workgroup
, SV_TYPE_DOMAIN_ENUM
, list_unique_wg_fn
,
2391 * Server not an empty string ... Check the rest and see what
2394 if (share
[0] == (char)0) {
2396 if (path
[0] != (char)0) { /* Should not have empty share with path */
2398 errno
= EINVAL
+ 8197;
2400 SAFE_FREE(dir
->fname
);
2407 /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
2408 /* However, we check to see if <server> is an IP address first */
2410 if (!is_ipaddress(server
) && /* Not an IP addr so check next */
2411 (resolve_name(server
, &rem_ip
, 0x1d) || /* Found LMB */
2412 resolve_name(server
, &rem_ip
, 0x1b) )) { /* Found DMB */
2415 dir
->dir_type
= SMBC_SERVER
;
2418 * Get the backup list ...
2422 if (!name_status_find(server
, 0, 0, rem_ip
, buserver
)) {
2424 DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server
));
2425 errno
= EPERM
; /* FIXME, is this correct */
2431 * Get a connection to IPC$ on the server if we do not already have one
2434 srv
= smbc_server(context
, buserver
, "IPC$", workgroup
, user
, password
);
2437 DEBUG(0, ("got no contact to IPC$\n"));
2439 SAFE_FREE(dir
->fname
);
2448 /* Now, list the servers ... */
2450 if (!cli_NetServerEnum(&srv
->cli
, server
, 0x0000FFFE, list_fn
,
2454 SAFE_FREE(dir
->fname
);
2463 if (resolve_name(server
, &rem_ip
, 0x20)) {
2465 /* Now, list the shares ... */
2467 dir
->dir_type
= SMBC_FILE_SHARE
;
2469 srv
= smbc_server(context
, server
, "IPC$", workgroup
, user
, password
);
2474 SAFE_FREE(dir
->fname
);
2483 /* Now, list the servers ... */
2485 if (cli_RNetShareEnum(&srv
->cli
, list_fn
,
2488 errno
= cli_errno(&srv
->cli
);
2490 SAFE_FREE(dir
->fname
);
2500 errno
= ECONNREFUSED
; /* Neither the workgroup nor server exists */
2502 SAFE_FREE(dir
->fname
);
2512 else { /* The server and share are specified ... work from there ... */
2514 struct cli_state
*targetcli
;
2516 /* Well, we connect to the server and list the directory */
2518 dir
->dir_type
= SMBC_FILE_SHARE
;
2520 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2525 SAFE_FREE(dir
->fname
);
2534 /* Now, list the files ... */
2536 p
= path
+ strlen(path
);
2537 pstrcat(path
, "\\*");
2539 if (!cli_resolve_path( "", &srv
->cli
, path
, &targetcli
, targetpath
))
2541 d_printf("Could not resolve %s\n", path
);
2545 if (cli_list(targetcli
, targetpath
, aDIR
| aSYSTEM
| aHIDDEN
, dir_list_fn
,
2549 SAFE_FREE(dir
->fname
);
2552 errno
= smbc_errno(context
, targetcli
);
2554 if (errno
== EINVAL
) {
2556 * See if they asked to opendir something
2557 * other than a directory. If so, the
2558 * converted error value we got would have
2559 * been EINVAL rather than ENOTDIR.
2561 *p
= '\0'; /* restore original path */
2563 if (smbc_getatr(context
, srv
, path
,
2567 ! IS_DOS_DIR(mode
)) {
2569 /* It is. Correct the error value */
2581 DLIST_ADD(context
->internal
->_files
, dir
);
2587 * Routine to close a directory
2590 static int smbc_closedir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2593 if (!context
|| !context
->internal
||
2594 !context
->internal
->_initialized
) {
2601 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2608 smbc_remove_dir(dir
); /* Clean it up */
2610 DLIST_REMOVE(context
->internal
->_files
, dir
);
2614 SAFE_FREE(dir
->fname
);
2615 SAFE_FREE(dir
); /* Free the space too */
2622 static void smbc_readdir_internal(SMBCCTX
* context
,
2623 struct smbc_dirent
*dest
,
2624 struct smbc_dirent
*src
,
2625 int max_namebuf_len
)
2627 if (context
->options
.urlencode_readdir_entries
) {
2629 /* url-encode the name. get back remaining buffer space */
2631 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
2633 /* We now know the name length */
2634 dest
->namelen
= strlen(dest
->name
);
2636 /* Save the pointer to the beginning of the comment */
2637 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
2639 /* Copy the comment */
2640 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
);
2642 /* Ensure the comment is null terminated */
2643 if (max_namebuf_len
> src
->commentlen
) {
2644 dest
->comment
[src
->commentlen
] = '\0';
2646 dest
->comment
[max_namebuf_len
- 1] = '\0';
2649 /* Save other fields */
2650 dest
->smbc_type
= src
->smbc_type
;
2651 dest
->commentlen
= strlen(dest
->comment
);
2652 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
2656 /* No encoding. Just copy the entry as is. */
2657 memcpy(dest
, src
, src
->dirlen
);
2658 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
2664 * Routine to get a directory entry
2667 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
2670 struct smbc_dirent
*dirp
, *dirent
;
2672 /* Check that all is ok first ... */
2674 if (!context
|| !context
->internal
||
2675 !context
->internal
->_initialized
) {
2678 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
2683 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2686 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
2691 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2694 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
2699 if (!dir
->dir_next
) {
2703 dirent
= dir
->dir_next
->dirent
;
2711 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
2712 maxlen
= (sizeof(context
->internal
->_dirent
) -
2713 sizeof(struct smbc_dirent
));
2715 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
2717 dir
->dir_next
= dir
->dir_next
->next
;
2723 * Routine to get directory entries
2726 static int smbc_getdents_ctx(SMBCCTX
*context
,
2728 struct smbc_dirent
*dirp
,
2734 char *ndir
= (char *)dirp
;
2735 struct smbc_dir_list
*dirlist
;
2737 /* Check that all is ok first ... */
2739 if (!context
|| !context
->internal
||
2740 !context
->internal
->_initialized
) {
2747 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
2754 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
2762 * Now, retrieve the number of entries that will fit in what was passed
2763 * We have to figure out if the info is in the list, or we need to
2764 * send a request to the server to get the info.
2767 while ((dirlist
= dir
->dir_next
)) {
2768 struct smbc_dirent
*dirent
;
2770 if (!dirlist
->dirent
) {
2772 errno
= ENOENT
; /* Bad error */
2777 /* Do urlencoding of next entry, if so selected */
2778 dirent
= (struct smbc_dirent
*)context
->internal
->_dirent
;
2779 maxlen
= (sizeof(context
->internal
->_dirent
) -
2780 sizeof(struct smbc_dirent
));
2781 smbc_readdir_internal(context
, dirent
, dirlist
->dirent
, maxlen
);
2783 reqd
= dirent
->dirlen
;
2787 if (rem
< count
) { /* We managed to copy something */
2793 else { /* Nothing copied ... */
2795 errno
= EINVAL
; /* Not enough space ... */
2802 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
2804 ((struct smbc_dirent
*)ndir
)->comment
=
2805 (char *)(&((struct smbc_dirent
*)ndir
)->name
+
2813 dir
->dir_next
= dirlist
= dirlist
-> next
;
2824 * Routine to create a directory ...
2827 static int smbc_mkdir_ctx(SMBCCTX
*context
, const char *fname
, mode_t mode
)
2830 fstring server
, share
, user
, password
, workgroup
;
2831 pstring path
, targetpath
;
2832 struct cli_state
*targetcli
;
2834 if (!context
|| !context
->internal
||
2835 !context
->internal
->_initialized
) {
2849 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
2851 if (smbc_parse_path(context
, fname
,
2852 server
, sizeof(server
),
2853 share
, sizeof(share
),
2856 password
, sizeof(password
),
2862 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2864 fstrcpy(workgroup
, context
->workgroup
);
2866 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2870 return -1; /* errno set by smbc_server */
2874 /*d_printf(">>>mkdir: resolving %s\n", path);*/
2875 if (!cli_resolve_path( "", &srv
->cli
, path
, &targetcli
, targetpath
))
2877 d_printf("Could not resolve %s\n", path
);
2880 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
2882 if (!cli_mkdir(targetcli
, targetpath
)) {
2884 errno
= smbc_errno(context
, targetcli
);
2894 * Our list function simply checks to see if a directory is not empty
2897 static int smbc_rmdir_dirempty
= True
;
2899 static void rmdir_list_fn(const char *mnt
, file_info
*finfo
, const char *mask
, void *state
)
2902 if (strncmp(finfo
->name
, ".", 1) != 0 && strncmp(finfo
->name
, "..", 2) != 0)
2903 smbc_rmdir_dirempty
= False
;
2908 * Routine to remove a directory
2911 static int smbc_rmdir_ctx(SMBCCTX
*context
, const char *fname
)
2914 fstring server
, share
, user
, password
, workgroup
;
2915 pstring path
, targetpath
;
2916 struct cli_state
*targetcli
;
2918 if (!context
|| !context
->internal
||
2919 !context
->internal
->_initialized
) {
2933 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
2935 if (smbc_parse_path(context
, fname
,
2936 server
, sizeof(server
),
2937 share
, sizeof(share
),
2940 password
, sizeof(password
),
2947 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2949 fstrcpy(workgroup
, context
->workgroup
);
2951 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
2955 return -1; /* errno set by smbc_server */
2959 /* if (strncmp(srv->cli.dev, "IPC", 3) == 0) {
2961 mode = aDIR | aRONLY;
2964 else if (strncmp(srv->cli.dev, "LPT", 3) == 0) {
2966 if (strcmp(path, "\\") == 0) {
2968 mode = aDIR | aRONLY;
2974 smbc_stat_printjob(srv, path, &size, &m_time);
2975 c_time = a_time = m_time;
2980 /*d_printf(">>>rmdir: resolving %s\n", path);*/
2981 if (!cli_resolve_path( "", &srv
->cli
, path
, &targetcli
, targetpath
))
2983 d_printf("Could not resolve %s\n", path
);
2986 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
2989 if (!cli_rmdir(targetcli
, targetpath
)) {
2991 errno
= smbc_errno(context
, targetcli
);
2993 if (errno
== EACCES
) { /* Check if the dir empty or not */
2995 pstring lpath
; /* Local storage to avoid buffer overflows */
2997 smbc_rmdir_dirempty
= True
; /* Make this so ... */
2999 pstrcpy(lpath
, targetpath
);
3000 pstrcat(lpath
, "\\*");
3002 if (cli_list(targetcli
, lpath
, aDIR
| aSYSTEM
| aHIDDEN
, rmdir_list_fn
,
3005 /* Fix errno to ignore latest error ... */
3007 DEBUG(5, ("smbc_rmdir: cli_list returned an error: %d\n",
3008 smbc_errno(context
, targetcli
)));
3013 if (smbc_rmdir_dirempty
)
3029 * Routine to return the current directory position
3032 static off_t
smbc_telldir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
)
3034 off_t ret_val
; /* Squash warnings about cast */
3036 if (!context
|| !context
->internal
||
3037 !context
->internal
->_initialized
) {
3044 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3051 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3059 * We return the pointer here as the offset
3061 ret_val
= (off_t
)(long)dir
->dir_next
;
3067 * A routine to run down the list and see if the entry is OK
3070 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
3071 struct smbc_dirent
*dirent
)
3074 /* Run down the list looking for what we want */
3078 struct smbc_dir_list
*tmp
= list
;
3082 if (tmp
->dirent
== dirent
)
3091 return NULL
; /* Not found, or an error */
3097 * Routine to seek on a directory
3100 static int smbc_lseekdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, off_t offset
)
3102 long int l_offset
= offset
; /* Handle problems of size */
3103 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
3104 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
3106 if (!context
|| !context
->internal
||
3107 !context
->internal
->_initialized
) {
3114 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3121 /* Now, check what we were passed and see if it is OK ... */
3123 if (dirent
== NULL
) { /* Seek to the begining of the list */
3125 dir
->dir_next
= dir
->dir_list
;
3130 /* Now, run down the list and make sure that the entry is OK */
3131 /* This may need to be changed if we change the format of the list */
3133 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
3135 errno
= EINVAL
; /* Bad entry */
3140 dir
->dir_next
= list_ent
;
3147 * Routine to fstat a dir
3150 static int smbc_fstatdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
, struct stat
*st
)
3153 if (!context
|| !context
->internal
||
3154 !context
->internal
->_initialized
) {
3161 /* No code yet ... */
3167 int smbc_chmod_ctx(SMBCCTX
*context
, const char *fname
, mode_t newmode
)
3170 fstring server
, share
, user
, password
, workgroup
;
3174 if (!context
|| !context
->internal
||
3175 !context
->internal
->_initialized
) {
3177 errno
= EINVAL
; /* Best I can think of ... */
3189 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, newmode
));
3191 if (smbc_parse_path(context
, fname
,
3192 server
, sizeof(server
),
3193 share
, sizeof(share
),
3196 password
, sizeof(password
),
3202 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3204 fstrcpy(workgroup
, context
->workgroup
);
3206 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
3209 return -1; /* errno set by smbc_server */
3214 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
3215 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
3216 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
3217 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
3219 if (!cli_setatr(&srv
->cli
, path
, mode
, 0)) {
3220 errno
= smbc_errno(context
, &srv
->cli
);
3227 int smbc_utimes_ctx(SMBCCTX
*context
, const char *fname
, struct timeval
*tbuf
)
3230 fstring server
, share
, user
, password
, workgroup
;
3235 if (!context
|| !context
->internal
||
3236 !context
->internal
->_initialized
) {
3238 errno
= EINVAL
; /* Best I can think of ... */
3251 a_time
= m_time
= time(NULL
);
3253 a_time
= tbuf
[0].tv_sec
;
3254 m_time
= tbuf
[1].tv_sec
;
3263 strncpy(atimebuf
, ctime(&a_time
), sizeof(atimebuf
));
3264 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
3265 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
3269 strncpy(mtimebuf
, ctime(&m_time
), sizeof(mtimebuf
));
3270 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
3271 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
3275 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3276 fname
, atimebuf
, mtimebuf
);
3279 if (smbc_parse_path(context
, fname
,
3280 server
, sizeof(server
),
3281 share
, sizeof(share
),
3284 password
, sizeof(password
),
3290 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3292 fstrcpy(workgroup
, context
->workgroup
);
3294 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
3297 return -1; /* errno set by smbc_server */
3300 if (!smbc_setatr(context
, srv
, path
, 0, a_time
, m_time
, 0)) {
3301 return -1; /* errno set by smbc_setatr */
3308 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3309 However NT4 gives a "The information may have been modified by a
3310 computer running Windows NT 5.0" if denied ACEs do not appear before
3313 static int ace_compare(SEC_ACE
*ace1
, SEC_ACE
*ace2
)
3315 if (sec_ace_equal(ace1
, ace2
))
3318 if (ace1
->type
!= ace2
->type
)
3319 return ace2
->type
- ace1
->type
;
3321 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
3322 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
3324 if (ace1
->flags
!= ace2
->flags
)
3325 return ace1
->flags
- ace2
->flags
;
3327 if (ace1
->info
.mask
!= ace2
->info
.mask
)
3328 return ace1
->info
.mask
- ace2
->info
.mask
;
3330 if (ace1
->size
!= ace2
->size
)
3331 return ace1
->size
- ace2
->size
;
3333 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
3337 static void sort_acl(SEC_ACL
*the_acl
)
3340 if (!the_acl
) return;
3342 qsort(the_acl
->ace
, the_acl
->num_aces
, sizeof(the_acl
->ace
[0]), QSORT_CAST ace_compare
);
3344 for (i
=1;i
<the_acl
->num_aces
;) {
3345 if (sec_ace_equal(&the_acl
->ace
[i
-1], &the_acl
->ace
[i
])) {
3347 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
3348 the_acl
->ace
[j
] = the_acl
->ace
[j
+1];
3350 the_acl
->num_aces
--;
3357 /* convert a SID to a string, either numeric or username/group */
3358 static void convert_sid_to_string(struct cli_state
*ipc_cli
,
3364 char **domains
= NULL
;
3365 char **names
= NULL
;
3366 uint32
*types
= NULL
;
3367 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
3368 sid_to_string(str
, sid
);
3371 return; /* no lookup desired */
3378 /* Ask LSA to convert the sid to a name */
3380 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd
, ipc_cli
->mem_ctx
,
3381 pol
, 1, sid
, &domains
,
3383 !domains
|| !domains
[0] || !names
|| !names
[0]) {
3389 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
3390 domains
[0], lp_winbind_separator(),
3394 /* convert a string to a SID, either numeric or username/group */
3395 static BOOL
convert_string_to_sid(struct cli_state
*ipc_cli
,
3401 uint32
*types
= NULL
;
3402 DOM_SID
*sids
= NULL
;
3404 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
3411 if (strncmp(str
, "S-", 2) == 0) {
3412 return string_to_sid(sid
, str
);
3419 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd
, ipc_cli
->mem_ctx
,
3420 pol
, 1, &str
, &sids
,
3426 sid_copy(sid
, &sids
[0]);
3433 /* parse an ACE in the same format as print_ace() */
3434 static BOOL
parse_ace(struct cli_state
*ipc_cli
,
3443 unsigned atype
, aflags
, amask
;
3446 const struct perm_value
*v
;
3452 /* These values discovered by inspection */
3453 static const struct perm_value special_values
[] = {
3454 { "R", 0x00120089 },
3455 { "W", 0x00120116 },
3456 { "X", 0x001200a0 },
3457 { "D", 0x00010000 },
3458 { "P", 0x00040000 },
3459 { "O", 0x00080000 },
3463 static const struct perm_value standard_values
[] = {
3464 { "READ", 0x001200a9 },
3465 { "CHANGE", 0x001301bf },
3466 { "FULL", 0x001f01ff },
3472 p
= strchr_m(str
,':');
3473 if (!p
) return False
;
3476 /* Try to parse numeric form */
3478 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
3479 convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3483 /* Try to parse text form */
3485 if (!convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3490 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3494 if (StrnCaseCmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
3495 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
3496 } else if (StrnCaseCmp(tok
, "DENIED", strlen("DENIED")) == 0) {
3497 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
3502 /* Only numeric form accepted for flags at present */
3504 if (!(next_token(&cp
, tok
, "/", sizeof(fstring
)) &&
3505 sscanf(tok
, "%i", &aflags
))) {
3509 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3513 if (strncmp(tok
, "0x", 2) == 0) {
3514 if (sscanf(tok
, "%i", &amask
) != 1) {
3520 for (v
= standard_values
; v
->perm
; v
++) {
3521 if (strcmp(tok
, v
->perm
) == 0) {
3532 for (v
= special_values
; v
->perm
; v
++) {
3533 if (v
->perm
[0] == *p
) {
3539 if (!found
) return False
;
3549 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
3553 /* add an ACE to a list of ACEs in a SEC_ACL */
3554 static BOOL
add_ace(SEC_ACL
**the_acl
, SEC_ACE
*ace
, TALLOC_CTX
*ctx
)
3559 (*the_acl
) = make_sec_acl(ctx
, 3, 1, ace
);
3563 aces
= SMB_CALLOC_ARRAY(SEC_ACE
, 1+(*the_acl
)->num_aces
);
3564 memcpy(aces
, (*the_acl
)->ace
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
3565 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
3566 newacl
= make_sec_acl(ctx
,(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
3568 (*the_acl
) = newacl
;
3573 /* parse a ascii version of a security descriptor */
3574 static SEC_DESC
*sec_desc_parse(TALLOC_CTX
*ctx
,
3575 struct cli_state
*ipc_cli
,
3580 const char *p
= str
;
3584 DOM_SID
*grp_sid
=NULL
, *owner_sid
=NULL
;
3588 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
3590 if (StrnCaseCmp(tok
,"REVISION:", 9) == 0) {
3591 revision
= strtol(tok
+9, NULL
, 16);
3595 if (StrnCaseCmp(tok
,"OWNER:", 6) == 0) {
3596 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
3598 !convert_string_to_sid(ipc_cli
, pol
,
3600 owner_sid
, tok
+6)) {
3601 DEBUG(5, ("Failed to parse owner sid\n"));
3607 if (StrnCaseCmp(tok
,"OWNER+:", 7) == 0) {
3608 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
3610 !convert_string_to_sid(ipc_cli
, pol
,
3612 owner_sid
, tok
+7)) {
3613 DEBUG(5, ("Failed to parse owner sid\n"));
3619 if (StrnCaseCmp(tok
,"GROUP:", 6) == 0) {
3620 grp_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
3622 !convert_string_to_sid(ipc_cli
, pol
,
3625 DEBUG(5, ("Failed to parse group sid\n"));
3631 if (StrnCaseCmp(tok
,"GROUP+:", 7) == 0) {
3632 grp_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
3634 !convert_string_to_sid(ipc_cli
, pol
,
3637 DEBUG(5, ("Failed to parse group sid\n"));
3643 if (StrnCaseCmp(tok
,"ACL:", 4) == 0) {
3645 if (!parse_ace(ipc_cli
, pol
, &ace
, numeric
, tok
+4)) {
3646 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
3649 if(!add_ace(&dacl
, &ace
, ctx
)) {
3650 DEBUG(5, ("Failed to add ACL %s\n", tok
));
3656 if (StrnCaseCmp(tok
,"ACL+:", 5) == 0) {
3658 if (!parse_ace(ipc_cli
, pol
, &ace
, False
, tok
+5)) {
3659 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
3662 if(!add_ace(&dacl
, &ace
, ctx
)) {
3663 DEBUG(5, ("Failed to add ACL %s\n", tok
));
3669 DEBUG(5, ("Failed to parse security descriptor\n"));
3673 ret
= make_sec_desc(ctx
, revision
, SEC_DESC_SELF_RELATIVE
,
3674 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
3677 SAFE_FREE(owner_sid
);
3683 /* Obtain the current dos attributes */
3684 static DOS_ATTR_DESC
*dos_attr_query(SMBCCTX
*context
,
3686 const char *filename
,
3689 time_t m_time
= 0, a_time
= 0, c_time
= 0;
3692 SMB_INO_T inode
= 0;
3695 ret
= TALLOC_P(ctx
, DOS_ATTR_DESC
);
3701 /* Obtain the DOS attributes */
3702 if (!smbc_getatr(context
, srv
, CONST_DISCARD(char *, filename
),
3704 &c_time
, &a_time
, &m_time
, &inode
)) {
3706 errno
= smbc_errno(context
, &srv
->cli
);
3707 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
3714 ret
->a_time
= a_time
;
3715 ret
->c_time
= c_time
;
3716 ret
->m_time
= m_time
;
3723 /* parse a ascii version of a security descriptor */
3724 static void dos_attr_parse(SMBCCTX
*context
,
3729 const char *p
= str
;
3732 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
3734 if (StrnCaseCmp(tok
, "MODE:", 5) == 0) {
3735 dad
->mode
= strtol(tok
+5, NULL
, 16);
3739 if (StrnCaseCmp(tok
, "SIZE:", 5) == 0) {
3740 dad
->size
= (SMB_OFF_T
)atof(tok
+5);
3744 if (StrnCaseCmp(tok
, "A_TIME:", 7) == 0) {
3745 dad
->a_time
= (time_t)strtol(tok
+7, NULL
, 10);
3749 if (StrnCaseCmp(tok
, "C_TIME:", 7) == 0) {
3750 dad
->c_time
= (time_t)strtol(tok
+7, NULL
, 10);
3754 if (StrnCaseCmp(tok
, "M_TIME:", 7) == 0) {
3755 dad
->m_time
= (time_t)strtol(tok
+7, NULL
, 10);
3759 if (StrnCaseCmp(tok
, "INODE:", 6) == 0) {
3760 dad
->inode
= (SMB_INO_T
)atof(tok
+6);
3766 /*****************************************************
3767 Retrieve the acls for a file.
3768 *******************************************************/
3770 static int cacl_get(SMBCCTX
*context
, TALLOC_CTX
*ctx
, SMBCSRV
*srv
,
3771 struct cli_state
*ipc_cli
, POLICY_HND
*pol
,
3772 char *filename
, char *attr_name
, char *buf
, int bufsize
)
3783 BOOL exclude_nt_revision
= False
;
3784 BOOL exclude_nt_owner
= False
;
3785 BOOL exclude_nt_group
= False
;
3786 BOOL exclude_nt_acl
= False
;
3787 BOOL exclude_dos_mode
= False
;
3788 BOOL exclude_dos_size
= False
;
3789 BOOL exclude_dos_ctime
= False
;
3790 BOOL exclude_dos_atime
= False
;
3791 BOOL exclude_dos_mtime
= False
;
3792 BOOL exclude_dos_inode
= False
;
3793 BOOL numeric
= True
;
3794 BOOL determine_size
= (bufsize
== 0);
3798 fstring name_sandbox
;
3802 time_t m_time
= 0, a_time
= 0, c_time
= 0;
3806 struct cli_state
*cli
= &srv
->cli
;
3808 /* Copy name so we can strip off exclusions (if any are specified) */
3809 strncpy(name_sandbox
, attr_name
, sizeof(name_sandbox
) - 1);
3811 /* Ensure name is null terminated */
3812 name_sandbox
[sizeof(name_sandbox
) - 1] = '\0';
3814 /* Play in the sandbox */
3815 name
= name_sandbox
;
3817 /* If there are any exclusions, point to them and mask them from name */
3818 if ((pExclude
= strchr(name
, '!')) != NULL
)
3823 all
= (StrnCaseCmp(name
, "system.*", 8) == 0);
3824 all_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.*", 20) == 0);
3825 all_nt_acls
= (StrnCaseCmp(name
, "system.nt_sec_desc.acl.*", 24) == 0);
3826 all_dos
= (StrnCaseCmp(name
, "system.dos_attr.*", 17) == 0);
3827 some_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.", 19) == 0);
3828 some_dos
= (StrnCaseCmp(name
, "system.dos_attr.", 16) == 0);
3829 numeric
= (* (name
+ strlen(name
) - 1) != '+');
3831 /* Look for exclusions from "all" requests */
3832 if (all
|| all_nt
|| all_dos
) {
3834 /* Exclusions are delimited by '!' */
3835 for (; pExclude
!= NULL
; pExclude
= (p
== NULL
? NULL
: p
+ 1)) {
3837 /* Find end of this exclusion name */
3838 if ((p
= strchr(pExclude
, '!')) != NULL
)
3843 /* Which exclusion name is this? */
3844 if (StrCaseCmp(pExclude
, "nt_sec_desc.revision") == 0) {
3845 exclude_nt_revision
= True
;
3847 else if (StrCaseCmp(pExclude
, "nt_sec_desc.owner") == 0) {
3848 exclude_nt_owner
= True
;
3850 else if (StrCaseCmp(pExclude
, "nt_sec_desc.group") == 0) {
3851 exclude_nt_group
= True
;
3853 else if (StrCaseCmp(pExclude
, "nt_sec_desc.acl") == 0) {
3854 exclude_nt_acl
= True
;
3856 else if (StrCaseCmp(pExclude
, "dos_attr.mode") == 0) {
3857 exclude_dos_mode
= True
;
3859 else if (StrCaseCmp(pExclude
, "dos_attr.size") == 0) {
3860 exclude_dos_size
= True
;
3862 else if (StrCaseCmp(pExclude
, "dos_attr.c_time") == 0) {
3863 exclude_dos_ctime
= True
;
3865 else if (StrCaseCmp(pExclude
, "dos_attr.a_time") == 0) {
3866 exclude_dos_atime
= True
;
3868 else if (StrCaseCmp(pExclude
, "dos_attr.m_time") == 0) {
3869 exclude_dos_mtime
= True
;
3871 else if (StrCaseCmp(pExclude
, "dos_attr.inode") == 0) {
3872 exclude_dos_inode
= True
;
3875 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
3886 * If we are (possibly) talking to an NT or new system and some NT
3887 * attributes have been requested...
3889 if (ipc_cli
&& (all
|| some_nt
|| all_nt_acls
)) {
3890 /* Point to the portion after "system.nt_sec_desc." */
3891 name
+= 19; /* if (all) this will be invalid but unused */
3893 /* ... then obtain any NT attributes which were requested */
3894 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
3897 DEBUG(5, ("cacl_get failed to open %s: %s\n",
3898 filename
, cli_errstr(cli
)));
3903 sd
= cli_query_secdesc(cli
, fnum
, ctx
);
3907 ("cacl_get Failed to query old descriptor\n"));
3912 cli_close(cli
, fnum
);
3914 if (! exclude_nt_revision
) {
3915 if (all
|| all_nt
) {
3916 if (determine_size
) {
3917 p
= talloc_asprintf(ctx
,
3926 n
= snprintf(buf
, bufsize
,
3927 "REVISION:%d", sd
->revision
);
3929 } else if (StrCaseCmp(name
, "revision") == 0) {
3930 if (determine_size
) {
3931 p
= talloc_asprintf(ctx
, "%d",
3939 n
= snprintf(buf
, bufsize
, "%d",
3944 if (!determine_size
&& n
> bufsize
) {
3953 if (! exclude_nt_owner
) {
3954 /* Get owner and group sid */
3955 if (sd
->owner_sid
) {
3956 convert_sid_to_string(ipc_cli
, pol
,
3961 fstrcpy(sidstr
, "");
3964 if (all
|| all_nt
) {
3965 if (determine_size
) {
3966 p
= talloc_asprintf(ctx
, ",OWNER:%s",
3974 n
= snprintf(buf
, bufsize
,
3975 ",OWNER:%s", sidstr
);
3977 } else if (StrnCaseCmp(name
, "owner", 5) == 0) {
3978 if (determine_size
) {
3979 p
= talloc_asprintf(ctx
, "%s", sidstr
);
3986 n
= snprintf(buf
, bufsize
, "%s",
3991 if (!determine_size
&& n
> bufsize
) {
4000 if (! exclude_nt_group
) {
4002 convert_sid_to_string(ipc_cli
, pol
,
4006 fstrcpy(sidstr
, "");
4009 if (all
|| all_nt
) {
4010 if (determine_size
) {
4011 p
= talloc_asprintf(ctx
, ",GROUP:%s",
4019 n
= snprintf(buf
, bufsize
,
4020 ",GROUP:%s", sidstr
);
4022 } else if (StrnCaseCmp(name
, "group", 5) == 0) {
4023 if (determine_size
) {
4024 p
= talloc_asprintf(ctx
, "%s", sidstr
);
4031 n
= snprintf(buf
, bufsize
, "%s", sidstr
);
4035 if (!determine_size
&& n
> bufsize
) {
4044 if (! exclude_nt_acl
) {
4045 /* Add aces to value buffer */
4046 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
4048 SEC_ACE
*ace
= &sd
->dacl
->ace
[i
];
4049 convert_sid_to_string(ipc_cli
, pol
,
4053 if (all
|| all_nt
) {
4054 if (determine_size
) {
4055 p
= talloc_asprintf(
4071 ",ACL:%s:%d/%d/0x%08x",
4077 } else if ((StrnCaseCmp(name
, "acl", 3) == 0 &&
4078 StrCaseCmp(name
+ 3, sidstr
) == 0) ||
4079 (StrnCaseCmp(name
, "acl+", 4) == 0 &&
4080 StrCaseCmp(name
+ 4, sidstr
) == 0)) {
4081 if (determine_size
) {
4082 p
= talloc_asprintf(
4094 n
= snprintf(buf
, bufsize
,
4100 } else if (all_nt_acls
) {
4101 if (determine_size
) {
4102 p
= talloc_asprintf(
4104 "%s%s:%d/%d/0x%08x",
4116 n
= snprintf(buf
, bufsize
,
4117 "%s%s:%d/%d/0x%08x",
4135 /* Restore name pointer to its original value */
4139 if (all
|| some_dos
) {
4140 /* Point to the portion after "system.dos_attr." */
4141 name
+= 16; /* if (all) this will be invalid but unused */
4143 /* Obtain the DOS attributes */
4144 if (!smbc_getatr(context
, srv
, filename
, &mode
, &size
,
4145 &c_time
, &a_time
, &m_time
, &ino
)) {
4147 errno
= smbc_errno(context
, &srv
->cli
);
4152 if (! exclude_dos_mode
) {
4153 if (all
|| all_dos
) {
4154 if (determine_size
) {
4155 p
= talloc_asprintf(ctx
,
4168 n
= snprintf(buf
, bufsize
,
4176 } else if (StrCaseCmp(name
, "mode") == 0) {
4177 if (determine_size
) {
4178 p
= talloc_asprintf(ctx
, "0x%x", mode
);
4185 n
= snprintf(buf
, bufsize
, "0x%x", mode
);
4189 if (!determine_size
&& n
> bufsize
) {
4198 if (! exclude_dos_size
) {
4199 if (all
|| all_dos
) {
4200 if (determine_size
) {
4201 p
= talloc_asprintf(
4211 n
= snprintf(buf
, bufsize
,
4215 } else if (StrCaseCmp(name
, "size") == 0) {
4216 if (determine_size
) {
4217 p
= talloc_asprintf(
4227 n
= snprintf(buf
, bufsize
,
4233 if (!determine_size
&& n
> bufsize
) {
4242 if (! exclude_dos_ctime
) {
4243 if (all
|| all_dos
) {
4244 if (determine_size
) {
4245 p
= talloc_asprintf(ctx
,
4254 n
= snprintf(buf
, bufsize
,
4255 ",C_TIME:%lu", c_time
);
4257 } else if (StrCaseCmp(name
, "c_time") == 0) {
4258 if (determine_size
) {
4259 p
= talloc_asprintf(ctx
, "%lu", c_time
);
4266 n
= snprintf(buf
, bufsize
, "%lu", c_time
);
4270 if (!determine_size
&& n
> bufsize
) {
4279 if (! exclude_dos_atime
) {
4280 if (all
|| all_dos
) {
4281 if (determine_size
) {
4282 p
= talloc_asprintf(ctx
,
4291 n
= snprintf(buf
, bufsize
,
4292 ",A_TIME:%lu", a_time
);
4294 } else if (StrCaseCmp(name
, "a_time") == 0) {
4295 if (determine_size
) {
4296 p
= talloc_asprintf(ctx
, "%lu", a_time
);
4303 n
= snprintf(buf
, bufsize
, "%lu", a_time
);
4307 if (!determine_size
&& n
> bufsize
) {
4316 if (! exclude_dos_mtime
) {
4317 if (all
|| all_dos
) {
4318 if (determine_size
) {
4319 p
= talloc_asprintf(ctx
,
4328 n
= snprintf(buf
, bufsize
,
4329 ",M_TIME:%lu", m_time
);
4331 } else if (StrCaseCmp(name
, "m_time") == 0) {
4332 if (determine_size
) {
4333 p
= talloc_asprintf(ctx
, "%lu", m_time
);
4340 n
= snprintf(buf
, bufsize
, "%lu", m_time
);
4344 if (!determine_size
&& n
> bufsize
) {
4353 if (! exclude_dos_inode
) {
4354 if (all
|| all_dos
) {
4355 if (determine_size
) {
4356 p
= talloc_asprintf(
4366 n
= snprintf(buf
, bufsize
,
4370 } else if (StrCaseCmp(name
, "inode") == 0) {
4371 if (determine_size
) {
4372 p
= talloc_asprintf(
4382 n
= snprintf(buf
, bufsize
,
4388 if (!determine_size
&& n
> bufsize
) {
4397 /* Restore name pointer to its original value */
4410 /*****************************************************
4411 set the ACLs on a file given an ascii description
4412 *******************************************************/
4413 static int cacl_set(TALLOC_CTX
*ctx
, struct cli_state
*cli
,
4414 struct cli_state
*ipc_cli
, POLICY_HND
*pol
,
4415 const char *filename
, const char *the_acl
,
4416 int mode
, int flags
)
4420 SEC_DESC
*sd
= NULL
, *old
;
4421 SEC_ACL
*dacl
= NULL
;
4422 DOM_SID
*owner_sid
= NULL
;
4423 DOM_SID
*grp_sid
= NULL
;
4428 BOOL numeric
= True
;
4430 /* the_acl will be null for REMOVE_ALL operations */
4432 numeric
= ((p
= strchr(the_acl
, ':')) != NULL
&&
4436 /* if this is to set the entire ACL... */
4437 if (*the_acl
== '*') {
4438 /* ... then increment past the first colon */
4442 sd
= sec_desc_parse(ctx
, ipc_cli
, pol
, numeric
,
4443 CONST_DISCARD(char *, the_acl
));
4451 /* The desired access below is the only one I could find that works
4452 with NT4, W2KP and Samba */
4454 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
4457 DEBUG(5, ("cacl_set failed to open %s: %s\n",
4458 filename
, cli_errstr(cli
)));
4463 old
= cli_query_secdesc(cli
, fnum
, ctx
);
4466 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
4471 cli_close(cli
, fnum
);
4474 case SMBC_XATTR_MODE_REMOVE_ALL
:
4475 old
->dacl
->num_aces
= 0;
4476 SAFE_FREE(old
->dacl
->ace
);
4477 SAFE_FREE(old
->dacl
);
4482 case SMBC_XATTR_MODE_REMOVE
:
4483 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
4486 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
4487 if (sec_ace_equal(&sd
->dacl
->ace
[i
],
4488 &old
->dacl
->ace
[j
])) {
4490 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
4491 old
->dacl
->ace
[k
] = old
->dacl
->ace
[k
+1];
4493 old
->dacl
->num_aces
--;
4494 if (old
->dacl
->num_aces
== 0) {
4495 SAFE_FREE(old
->dacl
->ace
);
4496 SAFE_FREE(old
->dacl
);
4513 case SMBC_XATTR_MODE_ADD
:
4514 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
4517 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
4518 if (sid_equal(&sd
->dacl
->ace
[i
].trustee
,
4519 &old
->dacl
->ace
[j
].trustee
)) {
4520 if (!(flags
& SMBC_XATTR_FLAG_CREATE
)) {
4525 old
->dacl
->ace
[j
] = sd
->dacl
->ace
[i
];
4531 if (!found
&& (flags
& SMBC_XATTR_FLAG_REPLACE
)) {
4537 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
4538 add_ace(&old
->dacl
, &sd
->dacl
->ace
[i
], ctx
);
4544 case SMBC_XATTR_MODE_SET
:
4546 owner_sid
= old
->owner_sid
;
4547 grp_sid
= old
->grp_sid
;
4551 case SMBC_XATTR_MODE_CHOWN
:
4552 owner_sid
= sd
->owner_sid
;
4555 case SMBC_XATTR_MODE_CHGRP
:
4556 grp_sid
= sd
->grp_sid
;
4560 /* Denied ACE entries must come before allowed ones */
4561 sort_acl(old
->dacl
);
4563 /* Create new security descriptor and set it */
4564 sd
= make_sec_desc(ctx
, old
->revision
, SEC_DESC_SELF_RELATIVE
,
4565 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
4567 fnum
= cli_nt_create(cli
, filename
,
4568 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
);
4571 DEBUG(5, ("cacl_set failed to open %s: %s\n",
4572 filename
, cli_errstr(cli
)));
4577 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
4578 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli
)));
4585 cli_close(cli
, fnum
);
4595 int smbc_setxattr_ctx(SMBCCTX
*context
,
4606 fstring server
, share
, user
, password
, workgroup
;
4612 if (!context
|| !context
->internal
||
4613 !context
->internal
->_initialized
) {
4615 errno
= EINVAL
; /* Best I can think of ... */
4627 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n", fname
, name
, (int) size
, (const char*)value
));
4629 if (smbc_parse_path(context
, fname
,
4630 server
, sizeof(server
),
4631 share
, sizeof(share
),
4634 password
, sizeof(password
),
4640 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
4642 fstrcpy(workgroup
, context
->workgroup
);
4644 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
4646 return -1; /* errno set by smbc_server */
4649 if (! srv
->no_nt_session
) {
4650 ipc_srv
= smbc_attr_server(context
, server
, share
,
4651 workgroup
, user
, password
,
4653 srv
->no_nt_session
= True
;
4658 ctx
= talloc_init("smbc_setxattr");
4665 * Are they asking to set the entire set of known attributes?
4667 if (StrCaseCmp(name
, "system.*") == 0 ||
4668 StrCaseCmp(name
, "system.*+") == 0) {
4671 talloc_asprintf(ctx
, "%s:%s", name
+7, (const char *) value
);
4679 ret
= cacl_set(ctx
, &srv
->cli
,
4680 &ipc_srv
->cli
, &pol
, path
,
4683 ? SMBC_XATTR_MODE_SET
4684 : SMBC_XATTR_MODE_ADD
),
4690 /* get a DOS Attribute Descriptor with current attributes */
4691 dad
= dos_attr_query(context
, ctx
, path
, srv
);
4693 /* Overwrite old with new, using what was provided */
4694 dos_attr_parse(context
, dad
, srv
, namevalue
);
4696 /* Set the new DOS attributes */
4697 if (! smbc_setatr(context
, srv
, path
,
4703 /* cause failure if NT failed too */
4708 /* we only fail if both NT and DOS sets failed */
4709 if (ret
< 0 && ! dad
) {
4710 ret
= -1; /* in case dad was null */
4716 talloc_destroy(ctx
);
4721 * Are they asking to set an access control element or to set
4722 * the entire access control list?
4724 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
4725 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
4726 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
4727 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
4728 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
4732 talloc_asprintf(ctx
, "%s:%s", name
+19, (const char *) value
);
4735 ret
= -1; /* errno set by smbc_server() */
4737 else if (! namevalue
) {
4741 ret
= cacl_set(ctx
, &srv
->cli
,
4742 &ipc_srv
->cli
, &pol
, path
,
4745 ? SMBC_XATTR_MODE_SET
4746 : SMBC_XATTR_MODE_ADD
),
4749 talloc_destroy(ctx
);
4754 * Are they asking to set the owner?
4756 if (StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
4757 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0) {
4761 talloc_asprintf(ctx
, "%s:%s", name
+19, (const char *) value
);
4765 ret
= -1; /* errno set by smbc_server() */
4767 else if (! namevalue
) {
4771 ret
= cacl_set(ctx
, &srv
->cli
,
4772 &ipc_srv
->cli
, &pol
, path
,
4773 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
4775 talloc_destroy(ctx
);
4780 * Are they asking to set the group?
4782 if (StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
4783 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0) {
4787 talloc_asprintf(ctx
, "%s:%s", name
+19, (const char *) value
);
4790 /* errno set by smbc_server() */
4793 else if (! namevalue
) {
4797 ret
= cacl_set(ctx
, &srv
->cli
,
4798 &ipc_srv
->cli
, &pol
, path
,
4799 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
4801 talloc_destroy(ctx
);
4806 * Are they asking to set a DOS attribute?
4808 if (StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
4809 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
4810 StrCaseCmp(name
, "system.dos_attr.c_time") == 0 ||
4811 StrCaseCmp(name
, "system.dos_attr.a_time") == 0 ||
4812 StrCaseCmp(name
, "system.dos_attr.m_time") == 0) {
4814 /* get a DOS Attribute Descriptor with current attributes */
4815 dad
= dos_attr_query(context
, ctx
, path
, srv
);
4818 talloc_asprintf(ctx
, "%s:%s", name
+16, (const char *) value
);
4823 /* Overwrite old with provided new params */
4824 dos_attr_parse(context
, dad
, srv
, namevalue
);
4826 /* Set the new DOS attributes */
4827 ret2
= smbc_setatr(context
, srv
, path
,
4833 /* ret2 has True (success) / False (failure) */
4844 talloc_destroy(ctx
);
4848 /* Unsupported attribute name */
4849 talloc_destroy(ctx
);
4854 int smbc_getxattr_ctx(SMBCCTX
*context
,
4863 fstring server
, share
, user
, password
, workgroup
;
4869 if (!context
|| !context
->internal
||
4870 !context
->internal
->_initialized
) {
4872 errno
= EINVAL
; /* Best I can think of ... */
4884 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname
, name
));
4886 if (smbc_parse_path(context
, fname
,
4887 server
, sizeof(server
),
4888 share
, sizeof(share
),
4891 password
, sizeof(password
),
4897 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
4899 fstrcpy(workgroup
, context
->workgroup
);
4901 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
4903 return -1; /* errno set by smbc_server */
4906 if (! srv
->no_nt_session
) {
4907 ipc_srv
= smbc_attr_server(context
, server
, share
,
4908 workgroup
, user
, password
,
4911 srv
->no_nt_session
= True
;
4917 ctx
= talloc_init("smbc:getxattr");
4923 /* Are they requesting a supported attribute? */
4924 if (StrCaseCmp(name
, "system.*") == 0 ||
4925 StrnCaseCmp(name
, "system.*!", 9) == 0 ||
4926 StrCaseCmp(name
, "system.*+") == 0 ||
4927 StrnCaseCmp(name
, "system.*+!", 10) == 0 ||
4928 StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
4929 StrnCaseCmp(name
, "system.nt_sec_desc.*!", 21) == 0 ||
4930 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
4931 StrnCaseCmp(name
, "system.nt_sec_desc.*+!", 22) == 0 ||
4932 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
4933 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
4934 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
4935 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
4936 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
4937 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
4938 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0 ||
4939 StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
4940 StrnCaseCmp(name
, "system.dos_attr.*!", 18) == 0 ||
4941 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
4942 StrCaseCmp(name
, "system.dos_attr.size") == 0 ||
4943 StrCaseCmp(name
, "system.dos_attr.c_time") == 0 ||
4944 StrCaseCmp(name
, "system.dos_attr.a_time") == 0 ||
4945 StrCaseCmp(name
, "system.dos_attr.m_time") == 0 ||
4946 StrCaseCmp(name
, "system.dos_attr.inode") == 0) {
4949 ret
= cacl_get(context
, ctx
, srv
,
4950 ipc_srv
== NULL
? NULL
: &ipc_srv
->cli
,
4952 CONST_DISCARD(char *, name
),
4953 CONST_DISCARD(char *, value
), size
);
4954 if (ret
< 0 && errno
== 0) {
4955 errno
= smbc_errno(context
, &srv
->cli
);
4957 talloc_destroy(ctx
);
4961 /* Unsupported attribute name */
4962 talloc_destroy(ctx
);
4968 int smbc_removexattr_ctx(SMBCCTX
*context
,
4975 fstring server
, share
, user
, password
, workgroup
;
4980 if (!context
|| !context
->internal
||
4981 !context
->internal
->_initialized
) {
4983 errno
= EINVAL
; /* Best I can think of ... */
4995 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname
, name
));
4997 if (smbc_parse_path(context
, fname
,
4998 server
, sizeof(server
),
4999 share
, sizeof(share
),
5002 password
, sizeof(password
),
5008 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5010 fstrcpy(workgroup
, context
->workgroup
);
5012 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
5014 return -1; /* errno set by smbc_server */
5017 if (! srv
->no_nt_session
) {
5018 ipc_srv
= smbc_attr_server(context
, server
, share
,
5019 workgroup
, user
, password
,
5021 srv
->no_nt_session
= True
;
5027 return -1; /* errno set by smbc_attr_server */
5030 ctx
= talloc_init("smbc_removexattr");
5036 /* Are they asking to set the entire ACL? */
5037 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5038 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0) {
5041 ret
= cacl_set(ctx
, &srv
->cli
,
5042 &ipc_srv
->cli
, &pol
, path
,
5043 NULL
, SMBC_XATTR_MODE_REMOVE_ALL
, 0);
5044 talloc_destroy(ctx
);
5049 * Are they asking to remove one or more spceific security descriptor
5052 if (StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5053 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5054 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
5055 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5056 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
5057 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5058 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
5061 ret
= cacl_set(ctx
, &srv
->cli
,
5062 &ipc_srv
->cli
, &pol
, path
,
5063 name
+ 19, SMBC_XATTR_MODE_REMOVE
, 0);
5064 talloc_destroy(ctx
);
5068 /* Unsupported attribute name */
5069 talloc_destroy(ctx
);
5074 int smbc_listxattr_ctx(SMBCCTX
*context
,
5080 * This isn't quite what listxattr() is supposed to do. This returns
5081 * the complete set of attribute names, always, rather than only those
5082 * attribute names which actually exist for a file. Hmmm...
5084 const char supported
[] =
5087 "system.nt_sec_desc.revision\0"
5088 "system.nt_sec_desc.owner\0"
5089 "system.nt_sec_desc.owner+\0"
5090 "system.nt_sec_desc.group\0"
5091 "system.nt_sec_desc.group+\0"
5092 "system.nt_sec_desc.acl.*\0"
5093 "system.nt_sec_desc.acl\0"
5094 "system.nt_sec_desc.acl+\0"
5095 "system.nt_sec_desc.*\0"
5096 "system.nt_sec_desc.*+\0"
5097 "system.dos_attr.*\0"
5098 "system.dos_attr.mode\0"
5099 "system.dos_attr.c_time\0"
5100 "system.dos_attr.a_time\0"
5101 "system.dos_attr.m_time\0"
5105 return sizeof(supported
);
5108 if (sizeof(supported
) > size
) {
5113 /* this can't be strcpy() because there are embedded null characters */
5114 memcpy(list
, supported
, sizeof(supported
));
5115 return sizeof(supported
);
5120 * Open a print file to be written to by other calls
5123 static SMBCFILE
*smbc_open_print_job_ctx(SMBCCTX
*context
, const char *fname
)
5125 fstring server
, share
, user
, password
;
5128 if (!context
|| !context
->internal
||
5129 !context
->internal
->_initialized
) {
5143 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
5145 if (smbc_parse_path(context
, fname
,
5146 server
, sizeof(server
),
5147 share
, sizeof(share
),
5150 password
, sizeof(password
),
5156 /* What if the path is empty, or the file exists? */
5158 return context
->open(context
, fname
, O_WRONLY
, 666);
5163 * Routine to print a file on a remote server ...
5165 * We open the file, which we assume to be on a remote server, and then
5166 * copy it to a print file on the share specified by printq.
5169 static int smbc_print_file_ctx(SMBCCTX
*c_file
, const char *fname
, SMBCCTX
*c_print
, const char *printq
)
5171 SMBCFILE
*fid1
, *fid2
;
5172 int bytes
, saverr
, tot_bytes
= 0;
5175 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
5176 !c_print
->internal
->_initialized
) {
5183 if (!fname
&& !printq
) {
5190 /* Try to open the file for reading ... */
5192 if ((long)(fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
5194 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
5195 return -1; /* smbc_open sets errno */
5199 /* Now, try to open the printer file for writing */
5201 if ((long)(fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
5203 saverr
= errno
; /* Save errno */
5204 c_file
->close_fn(c_file
, fid1
);
5210 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
5214 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
5217 c_file
->close_fn(c_file
, fid1
);
5218 c_print
->close_fn(c_print
, fid2
);
5227 c_file
->close_fn(c_file
, fid1
); /* We have to close these anyway */
5228 c_print
->close_fn(c_print
, fid2
);
5242 * Routine to list print jobs on a printer share ...
5245 static int smbc_list_print_jobs_ctx(SMBCCTX
*context
, const char *fname
, smbc_list_print_job_fn fn
)
5248 fstring server
, share
, user
, password
, workgroup
;
5251 if (!context
|| !context
->internal
||
5252 !context
->internal
->_initialized
) {
5266 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
5268 if (smbc_parse_path(context
, fname
,
5269 server
, sizeof(server
),
5270 share
, sizeof(share
),
5273 password
, sizeof(password
),
5279 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5281 fstrcpy(workgroup
, context
->workgroup
);
5283 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
5287 return -1; /* errno set by smbc_server */
5291 if (cli_print_queue(&srv
->cli
, (void (*)(struct print_job_info
*))fn
) < 0) {
5293 errno
= smbc_errno(context
, &srv
->cli
);
5303 * Delete a print job from a remote printer share
5306 static int smbc_unlink_print_job_ctx(SMBCCTX
*context
, const char *fname
, int id
)
5309 fstring server
, share
, user
, password
, workgroup
;
5313 if (!context
|| !context
->internal
||
5314 !context
->internal
->_initialized
) {
5328 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
5330 if (smbc_parse_path(context
, fname
,
5331 server
, sizeof(server
),
5332 share
, sizeof(share
),
5335 password
, sizeof(password
),
5341 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5343 fstrcpy(workgroup
, context
->workgroup
);
5345 srv
= smbc_server(context
, server
, share
, workgroup
, user
, password
);
5349 return -1; /* errno set by smbc_server */
5353 if ((err
= cli_printjob_del(&srv
->cli
, id
)) != 0) {
5356 errno
= smbc_errno(context
, &srv
->cli
);
5357 else if (err
== ERRnosuchprintjob
)
5368 * Get a new empty handle to fill in with your own info
5370 SMBCCTX
* smbc_new_context(void)
5374 context
= SMB_MALLOC_P(SMBCCTX
);
5380 ZERO_STRUCTP(context
);
5382 context
->internal
= SMB_MALLOC_P(struct smbc_internal_data
);
5383 if (!context
->internal
) {
5388 ZERO_STRUCTP(context
->internal
);
5391 /* ADD REASONABLE DEFAULTS */
5393 context
->timeout
= 20000; /* 20 seconds */
5395 context
->options
.browse_max_lmb_count
= 3; /* # LMBs to query */
5396 context
->options
.urlencode_readdir_entries
= False
;/* backward compat */
5397 context
->options
.one_share_per_server
= False
;/* backward compat */
5399 context
->open
= smbc_open_ctx
;
5400 context
->creat
= smbc_creat_ctx
;
5401 context
->read
= smbc_read_ctx
;
5402 context
->write
= smbc_write_ctx
;
5403 context
->close_fn
= smbc_close_ctx
;
5404 context
->unlink
= smbc_unlink_ctx
;
5405 context
->rename
= smbc_rename_ctx
;
5406 context
->lseek
= smbc_lseek_ctx
;
5407 context
->stat
= smbc_stat_ctx
;
5408 context
->fstat
= smbc_fstat_ctx
;
5409 context
->opendir
= smbc_opendir_ctx
;
5410 context
->closedir
= smbc_closedir_ctx
;
5411 context
->readdir
= smbc_readdir_ctx
;
5412 context
->getdents
= smbc_getdents_ctx
;
5413 context
->mkdir
= smbc_mkdir_ctx
;
5414 context
->rmdir
= smbc_rmdir_ctx
;
5415 context
->telldir
= smbc_telldir_ctx
;
5416 context
->lseekdir
= smbc_lseekdir_ctx
;
5417 context
->fstatdir
= smbc_fstatdir_ctx
;
5418 context
->chmod
= smbc_chmod_ctx
;
5419 context
->utimes
= smbc_utimes_ctx
;
5420 context
->setxattr
= smbc_setxattr_ctx
;
5421 context
->getxattr
= smbc_getxattr_ctx
;
5422 context
->removexattr
= smbc_removexattr_ctx
;
5423 context
->listxattr
= smbc_listxattr_ctx
;
5424 context
->open_print_job
= smbc_open_print_job_ctx
;
5425 context
->print_file
= smbc_print_file_ctx
;
5426 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
5427 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
5429 context
->callbacks
.check_server_fn
= smbc_check_server
;
5430 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
5432 smbc_default_cache_functions(context
);
5440 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
5441 * and thus you'll be leaking memory if not handled properly.
5444 int smbc_free_context(SMBCCTX
* context
, int shutdown_ctx
)
5453 DEBUG(1,("Performing aggressive shutdown.\n"));
5455 f
= context
->internal
->_files
;
5457 context
->close_fn(context
, f
);
5460 context
->internal
->_files
= NULL
;
5462 /* First try to remove the servers the nice way. */
5463 if (context
->callbacks
.purge_cached_fn(context
)) {
5466 DEBUG(1, ("Could not purge all servers, Nice way shutdown failed.\n"));
5467 s
= context
->internal
->_servers
;
5469 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n", s
, s
->cli
.fd
));
5470 cli_shutdown(&s
->cli
);
5471 context
->callbacks
.remove_cached_srv_fn(context
, s
);
5473 DLIST_REMOVE(context
->internal
->_servers
, s
);
5477 context
->internal
->_servers
= NULL
;
5481 /* This is the polite way */
5482 if (context
->callbacks
.purge_cached_fn(context
)) {
5483 DEBUG(1, ("Could not purge all servers, free_context failed.\n"));
5487 if (context
->internal
->_servers
) {
5488 DEBUG(1, ("Active servers in context, free_context failed.\n"));
5492 if (context
->internal
->_files
) {
5493 DEBUG(1, ("Active files in context, free_context failed.\n"));
5499 /* Things we have to clean up */
5500 SAFE_FREE(context
->workgroup
);
5501 SAFE_FREE(context
->netbios_name
);
5502 SAFE_FREE(context
->user
);
5504 DEBUG(3, ("Context %p succesfully freed\n", context
));
5505 SAFE_FREE(context
->internal
);
5512 * Initialise the library etc
5514 * We accept a struct containing handle information.
5515 * valid values for info->debug from 0 to 100,
5516 * and insist that info->fn must be non-null.
5518 SMBCCTX
* smbc_init_context(SMBCCTX
* context
)
5522 char *user
= NULL
, *home
= NULL
;
5524 if (!context
|| !context
->internal
) {
5529 /* Do not initialise the same client twice */
5530 if (context
->internal
->_initialized
) {
5534 if (!context
->callbacks
.auth_fn
|| context
->debug
< 0 || context
->debug
> 100) {
5541 if (!smbc_initialized
) {
5542 /* Do some library wide intialisations the first time we get called */
5543 BOOL conf_loaded
= False
;
5545 /* Set this to what the user wants */
5546 DEBUGLEVEL
= context
->debug
;
5548 setup_logging( "libsmbclient", True
);
5550 /* Here we would open the smb.conf file if needed ... */
5552 load_interfaces(); /* Load the list of interfaces ... */
5554 in_client
= True
; /* FIXME, make a param */
5556 home
= getenv("HOME");
5558 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
5559 if (lp_load(conf
, True
, False
, False
)) {
5562 DEBUG(5, ("Could not load config file: %s\n",
5569 * Well, if that failed, try the dyn_CONFIGFILE
5570 * Which points to the standard locn, and if that
5571 * fails, silently ignore it and use the internal
5575 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
)) {
5576 DEBUG(5, ("Could not load config file: %s\n",
5580 * We loaded the global config file. Now lets
5581 * load user-specific modifications to the
5584 slprintf(conf
, sizeof(conf
),
5585 "%s/.smb/smb.conf.append", home
);
5586 if (!lp_load(conf
, True
, False
, False
)) {
5588 ("Could not append config file: "
5595 reopen_logs(); /* Get logging working ... */
5598 * Block SIGPIPE (from lib/util_sock.c: write())
5599 * It is not needed and should not stop execution
5601 BlockSignals(True
, SIGPIPE
);
5603 /* Done with one-time initialisation */
5604 smbc_initialized
= 1;
5608 if (!context
->user
) {
5610 * FIXME: Is this the best way to get the user info?
5612 user
= getenv("USER");
5613 /* walk around as "guest" if no username can be found */
5614 if (!user
) context
->user
= SMB_STRDUP("guest");
5615 else context
->user
= SMB_STRDUP(user
);
5618 if (!context
->netbios_name
) {
5620 * We try to get our netbios name from the config. If that fails we fall
5621 * back on constructing our netbios name from our hostname etc
5623 if (global_myname()) {
5624 context
->netbios_name
= SMB_STRDUP(global_myname());
5628 * Hmmm, I want to get hostname as well, but I am too lazy for the moment
5631 context
->netbios_name
= SMB_MALLOC(17);
5632 if (!context
->netbios_name
) {
5636 slprintf(context
->netbios_name
, 16, "smbc%s%d", context
->user
, pid
);
5640 DEBUG(1, ("Using netbios name %s.\n", context
->netbios_name
));
5642 if (!context
->workgroup
) {
5643 if (lp_workgroup()) {
5644 context
->workgroup
= SMB_STRDUP(lp_workgroup());
5647 /* TODO: Think about a decent default workgroup */
5648 context
->workgroup
= SMB_STRDUP("samba");
5652 DEBUG(1, ("Using workgroup %s.\n", context
->workgroup
));
5654 /* shortest timeout is 1 second */
5655 if (context
->timeout
> 0 && context
->timeout
< 1000)
5656 context
->timeout
= 1000;
5659 * FIXME: Should we check the function pointers here?
5662 context
->internal
->_initialized
= 1;
5668 /* Return the verion of samba, and thus libsmbclient */
5672 return samba_version_string();