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 3 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, see <http://www.gnu.org/licenses/>.
26 #include "include/libsmb_internal.h"
28 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
);
29 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
30 struct smbc_dirent
*dirent
);
33 * DOS Attribute values (used internally)
35 typedef struct DOS_ATTR_DESC
{
47 * Internal flags for extended attributes
50 /* internal mode values */
51 #define SMBC_XATTR_MODE_ADD 1
52 #define SMBC_XATTR_MODE_REMOVE 2
53 #define SMBC_XATTR_MODE_REMOVE_ALL 3
54 #define SMBC_XATTR_MODE_SET 4
55 #define SMBC_XATTR_MODE_CHOWN 5
56 #define SMBC_XATTR_MODE_CHGRP 6
58 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
60 /*We should test for this in configure ... */
62 #define ENOTSUP EOPNOTSUPP
66 * Functions exported by libsmb_cache.c that we need here
68 int smbc_default_cache_functions(SMBCCTX
*context
);
71 * check if an element is part of the list.
72 * FIXME: Does not belong here !
73 * Can anyone put this in a macro in dlinklist.h ?
76 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
77 if (!p
|| !list
) return False
;
79 if (p
== list
) return True
;
86 * Find an lsa pipe handle associated with a cli struct.
88 static struct rpc_pipe_client
*
89 find_lsa_pipe_hnd(struct cli_state
*ipc_cli
)
91 struct rpc_pipe_client
*pipe_hnd
;
93 for (pipe_hnd
= ipc_cli
->pipe_list
;
95 pipe_hnd
= pipe_hnd
->next
) {
97 if (pipe_hnd
->pipe_idx
== PI_LSARPC
) {
106 smbc_close_ctx(SMBCCTX
*context
,
109 smbc_lseek_ctx(SMBCCTX
*context
,
114 extern BOOL in_client
;
117 * Is the logging working / configfile read ?
119 static int smbc_initialized
= 0;
122 hex2int( unsigned int _char
)
124 if ( _char
>= 'A' && _char
<='F')
125 return _char
- 'A' + 10;
126 if ( _char
>= 'a' && _char
<='f')
127 return _char
- 'a' + 10;
128 if ( _char
>= '0' && _char
<='9')
136 * Convert strings of %xx to their single character equivalent. Each 'x' must
137 * be a valid hexadecimal digit, or that % sequence is left undecoded.
139 * dest may, but need not be, the same pointer as src.
141 * Returns the number of % sequences which could not be converted due to lack
142 * of two following hexadecimal digits.
145 smbc_urldecode(char *dest
, char * src
, size_t max_dest_len
)
147 int old_length
= strlen(src
);
153 if ( old_length
== 0 ) {
158 while ( i
< old_length
) {
159 unsigned char character
= src
[ i
++ ];
161 if (character
== '%') {
162 int a
= i
+1 < old_length
? hex2int( src
[i
] ) : -1;
163 int b
= i
+1 < old_length
? hex2int( src
[i
+1] ) : -1;
165 /* Replace valid sequence */
166 if (a
!= -1 && b
!= -1) {
168 /* Replace valid %xx sequence with %dd */
169 character
= (a
* 16) + b
;
171 if (character
== '\0') {
172 break; /* Stop at %00 */
187 strncpy(dest
, temp
, max_dest_len
- 1);
188 dest
[max_dest_len
- 1] = '\0';
196 * Convert any characters not specifically allowed in a URL into their %xx
199 * Returns the remaining buffer length.
202 smbc_urlencode(char * dest
, char * src
, int max_dest_len
)
204 char hex
[] = "0123456789ABCDEF";
206 for (; *src
!= '\0' && max_dest_len
>= 3; src
++) {
218 *dest
++ = hex
[(*src
>> 4) & 0x0f];
219 *dest
++ = hex
[*src
& 0x0f];
234 * Function to parse a path and turn it into components
236 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
237 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
238 * general format ("smb:" only; we do not look for "cifs:").
242 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
246 * smb:// Show all workgroups.
248 * The method of locating the list of workgroups varies
249 * depending upon the setting of the context variable
250 * context->options.browse_max_lmb_count. This value
251 * determine the maximum number of local master browsers to
252 * query for the list of workgroups. In order to ensure that
253 * a complete list of workgroups is obtained, all master
254 * browsers must be queried, but if there are many
255 * workgroups, the time spent querying can begin to add up.
256 * For small networks (not many workgroups), it is suggested
257 * that this variable be set to 0, indicating query all local
258 * master browsers. When the network has many workgroups, a
259 * reasonable setting for this variable might be around 3.
261 * smb://name/ if name<1D> or name<1B> exists, list servers in
262 * workgroup, else, if name<20> exists, list all shares
265 * If "options" are provided, this function returns the entire option list as a
266 * string, for later parsing by the caller. Note that currently, no options
270 static const char *smbc_prefix
= "smb:";
273 smbc_parse_path(SMBCCTX
*context
,
275 char *workgroup
, int workgroup_len
,
276 char *server
, int server_len
,
277 char *share
, int share_len
,
278 char *path
, int path_len
,
279 char *user
, int user_len
,
280 char *password
, int password_len
,
281 char *options
, int options_len
)
289 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
292 * Assume we wont find an authentication domain to parse, so default
293 * to the workgroup in the provided context.
295 if (workgroup
!= NULL
) {
296 strncpy(workgroup
, context
->workgroup
, workgroup_len
- 1);
297 workgroup
[workgroup_len
- 1] = '\0';
300 if (options
!= NULL
&& options_len
> 0) {
301 options
[0] = (char)0;
305 /* see if it has the right prefix */
306 len
= strlen(smbc_prefix
);
307 if (strncmp(s
,smbc_prefix
,len
) || (s
[len
] != '/' && s
[len
] != 0)) {
308 return -1; /* What about no smb: ? */
313 /* Watch the test below, we are testing to see if we should exit */
315 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
317 DEBUG(1, ("Invalid path (does not begin with smb://"));
322 p
+= 2; /* Skip the double slash */
324 /* See if any options were specified */
325 if ((q
= strrchr(p
, '?')) != NULL
) {
326 /* There are options. Null terminate here and point to them */
329 DEBUG(4, ("Found options '%s'", q
));
331 /* Copy the options */
332 if (options
!= NULL
&& options_len
> 0) {
333 safe_strcpy(options
, q
, options_len
- 1);
341 int wl
= strlen(context
->workgroup
);
347 strncpy(server
, context
->workgroup
, wl
);
353 * ok, its for us. Now parse out the server, share etc.
355 * However, we want to parse out [[domain;]user[:password]@] if it
359 /* check that '@' occurs before '/', if '/' exists at all */
360 q
= strchr_m(p
, '@');
361 r
= strchr_m(p
, '/');
362 if (q
&& (!r
|| q
< r
)) {
363 pstring username
, passwd
, domain
;
364 const char *u
= userinfo
;
366 next_token_no_ltrim(&p
, userinfo
, "@", sizeof(fstring
));
368 username
[0] = passwd
[0] = domain
[0] = 0;
370 if (strchr_m(u
, ';')) {
372 next_token_no_ltrim(&u
, domain
, ";", sizeof(fstring
));
376 if (strchr_m(u
, ':')) {
378 next_token_no_ltrim(&u
, username
, ":", sizeof(fstring
));
385 pstrcpy(username
, u
);
389 if (domain
[0] && workgroup
) {
390 strncpy(workgroup
, domain
, workgroup_len
- 1);
391 workgroup
[workgroup_len
- 1] = '\0';
395 strncpy(user
, username
, user_len
- 1);
396 user
[user_len
- 1] = '\0';
400 strncpy(password
, passwd
, password_len
- 1);
401 password
[password_len
- 1] = '\0';
406 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
412 if (*p
== (char)0) goto decoding
; /* That's it ... */
414 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
421 * Prepend a leading slash if there's a file path, as required by
427 safe_strcpy(path
+ 1, p
, path_len
- 2);
430 all_string_sub(path
, "/", "\\", 0);
433 (void) smbc_urldecode(path
, path
, path_len
);
434 (void) smbc_urldecode(server
, server
, server_len
);
435 (void) smbc_urldecode(share
, share
, share_len
);
436 (void) smbc_urldecode(user
, user
, user_len
);
437 (void) smbc_urldecode(password
, password
, password_len
);
443 * Verify that the options specified in a URL are valid
446 smbc_check_options(char *server
,
451 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
452 "path='%s' options='%s'\n",
453 server
, share
, path
, options
));
455 /* No options at all is always ok */
456 if (! *options
) return 0;
458 /* Currently, we don't support any options. */
463 * Convert an SMB error into a UNIX error ...
466 smbc_errno(SMBCCTX
*context
,
469 int ret
= cli_errno(c
);
471 if (cli_is_dos_error(c
)) {
475 cli_dos_error(c
, &eclass
, &ecode
);
477 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
478 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
482 status
= cli_nt_error(c
);
484 DEBUG(3,("smbc errno %s -> %d\n",
485 nt_errstr(status
), ret
));
492 * Check a server for being alive and well.
493 * returns 0 if the server is in shape. Returns 1 on error
495 * Also useable outside libsmbclient to enable external cache
496 * to do some checks too.
499 smbc_check_server(SMBCCTX
* context
,
503 struct sockaddr addr
;
506 * Although the use of port 139 is not a guarantee that we're using
507 * netbios, we assume so. We don't want to send a keepalive packet if
508 * not netbios because it's not valid, and Vista, at least,
509 * disconnects the client on such a request.
511 if (server
->cli
->port
== 139) {
512 /* Assuming netbios. Send a keepalive packet */
513 if ( send_keepalive(server
->cli
->fd
) == False
) {
518 * Assuming not netbios. Try a different method to detect if
519 * the connection is still alive.
522 if (getpeername(server
->cli
->fd
, &addr
, &size
) == -1) {
527 /* connection is ok */
532 * Remove a server from the cached server list it's unused.
533 * On success, 0 is returned. 1 is returned if the server could not be removed.
535 * Also useable outside libsmbclient
538 smbc_remove_unused_server(SMBCCTX
* context
,
543 /* are we being fooled ? */
544 if (!context
|| !context
->internal
||
545 !context
->internal
->_initialized
|| !srv
) return 1;
548 /* Check all open files/directories for a relation with this server */
549 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
550 if (file
->srv
== srv
) {
552 DEBUG(3, ("smbc_remove_usused_server: "
553 "%p still used by %p.\n",
559 DLIST_REMOVE(context
->internal
->_servers
, srv
);
561 cli_shutdown(srv
->cli
);
564 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
566 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
574 find_server(SMBCCTX
*context
,
586 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
587 workgroup
, username
);
589 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
590 if (context
->internal
->_auth_fn_with_context
!= NULL
) {
591 context
->internal
->_auth_fn_with_context(
594 workgroup
, sizeof(fstring
),
595 username
, sizeof(fstring
),
596 password
, sizeof(fstring
));
598 context
->callbacks
.auth_fn(
600 workgroup
, sizeof(fstring
),
601 username
, sizeof(fstring
),
602 password
, sizeof(fstring
));
606 * However, smbc_auth_fn may have picked up info relating to
607 * an existing connection, so try for an existing connection
611 goto check_server_cache
;
616 if (context
->callbacks
.check_server_fn(context
, srv
)) {
618 * This server is no good anymore
619 * Try to remove it and check for more possible
620 * servers in the cache
622 if (context
->callbacks
.remove_unused_server_fn(context
,
625 * We could not remove the server completely,
626 * remove it from the cache so we will not get
627 * it again. It will be removed when the last
628 * file/dir is closed.
630 context
->callbacks
.remove_cached_srv_fn(context
,
635 * Maybe there are more cached connections to this
638 goto check_server_cache
;
648 * Connect to a server, possibly on an existing connection
650 * Here, what we want to do is: If the server and username
651 * match an existing connection, reuse that, otherwise, establish a
654 * If we have to create a new connection, call the auth_fn to get the
655 * info we need, unless the username and password were passed in.
659 smbc_server(SMBCCTX
*context
,
660 BOOL connect_if_not_found
,
669 struct nmb_name called
, calling
;
670 const char *server_n
= server
;
673 int tried_reverse
= 0;
676 const char *username_used
;
682 if (server
[0] == 0) {
687 /* Look for a cached connection */
688 srv
= find_server(context
, server
, share
,
689 workgroup
, username
, password
);
692 * If we found a connection and we're only allowed one share per
695 if (srv
&& *share
!= '\0' && context
->options
.one_share_per_server
) {
698 * ... then if there's no current connection to the share,
699 * connect to it. find_server(), or rather the function
700 * pointed to by context->callbacks.get_cached_srv_fn which
701 * was called by find_server(), will have issued a tree
702 * disconnect if the requested share is not the same as the
703 * one that was already connected.
705 if (srv
->cli
->cnum
== (uint16
) -1) {
706 /* Ensure we have accurate auth info */
707 if (context
->internal
->_auth_fn_with_context
!= NULL
) {
708 context
->internal
->_auth_fn_with_context(
711 workgroup
, sizeof(fstring
),
712 username
, sizeof(fstring
),
713 password
, sizeof(fstring
));
715 context
->callbacks
.auth_fn(
717 workgroup
, sizeof(fstring
),
718 username
, sizeof(fstring
),
719 password
, sizeof(fstring
));
722 if (! cli_send_tconX(srv
->cli
, share
, "?????",
723 password
, strlen(password
)+1)) {
725 errno
= smbc_errno(context
, srv
->cli
);
726 cli_shutdown(srv
->cli
);
728 context
->callbacks
.remove_cached_srv_fn(context
,
734 * Regenerate the dev value since it's based on both
738 srv
->dev
= (dev_t
)(str_checksum(server
) ^
739 str_checksum(share
));
744 /* If we have a connection... */
747 /* ... then we're done here. Give 'em what they came for. */
751 /* If we're not asked to connect when a connection doesn't exist... */
752 if (! connect_if_not_found
) {
753 /* ... then we're done here. */
757 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
758 make_nmb_name(&called
, server
, 0x20);
760 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
762 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
765 slprintf(ipenv
,sizeof(ipenv
)-1,"HOST_%s", server_n
);
769 /* have to open a new connection */
770 if ((c
= cli_initialise()) == NULL
) {
775 if (context
->flags
& SMB_CTX_FLAG_USE_KERBEROS
) {
776 c
->use_kerberos
= True
;
778 if (context
->flags
& SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS
) {
779 c
->fallback_after_kerberos
= True
;
782 c
->timeout
= context
->timeout
;
785 * Force use of port 139 for first try if share is $IPC, empty, or
786 * null, so browse lists can work
788 if (share
== NULL
|| *share
== '\0' || strcmp(share
, "IPC$") == 0) {
789 port_try_first
= 139;
792 port_try_first
= 445;
796 c
->port
= port_try_first
;
798 status
= cli_connect(c
, server_n
, &ip
);
799 if (!NT_STATUS_IS_OK(status
)) {
801 /* First connection attempt failed. Try alternate port. */
802 c
->port
= port_try_next
;
804 status
= cli_connect(c
, server_n
, &ip
);
805 if (!NT_STATUS_IS_OK(status
)) {
812 if (!cli_session_request(c
, &calling
, &called
)) {
814 if (strcmp(called
.name
, "*SMBSERVER")) {
815 make_nmb_name(&called
, "*SMBSERVER", 0x20);
817 } else { /* Try one more time, but ensure we don't loop */
819 /* Only try this if server is an IP address ... */
821 if (is_ipaddress(server
) && !tried_reverse
) {
823 struct in_addr rem_ip
;
825 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
826 DEBUG(4, ("Could not convert IP address "
827 "%s to struct in_addr\n", server
));
832 tried_reverse
++; /* Yuck */
834 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
835 make_nmb_name(&called
, remote_name
, 0x20);
844 DEBUG(4,(" session request ok\n"));
846 if (!cli_negprot(c
)) {
852 username_used
= username
;
854 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
855 password
, strlen(password
),
856 password
, strlen(password
),
859 /* Failed. Try an anonymous login, if allowed by flags. */
862 if ((context
->flags
& SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON
) ||
863 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
874 DEBUG(4,(" session setup ok\n"));
876 if (!cli_send_tconX(c
, share
, "?????",
877 password
, strlen(password
)+1)) {
878 errno
= smbc_errno(context
, c
);
883 DEBUG(4,(" tconx ok\n"));
886 * Ok, we have got a nice connection
887 * Let's allocate a server structure.
890 srv
= SMB_MALLOC_P(SMBCSRV
);
898 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
899 srv
->no_pathinfo
= False
;
900 srv
->no_pathinfo2
= False
;
901 srv
->no_nt_session
= False
;
903 /* now add it to the cache (internal or external) */
904 /* Let the cache function set errno if it wants to */
906 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
907 int saved_errno
= errno
;
908 DEBUG(3, (" Failed to add server to cache\n"));
916 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
917 server
, share
, srv
));
919 DLIST_ADD(context
->internal
->_servers
, srv
);
933 * Connect to a server for getting/setting attributes, possibly on an existing
934 * connection. This works similarly to smbc_server().
937 smbc_attr_server(SMBCCTX
*context
,
947 struct cli_state
*ipc_cli
;
948 struct rpc_pipe_client
*pipe_hnd
;
950 SMBCSRV
*ipc_srv
=NULL
;
953 * See if we've already created this special connection. Reference
954 * our "special" share name '*IPC$', which is an impossible real share
955 * name due to the leading asterisk.
957 ipc_srv
= find_server(context
, server
, "*IPC$",
958 workgroup
, username
, password
);
961 /* We didn't find a cached connection. Get the password */
962 if (*password
== '\0') {
963 /* ... then retrieve it now. */
964 if (context
->internal
->_auth_fn_with_context
!= NULL
) {
965 context
->internal
->_auth_fn_with_context(
968 workgroup
, sizeof(fstring
),
969 username
, sizeof(fstring
),
970 password
, sizeof(fstring
));
972 context
->callbacks
.auth_fn(
974 workgroup
, sizeof(fstring
),
975 username
, sizeof(fstring
),
976 password
, sizeof(fstring
));
981 if (context
->flags
& SMB_CTX_FLAG_USE_KERBEROS
) {
982 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
986 nt_status
= cli_full_connection(&ipc_cli
,
987 global_myname(), server
,
988 &ip
, 0, "IPC$", "?????",
992 if (! NT_STATUS_IS_OK(nt_status
)) {
993 DEBUG(1,("cli_full_connection failed! (%s)\n",
994 nt_errstr(nt_status
)));
999 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
1002 cli_shutdown(ipc_cli
);
1006 ZERO_STRUCTP(ipc_srv
);
1007 ipc_srv
->cli
= ipc_cli
;
1010 pipe_hnd
= cli_rpc_pipe_open_noauth(ipc_srv
->cli
,
1014 DEBUG(1, ("cli_nt_session_open fail!\n"));
1016 cli_shutdown(ipc_srv
->cli
);
1022 * Some systems don't support
1023 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1024 * so we might as well do it too.
1027 nt_status
= rpccli_lsa_open_policy(
1029 ipc_srv
->cli
->mem_ctx
,
1031 GENERIC_EXECUTE_ACCESS
,
1034 if (!NT_STATUS_IS_OK(nt_status
)) {
1035 errno
= smbc_errno(context
, ipc_srv
->cli
);
1036 cli_shutdown(ipc_srv
->cli
);
1041 /* now add it to the cache (internal or external) */
1043 errno
= 0; /* let cache function set errno if it likes */
1044 if (context
->callbacks
.add_cached_srv_fn(context
, ipc_srv
,
1049 DEBUG(3, (" Failed to add server to cache\n"));
1053 cli_shutdown(ipc_srv
->cli
);
1058 DLIST_ADD(context
->internal
->_servers
, ipc_srv
);
1065 * Routine to open() a file ...
1069 smbc_open_ctx(SMBCCTX
*context
,
1074 fstring server
, share
, user
, password
, workgroup
;
1077 struct cli_state
*targetcli
;
1078 SMBCSRV
*srv
= NULL
;
1079 SMBCFILE
*file
= NULL
;
1082 if (!context
|| !context
->internal
||
1083 !context
->internal
->_initialized
) {
1085 errno
= EINVAL
; /* Best I can think of ... */
1097 if (smbc_parse_path(context
, fname
,
1098 workgroup
, sizeof(workgroup
),
1099 server
, sizeof(server
),
1100 share
, sizeof(share
),
1103 password
, sizeof(password
),
1109 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1111 srv
= smbc_server(context
, True
,
1112 server
, share
, workgroup
, user
, password
);
1116 if (errno
== EPERM
) errno
= EACCES
;
1117 return NULL
; /* smbc_server sets errno */
1121 /* Hmmm, the test for a directory is suspect here ... FIXME */
1123 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
1130 file
= SMB_MALLOC_P(SMBCFILE
);
1141 /*d_printf(">>>open: resolving %s\n", path);*/
1142 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
1144 d_printf("Could not resolve %s\n", path
);
1148 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1150 if ((fd
= cli_open(targetcli
, targetpath
, flags
,
1151 context
->internal
->_share_mode
)) < 0) {
1153 /* Handle the error ... */
1156 errno
= smbc_errno(context
, targetcli
);
1161 /* Fill in file struct */
1164 file
->fname
= SMB_STRDUP(fname
);
1169 DLIST_ADD(context
->internal
->_files
, file
);
1172 * If the file was opened in O_APPEND mode, all write
1173 * operations should be appended to the file. To do that,
1174 * though, using this protocol, would require a getattrE()
1175 * call for each and every write, to determine where the end
1176 * of the file is. (There does not appear to be an append flag
1177 * in the protocol.) Rather than add all of that overhead of
1178 * retrieving the current end-of-file offset prior to each
1179 * write operation, we'll assume that most append operations
1180 * will continuously write, so we'll just set the offset to
1181 * the end of the file now and hope that's adequate.
1183 * Note to self: If this proves inadequate, and O_APPEND
1184 * should, in some cases, be forced for each write, add a
1185 * field in the context options structure, for
1186 * "strict_append_mode" which would select between the current
1187 * behavior (if FALSE) or issuing a getattrE() prior to each
1188 * write and forcing the write to the end of the file (if
1189 * TRUE). Adding that capability will likely require adding
1190 * an "append" flag into the _SMBCFILE structure to track
1191 * whether a file was opened in O_APPEND mode. -- djl
1193 if (flags
& O_APPEND
) {
1194 if (smbc_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
1195 (void) smbc_close_ctx(context
, file
);
1205 /* Check if opendir needed ... */
1210 eno
= smbc_errno(context
, srv
->cli
);
1211 file
= context
->opendir(context
, fname
);
1212 if (!file
) errno
= eno
;
1217 errno
= EINVAL
; /* FIXME, correct errno ? */
1223 * Routine to create a file
1226 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
1229 smbc_creat_ctx(SMBCCTX
*context
,
1234 if (!context
|| !context
->internal
||
1235 !context
->internal
->_initialized
) {
1242 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
1246 * Routine to read() a file ...
1250 smbc_read_ctx(SMBCCTX
*context
,
1256 fstring server
, share
, user
, password
;
1257 pstring path
, targetpath
;
1258 struct cli_state
*targetcli
;
1263 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1264 * appears to pass file->offset (which is type off_t) differently than
1265 * a local variable of type off_t. Using local variable "offset" in
1266 * the call to cli_read() instead of file->offset fixes a problem
1267 * retrieving data at an offset greater than 4GB.
1271 if (!context
|| !context
->internal
||
1272 !context
->internal
->_initialized
) {
1279 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
1281 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1288 offset
= file
->offset
;
1290 /* Check that the buffer exists ... */
1299 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1300 if (smbc_parse_path(context
, file
->fname
,
1302 server
, sizeof(server
),
1303 share
, sizeof(share
),
1306 password
, sizeof(password
),
1312 /*d_printf(">>>read: resolving %s\n", path);*/
1313 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1314 &targetcli
, targetpath
))
1316 d_printf("Could not resolve %s\n", path
);
1319 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1321 ret
= cli_read(targetcli
, file
->cli_fd
, (char *)buf
, offset
, count
);
1325 errno
= smbc_errno(context
, targetcli
);
1330 file
->offset
+= ret
;
1332 DEBUG(4, (" --> %d\n", ret
));
1334 return ret
; /* Success, ret bytes of data ... */
1339 * Routine to write() a file ...
1343 smbc_write_ctx(SMBCCTX
*context
,
1350 fstring server
, share
, user
, password
;
1351 pstring path
, targetpath
;
1352 struct cli_state
*targetcli
;
1354 /* First check all pointers before dereferencing them */
1356 if (!context
|| !context
->internal
||
1357 !context
->internal
->_initialized
) {
1364 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1371 /* Check that the buffer exists ... */
1380 offset
= file
->offset
; /* See "offset" comment in smbc_read_ctx() */
1382 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1383 if (smbc_parse_path(context
, file
->fname
,
1385 server
, sizeof(server
),
1386 share
, sizeof(share
),
1389 password
, sizeof(password
),
1395 /*d_printf(">>>write: resolving %s\n", path);*/
1396 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1397 &targetcli
, targetpath
))
1399 d_printf("Could not resolve %s\n", path
);
1402 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1405 ret
= cli_write(targetcli
, file
->cli_fd
, 0, (char *)buf
, offset
, count
);
1409 errno
= smbc_errno(context
, targetcli
);
1414 file
->offset
+= ret
;
1416 return ret
; /* Success, 0 bytes of data ... */
1420 * Routine to close() a file ...
1424 smbc_close_ctx(SMBCCTX
*context
,
1428 fstring server
, share
, user
, password
;
1429 pstring path
, targetpath
;
1430 struct cli_state
*targetcli
;
1432 if (!context
|| !context
->internal
||
1433 !context
->internal
->_initialized
) {
1440 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1450 return context
->closedir(context
, file
);
1454 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1455 if (smbc_parse_path(context
, file
->fname
,
1457 server
, sizeof(server
),
1458 share
, sizeof(share
),
1461 password
, sizeof(password
),
1467 /*d_printf(">>>close: resolving %s\n", path);*/
1468 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1469 &targetcli
, targetpath
))
1471 d_printf("Could not resolve %s\n", path
);
1474 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1476 if (!cli_close(targetcli
, file
->cli_fd
)) {
1478 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1480 /* Deallocate slot and remove the server
1481 * from the server cache if unused */
1482 errno
= smbc_errno(context
, targetcli
);
1484 DLIST_REMOVE(context
->internal
->_files
, file
);
1485 SAFE_FREE(file
->fname
);
1487 context
->callbacks
.remove_unused_server_fn(context
, srv
);
1493 DLIST_REMOVE(context
->internal
->_files
, file
);
1494 SAFE_FREE(file
->fname
);
1501 * Get info from an SMB server on a file. Use a qpathinfo call first
1502 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1505 smbc_getatr(SMBCCTX
* context
,
1510 struct timespec
*create_time_ts
,
1511 struct timespec
*access_time_ts
,
1512 struct timespec
*write_time_ts
,
1513 struct timespec
*change_time_ts
,
1518 struct cli_state
*targetcli
;
1521 if (!context
|| !context
->internal
||
1522 !context
->internal
->_initialized
) {
1529 /* path fixup for . and .. */
1530 if (strequal(path
, ".") || strequal(path
, ".."))
1531 pstrcpy(fixedpath
, "\\");
1534 pstrcpy(fixedpath
, path
);
1535 trim_string(fixedpath
, NULL
, "\\..");
1536 trim_string(fixedpath
, NULL
, "\\.");
1538 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1540 if (!cli_resolve_path( "", srv
->cli
, fixedpath
, &targetcli
, targetpath
))
1542 d_printf("Couldn't resolve %s\n", path
);
1546 if (!srv
->no_pathinfo2
&&
1547 cli_qpathinfo2(targetcli
, targetpath
,
1556 /* if this is NT then don't bother with the getatr */
1557 if (targetcli
->capabilities
& CAP_NT_SMBS
) {
1562 if (cli_getatr(targetcli
, targetpath
, mode
, size
, &write_time
)) {
1564 struct timespec w_time_ts
;
1566 w_time_ts
= convert_time_t_to_timespec(write_time
);
1568 if (write_time_ts
!= NULL
) {
1569 *write_time_ts
= w_time_ts
;
1572 if (create_time_ts
!= NULL
) {
1573 *create_time_ts
= w_time_ts
;
1576 if (access_time_ts
!= NULL
) {
1577 *access_time_ts
= w_time_ts
;
1580 if (change_time_ts
!= NULL
) {
1581 *change_time_ts
= w_time_ts
;
1584 srv
->no_pathinfo2
= True
;
1594 * Set file info on an SMB server. Use setpathinfo call first. If that
1595 * fails, use setattrE..
1597 * Access and modification time parameters are always used and must be
1598 * provided. Create time, if zero, will be determined from the actual create
1599 * time of the file. If non-zero, the create time will be set as well.
1601 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1604 smbc_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
1615 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1616 * modern function for "new code" to be using, and it works given a
1617 * filename rather than requiring that the file be opened to have its
1618 * attributes manipulated.
1620 if (srv
->no_pathinfo
||
1621 ! cli_setpathinfo(srv
->cli
, path
,
1629 * setpathinfo is not supported; go to plan B.
1631 * cli_setatr() does not work on win98, and it also doesn't
1632 * support setting the access time (only the modification
1633 * time), so in all cases, we open the specified file and use
1634 * cli_setattrE() which should work on all OS versions, and
1635 * supports both times.
1638 /* Don't try {q,set}pathinfo() again, with this server */
1639 srv
->no_pathinfo
= True
;
1642 if ((fd
= cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
)) < 0) {
1644 errno
= smbc_errno(context
, srv
->cli
);
1648 /* Set the new attributes */
1649 ret
= cli_setattrE(srv
->cli
, fd
,
1654 /* Close the file */
1655 cli_close(srv
->cli
, fd
);
1658 * Unfortunately, setattrE() doesn't have a provision for
1659 * setting the access mode (attributes). We'll have to try
1660 * cli_setatr() for that, and with only this parameter, it
1661 * seems to work on win98.
1663 if (ret
&& mode
!= (uint16
) -1) {
1664 ret
= cli_setatr(srv
->cli
, path
, mode
, 0);
1668 errno
= smbc_errno(context
, srv
->cli
);
1677 * Routine to unlink() a file
1681 smbc_unlink_ctx(SMBCCTX
*context
,
1684 fstring server
, share
, user
, password
, workgroup
;
1685 pstring path
, targetpath
;
1686 struct cli_state
*targetcli
;
1687 SMBCSRV
*srv
= NULL
;
1689 if (!context
|| !context
->internal
||
1690 !context
->internal
->_initialized
) {
1692 errno
= EINVAL
; /* Best I can think of ... */
1704 if (smbc_parse_path(context
, fname
,
1705 workgroup
, sizeof(workgroup
),
1706 server
, sizeof(server
),
1707 share
, sizeof(share
),
1710 password
, sizeof(password
),
1716 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1718 srv
= smbc_server(context
, True
,
1719 server
, share
, workgroup
, user
, password
);
1723 return -1; /* smbc_server sets errno */
1727 /*d_printf(">>>unlink: resolving %s\n", path);*/
1728 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
1730 d_printf("Could not resolve %s\n", path
);
1733 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1735 if (!cli_unlink(targetcli
, targetpath
)) {
1737 errno
= smbc_errno(context
, targetcli
);
1739 if (errno
== EACCES
) { /* Check if the file is a directory */
1744 struct timespec write_time_ts
;
1745 struct timespec access_time_ts
;
1746 struct timespec change_time_ts
;
1749 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1756 /* Hmmm, bad error ... What? */
1758 errno
= smbc_errno(context
, targetcli
);
1764 if (IS_DOS_DIR(mode
))
1767 errno
= saverr
; /* Restore this */
1776 return 0; /* Success ... */
1781 * Routine to rename() a file
1785 smbc_rename_ctx(SMBCCTX
*ocontext
,
1801 pstring targetpath1
;
1802 pstring targetpath2
;
1803 struct cli_state
*targetcli1
;
1804 struct cli_state
*targetcli2
;
1805 SMBCSRV
*srv
= NULL
;
1807 if (!ocontext
|| !ncontext
||
1808 !ocontext
->internal
|| !ncontext
->internal
||
1809 !ocontext
->internal
->_initialized
||
1810 !ncontext
->internal
->_initialized
) {
1812 errno
= EINVAL
; /* Best I can think of ... */
1817 if (!oname
|| !nname
) {
1824 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1826 smbc_parse_path(ocontext
, oname
,
1827 workgroup
, sizeof(workgroup
),
1828 server1
, sizeof(server1
),
1829 share1
, sizeof(share1
),
1830 path1
, sizeof(path1
),
1831 user1
, sizeof(user1
),
1832 password1
, sizeof(password1
),
1835 if (user1
[0] == (char)0) fstrcpy(user1
, ocontext
->user
);
1837 smbc_parse_path(ncontext
, nname
,
1839 server2
, sizeof(server2
),
1840 share2
, sizeof(share2
),
1841 path2
, sizeof(path2
),
1842 user2
, sizeof(user2
),
1843 password2
, sizeof(password2
),
1846 if (user2
[0] == (char)0) fstrcpy(user2
, ncontext
->user
);
1848 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1849 strcmp(user1
, user2
)) {
1851 /* Can't rename across file systems, or users?? */
1858 srv
= smbc_server(ocontext
, True
,
1859 server1
, share1
, workgroup
, user1
, password1
);
1866 /*d_printf(">>>rename: resolving %s\n", path1);*/
1867 if (!cli_resolve_path( "", srv
->cli
, path1
, &targetcli1
, targetpath1
))
1869 d_printf("Could not resolve %s\n", path1
);
1872 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1873 /*d_printf(">>>rename: resolving %s\n", path2);*/
1874 if (!cli_resolve_path( "", srv
->cli
, path2
, &targetcli2
, targetpath2
))
1876 d_printf("Could not resolve %s\n", path2
);
1879 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1881 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
1882 strcmp(targetcli1
->share
, targetcli2
->share
))
1884 /* can't rename across file systems */
1890 if (!cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1891 int eno
= smbc_errno(ocontext
, targetcli1
);
1893 if (eno
!= EEXIST
||
1894 !cli_unlink(targetcli1
, targetpath2
) ||
1895 !cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1903 return 0; /* Success */
1908 * A routine to lseek() a file
1912 smbc_lseek_ctx(SMBCCTX
*context
,
1918 fstring server
, share
, user
, password
;
1919 pstring path
, targetpath
;
1920 struct cli_state
*targetcli
;
1922 if (!context
|| !context
->internal
||
1923 !context
->internal
->_initialized
) {
1930 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1940 return -1; /* Can't lseek a dir ... */
1946 file
->offset
= offset
;
1950 file
->offset
+= offset
;
1954 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1955 if (smbc_parse_path(context
, file
->fname
,
1957 server
, sizeof(server
),
1958 share
, sizeof(share
),
1961 password
, sizeof(password
),
1968 /*d_printf(">>>lseek: resolving %s\n", path);*/
1969 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1970 &targetcli
, targetpath
))
1972 d_printf("Could not resolve %s\n", path
);
1975 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1977 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, NULL
,
1978 &size
, NULL
, NULL
, NULL
, NULL
, NULL
))
1980 SMB_OFF_T b_size
= size
;
1981 if (!cli_getattrE(targetcli
, file
->cli_fd
,
1982 NULL
, &b_size
, NULL
, NULL
, NULL
))
1989 file
->offset
= size
+ offset
;
1998 return file
->offset
;
2003 * Generate an inode number from file name for those things that need it
2007 smbc_inode(SMBCCTX
*context
,
2011 if (!context
|| !context
->internal
||
2012 !context
->internal
->_initialized
) {
2019 if (!*name
) return 2; /* FIXME, why 2 ??? */
2020 return (ino_t
)str_checksum(name
);
2025 * Routine to put basic stat info into a stat structure ... Used by stat and
2030 smbc_setup_stat(SMBCCTX
*context
,
2039 if (IS_DOS_DIR(mode
)) {
2040 st
->st_mode
= SMBC_DIR_MODE
;
2042 st
->st_mode
= SMBC_FILE_MODE
;
2045 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
2046 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
2047 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
2048 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
2051 #ifdef HAVE_STAT_ST_BLKSIZE
2052 st
->st_blksize
= 512;
2054 #ifdef HAVE_STAT_ST_BLOCKS
2055 st
->st_blocks
= (size
+511)/512;
2057 st
->st_uid
= getuid();
2058 st
->st_gid
= getgid();
2060 if (IS_DOS_DIR(mode
)) {
2066 if (st
->st_ino
== 0) {
2067 st
->st_ino
= smbc_inode(context
, fname
);
2070 return True
; /* FIXME: Is this needed ? */
2075 * Routine to stat a file given a name
2079 smbc_stat_ctx(SMBCCTX
*context
,
2090 struct timespec write_time_ts
;
2091 struct timespec access_time_ts
;
2092 struct timespec change_time_ts
;
2097 if (!context
|| !context
->internal
||
2098 !context
->internal
->_initialized
) {
2100 errno
= EINVAL
; /* Best I can think of ... */
2112 DEBUG(4, ("smbc_stat(%s)\n", fname
));
2114 if (smbc_parse_path(context
, fname
,
2115 workgroup
, sizeof(workgroup
),
2116 server
, sizeof(server
),
2117 share
, sizeof(share
),
2120 password
, sizeof(password
),
2126 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2128 srv
= smbc_server(context
, True
,
2129 server
, share
, workgroup
, user
, password
);
2132 return -1; /* errno set by smbc_server */
2135 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
2142 errno
= smbc_errno(context
, srv
->cli
);
2149 smbc_setup_stat(context
, st
, path
, size
, mode
);
2151 set_atimespec(st
, access_time_ts
);
2152 set_ctimespec(st
, change_time_ts
);
2153 set_mtimespec(st
, write_time_ts
);
2154 st
->st_dev
= srv
->dev
;
2161 * Routine to stat a file given an fd
2165 smbc_fstat_ctx(SMBCCTX
*context
,
2169 struct timespec change_time_ts
;
2170 struct timespec access_time_ts
;
2171 struct timespec write_time_ts
;
2180 struct cli_state
*targetcli
;
2183 if (!context
|| !context
->internal
||
2184 !context
->internal
->_initialized
) {
2191 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
2200 return context
->fstatdir(context
, file
, st
);
2204 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2205 if (smbc_parse_path(context
, file
->fname
,
2207 server
, sizeof(server
),
2208 share
, sizeof(share
),
2211 password
, sizeof(password
),
2217 /*d_printf(">>>fstat: resolving %s\n", path);*/
2218 if (!cli_resolve_path("", file
->srv
->cli
, path
,
2219 &targetcli
, targetpath
))
2221 d_printf("Could not resolve %s\n", path
);
2224 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2226 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, &mode
, &size
,
2233 time_t change_time
, access_time
, write_time
;
2235 if (!cli_getattrE(targetcli
, file
->cli_fd
, &mode
, &size
,
2236 &change_time
, &access_time
, &write_time
)) {
2242 change_time_ts
= convert_time_t_to_timespec(change_time
);
2243 access_time_ts
= convert_time_t_to_timespec(access_time
);
2244 write_time_ts
= convert_time_t_to_timespec(write_time
);
2249 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
2251 set_atimespec(st
, access_time_ts
);
2252 set_ctimespec(st
, change_time_ts
);
2253 set_mtimespec(st
, write_time_ts
);
2254 st
->st_dev
= file
->srv
->dev
;
2261 * Routine to open a directory
2262 * We accept the URL syntax explained in smbc_parse_path(), above.
2266 smbc_remove_dir(SMBCFILE
*dir
)
2268 struct smbc_dir_list
*d
,*f
;
2275 SAFE_FREE(f
->dirent
);
2280 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
2285 add_dirent(SMBCFILE
*dir
,
2287 const char *comment
,
2290 struct smbc_dirent
*dirent
;
2292 int name_length
= (name
== NULL
? 0 : strlen(name
));
2293 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
2296 * Allocate space for the dirent, which must be increased by the
2297 * size of the name and the comment and 1 each for the null terminator.
2300 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
2302 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
2306 dir
->dir_error
= ENOMEM
;
2311 ZERO_STRUCTP(dirent
);
2313 if (dir
->dir_list
== NULL
) {
2315 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
2316 if (!dir
->dir_list
) {
2319 dir
->dir_error
= ENOMEM
;
2323 ZERO_STRUCTP(dir
->dir_list
);
2325 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
2329 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
2331 if (!dir
->dir_end
->next
) {
2334 dir
->dir_error
= ENOMEM
;
2338 ZERO_STRUCTP(dir
->dir_end
->next
);
2340 dir
->dir_end
= dir
->dir_end
->next
;
2343 dir
->dir_end
->next
= NULL
;
2344 dir
->dir_end
->dirent
= dirent
;
2346 dirent
->smbc_type
= type
;
2347 dirent
->namelen
= name_length
;
2348 dirent
->commentlen
= comment_len
;
2349 dirent
->dirlen
= size
;
2352 * dirent->namelen + 1 includes the null (no null termination needed)
2353 * Ditto for dirent->commentlen.
2354 * The space for the two null bytes was allocated.
2356 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
2357 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
2358 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
2365 list_unique_wg_fn(const char *name
,
2367 const char *comment
,
2370 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2371 struct smbc_dir_list
*dir_list
;
2372 struct smbc_dirent
*dirent
;
2376 dirent_type
= dir
->dir_type
;
2378 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2380 /* An error occurred, what do we do? */
2381 /* FIXME: Add some code here */
2384 /* Point to the one just added */
2385 dirent
= dir
->dir_end
->dirent
;
2387 /* See if this was a duplicate */
2388 for (dir_list
= dir
->dir_list
;
2389 dir_list
!= dir
->dir_end
;
2390 dir_list
= dir_list
->next
) {
2392 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
2393 /* Duplicate. End end of list need to be removed. */
2397 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
2398 /* Found the end of the list. Remove it. */
2399 dir
->dir_end
= dir_list
;
2400 free(dir_list
->next
);
2402 dir_list
->next
= NULL
;
2409 list_fn(const char *name
,
2411 const char *comment
,
2414 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2418 * We need to process the type a little ...
2420 * Disk share = 0x00000000
2421 * Print share = 0x00000001
2422 * Comms share = 0x00000002 (obsolete?)
2423 * IPC$ share = 0x00000003
2425 * administrative shares:
2426 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2429 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
2432 case 0 | 0x80000000:
2434 dirent_type
= SMBC_FILE_SHARE
;
2438 dirent_type
= SMBC_PRINTER_SHARE
;
2442 dirent_type
= SMBC_COMMS_SHARE
;
2445 case 3 | 0x80000000:
2447 dirent_type
= SMBC_IPC_SHARE
;
2451 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
2456 dirent_type
= dir
->dir_type
;
2459 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2461 /* An error occurred, what do we do? */
2462 /* FIXME: Add some code here */
2468 dir_list_fn(const char *mnt
,
2474 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
2475 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
2477 /* Handle an error ... */
2479 /* FIXME: Add some code ... */
2486 net_share_enum_rpc(struct cli_state
*cli
,
2487 void (*fn
)(const char *name
,
2489 const char *comment
,
2496 uint32 info_level
= 1;
2497 uint32 preferred_len
= 0xffffffff;
2498 struct srvsvc_NetShareCtr1 ctr1
;
2499 union srvsvc_NetShareCtr ctr
;
2501 struct rpc_pipe_client
*pipe_hnd
;
2505 /* Open the server service pipe */
2506 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SRVSVC
, &nt_status
);
2508 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2512 /* Allocate a context for parsing and for the entries in "ctr" */
2513 mem_ctx
= talloc_init("libsmbclient: net_share_enum_rpc");
2514 if (mem_ctx
== NULL
) {
2515 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2516 cli_rpc_pipe_close(pipe_hnd
);
2523 /* Issue the NetShareEnum RPC call and retrieve the response */
2525 result
= rpccli_srvsvc_NetShareEnum(pipe_hnd
, mem_ctx
, NULL
,
2526 &info_level
, &ctr
, preferred_len
,
2527 &numentries
, &enum_hnd
);
2529 /* Was it successful? */
2530 if (!NT_STATUS_IS_OK(result
) || numentries
== 0) {
2531 /* Nope. Go clean up. */
2535 /* For each returned entry... */
2536 for (i
= 0; i
< numentries
; i
++) {
2538 /* Add this share to the list */
2539 (*fn
)(ctr
.ctr1
->array
[i
].name
,
2540 ctr
.ctr1
->array
[i
].type
,
2541 ctr
.ctr1
->array
[i
].comment
, state
);
2545 /* Close the server service pipe */
2546 cli_rpc_pipe_close(pipe_hnd
);
2548 /* Free all memory which was allocated for this request */
2549 TALLOC_FREE(mem_ctx
);
2551 /* Tell 'em if it worked */
2552 return NT_STATUS_IS_OK(result
) ? 0 : -1;
2558 smbc_opendir_ctx(SMBCCTX
*context
,
2562 fstring server
, share
, user
, password
, options
;
2567 SMBCSRV
*srv
= NULL
;
2568 SMBCFILE
*dir
= NULL
;
2569 struct _smbc_callbacks
*cb
;
2570 struct in_addr rem_ip
;
2572 if (!context
|| !context
->internal
||
2573 !context
->internal
->_initialized
) {
2574 DEBUG(4, ("no valid context\n"));
2575 errno
= EINVAL
+ 8192;
2581 DEBUG(4, ("no valid fname\n"));
2582 errno
= EINVAL
+ 8193;
2586 if (smbc_parse_path(context
, fname
,
2587 workgroup
, sizeof(workgroup
),
2588 server
, sizeof(server
),
2589 share
, sizeof(share
),
2592 password
, sizeof(password
),
2593 options
, sizeof(options
))) {
2594 DEBUG(4, ("no valid path\n"));
2595 errno
= EINVAL
+ 8194;
2599 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2600 "path='%s' options='%s'\n",
2601 fname
, server
, share
, path
, options
));
2603 /* Ensure the options are valid */
2604 if (smbc_check_options(server
, share
, path
, options
)) {
2605 DEBUG(4, ("unacceptable options (%s)\n", options
));
2606 errno
= EINVAL
+ 8195;
2610 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2612 dir
= SMB_MALLOC_P(SMBCFILE
);
2624 dir
->fname
= SMB_STRDUP(fname
);
2628 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
2630 if (server
[0] == (char)0) {
2635 struct ip_service
*ip_list
;
2636 struct ip_service server_addr
;
2637 struct user_auth_info u_info
;
2638 struct cli_state
*cli
;
2640 if (share
[0] != (char)0 || path
[0] != (char)0) {
2642 errno
= EINVAL
+ 8196;
2644 SAFE_FREE(dir
->fname
);
2650 /* Determine how many local master browsers to query */
2651 max_lmb_count
= (context
->options
.browse_max_lmb_count
== 0
2653 : context
->options
.browse_max_lmb_count
);
2655 pstrcpy(u_info
.username
, user
);
2656 pstrcpy(u_info
.password
, password
);
2659 * We have server and share and path empty but options
2660 * requesting that we scan all master browsers for their list
2661 * of workgroups/domains. This implies that we must first try
2662 * broadcast queries to find all master browsers, and if that
2663 * doesn't work, then try our other methods which return only
2664 * a single master browser.
2668 if (!name_resolve_bcast(MSBROWSE
, 1, &ip_list
, &count
)) {
2672 if (!find_master_ip(workgroup
, &server_addr
.ip
)) {
2675 SAFE_FREE(dir
->fname
);
2682 ip_list
= &server_addr
;
2686 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
2687 DEBUG(99, ("Found master browser %d of %d: %s\n",
2688 i
+1, MAX(count
, max_lmb_count
),
2689 inet_ntoa(ip_list
[i
].ip
)));
2691 cli
= get_ipc_connect_master_ip(&ip_list
[i
],
2692 workgroup
, &u_info
);
2693 /* cli == NULL is the master browser refused to talk or
2694 could not be found */
2698 fstrcpy(server
, cli
->desthost
);
2701 DEBUG(4, ("using workgroup %s %s\n",
2702 workgroup
, server
));
2705 * For each returned master browser IP address, get a
2706 * connection to IPC$ on the server if we do not
2707 * already have one, and determine the
2708 * workgroups/domains that it knows about.
2711 srv
= smbc_server(context
, True
, server
, "IPC$",
2712 workgroup
, user
, password
);
2718 dir
->dir_type
= SMBC_WORKGROUP
;
2720 /* Now, list the stuff ... */
2722 if (!cli_NetServerEnum(srv
->cli
,
2724 SV_TYPE_DOMAIN_ENUM
,
2734 * Server not an empty string ... Check the rest and see what
2737 if (*share
== '\0') {
2738 if (*path
!= '\0') {
2740 /* Should not have empty share with path */
2741 errno
= EINVAL
+ 8197;
2743 SAFE_FREE(dir
->fname
);
2751 * We don't know if <server> is really a server name
2752 * or is a workgroup/domain name. If we already have
2753 * a server structure for it, we'll use it.
2754 * Otherwise, check to see if <server><1D>,
2755 * <server><1B>, or <server><20> translates. We check
2756 * to see if <server> is an IP address first.
2760 * See if we have an existing server. Do not
2761 * establish a connection if one does not already
2764 srv
= smbc_server(context
, False
, server
, "IPC$",
2765 workgroup
, user
, password
);
2768 * If no existing server and not an IP addr, look for
2772 !is_ipaddress(server
) &&
2773 (resolve_name(server
, &rem_ip
, 0x1d) || /* LMB */
2774 resolve_name(server
, &rem_ip
, 0x1b) )) { /* DMB */
2778 dir
->dir_type
= SMBC_SERVER
;
2781 * Get the backup list ...
2783 if (!name_status_find(server
, 0, 0,
2784 rem_ip
, buserver
)) {
2786 DEBUG(0, ("Could not get name of "
2787 "local/domain master browser "
2788 "for server %s\n", server
));
2790 SAFE_FREE(dir
->fname
);
2799 * Get a connection to IPC$ on the server if
2800 * we do not already have one
2802 srv
= smbc_server(context
, True
,
2804 workgroup
, user
, password
);
2806 DEBUG(0, ("got no contact to IPC$\n"));
2808 SAFE_FREE(dir
->fname
);
2817 /* Now, list the servers ... */
2818 if (!cli_NetServerEnum(srv
->cli
, server
,
2819 0x0000FFFE, list_fn
,
2823 SAFE_FREE(dir
->fname
);
2829 (resolve_name(server
, &rem_ip
, 0x20))) {
2831 /* If we hadn't found the server, get one now */
2833 srv
= smbc_server(context
, True
,
2841 SAFE_FREE(dir
->fname
);
2848 dir
->dir_type
= SMBC_FILE_SHARE
;
2851 /* List the shares ... */
2853 if (net_share_enum_rpc(
2856 (void *) dir
) < 0 &&
2862 errno
= cli_errno(srv
->cli
);
2864 SAFE_FREE(dir
->fname
);
2871 /* Neither the workgroup nor server exists */
2872 errno
= ECONNREFUSED
;
2874 SAFE_FREE(dir
->fname
);
2883 * The server and share are specified ... work from
2887 struct cli_state
*targetcli
;
2889 /* We connect to the server and list the directory */
2890 dir
->dir_type
= SMBC_FILE_SHARE
;
2892 srv
= smbc_server(context
, True
, server
, share
,
2893 workgroup
, user
, password
);
2898 SAFE_FREE(dir
->fname
);
2907 /* Now, list the files ... */
2909 p
= path
+ strlen(path
);
2910 pstrcat(path
, "\\*");
2912 if (!cli_resolve_path("", srv
->cli
, path
,
2913 &targetcli
, targetpath
))
2915 d_printf("Could not resolve %s\n", path
);
2917 SAFE_FREE(dir
->fname
);
2923 if (cli_list(targetcli
, targetpath
,
2924 aDIR
| aSYSTEM
| aHIDDEN
,
2925 dir_list_fn
, (void *)dir
) < 0) {
2928 SAFE_FREE(dir
->fname
);
2931 saved_errno
= smbc_errno(context
, targetcli
);
2933 if (saved_errno
== EINVAL
) {
2935 * See if they asked to opendir something
2936 * other than a directory. If so, the
2937 * converted error value we got would have
2938 * been EINVAL rather than ENOTDIR.
2940 *p
= '\0'; /* restore original path */
2942 if (smbc_getatr(context
, srv
, path
,
2944 NULL
, NULL
, NULL
, NULL
,
2946 ! IS_DOS_DIR(mode
)) {
2948 /* It is. Correct the error value */
2949 saved_errno
= ENOTDIR
;
2954 * If there was an error and the server is no
2957 cb
= &context
->callbacks
;
2958 if (cli_is_error(targetcli
) &&
2959 cb
->check_server_fn(context
, srv
)) {
2961 /* ... then remove it. */
2962 if (cb
->remove_unused_server_fn(context
,
2965 * We could not remove the server
2966 * completely, remove it from the
2967 * cache so we will not get it
2968 * again. It will be removed when the
2969 * last file/dir is closed.
2971 cb
->remove_cached_srv_fn(context
, srv
);
2975 errno
= saved_errno
;
2982 DLIST_ADD(context
->internal
->_files
, dir
);
2988 * Routine to close a directory
2992 smbc_closedir_ctx(SMBCCTX
*context
,
2996 if (!context
|| !context
->internal
||
2997 !context
->internal
->_initialized
) {
3004 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3011 smbc_remove_dir(dir
); /* Clean it up */
3013 DLIST_REMOVE(context
->internal
->_files
, dir
);
3017 SAFE_FREE(dir
->fname
);
3018 SAFE_FREE(dir
); /* Free the space too */
3026 smbc_readdir_internal(SMBCCTX
* context
,
3027 struct smbc_dirent
*dest
,
3028 struct smbc_dirent
*src
,
3029 int max_namebuf_len
)
3031 if (context
->options
.urlencode_readdir_entries
) {
3033 /* url-encode the name. get back remaining buffer space */
3035 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
3037 /* We now know the name length */
3038 dest
->namelen
= strlen(dest
->name
);
3040 /* Save the pointer to the beginning of the comment */
3041 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
3043 /* Copy the comment */
3044 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
3045 dest
->comment
[max_namebuf_len
- 1] = '\0';
3047 /* Save other fields */
3048 dest
->smbc_type
= src
->smbc_type
;
3049 dest
->commentlen
= strlen(dest
->comment
);
3050 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
3054 /* No encoding. Just copy the entry as is. */
3055 memcpy(dest
, src
, src
->dirlen
);
3056 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
3062 * Routine to get a directory entry
3065 struct smbc_dirent
*
3066 smbc_readdir_ctx(SMBCCTX
*context
,
3070 struct smbc_dirent
*dirp
, *dirent
;
3072 /* Check that all is ok first ... */
3074 if (!context
|| !context
->internal
||
3075 !context
->internal
->_initialized
) {
3078 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3083 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3086 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3091 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3094 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3099 if (!dir
->dir_next
) {
3103 dirent
= dir
->dir_next
->dirent
;
3111 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
3112 maxlen
= (sizeof(context
->internal
->_dirent
) -
3113 sizeof(struct smbc_dirent
));
3115 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
3117 dir
->dir_next
= dir
->dir_next
->next
;
3123 * Routine to get directory entries
3127 smbc_getdents_ctx(SMBCCTX
*context
,
3129 struct smbc_dirent
*dirp
,
3135 char *ndir
= (char *)dirp
;
3136 struct smbc_dir_list
*dirlist
;
3138 /* Check that all is ok first ... */
3140 if (!context
|| !context
->internal
||
3141 !context
->internal
->_initialized
) {
3148 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3155 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3163 * Now, retrieve the number of entries that will fit in what was passed
3164 * We have to figure out if the info is in the list, or we need to
3165 * send a request to the server to get the info.
3168 while ((dirlist
= dir
->dir_next
)) {
3169 struct smbc_dirent
*dirent
;
3171 if (!dirlist
->dirent
) {
3173 errno
= ENOENT
; /* Bad error */
3178 /* Do urlencoding of next entry, if so selected */
3179 dirent
= (struct smbc_dirent
*)context
->internal
->_dirent
;
3180 maxlen
= (sizeof(context
->internal
->_dirent
) -
3181 sizeof(struct smbc_dirent
));
3182 smbc_readdir_internal(context
, dirent
, dirlist
->dirent
, maxlen
);
3184 reqd
= dirent
->dirlen
;
3188 if (rem
< count
) { /* We managed to copy something */
3194 else { /* Nothing copied ... */
3196 errno
= EINVAL
; /* Not enough space ... */
3203 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
3205 ((struct smbc_dirent
*)ndir
)->comment
=
3206 (char *)(&((struct smbc_dirent
*)ndir
)->name
+
3214 dir
->dir_next
= dirlist
= dirlist
-> next
;
3225 * Routine to create a directory ...
3229 smbc_mkdir_ctx(SMBCCTX
*context
,
3239 pstring path
, targetpath
;
3240 struct cli_state
*targetcli
;
3242 if (!context
|| !context
->internal
||
3243 !context
->internal
->_initialized
) {
3257 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
3259 if (smbc_parse_path(context
, fname
,
3260 workgroup
, sizeof(workgroup
),
3261 server
, sizeof(server
),
3262 share
, sizeof(share
),
3265 password
, sizeof(password
),
3271 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3273 srv
= smbc_server(context
, True
,
3274 server
, share
, workgroup
, user
, password
);
3278 return -1; /* errno set by smbc_server */
3282 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3283 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
3285 d_printf("Could not resolve %s\n", path
);
3288 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3290 if (!cli_mkdir(targetcli
, targetpath
)) {
3292 errno
= smbc_errno(context
, targetcli
);
3302 * Our list function simply checks to see if a directory is not empty
3305 static int smbc_rmdir_dirempty
= True
;
3308 rmdir_list_fn(const char *mnt
,
3313 if (strncmp(finfo
->name
, ".", 1) != 0 &&
3314 strncmp(finfo
->name
, "..", 2) != 0) {
3316 smbc_rmdir_dirempty
= False
;
3321 * Routine to remove a directory
3325 smbc_rmdir_ctx(SMBCCTX
*context
,
3336 struct cli_state
*targetcli
;
3338 if (!context
|| !context
->internal
||
3339 !context
->internal
->_initialized
) {
3353 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
3355 if (smbc_parse_path(context
, fname
,
3356 workgroup
, sizeof(workgroup
),
3357 server
, sizeof(server
),
3358 share
, sizeof(share
),
3361 password
, sizeof(password
),
3368 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3370 srv
= smbc_server(context
, True
,
3371 server
, share
, workgroup
, user
, password
);
3375 return -1; /* errno set by smbc_server */
3379 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3380 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
3382 d_printf("Could not resolve %s\n", path
);
3385 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3388 if (!cli_rmdir(targetcli
, targetpath
)) {
3390 errno
= smbc_errno(context
, targetcli
);
3392 if (errno
== EACCES
) { /* Check if the dir empty or not */
3394 /* Local storage to avoid buffer overflows */
3397 smbc_rmdir_dirempty
= True
; /* Make this so ... */
3399 pstrcpy(lpath
, targetpath
);
3400 pstrcat(lpath
, "\\*");
3402 if (cli_list(targetcli
, lpath
,
3403 aDIR
| aSYSTEM
| aHIDDEN
,
3404 rmdir_list_fn
, NULL
) < 0) {
3406 /* Fix errno to ignore latest error ... */
3407 DEBUG(5, ("smbc_rmdir: "
3408 "cli_list returned an error: %d\n",
3409 smbc_errno(context
, targetcli
)));
3414 if (smbc_rmdir_dirempty
)
3430 * Routine to return the current directory position
3434 smbc_telldir_ctx(SMBCCTX
*context
,
3437 off_t ret_val
; /* Squash warnings about cast */
3439 if (!context
|| !context
->internal
||
3440 !context
->internal
->_initialized
) {
3447 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3454 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3462 * We return the pointer here as the offset
3464 ret_val
= (off_t
)(long)dir
->dir_next
;
3470 * A routine to run down the list and see if the entry is OK
3473 struct smbc_dir_list
*
3474 smbc_check_dir_ent(struct smbc_dir_list
*list
,
3475 struct smbc_dirent
*dirent
)
3478 /* Run down the list looking for what we want */
3482 struct smbc_dir_list
*tmp
= list
;
3486 if (tmp
->dirent
== dirent
)
3495 return NULL
; /* Not found, or an error */
3501 * Routine to seek on a directory
3505 smbc_lseekdir_ctx(SMBCCTX
*context
,
3509 long int l_offset
= offset
; /* Handle problems of size */
3510 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
3511 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
3513 if (!context
|| !context
->internal
||
3514 !context
->internal
->_initialized
) {
3521 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3528 /* Now, check what we were passed and see if it is OK ... */
3530 if (dirent
== NULL
) { /* Seek to the begining of the list */
3532 dir
->dir_next
= dir
->dir_list
;
3537 /* Now, run down the list and make sure that the entry is OK */
3538 /* This may need to be changed if we change the format of the list */
3540 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
3542 errno
= EINVAL
; /* Bad entry */
3547 dir
->dir_next
= list_ent
;
3554 * Routine to fstat a dir
3558 smbc_fstatdir_ctx(SMBCCTX
*context
,
3563 if (!context
|| !context
->internal
||
3564 !context
->internal
->_initialized
) {
3571 /* No code yet ... */
3578 smbc_chmod_ctx(SMBCCTX
*context
,
3591 if (!context
|| !context
->internal
||
3592 !context
->internal
->_initialized
) {
3594 errno
= EINVAL
; /* Best I can think of ... */
3606 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, newmode
));
3608 if (smbc_parse_path(context
, fname
,
3609 workgroup
, sizeof(workgroup
),
3610 server
, sizeof(server
),
3611 share
, sizeof(share
),
3614 password
, sizeof(password
),
3620 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3622 srv
= smbc_server(context
, True
,
3623 server
, share
, workgroup
, user
, password
);
3626 return -1; /* errno set by smbc_server */
3631 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
3632 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
3633 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
3634 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
3636 if (!cli_setatr(srv
->cli
, path
, mode
, 0)) {
3637 errno
= smbc_errno(context
, srv
->cli
);
3645 smbc_utimes_ctx(SMBCCTX
*context
,
3647 struct timeval
*tbuf
)
3659 if (!context
|| !context
->internal
||
3660 !context
->internal
->_initialized
) {
3662 errno
= EINVAL
; /* Best I can think of ... */
3675 access_time
= write_time
= time(NULL
);
3677 access_time
= tbuf
[0].tv_sec
;
3678 write_time
= tbuf
[1].tv_sec
;
3687 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
3688 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
3689 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
3693 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
3694 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
3695 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
3699 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3700 fname
, atimebuf
, mtimebuf
);
3703 if (smbc_parse_path(context
, fname
,
3704 workgroup
, sizeof(workgroup
),
3705 server
, sizeof(server
),
3706 share
, sizeof(share
),
3709 password
, sizeof(password
),
3715 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3717 srv
= smbc_server(context
, True
,
3718 server
, share
, workgroup
, user
, password
);
3721 return -1; /* errno set by smbc_server */
3724 if (!smbc_setatr(context
, srv
, path
,
3725 0, access_time
, write_time
, 0, 0)) {
3726 return -1; /* errno set by smbc_setatr */
3733 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3734 However NT4 gives a "The information may have been modified by a
3735 computer running Windows NT 5.0" if denied ACEs do not appear before
3739 ace_compare(SEC_ACE
*ace1
,
3742 if (sec_ace_equal(ace1
, ace2
))
3745 if (ace1
->type
!= ace2
->type
)
3746 return ace2
->type
- ace1
->type
;
3748 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
3749 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
3751 if (ace1
->flags
!= ace2
->flags
)
3752 return ace1
->flags
- ace2
->flags
;
3754 if (ace1
->access_mask
!= ace2
->access_mask
)
3755 return ace1
->access_mask
- ace2
->access_mask
;
3757 if (ace1
->size
!= ace2
->size
)
3758 return ace1
->size
- ace2
->size
;
3760 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
3765 sort_acl(SEC_ACL
*the_acl
)
3768 if (!the_acl
) return;
3770 qsort(the_acl
->aces
, the_acl
->num_aces
, sizeof(the_acl
->aces
[0]),
3771 QSORT_CAST ace_compare
);
3773 for (i
=1;i
<the_acl
->num_aces
;) {
3774 if (sec_ace_equal(&the_acl
->aces
[i
-1], &the_acl
->aces
[i
])) {
3776 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
3777 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
3779 the_acl
->num_aces
--;
3786 /* convert a SID to a string, either numeric or username/group */
3788 convert_sid_to_string(struct cli_state
*ipc_cli
,
3794 char **domains
= NULL
;
3795 char **names
= NULL
;
3796 enum lsa_SidType
*types
= NULL
;
3797 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
3798 sid_to_string(str
, sid
);
3801 return; /* no lookup desired */
3808 /* Ask LSA to convert the sid to a name */
3810 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd
, ipc_cli
->mem_ctx
,
3811 pol
, 1, sid
, &domains
,
3813 !domains
|| !domains
[0] || !names
|| !names
[0]) {
3819 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
3820 domains
[0], lp_winbind_separator(),
3824 /* convert a string to a SID, either numeric or username/group */
3826 convert_string_to_sid(struct cli_state
*ipc_cli
,
3832 enum lsa_SidType
*types
= NULL
;
3833 DOM_SID
*sids
= NULL
;
3835 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
3842 if (strncmp(str
, "S-", 2) == 0) {
3843 return string_to_sid(sid
, str
);
3850 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd
, ipc_cli
->mem_ctx
,
3851 pol
, 1, &str
, NULL
, 1, &sids
,
3857 sid_copy(sid
, &sids
[0]);
3864 /* parse an ACE in the same format as print_ace() */
3866 parse_ace(struct cli_state
*ipc_cli
,
3876 unsigned int aflags
;
3880 const struct perm_value
*v
;
3886 /* These values discovered by inspection */
3887 static const struct perm_value special_values
[] = {
3888 { "R", 0x00120089 },
3889 { "W", 0x00120116 },
3890 { "X", 0x001200a0 },
3891 { "D", 0x00010000 },
3892 { "P", 0x00040000 },
3893 { "O", 0x00080000 },
3897 static const struct perm_value standard_values
[] = {
3898 { "READ", 0x001200a9 },
3899 { "CHANGE", 0x001301bf },
3900 { "FULL", 0x001f01ff },
3906 p
= strchr_m(str
,':');
3907 if (!p
) return False
;
3910 /* Try to parse numeric form */
3912 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
3913 convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3917 /* Try to parse text form */
3919 if (!convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3924 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3928 if (StrnCaseCmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
3929 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
3930 } else if (StrnCaseCmp(tok
, "DENIED", strlen("DENIED")) == 0) {
3931 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
3936 /* Only numeric form accepted for flags at present */
3938 if (!(next_token(&cp
, tok
, "/", sizeof(fstring
)) &&
3939 sscanf(tok
, "%i", &aflags
))) {
3943 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3947 if (strncmp(tok
, "0x", 2) == 0) {
3948 if (sscanf(tok
, "%i", &amask
) != 1) {
3954 for (v
= standard_values
; v
->perm
; v
++) {
3955 if (strcmp(tok
, v
->perm
) == 0) {
3966 for (v
= special_values
; v
->perm
; v
++) {
3967 if (v
->perm
[0] == *p
) {
3973 if (!found
) return False
;
3983 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
3987 /* add an ACE to a list of ACEs in a SEC_ACL */
3989 add_ace(SEC_ACL
**the_acl
,
3997 (*the_acl
) = make_sec_acl(ctx
, 3, 1, ace
);
4001 if ((aces
= SMB_CALLOC_ARRAY(SEC_ACE
, 1+(*the_acl
)->num_aces
)) == NULL
) {
4004 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
4005 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
4006 newacl
= make_sec_acl(ctx
, (*the_acl
)->revision
,
4007 1+(*the_acl
)->num_aces
, aces
);
4009 (*the_acl
) = newacl
;
4014 /* parse a ascii version of a security descriptor */
4016 sec_desc_parse(TALLOC_CTX
*ctx
,
4017 struct cli_state
*ipc_cli
,
4022 const char *p
= str
;
4024 SEC_DESC
*ret
= NULL
;
4026 DOM_SID
*grp_sid
=NULL
;
4027 DOM_SID
*owner_sid
=NULL
;
4031 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
4033 if (StrnCaseCmp(tok
,"REVISION:", 9) == 0) {
4034 revision
= strtol(tok
+9, NULL
, 16);
4038 if (StrnCaseCmp(tok
,"OWNER:", 6) == 0) {
4040 DEBUG(5, ("OWNER specified more than once!\n"));
4043 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4045 !convert_string_to_sid(ipc_cli
, pol
,
4047 owner_sid
, tok
+6)) {
4048 DEBUG(5, ("Failed to parse owner sid\n"));
4054 if (StrnCaseCmp(tok
,"OWNER+:", 7) == 0) {
4056 DEBUG(5, ("OWNER specified more than once!\n"));
4059 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4061 !convert_string_to_sid(ipc_cli
, pol
,
4063 owner_sid
, tok
+7)) {
4064 DEBUG(5, ("Failed to parse owner sid\n"));
4070 if (StrnCaseCmp(tok
,"GROUP:", 6) == 0) {
4072 DEBUG(5, ("GROUP specified more than once!\n"));
4075 grp_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4077 !convert_string_to_sid(ipc_cli
, pol
,
4080 DEBUG(5, ("Failed to parse group sid\n"));
4086 if (StrnCaseCmp(tok
,"GROUP+:", 7) == 0) {
4088 DEBUG(5, ("GROUP specified more than once!\n"));
4091 grp_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4093 !convert_string_to_sid(ipc_cli
, pol
,
4096 DEBUG(5, ("Failed to parse group sid\n"));
4102 if (StrnCaseCmp(tok
,"ACL:", 4) == 0) {
4104 if (!parse_ace(ipc_cli
, pol
, &ace
, numeric
, tok
+4)) {
4105 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
4108 if(!add_ace(&dacl
, &ace
, ctx
)) {
4109 DEBUG(5, ("Failed to add ACL %s\n", tok
));
4115 if (StrnCaseCmp(tok
,"ACL+:", 5) == 0) {
4117 if (!parse_ace(ipc_cli
, pol
, &ace
, False
, tok
+5)) {
4118 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
4121 if(!add_ace(&dacl
, &ace
, ctx
)) {
4122 DEBUG(5, ("Failed to add ACL %s\n", tok
));
4128 DEBUG(5, ("Failed to parse security descriptor\n"));
4132 ret
= make_sec_desc(ctx
, revision
, SEC_DESC_SELF_RELATIVE
,
4133 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
4137 SAFE_FREE(owner_sid
);
4143 /* Obtain the current dos attributes */
4144 static DOS_ATTR_DESC
*
4145 dos_attr_query(SMBCCTX
*context
,
4147 const char *filename
,
4150 struct timespec create_time_ts
;
4151 struct timespec write_time_ts
;
4152 struct timespec access_time_ts
;
4153 struct timespec change_time_ts
;
4156 SMB_INO_T inode
= 0;
4159 ret
= TALLOC_P(ctx
, DOS_ATTR_DESC
);
4165 /* Obtain the DOS attributes */
4166 if (!smbc_getatr(context
, srv
, CONST_DISCARD(char *, filename
),
4174 errno
= smbc_errno(context
, srv
->cli
);
4175 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4182 ret
->create_time
= convert_timespec_to_time_t(create_time_ts
);
4183 ret
->access_time
= convert_timespec_to_time_t(access_time_ts
);
4184 ret
->write_time
= convert_timespec_to_time_t(write_time_ts
);
4185 ret
->change_time
= convert_timespec_to_time_t(change_time_ts
);
4192 /* parse a ascii version of a security descriptor */
4194 dos_attr_parse(SMBCCTX
*context
,
4200 const char *p
= str
;
4203 const char * create_time_attr
;
4204 const char * access_time_attr
;
4205 const char * write_time_attr
;
4206 const char * change_time_attr
;
4209 /* Determine whether to use old-style or new-style attribute names */
4210 if (context
->internal
->_full_time_names
) {
4211 /* new-style names */
4212 attr_strings
.create_time_attr
= "CREATE_TIME";
4213 attr_strings
.access_time_attr
= "ACCESS_TIME";
4214 attr_strings
.write_time_attr
= "WRITE_TIME";
4215 attr_strings
.change_time_attr
= "CHANGE_TIME";
4217 /* old-style names */
4218 attr_strings
.create_time_attr
= NULL
;
4219 attr_strings
.access_time_attr
= "A_TIME";
4220 attr_strings
.write_time_attr
= "M_TIME";
4221 attr_strings
.change_time_attr
= "C_TIME";
4224 /* if this is to set the entire ACL... */
4226 /* ... then increment past the first colon if there is one */
4227 if ((p
= strchr(str
, ':')) != NULL
) {
4234 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
4236 if (StrnCaseCmp(tok
, "MODE:", 5) == 0) {
4237 dad
->mode
= strtol(tok
+5, NULL
, 16);
4241 if (StrnCaseCmp(tok
, "SIZE:", 5) == 0) {
4242 dad
->size
= (SMB_OFF_T
)atof(tok
+5);
4246 n
= strlen(attr_strings
.access_time_attr
);
4247 if (StrnCaseCmp(tok
, attr_strings
.access_time_attr
, n
) == 0) {
4248 dad
->access_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4252 n
= strlen(attr_strings
.change_time_attr
);
4253 if (StrnCaseCmp(tok
, attr_strings
.change_time_attr
, n
) == 0) {
4254 dad
->change_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4258 n
= strlen(attr_strings
.write_time_attr
);
4259 if (StrnCaseCmp(tok
, attr_strings
.write_time_attr
, n
) == 0) {
4260 dad
->write_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4264 if (attr_strings
.create_time_attr
!= NULL
) {
4265 n
= strlen(attr_strings
.create_time_attr
);
4266 if (StrnCaseCmp(tok
, attr_strings
.create_time_attr
,
4268 dad
->create_time
= (time_t)strtol(tok
+n
+1,
4274 if (StrnCaseCmp(tok
, "INODE:", 6) == 0) {
4275 dad
->inode
= (SMB_INO_T
)atof(tok
+6);
4281 /*****************************************************
4282 Retrieve the acls for a file.
4283 *******************************************************/
4286 cacl_get(SMBCCTX
*context
,
4289 struct cli_state
*ipc_cli
,
4305 BOOL exclude_nt_revision
= False
;
4306 BOOL exclude_nt_owner
= False
;
4307 BOOL exclude_nt_group
= False
;
4308 BOOL exclude_nt_acl
= False
;
4309 BOOL exclude_dos_mode
= False
;
4310 BOOL exclude_dos_size
= False
;
4311 BOOL exclude_dos_create_time
= False
;
4312 BOOL exclude_dos_access_time
= False
;
4313 BOOL exclude_dos_write_time
= False
;
4314 BOOL exclude_dos_change_time
= False
;
4315 BOOL exclude_dos_inode
= False
;
4316 BOOL numeric
= True
;
4317 BOOL determine_size
= (bufsize
== 0);
4321 fstring name_sandbox
;
4325 struct timespec create_time_ts
;
4326 struct timespec write_time_ts
;
4327 struct timespec access_time_ts
;
4328 struct timespec change_time_ts
;
4329 time_t create_time
= (time_t)0;
4330 time_t write_time
= (time_t)0;
4331 time_t access_time
= (time_t)0;
4332 time_t change_time
= (time_t)0;
4336 struct cli_state
*cli
= srv
->cli
;
4338 const char * create_time_attr
;
4339 const char * access_time_attr
;
4340 const char * write_time_attr
;
4341 const char * change_time_attr
;
4344 const char * create_time_attr
;
4345 const char * access_time_attr
;
4346 const char * write_time_attr
;
4347 const char * change_time_attr
;
4348 } excl_attr_strings
;
4350 /* Determine whether to use old-style or new-style attribute names */
4351 if (context
->internal
->_full_time_names
) {
4352 /* new-style names */
4353 attr_strings
.create_time_attr
= "CREATE_TIME";
4354 attr_strings
.access_time_attr
= "ACCESS_TIME";
4355 attr_strings
.write_time_attr
= "WRITE_TIME";
4356 attr_strings
.change_time_attr
= "CHANGE_TIME";
4358 excl_attr_strings
.create_time_attr
= "CREATE_TIME";
4359 excl_attr_strings
.access_time_attr
= "ACCESS_TIME";
4360 excl_attr_strings
.write_time_attr
= "WRITE_TIME";
4361 excl_attr_strings
.change_time_attr
= "CHANGE_TIME";
4363 /* old-style names */
4364 attr_strings
.create_time_attr
= NULL
;
4365 attr_strings
.access_time_attr
= "A_TIME";
4366 attr_strings
.write_time_attr
= "M_TIME";
4367 attr_strings
.change_time_attr
= "C_TIME";
4369 excl_attr_strings
.create_time_attr
= NULL
;
4370 excl_attr_strings
.access_time_attr
= "dos_attr.A_TIME";
4371 excl_attr_strings
.write_time_attr
= "dos_attr.M_TIME";
4372 excl_attr_strings
.change_time_attr
= "dos_attr.C_TIME";
4375 /* Copy name so we can strip off exclusions (if any are specified) */
4376 strncpy(name_sandbox
, attr_name
, sizeof(name_sandbox
) - 1);
4378 /* Ensure name is null terminated */
4379 name_sandbox
[sizeof(name_sandbox
) - 1] = '\0';
4381 /* Play in the sandbox */
4382 name
= name_sandbox
;
4384 /* If there are any exclusions, point to them and mask them from name */
4385 if ((pExclude
= strchr(name
, '!')) != NULL
)
4390 all
= (StrnCaseCmp(name
, "system.*", 8) == 0);
4391 all_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.*", 20) == 0);
4392 all_nt_acls
= (StrnCaseCmp(name
, "system.nt_sec_desc.acl.*", 24) == 0);
4393 all_dos
= (StrnCaseCmp(name
, "system.dos_attr.*", 17) == 0);
4394 some_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.", 19) == 0);
4395 some_dos
= (StrnCaseCmp(name
, "system.dos_attr.", 16) == 0);
4396 numeric
= (* (name
+ strlen(name
) - 1) != '+');
4398 /* Look for exclusions from "all" requests */
4399 if (all
|| all_nt
|| all_dos
) {
4401 /* Exclusions are delimited by '!' */
4404 pExclude
= (p
== NULL
? NULL
: p
+ 1)) {
4406 /* Find end of this exclusion name */
4407 if ((p
= strchr(pExclude
, '!')) != NULL
)
4412 /* Which exclusion name is this? */
4413 if (StrCaseCmp(pExclude
, "nt_sec_desc.revision") == 0) {
4414 exclude_nt_revision
= True
;
4416 else if (StrCaseCmp(pExclude
, "nt_sec_desc.owner") == 0) {
4417 exclude_nt_owner
= True
;
4419 else if (StrCaseCmp(pExclude
, "nt_sec_desc.group") == 0) {
4420 exclude_nt_group
= True
;
4422 else if (StrCaseCmp(pExclude
, "nt_sec_desc.acl") == 0) {
4423 exclude_nt_acl
= True
;
4425 else if (StrCaseCmp(pExclude
, "dos_attr.mode") == 0) {
4426 exclude_dos_mode
= True
;
4428 else if (StrCaseCmp(pExclude
, "dos_attr.size") == 0) {
4429 exclude_dos_size
= True
;
4431 else if (excl_attr_strings
.create_time_attr
!= NULL
&&
4432 StrCaseCmp(pExclude
,
4433 excl_attr_strings
.change_time_attr
) == 0) {
4434 exclude_dos_create_time
= True
;
4436 else if (StrCaseCmp(pExclude
,
4437 excl_attr_strings
.access_time_attr
) == 0) {
4438 exclude_dos_access_time
= True
;
4440 else if (StrCaseCmp(pExclude
,
4441 excl_attr_strings
.write_time_attr
) == 0) {
4442 exclude_dos_write_time
= True
;
4444 else if (StrCaseCmp(pExclude
,
4445 excl_attr_strings
.change_time_attr
) == 0) {
4446 exclude_dos_change_time
= True
;
4448 else if (StrCaseCmp(pExclude
, "dos_attr.inode") == 0) {
4449 exclude_dos_inode
= True
;
4452 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4463 * If we are (possibly) talking to an NT or new system and some NT
4464 * attributes have been requested...
4466 if (ipc_cli
&& (all
|| some_nt
|| all_nt_acls
)) {
4467 /* Point to the portion after "system.nt_sec_desc." */
4468 name
+= 19; /* if (all) this will be invalid but unused */
4470 /* ... then obtain any NT attributes which were requested */
4471 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
4474 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4475 filename
, cli_errstr(cli
)));
4480 sd
= cli_query_secdesc(cli
, fnum
, ctx
);
4484 ("cacl_get Failed to query old descriptor\n"));
4489 cli_close(cli
, fnum
);
4491 if (! exclude_nt_revision
) {
4492 if (all
|| all_nt
) {
4493 if (determine_size
) {
4494 p
= talloc_asprintf(ctx
,
4503 n
= snprintf(buf
, bufsize
,
4507 } else if (StrCaseCmp(name
, "revision") == 0) {
4508 if (determine_size
) {
4509 p
= talloc_asprintf(ctx
, "%d",
4517 n
= snprintf(buf
, bufsize
, "%d",
4522 if (!determine_size
&& n
> bufsize
) {
4531 if (! exclude_nt_owner
) {
4532 /* Get owner and group sid */
4533 if (sd
->owner_sid
) {
4534 convert_sid_to_string(ipc_cli
, pol
,
4539 fstrcpy(sidstr
, "");
4542 if (all
|| all_nt
) {
4543 if (determine_size
) {
4544 p
= talloc_asprintf(ctx
, ",OWNER:%s",
4551 } else if (sidstr
[0] != '\0') {
4552 n
= snprintf(buf
, bufsize
,
4553 ",OWNER:%s", sidstr
);
4555 } else if (StrnCaseCmp(name
, "owner", 5) == 0) {
4556 if (determine_size
) {
4557 p
= talloc_asprintf(ctx
, "%s", sidstr
);
4564 n
= snprintf(buf
, bufsize
, "%s",
4569 if (!determine_size
&& n
> bufsize
) {
4578 if (! exclude_nt_group
) {
4579 if (sd
->group_sid
) {
4580 convert_sid_to_string(ipc_cli
, pol
,
4584 fstrcpy(sidstr
, "");
4587 if (all
|| all_nt
) {
4588 if (determine_size
) {
4589 p
= talloc_asprintf(ctx
, ",GROUP:%s",
4596 } else if (sidstr
[0] != '\0') {
4597 n
= snprintf(buf
, bufsize
,
4598 ",GROUP:%s", sidstr
);
4600 } else if (StrnCaseCmp(name
, "group", 5) == 0) {
4601 if (determine_size
) {
4602 p
= talloc_asprintf(ctx
, "%s", sidstr
);
4609 n
= snprintf(buf
, bufsize
,
4614 if (!determine_size
&& n
> bufsize
) {
4623 if (! exclude_nt_acl
) {
4624 /* Add aces to value buffer */
4625 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
4627 SEC_ACE
*ace
= &sd
->dacl
->aces
[i
];
4628 convert_sid_to_string(ipc_cli
, pol
,
4632 if (all
|| all_nt
) {
4633 if (determine_size
) {
4634 p
= talloc_asprintf(
4650 ",ACL:%s:%d/%d/0x%08x",
4656 } else if ((StrnCaseCmp(name
, "acl", 3) == 0 &&
4657 StrCaseCmp(name
+3, sidstr
) == 0) ||
4658 (StrnCaseCmp(name
, "acl+", 4) == 0 &&
4659 StrCaseCmp(name
+4, sidstr
) == 0)) {
4660 if (determine_size
) {
4661 p
= talloc_asprintf(
4673 n
= snprintf(buf
, bufsize
,
4679 } else if (all_nt_acls
) {
4680 if (determine_size
) {
4681 p
= talloc_asprintf(
4683 "%s%s:%d/%d/0x%08x",
4695 n
= snprintf(buf
, bufsize
,
4696 "%s%s:%d/%d/0x%08x",
4704 if (!determine_size
&& n
> bufsize
) {
4714 /* Restore name pointer to its original value */
4718 if (all
|| some_dos
) {
4719 /* Point to the portion after "system.dos_attr." */
4720 name
+= 16; /* if (all) this will be invalid but unused */
4722 /* Obtain the DOS attributes */
4723 if (!smbc_getatr(context
, srv
, filename
, &mode
, &size
,
4730 errno
= smbc_errno(context
, srv
->cli
);
4735 create_time
= convert_timespec_to_time_t(create_time_ts
);
4736 access_time
= convert_timespec_to_time_t(access_time_ts
);
4737 write_time
= convert_timespec_to_time_t(write_time_ts
);
4738 change_time
= convert_timespec_to_time_t(change_time_ts
);
4740 if (! exclude_dos_mode
) {
4741 if (all
|| all_dos
) {
4742 if (determine_size
) {
4743 p
= talloc_asprintf(ctx
,
4756 n
= snprintf(buf
, bufsize
,
4764 } else if (StrCaseCmp(name
, "mode") == 0) {
4765 if (determine_size
) {
4766 p
= talloc_asprintf(ctx
, "0x%x", mode
);
4773 n
= snprintf(buf
, bufsize
,
4778 if (!determine_size
&& n
> bufsize
) {
4787 if (! exclude_dos_size
) {
4788 if (all
|| all_dos
) {
4789 if (determine_size
) {
4790 p
= talloc_asprintf(
4800 n
= snprintf(buf
, bufsize
,
4804 } else if (StrCaseCmp(name
, "size") == 0) {
4805 if (determine_size
) {
4806 p
= talloc_asprintf(
4816 n
= snprintf(buf
, bufsize
,
4822 if (!determine_size
&& n
> bufsize
) {
4831 if (! exclude_dos_create_time
&&
4832 attr_strings
.create_time_attr
!= NULL
) {
4833 if (all
|| all_dos
) {
4834 if (determine_size
) {
4835 p
= talloc_asprintf(ctx
,
4837 attr_strings
.create_time_attr
,
4845 n
= snprintf(buf
, bufsize
,
4847 attr_strings
.create_time_attr
,
4850 } else if (StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) {
4851 if (determine_size
) {
4852 p
= talloc_asprintf(ctx
, "%lu", create_time
);
4859 n
= snprintf(buf
, bufsize
,
4860 "%lu", create_time
);
4864 if (!determine_size
&& n
> bufsize
) {
4873 if (! exclude_dos_access_time
) {
4874 if (all
|| all_dos
) {
4875 if (determine_size
) {
4876 p
= talloc_asprintf(ctx
,
4878 attr_strings
.access_time_attr
,
4886 n
= snprintf(buf
, bufsize
,
4888 attr_strings
.access_time_attr
,
4891 } else if (StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0) {
4892 if (determine_size
) {
4893 p
= talloc_asprintf(ctx
, "%lu", access_time
);
4900 n
= snprintf(buf
, bufsize
,
4901 "%lu", access_time
);
4905 if (!determine_size
&& n
> bufsize
) {
4914 if (! exclude_dos_write_time
) {
4915 if (all
|| all_dos
) {
4916 if (determine_size
) {
4917 p
= talloc_asprintf(ctx
,
4919 attr_strings
.write_time_attr
,
4927 n
= snprintf(buf
, bufsize
,
4929 attr_strings
.write_time_attr
,
4932 } else if (StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0) {
4933 if (determine_size
) {
4934 p
= talloc_asprintf(ctx
, "%lu", write_time
);
4941 n
= snprintf(buf
, bufsize
,
4946 if (!determine_size
&& n
> bufsize
) {
4955 if (! exclude_dos_change_time
) {
4956 if (all
|| all_dos
) {
4957 if (determine_size
) {
4958 p
= talloc_asprintf(ctx
,
4960 attr_strings
.change_time_attr
,
4968 n
= snprintf(buf
, bufsize
,
4970 attr_strings
.change_time_attr
,
4973 } else if (StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0) {
4974 if (determine_size
) {
4975 p
= talloc_asprintf(ctx
, "%lu", change_time
);
4982 n
= snprintf(buf
, bufsize
,
4983 "%lu", change_time
);
4987 if (!determine_size
&& n
> bufsize
) {
4996 if (! exclude_dos_inode
) {
4997 if (all
|| all_dos
) {
4998 if (determine_size
) {
4999 p
= talloc_asprintf(
5009 n
= snprintf(buf
, bufsize
,
5013 } else if (StrCaseCmp(name
, "inode") == 0) {
5014 if (determine_size
) {
5015 p
= talloc_asprintf(
5025 n
= snprintf(buf
, bufsize
,
5031 if (!determine_size
&& n
> bufsize
) {
5040 /* Restore name pointer to its original value */
5053 /*****************************************************
5054 set the ACLs on a file given an ascii description
5055 *******************************************************/
5057 cacl_set(TALLOC_CTX
*ctx
,
5058 struct cli_state
*cli
,
5059 struct cli_state
*ipc_cli
,
5061 const char *filename
,
5062 const char *the_acl
,
5068 SEC_DESC
*sd
= NULL
, *old
;
5069 SEC_ACL
*dacl
= NULL
;
5070 DOM_SID
*owner_sid
= NULL
;
5071 DOM_SID
*grp_sid
= NULL
;
5076 BOOL numeric
= True
;
5078 /* the_acl will be null for REMOVE_ALL operations */
5080 numeric
= ((p
= strchr(the_acl
, ':')) != NULL
&&
5084 /* if this is to set the entire ACL... */
5085 if (*the_acl
== '*') {
5086 /* ... then increment past the first colon */
5090 sd
= sec_desc_parse(ctx
, ipc_cli
, pol
, numeric
,
5091 CONST_DISCARD(char *, the_acl
));
5099 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5100 that doesn't deref sd */
5102 if (!sd
&& (mode
!= SMBC_XATTR_MODE_REMOVE_ALL
)) {
5107 /* The desired access below is the only one I could find that works
5108 with NT4, W2KP and Samba */
5110 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
5113 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5114 filename
, cli_errstr(cli
)));
5119 old
= cli_query_secdesc(cli
, fnum
, ctx
);
5122 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5127 cli_close(cli
, fnum
);
5130 case SMBC_XATTR_MODE_REMOVE_ALL
:
5131 old
->dacl
->num_aces
= 0;
5132 SAFE_FREE(old
->dacl
->aces
);
5133 SAFE_FREE(old
->dacl
);
5138 case SMBC_XATTR_MODE_REMOVE
:
5139 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5142 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
5143 if (sec_ace_equal(&sd
->dacl
->aces
[i
],
5144 &old
->dacl
->aces
[j
])) {
5146 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
5147 old
->dacl
->aces
[k
] =
5148 old
->dacl
->aces
[k
+1];
5150 old
->dacl
->num_aces
--;
5151 if (old
->dacl
->num_aces
== 0) {
5152 SAFE_FREE(old
->dacl
->aces
);
5153 SAFE_FREE(old
->dacl
);
5170 case SMBC_XATTR_MODE_ADD
:
5171 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5174 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
5175 if (sid_equal(&sd
->dacl
->aces
[i
].trustee
,
5176 &old
->dacl
->aces
[j
].trustee
)) {
5177 if (!(flags
& SMBC_XATTR_FLAG_CREATE
)) {
5182 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
5188 if (!found
&& (flags
& SMBC_XATTR_FLAG_REPLACE
)) {
5194 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5195 add_ace(&old
->dacl
, &sd
->dacl
->aces
[i
], ctx
);
5201 case SMBC_XATTR_MODE_SET
:
5203 owner_sid
= old
->owner_sid
;
5204 grp_sid
= old
->group_sid
;
5208 case SMBC_XATTR_MODE_CHOWN
:
5209 owner_sid
= sd
->owner_sid
;
5212 case SMBC_XATTR_MODE_CHGRP
:
5213 grp_sid
= sd
->group_sid
;
5217 /* Denied ACE entries must come before allowed ones */
5218 sort_acl(old
->dacl
);
5220 /* Create new security descriptor and set it */
5221 sd
= make_sec_desc(ctx
, old
->revision
, SEC_DESC_SELF_RELATIVE
,
5222 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
5224 fnum
= cli_nt_create(cli
, filename
,
5225 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
);
5228 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5229 filename
, cli_errstr(cli
)));
5234 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
5235 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli
)));
5242 cli_close(cli
, fnum
);
5253 smbc_setxattr_ctx(SMBCCTX
*context
,
5274 const char * create_time_attr
;
5275 const char * access_time_attr
;
5276 const char * write_time_attr
;
5277 const char * change_time_attr
;
5280 if (!context
|| !context
->internal
||
5281 !context
->internal
->_initialized
) {
5283 errno
= EINVAL
; /* Best I can think of ... */
5295 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5296 fname
, name
, (int) size
, (const char*)value
));
5298 if (smbc_parse_path(context
, fname
,
5299 workgroup
, sizeof(workgroup
),
5300 server
, sizeof(server
),
5301 share
, sizeof(share
),
5304 password
, sizeof(password
),
5310 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5312 srv
= smbc_server(context
, True
,
5313 server
, share
, workgroup
, user
, password
);
5315 return -1; /* errno set by smbc_server */
5318 if (! srv
->no_nt_session
) {
5319 ipc_srv
= smbc_attr_server(context
, server
, share
,
5320 workgroup
, user
, password
,
5323 srv
->no_nt_session
= True
;
5329 ctx
= talloc_init("smbc_setxattr");
5336 * Are they asking to set the entire set of known attributes?
5338 if (StrCaseCmp(name
, "system.*") == 0 ||
5339 StrCaseCmp(name
, "system.*+") == 0) {
5342 talloc_asprintf(ctx
, "%s:%s",
5343 name
+7, (const char *) value
);
5351 ret
= cacl_set(ctx
, srv
->cli
,
5352 ipc_srv
->cli
, &pol
, path
,
5355 ? SMBC_XATTR_MODE_SET
5356 : SMBC_XATTR_MODE_ADD
),
5362 /* get a DOS Attribute Descriptor with current attributes */
5363 dad
= dos_attr_query(context
, ctx
, path
, srv
);
5365 /* Overwrite old with new, using what was provided */
5366 dos_attr_parse(context
, dad
, srv
, namevalue
);
5368 /* Set the new DOS attributes */
5369 if (! smbc_setatr(context
, srv
, path
,
5376 /* cause failure if NT failed too */
5381 /* we only fail if both NT and DOS sets failed */
5382 if (ret
< 0 && ! dad
) {
5383 ret
= -1; /* in case dad was null */
5389 talloc_destroy(ctx
);
5394 * Are they asking to set an access control element or to set
5395 * the entire access control list?
5397 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5398 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
5399 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5400 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5401 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
5405 talloc_asprintf(ctx
, "%s:%s",
5406 name
+19, (const char *) value
);
5409 ret
= -1; /* errno set by smbc_server() */
5411 else if (! namevalue
) {
5415 ret
= cacl_set(ctx
, srv
->cli
,
5416 ipc_srv
->cli
, &pol
, path
,
5419 ? SMBC_XATTR_MODE_SET
5420 : SMBC_XATTR_MODE_ADD
),
5423 talloc_destroy(ctx
);
5428 * Are they asking to set the owner?
5430 if (StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5431 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0) {
5435 talloc_asprintf(ctx
, "%s:%s",
5436 name
+19, (const char *) value
);
5440 ret
= -1; /* errno set by smbc_server() */
5442 else if (! namevalue
) {
5446 ret
= cacl_set(ctx
, srv
->cli
,
5447 ipc_srv
->cli
, &pol
, path
,
5448 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
5450 talloc_destroy(ctx
);
5455 * Are they asking to set the group?
5457 if (StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5458 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0) {
5462 talloc_asprintf(ctx
, "%s:%s",
5463 name
+19, (const char *) value
);
5466 /* errno set by smbc_server() */
5469 else if (! namevalue
) {
5473 ret
= cacl_set(ctx
, srv
->cli
,
5474 ipc_srv
->cli
, &pol
, path
,
5475 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
5477 talloc_destroy(ctx
);
5481 /* Determine whether to use old-style or new-style attribute names */
5482 if (context
->internal
->_full_time_names
) {
5483 /* new-style names */
5484 attr_strings
.create_time_attr
= "system.dos_attr.CREATE_TIME";
5485 attr_strings
.access_time_attr
= "system.dos_attr.ACCESS_TIME";
5486 attr_strings
.write_time_attr
= "system.dos_attr.WRITE_TIME";
5487 attr_strings
.change_time_attr
= "system.dos_attr.CHANGE_TIME";
5489 /* old-style names */
5490 attr_strings
.create_time_attr
= NULL
;
5491 attr_strings
.access_time_attr
= "system.dos_attr.A_TIME";
5492 attr_strings
.write_time_attr
= "system.dos_attr.M_TIME";
5493 attr_strings
.change_time_attr
= "system.dos_attr.C_TIME";
5497 * Are they asking to set a DOS attribute?
5499 if (StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
5500 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
5501 (attr_strings
.create_time_attr
!= NULL
&&
5502 StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) ||
5503 StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0 ||
5504 StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0 ||
5505 StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0) {
5507 /* get a DOS Attribute Descriptor with current attributes */
5508 dad
= dos_attr_query(context
, ctx
, path
, srv
);
5511 talloc_asprintf(ctx
, "%s:%s",
5512 name
+16, (const char *) value
);
5517 /* Overwrite old with provided new params */
5518 dos_attr_parse(context
, dad
, srv
, namevalue
);
5520 /* Set the new DOS attributes */
5521 ret2
= smbc_setatr(context
, srv
, path
,
5528 /* ret2 has True (success) / False (failure) */
5539 talloc_destroy(ctx
);
5543 /* Unsupported attribute name */
5544 talloc_destroy(ctx
);
5550 smbc_getxattr_ctx(SMBCCTX
*context
,
5568 const char * create_time_attr
;
5569 const char * access_time_attr
;
5570 const char * write_time_attr
;
5571 const char * change_time_attr
;
5575 if (!context
|| !context
->internal
||
5576 !context
->internal
->_initialized
) {
5578 errno
= EINVAL
; /* Best I can think of ... */
5590 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname
, name
));
5592 if (smbc_parse_path(context
, fname
,
5593 workgroup
, sizeof(workgroup
),
5594 server
, sizeof(server
),
5595 share
, sizeof(share
),
5598 password
, sizeof(password
),
5604 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5606 srv
= smbc_server(context
, True
,
5607 server
, share
, workgroup
, user
, password
);
5609 return -1; /* errno set by smbc_server */
5612 if (! srv
->no_nt_session
) {
5613 ipc_srv
= smbc_attr_server(context
, server
, share
,
5614 workgroup
, user
, password
,
5617 srv
->no_nt_session
= True
;
5623 ctx
= talloc_init("smbc:getxattr");
5629 /* Determine whether to use old-style or new-style attribute names */
5630 if (context
->internal
->_full_time_names
) {
5631 /* new-style names */
5632 attr_strings
.create_time_attr
= "system.dos_attr.CREATE_TIME";
5633 attr_strings
.access_time_attr
= "system.dos_attr.ACCESS_TIME";
5634 attr_strings
.write_time_attr
= "system.dos_attr.WRITE_TIME";
5635 attr_strings
.change_time_attr
= "system.dos_attr.CHANGE_TIME";
5637 /* old-style names */
5638 attr_strings
.create_time_attr
= NULL
;
5639 attr_strings
.access_time_attr
= "system.dos_attr.A_TIME";
5640 attr_strings
.write_time_attr
= "system.dos_attr.M_TIME";
5641 attr_strings
.change_time_attr
= "system.dos_attr.C_TIME";
5644 /* Are they requesting a supported attribute? */
5645 if (StrCaseCmp(name
, "system.*") == 0 ||
5646 StrnCaseCmp(name
, "system.*!", 9) == 0 ||
5647 StrCaseCmp(name
, "system.*+") == 0 ||
5648 StrnCaseCmp(name
, "system.*+!", 10) == 0 ||
5649 StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5650 StrnCaseCmp(name
, "system.nt_sec_desc.*!", 21) == 0 ||
5651 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
5652 StrnCaseCmp(name
, "system.nt_sec_desc.*+!", 22) == 0 ||
5653 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5654 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5655 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
5656 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5657 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
5658 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5659 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0 ||
5660 StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
5661 StrnCaseCmp(name
, "system.dos_attr.*!", 18) == 0 ||
5662 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
5663 StrCaseCmp(name
, "system.dos_attr.size") == 0 ||
5664 (attr_strings
.create_time_attr
!= NULL
&&
5665 StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) ||
5666 StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0 ||
5667 StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0 ||
5668 StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0 ||
5669 StrCaseCmp(name
, "system.dos_attr.inode") == 0) {
5672 ret
= cacl_get(context
, ctx
, srv
,
5673 ipc_srv
== NULL
? NULL
: ipc_srv
->cli
,
5675 CONST_DISCARD(char *, name
),
5676 CONST_DISCARD(char *, value
), size
);
5677 if (ret
< 0 && errno
== 0) {
5678 errno
= smbc_errno(context
, srv
->cli
);
5680 talloc_destroy(ctx
);
5684 /* Unsupported attribute name */
5685 talloc_destroy(ctx
);
5692 smbc_removexattr_ctx(SMBCCTX
*context
,
5708 if (!context
|| !context
->internal
||
5709 !context
->internal
->_initialized
) {
5711 errno
= EINVAL
; /* Best I can think of ... */
5723 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname
, name
));
5725 if (smbc_parse_path(context
, fname
,
5726 workgroup
, sizeof(workgroup
),
5727 server
, sizeof(server
),
5728 share
, sizeof(share
),
5731 password
, sizeof(password
),
5737 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5739 srv
= smbc_server(context
, True
,
5740 server
, share
, workgroup
, user
, password
);
5742 return -1; /* errno set by smbc_server */
5745 if (! srv
->no_nt_session
) {
5746 ipc_srv
= smbc_attr_server(context
, server
, share
,
5747 workgroup
, user
, password
,
5750 srv
->no_nt_session
= True
;
5757 return -1; /* errno set by smbc_attr_server */
5760 ctx
= talloc_init("smbc_removexattr");
5766 /* Are they asking to set the entire ACL? */
5767 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5768 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0) {
5771 ret
= cacl_set(ctx
, srv
->cli
,
5772 ipc_srv
->cli
, &pol
, path
,
5773 NULL
, SMBC_XATTR_MODE_REMOVE_ALL
, 0);
5774 talloc_destroy(ctx
);
5779 * Are they asking to remove one or more spceific security descriptor
5782 if (StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5783 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5784 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
5785 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5786 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
5787 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5788 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
5791 ret
= cacl_set(ctx
, srv
->cli
,
5792 ipc_srv
->cli
, &pol
, path
,
5793 name
+ 19, SMBC_XATTR_MODE_REMOVE
, 0);
5794 talloc_destroy(ctx
);
5798 /* Unsupported attribute name */
5799 talloc_destroy(ctx
);
5805 smbc_listxattr_ctx(SMBCCTX
*context
,
5811 * This isn't quite what listxattr() is supposed to do. This returns
5812 * the complete set of attribute names, always, rather than only those
5813 * attribute names which actually exist for a file. Hmmm...
5815 const char supported_old
[] =
5818 "system.nt_sec_desc.revision\0"
5819 "system.nt_sec_desc.owner\0"
5820 "system.nt_sec_desc.owner+\0"
5821 "system.nt_sec_desc.group\0"
5822 "system.nt_sec_desc.group+\0"
5823 "system.nt_sec_desc.acl.*\0"
5824 "system.nt_sec_desc.acl\0"
5825 "system.nt_sec_desc.acl+\0"
5826 "system.nt_sec_desc.*\0"
5827 "system.nt_sec_desc.*+\0"
5828 "system.dos_attr.*\0"
5829 "system.dos_attr.mode\0"
5830 "system.dos_attr.c_time\0"
5831 "system.dos_attr.a_time\0"
5832 "system.dos_attr.m_time\0"
5834 const char supported_new
[] =
5837 "system.nt_sec_desc.revision\0"
5838 "system.nt_sec_desc.owner\0"
5839 "system.nt_sec_desc.owner+\0"
5840 "system.nt_sec_desc.group\0"
5841 "system.nt_sec_desc.group+\0"
5842 "system.nt_sec_desc.acl.*\0"
5843 "system.nt_sec_desc.acl\0"
5844 "system.nt_sec_desc.acl+\0"
5845 "system.nt_sec_desc.*\0"
5846 "system.nt_sec_desc.*+\0"
5847 "system.dos_attr.*\0"
5848 "system.dos_attr.mode\0"
5849 "system.dos_attr.create_time\0"
5850 "system.dos_attr.access_time\0"
5851 "system.dos_attr.write_time\0"
5852 "system.dos_attr.change_time\0"
5854 const char * supported
;
5856 if (context
->internal
->_full_time_names
) {
5857 supported
= supported_new
;
5859 supported
= supported_old
;
5863 return sizeof(supported
);
5866 if (sizeof(supported
) > size
) {
5871 /* this can't be strcpy() because there are embedded null characters */
5872 memcpy(list
, supported
, sizeof(supported
));
5873 return sizeof(supported
);
5878 * Open a print file to be written to by other calls
5882 smbc_open_print_job_ctx(SMBCCTX
*context
,
5891 if (!context
|| !context
->internal
||
5892 !context
->internal
->_initialized
) {
5906 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
5908 if (smbc_parse_path(context
, fname
,
5910 server
, sizeof(server
),
5911 share
, sizeof(share
),
5914 password
, sizeof(password
),
5920 /* What if the path is empty, or the file exists? */
5922 return context
->open(context
, fname
, O_WRONLY
, 666);
5927 * Routine to print a file on a remote server ...
5929 * We open the file, which we assume to be on a remote server, and then
5930 * copy it to a print file on the share specified by printq.
5934 smbc_print_file_ctx(SMBCCTX
*c_file
,
5946 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
5947 !c_print
->internal
->_initialized
) {
5954 if (!fname
&& !printq
) {
5961 /* Try to open the file for reading ... */
5963 if ((long)(fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
5965 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
5966 return -1; /* smbc_open sets errno */
5970 /* Now, try to open the printer file for writing */
5972 if ((long)(fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
5974 saverr
= errno
; /* Save errno */
5975 c_file
->close_fn(c_file
, fid1
);
5981 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
5985 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
5988 c_file
->close_fn(c_file
, fid1
);
5989 c_print
->close_fn(c_print
, fid2
);
5998 c_file
->close_fn(c_file
, fid1
); /* We have to close these anyway */
5999 c_print
->close_fn(c_print
, fid2
);
6013 * Routine to list print jobs on a printer share ...
6017 smbc_list_print_jobs_ctx(SMBCCTX
*context
,
6019 smbc_list_print_job_fn fn
)
6029 if (!context
|| !context
->internal
||
6030 !context
->internal
->_initialized
) {
6044 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
6046 if (smbc_parse_path(context
, fname
,
6047 workgroup
, sizeof(workgroup
),
6048 server
, sizeof(server
),
6049 share
, sizeof(share
),
6052 password
, sizeof(password
),
6058 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
6060 srv
= smbc_server(context
, True
,
6061 server
, share
, workgroup
, user
, password
);
6065 return -1; /* errno set by smbc_server */
6069 if (cli_print_queue(srv
->cli
,
6070 (void (*)(struct print_job_info
*))fn
) < 0) {
6072 errno
= smbc_errno(context
, srv
->cli
);
6082 * Delete a print job from a remote printer share
6086 smbc_unlink_print_job_ctx(SMBCCTX
*context
,
6099 if (!context
|| !context
->internal
||
6100 !context
->internal
->_initialized
) {
6114 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
6116 if (smbc_parse_path(context
, fname
,
6117 workgroup
, sizeof(workgroup
),
6118 server
, sizeof(server
),
6119 share
, sizeof(share
),
6122 password
, sizeof(password
),
6128 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
6130 srv
= smbc_server(context
, True
,
6131 server
, share
, workgroup
, user
, password
);
6135 return -1; /* errno set by smbc_server */
6139 if ((err
= cli_printjob_del(srv
->cli
, id
)) != 0) {
6142 errno
= smbc_errno(context
, srv
->cli
);
6143 else if (err
== ERRnosuchprintjob
)
6154 * Get a new empty handle to fill in with your own info
6157 smbc_new_context(void)
6161 context
= SMB_MALLOC_P(SMBCCTX
);
6167 ZERO_STRUCTP(context
);
6169 context
->internal
= SMB_MALLOC_P(struct smbc_internal_data
);
6170 if (!context
->internal
) {
6176 ZERO_STRUCTP(context
->internal
);
6179 /* ADD REASONABLE DEFAULTS */
6181 context
->timeout
= 20000; /* 20 seconds */
6183 context
->options
.browse_max_lmb_count
= 3; /* # LMBs to query */
6184 context
->options
.urlencode_readdir_entries
= False
;/* backward compat */
6185 context
->options
.one_share_per_server
= False
;/* backward compat */
6186 context
->internal
->_share_mode
= SMBC_SHAREMODE_DENY_NONE
;
6187 /* backward compat */
6189 context
->open
= smbc_open_ctx
;
6190 context
->creat
= smbc_creat_ctx
;
6191 context
->read
= smbc_read_ctx
;
6192 context
->write
= smbc_write_ctx
;
6193 context
->close_fn
= smbc_close_ctx
;
6194 context
->unlink
= smbc_unlink_ctx
;
6195 context
->rename
= smbc_rename_ctx
;
6196 context
->lseek
= smbc_lseek_ctx
;
6197 context
->stat
= smbc_stat_ctx
;
6198 context
->fstat
= smbc_fstat_ctx
;
6199 context
->opendir
= smbc_opendir_ctx
;
6200 context
->closedir
= smbc_closedir_ctx
;
6201 context
->readdir
= smbc_readdir_ctx
;
6202 context
->getdents
= smbc_getdents_ctx
;
6203 context
->mkdir
= smbc_mkdir_ctx
;
6204 context
->rmdir
= smbc_rmdir_ctx
;
6205 context
->telldir
= smbc_telldir_ctx
;
6206 context
->lseekdir
= smbc_lseekdir_ctx
;
6207 context
->fstatdir
= smbc_fstatdir_ctx
;
6208 context
->chmod
= smbc_chmod_ctx
;
6209 context
->utimes
= smbc_utimes_ctx
;
6210 context
->setxattr
= smbc_setxattr_ctx
;
6211 context
->getxattr
= smbc_getxattr_ctx
;
6212 context
->removexattr
= smbc_removexattr_ctx
;
6213 context
->listxattr
= smbc_listxattr_ctx
;
6214 context
->open_print_job
= smbc_open_print_job_ctx
;
6215 context
->print_file
= smbc_print_file_ctx
;
6216 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
6217 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
6219 context
->callbacks
.check_server_fn
= smbc_check_server
;
6220 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
6222 smbc_default_cache_functions(context
);
6230 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6231 * and thus you'll be leaking memory if not handled properly.
6235 smbc_free_context(SMBCCTX
*context
,
6245 DEBUG(1,("Performing aggressive shutdown.\n"));
6247 f
= context
->internal
->_files
;
6249 context
->close_fn(context
, f
);
6252 context
->internal
->_files
= NULL
;
6254 /* First try to remove the servers the nice way. */
6255 if (context
->callbacks
.purge_cached_fn(context
)) {
6258 DEBUG(1, ("Could not purge all servers, "
6259 "Nice way shutdown failed.\n"));
6260 s
= context
->internal
->_servers
;
6262 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6264 cli_shutdown(s
->cli
);
6265 context
->callbacks
.remove_cached_srv_fn(context
,
6268 DLIST_REMOVE(context
->internal
->_servers
, s
);
6272 context
->internal
->_servers
= NULL
;
6276 /* This is the polite way */
6277 if (context
->callbacks
.purge_cached_fn(context
)) {
6278 DEBUG(1, ("Could not purge all servers, "
6279 "free_context failed.\n"));
6283 if (context
->internal
->_servers
) {
6284 DEBUG(1, ("Active servers in context, "
6285 "free_context failed.\n"));
6289 if (context
->internal
->_files
) {
6290 DEBUG(1, ("Active files in context, "
6291 "free_context failed.\n"));
6297 /* Things we have to clean up */
6298 SAFE_FREE(context
->workgroup
);
6299 SAFE_FREE(context
->netbios_name
);
6300 SAFE_FREE(context
->user
);
6302 DEBUG(3, ("Context %p succesfully freed\n", context
));
6303 SAFE_FREE(context
->internal
);
6310 * Each time the context structure is changed, we have binary backward
6311 * compatibility issues. Instead of modifying the public portions of the
6312 * context structure to add new options, instead, we put them in the internal
6313 * portion of the context structure and provide a set function for these new
6317 smbc_option_set(SMBCCTX
*context
,
6319 ... /* option_value */)
6325 smbc_get_auth_data_with_context_fn auth_fn
;
6329 va_start(ap
, option_name
);
6331 if (strcmp(option_name
, "debug_to_stderr") == 0) {
6333 * Log to standard error instead of standard output.
6335 option_value
.b
= (BOOL
) va_arg(ap
, int);
6336 context
->internal
->_debug_stderr
= option_value
.b
;
6338 } else if (strcmp(option_name
, "full_time_names") == 0) {
6340 * Use new-style time attribute names, e.g. WRITE_TIME rather
6341 * than the old-style names such as M_TIME. This allows also
6342 * setting/getting CREATE_TIME which was previously
6343 * unimplemented. (Note that the old C_TIME was supposed to
6344 * be CHANGE_TIME but was confused and sometimes referred to
6347 option_value
.b
= (BOOL
) va_arg(ap
, int);
6348 context
->internal
->_full_time_names
= option_value
.b
;
6350 } else if (strcmp(option_name
, "open_share_mode") == 0) {
6352 * The share mode to use for files opened with
6353 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6355 option_value
.i
= va_arg(ap
, int);
6356 context
->internal
->_share_mode
=
6357 (smbc_share_mode
) option_value
.i
;
6359 } else if (strcmp(option_name
, "auth_function") == 0) {
6361 * Use the new-style authentication function which includes
6364 option_value
.auth_fn
=
6365 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
6366 context
->internal
->_auth_fn_with_context
=
6367 option_value
.auth_fn
;
6368 } else if (strcmp(option_name
, "user_data") == 0) {
6370 * Save a user data handle which may be retrieved by the user
6371 * with smbc_option_get()
6373 option_value
.v
= va_arg(ap
, void *);
6374 context
->internal
->_user_data
= option_value
.v
;
6382 * Retrieve the current value of an option
6385 smbc_option_get(SMBCCTX
*context
,
6388 if (strcmp(option_name
, "debug_stderr") == 0) {
6390 * Log to standard error instead of standard output.
6392 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6393 return (void *) (intptr_t) context
->internal
->_debug_stderr
;
6395 return (void *) context
->internal
->_debug_stderr
;
6397 } else if (strcmp(option_name
, "full_time_names") == 0) {
6399 * Use new-style time attribute names, e.g. WRITE_TIME rather
6400 * than the old-style names such as M_TIME. This allows also
6401 * setting/getting CREATE_TIME which was previously
6402 * unimplemented. (Note that the old C_TIME was supposed to
6403 * be CHANGE_TIME but was confused and sometimes referred to
6406 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6407 return (void *) (intptr_t) context
->internal
->_full_time_names
;
6409 return (void *) context
->internal
->_full_time_names
;
6412 } else if (strcmp(option_name
, "auth_function") == 0) {
6414 * Use the new-style authentication function which includes
6417 return (void *) context
->internal
->_auth_fn_with_context
;
6418 } else if (strcmp(option_name
, "user_data") == 0) {
6420 * Save a user data handle which may be retrieved by the user
6421 * with smbc_option_get()
6423 return context
->internal
->_user_data
;
6431 * Initialise the library etc
6433 * We accept a struct containing handle information.
6434 * valid values for info->debug from 0 to 100,
6435 * and insist that info->fn must be non-null.
6438 smbc_init_context(SMBCCTX
*context
)
6445 if (!context
|| !context
->internal
) {
6450 /* Do not initialise the same client twice */
6451 if (context
->internal
->_initialized
) {
6455 if ((!context
->callbacks
.auth_fn
&&
6456 !context
->internal
->_auth_fn_with_context
) ||
6457 context
->debug
< 0 ||
6458 context
->debug
> 100) {
6465 if (!smbc_initialized
) {
6467 * Do some library-wide intializations the first time we get
6470 BOOL conf_loaded
= False
;
6472 /* Set this to what the user wants */
6473 DEBUGLEVEL
= context
->debug
;
6477 setup_logging("libsmbclient", True
);
6478 if (context
->internal
->_debug_stderr
) {
6480 x_setbuf(x_stderr
, NULL
);
6483 /* Here we would open the smb.conf file if needed ... */
6485 in_client
= True
; /* FIXME, make a param */
6487 home
= getenv("HOME");
6489 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
6490 if (lp_load(conf
, True
, False
, False
, True
)) {
6493 DEBUG(5, ("Could not load config file: %s\n",
6500 * Well, if that failed, try the dyn_CONFIGFILE
6501 * Which points to the standard locn, and if that
6502 * fails, silently ignore it and use the internal
6506 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
, False
)) {
6507 DEBUG(5, ("Could not load config file: %s\n",
6511 * We loaded the global config file. Now lets
6512 * load user-specific modifications to the
6515 slprintf(conf
, sizeof(conf
),
6516 "%s/.smb/smb.conf.append", home
);
6517 if (!lp_load(conf
, True
, False
, False
, False
)) {
6519 ("Could not append config file: "
6526 load_interfaces(); /* Load the list of interfaces ... */
6528 reopen_logs(); /* Get logging working ... */
6531 * Block SIGPIPE (from lib/util_sock.c: write())
6532 * It is not needed and should not stop execution
6534 BlockSignals(True
, SIGPIPE
);
6536 /* Done with one-time initialisation */
6537 smbc_initialized
= 1;
6541 if (!context
->user
) {
6543 * FIXME: Is this the best way to get the user info?
6545 user
= getenv("USER");
6546 /* walk around as "guest" if no username can be found */
6547 if (!user
) context
->user
= SMB_STRDUP("guest");
6548 else context
->user
= SMB_STRDUP(user
);
6551 if (!context
->netbios_name
) {
6553 * We try to get our netbios name from the config. If that
6554 * fails we fall back on constructing our netbios name from
6557 if (global_myname()) {
6558 context
->netbios_name
= SMB_STRDUP(global_myname());
6562 * Hmmm, I want to get hostname as well, but I am too
6563 * lazy for the moment
6566 context
->netbios_name
= (char *)SMB_MALLOC(17);
6567 if (!context
->netbios_name
) {
6571 slprintf(context
->netbios_name
, 16,
6572 "smbc%s%d", context
->user
, pid
);
6576 DEBUG(1, ("Using netbios name %s.\n", context
->netbios_name
));
6578 if (!context
->workgroup
) {
6579 if (lp_workgroup()) {
6580 context
->workgroup
= SMB_STRDUP(lp_workgroup());
6583 /* TODO: Think about a decent default workgroup */
6584 context
->workgroup
= SMB_STRDUP("samba");
6588 DEBUG(1, ("Using workgroup %s.\n", context
->workgroup
));
6590 /* shortest timeout is 1 second */
6591 if (context
->timeout
> 0 && context
->timeout
< 1000)
6592 context
->timeout
= 1000;
6595 * FIXME: Should we check the function pointers here?
6598 context
->internal
->_initialized
= True
;
6604 /* Return the verion of samba, and thus libsmbclient */
6608 return samba_version_string();