2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003, 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "include/libsmb_internal.h"
29 struct smbc_dirent
*smbc_readdir_ctx(SMBCCTX
*context
, SMBCFILE
*dir
);
30 struct smbc_dir_list
*smbc_check_dir_ent(struct smbc_dir_list
*list
,
31 struct smbc_dirent
*dirent
);
34 * DOS Attribute values (used internally)
36 typedef struct DOS_ATTR_DESC
{
48 * Internal flags for extended attributes
51 /* internal mode values */
52 #define SMBC_XATTR_MODE_ADD 1
53 #define SMBC_XATTR_MODE_REMOVE 2
54 #define SMBC_XATTR_MODE_REMOVE_ALL 3
55 #define SMBC_XATTR_MODE_SET 4
56 #define SMBC_XATTR_MODE_CHOWN 5
57 #define SMBC_XATTR_MODE_CHGRP 6
59 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
61 /*We should test for this in configure ... */
63 #define ENOTSUP EOPNOTSUPP
67 * Functions exported by libsmb_cache.c that we need here
69 int smbc_default_cache_functions(SMBCCTX
*context
);
72 * check if an element is part of the list.
73 * FIXME: Does not belong here !
74 * Can anyone put this in a macro in dlinklist.h ?
77 static int DLIST_CONTAINS(SMBCFILE
* list
, SMBCFILE
*p
) {
78 if (!p
|| !list
) return False
;
80 if (p
== list
) return True
;
87 * Find an lsa pipe handle associated with a cli struct.
89 static struct rpc_pipe_client
*
90 find_lsa_pipe_hnd(struct cli_state
*ipc_cli
)
92 struct rpc_pipe_client
*pipe_hnd
;
94 for (pipe_hnd
= ipc_cli
->pipe_list
;
96 pipe_hnd
= pipe_hnd
->next
) {
98 if (pipe_hnd
->pipe_idx
== PI_LSARPC
) {
107 smbc_close_ctx(SMBCCTX
*context
,
110 smbc_lseek_ctx(SMBCCTX
*context
,
115 extern BOOL in_client
;
118 * Is the logging working / configfile read ?
120 static int smbc_initialized
= 0;
123 hex2int( unsigned int _char
)
125 if ( _char
>= 'A' && _char
<='F')
126 return _char
- 'A' + 10;
127 if ( _char
>= 'a' && _char
<='f')
128 return _char
- 'a' + 10;
129 if ( _char
>= '0' && _char
<='9')
137 * Convert strings of %xx to their single character equivalent. Each 'x' must
138 * be a valid hexadecimal digit, or that % sequence is left undecoded.
140 * dest may, but need not be, the same pointer as src.
142 * Returns the number of % sequences which could not be converted due to lack
143 * of two following hexadecimal digits.
146 smbc_urldecode(char *dest
, char * src
, size_t max_dest_len
)
148 int old_length
= strlen(src
);
154 if ( old_length
== 0 ) {
159 while ( i
< old_length
) {
160 unsigned char character
= src
[ i
++ ];
162 if (character
== '%') {
163 int a
= i
+1 < old_length
? hex2int( src
[i
] ) : -1;
164 int b
= i
+1 < old_length
? hex2int( src
[i
+1] ) : -1;
166 /* Replace valid sequence */
167 if (a
!= -1 && b
!= -1) {
169 /* Replace valid %xx sequence with %dd */
170 character
= (a
* 16) + b
;
172 if (character
== '\0') {
173 break; /* Stop at %00 */
188 strncpy(dest
, temp
, max_dest_len
- 1);
189 dest
[max_dest_len
- 1] = '\0';
197 * Convert any characters not specifically allowed in a URL into their %xx
200 * Returns the remaining buffer length.
203 smbc_urlencode(char * dest
, char * src
, int max_dest_len
)
205 char hex
[] = "0123456789ABCDEF";
207 for (; *src
!= '\0' && max_dest_len
>= 3; src
++) {
219 *dest
++ = hex
[(*src
>> 4) & 0x0f];
220 *dest
++ = hex
[*src
& 0x0f];
235 * Function to parse a path and turn it into components
237 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
238 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
239 * general format ("smb:" only; we do not look for "cifs:").
243 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
247 * smb:// Show all workgroups.
249 * The method of locating the list of workgroups varies
250 * depending upon the setting of the context variable
251 * context->options.browse_max_lmb_count. This value
252 * determine the maximum number of local master browsers to
253 * query for the list of workgroups. In order to ensure that
254 * a complete list of workgroups is obtained, all master
255 * browsers must be queried, but if there are many
256 * workgroups, the time spent querying can begin to add up.
257 * For small networks (not many workgroups), it is suggested
258 * that this variable be set to 0, indicating query all local
259 * master browsers. When the network has many workgroups, a
260 * reasonable setting for this variable might be around 3.
262 * smb://name/ if name<1D> or name<1B> exists, list servers in
263 * workgroup, else, if name<20> exists, list all shares
266 * If "options" are provided, this function returns the entire option list as a
267 * string, for later parsing by the caller. Note that currently, no options
271 static const char *smbc_prefix
= "smb:";
274 smbc_parse_path(SMBCCTX
*context
,
276 char *workgroup
, int workgroup_len
,
277 char *server
, int server_len
,
278 char *share
, int share_len
,
279 char *path
, int path_len
,
280 char *user
, int user_len
,
281 char *password
, int password_len
,
282 char *options
, int options_len
)
290 server
[0] = share
[0] = path
[0] = user
[0] = password
[0] = (char)0;
293 * Assume we wont find an authentication domain to parse, so default
294 * to the workgroup in the provided context.
296 if (workgroup
!= NULL
) {
297 strncpy(workgroup
, context
->workgroup
, workgroup_len
- 1);
298 workgroup
[workgroup_len
- 1] = '\0';
301 if (options
!= NULL
&& options_len
> 0) {
302 options
[0] = (char)0;
306 /* see if it has the right prefix */
307 len
= strlen(smbc_prefix
);
308 if (strncmp(s
,smbc_prefix
,len
) || (s
[len
] != '/' && s
[len
] != 0)) {
309 return -1; /* What about no smb: ? */
314 /* Watch the test below, we are testing to see if we should exit */
316 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
318 DEBUG(1, ("Invalid path (does not begin with smb://"));
323 p
+= 2; /* Skip the double slash */
325 /* See if any options were specified */
326 if ((q
= strrchr(p
, '?')) != NULL
) {
327 /* There are options. Null terminate here and point to them */
330 DEBUG(4, ("Found options '%s'", q
));
332 /* Copy the options */
333 if (options
!= NULL
&& options_len
> 0) {
334 safe_strcpy(options
, q
, options_len
- 1);
342 int wl
= strlen(context
->workgroup
);
348 strncpy(server
, context
->workgroup
, wl
);
354 * ok, its for us. Now parse out the server, share etc.
356 * However, we want to parse out [[domain;]user[:password]@] if it
360 /* check that '@' occurs before '/', if '/' exists at all */
361 q
= strchr_m(p
, '@');
362 r
= strchr_m(p
, '/');
363 if (q
&& (!r
|| q
< r
)) {
364 pstring username
, passwd
, domain
;
365 const char *u
= userinfo
;
367 next_token_no_ltrim(&p
, userinfo
, "@", sizeof(fstring
));
369 username
[0] = passwd
[0] = domain
[0] = 0;
371 if (strchr_m(u
, ';')) {
373 next_token_no_ltrim(&u
, domain
, ";", sizeof(fstring
));
377 if (strchr_m(u
, ':')) {
379 next_token_no_ltrim(&u
, username
, ":", sizeof(fstring
));
386 pstrcpy(username
, u
);
390 if (domain
[0] && workgroup
) {
391 strncpy(workgroup
, domain
, workgroup_len
- 1);
392 workgroup
[workgroup_len
- 1] = '\0';
396 strncpy(user
, username
, user_len
- 1);
397 user
[user_len
- 1] = '\0';
401 strncpy(password
, passwd
, password_len
- 1);
402 password
[password_len
- 1] = '\0';
407 if (!next_token(&p
, server
, "/", sizeof(fstring
))) {
413 if (*p
== (char)0) goto decoding
; /* That's it ... */
415 if (!next_token(&p
, share
, "/", sizeof(fstring
))) {
422 * Prepend a leading slash if there's a file path, as required by
428 safe_strcpy(path
+ 1, p
, path_len
- 2);
431 all_string_sub(path
, "/", "\\", 0);
434 (void) smbc_urldecode(path
, path
, path_len
);
435 (void) smbc_urldecode(server
, server
, server_len
);
436 (void) smbc_urldecode(share
, share
, share_len
);
437 (void) smbc_urldecode(user
, user
, user_len
);
438 (void) smbc_urldecode(password
, password
, password_len
);
444 * Verify that the options specified in a URL are valid
447 smbc_check_options(char *server
,
452 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
453 "path='%s' options='%s'\n",
454 server
, share
, path
, options
));
456 /* No options at all is always ok */
457 if (! *options
) return 0;
459 /* Currently, we don't support any options. */
464 * Convert an SMB error into a UNIX error ...
467 smbc_errno(SMBCCTX
*context
,
470 int ret
= cli_errno(c
);
472 if (cli_is_dos_error(c
)) {
476 cli_dos_error(c
, &eclass
, &ecode
);
478 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
479 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
483 status
= cli_nt_error(c
);
485 DEBUG(3,("smbc errno %s -> %d\n",
486 nt_errstr(status
), ret
));
493 * Check a server for being alive and well.
494 * returns 0 if the server is in shape. Returns 1 on error
496 * Also useable outside libsmbclient to enable external cache
497 * to do some checks too.
500 smbc_check_server(SMBCCTX
* context
,
504 struct sockaddr addr
;
507 * Although the use of port 139 is not a guarantee that we're using
508 * netbios, we assume so. We don't want to send a keepalive packet if
509 * not netbios because it's not valid, and Vista, at least,
510 * disconnects the client on such a request.
512 if (server
->cli
->port
== 139) {
513 /* Assuming netbios. Send a keepalive packet */
514 if ( send_keepalive(server
->cli
->fd
) == False
) {
519 * Assuming not netbios. Try a different method to detect if
520 * the connection is still alive.
523 if (getpeername(server
->cli
->fd
, &addr
, &size
) == -1) {
528 /* connection is ok */
533 * Remove a server from the cached server list it's unused.
534 * On success, 0 is returned. 1 is returned if the server could not be removed.
536 * Also useable outside libsmbclient
539 smbc_remove_unused_server(SMBCCTX
* context
,
544 /* are we being fooled ? */
545 if (!context
|| !context
->internal
||
546 !context
->internal
->_initialized
|| !srv
) return 1;
549 /* Check all open files/directories for a relation with this server */
550 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
551 if (file
->srv
== srv
) {
553 DEBUG(3, ("smbc_remove_usused_server: "
554 "%p still used by %p.\n",
560 DLIST_REMOVE(context
->internal
->_servers
, srv
);
562 cli_shutdown(srv
->cli
);
565 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
567 context
->callbacks
.remove_cached_srv_fn(context
, srv
);
575 find_server(SMBCCTX
*context
,
587 srv
= context
->callbacks
.get_cached_srv_fn(context
, server
, share
,
588 workgroup
, username
);
590 if (!auth_called
&& !srv
&& (!username
[0] || !password
[0])) {
591 if (context
->internal
->_auth_fn_with_context
!= NULL
) {
592 context
->internal
->_auth_fn_with_context(
595 workgroup
, sizeof(fstring
),
596 username
, sizeof(fstring
),
597 password
, sizeof(fstring
));
599 context
->callbacks
.auth_fn(
601 workgroup
, sizeof(fstring
),
602 username
, sizeof(fstring
),
603 password
, sizeof(fstring
));
607 * However, smbc_auth_fn may have picked up info relating to
608 * an existing connection, so try for an existing connection
612 goto check_server_cache
;
617 if (context
->callbacks
.check_server_fn(context
, srv
)) {
619 * This server is no good anymore
620 * Try to remove it and check for more possible
621 * servers in the cache
623 if (context
->callbacks
.remove_unused_server_fn(context
,
626 * We could not remove the server completely,
627 * remove it from the cache so we will not get
628 * it again. It will be removed when the last
629 * file/dir is closed.
631 context
->callbacks
.remove_cached_srv_fn(context
,
636 * Maybe there are more cached connections to this
639 goto check_server_cache
;
649 * Connect to a server, possibly on an existing connection
651 * Here, what we want to do is: If the server and username
652 * match an existing connection, reuse that, otherwise, establish a
655 * If we have to create a new connection, call the auth_fn to get the
656 * info we need, unless the username and password were passed in.
660 smbc_server(SMBCCTX
*context
,
661 BOOL connect_if_not_found
,
670 struct nmb_name called
, calling
;
671 const char *server_n
= server
;
674 int tried_reverse
= 0;
677 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 if (!cli_connect(c
, server_n
, &ip
)) {
800 /* First connection attempt failed. Try alternate port. */
801 c
->port
= port_try_next
;
803 if (!cli_connect(c
, server_n
, &ip
)) {
810 if (!cli_session_request(c
, &calling
, &called
)) {
812 if (strcmp(called
.name
, "*SMBSERVER")) {
813 make_nmb_name(&called
, "*SMBSERVER", 0x20);
815 } else { /* Try one more time, but ensure we don't loop */
817 /* Only try this if server is an IP address ... */
819 if (is_ipaddress(server
) && !tried_reverse
) {
821 struct in_addr rem_ip
;
823 if ((rem_ip
.s_addr
=inet_addr(server
)) == INADDR_NONE
) {
824 DEBUG(4, ("Could not convert IP address "
825 "%s to struct in_addr\n", server
));
830 tried_reverse
++; /* Yuck */
832 if (name_status_find("*", 0, 0, rem_ip
, remote_name
)) {
833 make_nmb_name(&called
, remote_name
, 0x20);
842 DEBUG(4,(" session request ok\n"));
844 if (!cli_negprot(c
)) {
850 username_used
= username
;
852 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
853 password
, strlen(password
),
854 password
, strlen(password
),
857 /* Failed. Try an anonymous login, if allowed by flags. */
860 if ((context
->flags
& SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON
) ||
861 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
872 DEBUG(4,(" session setup ok\n"));
874 if (!cli_send_tconX(c
, share
, "?????",
875 password
, strlen(password
)+1)) {
876 errno
= smbc_errno(context
, c
);
881 DEBUG(4,(" tconx ok\n"));
884 * Ok, we have got a nice connection
885 * Let's allocate a server structure.
888 srv
= SMB_MALLOC_P(SMBCSRV
);
896 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
897 srv
->no_pathinfo
= False
;
898 srv
->no_pathinfo2
= False
;
899 srv
->no_nt_session
= False
;
901 /* now add it to the cache (internal or external) */
902 /* Let the cache function set errno if it wants to */
904 if (context
->callbacks
.add_cached_srv_fn(context
, srv
, server
, share
, workgroup
, username
)) {
905 int saved_errno
= errno
;
906 DEBUG(3, (" Failed to add server to cache\n"));
914 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
915 server
, share
, srv
));
917 DLIST_ADD(context
->internal
->_servers
, srv
);
931 * Connect to a server for getting/setting attributes, possibly on an existing
932 * connection. This works similarly to smbc_server().
935 smbc_attr_server(SMBCCTX
*context
,
945 struct cli_state
*ipc_cli
;
946 struct rpc_pipe_client
*pipe_hnd
;
948 SMBCSRV
*ipc_srv
=NULL
;
951 * See if we've already created this special connection. Reference
952 * our "special" share name '*IPC$', which is an impossible real share
953 * name due to the leading asterisk.
955 ipc_srv
= find_server(context
, server
, "*IPC$",
956 workgroup
, username
, password
);
959 /* We didn't find a cached connection. Get the password */
960 if (*password
== '\0') {
961 /* ... then retrieve it now. */
962 if (context
->internal
->_auth_fn_with_context
!= NULL
) {
963 context
->internal
->_auth_fn_with_context(
966 workgroup
, sizeof(fstring
),
967 username
, sizeof(fstring
),
968 password
, sizeof(fstring
));
970 context
->callbacks
.auth_fn(
972 workgroup
, sizeof(fstring
),
973 username
, sizeof(fstring
),
974 password
, sizeof(fstring
));
979 if (context
->flags
& SMB_CTX_FLAG_USE_KERBEROS
) {
980 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
984 nt_status
= cli_full_connection(&ipc_cli
,
985 global_myname(), server
,
986 &ip
, 0, "IPC$", "?????",
990 if (! NT_STATUS_IS_OK(nt_status
)) {
991 DEBUG(1,("cli_full_connection failed! (%s)\n",
992 nt_errstr(nt_status
)));
997 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
1000 cli_shutdown(ipc_cli
);
1004 ZERO_STRUCTP(ipc_srv
);
1005 ipc_srv
->cli
= ipc_cli
;
1008 pipe_hnd
= cli_rpc_pipe_open_noauth(ipc_srv
->cli
,
1012 DEBUG(1, ("cli_nt_session_open fail!\n"));
1014 cli_shutdown(ipc_srv
->cli
);
1020 * Some systems don't support
1021 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1022 * so we might as well do it too.
1025 nt_status
= rpccli_lsa_open_policy(
1027 ipc_srv
->cli
->mem_ctx
,
1029 GENERIC_EXECUTE_ACCESS
,
1032 if (!NT_STATUS_IS_OK(nt_status
)) {
1033 errno
= smbc_errno(context
, ipc_srv
->cli
);
1034 cli_shutdown(ipc_srv
->cli
);
1039 /* now add it to the cache (internal or external) */
1041 errno
= 0; /* let cache function set errno if it likes */
1042 if (context
->callbacks
.add_cached_srv_fn(context
, ipc_srv
,
1047 DEBUG(3, (" Failed to add server to cache\n"));
1051 cli_shutdown(ipc_srv
->cli
);
1056 DLIST_ADD(context
->internal
->_servers
, ipc_srv
);
1063 * Routine to open() a file ...
1067 smbc_open_ctx(SMBCCTX
*context
,
1072 fstring server
, share
, user
, password
, workgroup
;
1075 struct cli_state
*targetcli
;
1076 SMBCSRV
*srv
= NULL
;
1077 SMBCFILE
*file
= NULL
;
1080 if (!context
|| !context
->internal
||
1081 !context
->internal
->_initialized
) {
1083 errno
= EINVAL
; /* Best I can think of ... */
1095 if (smbc_parse_path(context
, fname
,
1096 workgroup
, sizeof(workgroup
),
1097 server
, sizeof(server
),
1098 share
, sizeof(share
),
1101 password
, sizeof(password
),
1107 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1109 srv
= smbc_server(context
, True
,
1110 server
, share
, workgroup
, user
, password
);
1114 if (errno
== EPERM
) errno
= EACCES
;
1115 return NULL
; /* smbc_server sets errno */
1119 /* Hmmm, the test for a directory is suspect here ... FIXME */
1121 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
1128 file
= SMB_MALLOC_P(SMBCFILE
);
1139 /*d_printf(">>>open: resolving %s\n", path);*/
1140 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
1142 d_printf("Could not resolve %s\n", path
);
1146 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1148 if ((fd
= cli_open(targetcli
, targetpath
, flags
,
1149 context
->internal
->_share_mode
)) < 0) {
1151 /* Handle the error ... */
1154 errno
= smbc_errno(context
, targetcli
);
1159 /* Fill in file struct */
1162 file
->fname
= SMB_STRDUP(fname
);
1167 DLIST_ADD(context
->internal
->_files
, file
);
1170 * If the file was opened in O_APPEND mode, all write
1171 * operations should be appended to the file. To do that,
1172 * though, using this protocol, would require a getattrE()
1173 * call for each and every write, to determine where the end
1174 * of the file is. (There does not appear to be an append flag
1175 * in the protocol.) Rather than add all of that overhead of
1176 * retrieving the current end-of-file offset prior to each
1177 * write operation, we'll assume that most append operations
1178 * will continuously write, so we'll just set the offset to
1179 * the end of the file now and hope that's adequate.
1181 * Note to self: If this proves inadequate, and O_APPEND
1182 * should, in some cases, be forced for each write, add a
1183 * field in the context options structure, for
1184 * "strict_append_mode" which would select between the current
1185 * behavior (if FALSE) or issuing a getattrE() prior to each
1186 * write and forcing the write to the end of the file (if
1187 * TRUE). Adding that capability will likely require adding
1188 * an "append" flag into the _SMBCFILE structure to track
1189 * whether a file was opened in O_APPEND mode. -- djl
1191 if (flags
& O_APPEND
) {
1192 if (smbc_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
1193 (void) smbc_close_ctx(context
, file
);
1203 /* Check if opendir needed ... */
1208 eno
= smbc_errno(context
, srv
->cli
);
1209 file
= context
->opendir(context
, fname
);
1210 if (!file
) errno
= eno
;
1215 errno
= EINVAL
; /* FIXME, correct errno ? */
1221 * Routine to create a file
1224 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
1227 smbc_creat_ctx(SMBCCTX
*context
,
1232 if (!context
|| !context
->internal
||
1233 !context
->internal
->_initialized
) {
1240 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
1244 * Routine to read() a file ...
1248 smbc_read_ctx(SMBCCTX
*context
,
1254 fstring server
, share
, user
, password
;
1255 pstring path
, targetpath
;
1256 struct cli_state
*targetcli
;
1261 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1262 * appears to pass file->offset (which is type off_t) differently than
1263 * a local variable of type off_t. Using local variable "offset" in
1264 * the call to cli_read() instead of file->offset fixes a problem
1265 * retrieving data at an offset greater than 4GB.
1269 if (!context
|| !context
->internal
||
1270 !context
->internal
->_initialized
) {
1277 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
1279 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1286 offset
= file
->offset
;
1288 /* Check that the buffer exists ... */
1297 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1298 if (smbc_parse_path(context
, file
->fname
,
1300 server
, sizeof(server
),
1301 share
, sizeof(share
),
1304 password
, sizeof(password
),
1310 /*d_printf(">>>read: resolving %s\n", path);*/
1311 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1312 &targetcli
, targetpath
))
1314 d_printf("Could not resolve %s\n", path
);
1317 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1319 ret
= cli_read(targetcli
, file
->cli_fd
, (char *)buf
, offset
, count
);
1323 errno
= smbc_errno(context
, targetcli
);
1328 file
->offset
+= ret
;
1330 DEBUG(4, (" --> %d\n", ret
));
1332 return ret
; /* Success, ret bytes of data ... */
1337 * Routine to write() a file ...
1341 smbc_write_ctx(SMBCCTX
*context
,
1348 fstring server
, share
, user
, password
;
1349 pstring path
, targetpath
;
1350 struct cli_state
*targetcli
;
1352 /* First check all pointers before dereferencing them */
1354 if (!context
|| !context
->internal
||
1355 !context
->internal
->_initialized
) {
1362 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1369 /* Check that the buffer exists ... */
1378 offset
= file
->offset
; /* See "offset" comment in smbc_read_ctx() */
1380 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1381 if (smbc_parse_path(context
, file
->fname
,
1383 server
, sizeof(server
),
1384 share
, sizeof(share
),
1387 password
, sizeof(password
),
1393 /*d_printf(">>>write: resolving %s\n", path);*/
1394 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1395 &targetcli
, targetpath
))
1397 d_printf("Could not resolve %s\n", path
);
1400 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1403 ret
= cli_write(targetcli
, file
->cli_fd
, 0, (char *)buf
, offset
, count
);
1407 errno
= smbc_errno(context
, targetcli
);
1412 file
->offset
+= ret
;
1414 return ret
; /* Success, 0 bytes of data ... */
1418 * Routine to close() a file ...
1422 smbc_close_ctx(SMBCCTX
*context
,
1426 fstring server
, share
, user
, password
;
1427 pstring path
, targetpath
;
1428 struct cli_state
*targetcli
;
1430 if (!context
|| !context
->internal
||
1431 !context
->internal
->_initialized
) {
1438 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1448 return context
->closedir(context
, file
);
1452 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1453 if (smbc_parse_path(context
, file
->fname
,
1455 server
, sizeof(server
),
1456 share
, sizeof(share
),
1459 password
, sizeof(password
),
1465 /*d_printf(">>>close: resolving %s\n", path);*/
1466 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1467 &targetcli
, targetpath
))
1469 d_printf("Could not resolve %s\n", path
);
1472 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1474 if (!cli_close(targetcli
, file
->cli_fd
)) {
1476 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1478 /* Deallocate slot and remove the server
1479 * from the server cache if unused */
1480 errno
= smbc_errno(context
, targetcli
);
1482 DLIST_REMOVE(context
->internal
->_files
, file
);
1483 SAFE_FREE(file
->fname
);
1485 context
->callbacks
.remove_unused_server_fn(context
, srv
);
1491 DLIST_REMOVE(context
->internal
->_files
, file
);
1492 SAFE_FREE(file
->fname
);
1499 * Get info from an SMB server on a file. Use a qpathinfo call first
1500 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1503 smbc_getatr(SMBCCTX
* context
,
1508 struct timespec
*create_time_ts
,
1509 struct timespec
*access_time_ts
,
1510 struct timespec
*write_time_ts
,
1511 struct timespec
*change_time_ts
,
1516 struct cli_state
*targetcli
;
1519 if (!context
|| !context
->internal
||
1520 !context
->internal
->_initialized
) {
1527 /* path fixup for . and .. */
1528 if (strequal(path
, ".") || strequal(path
, ".."))
1529 pstrcpy(fixedpath
, "\\");
1532 pstrcpy(fixedpath
, path
);
1533 trim_string(fixedpath
, NULL
, "\\..");
1534 trim_string(fixedpath
, NULL
, "\\.");
1536 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1538 if (!cli_resolve_path( "", srv
->cli
, fixedpath
, &targetcli
, targetpath
))
1540 d_printf("Couldn't resolve %s\n", path
);
1544 if (!srv
->no_pathinfo2
&&
1545 cli_qpathinfo2(targetcli
, targetpath
,
1554 /* if this is NT then don't bother with the getatr */
1555 if (targetcli
->capabilities
& CAP_NT_SMBS
) {
1560 if (cli_getatr(targetcli
, targetpath
, mode
, size
, &write_time
)) {
1562 struct timespec w_time_ts
;
1564 w_time_ts
= convert_time_t_to_timespec(write_time
);
1566 if (write_time_ts
!= NULL
) {
1567 *write_time_ts
= w_time_ts
;
1570 if (create_time_ts
!= NULL
) {
1571 *create_time_ts
= w_time_ts
;
1574 if (access_time_ts
!= NULL
) {
1575 *access_time_ts
= w_time_ts
;
1578 if (change_time_ts
!= NULL
) {
1579 *change_time_ts
= w_time_ts
;
1582 srv
->no_pathinfo2
= True
;
1592 * Set file info on an SMB server. Use setpathinfo call first. If that
1593 * fails, use setattrE..
1595 * Access and modification time parameters are always used and must be
1596 * provided. Create time, if zero, will be determined from the actual create
1597 * time of the file. If non-zero, the create time will be set as well.
1599 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1602 smbc_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
1613 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1614 * modern function for "new code" to be using, and it works given a
1615 * filename rather than requiring that the file be opened to have its
1616 * attributes manipulated.
1618 if (srv
->no_pathinfo
||
1619 ! cli_setpathinfo(srv
->cli
, path
,
1627 * setpathinfo is not supported; go to plan B.
1629 * cli_setatr() does not work on win98, and it also doesn't
1630 * support setting the access time (only the modification
1631 * time), so in all cases, we open the specified file and use
1632 * cli_setattrE() which should work on all OS versions, and
1633 * supports both times.
1636 /* Don't try {q,set}pathinfo() again, with this server */
1637 srv
->no_pathinfo
= True
;
1640 if ((fd
= cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
)) < 0) {
1642 errno
= smbc_errno(context
, srv
->cli
);
1646 /* Set the new attributes */
1647 ret
= cli_setattrE(srv
->cli
, fd
,
1652 /* Close the file */
1653 cli_close(srv
->cli
, fd
);
1656 * Unfortunately, setattrE() doesn't have a provision for
1657 * setting the access mode (attributes). We'll have to try
1658 * cli_setatr() for that, and with only this parameter, it
1659 * seems to work on win98.
1661 if (ret
&& mode
!= (uint16
) -1) {
1662 ret
= cli_setatr(srv
->cli
, path
, mode
, 0);
1666 errno
= smbc_errno(context
, srv
->cli
);
1675 * Routine to unlink() a file
1679 smbc_unlink_ctx(SMBCCTX
*context
,
1682 fstring server
, share
, user
, password
, workgroup
;
1683 pstring path
, targetpath
;
1684 struct cli_state
*targetcli
;
1685 SMBCSRV
*srv
= NULL
;
1687 if (!context
|| !context
->internal
||
1688 !context
->internal
->_initialized
) {
1690 errno
= EINVAL
; /* Best I can think of ... */
1702 if (smbc_parse_path(context
, fname
,
1703 workgroup
, sizeof(workgroup
),
1704 server
, sizeof(server
),
1705 share
, sizeof(share
),
1708 password
, sizeof(password
),
1714 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
1716 srv
= smbc_server(context
, True
,
1717 server
, share
, workgroup
, user
, password
);
1721 return -1; /* smbc_server sets errno */
1725 /*d_printf(">>>unlink: resolving %s\n", path);*/
1726 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
1728 d_printf("Could not resolve %s\n", path
);
1731 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1733 if (!cli_unlink(targetcli
, targetpath
)) {
1735 errno
= smbc_errno(context
, targetcli
);
1737 if (errno
== EACCES
) { /* Check if the file is a directory */
1742 struct timespec write_time_ts
;
1743 struct timespec access_time_ts
;
1744 struct timespec change_time_ts
;
1747 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1754 /* Hmmm, bad error ... What? */
1756 errno
= smbc_errno(context
, targetcli
);
1762 if (IS_DOS_DIR(mode
))
1765 errno
= saverr
; /* Restore this */
1774 return 0; /* Success ... */
1779 * Routine to rename() a file
1783 smbc_rename_ctx(SMBCCTX
*ocontext
,
1799 pstring targetpath1
;
1800 pstring targetpath2
;
1801 struct cli_state
*targetcli1
;
1802 struct cli_state
*targetcli2
;
1803 SMBCSRV
*srv
= NULL
;
1805 if (!ocontext
|| !ncontext
||
1806 !ocontext
->internal
|| !ncontext
->internal
||
1807 !ocontext
->internal
->_initialized
||
1808 !ncontext
->internal
->_initialized
) {
1810 errno
= EINVAL
; /* Best I can think of ... */
1815 if (!oname
|| !nname
) {
1822 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1824 smbc_parse_path(ocontext
, oname
,
1825 workgroup
, sizeof(workgroup
),
1826 server1
, sizeof(server1
),
1827 share1
, sizeof(share1
),
1828 path1
, sizeof(path1
),
1829 user1
, sizeof(user1
),
1830 password1
, sizeof(password1
),
1833 if (user1
[0] == (char)0) fstrcpy(user1
, ocontext
->user
);
1835 smbc_parse_path(ncontext
, nname
,
1837 server2
, sizeof(server2
),
1838 share2
, sizeof(share2
),
1839 path2
, sizeof(path2
),
1840 user2
, sizeof(user2
),
1841 password2
, sizeof(password2
),
1844 if (user2
[0] == (char)0) fstrcpy(user2
, ncontext
->user
);
1846 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
1847 strcmp(user1
, user2
)) {
1849 /* Can't rename across file systems, or users?? */
1856 srv
= smbc_server(ocontext
, True
,
1857 server1
, share1
, workgroup
, user1
, password1
);
1864 /*d_printf(">>>rename: resolving %s\n", path1);*/
1865 if (!cli_resolve_path( "", srv
->cli
, path1
, &targetcli1
, targetpath1
))
1867 d_printf("Could not resolve %s\n", path1
);
1870 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
1871 /*d_printf(">>>rename: resolving %s\n", path2);*/
1872 if (!cli_resolve_path( "", srv
->cli
, path2
, &targetcli2
, targetpath2
))
1874 d_printf("Could not resolve %s\n", path2
);
1877 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
1879 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
1880 strcmp(targetcli1
->share
, targetcli2
->share
))
1882 /* can't rename across file systems */
1888 if (!cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1889 int eno
= smbc_errno(ocontext
, targetcli1
);
1891 if (eno
!= EEXIST
||
1892 !cli_unlink(targetcli1
, targetpath2
) ||
1893 !cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
1901 return 0; /* Success */
1906 * A routine to lseek() a file
1910 smbc_lseek_ctx(SMBCCTX
*context
,
1916 fstring server
, share
, user
, password
;
1917 pstring path
, targetpath
;
1918 struct cli_state
*targetcli
;
1920 if (!context
|| !context
->internal
||
1921 !context
->internal
->_initialized
) {
1928 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1938 return -1; /* Can't lseek a dir ... */
1944 file
->offset
= offset
;
1948 file
->offset
+= offset
;
1952 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
1953 if (smbc_parse_path(context
, file
->fname
,
1955 server
, sizeof(server
),
1956 share
, sizeof(share
),
1959 password
, sizeof(password
),
1966 /*d_printf(">>>lseek: resolving %s\n", path);*/
1967 if (!cli_resolve_path("", file
->srv
->cli
, path
,
1968 &targetcli
, targetpath
))
1970 d_printf("Could not resolve %s\n", path
);
1973 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
1975 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, NULL
,
1976 &size
, NULL
, NULL
, NULL
, NULL
, NULL
))
1978 SMB_OFF_T b_size
= size
;
1979 if (!cli_getattrE(targetcli
, file
->cli_fd
,
1980 NULL
, &b_size
, NULL
, NULL
, NULL
))
1987 file
->offset
= size
+ offset
;
1996 return file
->offset
;
2001 * Generate an inode number from file name for those things that need it
2005 smbc_inode(SMBCCTX
*context
,
2009 if (!context
|| !context
->internal
||
2010 !context
->internal
->_initialized
) {
2017 if (!*name
) return 2; /* FIXME, why 2 ??? */
2018 return (ino_t
)str_checksum(name
);
2023 * Routine to put basic stat info into a stat structure ... Used by stat and
2028 smbc_setup_stat(SMBCCTX
*context
,
2037 if (IS_DOS_DIR(mode
)) {
2038 st
->st_mode
= SMBC_DIR_MODE
;
2040 st
->st_mode
= SMBC_FILE_MODE
;
2043 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
2044 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
2045 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
2046 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
2049 #ifdef HAVE_STAT_ST_BLKSIZE
2050 st
->st_blksize
= 512;
2052 #ifdef HAVE_STAT_ST_BLOCKS
2053 st
->st_blocks
= (size
+511)/512;
2055 st
->st_uid
= getuid();
2056 st
->st_gid
= getgid();
2058 if (IS_DOS_DIR(mode
)) {
2064 if (st
->st_ino
== 0) {
2065 st
->st_ino
= smbc_inode(context
, fname
);
2068 return True
; /* FIXME: Is this needed ? */
2073 * Routine to stat a file given a name
2077 smbc_stat_ctx(SMBCCTX
*context
,
2088 struct timespec write_time_ts
;
2089 struct timespec access_time_ts
;
2090 struct timespec change_time_ts
;
2095 if (!context
|| !context
->internal
||
2096 !context
->internal
->_initialized
) {
2098 errno
= EINVAL
; /* Best I can think of ... */
2110 DEBUG(4, ("smbc_stat(%s)\n", fname
));
2112 if (smbc_parse_path(context
, fname
,
2113 workgroup
, sizeof(workgroup
),
2114 server
, sizeof(server
),
2115 share
, sizeof(share
),
2118 password
, sizeof(password
),
2124 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2126 srv
= smbc_server(context
, True
,
2127 server
, share
, workgroup
, user
, password
);
2130 return -1; /* errno set by smbc_server */
2133 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
2140 errno
= smbc_errno(context
, srv
->cli
);
2147 smbc_setup_stat(context
, st
, path
, size
, mode
);
2149 set_atimespec(st
, access_time_ts
);
2150 set_ctimespec(st
, change_time_ts
);
2151 set_mtimespec(st
, write_time_ts
);
2152 st
->st_dev
= srv
->dev
;
2159 * Routine to stat a file given an fd
2163 smbc_fstat_ctx(SMBCCTX
*context
,
2167 struct timespec change_time_ts
;
2168 struct timespec access_time_ts
;
2169 struct timespec write_time_ts
;
2178 struct cli_state
*targetcli
;
2181 if (!context
|| !context
->internal
||
2182 !context
->internal
->_initialized
) {
2189 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
2198 return context
->fstatdir(context
, file
, st
);
2202 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2203 if (smbc_parse_path(context
, file
->fname
,
2205 server
, sizeof(server
),
2206 share
, sizeof(share
),
2209 password
, sizeof(password
),
2215 /*d_printf(">>>fstat: resolving %s\n", path);*/
2216 if (!cli_resolve_path("", file
->srv
->cli
, path
,
2217 &targetcli
, targetpath
))
2219 d_printf("Could not resolve %s\n", path
);
2222 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2224 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, &mode
, &size
,
2231 time_t change_time
, access_time
, write_time
;
2233 if (!cli_getattrE(targetcli
, file
->cli_fd
, &mode
, &size
,
2234 &change_time
, &access_time
, &write_time
)) {
2240 change_time_ts
= convert_time_t_to_timespec(change_time
);
2241 access_time_ts
= convert_time_t_to_timespec(access_time
);
2242 write_time_ts
= convert_time_t_to_timespec(write_time
);
2247 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
2249 set_atimespec(st
, access_time_ts
);
2250 set_ctimespec(st
, change_time_ts
);
2251 set_mtimespec(st
, write_time_ts
);
2252 st
->st_dev
= file
->srv
->dev
;
2259 * Routine to open a directory
2260 * We accept the URL syntax explained in smbc_parse_path(), above.
2264 smbc_remove_dir(SMBCFILE
*dir
)
2266 struct smbc_dir_list
*d
,*f
;
2273 SAFE_FREE(f
->dirent
);
2278 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
2283 add_dirent(SMBCFILE
*dir
,
2285 const char *comment
,
2288 struct smbc_dirent
*dirent
;
2290 int name_length
= (name
== NULL
? 0 : strlen(name
));
2291 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
2294 * Allocate space for the dirent, which must be increased by the
2295 * size of the name and the comment and 1 each for the null terminator.
2298 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
2300 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
2304 dir
->dir_error
= ENOMEM
;
2309 ZERO_STRUCTP(dirent
);
2311 if (dir
->dir_list
== NULL
) {
2313 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
2314 if (!dir
->dir_list
) {
2317 dir
->dir_error
= ENOMEM
;
2321 ZERO_STRUCTP(dir
->dir_list
);
2323 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
2327 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
2329 if (!dir
->dir_end
->next
) {
2332 dir
->dir_error
= ENOMEM
;
2336 ZERO_STRUCTP(dir
->dir_end
->next
);
2338 dir
->dir_end
= dir
->dir_end
->next
;
2341 dir
->dir_end
->next
= NULL
;
2342 dir
->dir_end
->dirent
= dirent
;
2344 dirent
->smbc_type
= type
;
2345 dirent
->namelen
= name_length
;
2346 dirent
->commentlen
= comment_len
;
2347 dirent
->dirlen
= size
;
2350 * dirent->namelen + 1 includes the null (no null termination needed)
2351 * Ditto for dirent->commentlen.
2352 * The space for the two null bytes was allocated.
2354 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
2355 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
2356 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
2363 list_unique_wg_fn(const char *name
,
2365 const char *comment
,
2368 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2369 struct smbc_dir_list
*dir_list
;
2370 struct smbc_dirent
*dirent
;
2374 dirent_type
= dir
->dir_type
;
2376 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2378 /* An error occurred, what do we do? */
2379 /* FIXME: Add some code here */
2382 /* Point to the one just added */
2383 dirent
= dir
->dir_end
->dirent
;
2385 /* See if this was a duplicate */
2386 for (dir_list
= dir
->dir_list
;
2387 dir_list
!= dir
->dir_end
;
2388 dir_list
= dir_list
->next
) {
2390 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
2391 /* Duplicate. End end of list need to be removed. */
2395 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
2396 /* Found the end of the list. Remove it. */
2397 dir
->dir_end
= dir_list
;
2398 free(dir_list
->next
);
2400 dir_list
->next
= NULL
;
2407 list_fn(const char *name
,
2409 const char *comment
,
2412 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2416 * We need to process the type a little ...
2418 * Disk share = 0x00000000
2419 * Print share = 0x00000001
2420 * Comms share = 0x00000002 (obsolete?)
2421 * IPC$ share = 0x00000003
2423 * administrative shares:
2424 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2427 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
2430 case 0 | 0x80000000:
2432 dirent_type
= SMBC_FILE_SHARE
;
2436 dirent_type
= SMBC_PRINTER_SHARE
;
2440 dirent_type
= SMBC_COMMS_SHARE
;
2443 case 3 | 0x80000000:
2445 dirent_type
= SMBC_IPC_SHARE
;
2449 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
2454 dirent_type
= dir
->dir_type
;
2457 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2459 /* An error occurred, what do we do? */
2460 /* FIXME: Add some code here */
2466 dir_list_fn(const char *mnt
,
2472 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
2473 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
2475 /* Handle an error ... */
2477 /* FIXME: Add some code ... */
2484 net_share_enum_rpc(struct cli_state
*cli
,
2485 void (*fn
)(const char *name
,
2487 const char *comment
,
2494 uint32 info_level
= 1;
2495 uint32 preferred_len
= 0xffffffff;
2496 struct srvsvc_NetShareCtr1 ctr1
;
2497 union srvsvc_NetShareCtr ctr
;
2499 struct rpc_pipe_client
*pipe_hnd
;
2503 /* Open the server service pipe */
2504 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SRVSVC
, &nt_status
);
2506 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2510 /* Allocate a context for parsing and for the entries in "ctr" */
2511 mem_ctx
= talloc_init("libsmbclient: net_share_enum_rpc");
2512 if (mem_ctx
== NULL
) {
2513 DEBUG(0, ("out of memory for net_share_enum_rpc!\n"));
2514 cli_rpc_pipe_close(pipe_hnd
);
2521 /* Issue the NetShareEnum RPC call and retrieve the response */
2523 result
= rpccli_srvsvc_NetShareEnum(pipe_hnd
, mem_ctx
, NULL
,
2524 &info_level
, &ctr
, preferred_len
,
2525 &numentries
, &enum_hnd
);
2527 /* Was it successful? */
2528 if (!NT_STATUS_IS_OK(result
) || numentries
== 0) {
2529 /* Nope. Go clean up. */
2533 /* For each returned entry... */
2534 for (i
= 0; i
< numentries
; i
++) {
2536 /* Add this share to the list */
2537 (*fn
)(ctr
.ctr1
->array
[i
].name
,
2538 ctr
.ctr1
->array
[i
].type
,
2539 ctr
.ctr1
->array
[i
].comment
, state
);
2543 /* Close the server service pipe */
2544 cli_rpc_pipe_close(pipe_hnd
);
2546 /* Free all memory which was allocated for this request */
2547 TALLOC_FREE(mem_ctx
);
2549 /* Tell 'em if it worked */
2550 return NT_STATUS_IS_OK(result
) ? 0 : -1;
2556 smbc_opendir_ctx(SMBCCTX
*context
,
2560 fstring server
, share
, user
, password
, options
;
2565 SMBCSRV
*srv
= NULL
;
2566 SMBCFILE
*dir
= NULL
;
2567 struct _smbc_callbacks
*cb
;
2568 struct in_addr rem_ip
;
2570 if (!context
|| !context
->internal
||
2571 !context
->internal
->_initialized
) {
2572 DEBUG(4, ("no valid context\n"));
2573 errno
= EINVAL
+ 8192;
2579 DEBUG(4, ("no valid fname\n"));
2580 errno
= EINVAL
+ 8193;
2584 if (smbc_parse_path(context
, fname
,
2585 workgroup
, sizeof(workgroup
),
2586 server
, sizeof(server
),
2587 share
, sizeof(share
),
2590 password
, sizeof(password
),
2591 options
, sizeof(options
))) {
2592 DEBUG(4, ("no valid path\n"));
2593 errno
= EINVAL
+ 8194;
2597 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2598 "path='%s' options='%s'\n",
2599 fname
, server
, share
, path
, options
));
2601 /* Ensure the options are valid */
2602 if (smbc_check_options(server
, share
, path
, options
)) {
2603 DEBUG(4, ("unacceptable options (%s)\n", options
));
2604 errno
= EINVAL
+ 8195;
2608 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
2610 dir
= SMB_MALLOC_P(SMBCFILE
);
2622 dir
->fname
= SMB_STRDUP(fname
);
2626 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
2628 if (server
[0] == (char)0) {
2633 struct ip_service
*ip_list
;
2634 struct ip_service server_addr
;
2635 struct user_auth_info u_info
;
2636 struct cli_state
*cli
;
2638 if (share
[0] != (char)0 || path
[0] != (char)0) {
2640 errno
= EINVAL
+ 8196;
2642 SAFE_FREE(dir
->fname
);
2648 /* Determine how many local master browsers to query */
2649 max_lmb_count
= (context
->options
.browse_max_lmb_count
== 0
2651 : context
->options
.browse_max_lmb_count
);
2653 pstrcpy(u_info
.username
, user
);
2654 pstrcpy(u_info
.password
, password
);
2657 * We have server and share and path empty but options
2658 * requesting that we scan all master browsers for their list
2659 * of workgroups/domains. This implies that we must first try
2660 * broadcast queries to find all master browsers, and if that
2661 * doesn't work, then try our other methods which return only
2662 * a single master browser.
2666 if (!name_resolve_bcast(MSBROWSE
, 1, &ip_list
, &count
)) {
2670 if (!find_master_ip(workgroup
, &server_addr
.ip
)) {
2673 SAFE_FREE(dir
->fname
);
2680 ip_list
= &server_addr
;
2684 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
2685 DEBUG(99, ("Found master browser %d of %d: %s\n",
2686 i
+1, MAX(count
, max_lmb_count
),
2687 inet_ntoa(ip_list
[i
].ip
)));
2689 cli
= get_ipc_connect_master_ip(&ip_list
[i
],
2690 workgroup
, &u_info
);
2691 /* cli == NULL is the master browser refused to talk or
2692 could not be found */
2696 fstrcpy(server
, cli
->desthost
);
2699 DEBUG(4, ("using workgroup %s %s\n",
2700 workgroup
, server
));
2703 * For each returned master browser IP address, get a
2704 * connection to IPC$ on the server if we do not
2705 * already have one, and determine the
2706 * workgroups/domains that it knows about.
2709 srv
= smbc_server(context
, True
, server
, "IPC$",
2710 workgroup
, user
, password
);
2716 dir
->dir_type
= SMBC_WORKGROUP
;
2718 /* Now, list the stuff ... */
2720 if (!cli_NetServerEnum(srv
->cli
,
2722 SV_TYPE_DOMAIN_ENUM
,
2732 * Server not an empty string ... Check the rest and see what
2735 if (*share
== '\0') {
2736 if (*path
!= '\0') {
2738 /* Should not have empty share with path */
2739 errno
= EINVAL
+ 8197;
2741 SAFE_FREE(dir
->fname
);
2749 * We don't know if <server> is really a server name
2750 * or is a workgroup/domain name. If we already have
2751 * a server structure for it, we'll use it.
2752 * Otherwise, check to see if <server><1D>,
2753 * <server><1B>, or <server><20> translates. We check
2754 * to see if <server> is an IP address first.
2758 * See if we have an existing server. Do not
2759 * establish a connection if one does not already
2762 srv
= smbc_server(context
, False
, server
, "IPC$",
2763 workgroup
, user
, password
);
2766 * If no existing server and not an IP addr, look for
2770 !is_ipaddress(server
) &&
2771 (resolve_name(server
, &rem_ip
, 0x1d) || /* LMB */
2772 resolve_name(server
, &rem_ip
, 0x1b) )) { /* DMB */
2776 dir
->dir_type
= SMBC_SERVER
;
2779 * Get the backup list ...
2781 if (!name_status_find(server
, 0, 0,
2782 rem_ip
, buserver
)) {
2784 DEBUG(0, ("Could not get name of "
2785 "local/domain master browser "
2786 "for server %s\n", server
));
2788 SAFE_FREE(dir
->fname
);
2797 * Get a connection to IPC$ on the server if
2798 * we do not already have one
2800 srv
= smbc_server(context
, True
,
2802 workgroup
, user
, password
);
2804 DEBUG(0, ("got no contact to IPC$\n"));
2806 SAFE_FREE(dir
->fname
);
2815 /* Now, list the servers ... */
2816 if (!cli_NetServerEnum(srv
->cli
, server
,
2817 0x0000FFFE, list_fn
,
2821 SAFE_FREE(dir
->fname
);
2827 (resolve_name(server
, &rem_ip
, 0x20))) {
2829 /* If we hadn't found the server, get one now */
2831 srv
= smbc_server(context
, True
,
2839 SAFE_FREE(dir
->fname
);
2846 dir
->dir_type
= SMBC_FILE_SHARE
;
2849 /* List the shares ... */
2851 if (net_share_enum_rpc(
2854 (void *) dir
) < 0 &&
2860 errno
= cli_errno(srv
->cli
);
2862 SAFE_FREE(dir
->fname
);
2869 /* Neither the workgroup nor server exists */
2870 errno
= ECONNREFUSED
;
2872 SAFE_FREE(dir
->fname
);
2881 * The server and share are specified ... work from
2885 struct cli_state
*targetcli
;
2887 /* We connect to the server and list the directory */
2888 dir
->dir_type
= SMBC_FILE_SHARE
;
2890 srv
= smbc_server(context
, True
, server
, share
,
2891 workgroup
, user
, password
);
2896 SAFE_FREE(dir
->fname
);
2905 /* Now, list the files ... */
2907 p
= path
+ strlen(path
);
2908 pstrcat(path
, "\\*");
2910 if (!cli_resolve_path("", srv
->cli
, path
,
2911 &targetcli
, targetpath
))
2913 d_printf("Could not resolve %s\n", path
);
2915 SAFE_FREE(dir
->fname
);
2921 if (cli_list(targetcli
, targetpath
,
2922 aDIR
| aSYSTEM
| aHIDDEN
,
2923 dir_list_fn
, (void *)dir
) < 0) {
2926 SAFE_FREE(dir
->fname
);
2929 saved_errno
= smbc_errno(context
, targetcli
);
2931 if (saved_errno
== EINVAL
) {
2933 * See if they asked to opendir something
2934 * other than a directory. If so, the
2935 * converted error value we got would have
2936 * been EINVAL rather than ENOTDIR.
2938 *p
= '\0'; /* restore original path */
2940 if (smbc_getatr(context
, srv
, path
,
2942 NULL
, NULL
, NULL
, NULL
,
2944 ! IS_DOS_DIR(mode
)) {
2946 /* It is. Correct the error value */
2947 saved_errno
= ENOTDIR
;
2952 * If there was an error and the server is no
2955 cb
= &context
->callbacks
;
2956 if (cli_is_error(targetcli
) &&
2957 cb
->check_server_fn(context
, srv
)) {
2959 /* ... then remove it. */
2960 if (cb
->remove_unused_server_fn(context
,
2963 * We could not remove the server
2964 * completely, remove it from the
2965 * cache so we will not get it
2966 * again. It will be removed when the
2967 * last file/dir is closed.
2969 cb
->remove_cached_srv_fn(context
, srv
);
2973 errno
= saved_errno
;
2980 DLIST_ADD(context
->internal
->_files
, dir
);
2986 * Routine to close a directory
2990 smbc_closedir_ctx(SMBCCTX
*context
,
2994 if (!context
|| !context
->internal
||
2995 !context
->internal
->_initialized
) {
3002 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3009 smbc_remove_dir(dir
); /* Clean it up */
3011 DLIST_REMOVE(context
->internal
->_files
, dir
);
3015 SAFE_FREE(dir
->fname
);
3016 SAFE_FREE(dir
); /* Free the space too */
3024 smbc_readdir_internal(SMBCCTX
* context
,
3025 struct smbc_dirent
*dest
,
3026 struct smbc_dirent
*src
,
3027 int max_namebuf_len
)
3029 if (context
->options
.urlencode_readdir_entries
) {
3031 /* url-encode the name. get back remaining buffer space */
3033 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
3035 /* We now know the name length */
3036 dest
->namelen
= strlen(dest
->name
);
3038 /* Save the pointer to the beginning of the comment */
3039 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
3041 /* Copy the comment */
3042 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
3043 dest
->comment
[max_namebuf_len
- 1] = '\0';
3045 /* Save other fields */
3046 dest
->smbc_type
= src
->smbc_type
;
3047 dest
->commentlen
= strlen(dest
->comment
);
3048 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
3052 /* No encoding. Just copy the entry as is. */
3053 memcpy(dest
, src
, src
->dirlen
);
3054 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
3060 * Routine to get a directory entry
3063 struct smbc_dirent
*
3064 smbc_readdir_ctx(SMBCCTX
*context
,
3068 struct smbc_dirent
*dirp
, *dirent
;
3070 /* Check that all is ok first ... */
3072 if (!context
|| !context
->internal
||
3073 !context
->internal
->_initialized
) {
3076 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3081 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3084 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3089 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3092 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3097 if (!dir
->dir_next
) {
3101 dirent
= dir
->dir_next
->dirent
;
3109 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
3110 maxlen
= (sizeof(context
->internal
->_dirent
) -
3111 sizeof(struct smbc_dirent
));
3113 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
3115 dir
->dir_next
= dir
->dir_next
->next
;
3121 * Routine to get directory entries
3125 smbc_getdents_ctx(SMBCCTX
*context
,
3127 struct smbc_dirent
*dirp
,
3133 char *ndir
= (char *)dirp
;
3134 struct smbc_dir_list
*dirlist
;
3136 /* Check that all is ok first ... */
3138 if (!context
|| !context
->internal
||
3139 !context
->internal
->_initialized
) {
3146 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3153 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3161 * Now, retrieve the number of entries that will fit in what was passed
3162 * We have to figure out if the info is in the list, or we need to
3163 * send a request to the server to get the info.
3166 while ((dirlist
= dir
->dir_next
)) {
3167 struct smbc_dirent
*dirent
;
3169 if (!dirlist
->dirent
) {
3171 errno
= ENOENT
; /* Bad error */
3176 /* Do urlencoding of next entry, if so selected */
3177 dirent
= (struct smbc_dirent
*)context
->internal
->_dirent
;
3178 maxlen
= (sizeof(context
->internal
->_dirent
) -
3179 sizeof(struct smbc_dirent
));
3180 smbc_readdir_internal(context
, dirent
, dirlist
->dirent
, maxlen
);
3182 reqd
= dirent
->dirlen
;
3186 if (rem
< count
) { /* We managed to copy something */
3192 else { /* Nothing copied ... */
3194 errno
= EINVAL
; /* Not enough space ... */
3201 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
3203 ((struct smbc_dirent
*)ndir
)->comment
=
3204 (char *)(&((struct smbc_dirent
*)ndir
)->name
+
3212 dir
->dir_next
= dirlist
= dirlist
-> next
;
3223 * Routine to create a directory ...
3227 smbc_mkdir_ctx(SMBCCTX
*context
,
3237 pstring path
, targetpath
;
3238 struct cli_state
*targetcli
;
3240 if (!context
|| !context
->internal
||
3241 !context
->internal
->_initialized
) {
3255 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
3257 if (smbc_parse_path(context
, fname
,
3258 workgroup
, sizeof(workgroup
),
3259 server
, sizeof(server
),
3260 share
, sizeof(share
),
3263 password
, sizeof(password
),
3269 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3271 srv
= smbc_server(context
, True
,
3272 server
, share
, workgroup
, user
, password
);
3276 return -1; /* errno set by smbc_server */
3280 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3281 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
3283 d_printf("Could not resolve %s\n", path
);
3286 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3288 if (!cli_mkdir(targetcli
, targetpath
)) {
3290 errno
= smbc_errno(context
, targetcli
);
3300 * Our list function simply checks to see if a directory is not empty
3303 static int smbc_rmdir_dirempty
= True
;
3306 rmdir_list_fn(const char *mnt
,
3311 if (strncmp(finfo
->name
, ".", 1) != 0 &&
3312 strncmp(finfo
->name
, "..", 2) != 0) {
3314 smbc_rmdir_dirempty
= False
;
3319 * Routine to remove a directory
3323 smbc_rmdir_ctx(SMBCCTX
*context
,
3334 struct cli_state
*targetcli
;
3336 if (!context
|| !context
->internal
||
3337 !context
->internal
->_initialized
) {
3351 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
3353 if (smbc_parse_path(context
, fname
,
3354 workgroup
, sizeof(workgroup
),
3355 server
, sizeof(server
),
3356 share
, sizeof(share
),
3359 password
, sizeof(password
),
3366 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3368 srv
= smbc_server(context
, True
,
3369 server
, share
, workgroup
, user
, password
);
3373 return -1; /* errno set by smbc_server */
3377 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3378 if (!cli_resolve_path( "", srv
->cli
, path
, &targetcli
, targetpath
))
3380 d_printf("Could not resolve %s\n", path
);
3383 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3386 if (!cli_rmdir(targetcli
, targetpath
)) {
3388 errno
= smbc_errno(context
, targetcli
);
3390 if (errno
== EACCES
) { /* Check if the dir empty or not */
3392 /* Local storage to avoid buffer overflows */
3395 smbc_rmdir_dirempty
= True
; /* Make this so ... */
3397 pstrcpy(lpath
, targetpath
);
3398 pstrcat(lpath
, "\\*");
3400 if (cli_list(targetcli
, lpath
,
3401 aDIR
| aSYSTEM
| aHIDDEN
,
3402 rmdir_list_fn
, NULL
) < 0) {
3404 /* Fix errno to ignore latest error ... */
3405 DEBUG(5, ("smbc_rmdir: "
3406 "cli_list returned an error: %d\n",
3407 smbc_errno(context
, targetcli
)));
3412 if (smbc_rmdir_dirempty
)
3428 * Routine to return the current directory position
3432 smbc_telldir_ctx(SMBCCTX
*context
,
3435 off_t ret_val
; /* Squash warnings about cast */
3437 if (!context
|| !context
->internal
||
3438 !context
->internal
->_initialized
) {
3445 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3452 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3460 * We return the pointer here as the offset
3462 ret_val
= (off_t
)(long)dir
->dir_next
;
3468 * A routine to run down the list and see if the entry is OK
3471 struct smbc_dir_list
*
3472 smbc_check_dir_ent(struct smbc_dir_list
*list
,
3473 struct smbc_dirent
*dirent
)
3476 /* Run down the list looking for what we want */
3480 struct smbc_dir_list
*tmp
= list
;
3484 if (tmp
->dirent
== dirent
)
3493 return NULL
; /* Not found, or an error */
3499 * Routine to seek on a directory
3503 smbc_lseekdir_ctx(SMBCCTX
*context
,
3507 long int l_offset
= offset
; /* Handle problems of size */
3508 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
3509 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
3511 if (!context
|| !context
->internal
||
3512 !context
->internal
->_initialized
) {
3519 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3526 /* Now, check what we were passed and see if it is OK ... */
3528 if (dirent
== NULL
) { /* Seek to the begining of the list */
3530 dir
->dir_next
= dir
->dir_list
;
3535 /* Now, run down the list and make sure that the entry is OK */
3536 /* This may need to be changed if we change the format of the list */
3538 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
3540 errno
= EINVAL
; /* Bad entry */
3545 dir
->dir_next
= list_ent
;
3552 * Routine to fstat a dir
3556 smbc_fstatdir_ctx(SMBCCTX
*context
,
3561 if (!context
|| !context
->internal
||
3562 !context
->internal
->_initialized
) {
3569 /* No code yet ... */
3576 smbc_chmod_ctx(SMBCCTX
*context
,
3589 if (!context
|| !context
->internal
||
3590 !context
->internal
->_initialized
) {
3592 errno
= EINVAL
; /* Best I can think of ... */
3604 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, newmode
));
3606 if (smbc_parse_path(context
, fname
,
3607 workgroup
, sizeof(workgroup
),
3608 server
, sizeof(server
),
3609 share
, sizeof(share
),
3612 password
, sizeof(password
),
3618 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3620 srv
= smbc_server(context
, True
,
3621 server
, share
, workgroup
, user
, password
);
3624 return -1; /* errno set by smbc_server */
3629 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
3630 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
3631 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
3632 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
3634 if (!cli_setatr(srv
->cli
, path
, mode
, 0)) {
3635 errno
= smbc_errno(context
, srv
->cli
);
3643 smbc_utimes_ctx(SMBCCTX
*context
,
3645 struct timeval
*tbuf
)
3657 if (!context
|| !context
->internal
||
3658 !context
->internal
->_initialized
) {
3660 errno
= EINVAL
; /* Best I can think of ... */
3673 access_time
= write_time
= time(NULL
);
3675 access_time
= tbuf
[0].tv_sec
;
3676 write_time
= tbuf
[1].tv_sec
;
3685 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
3686 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
3687 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
3691 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
3692 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
3693 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
3697 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
3698 fname
, atimebuf
, mtimebuf
);
3701 if (smbc_parse_path(context
, fname
,
3702 workgroup
, sizeof(workgroup
),
3703 server
, sizeof(server
),
3704 share
, sizeof(share
),
3707 password
, sizeof(password
),
3713 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
3715 srv
= smbc_server(context
, True
,
3716 server
, share
, workgroup
, user
, password
);
3719 return -1; /* errno set by smbc_server */
3722 if (!smbc_setatr(context
, srv
, path
,
3723 0, access_time
, write_time
, 0, 0)) {
3724 return -1; /* errno set by smbc_setatr */
3731 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
3732 However NT4 gives a "The information may have been modified by a
3733 computer running Windows NT 5.0" if denied ACEs do not appear before
3737 ace_compare(SEC_ACE
*ace1
,
3740 if (sec_ace_equal(ace1
, ace2
))
3743 if (ace1
->type
!= ace2
->type
)
3744 return ace2
->type
- ace1
->type
;
3746 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
3747 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
3749 if (ace1
->flags
!= ace2
->flags
)
3750 return ace1
->flags
- ace2
->flags
;
3752 if (ace1
->access_mask
!= ace2
->access_mask
)
3753 return ace1
->access_mask
- ace2
->access_mask
;
3755 if (ace1
->size
!= ace2
->size
)
3756 return ace1
->size
- ace2
->size
;
3758 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
3763 sort_acl(SEC_ACL
*the_acl
)
3766 if (!the_acl
) return;
3768 qsort(the_acl
->aces
, the_acl
->num_aces
, sizeof(the_acl
->aces
[0]),
3769 QSORT_CAST ace_compare
);
3771 for (i
=1;i
<the_acl
->num_aces
;) {
3772 if (sec_ace_equal(&the_acl
->aces
[i
-1], &the_acl
->aces
[i
])) {
3774 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
3775 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
3777 the_acl
->num_aces
--;
3784 /* convert a SID to a string, either numeric or username/group */
3786 convert_sid_to_string(struct cli_state
*ipc_cli
,
3792 char **domains
= NULL
;
3793 char **names
= NULL
;
3794 enum lsa_SidType
*types
= NULL
;
3795 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
3796 sid_to_string(str
, sid
);
3799 return; /* no lookup desired */
3806 /* Ask LSA to convert the sid to a name */
3808 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd
, ipc_cli
->mem_ctx
,
3809 pol
, 1, sid
, &domains
,
3811 !domains
|| !domains
[0] || !names
|| !names
[0]) {
3817 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
3818 domains
[0], lp_winbind_separator(),
3822 /* convert a string to a SID, either numeric or username/group */
3824 convert_string_to_sid(struct cli_state
*ipc_cli
,
3830 enum lsa_SidType
*types
= NULL
;
3831 DOM_SID
*sids
= NULL
;
3833 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
3840 if (strncmp(str
, "S-", 2) == 0) {
3841 return string_to_sid(sid
, str
);
3848 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd
, ipc_cli
->mem_ctx
,
3849 pol
, 1, &str
, NULL
, &sids
,
3855 sid_copy(sid
, &sids
[0]);
3862 /* parse an ACE in the same format as print_ace() */
3864 parse_ace(struct cli_state
*ipc_cli
,
3874 unsigned int aflags
;
3878 const struct perm_value
*v
;
3884 /* These values discovered by inspection */
3885 static const struct perm_value special_values
[] = {
3886 { "R", 0x00120089 },
3887 { "W", 0x00120116 },
3888 { "X", 0x001200a0 },
3889 { "D", 0x00010000 },
3890 { "P", 0x00040000 },
3891 { "O", 0x00080000 },
3895 static const struct perm_value standard_values
[] = {
3896 { "READ", 0x001200a9 },
3897 { "CHANGE", 0x001301bf },
3898 { "FULL", 0x001f01ff },
3904 p
= strchr_m(str
,':');
3905 if (!p
) return False
;
3908 /* Try to parse numeric form */
3910 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
3911 convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3915 /* Try to parse text form */
3917 if (!convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
3922 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3926 if (StrnCaseCmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
3927 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
3928 } else if (StrnCaseCmp(tok
, "DENIED", strlen("DENIED")) == 0) {
3929 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
3934 /* Only numeric form accepted for flags at present */
3936 if (!(next_token(&cp
, tok
, "/", sizeof(fstring
)) &&
3937 sscanf(tok
, "%i", &aflags
))) {
3941 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
3945 if (strncmp(tok
, "0x", 2) == 0) {
3946 if (sscanf(tok
, "%i", &amask
) != 1) {
3952 for (v
= standard_values
; v
->perm
; v
++) {
3953 if (strcmp(tok
, v
->perm
) == 0) {
3964 for (v
= special_values
; v
->perm
; v
++) {
3965 if (v
->perm
[0] == *p
) {
3971 if (!found
) return False
;
3981 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
3985 /* add an ACE to a list of ACEs in a SEC_ACL */
3987 add_ace(SEC_ACL
**the_acl
,
3995 (*the_acl
) = make_sec_acl(ctx
, 3, 1, ace
);
3999 if ((aces
= SMB_CALLOC_ARRAY(SEC_ACE
, 1+(*the_acl
)->num_aces
)) == NULL
) {
4002 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
4003 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
4004 newacl
= make_sec_acl(ctx
, (*the_acl
)->revision
,
4005 1+(*the_acl
)->num_aces
, aces
);
4007 (*the_acl
) = newacl
;
4012 /* parse a ascii version of a security descriptor */
4014 sec_desc_parse(TALLOC_CTX
*ctx
,
4015 struct cli_state
*ipc_cli
,
4020 const char *p
= str
;
4022 SEC_DESC
*ret
= NULL
;
4024 DOM_SID
*grp_sid
=NULL
;
4025 DOM_SID
*owner_sid
=NULL
;
4029 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
4031 if (StrnCaseCmp(tok
,"REVISION:", 9) == 0) {
4032 revision
= strtol(tok
+9, NULL
, 16);
4036 if (StrnCaseCmp(tok
,"OWNER:", 6) == 0) {
4038 DEBUG(5, ("OWNER specified more than once!\n"));
4041 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4043 !convert_string_to_sid(ipc_cli
, pol
,
4045 owner_sid
, tok
+6)) {
4046 DEBUG(5, ("Failed to parse owner sid\n"));
4052 if (StrnCaseCmp(tok
,"OWNER+:", 7) == 0) {
4054 DEBUG(5, ("OWNER specified more than once!\n"));
4057 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4059 !convert_string_to_sid(ipc_cli
, pol
,
4061 owner_sid
, tok
+7)) {
4062 DEBUG(5, ("Failed to parse owner sid\n"));
4068 if (StrnCaseCmp(tok
,"GROUP:", 6) == 0) {
4070 DEBUG(5, ("GROUP specified more than once!\n"));
4073 grp_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4075 !convert_string_to_sid(ipc_cli
, pol
,
4078 DEBUG(5, ("Failed to parse group sid\n"));
4084 if (StrnCaseCmp(tok
,"GROUP+:", 7) == 0) {
4086 DEBUG(5, ("GROUP specified more than once!\n"));
4089 grp_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4091 !convert_string_to_sid(ipc_cli
, pol
,
4094 DEBUG(5, ("Failed to parse group sid\n"));
4100 if (StrnCaseCmp(tok
,"ACL:", 4) == 0) {
4102 if (!parse_ace(ipc_cli
, pol
, &ace
, numeric
, tok
+4)) {
4103 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
4106 if(!add_ace(&dacl
, &ace
, ctx
)) {
4107 DEBUG(5, ("Failed to add ACL %s\n", tok
));
4113 if (StrnCaseCmp(tok
,"ACL+:", 5) == 0) {
4115 if (!parse_ace(ipc_cli
, pol
, &ace
, False
, tok
+5)) {
4116 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
4119 if(!add_ace(&dacl
, &ace
, ctx
)) {
4120 DEBUG(5, ("Failed to add ACL %s\n", tok
));
4126 DEBUG(5, ("Failed to parse security descriptor\n"));
4130 ret
= make_sec_desc(ctx
, revision
, SEC_DESC_SELF_RELATIVE
,
4131 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
4135 SAFE_FREE(owner_sid
);
4141 /* Obtain the current dos attributes */
4142 static DOS_ATTR_DESC
*
4143 dos_attr_query(SMBCCTX
*context
,
4145 const char *filename
,
4148 struct timespec create_time_ts
;
4149 struct timespec write_time_ts
;
4150 struct timespec access_time_ts
;
4151 struct timespec change_time_ts
;
4154 SMB_INO_T inode
= 0;
4157 ret
= TALLOC_P(ctx
, DOS_ATTR_DESC
);
4163 /* Obtain the DOS attributes */
4164 if (!smbc_getatr(context
, srv
, CONST_DISCARD(char *, filename
),
4172 errno
= smbc_errno(context
, srv
->cli
);
4173 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4180 ret
->create_time
= convert_timespec_to_time_t(create_time_ts
);
4181 ret
->access_time
= convert_timespec_to_time_t(access_time_ts
);
4182 ret
->write_time
= convert_timespec_to_time_t(write_time_ts
);
4183 ret
->change_time
= convert_timespec_to_time_t(change_time_ts
);
4190 /* parse a ascii version of a security descriptor */
4192 dos_attr_parse(SMBCCTX
*context
,
4198 const char *p
= str
;
4201 const char * create_time_attr
;
4202 const char * access_time_attr
;
4203 const char * write_time_attr
;
4204 const char * change_time_attr
;
4207 /* Determine whether to use old-style or new-style attribute names */
4208 if (context
->internal
->_full_time_names
) {
4209 /* new-style names */
4210 attr_strings
.create_time_attr
= "CREATE_TIME";
4211 attr_strings
.access_time_attr
= "ACCESS_TIME";
4212 attr_strings
.write_time_attr
= "WRITE_TIME";
4213 attr_strings
.change_time_attr
= "CHANGE_TIME";
4215 /* old-style names */
4216 attr_strings
.create_time_attr
= NULL
;
4217 attr_strings
.access_time_attr
= "A_TIME";
4218 attr_strings
.write_time_attr
= "M_TIME";
4219 attr_strings
.change_time_attr
= "C_TIME";
4222 /* if this is to set the entire ACL... */
4224 /* ... then increment past the first colon if there is one */
4225 if ((p
= strchr(str
, ':')) != NULL
) {
4232 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
4234 if (StrnCaseCmp(tok
, "MODE:", 5) == 0) {
4235 dad
->mode
= strtol(tok
+5, NULL
, 16);
4239 if (StrnCaseCmp(tok
, "SIZE:", 5) == 0) {
4240 dad
->size
= (SMB_OFF_T
)atof(tok
+5);
4244 n
= strlen(attr_strings
.access_time_attr
);
4245 if (StrnCaseCmp(tok
, attr_strings
.access_time_attr
, n
) == 0) {
4246 dad
->access_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4250 n
= strlen(attr_strings
.change_time_attr
);
4251 if (StrnCaseCmp(tok
, attr_strings
.change_time_attr
, n
) == 0) {
4252 dad
->change_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4256 n
= strlen(attr_strings
.write_time_attr
);
4257 if (StrnCaseCmp(tok
, attr_strings
.write_time_attr
, n
) == 0) {
4258 dad
->write_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4262 if (attr_strings
.create_time_attr
!= NULL
) {
4263 n
= strlen(attr_strings
.create_time_attr
);
4264 if (StrnCaseCmp(tok
, attr_strings
.create_time_attr
,
4266 dad
->create_time
= (time_t)strtol(tok
+n
+1,
4272 if (StrnCaseCmp(tok
, "INODE:", 6) == 0) {
4273 dad
->inode
= (SMB_INO_T
)atof(tok
+6);
4279 /*****************************************************
4280 Retrieve the acls for a file.
4281 *******************************************************/
4284 cacl_get(SMBCCTX
*context
,
4287 struct cli_state
*ipc_cli
,
4303 BOOL exclude_nt_revision
= False
;
4304 BOOL exclude_nt_owner
= False
;
4305 BOOL exclude_nt_group
= False
;
4306 BOOL exclude_nt_acl
= False
;
4307 BOOL exclude_dos_mode
= False
;
4308 BOOL exclude_dos_size
= False
;
4309 BOOL exclude_dos_create_time
= False
;
4310 BOOL exclude_dos_access_time
= False
;
4311 BOOL exclude_dos_write_time
= False
;
4312 BOOL exclude_dos_change_time
= False
;
4313 BOOL exclude_dos_inode
= False
;
4314 BOOL numeric
= True
;
4315 BOOL determine_size
= (bufsize
== 0);
4319 fstring name_sandbox
;
4323 struct timespec create_time_ts
;
4324 struct timespec write_time_ts
;
4325 struct timespec access_time_ts
;
4326 struct timespec change_time_ts
;
4327 time_t create_time
= (time_t)0;
4328 time_t write_time
= (time_t)0;
4329 time_t access_time
= (time_t)0;
4330 time_t change_time
= (time_t)0;
4334 struct cli_state
*cli
= srv
->cli
;
4336 const char * create_time_attr
;
4337 const char * access_time_attr
;
4338 const char * write_time_attr
;
4339 const char * change_time_attr
;
4342 const char * create_time_attr
;
4343 const char * access_time_attr
;
4344 const char * write_time_attr
;
4345 const char * change_time_attr
;
4346 } excl_attr_strings
;
4348 /* Determine whether to use old-style or new-style attribute names */
4349 if (context
->internal
->_full_time_names
) {
4350 /* new-style names */
4351 attr_strings
.create_time_attr
= "CREATE_TIME";
4352 attr_strings
.access_time_attr
= "ACCESS_TIME";
4353 attr_strings
.write_time_attr
= "WRITE_TIME";
4354 attr_strings
.change_time_attr
= "CHANGE_TIME";
4356 excl_attr_strings
.create_time_attr
= "CREATE_TIME";
4357 excl_attr_strings
.access_time_attr
= "ACCESS_TIME";
4358 excl_attr_strings
.write_time_attr
= "WRITE_TIME";
4359 excl_attr_strings
.change_time_attr
= "CHANGE_TIME";
4361 /* old-style names */
4362 attr_strings
.create_time_attr
= NULL
;
4363 attr_strings
.access_time_attr
= "A_TIME";
4364 attr_strings
.write_time_attr
= "M_TIME";
4365 attr_strings
.change_time_attr
= "C_TIME";
4367 excl_attr_strings
.create_time_attr
= NULL
;
4368 excl_attr_strings
.access_time_attr
= "dos_attr.A_TIME";
4369 excl_attr_strings
.write_time_attr
= "dos_attr.M_TIME";
4370 excl_attr_strings
.change_time_attr
= "dos_attr.C_TIME";
4373 /* Copy name so we can strip off exclusions (if any are specified) */
4374 strncpy(name_sandbox
, attr_name
, sizeof(name_sandbox
) - 1);
4376 /* Ensure name is null terminated */
4377 name_sandbox
[sizeof(name_sandbox
) - 1] = '\0';
4379 /* Play in the sandbox */
4380 name
= name_sandbox
;
4382 /* If there are any exclusions, point to them and mask them from name */
4383 if ((pExclude
= strchr(name
, '!')) != NULL
)
4388 all
= (StrnCaseCmp(name
, "system.*", 8) == 0);
4389 all_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.*", 20) == 0);
4390 all_nt_acls
= (StrnCaseCmp(name
, "system.nt_sec_desc.acl.*", 24) == 0);
4391 all_dos
= (StrnCaseCmp(name
, "system.dos_attr.*", 17) == 0);
4392 some_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.", 19) == 0);
4393 some_dos
= (StrnCaseCmp(name
, "system.dos_attr.", 16) == 0);
4394 numeric
= (* (name
+ strlen(name
) - 1) != '+');
4396 /* Look for exclusions from "all" requests */
4397 if (all
|| all_nt
|| all_dos
) {
4399 /* Exclusions are delimited by '!' */
4402 pExclude
= (p
== NULL
? NULL
: p
+ 1)) {
4404 /* Find end of this exclusion name */
4405 if ((p
= strchr(pExclude
, '!')) != NULL
)
4410 /* Which exclusion name is this? */
4411 if (StrCaseCmp(pExclude
, "nt_sec_desc.revision") == 0) {
4412 exclude_nt_revision
= True
;
4414 else if (StrCaseCmp(pExclude
, "nt_sec_desc.owner") == 0) {
4415 exclude_nt_owner
= True
;
4417 else if (StrCaseCmp(pExclude
, "nt_sec_desc.group") == 0) {
4418 exclude_nt_group
= True
;
4420 else if (StrCaseCmp(pExclude
, "nt_sec_desc.acl") == 0) {
4421 exclude_nt_acl
= True
;
4423 else if (StrCaseCmp(pExclude
, "dos_attr.mode") == 0) {
4424 exclude_dos_mode
= True
;
4426 else if (StrCaseCmp(pExclude
, "dos_attr.size") == 0) {
4427 exclude_dos_size
= True
;
4429 else if (excl_attr_strings
.create_time_attr
!= NULL
&&
4430 StrCaseCmp(pExclude
,
4431 excl_attr_strings
.change_time_attr
) == 0) {
4432 exclude_dos_create_time
= True
;
4434 else if (StrCaseCmp(pExclude
,
4435 excl_attr_strings
.access_time_attr
) == 0) {
4436 exclude_dos_access_time
= True
;
4438 else if (StrCaseCmp(pExclude
,
4439 excl_attr_strings
.write_time_attr
) == 0) {
4440 exclude_dos_write_time
= True
;
4442 else if (StrCaseCmp(pExclude
,
4443 excl_attr_strings
.change_time_attr
) == 0) {
4444 exclude_dos_change_time
= True
;
4446 else if (StrCaseCmp(pExclude
, "dos_attr.inode") == 0) {
4447 exclude_dos_inode
= True
;
4450 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4461 * If we are (possibly) talking to an NT or new system and some NT
4462 * attributes have been requested...
4464 if (ipc_cli
&& (all
|| some_nt
|| all_nt_acls
)) {
4465 /* Point to the portion after "system.nt_sec_desc." */
4466 name
+= 19; /* if (all) this will be invalid but unused */
4468 /* ... then obtain any NT attributes which were requested */
4469 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
4472 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4473 filename
, cli_errstr(cli
)));
4478 sd
= cli_query_secdesc(cli
, fnum
, ctx
);
4482 ("cacl_get Failed to query old descriptor\n"));
4487 cli_close(cli
, fnum
);
4489 if (! exclude_nt_revision
) {
4490 if (all
|| all_nt
) {
4491 if (determine_size
) {
4492 p
= talloc_asprintf(ctx
,
4501 n
= snprintf(buf
, bufsize
,
4505 } else if (StrCaseCmp(name
, "revision") == 0) {
4506 if (determine_size
) {
4507 p
= talloc_asprintf(ctx
, "%d",
4515 n
= snprintf(buf
, bufsize
, "%d",
4520 if (!determine_size
&& n
> bufsize
) {
4529 if (! exclude_nt_owner
) {
4530 /* Get owner and group sid */
4531 if (sd
->owner_sid
) {
4532 convert_sid_to_string(ipc_cli
, pol
,
4537 fstrcpy(sidstr
, "");
4540 if (all
|| all_nt
) {
4541 if (determine_size
) {
4542 p
= talloc_asprintf(ctx
, ",OWNER:%s",
4549 } else if (sidstr
[0] != '\0') {
4550 n
= snprintf(buf
, bufsize
,
4551 ",OWNER:%s", sidstr
);
4553 } else if (StrnCaseCmp(name
, "owner", 5) == 0) {
4554 if (determine_size
) {
4555 p
= talloc_asprintf(ctx
, "%s", sidstr
);
4562 n
= snprintf(buf
, bufsize
, "%s",
4567 if (!determine_size
&& n
> bufsize
) {
4576 if (! exclude_nt_group
) {
4577 if (sd
->group_sid
) {
4578 convert_sid_to_string(ipc_cli
, pol
,
4582 fstrcpy(sidstr
, "");
4585 if (all
|| all_nt
) {
4586 if (determine_size
) {
4587 p
= talloc_asprintf(ctx
, ",GROUP:%s",
4594 } else if (sidstr
[0] != '\0') {
4595 n
= snprintf(buf
, bufsize
,
4596 ",GROUP:%s", sidstr
);
4598 } else if (StrnCaseCmp(name
, "group", 5) == 0) {
4599 if (determine_size
) {
4600 p
= talloc_asprintf(ctx
, "%s", sidstr
);
4607 n
= snprintf(buf
, bufsize
,
4612 if (!determine_size
&& n
> bufsize
) {
4621 if (! exclude_nt_acl
) {
4622 /* Add aces to value buffer */
4623 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
4625 SEC_ACE
*ace
= &sd
->dacl
->aces
[i
];
4626 convert_sid_to_string(ipc_cli
, pol
,
4630 if (all
|| all_nt
) {
4631 if (determine_size
) {
4632 p
= talloc_asprintf(
4648 ",ACL:%s:%d/%d/0x%08x",
4654 } else if ((StrnCaseCmp(name
, "acl", 3) == 0 &&
4655 StrCaseCmp(name
+3, sidstr
) == 0) ||
4656 (StrnCaseCmp(name
, "acl+", 4) == 0 &&
4657 StrCaseCmp(name
+4, sidstr
) == 0)) {
4658 if (determine_size
) {
4659 p
= talloc_asprintf(
4671 n
= snprintf(buf
, bufsize
,
4677 } else if (all_nt_acls
) {
4678 if (determine_size
) {
4679 p
= talloc_asprintf(
4681 "%s%s:%d/%d/0x%08x",
4693 n
= snprintf(buf
, bufsize
,
4694 "%s%s:%d/%d/0x%08x",
4702 if (!determine_size
&& n
> bufsize
) {
4712 /* Restore name pointer to its original value */
4716 if (all
|| some_dos
) {
4717 /* Point to the portion after "system.dos_attr." */
4718 name
+= 16; /* if (all) this will be invalid but unused */
4720 /* Obtain the DOS attributes */
4721 if (!smbc_getatr(context
, srv
, filename
, &mode
, &size
,
4728 errno
= smbc_errno(context
, srv
->cli
);
4733 create_time
= convert_timespec_to_time_t(create_time_ts
);
4734 access_time
= convert_timespec_to_time_t(access_time_ts
);
4735 write_time
= convert_timespec_to_time_t(write_time_ts
);
4736 change_time
= convert_timespec_to_time_t(change_time_ts
);
4738 if (! exclude_dos_mode
) {
4739 if (all
|| all_dos
) {
4740 if (determine_size
) {
4741 p
= talloc_asprintf(ctx
,
4754 n
= snprintf(buf
, bufsize
,
4762 } else if (StrCaseCmp(name
, "mode") == 0) {
4763 if (determine_size
) {
4764 p
= talloc_asprintf(ctx
, "0x%x", mode
);
4771 n
= snprintf(buf
, bufsize
,
4776 if (!determine_size
&& n
> bufsize
) {
4785 if (! exclude_dos_size
) {
4786 if (all
|| all_dos
) {
4787 if (determine_size
) {
4788 p
= talloc_asprintf(
4798 n
= snprintf(buf
, bufsize
,
4802 } else if (StrCaseCmp(name
, "size") == 0) {
4803 if (determine_size
) {
4804 p
= talloc_asprintf(
4814 n
= snprintf(buf
, bufsize
,
4820 if (!determine_size
&& n
> bufsize
) {
4829 if (! exclude_dos_create_time
&&
4830 attr_strings
.create_time_attr
!= NULL
) {
4831 if (all
|| all_dos
) {
4832 if (determine_size
) {
4833 p
= talloc_asprintf(ctx
,
4835 attr_strings
.create_time_attr
,
4843 n
= snprintf(buf
, bufsize
,
4845 attr_strings
.create_time_attr
,
4848 } else if (StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) {
4849 if (determine_size
) {
4850 p
= talloc_asprintf(ctx
, "%lu", create_time
);
4857 n
= snprintf(buf
, bufsize
,
4858 "%lu", create_time
);
4862 if (!determine_size
&& n
> bufsize
) {
4871 if (! exclude_dos_access_time
) {
4872 if (all
|| all_dos
) {
4873 if (determine_size
) {
4874 p
= talloc_asprintf(ctx
,
4876 attr_strings
.access_time_attr
,
4884 n
= snprintf(buf
, bufsize
,
4886 attr_strings
.access_time_attr
,
4889 } else if (StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0) {
4890 if (determine_size
) {
4891 p
= talloc_asprintf(ctx
, "%lu", access_time
);
4898 n
= snprintf(buf
, bufsize
,
4899 "%lu", access_time
);
4903 if (!determine_size
&& n
> bufsize
) {
4912 if (! exclude_dos_write_time
) {
4913 if (all
|| all_dos
) {
4914 if (determine_size
) {
4915 p
= talloc_asprintf(ctx
,
4917 attr_strings
.write_time_attr
,
4925 n
= snprintf(buf
, bufsize
,
4927 attr_strings
.write_time_attr
,
4930 } else if (StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0) {
4931 if (determine_size
) {
4932 p
= talloc_asprintf(ctx
, "%lu", write_time
);
4939 n
= snprintf(buf
, bufsize
,
4944 if (!determine_size
&& n
> bufsize
) {
4953 if (! exclude_dos_change_time
) {
4954 if (all
|| all_dos
) {
4955 if (determine_size
) {
4956 p
= talloc_asprintf(ctx
,
4958 attr_strings
.change_time_attr
,
4966 n
= snprintf(buf
, bufsize
,
4968 attr_strings
.change_time_attr
,
4971 } else if (StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0) {
4972 if (determine_size
) {
4973 p
= talloc_asprintf(ctx
, "%lu", change_time
);
4980 n
= snprintf(buf
, bufsize
,
4981 "%lu", change_time
);
4985 if (!determine_size
&& n
> bufsize
) {
4994 if (! exclude_dos_inode
) {
4995 if (all
|| all_dos
) {
4996 if (determine_size
) {
4997 p
= talloc_asprintf(
5007 n
= snprintf(buf
, bufsize
,
5011 } else if (StrCaseCmp(name
, "inode") == 0) {
5012 if (determine_size
) {
5013 p
= talloc_asprintf(
5023 n
= snprintf(buf
, bufsize
,
5029 if (!determine_size
&& n
> bufsize
) {
5038 /* Restore name pointer to its original value */
5051 /*****************************************************
5052 set the ACLs on a file given an ascii description
5053 *******************************************************/
5055 cacl_set(TALLOC_CTX
*ctx
,
5056 struct cli_state
*cli
,
5057 struct cli_state
*ipc_cli
,
5059 const char *filename
,
5060 const char *the_acl
,
5066 SEC_DESC
*sd
= NULL
, *old
;
5067 SEC_ACL
*dacl
= NULL
;
5068 DOM_SID
*owner_sid
= NULL
;
5069 DOM_SID
*grp_sid
= NULL
;
5074 BOOL numeric
= True
;
5076 /* the_acl will be null for REMOVE_ALL operations */
5078 numeric
= ((p
= strchr(the_acl
, ':')) != NULL
&&
5082 /* if this is to set the entire ACL... */
5083 if (*the_acl
== '*') {
5084 /* ... then increment past the first colon */
5088 sd
= sec_desc_parse(ctx
, ipc_cli
, pol
, numeric
,
5089 CONST_DISCARD(char *, the_acl
));
5097 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5098 that doesn't deref sd */
5100 if (!sd
&& (mode
!= SMBC_XATTR_MODE_REMOVE_ALL
)) {
5105 /* The desired access below is the only one I could find that works
5106 with NT4, W2KP and Samba */
5108 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
5111 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5112 filename
, cli_errstr(cli
)));
5117 old
= cli_query_secdesc(cli
, fnum
, ctx
);
5120 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5125 cli_close(cli
, fnum
);
5128 case SMBC_XATTR_MODE_REMOVE_ALL
:
5129 old
->dacl
->num_aces
= 0;
5130 SAFE_FREE(old
->dacl
->aces
);
5131 SAFE_FREE(old
->dacl
);
5136 case SMBC_XATTR_MODE_REMOVE
:
5137 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5140 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
5141 if (sec_ace_equal(&sd
->dacl
->aces
[i
],
5142 &old
->dacl
->aces
[j
])) {
5144 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
5145 old
->dacl
->aces
[k
] =
5146 old
->dacl
->aces
[k
+1];
5148 old
->dacl
->num_aces
--;
5149 if (old
->dacl
->num_aces
== 0) {
5150 SAFE_FREE(old
->dacl
->aces
);
5151 SAFE_FREE(old
->dacl
);
5168 case SMBC_XATTR_MODE_ADD
:
5169 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5172 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
5173 if (sid_equal(&sd
->dacl
->aces
[i
].trustee
,
5174 &old
->dacl
->aces
[j
].trustee
)) {
5175 if (!(flags
& SMBC_XATTR_FLAG_CREATE
)) {
5180 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
5186 if (!found
&& (flags
& SMBC_XATTR_FLAG_REPLACE
)) {
5192 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5193 add_ace(&old
->dacl
, &sd
->dacl
->aces
[i
], ctx
);
5199 case SMBC_XATTR_MODE_SET
:
5201 owner_sid
= old
->owner_sid
;
5202 grp_sid
= old
->group_sid
;
5206 case SMBC_XATTR_MODE_CHOWN
:
5207 owner_sid
= sd
->owner_sid
;
5210 case SMBC_XATTR_MODE_CHGRP
:
5211 grp_sid
= sd
->group_sid
;
5215 /* Denied ACE entries must come before allowed ones */
5216 sort_acl(old
->dacl
);
5218 /* Create new security descriptor and set it */
5219 sd
= make_sec_desc(ctx
, old
->revision
, SEC_DESC_SELF_RELATIVE
,
5220 owner_sid
, grp_sid
, NULL
, dacl
, &sd_size
);
5222 fnum
= cli_nt_create(cli
, filename
,
5223 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
);
5226 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5227 filename
, cli_errstr(cli
)));
5232 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
5233 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli
)));
5240 cli_close(cli
, fnum
);
5251 smbc_setxattr_ctx(SMBCCTX
*context
,
5272 const char * create_time_attr
;
5273 const char * access_time_attr
;
5274 const char * write_time_attr
;
5275 const char * change_time_attr
;
5278 if (!context
|| !context
->internal
||
5279 !context
->internal
->_initialized
) {
5281 errno
= EINVAL
; /* Best I can think of ... */
5293 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5294 fname
, name
, (int) size
, (const char*)value
));
5296 if (smbc_parse_path(context
, fname
,
5297 workgroup
, sizeof(workgroup
),
5298 server
, sizeof(server
),
5299 share
, sizeof(share
),
5302 password
, sizeof(password
),
5308 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5310 srv
= smbc_server(context
, True
,
5311 server
, share
, workgroup
, user
, password
);
5313 return -1; /* errno set by smbc_server */
5316 if (! srv
->no_nt_session
) {
5317 ipc_srv
= smbc_attr_server(context
, server
, share
,
5318 workgroup
, user
, password
,
5321 srv
->no_nt_session
= True
;
5327 ctx
= talloc_init("smbc_setxattr");
5334 * Are they asking to set the entire set of known attributes?
5336 if (StrCaseCmp(name
, "system.*") == 0 ||
5337 StrCaseCmp(name
, "system.*+") == 0) {
5340 talloc_asprintf(ctx
, "%s:%s",
5341 name
+7, (const char *) value
);
5349 ret
= cacl_set(ctx
, srv
->cli
,
5350 ipc_srv
->cli
, &pol
, path
,
5353 ? SMBC_XATTR_MODE_SET
5354 : SMBC_XATTR_MODE_ADD
),
5360 /* get a DOS Attribute Descriptor with current attributes */
5361 dad
= dos_attr_query(context
, ctx
, path
, srv
);
5363 /* Overwrite old with new, using what was provided */
5364 dos_attr_parse(context
, dad
, srv
, namevalue
);
5366 /* Set the new DOS attributes */
5367 if (! smbc_setatr(context
, srv
, path
,
5374 /* cause failure if NT failed too */
5379 /* we only fail if both NT and DOS sets failed */
5380 if (ret
< 0 && ! dad
) {
5381 ret
= -1; /* in case dad was null */
5387 talloc_destroy(ctx
);
5392 * Are they asking to set an access control element or to set
5393 * the entire access control list?
5395 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5396 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
5397 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5398 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5399 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
5403 talloc_asprintf(ctx
, "%s:%s",
5404 name
+19, (const char *) value
);
5407 ret
= -1; /* errno set by smbc_server() */
5409 else if (! namevalue
) {
5413 ret
= cacl_set(ctx
, srv
->cli
,
5414 ipc_srv
->cli
, &pol
, path
,
5417 ? SMBC_XATTR_MODE_SET
5418 : SMBC_XATTR_MODE_ADD
),
5421 talloc_destroy(ctx
);
5426 * Are they asking to set the owner?
5428 if (StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5429 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0) {
5433 talloc_asprintf(ctx
, "%s:%s",
5434 name
+19, (const char *) value
);
5438 ret
= -1; /* errno set by smbc_server() */
5440 else if (! namevalue
) {
5444 ret
= cacl_set(ctx
, srv
->cli
,
5445 ipc_srv
->cli
, &pol
, path
,
5446 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
5448 talloc_destroy(ctx
);
5453 * Are they asking to set the group?
5455 if (StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5456 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0) {
5460 talloc_asprintf(ctx
, "%s:%s",
5461 name
+19, (const char *) value
);
5464 /* errno set by smbc_server() */
5467 else if (! namevalue
) {
5471 ret
= cacl_set(ctx
, srv
->cli
,
5472 ipc_srv
->cli
, &pol
, path
,
5473 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
5475 talloc_destroy(ctx
);
5479 /* Determine whether to use old-style or new-style attribute names */
5480 if (context
->internal
->_full_time_names
) {
5481 /* new-style names */
5482 attr_strings
.create_time_attr
= "system.dos_attr.CREATE_TIME";
5483 attr_strings
.access_time_attr
= "system.dos_attr.ACCESS_TIME";
5484 attr_strings
.write_time_attr
= "system.dos_attr.WRITE_TIME";
5485 attr_strings
.change_time_attr
= "system.dos_attr.CHANGE_TIME";
5487 /* old-style names */
5488 attr_strings
.create_time_attr
= NULL
;
5489 attr_strings
.access_time_attr
= "system.dos_attr.A_TIME";
5490 attr_strings
.write_time_attr
= "system.dos_attr.M_TIME";
5491 attr_strings
.change_time_attr
= "system.dos_attr.C_TIME";
5495 * Are they asking to set a DOS attribute?
5497 if (StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
5498 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
5499 (attr_strings
.create_time_attr
!= NULL
&&
5500 StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) ||
5501 StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0 ||
5502 StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0 ||
5503 StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0) {
5505 /* get a DOS Attribute Descriptor with current attributes */
5506 dad
= dos_attr_query(context
, ctx
, path
, srv
);
5509 talloc_asprintf(ctx
, "%s:%s",
5510 name
+16, (const char *) value
);
5515 /* Overwrite old with provided new params */
5516 dos_attr_parse(context
, dad
, srv
, namevalue
);
5518 /* Set the new DOS attributes */
5519 ret2
= smbc_setatr(context
, srv
, path
,
5526 /* ret2 has True (success) / False (failure) */
5537 talloc_destroy(ctx
);
5541 /* Unsupported attribute name */
5542 talloc_destroy(ctx
);
5548 smbc_getxattr_ctx(SMBCCTX
*context
,
5566 const char * create_time_attr
;
5567 const char * access_time_attr
;
5568 const char * write_time_attr
;
5569 const char * change_time_attr
;
5573 if (!context
|| !context
->internal
||
5574 !context
->internal
->_initialized
) {
5576 errno
= EINVAL
; /* Best I can think of ... */
5588 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname
, name
));
5590 if (smbc_parse_path(context
, fname
,
5591 workgroup
, sizeof(workgroup
),
5592 server
, sizeof(server
),
5593 share
, sizeof(share
),
5596 password
, sizeof(password
),
5602 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5604 srv
= smbc_server(context
, True
,
5605 server
, share
, workgroup
, user
, password
);
5607 return -1; /* errno set by smbc_server */
5610 if (! srv
->no_nt_session
) {
5611 ipc_srv
= smbc_attr_server(context
, server
, share
,
5612 workgroup
, user
, password
,
5615 srv
->no_nt_session
= True
;
5621 ctx
= talloc_init("smbc:getxattr");
5627 /* Determine whether to use old-style or new-style attribute names */
5628 if (context
->internal
->_full_time_names
) {
5629 /* new-style names */
5630 attr_strings
.create_time_attr
= "system.dos_attr.CREATE_TIME";
5631 attr_strings
.access_time_attr
= "system.dos_attr.ACCESS_TIME";
5632 attr_strings
.write_time_attr
= "system.dos_attr.WRITE_TIME";
5633 attr_strings
.change_time_attr
= "system.dos_attr.CHANGE_TIME";
5635 /* old-style names */
5636 attr_strings
.create_time_attr
= NULL
;
5637 attr_strings
.access_time_attr
= "system.dos_attr.A_TIME";
5638 attr_strings
.write_time_attr
= "system.dos_attr.M_TIME";
5639 attr_strings
.change_time_attr
= "system.dos_attr.C_TIME";
5642 /* Are they requesting a supported attribute? */
5643 if (StrCaseCmp(name
, "system.*") == 0 ||
5644 StrnCaseCmp(name
, "system.*!", 9) == 0 ||
5645 StrCaseCmp(name
, "system.*+") == 0 ||
5646 StrnCaseCmp(name
, "system.*+!", 10) == 0 ||
5647 StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5648 StrnCaseCmp(name
, "system.nt_sec_desc.*!", 21) == 0 ||
5649 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
5650 StrnCaseCmp(name
, "system.nt_sec_desc.*+!", 22) == 0 ||
5651 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5652 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5653 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
5654 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5655 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
5656 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5657 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0 ||
5658 StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
5659 StrnCaseCmp(name
, "system.dos_attr.*!", 18) == 0 ||
5660 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
5661 StrCaseCmp(name
, "system.dos_attr.size") == 0 ||
5662 (attr_strings
.create_time_attr
!= NULL
&&
5663 StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) ||
5664 StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0 ||
5665 StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0 ||
5666 StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0 ||
5667 StrCaseCmp(name
, "system.dos_attr.inode") == 0) {
5670 ret
= cacl_get(context
, ctx
, srv
,
5671 ipc_srv
== NULL
? NULL
: ipc_srv
->cli
,
5673 CONST_DISCARD(char *, name
),
5674 CONST_DISCARD(char *, value
), size
);
5675 if (ret
< 0 && errno
== 0) {
5676 errno
= smbc_errno(context
, srv
->cli
);
5678 talloc_destroy(ctx
);
5682 /* Unsupported attribute name */
5683 talloc_destroy(ctx
);
5690 smbc_removexattr_ctx(SMBCCTX
*context
,
5706 if (!context
|| !context
->internal
||
5707 !context
->internal
->_initialized
) {
5709 errno
= EINVAL
; /* Best I can think of ... */
5721 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname
, name
));
5723 if (smbc_parse_path(context
, fname
,
5724 workgroup
, sizeof(workgroup
),
5725 server
, sizeof(server
),
5726 share
, sizeof(share
),
5729 password
, sizeof(password
),
5735 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
5737 srv
= smbc_server(context
, True
,
5738 server
, share
, workgroup
, user
, password
);
5740 return -1; /* errno set by smbc_server */
5743 if (! srv
->no_nt_session
) {
5744 ipc_srv
= smbc_attr_server(context
, server
, share
,
5745 workgroup
, user
, password
,
5748 srv
->no_nt_session
= True
;
5755 return -1; /* errno set by smbc_attr_server */
5758 ctx
= talloc_init("smbc_removexattr");
5764 /* Are they asking to set the entire ACL? */
5765 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5766 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0) {
5769 ret
= cacl_set(ctx
, srv
->cli
,
5770 ipc_srv
->cli
, &pol
, path
,
5771 NULL
, SMBC_XATTR_MODE_REMOVE_ALL
, 0);
5772 talloc_destroy(ctx
);
5777 * Are they asking to remove one or more spceific security descriptor
5780 if (StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5781 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5782 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
5783 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5784 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
5785 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5786 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
5789 ret
= cacl_set(ctx
, srv
->cli
,
5790 ipc_srv
->cli
, &pol
, path
,
5791 name
+ 19, SMBC_XATTR_MODE_REMOVE
, 0);
5792 talloc_destroy(ctx
);
5796 /* Unsupported attribute name */
5797 talloc_destroy(ctx
);
5803 smbc_listxattr_ctx(SMBCCTX
*context
,
5809 * This isn't quite what listxattr() is supposed to do. This returns
5810 * the complete set of attribute names, always, rather than only those
5811 * attribute names which actually exist for a file. Hmmm...
5813 const char supported_old
[] =
5816 "system.nt_sec_desc.revision\0"
5817 "system.nt_sec_desc.owner\0"
5818 "system.nt_sec_desc.owner+\0"
5819 "system.nt_sec_desc.group\0"
5820 "system.nt_sec_desc.group+\0"
5821 "system.nt_sec_desc.acl.*\0"
5822 "system.nt_sec_desc.acl\0"
5823 "system.nt_sec_desc.acl+\0"
5824 "system.nt_sec_desc.*\0"
5825 "system.nt_sec_desc.*+\0"
5826 "system.dos_attr.*\0"
5827 "system.dos_attr.mode\0"
5828 "system.dos_attr.c_time\0"
5829 "system.dos_attr.a_time\0"
5830 "system.dos_attr.m_time\0"
5832 const char supported_new
[] =
5835 "system.nt_sec_desc.revision\0"
5836 "system.nt_sec_desc.owner\0"
5837 "system.nt_sec_desc.owner+\0"
5838 "system.nt_sec_desc.group\0"
5839 "system.nt_sec_desc.group+\0"
5840 "system.nt_sec_desc.acl.*\0"
5841 "system.nt_sec_desc.acl\0"
5842 "system.nt_sec_desc.acl+\0"
5843 "system.nt_sec_desc.*\0"
5844 "system.nt_sec_desc.*+\0"
5845 "system.dos_attr.*\0"
5846 "system.dos_attr.mode\0"
5847 "system.dos_attr.create_time\0"
5848 "system.dos_attr.access_time\0"
5849 "system.dos_attr.write_time\0"
5850 "system.dos_attr.change_time\0"
5852 const char * supported
;
5854 if (context
->internal
->_full_time_names
) {
5855 supported
= supported_new
;
5857 supported
= supported_old
;
5861 return sizeof(supported
);
5864 if (sizeof(supported
) > size
) {
5869 /* this can't be strcpy() because there are embedded null characters */
5870 memcpy(list
, supported
, sizeof(supported
));
5871 return sizeof(supported
);
5876 * Open a print file to be written to by other calls
5880 smbc_open_print_job_ctx(SMBCCTX
*context
,
5889 if (!context
|| !context
->internal
||
5890 !context
->internal
->_initialized
) {
5904 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
5906 if (smbc_parse_path(context
, fname
,
5908 server
, sizeof(server
),
5909 share
, sizeof(share
),
5912 password
, sizeof(password
),
5918 /* What if the path is empty, or the file exists? */
5920 return context
->open(context
, fname
, O_WRONLY
, 666);
5925 * Routine to print a file on a remote server ...
5927 * We open the file, which we assume to be on a remote server, and then
5928 * copy it to a print file on the share specified by printq.
5932 smbc_print_file_ctx(SMBCCTX
*c_file
,
5944 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
5945 !c_print
->internal
->_initialized
) {
5952 if (!fname
&& !printq
) {
5959 /* Try to open the file for reading ... */
5961 if ((long)(fid1
= c_file
->open(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
5963 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
5964 return -1; /* smbc_open sets errno */
5968 /* Now, try to open the printer file for writing */
5970 if ((long)(fid2
= c_print
->open_print_job(c_print
, printq
)) < 0) {
5972 saverr
= errno
; /* Save errno */
5973 c_file
->close_fn(c_file
, fid1
);
5979 while ((bytes
= c_file
->read(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
5983 if ((c_print
->write(c_print
, fid2
, buf
, bytes
)) < 0) {
5986 c_file
->close_fn(c_file
, fid1
);
5987 c_print
->close_fn(c_print
, fid2
);
5996 c_file
->close_fn(c_file
, fid1
); /* We have to close these anyway */
5997 c_print
->close_fn(c_print
, fid2
);
6011 * Routine to list print jobs on a printer share ...
6015 smbc_list_print_jobs_ctx(SMBCCTX
*context
,
6017 smbc_list_print_job_fn fn
)
6027 if (!context
|| !context
->internal
||
6028 !context
->internal
->_initialized
) {
6042 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
6044 if (smbc_parse_path(context
, fname
,
6045 workgroup
, sizeof(workgroup
),
6046 server
, sizeof(server
),
6047 share
, sizeof(share
),
6050 password
, sizeof(password
),
6056 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
6058 srv
= smbc_server(context
, True
,
6059 server
, share
, workgroup
, user
, password
);
6063 return -1; /* errno set by smbc_server */
6067 if (cli_print_queue(srv
->cli
,
6068 (void (*)(struct print_job_info
*))fn
) < 0) {
6070 errno
= smbc_errno(context
, srv
->cli
);
6080 * Delete a print job from a remote printer share
6084 smbc_unlink_print_job_ctx(SMBCCTX
*context
,
6097 if (!context
|| !context
->internal
||
6098 !context
->internal
->_initialized
) {
6112 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
6114 if (smbc_parse_path(context
, fname
,
6115 workgroup
, sizeof(workgroup
),
6116 server
, sizeof(server
),
6117 share
, sizeof(share
),
6120 password
, sizeof(password
),
6126 if (user
[0] == (char)0) fstrcpy(user
, context
->user
);
6128 srv
= smbc_server(context
, True
,
6129 server
, share
, workgroup
, user
, password
);
6133 return -1; /* errno set by smbc_server */
6137 if ((err
= cli_printjob_del(srv
->cli
, id
)) != 0) {
6140 errno
= smbc_errno(context
, srv
->cli
);
6141 else if (err
== ERRnosuchprintjob
)
6152 * Get a new empty handle to fill in with your own info
6155 smbc_new_context(void)
6159 context
= SMB_MALLOC_P(SMBCCTX
);
6165 ZERO_STRUCTP(context
);
6167 context
->internal
= SMB_MALLOC_P(struct smbc_internal_data
);
6168 if (!context
->internal
) {
6174 ZERO_STRUCTP(context
->internal
);
6177 /* ADD REASONABLE DEFAULTS */
6179 context
->timeout
= 20000; /* 20 seconds */
6181 context
->options
.browse_max_lmb_count
= 3; /* # LMBs to query */
6182 context
->options
.urlencode_readdir_entries
= False
;/* backward compat */
6183 context
->options
.one_share_per_server
= False
;/* backward compat */
6184 context
->internal
->_share_mode
= SMBC_SHAREMODE_DENY_NONE
;
6185 /* backward compat */
6187 context
->open
= smbc_open_ctx
;
6188 context
->creat
= smbc_creat_ctx
;
6189 context
->read
= smbc_read_ctx
;
6190 context
->write
= smbc_write_ctx
;
6191 context
->close_fn
= smbc_close_ctx
;
6192 context
->unlink
= smbc_unlink_ctx
;
6193 context
->rename
= smbc_rename_ctx
;
6194 context
->lseek
= smbc_lseek_ctx
;
6195 context
->stat
= smbc_stat_ctx
;
6196 context
->fstat
= smbc_fstat_ctx
;
6197 context
->opendir
= smbc_opendir_ctx
;
6198 context
->closedir
= smbc_closedir_ctx
;
6199 context
->readdir
= smbc_readdir_ctx
;
6200 context
->getdents
= smbc_getdents_ctx
;
6201 context
->mkdir
= smbc_mkdir_ctx
;
6202 context
->rmdir
= smbc_rmdir_ctx
;
6203 context
->telldir
= smbc_telldir_ctx
;
6204 context
->lseekdir
= smbc_lseekdir_ctx
;
6205 context
->fstatdir
= smbc_fstatdir_ctx
;
6206 context
->chmod
= smbc_chmod_ctx
;
6207 context
->utimes
= smbc_utimes_ctx
;
6208 context
->setxattr
= smbc_setxattr_ctx
;
6209 context
->getxattr
= smbc_getxattr_ctx
;
6210 context
->removexattr
= smbc_removexattr_ctx
;
6211 context
->listxattr
= smbc_listxattr_ctx
;
6212 context
->open_print_job
= smbc_open_print_job_ctx
;
6213 context
->print_file
= smbc_print_file_ctx
;
6214 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
6215 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
6217 context
->callbacks
.check_server_fn
= smbc_check_server
;
6218 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
6220 smbc_default_cache_functions(context
);
6228 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6229 * and thus you'll be leaking memory if not handled properly.
6233 smbc_free_context(SMBCCTX
*context
,
6243 DEBUG(1,("Performing aggressive shutdown.\n"));
6245 f
= context
->internal
->_files
;
6247 context
->close_fn(context
, f
);
6250 context
->internal
->_files
= NULL
;
6252 /* First try to remove the servers the nice way. */
6253 if (context
->callbacks
.purge_cached_fn(context
)) {
6256 DEBUG(1, ("Could not purge all servers, "
6257 "Nice way shutdown failed.\n"));
6258 s
= context
->internal
->_servers
;
6260 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6262 cli_shutdown(s
->cli
);
6263 context
->callbacks
.remove_cached_srv_fn(context
,
6266 DLIST_REMOVE(context
->internal
->_servers
, s
);
6270 context
->internal
->_servers
= NULL
;
6274 /* This is the polite way */
6275 if (context
->callbacks
.purge_cached_fn(context
)) {
6276 DEBUG(1, ("Could not purge all servers, "
6277 "free_context failed.\n"));
6281 if (context
->internal
->_servers
) {
6282 DEBUG(1, ("Active servers in context, "
6283 "free_context failed.\n"));
6287 if (context
->internal
->_files
) {
6288 DEBUG(1, ("Active files in context, "
6289 "free_context failed.\n"));
6295 /* Things we have to clean up */
6296 SAFE_FREE(context
->workgroup
);
6297 SAFE_FREE(context
->netbios_name
);
6298 SAFE_FREE(context
->user
);
6300 DEBUG(3, ("Context %p succesfully freed\n", context
));
6301 SAFE_FREE(context
->internal
);
6308 * Each time the context structure is changed, we have binary backward
6309 * compatibility issues. Instead of modifying the public portions of the
6310 * context structure to add new options, instead, we put them in the internal
6311 * portion of the context structure and provide a set function for these new
6315 smbc_option_set(SMBCCTX
*context
,
6317 ... /* option_value */)
6323 smbc_get_auth_data_with_context_fn auth_fn
;
6327 va_start(ap
, option_name
);
6329 if (strcmp(option_name
, "debug_to_stderr") == 0) {
6331 * Log to standard error instead of standard output.
6333 option_value
.b
= (BOOL
) va_arg(ap
, int);
6334 context
->internal
->_debug_stderr
= option_value
.b
;
6336 } else if (strcmp(option_name
, "full_time_names") == 0) {
6338 * Use new-style time attribute names, e.g. WRITE_TIME rather
6339 * than the old-style names such as M_TIME. This allows also
6340 * setting/getting CREATE_TIME which was previously
6341 * unimplemented. (Note that the old C_TIME was supposed to
6342 * be CHANGE_TIME but was confused and sometimes referred to
6345 option_value
.b
= (BOOL
) va_arg(ap
, int);
6346 context
->internal
->_full_time_names
= option_value
.b
;
6348 } else if (strcmp(option_name
, "open_share_mode") == 0) {
6350 * The share mode to use for files opened with
6351 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6353 option_value
.i
= va_arg(ap
, int);
6354 context
->internal
->_share_mode
=
6355 (smbc_share_mode
) option_value
.i
;
6357 } else if (strcmp(option_name
, "auth_function") == 0) {
6359 * Use the new-style authentication function which includes
6362 option_value
.auth_fn
=
6363 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
6364 context
->internal
->_auth_fn_with_context
=
6365 option_value
.auth_fn
;
6366 } else if (strcmp(option_name
, "user_data") == 0) {
6368 * Save a user data handle which may be retrieved by the user
6369 * with smbc_option_get()
6371 option_value
.v
= va_arg(ap
, void *);
6372 context
->internal
->_user_data
= option_value
.v
;
6380 * Retrieve the current value of an option
6383 smbc_option_get(SMBCCTX
*context
,
6386 if (strcmp(option_name
, "debug_stderr") == 0) {
6388 * Log to standard error instead of standard output.
6390 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6391 return (void *) (intptr_t) context
->internal
->_debug_stderr
;
6393 return (void *) context
->internal
->_debug_stderr
;
6395 } else if (strcmp(option_name
, "full_time_names") == 0) {
6397 * Use new-style time attribute names, e.g. WRITE_TIME rather
6398 * than the old-style names such as M_TIME. This allows also
6399 * setting/getting CREATE_TIME which was previously
6400 * unimplemented. (Note that the old C_TIME was supposed to
6401 * be CHANGE_TIME but was confused and sometimes referred to
6404 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6405 return (void *) (intptr_t) context
->internal
->_full_time_names
;
6407 return (void *) context
->internal
->_full_time_names
;
6410 } else if (strcmp(option_name
, "auth_function") == 0) {
6412 * Use the new-style authentication function which includes
6415 return (void *) context
->internal
->_auth_fn_with_context
;
6416 } else if (strcmp(option_name
, "user_data") == 0) {
6418 * Save a user data handle which may be retrieved by the user
6419 * with smbc_option_get()
6421 return context
->internal
->_user_data
;
6429 * Initialise the library etc
6431 * We accept a struct containing handle information.
6432 * valid values for info->debug from 0 to 100,
6433 * and insist that info->fn must be non-null.
6436 smbc_init_context(SMBCCTX
*context
)
6443 if (!context
|| !context
->internal
) {
6448 /* Do not initialise the same client twice */
6449 if (context
->internal
->_initialized
) {
6453 if ((!context
->callbacks
.auth_fn
&&
6454 !context
->internal
->_auth_fn_with_context
) ||
6455 context
->debug
< 0 ||
6456 context
->debug
> 100) {
6463 if (!smbc_initialized
) {
6465 * Do some library-wide intializations the first time we get
6468 BOOL conf_loaded
= False
;
6470 /* Set this to what the user wants */
6471 DEBUGLEVEL
= context
->debug
;
6475 setup_logging("libsmbclient", True
);
6476 if (context
->internal
->_debug_stderr
) {
6478 x_setbuf(x_stderr
, NULL
);
6481 /* Here we would open the smb.conf file if needed ... */
6483 in_client
= True
; /* FIXME, make a param */
6485 home
= getenv("HOME");
6487 slprintf(conf
, sizeof(conf
), "%s/.smb/smb.conf", home
);
6488 if (lp_load(conf
, True
, False
, False
, True
)) {
6491 DEBUG(5, ("Could not load config file: %s\n",
6498 * Well, if that failed, try the dyn_CONFIGFILE
6499 * Which points to the standard locn, and if that
6500 * fails, silently ignore it and use the internal
6504 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
, False
)) {
6505 DEBUG(5, ("Could not load config file: %s\n",
6509 * We loaded the global config file. Now lets
6510 * load user-specific modifications to the
6513 slprintf(conf
, sizeof(conf
),
6514 "%s/.smb/smb.conf.append", home
);
6515 if (!lp_load(conf
, True
, False
, False
, False
)) {
6517 ("Could not append config file: "
6524 load_interfaces(); /* Load the list of interfaces ... */
6526 reopen_logs(); /* Get logging working ... */
6529 * Block SIGPIPE (from lib/util_sock.c: write())
6530 * It is not needed and should not stop execution
6532 BlockSignals(True
, SIGPIPE
);
6534 /* Done with one-time initialisation */
6535 smbc_initialized
= 1;
6539 if (!context
->user
) {
6541 * FIXME: Is this the best way to get the user info?
6543 user
= getenv("USER");
6544 /* walk around as "guest" if no username can be found */
6545 if (!user
) context
->user
= SMB_STRDUP("guest");
6546 else context
->user
= SMB_STRDUP(user
);
6549 if (!context
->netbios_name
) {
6551 * We try to get our netbios name from the config. If that
6552 * fails we fall back on constructing our netbios name from
6555 if (global_myname()) {
6556 context
->netbios_name
= SMB_STRDUP(global_myname());
6560 * Hmmm, I want to get hostname as well, but I am too
6561 * lazy for the moment
6564 context
->netbios_name
= (char *)SMB_MALLOC(17);
6565 if (!context
->netbios_name
) {
6569 slprintf(context
->netbios_name
, 16,
6570 "smbc%s%d", context
->user
, pid
);
6574 DEBUG(1, ("Using netbios name %s.\n", context
->netbios_name
));
6576 if (!context
->workgroup
) {
6577 if (lp_workgroup()) {
6578 context
->workgroup
= SMB_STRDUP(lp_workgroup());
6581 /* TODO: Think about a decent default workgroup */
6582 context
->workgroup
= SMB_STRDUP("samba");
6586 DEBUG(1, ("Using workgroup %s.\n", context
->workgroup
));
6588 /* shortest timeout is 1 second */
6589 if (context
->timeout
> 0 && context
->timeout
< 1000)
6590 context
->timeout
= 1000;
6593 * FIXME: Should we check the function pointers here?
6596 context
->internal
->_initialized
= True
;
6602 /* Return the verion of samba, and thus libsmbclient */
6606 return samba_version_string();