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
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
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')
136 * and smbc_urldecode_talloc() (internal fn.)
138 * Convert strings of %xx to their single character equivalent. Each 'x' must
139 * be a valid hexadecimal digit, or that % sequence is left undecoded.
141 * dest may, but need not be, the same pointer as src.
143 * Returns the number of % sequences which could not be converted due to lack
144 * of two following hexadecimal digits.
147 smbc_urldecode_talloc(TALLOC_CTX
*ctx
, char **pp_dest
, const char *src
)
149 int old_length
= strlen(src
);
155 if (old_length
== 0) {
160 for (i
= 0; i
< old_length
; ) {
161 unsigned char character
= src
[i
++];
163 if (character
== '%') {
164 int a
= i
+1 < old_length
? hex2int(src
[i
]) : -1;
165 int b
= i
+1 < old_length
? hex2int(src
[i
+1]) : -1;
167 /* Replace valid sequence */
168 if (a
!= -1 && b
!= -1) {
169 /* Replace valid %xx sequence with %dd */
170 character
= (a
* 16) + b
;
171 if (character
== '\0') {
172 break; /* Stop at %00 */
182 dest
= TALLOC_ARRAY(ctx
, char, newlen
);
188 for (p
= dest
, i
= 0; i
< old_length
; ) {
189 unsigned char character
= src
[i
++];
191 if (character
== '%') {
192 int a
= i
+1 < old_length
? hex2int(src
[i
]) : -1;
193 int b
= i
+1 < old_length
? hex2int(src
[i
+1]) : -1;
195 /* Replace valid sequence */
196 if (a
!= -1 && b
!= -1) {
197 /* Replace valid %xx sequence with %dd */
198 character
= (a
* 16) + b
;
199 if (character
== '\0') {
200 break; /* Stop at %00 */
216 smbc_urldecode(char *dest
, char *src
, size_t max_dest_len
)
218 TALLOC_CTX
*frame
= talloc_stackframe();
220 int ret
= smbc_urldecode_talloc(frame
, &pdest
, src
);
223 strlcpy(dest
, pdest
, max_dest_len
);
232 * Convert any characters not specifically allowed in a URL into their %xx
235 * Returns the remaining buffer length.
238 smbc_urlencode(char *dest
, char *src
, int max_dest_len
)
240 char hex
[] = "0123456789ABCDEF";
242 for (; *src
!= '\0' && max_dest_len
>= 3; src
++) {
254 *dest
++ = hex
[(*src
>> 4) & 0x0f];
255 *dest
++ = hex
[*src
& 0x0f];
270 * Function to parse a path and turn it into components
272 * The general format of an SMB URI is explain in Christopher Hertel's CIFS
273 * book, at http://ubiqx.org/cifs/Appendix-D.html. We accept a subset of the
274 * general format ("smb:" only; we do not look for "cifs:").
278 * smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
282 * smb:// Show all workgroups.
284 * The method of locating the list of workgroups varies
285 * depending upon the setting of the context variable
286 * context->options.browse_max_lmb_count. This value
287 * determine the maximum number of local master browsers to
288 * query for the list of workgroups. In order to ensure that
289 * a complete list of workgroups is obtained, all master
290 * browsers must be queried, but if there are many
291 * workgroups, the time spent querying can begin to add up.
292 * For small networks (not many workgroups), it is suggested
293 * that this variable be set to 0, indicating query all local
294 * master browsers. When the network has many workgroups, a
295 * reasonable setting for this variable might be around 3.
297 * smb://name/ if name<1D> or name<1B> exists, list servers in
298 * workgroup, else, if name<20> exists, list all shares
301 * If "options" are provided, this function returns the entire option list as a
302 * string, for later parsing by the caller. Note that currently, no options
306 static const char *smbc_prefix
= "smb:";
309 smbc_parse_path(TALLOC_CTX
*ctx
,
325 /* Ensure these returns are at least valid pointers. */
326 *pp_server
= talloc_strdup(ctx
, "");
327 *pp_share
= talloc_strdup(ctx
, "");
328 *pp_path
= talloc_strdup(ctx
, "");
329 *pp_user
= talloc_strdup(ctx
, "");
330 *pp_password
= talloc_strdup(ctx
, "");
332 if (!*pp_server
|| !*pp_share
|| !*pp_path
||
333 !*pp_user
|| !*pp_password
) {
338 * Assume we wont find an authentication domain to parse, so default
339 * to the workgroup in the provided context.
341 if (pp_workgroup
!= NULL
) {
342 *pp_workgroup
= talloc_strdup(ctx
, context
->workgroup
);
346 *pp_options
= talloc_strdup(ctx
, "");
348 s
= talloc_strdup(ctx
, fname
);
350 /* see if it has the right prefix */
351 len
= strlen(smbc_prefix
);
352 if (strncmp(s
,smbc_prefix
,len
) || (s
[len
] != '/' && s
[len
] != 0)) {
353 return -1; /* What about no smb: ? */
358 /* Watch the test below, we are testing to see if we should exit */
360 if (strncmp(p
, "//", 2) && strncmp(p
, "\\\\", 2)) {
361 DEBUG(1, ("Invalid path (does not begin with smb://"));
365 p
+= 2; /* Skip the double slash */
367 /* See if any options were specified */
368 if ((q
= strrchr(p
, '?')) != NULL
) {
369 /* There are options. Null terminate here and point to them */
372 DEBUG(4, ("Found options '%s'", q
));
374 /* Copy the options */
375 if (*pp_options
!= NULL
) {
376 TALLOC_FREE(*pp_options
);
377 *pp_options
= talloc_strdup(ctx
, q
);
386 int wl
= strlen(context
->workgroup
);
392 *pp_server
= talloc_strdup(ctx
, context
->workgroup
);
396 *pp_server
[wl
] = '\0';
401 * ok, its for us. Now parse out the server, share etc.
403 * However, we want to parse out [[domain;]user[:password]@] if it
407 /* check that '@' occurs before '/', if '/' exists at all */
408 q
= strchr_m(p
, '@');
409 r
= strchr_m(p
, '/');
410 if (q
&& (!r
|| q
< r
)) {
411 char *userinfo
= NULL
;
414 next_token_no_ltrim_talloc(ctx
, &p
, &userinfo
, "@");
420 if (strchr_m(u
, ';')) {
422 next_token_no_ltrim_talloc(ctx
, &u
, &workgroup
, ";");
427 *pp_workgroup
= workgroup
;
431 if (strchr_m(u
, ':')) {
432 next_token_no_ltrim_talloc(ctx
, &u
, pp_user
, ":");
436 *pp_password
= talloc_strdup(ctx
, u
);
441 *pp_user
= talloc_strdup(ctx
, u
);
448 if (!next_token_talloc(ctx
, &p
, pp_server
, "/")) {
453 goto decoding
; /* That's it ... */
456 if (!next_token_talloc(ctx
, &p
, pp_share
, "/")) {
461 * Prepend a leading slash if there's a file path, as required by
465 *pp_path
= talloc_asprintf(ctx
,
469 *pp_path
= talloc_strdup(ctx
, "");
474 string_replace(*pp_path
, '/', '\\');
478 (void) smbc_urldecode_talloc(ctx
, pp_path
, *pp_path
);
479 (void) smbc_urldecode_talloc(ctx
, pp_server
, *pp_server
);
480 (void) smbc_urldecode_talloc(ctx
, pp_share
, *pp_share
);
481 (void) smbc_urldecode_talloc(ctx
, pp_user
, *pp_user
);
482 (void) smbc_urldecode_talloc(ctx
, pp_password
, *pp_password
);
488 * Verify that the options specified in a URL are valid
491 smbc_check_options(char *server
,
496 DEBUG(4, ("smbc_check_options(): server='%s' share='%s' "
497 "path='%s' options='%s'\n",
498 server
, share
, path
, options
));
500 /* No options at all is always ok */
501 if (! *options
) return 0;
503 /* Currently, we don't support any options. */
508 * Convert an SMB error into a UNIX error ...
511 smbc_errno(SMBCCTX
*context
,
514 int ret
= cli_errno(c
);
516 if (cli_is_dos_error(c
)) {
520 cli_dos_error(c
, &eclass
, &ecode
);
522 DEBUG(3,("smbc_error %d %d (0x%x) -> %d\n",
523 (int)eclass
, (int)ecode
, (int)ecode
, ret
));
527 status
= cli_nt_error(c
);
529 DEBUG(3,("smbc errno %s -> %d\n",
530 nt_errstr(status
), ret
));
537 * Check a server for being alive and well.
538 * returns 0 if the server is in shape. Returns 1 on error
540 * Also useable outside libsmbclient to enable external cache
541 * to do some checks too.
544 smbc_check_server(SMBCCTX
* context
,
548 struct sockaddr addr
;
551 return (getpeername(server
->cli
->fd
, &addr
, &size
) == -1);
555 * Remove a server from the cached server list it's unused.
556 * On success, 0 is returned. 1 is returned if the server could not be removed.
558 * Also useable outside libsmbclient
561 smbc_remove_unused_server(SMBCCTX
* context
,
566 /* are we being fooled ? */
567 if (!context
|| !context
->internal
||
568 !context
->internal
->_initialized
|| !srv
) return 1;
571 /* Check all open files/directories for a relation with this server */
572 for (file
= context
->internal
->_files
; file
; file
=file
->next
) {
573 if (file
->srv
== srv
) {
575 DEBUG(3, ("smbc_remove_usused_server: "
576 "%p still used by %p.\n",
582 DLIST_REMOVE(context
->internal
->_servers
, srv
);
584 cli_shutdown(srv
->cli
);
587 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
589 (context
->callbacks
.remove_cached_srv_fn
)(context
, srv
);
595 /****************************************************************
596 * Call the auth_fn with fixed size (fstring) buffers.
597 ***************************************************************/
599 static void call_auth_fn(TALLOC_CTX
*ctx
,
611 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
612 strlcpy(username
, *pp_username
, sizeof(username
));
613 strlcpy(password
, *pp_password
, sizeof(password
));
615 if (context
->internal
->_auth_fn_with_context
!= NULL
) {
616 (context
->internal
->_auth_fn_with_context
)(
619 workgroup
, sizeof(workgroup
),
620 username
, sizeof(username
),
621 password
, sizeof(password
));
623 (context
->callbacks
.auth_fn
)(
625 workgroup
, sizeof(workgroup
),
626 username
, sizeof(username
),
627 password
, sizeof(password
));
630 TALLOC_FREE(*pp_workgroup
);
631 TALLOC_FREE(*pp_username
);
632 TALLOC_FREE(*pp_password
);
634 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
635 *pp_username
= talloc_strdup(ctx
, username
);
636 *pp_password
= talloc_strdup(ctx
, password
);
640 find_server(TALLOC_CTX
*ctx
,
653 srv
= (context
->callbacks
.get_cached_srv_fn
)(context
, server
, share
,
654 *pp_workgroup
, *pp_username
);
656 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
657 !*pp_password
|| !(*pp_password
)[0])) {
658 call_auth_fn(ctx
, context
, server
, share
,
659 pp_workgroup
, pp_username
, pp_password
);
661 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
666 * However, smbc_auth_fn may have picked up info relating to
667 * an existing connection, so try for an existing connection
671 goto check_server_cache
;
676 if ((context
->callbacks
.check_server_fn
)(context
, srv
)) {
678 * This server is no good anymore
679 * Try to remove it and check for more possible
680 * servers in the cache
682 if ((context
->callbacks
.remove_unused_server_fn
)(context
,
685 * We could not remove the server completely,
686 * remove it from the cache so we will not get
687 * it again. It will be removed when the last
688 * file/dir is closed.
690 (context
->callbacks
.remove_cached_srv_fn
)(context
,
695 * Maybe there are more cached connections to this
698 goto check_server_cache
;
708 * Connect to a server, possibly on an existing connection
710 * Here, what we want to do is: If the server and username
711 * match an existing connection, reuse that, otherwise, establish a
714 * If we have to create a new connection, call the auth_fn to get the
715 * info we need, unless the username and password were passed in.
719 smbc_server(TALLOC_CTX
*ctx
,
721 bool connect_if_not_found
,
730 struct nmb_name called
, calling
;
731 const char *server_n
= server
;
732 struct sockaddr_storage ss
;
733 int tried_reverse
= 0;
736 const char *username_used
;
742 if (server
[0] == 0) {
747 /* Look for a cached connection */
748 srv
= find_server(ctx
, context
, server
, share
,
749 pp_workgroup
, pp_username
, pp_password
);
752 * If we found a connection and we're only allowed one share per
755 if (srv
&& *share
!= '\0' && context
->options
.one_share_per_server
) {
758 * ... then if there's no current connection to the share,
759 * connect to it. find_server(), or rather the function
760 * pointed to by context->callbacks.get_cached_srv_fn which
761 * was called by find_server(), will have issued a tree
762 * disconnect if the requested share is not the same as the
763 * one that was already connected.
765 if (srv
->cli
->cnum
== (uint16
) -1) {
766 /* Ensure we have accurate auth info */
767 call_auth_fn(ctx
, context
, server
, share
,
768 pp_workgroup
, pp_username
, pp_password
);
770 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
772 cli_shutdown(srv
->cli
);
774 (context
->callbacks
.remove_cached_srv_fn
)(context
,
780 * We don't need to renegotiate encryption
781 * here as the encryption context is not per
785 if (!cli_send_tconX(srv
->cli
, share
, "?????",
787 strlen(*pp_password
)+1)) {
789 errno
= smbc_errno(context
, srv
->cli
);
790 cli_shutdown(srv
->cli
);
792 (context
->callbacks
.remove_cached_srv_fn
)(context
,
798 * Regenerate the dev value since it's based on both
802 srv
->dev
= (dev_t
)(str_checksum(server
) ^
803 str_checksum(share
));
808 /* If we have a connection... */
811 /* ... then we're done here. Give 'em what they came for. */
815 /* If we're not asked to connect when a connection doesn't exist... */
816 if (! connect_if_not_found
) {
817 /* ... then we're done here. */
821 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
826 make_nmb_name(&calling
, context
->netbios_name
, 0x0);
827 make_nmb_name(&called
, server
, 0x20);
829 DEBUG(4,("smbc_server: server_n=[%s] server=[%s]\n", server_n
, server
));
831 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
837 /* have to open a new connection */
838 if ((c
= cli_initialise()) == NULL
) {
843 if (context
->flags
& SMB_CTX_FLAG_USE_KERBEROS
) {
844 c
->use_kerberos
= True
;
846 if (context
->flags
& SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS
) {
847 c
->fallback_after_kerberos
= True
;
850 c
->timeout
= context
->timeout
;
853 * Force use of port 139 for first try if share is $IPC, empty, or
854 * null, so browse lists can work
856 if (share
== NULL
|| *share
== '\0' || strcmp(share
, "IPC$") == 0) {
857 port_try_first
= 139;
860 port_try_first
= 445;
864 c
->port
= port_try_first
;
866 status
= cli_connect(c
, server_n
, &ss
);
867 if (!NT_STATUS_IS_OK(status
)) {
869 /* First connection attempt failed. Try alternate port. */
870 c
->port
= port_try_next
;
872 status
= cli_connect(c
, server_n
, &ss
);
873 if (!NT_STATUS_IS_OK(status
)) {
880 if (!cli_session_request(c
, &calling
, &called
)) {
882 if (strcmp(called
.name
, "*SMBSERVER")) {
883 make_nmb_name(&called
, "*SMBSERVER", 0x20);
885 } else { /* Try one more time, but ensure we don't loop */
887 /* Only try this if server is an IP address ... */
889 if (is_ipaddress(server
) && !tried_reverse
) {
891 struct sockaddr_storage rem_ss
;
893 if (!interpret_string_addr(&rem_ss
, server
,
895 DEBUG(4, ("Could not convert IP address "
896 "%s to struct sockaddr_storage\n",
902 tried_reverse
++; /* Yuck */
904 if (name_status_find("*", 0, 0, &rem_ss
, remote_name
)) {
905 make_nmb_name(&called
, remote_name
, 0x20);
914 DEBUG(4,(" session request ok\n"));
916 if (!cli_negprot(c
)) {
922 username_used
= *pp_username
;
924 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
925 *pp_password
, strlen(*pp_password
),
926 *pp_password
, strlen(*pp_password
),
929 /* Failed. Try an anonymous login, if allowed by flags. */
932 if ((context
->flags
& SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON
) ||
933 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
944 DEBUG(4,(" session setup ok\n"));
946 if (!cli_send_tconX(c
, share
, "?????",
947 *pp_password
, strlen(*pp_password
)+1)) {
948 errno
= smbc_errno(context
, c
);
953 DEBUG(4,(" tconx ok\n"));
955 if (context
->internal
->_smb_encryption_level
) {
956 /* Attempt UNIX smb encryption. */
957 if (!NT_STATUS_IS_OK(cli_force_encryption(c
,
963 * context->internal->_smb_encryption_level == 1
964 * means don't fail if encryption can't be negotiated,
965 * == 2 means fail if encryption can't be negotiated.
968 DEBUG(4,(" SMB encrypt failed\n"));
970 if (context
->internal
->_smb_encryption_level
== 2) {
976 DEBUG(4,(" SMB encrypt ok\n"));
980 * Ok, we have got a nice connection
981 * Let's allocate a server structure.
984 srv
= SMB_MALLOC_P(SMBCSRV
);
992 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
993 srv
->no_pathinfo
= False
;
994 srv
->no_pathinfo2
= False
;
995 srv
->no_nt_session
= False
;
997 /* now add it to the cache (internal or external) */
998 /* Let the cache function set errno if it wants to */
1000 if ((context
->callbacks
.add_cached_srv_fn
)(context
, srv
,
1004 int saved_errno
= errno
;
1005 DEBUG(3, (" Failed to add server to cache\n"));
1006 errno
= saved_errno
;
1013 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
1014 server
, share
, srv
));
1016 DLIST_ADD(context
->internal
->_servers
, srv
);
1030 * Connect to a server for getting/setting attributes, possibly on an existing
1031 * connection. This works similarly to smbc_server().
1034 smbc_attr_server(TALLOC_CTX
*ctx
,
1038 char **pp_workgroup
,
1043 struct sockaddr_storage ss
;
1044 struct cli_state
*ipc_cli
;
1045 struct rpc_pipe_client
*pipe_hnd
;
1047 SMBCSRV
*ipc_srv
=NULL
;
1050 * See if we've already created this special connection. Reference
1051 * our "special" share name '*IPC$', which is an impossible real share
1052 * name due to the leading asterisk.
1054 ipc_srv
= find_server(ctx
, context
, server
, "*IPC$",
1055 pp_workgroup
, pp_username
, pp_password
);
1058 /* We didn't find a cached connection. Get the password */
1059 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
1060 /* ... then retrieve it now. */
1061 call_auth_fn(ctx
, context
, server
, share
,
1062 pp_workgroup
, pp_username
, pp_password
);
1063 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
1070 if (context
->flags
& SMB_CTX_FLAG_USE_KERBEROS
) {
1071 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1075 nt_status
= cli_full_connection(&ipc_cli
,
1076 global_myname(), server
,
1077 &ss
, 0, "IPC$", "?????",
1083 if (! NT_STATUS_IS_OK(nt_status
)) {
1084 DEBUG(1,("cli_full_connection failed! (%s)\n",
1085 nt_errstr(nt_status
)));
1090 if (context
->internal
->_smb_encryption_level
) {
1091 /* Attempt UNIX smb encryption. */
1092 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli
,
1098 * context->internal->_smb_encryption_level == 1
1099 * means don't fail if encryption can't be negotiated,
1100 * == 2 means fail if encryption can't be negotiated.
1103 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
1105 if (context
->internal
->_smb_encryption_level
== 2) {
1106 cli_shutdown(ipc_cli
);
1111 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
1114 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
1117 cli_shutdown(ipc_cli
);
1121 ZERO_STRUCTP(ipc_srv
);
1122 ipc_srv
->cli
= ipc_cli
;
1124 pipe_hnd
= cli_rpc_pipe_open_noauth(ipc_srv
->cli
,
1128 DEBUG(1, ("cli_nt_session_open fail!\n"));
1130 cli_shutdown(ipc_srv
->cli
);
1136 * Some systems don't support
1137 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1138 * so we might as well do it too.
1141 nt_status
= rpccli_lsa_open_policy(
1145 GENERIC_EXECUTE_ACCESS
,
1148 if (!NT_STATUS_IS_OK(nt_status
)) {
1149 errno
= smbc_errno(context
, ipc_srv
->cli
);
1150 cli_shutdown(ipc_srv
->cli
);
1154 /* now add it to the cache (internal or external) */
1156 errno
= 0; /* let cache function set errno if it likes */
1157 if ((context
->callbacks
.add_cached_srv_fn
)(context
, ipc_srv
,
1162 DEBUG(3, (" Failed to add server to cache\n"));
1166 cli_shutdown(ipc_srv
->cli
);
1171 DLIST_ADD(context
->internal
->_servers
, ipc_srv
);
1178 * Routine to open() a file ...
1182 smbc_open_ctx(SMBCCTX
*context
,
1187 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
, *workgroup
= NULL
;
1189 char *targetpath
= NULL
;
1190 struct cli_state
*targetcli
= NULL
;
1191 SMBCSRV
*srv
= NULL
;
1192 SMBCFILE
*file
= NULL
;
1194 TALLOC_CTX
*frame
= talloc_stackframe();
1196 if (!context
|| !context
->internal
||
1197 !context
->internal
->_initialized
) {
1199 errno
= EINVAL
; /* Best I can think of ... */
1213 if (smbc_parse_path(frame
,
1228 if (!user
|| user
[0] == (char)0) {
1229 user
= talloc_strdup(frame
, context
->user
);
1237 srv
= smbc_server(frame
, context
, True
,
1238 server
, share
, &workgroup
, &user
, &password
);
1241 if (errno
== EPERM
) errno
= EACCES
;
1243 return NULL
; /* smbc_server sets errno */
1246 /* Hmmm, the test for a directory is suspect here ... FIXME */
1248 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
1251 file
= SMB_MALLOC_P(SMBCFILE
);
1261 /*d_printf(">>>open: resolving %s\n", path);*/
1262 if (!cli_resolve_path(frame
, "", srv
->cli
, path
, &targetcli
, &targetpath
)) {
1263 d_printf("Could not resolve %s\n", path
);
1268 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1270 if ((fd
= cli_open(targetcli
, targetpath
, flags
,
1271 context
->internal
->_share_mode
)) < 0) {
1273 /* Handle the error ... */
1276 errno
= smbc_errno(context
, targetcli
);
1282 /* Fill in file struct */
1285 file
->fname
= SMB_STRDUP(fname
);
1290 DLIST_ADD(context
->internal
->_files
, file
);
1293 * If the file was opened in O_APPEND mode, all write
1294 * operations should be appended to the file. To do that,
1295 * though, using this protocol, would require a getattrE()
1296 * call for each and every write, to determine where the end
1297 * of the file is. (There does not appear to be an append flag
1298 * in the protocol.) Rather than add all of that overhead of
1299 * retrieving the current end-of-file offset prior to each
1300 * write operation, we'll assume that most append operations
1301 * will continuously write, so we'll just set the offset to
1302 * the end of the file now and hope that's adequate.
1304 * Note to self: If this proves inadequate, and O_APPEND
1305 * should, in some cases, be forced for each write, add a
1306 * field in the context options structure, for
1307 * "strict_append_mode" which would select between the current
1308 * behavior (if FALSE) or issuing a getattrE() prior to each
1309 * write and forcing the write to the end of the file (if
1310 * TRUE). Adding that capability will likely require adding
1311 * an "append" flag into the _SMBCFILE structure to track
1312 * whether a file was opened in O_APPEND mode. -- djl
1314 if (flags
& O_APPEND
) {
1315 if (smbc_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
1316 (void) smbc_close_ctx(context
, file
);
1328 /* Check if opendir needed ... */
1333 eno
= smbc_errno(context
, srv
->cli
);
1334 file
= (context
->opendir
)(context
, fname
);
1335 if (!file
) errno
= eno
;
1341 errno
= EINVAL
; /* FIXME, correct errno ? */
1348 * Routine to create a file
1351 static int creat_bits
= O_WRONLY
| O_CREAT
| O_TRUNC
; /* FIXME: Do we need this */
1354 smbc_creat_ctx(SMBCCTX
*context
,
1359 if (!context
|| !context
->internal
||
1360 !context
->internal
->_initialized
) {
1367 return smbc_open_ctx(context
, path
, creat_bits
, mode
);
1371 * Routine to read() a file ...
1375 smbc_read_ctx(SMBCCTX
*context
,
1381 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
1383 char *targetpath
= NULL
;
1384 struct cli_state
*targetcli
= NULL
;
1385 TALLOC_CTX
*frame
= talloc_stackframe();
1390 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1391 * appears to pass file->offset (which is type off_t) differently than
1392 * a local variable of type off_t. Using local variable "offset" in
1393 * the call to cli_read() instead of file->offset fixes a problem
1394 * retrieving data at an offset greater than 4GB.
1398 if (!context
|| !context
->internal
||
1399 !context
->internal
->_initialized
) {
1406 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
1408 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1415 offset
= file
->offset
;
1417 /* Check that the buffer exists ... */
1426 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1427 if (smbc_parse_path(frame
,
1442 /*d_printf(">>>read: resolving %s\n", path);*/
1443 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
1444 &targetcli
, &targetpath
)) {
1445 d_printf("Could not resolve %s\n", path
);
1449 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1451 ret
= cli_read(targetcli
, file
->cli_fd
, (char *)buf
, offset
, count
);
1455 errno
= smbc_errno(context
, targetcli
);
1461 file
->offset
+= ret
;
1463 DEBUG(4, (" --> %d\n", ret
));
1466 return ret
; /* Success, ret bytes of data ... */
1471 * Routine to write() a file ...
1475 smbc_write_ctx(SMBCCTX
*context
,
1482 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
1484 char *targetpath
= NULL
;
1485 struct cli_state
*targetcli
= NULL
;
1486 TALLOC_CTX
*frame
= talloc_stackframe();
1488 /* First check all pointers before dereferencing them */
1490 if (!context
|| !context
->internal
||
1491 !context
->internal
->_initialized
) {
1498 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1504 /* Check that the buffer exists ... */
1513 offset
= file
->offset
; /* See "offset" comment in smbc_read_ctx() */
1515 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1516 if (smbc_parse_path(frame
,
1531 /*d_printf(">>>write: resolving %s\n", path);*/
1532 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
1533 &targetcli
, &targetpath
)) {
1534 d_printf("Could not resolve %s\n", path
);
1538 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1540 ret
= cli_write(targetcli
, file
->cli_fd
, 0, (char *)buf
, offset
, count
);
1543 errno
= smbc_errno(context
, targetcli
);
1549 file
->offset
+= ret
;
1552 return ret
; /* Success, 0 bytes of data ... */
1556 * Routine to close() a file ...
1560 smbc_close_ctx(SMBCCTX
*context
,
1564 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
1566 char *targetpath
= NULL
;
1567 struct cli_state
*targetcli
= NULL
;
1568 TALLOC_CTX
*frame
= talloc_stackframe();
1570 if (!context
|| !context
->internal
||
1571 !context
->internal
->_initialized
) {
1578 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
1587 return (context
->closedir
)(context
, file
);
1590 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1591 if (smbc_parse_path(frame
,
1606 /*d_printf(">>>close: resolving %s\n", path);*/
1607 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
1608 &targetcli
, &targetpath
)) {
1609 d_printf("Could not resolve %s\n", path
);
1613 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1615 if (!cli_close(targetcli
, file
->cli_fd
)) {
1617 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1619 /* Deallocate slot and remove the server
1620 * from the server cache if unused */
1621 errno
= smbc_errno(context
, targetcli
);
1623 DLIST_REMOVE(context
->internal
->_files
, file
);
1624 SAFE_FREE(file
->fname
);
1626 (context
->callbacks
.remove_unused_server_fn
)(context
, srv
);
1632 DLIST_REMOVE(context
->internal
->_files
, file
);
1633 SAFE_FREE(file
->fname
);
1641 * Get info from an SMB server on a file. Use a qpathinfo call first
1642 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1645 smbc_getatr(SMBCCTX
* context
,
1650 struct timespec
*create_time_ts
,
1651 struct timespec
*access_time_ts
,
1652 struct timespec
*write_time_ts
,
1653 struct timespec
*change_time_ts
,
1656 char *fixedpath
= NULL
;
1657 char *targetpath
= NULL
;
1658 struct cli_state
*targetcli
= NULL
;
1660 TALLOC_CTX
*frame
= talloc_stackframe();
1662 if (!context
|| !context
->internal
||
1663 !context
->internal
->_initialized
) {
1669 /* path fixup for . and .. */
1670 if (strequal(path
, ".") || strequal(path
, "..")) {
1671 fixedpath
= talloc_strdup(frame
, "\\");
1678 fixedpath
= talloc_strdup(frame
, path
);
1684 trim_string(fixedpath
, NULL
, "\\..");
1685 trim_string(fixedpath
, NULL
, "\\.");
1687 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1689 if (!cli_resolve_path(frame
, "", srv
->cli
, fixedpath
,
1690 &targetcli
, &targetpath
)) {
1691 d_printf("Couldn't resolve %s\n", path
);
1696 if (!srv
->no_pathinfo2
&&
1697 cli_qpathinfo2(targetcli
, targetpath
,
1707 /* if this is NT then don't bother with the getatr */
1708 if (targetcli
->capabilities
& CAP_NT_SMBS
) {
1714 if (cli_getatr(targetcli
, targetpath
, mode
, size
, &write_time
)) {
1716 struct timespec w_time_ts
;
1718 w_time_ts
= convert_time_t_to_timespec(write_time
);
1720 if (write_time_ts
!= NULL
) {
1721 *write_time_ts
= w_time_ts
;
1724 if (create_time_ts
!= NULL
) {
1725 *create_time_ts
= w_time_ts
;
1728 if (access_time_ts
!= NULL
) {
1729 *access_time_ts
= w_time_ts
;
1732 if (change_time_ts
!= NULL
) {
1733 *change_time_ts
= w_time_ts
;
1736 srv
->no_pathinfo2
= True
;
1748 * Set file info on an SMB server. Use setpathinfo call first. If that
1749 * fails, use setattrE..
1751 * Access and modification time parameters are always used and must be
1752 * provided. Create time, if zero, will be determined from the actual create
1753 * time of the file. If non-zero, the create time will be set as well.
1755 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1758 smbc_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
1767 TALLOC_CTX
*frame
= talloc_stackframe();
1770 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1771 * modern function for "new code" to be using, and it works given a
1772 * filename rather than requiring that the file be opened to have its
1773 * attributes manipulated.
1775 if (srv
->no_pathinfo
||
1776 ! cli_setpathinfo(srv
->cli
, path
,
1784 * setpathinfo is not supported; go to plan B.
1786 * cli_setatr() does not work on win98, and it also doesn't
1787 * support setting the access time (only the modification
1788 * time), so in all cases, we open the specified file and use
1789 * cli_setattrE() which should work on all OS versions, and
1790 * supports both times.
1793 /* Don't try {q,set}pathinfo() again, with this server */
1794 srv
->no_pathinfo
= True
;
1797 if ((fd
= cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
)) < 0) {
1799 errno
= smbc_errno(context
, srv
->cli
);
1804 /* Set the new attributes */
1805 ret
= cli_setattrE(srv
->cli
, fd
,
1810 /* Close the file */
1811 cli_close(srv
->cli
, fd
);
1814 * Unfortunately, setattrE() doesn't have a provision for
1815 * setting the access mode (attributes). We'll have to try
1816 * cli_setatr() for that, and with only this parameter, it
1817 * seems to work on win98.
1819 if (ret
&& mode
!= (uint16
) -1) {
1820 ret
= cli_setatr(srv
->cli
, path
, mode
, 0);
1824 errno
= smbc_errno(context
, srv
->cli
);
1835 * Routine to unlink() a file
1839 smbc_unlink_ctx(SMBCCTX
*context
,
1842 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
, *workgroup
= NULL
;
1844 char *targetpath
= NULL
;
1845 struct cli_state
*targetcli
= NULL
;
1846 SMBCSRV
*srv
= NULL
;
1847 TALLOC_CTX
*frame
= talloc_stackframe();
1849 if (!context
|| !context
->internal
||
1850 !context
->internal
->_initialized
) {
1851 errno
= EINVAL
; /* Best I can think of ... */
1864 if (smbc_parse_path(frame
,
1879 if (!user
|| user
[0] == (char)0) {
1880 user
= talloc_strdup(frame
, context
->user
);
1888 srv
= smbc_server(frame
, context
, True
,
1889 server
, share
, &workgroup
, &user
, &password
);
1893 return -1; /* smbc_server sets errno */
1897 /*d_printf(">>>unlink: resolving %s\n", path);*/
1898 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
1899 &targetcli
, &targetpath
)) {
1900 d_printf("Could not resolve %s\n", path
);
1904 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1906 if (!cli_unlink(targetcli
, targetpath
)) {
1908 errno
= smbc_errno(context
, targetcli
);
1910 if (errno
== EACCES
) { /* Check if the file is a directory */
1915 struct timespec write_time_ts
;
1916 struct timespec access_time_ts
;
1917 struct timespec change_time_ts
;
1920 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
1927 /* Hmmm, bad error ... What? */
1929 errno
= smbc_errno(context
, targetcli
);
1936 if (IS_DOS_DIR(mode
))
1939 errno
= saverr
; /* Restore this */
1950 return 0; /* Success ... */
1955 * Routine to rename() a file
1959 smbc_rename_ctx(SMBCCTX
*ocontext
,
1964 char *server1
= NULL
;
1965 char *share1
= NULL
;
1966 char *server2
= NULL
;
1967 char *share2
= NULL
;
1970 char *password1
= NULL
;
1971 char *password2
= NULL
;
1972 char *workgroup
= NULL
;
1975 char *targetpath1
= NULL
;
1976 char *targetpath2
= NULL
;
1977 struct cli_state
*targetcli1
= NULL
;
1978 struct cli_state
*targetcli2
= NULL
;
1979 SMBCSRV
*srv
= NULL
;
1980 TALLOC_CTX
*frame
= talloc_stackframe();
1982 if (!ocontext
|| !ncontext
||
1983 !ocontext
->internal
|| !ncontext
->internal
||
1984 !ocontext
->internal
->_initialized
||
1985 !ncontext
->internal
->_initialized
) {
1986 errno
= EINVAL
; /* Best I can think of ... */
1991 if (!oname
|| !nname
) {
1997 DEBUG(4, ("smbc_rename(%s,%s)\n", oname
, nname
));
1999 if (smbc_parse_path(frame
,
2014 if (!user1
|| user1
[0] == (char)0) {
2015 user1
= talloc_strdup(frame
, ocontext
->user
);
2023 if (smbc_parse_path(frame
,
2038 if (!user2
|| user2
[0] == (char)0) {
2039 user2
= talloc_strdup(frame
, ncontext
->user
);
2047 if (strcmp(server1
, server2
) || strcmp(share1
, share2
) ||
2048 strcmp(user1
, user2
)) {
2049 /* Can't rename across file systems, or users?? */
2055 srv
= smbc_server(frame
, ocontext
, True
,
2056 server1
, share1
, &workgroup
, &user1
, &password1
);
2063 /*d_printf(">>>rename: resolving %s\n", path1);*/
2064 if (!cli_resolve_path(frame
, "", srv
->cli
, path1
,
2065 &targetcli1
, &targetpath1
)) {
2066 d_printf("Could not resolve %s\n", path1
);
2070 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2071 /*d_printf(">>>rename: resolving %s\n", path2);*/
2072 if (!cli_resolve_path(frame
, "", srv
->cli
, path2
,
2073 &targetcli2
, &targetpath2
)) {
2074 d_printf("Could not resolve %s\n", path2
);
2078 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2080 if (strcmp(targetcli1
->desthost
, targetcli2
->desthost
) ||
2081 strcmp(targetcli1
->share
, targetcli2
->share
))
2083 /* can't rename across file systems */
2089 if (!cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
2090 int eno
= smbc_errno(ocontext
, targetcli1
);
2092 if (eno
!= EEXIST
||
2093 !cli_unlink(targetcli1
, targetpath2
) ||
2094 !cli_rename(targetcli1
, targetpath1
, targetpath2
)) {
2104 return 0; /* Success */
2108 * A routine to lseek() a file
2112 smbc_lseek_ctx(SMBCCTX
*context
,
2118 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
2120 char *targetpath
= NULL
;
2121 struct cli_state
*targetcli
= NULL
;
2122 TALLOC_CTX
*frame
= talloc_stackframe();
2124 if (!context
|| !context
->internal
||
2125 !context
->internal
->_initialized
) {
2131 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
2143 return -1; /* Can't lseek a dir ... */
2149 file
->offset
= offset
;
2153 file
->offset
+= offset
;
2157 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2158 if (smbc_parse_path(frame
,
2173 /*d_printf(">>>lseek: resolving %s\n", path);*/
2174 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
2175 &targetcli
, &targetpath
)) {
2176 d_printf("Could not resolve %s\n", path
);
2180 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2182 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, NULL
,
2183 &size
, NULL
, NULL
, NULL
, NULL
, NULL
))
2185 SMB_OFF_T b_size
= size
;
2186 if (!cli_getattrE(targetcli
, file
->cli_fd
,
2187 NULL
, &b_size
, NULL
, NULL
, NULL
))
2195 file
->offset
= size
+ offset
;
2205 return file
->offset
;
2210 * Generate an inode number from file name for those things that need it
2214 smbc_inode(SMBCCTX
*context
,
2217 if (!context
|| !context
->internal
||
2218 !context
->internal
->_initialized
) {
2225 if (!*name
) return 2; /* FIXME, why 2 ??? */
2226 return (ino_t
)str_checksum(name
);
2231 * Routine to put basic stat info into a stat structure ... Used by stat and
2236 smbc_setup_stat(SMBCCTX
*context
,
2242 TALLOC_CTX
*frame
= talloc_stackframe();
2246 if (IS_DOS_DIR(mode
)) {
2247 st
->st_mode
= SMBC_DIR_MODE
;
2249 st
->st_mode
= SMBC_FILE_MODE
;
2252 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
2253 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
2254 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
2255 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
2258 #ifdef HAVE_STAT_ST_BLKSIZE
2259 st
->st_blksize
= 512;
2261 #ifdef HAVE_STAT_ST_BLOCKS
2262 st
->st_blocks
= (size
+511)/512;
2264 #ifdef HAVE_STRUCT_STAT_ST_RDEV
2267 st
->st_uid
= getuid();
2268 st
->st_gid
= getgid();
2270 if (IS_DOS_DIR(mode
)) {
2276 if (st
->st_ino
== 0) {
2277 st
->st_ino
= smbc_inode(context
, fname
);
2281 return True
; /* FIXME: Is this needed ? */
2286 * Routine to stat a file given a name
2290 smbc_stat_ctx(SMBCCTX
*context
,
2294 SMBCSRV
*srv
= NULL
;
2295 char *server
= NULL
;
2298 char *password
= NULL
;
2299 char *workgroup
= NULL
;
2301 struct timespec write_time_ts
;
2302 struct timespec access_time_ts
;
2303 struct timespec change_time_ts
;
2307 TALLOC_CTX
*frame
= talloc_stackframe();
2309 if (!context
|| !context
->internal
||
2310 !context
->internal
->_initialized
) {
2312 errno
= EINVAL
; /* Best I can think of ... */
2323 DEBUG(4, ("smbc_stat(%s)\n", fname
));
2325 if (smbc_parse_path(frame
,
2340 if (!user
|| user
[0] == (char)0) {
2341 user
= talloc_strdup(frame
,context
->user
);
2349 srv
= smbc_server(frame
, context
, True
,
2350 server
, share
, &workgroup
, &user
, &password
);
2354 return -1; /* errno set by smbc_server */
2357 if (!smbc_getatr(context
, srv
, path
, &mode
, &size
,
2363 errno
= smbc_errno(context
, srv
->cli
);
2370 smbc_setup_stat(context
, st
, (char *) fname
, size
, mode
);
2372 set_atimespec(st
, access_time_ts
);
2373 set_ctimespec(st
, change_time_ts
);
2374 set_mtimespec(st
, write_time_ts
);
2375 st
->st_dev
= srv
->dev
;
2383 * Routine to stat a file given an fd
2387 smbc_fstat_ctx(SMBCCTX
*context
,
2391 struct timespec change_time_ts
;
2392 struct timespec access_time_ts
;
2393 struct timespec write_time_ts
;
2396 char *server
= NULL
;
2399 char *password
= NULL
;
2401 char *targetpath
= NULL
;
2402 struct cli_state
*targetcli
= NULL
;
2404 TALLOC_CTX
*frame
= talloc_stackframe();
2406 if (!context
|| !context
->internal
||
2407 !context
->internal
->_initialized
) {
2413 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
2421 return (context
->fstatdir
)(context
, file
, st
);
2424 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2425 if (smbc_parse_path(frame
,
2440 /*d_printf(">>>fstat: resolving %s\n", path);*/
2441 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
2442 &targetcli
, &targetpath
)) {
2443 d_printf("Could not resolve %s\n", path
);
2447 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2449 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, &mode
, &size
,
2456 time_t change_time
, access_time
, write_time
;
2458 if (!cli_getattrE(targetcli
, file
->cli_fd
, &mode
, &size
,
2459 &change_time
, &access_time
, &write_time
)) {
2466 change_time_ts
= convert_time_t_to_timespec(change_time
);
2467 access_time_ts
= convert_time_t_to_timespec(access_time
);
2468 write_time_ts
= convert_time_t_to_timespec(write_time
);
2473 smbc_setup_stat(context
, st
, file
->fname
, size
, mode
);
2475 set_atimespec(st
, access_time_ts
);
2476 set_ctimespec(st
, change_time_ts
);
2477 set_mtimespec(st
, write_time_ts
);
2478 st
->st_dev
= file
->srv
->dev
;
2486 * Routine to truncate a file given by its file descriptor, to a specified size
2490 smbc_ftruncate_ctx(SMBCCTX
*context
,
2494 SMB_OFF_T size
= length
;
2495 char *server
= NULL
;
2498 char *password
= NULL
;
2500 char *targetpath
= NULL
;
2501 struct cli_state
*targetcli
= NULL
;
2502 TALLOC_CTX
*frame
= talloc_stackframe();
2504 if (!context
|| !context
->internal
||
2505 !context
->internal
->_initialized
) {
2511 if (!file
|| !DLIST_CONTAINS(context
->internal
->_files
, file
)) {
2523 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
2524 if (smbc_parse_path(frame
,
2539 /*d_printf(">>>fstat: resolving %s\n", path);*/
2540 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
2541 &targetcli
, &targetpath
)) {
2542 d_printf("Could not resolve %s\n", path
);
2546 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
2548 if (!cli_ftruncate(targetcli
, file
->cli_fd
, size
)) {
2560 * Routine to open a directory
2561 * We accept the URL syntax explained in smbc_parse_path(), above.
2565 smbc_remove_dir(SMBCFILE
*dir
)
2567 struct smbc_dir_list
*d
,*f
;
2574 SAFE_FREE(f
->dirent
);
2579 dir
->dir_list
= dir
->dir_end
= dir
->dir_next
= NULL
;
2584 add_dirent(SMBCFILE
*dir
,
2586 const char *comment
,
2589 struct smbc_dirent
*dirent
;
2591 int name_length
= (name
== NULL
? 0 : strlen(name
));
2592 int comment_len
= (comment
== NULL
? 0 : strlen(comment
));
2595 * Allocate space for the dirent, which must be increased by the
2596 * size of the name and the comment and 1 each for the null terminator.
2599 size
= sizeof(struct smbc_dirent
) + name_length
+ comment_len
+ 2;
2601 dirent
= (struct smbc_dirent
*)SMB_MALLOC(size
);
2605 dir
->dir_error
= ENOMEM
;
2610 ZERO_STRUCTP(dirent
);
2612 if (dir
->dir_list
== NULL
) {
2614 dir
->dir_list
= SMB_MALLOC_P(struct smbc_dir_list
);
2615 if (!dir
->dir_list
) {
2618 dir
->dir_error
= ENOMEM
;
2622 ZERO_STRUCTP(dir
->dir_list
);
2624 dir
->dir_end
= dir
->dir_next
= dir
->dir_list
;
2628 dir
->dir_end
->next
= SMB_MALLOC_P(struct smbc_dir_list
);
2630 if (!dir
->dir_end
->next
) {
2633 dir
->dir_error
= ENOMEM
;
2637 ZERO_STRUCTP(dir
->dir_end
->next
);
2639 dir
->dir_end
= dir
->dir_end
->next
;
2642 dir
->dir_end
->next
= NULL
;
2643 dir
->dir_end
->dirent
= dirent
;
2645 dirent
->smbc_type
= type
;
2646 dirent
->namelen
= name_length
;
2647 dirent
->commentlen
= comment_len
;
2648 dirent
->dirlen
= size
;
2651 * dirent->namelen + 1 includes the null (no null termination needed)
2652 * Ditto for dirent->commentlen.
2653 * The space for the two null bytes was allocated.
2655 strncpy(dirent
->name
, (name
?name
:""), dirent
->namelen
+ 1);
2656 dirent
->comment
= (char *)(&dirent
->name
+ dirent
->namelen
+ 1);
2657 strncpy(dirent
->comment
, (comment
?comment
:""), dirent
->commentlen
+ 1);
2664 list_unique_wg_fn(const char *name
,
2666 const char *comment
,
2669 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2670 struct smbc_dir_list
*dir_list
;
2671 struct smbc_dirent
*dirent
;
2675 dirent_type
= dir
->dir_type
;
2677 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2679 /* An error occurred, what do we do? */
2680 /* FIXME: Add some code here */
2683 /* Point to the one just added */
2684 dirent
= dir
->dir_end
->dirent
;
2686 /* See if this was a duplicate */
2687 for (dir_list
= dir
->dir_list
;
2688 dir_list
!= dir
->dir_end
;
2689 dir_list
= dir_list
->next
) {
2691 strcmp(dir_list
->dirent
->name
, dirent
->name
) == 0) {
2692 /* Duplicate. End end of list need to be removed. */
2696 if (do_remove
&& dir_list
->next
== dir
->dir_end
) {
2697 /* Found the end of the list. Remove it. */
2698 dir
->dir_end
= dir_list
;
2699 free(dir_list
->next
);
2701 dir_list
->next
= NULL
;
2708 list_fn(const char *name
,
2710 const char *comment
,
2713 SMBCFILE
*dir
= (SMBCFILE
*)state
;
2717 * We need to process the type a little ...
2719 * Disk share = 0x00000000
2720 * Print share = 0x00000001
2721 * Comms share = 0x00000002 (obsolete?)
2722 * IPC$ share = 0x00000003
2724 * administrative shares:
2725 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2728 if (dir
->dir_type
== SMBC_FILE_SHARE
) {
2730 case 0 | 0x80000000:
2732 dirent_type
= SMBC_FILE_SHARE
;
2736 dirent_type
= SMBC_PRINTER_SHARE
;
2740 dirent_type
= SMBC_COMMS_SHARE
;
2743 case 3 | 0x80000000:
2745 dirent_type
= SMBC_IPC_SHARE
;
2749 dirent_type
= SMBC_FILE_SHARE
; /* FIXME, error? */
2754 dirent_type
= dir
->dir_type
;
2757 if (add_dirent(dir
, name
, comment
, dirent_type
) < 0) {
2759 /* An error occurred, what do we do? */
2760 /* FIXME: Add some code here */
2766 dir_list_fn(const char *mnt
,
2772 if (add_dirent((SMBCFILE
*)state
, finfo
->name
, "",
2773 (finfo
->mode
&aDIR
?SMBC_DIR
:SMBC_FILE
)) < 0) {
2775 /* Handle an error ... */
2777 /* FIXME: Add some code ... */
2784 net_share_enum_rpc(struct cli_state
*cli
,
2785 void (*fn
)(const char *name
,
2787 const char *comment
,
2794 uint32 info_level
= 1;
2795 uint32 preferred_len
= 0xffffffff;
2797 SRV_SHARE_INFO_CTR ctr
;
2799 fstring comment
= "";
2800 struct rpc_pipe_client
*pipe_hnd
;
2803 /* Open the server service pipe */
2804 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SRVSVC
, &nt_status
);
2806 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2810 /* Issue the NetShareEnum RPC call and retrieve the response */
2811 init_enum_hnd(&enum_hnd
, 0);
2812 result
= rpccli_srvsvc_net_share_enum(pipe_hnd
,
2819 /* Was it successful? */
2820 if (!W_ERROR_IS_OK(result
) || ctr
.num_entries
== 0) {
2821 /* Nope. Go clean up. */
2825 /* For each returned entry... */
2826 for (i
= 0; i
< ctr
.num_entries
; i
++) {
2828 /* pull out the share name */
2829 rpcstr_pull_unistr2_fstring(
2830 name
, &ctr
.share
.info1
[i
].info_1_str
.uni_netname
);
2832 /* pull out the share's comment */
2833 rpcstr_pull_unistr2_fstring(
2834 comment
, &ctr
.share
.info1
[i
].info_1_str
.uni_remark
);
2836 /* Get the type value */
2837 type
= ctr
.share
.info1
[i
].info_1
.type
;
2839 /* Add this share to the list */
2840 (*fn
)(name
, type
, comment
, state
);
2844 /* Close the server service pipe */
2845 cli_rpc_pipe_close(pipe_hnd
);
2847 /* Tell 'em if it worked */
2848 return W_ERROR_IS_OK(result
) ? 0 : -1;
2854 smbc_opendir_ctx(SMBCCTX
*context
,
2858 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
, *options
= NULL
;
2859 char *workgroup
= NULL
;
2863 SMBCSRV
*srv
= NULL
;
2864 SMBCFILE
*dir
= NULL
;
2865 struct _smbc_callbacks
*cb
= NULL
;
2866 struct sockaddr_storage rem_ss
;
2867 TALLOC_CTX
*frame
= talloc_stackframe();
2869 if (!context
|| !context
->internal
||
2870 !context
->internal
->_initialized
) {
2871 DEBUG(4, ("no valid context\n"));
2872 errno
= EINVAL
+ 8192;
2879 DEBUG(4, ("no valid fname\n"));
2880 errno
= EINVAL
+ 8193;
2885 if (smbc_parse_path(frame
,
2895 DEBUG(4, ("no valid path\n"));
2896 errno
= EINVAL
+ 8194;
2901 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2902 "path='%s' options='%s'\n",
2903 fname
, server
, share
, path
, options
));
2905 /* Ensure the options are valid */
2906 if (smbc_check_options(server
, share
, path
, options
)) {
2907 DEBUG(4, ("unacceptable options (%s)\n", options
));
2908 errno
= EINVAL
+ 8195;
2913 if (!user
|| user
[0] == (char)0) {
2914 user
= talloc_strdup(frame
, context
->user
);
2922 dir
= SMB_MALLOC_P(SMBCFILE
);
2933 dir
->fname
= SMB_STRDUP(fname
);
2937 dir
->dir_list
= dir
->dir_next
= dir
->dir_end
= NULL
;
2939 if (server
[0] == (char)0) {
2944 struct ip_service
*ip_list
;
2945 struct ip_service server_addr
;
2946 struct user_auth_info u_info
;
2948 if (share
[0] != (char)0 || path
[0] != (char)0) {
2950 errno
= EINVAL
+ 8196;
2952 SAFE_FREE(dir
->fname
);
2959 /* Determine how many local master browsers to query */
2960 max_lmb_count
= (context
->options
.browse_max_lmb_count
== 0
2962 : context
->options
.browse_max_lmb_count
);
2964 memset(&u_info
, '\0', sizeof(u_info
));
2965 u_info
.username
= talloc_strdup(frame
,user
);
2966 u_info
.password
= talloc_strdup(frame
,password
);
2967 if (!u_info
.username
|| !u_info
.password
) {
2969 SAFE_FREE(dir
->fname
);
2977 * We have server and share and path empty but options
2978 * requesting that we scan all master browsers for their list
2979 * of workgroups/domains. This implies that we must first try
2980 * broadcast queries to find all master browsers, and if that
2981 * doesn't work, then try our other methods which return only
2982 * a single master browser.
2986 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
2992 if (!find_master_ip(workgroup
, &server_addr
.ss
)) {
2995 SAFE_FREE(dir
->fname
);
3003 ip_list
= (struct ip_service
*)memdup(
3004 &server_addr
, sizeof(server_addr
));
3005 if (ip_list
== NULL
) {
3013 for (i
= 0; i
< count
&& i
< max_lmb_count
; i
++) {
3014 char addr
[INET6_ADDRSTRLEN
];
3015 char *wg_ptr
= NULL
;
3016 struct cli_state
*cli
= NULL
;
3018 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
3019 DEBUG(99, ("Found master browser %d of %d: %s\n",
3020 i
+1, MAX(count
, max_lmb_count
),
3023 cli
= get_ipc_connect_master_ip(talloc_tos(),
3027 /* cli == NULL is the master browser refused to talk or
3028 could not be found */
3033 workgroup
= talloc_strdup(frame
, wg_ptr
);
3034 server
= talloc_strdup(frame
, cli
->desthost
);
3038 if (!workgroup
|| !server
) {
3044 DEBUG(4, ("using workgroup %s %s\n",
3045 workgroup
, server
));
3048 * For each returned master browser IP address, get a
3049 * connection to IPC$ on the server if we do not
3050 * already have one, and determine the
3051 * workgroups/domains that it knows about.
3054 srv
= smbc_server(frame
, context
, True
, server
, "IPC$",
3055 &workgroup
, &user
, &password
);
3061 dir
->dir_type
= SMBC_WORKGROUP
;
3063 /* Now, list the stuff ... */
3065 if (!cli_NetServerEnum(srv
->cli
,
3067 SV_TYPE_DOMAIN_ENUM
,
3077 * Server not an empty string ... Check the rest and see what
3080 if (*share
== '\0') {
3081 if (*path
!= '\0') {
3083 /* Should not have empty share with path */
3084 errno
= EINVAL
+ 8197;
3086 SAFE_FREE(dir
->fname
);
3095 * We don't know if <server> is really a server name
3096 * or is a workgroup/domain name. If we already have
3097 * a server structure for it, we'll use it.
3098 * Otherwise, check to see if <server><1D>,
3099 * <server><1B>, or <server><20> translates. We check
3100 * to see if <server> is an IP address first.
3104 * See if we have an existing server. Do not
3105 * establish a connection if one does not already
3108 srv
= smbc_server(frame
, context
, False
, server
, "IPC$",
3109 &workgroup
, &user
, &password
);
3112 * If no existing server and not an IP addr, look for
3116 !is_ipaddress(server
) &&
3117 (resolve_name(server
, &rem_ss
, 0x1d) || /* LMB */
3118 resolve_name(server
, &rem_ss
, 0x1b) )) { /* DMB */
3122 dir
->dir_type
= SMBC_SERVER
;
3125 * Get the backup list ...
3127 if (!name_status_find(server
, 0, 0,
3128 &rem_ss
, buserver
)) {
3130 DEBUG(0, ("Could not get name of "
3131 "local/domain master browser "
3132 "for server %s\n", server
));
3134 SAFE_FREE(dir
->fname
);
3144 * Get a connection to IPC$ on the server if
3145 * we do not already have one
3147 srv
= smbc_server(frame
, context
, True
,
3149 &workgroup
, &user
, &password
);
3151 DEBUG(0, ("got no contact to IPC$\n"));
3153 SAFE_FREE(dir
->fname
);
3163 /* Now, list the servers ... */
3164 if (!cli_NetServerEnum(srv
->cli
, server
,
3165 0x0000FFFE, list_fn
,
3169 SAFE_FREE(dir
->fname
);
3176 (resolve_name(server
, &rem_ss
, 0x20))) {
3178 /* If we hadn't found the server, get one now */
3180 srv
= smbc_server(frame
, context
, True
,
3188 SAFE_FREE(dir
->fname
);
3196 dir
->dir_type
= SMBC_FILE_SHARE
;
3199 /* List the shares ... */
3201 if (net_share_enum_rpc(
3204 (void *) dir
) < 0 &&
3210 errno
= cli_errno(srv
->cli
);
3212 SAFE_FREE(dir
->fname
);
3220 /* Neither the workgroup nor server exists */
3221 errno
= ECONNREFUSED
;
3223 SAFE_FREE(dir
->fname
);
3233 * The server and share are specified ... work from
3237 struct cli_state
*targetcli
;
3239 /* We connect to the server and list the directory */
3240 dir
->dir_type
= SMBC_FILE_SHARE
;
3242 srv
= smbc_server(frame
, context
, True
, server
, share
,
3243 &workgroup
, &user
, &password
);
3247 SAFE_FREE(dir
->fname
);
3256 /* Now, list the files ... */
3258 p
= path
+ strlen(path
);
3259 path
= talloc_asprintf_append(path
, "\\*");
3262 SAFE_FREE(dir
->fname
);
3269 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
3270 &targetcli
, &targetpath
)) {
3271 d_printf("Could not resolve %s\n", path
);
3273 SAFE_FREE(dir
->fname
);
3280 if (cli_list(targetcli
, targetpath
,
3281 aDIR
| aSYSTEM
| aHIDDEN
,
3282 dir_list_fn
, (void *)dir
) < 0) {
3285 SAFE_FREE(dir
->fname
);
3288 saved_errno
= smbc_errno(context
, targetcli
);
3290 if (saved_errno
== EINVAL
) {
3292 * See if they asked to opendir something
3293 * other than a directory. If so, the
3294 * converted error value we got would have
3295 * been EINVAL rather than ENOTDIR.
3297 *p
= '\0'; /* restore original path */
3299 if (smbc_getatr(context
, srv
, path
,
3301 NULL
, NULL
, NULL
, NULL
,
3303 ! IS_DOS_DIR(mode
)) {
3305 /* It is. Correct the error value */
3306 saved_errno
= ENOTDIR
;
3311 * If there was an error and the server is no
3314 cb
= &context
->callbacks
;
3315 if (cli_is_error(targetcli
) &&
3316 (cb
->check_server_fn
)(context
, srv
)) {
3318 /* ... then remove it. */
3319 if ((cb
->remove_unused_server_fn
)(context
,
3322 * We could not remove the
3323 * server completely, remove
3324 * it from the cache so we
3325 * will not get it again. It
3326 * will be removed when the
3327 * last file/dir is closed.
3329 (cb
->remove_cached_srv_fn
)(context
,
3334 errno
= saved_errno
;
3342 DLIST_ADD(context
->internal
->_files
, dir
);
3349 * Routine to close a directory
3353 smbc_closedir_ctx(SMBCCTX
*context
,
3356 TALLOC_CTX
*frame
= talloc_stackframe();
3358 if (!context
|| !context
->internal
||
3359 !context
->internal
->_initialized
) {
3365 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3371 smbc_remove_dir(dir
); /* Clean it up */
3373 DLIST_REMOVE(context
->internal
->_files
, dir
);
3377 SAFE_FREE(dir
->fname
);
3378 SAFE_FREE(dir
); /* Free the space too */
3387 smbc_readdir_internal(SMBCCTX
* context
,
3388 struct smbc_dirent
*dest
,
3389 struct smbc_dirent
*src
,
3390 int max_namebuf_len
)
3392 if (context
->options
.urlencode_readdir_entries
) {
3394 /* url-encode the name. get back remaining buffer space */
3396 smbc_urlencode(dest
->name
, src
->name
, max_namebuf_len
);
3398 /* We now know the name length */
3399 dest
->namelen
= strlen(dest
->name
);
3401 /* Save the pointer to the beginning of the comment */
3402 dest
->comment
= dest
->name
+ dest
->namelen
+ 1;
3404 /* Copy the comment */
3405 strncpy(dest
->comment
, src
->comment
, max_namebuf_len
- 1);
3406 dest
->comment
[max_namebuf_len
- 1] = '\0';
3408 /* Save other fields */
3409 dest
->smbc_type
= src
->smbc_type
;
3410 dest
->commentlen
= strlen(dest
->comment
);
3411 dest
->dirlen
= ((dest
->comment
+ dest
->commentlen
+ 1) -
3415 /* No encoding. Just copy the entry as is. */
3416 memcpy(dest
, src
, src
->dirlen
);
3417 dest
->comment
= (char *)(&dest
->name
+ src
->namelen
+ 1);
3423 * Routine to get a directory entry
3426 struct smbc_dirent
*
3427 smbc_readdir_ctx(SMBCCTX
*context
,
3431 struct smbc_dirent
*dirp
, *dirent
;
3432 TALLOC_CTX
*frame
= talloc_stackframe();
3434 /* Check that all is ok first ... */
3436 if (!context
|| !context
->internal
||
3437 !context
->internal
->_initialized
) {
3440 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3446 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3449 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3455 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3458 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3464 if (!dir
->dir_next
) {
3469 dirent
= dir
->dir_next
->dirent
;
3478 dirp
= (struct smbc_dirent
*)context
->internal
->_dirent
;
3479 maxlen
= (sizeof(context
->internal
->_dirent
) -
3480 sizeof(struct smbc_dirent
));
3482 smbc_readdir_internal(context
, dirp
, dirent
, maxlen
);
3484 dir
->dir_next
= dir
->dir_next
->next
;
3491 * Routine to get directory entries
3495 smbc_getdents_ctx(SMBCCTX
*context
,
3497 struct smbc_dirent
*dirp
,
3503 char *ndir
= (char *)dirp
;
3504 struct smbc_dir_list
*dirlist
;
3505 TALLOC_CTX
*frame
= talloc_stackframe();
3507 /* Check that all is ok first ... */
3509 if (!context
|| !context
->internal
||
3510 !context
->internal
->_initialized
) {
3518 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3526 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3535 * Now, retrieve the number of entries that will fit in what was passed
3536 * We have to figure out if the info is in the list, or we need to
3537 * send a request to the server to get the info.
3540 while ((dirlist
= dir
->dir_next
)) {
3541 struct smbc_dirent
*dirent
;
3543 if (!dirlist
->dirent
) {
3545 errno
= ENOENT
; /* Bad error */
3551 /* Do urlencoding of next entry, if so selected */
3552 dirent
= (struct smbc_dirent
*)context
->internal
->_dirent
;
3553 maxlen
= (sizeof(context
->internal
->_dirent
) -
3554 sizeof(struct smbc_dirent
));
3555 smbc_readdir_internal(context
, dirent
, dirlist
->dirent
, maxlen
);
3557 reqd
= dirent
->dirlen
;
3561 if (rem
< count
) { /* We managed to copy something */
3568 else { /* Nothing copied ... */
3570 errno
= EINVAL
; /* Not enough space ... */
3578 memcpy(ndir
, dirent
, reqd
); /* Copy the data in ... */
3580 ((struct smbc_dirent
*)ndir
)->comment
=
3581 (char *)(&((struct smbc_dirent
*)ndir
)->name
+
3589 dir
->dir_next
= dirlist
= dirlist
-> next
;
3602 * Routine to create a directory ...
3606 smbc_mkdir_ctx(SMBCCTX
*context
,
3610 SMBCSRV
*srv
= NULL
;
3611 char *server
= NULL
;
3614 char *password
= NULL
;
3615 char *workgroup
= NULL
;
3617 char *targetpath
= NULL
;
3618 struct cli_state
*targetcli
= NULL
;
3619 TALLOC_CTX
*frame
= talloc_stackframe();
3621 if (!context
|| !context
->internal
||
3622 !context
->internal
->_initialized
) {
3634 DEBUG(4, ("smbc_mkdir(%s)\n", fname
));
3636 if (smbc_parse_path(frame
,
3651 if (!user
|| user
[0] == (char)0) {
3652 user
= talloc_strdup(frame
, context
->user
);
3660 srv
= smbc_server(frame
, context
, True
,
3661 server
, share
, &workgroup
, &user
, &password
);
3666 return -1; /* errno set by smbc_server */
3670 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3671 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
3672 &targetcli
, &targetpath
)) {
3673 d_printf("Could not resolve %s\n", path
);
3677 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3679 if (!cli_mkdir(targetcli
, targetpath
)) {
3681 errno
= smbc_errno(context
, targetcli
);
3693 * Our list function simply checks to see if a directory is not empty
3696 static int smbc_rmdir_dirempty
= True
;
3699 rmdir_list_fn(const char *mnt
,
3704 if (strncmp(finfo
->name
, ".", 1) != 0 &&
3705 strncmp(finfo
->name
, "..", 2) != 0) {
3706 smbc_rmdir_dirempty
= False
;
3711 * Routine to remove a directory
3715 smbc_rmdir_ctx(SMBCCTX
*context
,
3718 SMBCSRV
*srv
= NULL
;
3719 char *server
= NULL
;
3722 char *password
= NULL
;
3723 char *workgroup
= NULL
;
3725 char *targetpath
= NULL
;
3726 struct cli_state
*targetcli
= NULL
;
3727 TALLOC_CTX
*frame
= talloc_stackframe();
3729 if (!context
|| !context
->internal
||
3730 !context
->internal
->_initialized
) {
3742 DEBUG(4, ("smbc_rmdir(%s)\n", fname
));
3744 if (smbc_parse_path(frame
,
3759 if (!user
|| user
[0] == (char)0) {
3760 user
= talloc_strdup(frame
, context
->user
);
3768 srv
= smbc_server(frame
, context
, True
,
3769 server
, share
, &workgroup
, &user
, &password
);
3774 return -1; /* errno set by smbc_server */
3778 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3779 if (!cli_resolve_path(frame
, "", srv
->cli
, path
,
3780 &targetcli
, &targetpath
)) {
3781 d_printf("Could not resolve %s\n", path
);
3785 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3788 if (!cli_rmdir(targetcli
, targetpath
)) {
3790 errno
= smbc_errno(context
, targetcli
);
3792 if (errno
== EACCES
) { /* Check if the dir empty or not */
3794 /* Local storage to avoid buffer overflows */
3797 smbc_rmdir_dirempty
= True
; /* Make this so ... */
3799 lpath
= talloc_asprintf(frame
, "%s\\*",
3807 if (cli_list(targetcli
, lpath
,
3808 aDIR
| aSYSTEM
| aHIDDEN
,
3809 rmdir_list_fn
, NULL
) < 0) {
3811 /* Fix errno to ignore latest error ... */
3812 DEBUG(5, ("smbc_rmdir: "
3813 "cli_list returned an error: %d\n",
3814 smbc_errno(context
, targetcli
)));
3819 if (smbc_rmdir_dirempty
)
3837 * Routine to return the current directory position
3841 smbc_telldir_ctx(SMBCCTX
*context
,
3844 TALLOC_CTX
*frame
= talloc_stackframe();
3846 if (!context
|| !context
->internal
||
3847 !context
->internal
->_initialized
) {
3855 if (!dir
|| !DLIST_CONTAINS(context
->internal
->_files
, dir
)) {
3863 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3871 /* See if we're already at the end. */
3872 if (dir
->dir_next
== NULL
) {
3879 * We return the pointer here as the offset
3882 return (off_t
)(long)dir
->dir_next
->dirent
;
3886 * A routine to run down the list and see if the entry is OK
3889 struct smbc_dir_list
*
3890 smbc_check_dir_ent(struct smbc_dir_list
*list
,
3891 struct smbc_dirent
*dirent
)
3894 /* Run down the list looking for what we want */
3898 struct smbc_dir_list
*tmp
= list
;
3902 if (tmp
->dirent
== dirent
)
3911 return NULL
; /* Not found, or an error */
3917 * Routine to seek on a directory
3921 smbc_lseekdir_ctx(SMBCCTX
*context
,
3925 long int l_offset
= offset
; /* Handle problems of size */
3926 struct smbc_dirent
*dirent
= (struct smbc_dirent
*)l_offset
;
3927 struct smbc_dir_list
*list_ent
= (struct smbc_dir_list
*)NULL
;
3928 TALLOC_CTX
*frame
= talloc_stackframe();
3930 if (!context
|| !context
->internal
||
3931 !context
->internal
->_initialized
) {
3939 if (dir
->file
!= False
) { /* FIXME, should be dir, perhaps */
3947 /* Now, check what we were passed and see if it is OK ... */
3949 if (dirent
== NULL
) { /* Seek to the begining of the list */
3951 dir
->dir_next
= dir
->dir_list
;
3957 if (offset
== -1) { /* Seek to the end of the list */
3958 dir
->dir_next
= NULL
;
3963 /* Now, run down the list and make sure that the entry is OK */
3964 /* This may need to be changed if we change the format of the list */
3966 if ((list_ent
= smbc_check_dir_ent(dir
->dir_list
, dirent
)) == NULL
) {
3967 errno
= EINVAL
; /* Bad entry */
3972 dir
->dir_next
= list_ent
;
3979 * Routine to fstat a dir
3983 smbc_fstatdir_ctx(SMBCCTX
*context
,
3988 if (!context
|| !context
->internal
||
3989 !context
->internal
->_initialized
) {
3994 /* No code yet ... */
3999 smbc_chmod_ctx(SMBCCTX
*context
,
4003 SMBCSRV
*srv
= NULL
;
4004 char *server
= NULL
;
4007 char *password
= NULL
;
4008 char *workgroup
= NULL
;
4011 TALLOC_CTX
*frame
= talloc_stackframe();
4013 if (!context
|| !context
->internal
||
4014 !context
->internal
->_initialized
) {
4015 errno
= EINVAL
; /* Best I can think of ... */
4026 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname
, newmode
));
4028 if (smbc_parse_path(frame
,
4043 if (!user
|| user
[0] == (char)0) {
4044 user
= talloc_strdup(frame
, context
->user
);
4052 srv
= smbc_server(frame
, context
, True
,
4053 server
, share
, &workgroup
, &user
, &password
);
4057 return -1; /* errno set by smbc_server */
4062 if (!(newmode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
))) mode
|= aRONLY
;
4063 if ((newmode
& S_IXUSR
) && lp_map_archive(-1)) mode
|= aARCH
;
4064 if ((newmode
& S_IXGRP
) && lp_map_system(-1)) mode
|= aSYSTEM
;
4065 if ((newmode
& S_IXOTH
) && lp_map_hidden(-1)) mode
|= aHIDDEN
;
4067 if (!cli_setatr(srv
->cli
, path
, mode
, 0)) {
4068 errno
= smbc_errno(context
, srv
->cli
);
4078 smbc_utimes_ctx(SMBCCTX
*context
,
4080 struct timeval
*tbuf
)
4082 SMBCSRV
*srv
= NULL
;
4083 char *server
= NULL
;
4086 char *password
= NULL
;
4087 char *workgroup
= NULL
;
4091 TALLOC_CTX
*frame
= talloc_stackframe();
4093 if (!context
|| !context
->internal
||
4094 !context
->internal
->_initialized
) {
4095 errno
= EINVAL
; /* Best I can think of ... */
4107 access_time
= write_time
= time(NULL
);
4109 access_time
= tbuf
[0].tv_sec
;
4110 write_time
= tbuf
[1].tv_sec
;
4118 strncpy(atimebuf
, ctime(&access_time
), sizeof(atimebuf
) - 1);
4119 atimebuf
[sizeof(atimebuf
) - 1] = '\0';
4120 if ((p
= strchr(atimebuf
, '\n')) != NULL
) {
4124 strncpy(mtimebuf
, ctime(&write_time
), sizeof(mtimebuf
) - 1);
4125 mtimebuf
[sizeof(mtimebuf
) - 1] = '\0';
4126 if ((p
= strchr(mtimebuf
, '\n')) != NULL
) {
4130 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
4131 fname
, atimebuf
, mtimebuf
);
4134 if (smbc_parse_path(frame
,
4149 if (!user
|| user
[0] == (char)0) {
4150 user
= talloc_strdup(frame
, context
->user
);
4158 srv
= smbc_server(frame
, context
, True
,
4159 server
, share
, &workgroup
, &user
, &password
);
4163 return -1; /* errno set by smbc_server */
4166 if (!smbc_setatr(context
, srv
, path
,
4167 0, access_time
, write_time
, 0, 0)) {
4169 return -1; /* errno set by smbc_setatr */
4178 * Sort ACEs according to the documentation at
4179 * http://support.microsoft.com/kb/269175, at least as far as it defines the
4184 ace_compare(SEC_ACE
*ace1
,
4190 /* If the ACEs are equal, we have nothing more to do. */
4191 if (sec_ace_equal(ace1
, ace2
)) {
4195 /* Inherited follow non-inherited */
4196 b1
= ((ace1
->flags
& SEC_ACE_FLAG_INHERITED_ACE
) != 0);
4197 b2
= ((ace2
->flags
& SEC_ACE_FLAG_INHERITED_ACE
) != 0);
4199 return (b1
? 1 : -1);
4203 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
4204 * sort them after DENY and ALLOW.
4206 b1
= (ace1
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED
&&
4207 ace1
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT
&&
4208 ace1
->type
!= SEC_ACE_TYPE_ACCESS_DENIED
&&
4209 ace1
->type
!= SEC_ACE_TYPE_ACCESS_DENIED_OBJECT
);
4210 b2
= (ace2
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED
&&
4211 ace2
->type
!= SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT
&&
4212 ace2
->type
!= SEC_ACE_TYPE_ACCESS_DENIED
&&
4213 ace2
->type
!= SEC_ACE_TYPE_ACCESS_DENIED_OBJECT
);
4215 return (b1
? 1 : -1);
4218 /* Allowed ACEs follow denied ACEs */
4219 b1
= (ace1
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
||
4220 ace1
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT
);
4221 b2
= (ace2
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
||
4222 ace2
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT
);
4224 return (b1
? 1 : -1);
4228 * ACEs applying to an entity's object follow those applying to the
4231 b1
= (ace1
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT
||
4232 ace1
->type
== SEC_ACE_TYPE_ACCESS_DENIED_OBJECT
);
4233 b2
= (ace2
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT
||
4234 ace2
->type
== SEC_ACE_TYPE_ACCESS_DENIED_OBJECT
);
4236 return (b1
? 1 : -1);
4240 * If we get this far, the ACEs are similar as far as the
4241 * characteristics we typically care about (those defined by the
4242 * referenced MS document). We'll now sort by characteristics that
4243 * just seems reasonable.
4246 if (ace1
->type
!= ace2
->type
) {
4247 return ace2
->type
- ace1
->type
;
4250 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
)) {
4251 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
4254 if (ace1
->flags
!= ace2
->flags
) {
4255 return ace1
->flags
- ace2
->flags
;
4258 if (ace1
->access_mask
!= ace2
->access_mask
) {
4259 return ace1
->access_mask
- ace2
->access_mask
;
4262 if (ace1
->size
!= ace2
->size
) {
4263 return ace1
->size
- ace2
->size
;
4266 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
4271 sort_acl(SEC_ACL
*the_acl
)
4274 if (!the_acl
) return;
4276 qsort(the_acl
->aces
, the_acl
->num_aces
, sizeof(the_acl
->aces
[0]),
4277 QSORT_CAST ace_compare
);
4279 for (i
=1;i
<the_acl
->num_aces
;) {
4280 if (sec_ace_equal(&the_acl
->aces
[i
-1], &the_acl
->aces
[i
])) {
4282 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
4283 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
4285 the_acl
->num_aces
--;
4292 /* convert a SID to a string, either numeric or username/group */
4294 convert_sid_to_string(struct cli_state
*ipc_cli
,
4300 char **domains
= NULL
;
4301 char **names
= NULL
;
4302 enum lsa_SidType
*types
= NULL
;
4303 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
4306 sid_to_fstring(str
, sid
);
4309 return; /* no lookup desired */
4316 /* Ask LSA to convert the sid to a name */
4318 ctx
= talloc_stackframe();
4320 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd
, ctx
,
4321 pol
, 1, sid
, &domains
,
4323 !domains
|| !domains
[0] || !names
|| !names
[0]) {
4331 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
4332 domains
[0], lp_winbind_separator(),
4336 /* convert a string to a SID, either numeric or username/group */
4338 convert_string_to_sid(struct cli_state
*ipc_cli
,
4344 enum lsa_SidType
*types
= NULL
;
4345 DOM_SID
*sids
= NULL
;
4347 TALLOC_CTX
*ctx
= NULL
;
4348 struct rpc_pipe_client
*pipe_hnd
= find_lsa_pipe_hnd(ipc_cli
);
4355 if (strncmp(str
, "S-", 2) == 0) {
4356 return string_to_sid(sid
, str
);
4363 ctx
= talloc_stackframe();
4364 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd
, ctx
,
4365 pol
, 1, &str
, NULL
, 1, &sids
,
4371 sid_copy(sid
, &sids
[0]);
4379 /* parse an ACE in the same format as print_ace() */
4381 parse_ace(struct cli_state
*ipc_cli
,
4391 unsigned int aflags
;
4395 const struct perm_value
*v
;
4400 TALLOC_CTX
*frame
= talloc_stackframe();
4402 /* These values discovered by inspection */
4403 static const struct perm_value special_values
[] = {
4404 { "R", 0x00120089 },
4405 { "W", 0x00120116 },
4406 { "X", 0x001200a0 },
4407 { "D", 0x00010000 },
4408 { "P", 0x00040000 },
4409 { "O", 0x00080000 },
4413 static const struct perm_value standard_values
[] = {
4414 { "READ", 0x001200a9 },
4415 { "CHANGE", 0x001301bf },
4416 { "FULL", 0x001f01ff },
4422 p
= strchr_m(str
,':');
4429 /* Try to parse numeric form */
4431 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
4432 convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
4436 /* Try to parse text form */
4438 if (!convert_string_to_sid(ipc_cli
, pol
, numeric
, &sid
, str
)) {
4444 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
4449 if (StrnCaseCmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
4450 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
4451 } else if (StrnCaseCmp(tok
, "DENIED", strlen("DENIED")) == 0) {
4452 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
4458 /* Only numeric form accepted for flags at present */
4460 if (!(next_token_talloc(frame
, &cp
, &tok
, "/") &&
4461 sscanf(tok
, "%i", &aflags
))) {
4466 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
4471 if (strncmp(tok
, "0x", 2) == 0) {
4472 if (sscanf(tok
, "%i", &amask
) != 1) {
4479 for (v
= standard_values
; v
->perm
; v
++) {
4480 if (strcmp(tok
, v
->perm
) == 0) {
4491 for (v
= special_values
; v
->perm
; v
++) {
4492 if (v
->perm
[0] == *p
) {
4512 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
4517 /* add an ACE to a list of ACEs in a SEC_ACL */
4519 add_ace(SEC_ACL
**the_acl
,
4527 (*the_acl
) = make_sec_acl(ctx
, 3, 1, ace
);
4531 if ((aces
= SMB_CALLOC_ARRAY(SEC_ACE
, 1+(*the_acl
)->num_aces
)) == NULL
) {
4534 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
4535 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
4536 newacl
= make_sec_acl(ctx
, (*the_acl
)->revision
,
4537 1+(*the_acl
)->num_aces
, aces
);
4539 (*the_acl
) = newacl
;
4544 /* parse a ascii version of a security descriptor */
4546 sec_desc_parse(TALLOC_CTX
*ctx
,
4547 struct cli_state
*ipc_cli
,
4552 const char *p
= str
;
4554 SEC_DESC
*ret
= NULL
;
4556 DOM_SID
*group_sid
=NULL
;
4557 DOM_SID
*owner_sid
=NULL
;
4561 while (next_token_talloc(ctx
, &p
, &tok
, "\t,\r\n")) {
4563 if (StrnCaseCmp(tok
,"REVISION:", 9) == 0) {
4564 revision
= strtol(tok
+9, NULL
, 16);
4568 if (StrnCaseCmp(tok
,"OWNER:", 6) == 0) {
4570 DEBUG(5, ("OWNER specified more than once!\n"));
4573 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4575 !convert_string_to_sid(ipc_cli
, pol
,
4577 owner_sid
, tok
+6)) {
4578 DEBUG(5, ("Failed to parse owner sid\n"));
4584 if (StrnCaseCmp(tok
,"OWNER+:", 7) == 0) {
4586 DEBUG(5, ("OWNER specified more than once!\n"));
4589 owner_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4591 !convert_string_to_sid(ipc_cli
, pol
,
4593 owner_sid
, tok
+7)) {
4594 DEBUG(5, ("Failed to parse owner sid\n"));
4600 if (StrnCaseCmp(tok
,"GROUP:", 6) == 0) {
4602 DEBUG(5, ("GROUP specified more than once!\n"));
4605 group_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4607 !convert_string_to_sid(ipc_cli
, pol
,
4609 group_sid
, tok
+6)) {
4610 DEBUG(5, ("Failed to parse group sid\n"));
4616 if (StrnCaseCmp(tok
,"GROUP+:", 7) == 0) {
4618 DEBUG(5, ("GROUP specified more than once!\n"));
4621 group_sid
= SMB_CALLOC_ARRAY(DOM_SID
, 1);
4623 !convert_string_to_sid(ipc_cli
, pol
,
4625 group_sid
, tok
+6)) {
4626 DEBUG(5, ("Failed to parse group sid\n"));
4632 if (StrnCaseCmp(tok
,"ACL:", 4) == 0) {
4634 if (!parse_ace(ipc_cli
, pol
, &ace
, numeric
, tok
+4)) {
4635 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
4638 if(!add_ace(&dacl
, &ace
, ctx
)) {
4639 DEBUG(5, ("Failed to add ACL %s\n", tok
));
4645 if (StrnCaseCmp(tok
,"ACL+:", 5) == 0) {
4647 if (!parse_ace(ipc_cli
, pol
, &ace
, False
, tok
+5)) {
4648 DEBUG(5, ("Failed to parse ACL %s\n", tok
));
4651 if(!add_ace(&dacl
, &ace
, ctx
)) {
4652 DEBUG(5, ("Failed to add ACL %s\n", tok
));
4658 DEBUG(5, ("Failed to parse security descriptor\n"));
4662 ret
= make_sec_desc(ctx
, revision
, SEC_DESC_SELF_RELATIVE
,
4663 owner_sid
, group_sid
, NULL
, dacl
, &sd_size
);
4666 SAFE_FREE(group_sid
);
4667 SAFE_FREE(owner_sid
);
4673 /* Obtain the current dos attributes */
4674 static DOS_ATTR_DESC
*
4675 dos_attr_query(SMBCCTX
*context
,
4677 const char *filename
,
4680 struct timespec create_time_ts
;
4681 struct timespec write_time_ts
;
4682 struct timespec access_time_ts
;
4683 struct timespec change_time_ts
;
4686 SMB_INO_T inode
= 0;
4689 ret
= TALLOC_P(ctx
, DOS_ATTR_DESC
);
4695 /* Obtain the DOS attributes */
4696 if (!smbc_getatr(context
, srv
, CONST_DISCARD(char *, filename
),
4703 errno
= smbc_errno(context
, srv
->cli
);
4704 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4710 ret
->create_time
= convert_timespec_to_time_t(create_time_ts
);
4711 ret
->access_time
= convert_timespec_to_time_t(access_time_ts
);
4712 ret
->write_time
= convert_timespec_to_time_t(write_time_ts
);
4713 ret
->change_time
= convert_timespec_to_time_t(change_time_ts
);
4720 /* parse a ascii version of a security descriptor */
4722 dos_attr_parse(SMBCCTX
*context
,
4728 const char *p
= str
;
4730 TALLOC_CTX
*frame
= NULL
;
4732 const char * create_time_attr
;
4733 const char * access_time_attr
;
4734 const char * write_time_attr
;
4735 const char * change_time_attr
;
4738 /* Determine whether to use old-style or new-style attribute names */
4739 if (context
->internal
->_full_time_names
) {
4740 /* new-style names */
4741 attr_strings
.create_time_attr
= "CREATE_TIME";
4742 attr_strings
.access_time_attr
= "ACCESS_TIME";
4743 attr_strings
.write_time_attr
= "WRITE_TIME";
4744 attr_strings
.change_time_attr
= "CHANGE_TIME";
4746 /* old-style names */
4747 attr_strings
.create_time_attr
= NULL
;
4748 attr_strings
.access_time_attr
= "A_TIME";
4749 attr_strings
.write_time_attr
= "M_TIME";
4750 attr_strings
.change_time_attr
= "C_TIME";
4753 /* if this is to set the entire ACL... */
4755 /* ... then increment past the first colon if there is one */
4756 if ((p
= strchr(str
, ':')) != NULL
) {
4763 frame
= talloc_stackframe();
4764 while (next_token_talloc(frame
, &p
, &tok
, "\t,\r\n")) {
4765 if (StrnCaseCmp(tok
, "MODE:", 5) == 0) {
4766 long request
= strtol(tok
+5, NULL
, 16);
4768 dad
->mode
= (request
|
4769 (IS_DOS_DIR(dad
->mode
)
4770 ? FILE_ATTRIBUTE_DIRECTORY
4771 : FILE_ATTRIBUTE_NORMAL
));
4773 dad
->mode
= request
;
4778 if (StrnCaseCmp(tok
, "SIZE:", 5) == 0) {
4779 dad
->size
= (SMB_OFF_T
)atof(tok
+5);
4783 n
= strlen(attr_strings
.access_time_attr
);
4784 if (StrnCaseCmp(tok
, attr_strings
.access_time_attr
, n
) == 0) {
4785 dad
->access_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4789 n
= strlen(attr_strings
.change_time_attr
);
4790 if (StrnCaseCmp(tok
, attr_strings
.change_time_attr
, n
) == 0) {
4791 dad
->change_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4795 n
= strlen(attr_strings
.write_time_attr
);
4796 if (StrnCaseCmp(tok
, attr_strings
.write_time_attr
, n
) == 0) {
4797 dad
->write_time
= (time_t)strtol(tok
+n
+1, NULL
, 10);
4801 if (attr_strings
.create_time_attr
!= NULL
) {
4802 n
= strlen(attr_strings
.create_time_attr
);
4803 if (StrnCaseCmp(tok
, attr_strings
.create_time_attr
,
4805 dad
->create_time
= (time_t)strtol(tok
+n
+1,
4811 if (StrnCaseCmp(tok
, "INODE:", 6) == 0) {
4812 dad
->inode
= (SMB_INO_T
)atof(tok
+6);
4819 /*****************************************************
4820 Retrieve the acls for a file.
4821 *******************************************************/
4824 cacl_get(SMBCCTX
*context
,
4827 struct cli_state
*ipc_cli
,
4843 bool exclude_nt_revision
= False
;
4844 bool exclude_nt_owner
= False
;
4845 bool exclude_nt_group
= False
;
4846 bool exclude_nt_acl
= False
;
4847 bool exclude_dos_mode
= False
;
4848 bool exclude_dos_size
= False
;
4849 bool exclude_dos_create_time
= False
;
4850 bool exclude_dos_access_time
= False
;
4851 bool exclude_dos_write_time
= False
;
4852 bool exclude_dos_change_time
= False
;
4853 bool exclude_dos_inode
= False
;
4854 bool numeric
= True
;
4855 bool determine_size
= (bufsize
== 0);
4859 fstring name_sandbox
;
4863 struct timespec create_time_ts
;
4864 struct timespec write_time_ts
;
4865 struct timespec access_time_ts
;
4866 struct timespec change_time_ts
;
4867 time_t create_time
= (time_t)0;
4868 time_t write_time
= (time_t)0;
4869 time_t access_time
= (time_t)0;
4870 time_t change_time
= (time_t)0;
4874 struct cli_state
*cli
= srv
->cli
;
4876 const char * create_time_attr
;
4877 const char * access_time_attr
;
4878 const char * write_time_attr
;
4879 const char * change_time_attr
;
4882 const char * create_time_attr
;
4883 const char * access_time_attr
;
4884 const char * write_time_attr
;
4885 const char * change_time_attr
;
4886 } excl_attr_strings
;
4888 /* Determine whether to use old-style or new-style attribute names */
4889 if (context
->internal
->_full_time_names
) {
4890 /* new-style names */
4891 attr_strings
.create_time_attr
= "CREATE_TIME";
4892 attr_strings
.access_time_attr
= "ACCESS_TIME";
4893 attr_strings
.write_time_attr
= "WRITE_TIME";
4894 attr_strings
.change_time_attr
= "CHANGE_TIME";
4896 excl_attr_strings
.create_time_attr
= "CREATE_TIME";
4897 excl_attr_strings
.access_time_attr
= "ACCESS_TIME";
4898 excl_attr_strings
.write_time_attr
= "WRITE_TIME";
4899 excl_attr_strings
.change_time_attr
= "CHANGE_TIME";
4901 /* old-style names */
4902 attr_strings
.create_time_attr
= NULL
;
4903 attr_strings
.access_time_attr
= "A_TIME";
4904 attr_strings
.write_time_attr
= "M_TIME";
4905 attr_strings
.change_time_attr
= "C_TIME";
4907 excl_attr_strings
.create_time_attr
= NULL
;
4908 excl_attr_strings
.access_time_attr
= "dos_attr.A_TIME";
4909 excl_attr_strings
.write_time_attr
= "dos_attr.M_TIME";
4910 excl_attr_strings
.change_time_attr
= "dos_attr.C_TIME";
4913 /* Copy name so we can strip off exclusions (if any are specified) */
4914 strncpy(name_sandbox
, attr_name
, sizeof(name_sandbox
) - 1);
4916 /* Ensure name is null terminated */
4917 name_sandbox
[sizeof(name_sandbox
) - 1] = '\0';
4919 /* Play in the sandbox */
4920 name
= name_sandbox
;
4922 /* If there are any exclusions, point to them and mask them from name */
4923 if ((pExclude
= strchr(name
, '!')) != NULL
)
4928 all
= (StrnCaseCmp(name
, "system.*", 8) == 0);
4929 all_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.*", 20) == 0);
4930 all_nt_acls
= (StrnCaseCmp(name
, "system.nt_sec_desc.acl.*", 24) == 0);
4931 all_dos
= (StrnCaseCmp(name
, "system.dos_attr.*", 17) == 0);
4932 some_nt
= (StrnCaseCmp(name
, "system.nt_sec_desc.", 19) == 0);
4933 some_dos
= (StrnCaseCmp(name
, "system.dos_attr.", 16) == 0);
4934 numeric
= (* (name
+ strlen(name
) - 1) != '+');
4936 /* Look for exclusions from "all" requests */
4937 if (all
|| all_nt
|| all_dos
) {
4939 /* Exclusions are delimited by '!' */
4942 pExclude
= (p
== NULL
? NULL
: p
+ 1)) {
4944 /* Find end of this exclusion name */
4945 if ((p
= strchr(pExclude
, '!')) != NULL
)
4950 /* Which exclusion name is this? */
4951 if (StrCaseCmp(pExclude
, "nt_sec_desc.revision") == 0) {
4952 exclude_nt_revision
= True
;
4954 else if (StrCaseCmp(pExclude
, "nt_sec_desc.owner") == 0) {
4955 exclude_nt_owner
= True
;
4957 else if (StrCaseCmp(pExclude
, "nt_sec_desc.group") == 0) {
4958 exclude_nt_group
= True
;
4960 else if (StrCaseCmp(pExclude
, "nt_sec_desc.acl") == 0) {
4961 exclude_nt_acl
= True
;
4963 else if (StrCaseCmp(pExclude
, "dos_attr.mode") == 0) {
4964 exclude_dos_mode
= True
;
4966 else if (StrCaseCmp(pExclude
, "dos_attr.size") == 0) {
4967 exclude_dos_size
= True
;
4969 else if (excl_attr_strings
.create_time_attr
!= NULL
&&
4970 StrCaseCmp(pExclude
,
4971 excl_attr_strings
.change_time_attr
) == 0) {
4972 exclude_dos_create_time
= True
;
4974 else if (StrCaseCmp(pExclude
,
4975 excl_attr_strings
.access_time_attr
) == 0) {
4976 exclude_dos_access_time
= True
;
4978 else if (StrCaseCmp(pExclude
,
4979 excl_attr_strings
.write_time_attr
) == 0) {
4980 exclude_dos_write_time
= True
;
4982 else if (StrCaseCmp(pExclude
,
4983 excl_attr_strings
.change_time_attr
) == 0) {
4984 exclude_dos_change_time
= True
;
4986 else if (StrCaseCmp(pExclude
, "dos_attr.inode") == 0) {
4987 exclude_dos_inode
= True
;
4990 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
5001 * If we are (possibly) talking to an NT or new system and some NT
5002 * attributes have been requested...
5004 if (ipc_cli
&& (all
|| some_nt
|| all_nt_acls
)) {
5005 /* Point to the portion after "system.nt_sec_desc." */
5006 name
+= 19; /* if (all) this will be invalid but unused */
5008 /* ... then obtain any NT attributes which were requested */
5009 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
5012 DEBUG(5, ("cacl_get failed to open %s: %s\n",
5013 filename
, cli_errstr(cli
)));
5018 sd
= cli_query_secdesc(cli
, fnum
, ctx
);
5022 ("cacl_get Failed to query old descriptor\n"));
5027 cli_close(cli
, fnum
);
5029 if (! exclude_nt_revision
) {
5030 if (all
|| all_nt
) {
5031 if (determine_size
) {
5032 p
= talloc_asprintf(ctx
,
5041 n
= snprintf(buf
, bufsize
,
5045 } else if (StrCaseCmp(name
, "revision") == 0) {
5046 if (determine_size
) {
5047 p
= talloc_asprintf(ctx
, "%d",
5055 n
= snprintf(buf
, bufsize
, "%d",
5060 if (!determine_size
&& n
> bufsize
) {
5070 if (! exclude_nt_owner
) {
5071 /* Get owner and group sid */
5072 if (sd
->owner_sid
) {
5073 convert_sid_to_string(ipc_cli
, pol
,
5078 fstrcpy(sidstr
, "");
5081 if (all
|| all_nt
) {
5082 if (determine_size
) {
5083 p
= talloc_asprintf(ctx
, ",OWNER:%s",
5090 } else if (sidstr
[0] != '\0') {
5091 n
= snprintf(buf
, bufsize
,
5092 ",OWNER:%s", sidstr
);
5094 } else if (StrnCaseCmp(name
, "owner", 5) == 0) {
5095 if (determine_size
) {
5096 p
= talloc_asprintf(ctx
, "%s", sidstr
);
5103 n
= snprintf(buf
, bufsize
, "%s",
5108 if (!determine_size
&& n
> bufsize
) {
5118 if (! exclude_nt_group
) {
5119 if (sd
->group_sid
) {
5120 convert_sid_to_string(ipc_cli
, pol
,
5124 fstrcpy(sidstr
, "");
5127 if (all
|| all_nt
) {
5128 if (determine_size
) {
5129 p
= talloc_asprintf(ctx
, ",GROUP:%s",
5136 } else if (sidstr
[0] != '\0') {
5137 n
= snprintf(buf
, bufsize
,
5138 ",GROUP:%s", sidstr
);
5140 } else if (StrnCaseCmp(name
, "group", 5) == 0) {
5141 if (determine_size
) {
5142 p
= talloc_asprintf(ctx
, "%s", sidstr
);
5149 n
= snprintf(buf
, bufsize
,
5154 if (!determine_size
&& n
> bufsize
) {
5164 if (! exclude_nt_acl
) {
5165 /* Add aces to value buffer */
5166 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
5168 SEC_ACE
*ace
= &sd
->dacl
->aces
[i
];
5169 convert_sid_to_string(ipc_cli
, pol
,
5173 if (all
|| all_nt
) {
5174 if (determine_size
) {
5175 p
= talloc_asprintf(
5191 ",ACL:%s:%d/%d/0x%08x",
5197 } else if ((StrnCaseCmp(name
, "acl", 3) == 0 &&
5198 StrCaseCmp(name
+3, sidstr
) == 0) ||
5199 (StrnCaseCmp(name
, "acl+", 4) == 0 &&
5200 StrCaseCmp(name
+4, sidstr
) == 0)) {
5201 if (determine_size
) {
5202 p
= talloc_asprintf(
5214 n
= snprintf(buf
, bufsize
,
5220 } else if (all_nt_acls
) {
5221 if (determine_size
) {
5222 p
= talloc_asprintf(
5224 "%s%s:%d/%d/0x%08x",
5236 n
= snprintf(buf
, bufsize
,
5237 "%s%s:%d/%d/0x%08x",
5245 if (!determine_size
&& n
> bufsize
) {
5256 /* Restore name pointer to its original value */
5260 if (all
|| some_dos
) {
5261 /* Point to the portion after "system.dos_attr." */
5262 name
+= 16; /* if (all) this will be invalid but unused */
5264 /* Obtain the DOS attributes */
5265 if (!smbc_getatr(context
, srv
, filename
, &mode
, &size
,
5272 errno
= smbc_errno(context
, srv
->cli
);
5277 create_time
= convert_timespec_to_time_t(create_time_ts
);
5278 access_time
= convert_timespec_to_time_t(access_time_ts
);
5279 write_time
= convert_timespec_to_time_t(write_time_ts
);
5280 change_time
= convert_timespec_to_time_t(change_time_ts
);
5282 if (! exclude_dos_mode
) {
5283 if (all
|| all_dos
) {
5284 if (determine_size
) {
5285 p
= talloc_asprintf(ctx
,
5298 n
= snprintf(buf
, bufsize
,
5306 } else if (StrCaseCmp(name
, "mode") == 0) {
5307 if (determine_size
) {
5308 p
= talloc_asprintf(ctx
, "0x%x", mode
);
5315 n
= snprintf(buf
, bufsize
,
5320 if (!determine_size
&& n
> bufsize
) {
5330 if (! exclude_dos_size
) {
5331 if (all
|| all_dos
) {
5332 if (determine_size
) {
5333 p
= talloc_asprintf(
5343 n
= snprintf(buf
, bufsize
,
5347 } else if (StrCaseCmp(name
, "size") == 0) {
5348 if (determine_size
) {
5349 p
= talloc_asprintf(
5359 n
= snprintf(buf
, bufsize
,
5365 if (!determine_size
&& n
> bufsize
) {
5375 if (! exclude_dos_create_time
&&
5376 attr_strings
.create_time_attr
!= NULL
) {
5377 if (all
|| all_dos
) {
5378 if (determine_size
) {
5379 p
= talloc_asprintf(ctx
,
5381 attr_strings
.create_time_attr
,
5389 n
= snprintf(buf
, bufsize
,
5391 attr_strings
.create_time_attr
,
5394 } else if (StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) {
5395 if (determine_size
) {
5396 p
= talloc_asprintf(ctx
, "%lu", create_time
);
5403 n
= snprintf(buf
, bufsize
,
5404 "%lu", create_time
);
5408 if (!determine_size
&& n
> bufsize
) {
5418 if (! exclude_dos_access_time
) {
5419 if (all
|| all_dos
) {
5420 if (determine_size
) {
5421 p
= talloc_asprintf(ctx
,
5423 attr_strings
.access_time_attr
,
5431 n
= snprintf(buf
, bufsize
,
5433 attr_strings
.access_time_attr
,
5436 } else if (StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0) {
5437 if (determine_size
) {
5438 p
= talloc_asprintf(ctx
, "%lu", access_time
);
5445 n
= snprintf(buf
, bufsize
,
5446 "%lu", access_time
);
5450 if (!determine_size
&& n
> bufsize
) {
5460 if (! exclude_dos_write_time
) {
5461 if (all
|| all_dos
) {
5462 if (determine_size
) {
5463 p
= talloc_asprintf(ctx
,
5465 attr_strings
.write_time_attr
,
5473 n
= snprintf(buf
, bufsize
,
5475 attr_strings
.write_time_attr
,
5478 } else if (StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0) {
5479 if (determine_size
) {
5480 p
= talloc_asprintf(ctx
, "%lu", write_time
);
5487 n
= snprintf(buf
, bufsize
,
5492 if (!determine_size
&& n
> bufsize
) {
5502 if (! exclude_dos_change_time
) {
5503 if (all
|| all_dos
) {
5504 if (determine_size
) {
5505 p
= talloc_asprintf(ctx
,
5507 attr_strings
.change_time_attr
,
5515 n
= snprintf(buf
, bufsize
,
5517 attr_strings
.change_time_attr
,
5520 } else if (StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0) {
5521 if (determine_size
) {
5522 p
= talloc_asprintf(ctx
, "%lu", change_time
);
5529 n
= snprintf(buf
, bufsize
,
5530 "%lu", change_time
);
5534 if (!determine_size
&& n
> bufsize
) {
5544 if (! exclude_dos_inode
) {
5545 if (all
|| all_dos
) {
5546 if (determine_size
) {
5547 p
= talloc_asprintf(
5557 n
= snprintf(buf
, bufsize
,
5561 } else if (StrCaseCmp(name
, "inode") == 0) {
5562 if (determine_size
) {
5563 p
= talloc_asprintf(
5573 n
= snprintf(buf
, bufsize
,
5579 if (!determine_size
&& n
> bufsize
) {
5589 /* Restore name pointer to its original value */
5601 /*****************************************************
5602 set the ACLs on a file given an ascii description
5603 *******************************************************/
5605 cacl_set(TALLOC_CTX
*ctx
,
5606 struct cli_state
*cli
,
5607 struct cli_state
*ipc_cli
,
5609 const char *filename
,
5610 const char *the_acl
,
5616 SEC_DESC
*sd
= NULL
, *old
;
5617 SEC_ACL
*dacl
= NULL
;
5618 DOM_SID
*owner_sid
= NULL
;
5619 DOM_SID
*group_sid
= NULL
;
5624 bool numeric
= True
;
5626 /* the_acl will be null for REMOVE_ALL operations */
5628 numeric
= ((p
= strchr(the_acl
, ':')) != NULL
&&
5632 /* if this is to set the entire ACL... */
5633 if (*the_acl
== '*') {
5634 /* ... then increment past the first colon */
5638 sd
= sec_desc_parse(ctx
, ipc_cli
, pol
, numeric
,
5639 CONST_DISCARD(char *, the_acl
));
5647 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5648 that doesn't deref sd */
5650 if (!sd
&& (mode
!= SMBC_XATTR_MODE_REMOVE_ALL
)) {
5655 /* The desired access below is the only one I could find that works
5656 with NT4, W2KP and Samba */
5658 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
5661 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5662 filename
, cli_errstr(cli
)));
5667 old
= cli_query_secdesc(cli
, fnum
, ctx
);
5670 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5675 cli_close(cli
, fnum
);
5678 case SMBC_XATTR_MODE_REMOVE_ALL
:
5679 old
->dacl
->num_aces
= 0;
5683 case SMBC_XATTR_MODE_REMOVE
:
5684 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5687 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
5688 if (sec_ace_equal(&sd
->dacl
->aces
[i
],
5689 &old
->dacl
->aces
[j
])) {
5691 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
5692 old
->dacl
->aces
[k
] =
5693 old
->dacl
->aces
[k
+1];
5695 old
->dacl
->num_aces
--;
5710 case SMBC_XATTR_MODE_ADD
:
5711 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5714 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
5715 if (sid_equal(&sd
->dacl
->aces
[i
].trustee
,
5716 &old
->dacl
->aces
[j
].trustee
)) {
5717 if (!(flags
& SMBC_XATTR_FLAG_CREATE
)) {
5722 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
5728 if (!found
&& (flags
& SMBC_XATTR_FLAG_REPLACE
)) {
5734 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
5735 add_ace(&old
->dacl
, &sd
->dacl
->aces
[i
], ctx
);
5741 case SMBC_XATTR_MODE_SET
:
5743 owner_sid
= old
->owner_sid
;
5744 group_sid
= old
->group_sid
;
5748 case SMBC_XATTR_MODE_CHOWN
:
5749 owner_sid
= sd
->owner_sid
;
5752 case SMBC_XATTR_MODE_CHGRP
:
5753 group_sid
= sd
->group_sid
;
5757 /* Denied ACE entries must come before allowed ones */
5758 sort_acl(old
->dacl
);
5760 /* Create new security descriptor and set it */
5761 sd
= make_sec_desc(ctx
, old
->revision
, SEC_DESC_SELF_RELATIVE
,
5762 owner_sid
, group_sid
, NULL
, dacl
, &sd_size
);
5764 fnum
= cli_nt_create(cli
, filename
,
5765 WRITE_DAC_ACCESS
| WRITE_OWNER_ACCESS
);
5768 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5769 filename
, cli_errstr(cli
)));
5774 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
5775 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli
)));
5782 cli_close(cli
, fnum
);
5793 smbc_setxattr_ctx(SMBCCTX
*context
,
5802 SMBCSRV
*srv
= NULL
;
5803 SMBCSRV
*ipc_srv
= NULL
;
5804 char *server
= NULL
;
5807 char *password
= NULL
;
5808 char *workgroup
= NULL
;
5810 DOS_ATTR_DESC
*dad
= NULL
;
5812 const char * create_time_attr
;
5813 const char * access_time_attr
;
5814 const char * write_time_attr
;
5815 const char * change_time_attr
;
5817 TALLOC_CTX
*frame
= talloc_stackframe();
5819 if (!context
|| !context
->internal
||
5820 !context
->internal
->_initialized
) {
5821 errno
= EINVAL
; /* Best I can think of ... */
5832 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5833 fname
, name
, (int) size
, (const char*)value
));
5835 if (smbc_parse_path(frame
,
5850 if (!user
|| user
[0] == (char)0) {
5851 user
= talloc_strdup(frame
, context
->user
);
5859 srv
= smbc_server(frame
, context
, True
,
5860 server
, share
, &workgroup
, &user
, &password
);
5863 return -1; /* errno set by smbc_server */
5866 if (! srv
->no_nt_session
) {
5867 ipc_srv
= smbc_attr_server(frame
, context
, server
, share
,
5868 &workgroup
, &user
, &password
);
5870 srv
->no_nt_session
= True
;
5877 * Are they asking to set the entire set of known attributes?
5879 if (StrCaseCmp(name
, "system.*") == 0 ||
5880 StrCaseCmp(name
, "system.*+") == 0) {
5883 talloc_asprintf(talloc_tos(), "%s:%s",
5884 name
+7, (const char *) value
);
5893 ret
= cacl_set(talloc_tos(), srv
->cli
,
5894 ipc_srv
->cli
, &ipc_srv
->pol
, path
,
5897 ? SMBC_XATTR_MODE_SET
5898 : SMBC_XATTR_MODE_ADD
),
5904 /* get a DOS Attribute Descriptor with current attributes */
5905 dad
= dos_attr_query(context
, talloc_tos(), path
, srv
);
5907 /* Overwrite old with new, using what was provided */
5908 dos_attr_parse(context
, dad
, srv
, namevalue
);
5910 /* Set the new DOS attributes */
5911 if (! smbc_setatr(context
, srv
, path
,
5918 /* cause failure if NT failed too */
5923 /* we only fail if both NT and DOS sets failed */
5924 if (ret
< 0 && ! dad
) {
5925 ret
= -1; /* in case dad was null */
5936 * Are they asking to set an access control element or to set
5937 * the entire access control list?
5939 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
5940 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
5941 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
5942 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
5943 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
5947 talloc_asprintf(talloc_tos(), "%s:%s",
5948 name
+19, (const char *) value
);
5951 ret
= -1; /* errno set by smbc_server() */
5953 else if (! namevalue
) {
5957 ret
= cacl_set(talloc_tos(), srv
->cli
,
5958 ipc_srv
->cli
, &ipc_srv
->pol
, path
,
5961 ? SMBC_XATTR_MODE_SET
5962 : SMBC_XATTR_MODE_ADD
),
5970 * Are they asking to set the owner?
5972 if (StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
5973 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0) {
5977 talloc_asprintf(talloc_tos(), "%s:%s",
5978 name
+19, (const char *) value
);
5981 ret
= -1; /* errno set by smbc_server() */
5983 else if (! namevalue
) {
5987 ret
= cacl_set(talloc_tos(), srv
->cli
,
5988 ipc_srv
->cli
, &ipc_srv
->pol
, path
,
5989 namevalue
, SMBC_XATTR_MODE_CHOWN
, 0);
5996 * Are they asking to set the group?
5998 if (StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
5999 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0) {
6003 talloc_asprintf(talloc_tos(), "%s:%s",
6004 name
+19, (const char *) value
);
6007 /* errno set by smbc_server() */
6010 else if (! namevalue
) {
6014 ret
= cacl_set(talloc_tos(), srv
->cli
,
6015 ipc_srv
->cli
, &ipc_srv
->pol
, path
,
6016 namevalue
, SMBC_XATTR_MODE_CHGRP
, 0);
6022 /* Determine whether to use old-style or new-style attribute names */
6023 if (context
->internal
->_full_time_names
) {
6024 /* new-style names */
6025 attr_strings
.create_time_attr
= "system.dos_attr.CREATE_TIME";
6026 attr_strings
.access_time_attr
= "system.dos_attr.ACCESS_TIME";
6027 attr_strings
.write_time_attr
= "system.dos_attr.WRITE_TIME";
6028 attr_strings
.change_time_attr
= "system.dos_attr.CHANGE_TIME";
6030 /* old-style names */
6031 attr_strings
.create_time_attr
= NULL
;
6032 attr_strings
.access_time_attr
= "system.dos_attr.A_TIME";
6033 attr_strings
.write_time_attr
= "system.dos_attr.M_TIME";
6034 attr_strings
.change_time_attr
= "system.dos_attr.C_TIME";
6038 * Are they asking to set a DOS attribute?
6040 if (StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
6041 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
6042 (attr_strings
.create_time_attr
!= NULL
&&
6043 StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) ||
6044 StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0 ||
6045 StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0 ||
6046 StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0) {
6048 /* get a DOS Attribute Descriptor with current attributes */
6049 dad
= dos_attr_query(context
, talloc_tos(), path
, srv
);
6052 talloc_asprintf(talloc_tos(), "%s:%s",
6053 name
+16, (const char *) value
);
6058 /* Overwrite old with provided new params */
6059 dos_attr_parse(context
, dad
, srv
, namevalue
);
6061 /* Set the new DOS attributes */
6062 ret2
= smbc_setatr(context
, srv
, path
,
6069 /* ret2 has True (success) / False (failure) */
6084 /* Unsupported attribute name */
6091 smbc_getxattr_ctx(SMBCCTX
*context
,
6098 SMBCSRV
*srv
= NULL
;
6099 SMBCSRV
*ipc_srv
= NULL
;
6100 char *server
= NULL
;
6103 char *password
= NULL
;
6104 char *workgroup
= NULL
;
6107 const char * create_time_attr
;
6108 const char * access_time_attr
;
6109 const char * write_time_attr
;
6110 const char * change_time_attr
;
6112 TALLOC_CTX
*frame
= talloc_stackframe();
6114 if (!context
|| !context
->internal
||
6115 !context
->internal
->_initialized
) {
6116 errno
= EINVAL
; /* Best I can think of ... */
6127 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname
, name
));
6129 if (smbc_parse_path(frame
,
6144 if (!user
|| user
[0] == (char)0) {
6145 user
= talloc_strdup(frame
, context
->user
);
6153 srv
= smbc_server(frame
, context
, True
,
6154 server
, share
, &workgroup
, &user
, &password
);
6157 return -1; /* errno set by smbc_server */
6160 if (! srv
->no_nt_session
) {
6161 ipc_srv
= smbc_attr_server(frame
, context
, server
, share
,
6162 &workgroup
, &user
, &password
);
6164 srv
->no_nt_session
= True
;
6170 /* Determine whether to use old-style or new-style attribute names */
6171 if (context
->internal
->_full_time_names
) {
6172 /* new-style names */
6173 attr_strings
.create_time_attr
= "system.dos_attr.CREATE_TIME";
6174 attr_strings
.access_time_attr
= "system.dos_attr.ACCESS_TIME";
6175 attr_strings
.write_time_attr
= "system.dos_attr.WRITE_TIME";
6176 attr_strings
.change_time_attr
= "system.dos_attr.CHANGE_TIME";
6178 /* old-style names */
6179 attr_strings
.create_time_attr
= NULL
;
6180 attr_strings
.access_time_attr
= "system.dos_attr.A_TIME";
6181 attr_strings
.write_time_attr
= "system.dos_attr.M_TIME";
6182 attr_strings
.change_time_attr
= "system.dos_attr.C_TIME";
6185 /* Are they requesting a supported attribute? */
6186 if (StrCaseCmp(name
, "system.*") == 0 ||
6187 StrnCaseCmp(name
, "system.*!", 9) == 0 ||
6188 StrCaseCmp(name
, "system.*+") == 0 ||
6189 StrnCaseCmp(name
, "system.*+!", 10) == 0 ||
6190 StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
6191 StrnCaseCmp(name
, "system.nt_sec_desc.*!", 21) == 0 ||
6192 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0 ||
6193 StrnCaseCmp(name
, "system.nt_sec_desc.*+!", 22) == 0 ||
6194 StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
6195 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
6196 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
6197 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
6198 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
6199 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
6200 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0 ||
6201 StrCaseCmp(name
, "system.dos_attr.*") == 0 ||
6202 StrnCaseCmp(name
, "system.dos_attr.*!", 18) == 0 ||
6203 StrCaseCmp(name
, "system.dos_attr.mode") == 0 ||
6204 StrCaseCmp(name
, "system.dos_attr.size") == 0 ||
6205 (attr_strings
.create_time_attr
!= NULL
&&
6206 StrCaseCmp(name
, attr_strings
.create_time_attr
) == 0) ||
6207 StrCaseCmp(name
, attr_strings
.access_time_attr
) == 0 ||
6208 StrCaseCmp(name
, attr_strings
.write_time_attr
) == 0 ||
6209 StrCaseCmp(name
, attr_strings
.change_time_attr
) == 0 ||
6210 StrCaseCmp(name
, "system.dos_attr.inode") == 0) {
6213 ret
= cacl_get(context
, talloc_tos(), srv
,
6214 ipc_srv
== NULL
? NULL
: ipc_srv
->cli
,
6215 &ipc_srv
->pol
, path
,
6216 CONST_DISCARD(char *, name
),
6217 CONST_DISCARD(char *, value
), size
);
6218 if (ret
< 0 && errno
== 0) {
6219 errno
= smbc_errno(context
, srv
->cli
);
6225 /* Unsupported attribute name */
6233 smbc_removexattr_ctx(SMBCCTX
*context
,
6238 SMBCSRV
*srv
= NULL
;
6239 SMBCSRV
*ipc_srv
= NULL
;
6240 char *server
= NULL
;
6243 char *password
= NULL
;
6244 char *workgroup
= NULL
;
6246 TALLOC_CTX
*frame
= talloc_stackframe();
6248 if (!context
|| !context
->internal
||
6249 !context
->internal
->_initialized
) {
6250 errno
= EINVAL
; /* Best I can think of ... */
6261 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname
, name
));
6263 if (smbc_parse_path(frame
,
6278 if (!user
|| user
[0] == (char)0) {
6279 user
= talloc_strdup(frame
, context
->user
);
6287 srv
= smbc_server(frame
, context
, True
,
6288 server
, share
, &workgroup
, &user
, &password
);
6291 return -1; /* errno set by smbc_server */
6294 if (! srv
->no_nt_session
) {
6295 ipc_srv
= smbc_attr_server(frame
, context
, server
, share
,
6296 &workgroup
, &user
, &password
);
6298 srv
->no_nt_session
= True
;
6306 return -1; /* errno set by smbc_attr_server */
6309 /* Are they asking to set the entire ACL? */
6310 if (StrCaseCmp(name
, "system.nt_sec_desc.*") == 0 ||
6311 StrCaseCmp(name
, "system.nt_sec_desc.*+") == 0) {
6314 ret
= cacl_set(talloc_tos(), srv
->cli
,
6315 ipc_srv
->cli
, &ipc_srv
->pol
, path
,
6316 NULL
, SMBC_XATTR_MODE_REMOVE_ALL
, 0);
6322 * Are they asking to remove one or more spceific security descriptor
6325 if (StrCaseCmp(name
, "system.nt_sec_desc.revision") == 0 ||
6326 StrCaseCmp(name
, "system.nt_sec_desc.owner") == 0 ||
6327 StrCaseCmp(name
, "system.nt_sec_desc.owner+") == 0 ||
6328 StrCaseCmp(name
, "system.nt_sec_desc.group") == 0 ||
6329 StrCaseCmp(name
, "system.nt_sec_desc.group+") == 0 ||
6330 StrnCaseCmp(name
, "system.nt_sec_desc.acl", 22) == 0 ||
6331 StrnCaseCmp(name
, "system.nt_sec_desc.acl+", 23) == 0) {
6334 ret
= cacl_set(talloc_tos(), srv
->cli
,
6335 ipc_srv
->cli
, &ipc_srv
->pol
, path
,
6336 name
+ 19, SMBC_XATTR_MODE_REMOVE
, 0);
6341 /* Unsupported attribute name */
6348 smbc_listxattr_ctx(SMBCCTX
*context
,
6354 * This isn't quite what listxattr() is supposed to do. This returns
6355 * the complete set of attribute names, always, rather than only those
6356 * attribute names which actually exist for a file. Hmmm...
6359 const char supported_old
[] =
6362 "system.nt_sec_desc.revision\0"
6363 "system.nt_sec_desc.owner\0"
6364 "system.nt_sec_desc.owner+\0"
6365 "system.nt_sec_desc.group\0"
6366 "system.nt_sec_desc.group+\0"
6367 "system.nt_sec_desc.acl.*\0"
6368 "system.nt_sec_desc.acl\0"
6369 "system.nt_sec_desc.acl+\0"
6370 "system.nt_sec_desc.*\0"
6371 "system.nt_sec_desc.*+\0"
6372 "system.dos_attr.*\0"
6373 "system.dos_attr.mode\0"
6374 "system.dos_attr.c_time\0"
6375 "system.dos_attr.a_time\0"
6376 "system.dos_attr.m_time\0"
6378 const char supported_new
[] =
6381 "system.nt_sec_desc.revision\0"
6382 "system.nt_sec_desc.owner\0"
6383 "system.nt_sec_desc.owner+\0"
6384 "system.nt_sec_desc.group\0"
6385 "system.nt_sec_desc.group+\0"
6386 "system.nt_sec_desc.acl.*\0"
6387 "system.nt_sec_desc.acl\0"
6388 "system.nt_sec_desc.acl+\0"
6389 "system.nt_sec_desc.*\0"
6390 "system.nt_sec_desc.*+\0"
6391 "system.dos_attr.*\0"
6392 "system.dos_attr.mode\0"
6393 "system.dos_attr.create_time\0"
6394 "system.dos_attr.access_time\0"
6395 "system.dos_attr.write_time\0"
6396 "system.dos_attr.change_time\0"
6398 const char * supported
;
6400 if (context
->internal
->_full_time_names
) {
6401 supported
= supported_new
;
6402 retsize
= sizeof(supported_new
);
6404 supported
= supported_old
;
6405 retsize
= sizeof(supported_old
);
6412 if (retsize
> size
) {
6417 /* this can't be strcpy() because there are embedded null characters */
6418 memcpy(list
, supported
, retsize
);
6424 * Open a print file to be written to by other calls
6428 smbc_open_print_job_ctx(SMBCCTX
*context
,
6431 char *server
= NULL
;
6434 char *password
= NULL
;
6436 TALLOC_CTX
*frame
= talloc_stackframe();
6438 if (!context
|| !context
->internal
||
6439 !context
->internal
->_initialized
) {
6451 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname
));
6453 if (smbc_parse_path(frame
,
6468 /* What if the path is empty, or the file exists? */
6471 return (context
->open
)(context
, fname
, O_WRONLY
, 666);
6475 * Routine to print a file on a remote server ...
6477 * We open the file, which we assume to be on a remote server, and then
6478 * copy it to a print file on the share specified by printq.
6482 smbc_print_file_ctx(SMBCCTX
*c_file
,
6493 TALLOC_CTX
*frame
= talloc_stackframe();
6495 if (!c_file
|| !c_file
->internal
->_initialized
|| !c_print
||
6496 !c_print
->internal
->_initialized
) {
6504 if (!fname
&& !printq
) {
6512 /* Try to open the file for reading ... */
6514 if ((long)(fid1
= (c_file
->open
)(c_file
, fname
, O_RDONLY
, 0666)) < 0) {
6515 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname
, errno
));
6517 return -1; /* smbc_open sets errno */
6520 /* Now, try to open the printer file for writing */
6522 if ((long)(fid2
= (c_print
->open_print_job
)(c_print
, printq
)) < 0) {
6524 saverr
= errno
; /* Save errno */
6525 (c_file
->close_fn
)(c_file
, fid1
);
6532 while ((bytes
= (c_file
->read
)(c_file
, fid1
, buf
, sizeof(buf
))) > 0) {
6536 if (((c_print
->write
)(c_print
, fid2
, buf
, bytes
)) < 0) {
6539 (c_file
->close_fn
)(c_file
, fid1
);
6540 (c_print
->close_fn
)(c_print
, fid2
);
6549 (c_file
->close_fn
)(c_file
, fid1
); /* We have to close these anyway */
6550 (c_print
->close_fn
)(c_print
, fid2
);
6566 * Routine to list print jobs on a printer share ...
6570 smbc_list_print_jobs_ctx(SMBCCTX
*context
,
6572 smbc_list_print_job_fn fn
)
6574 SMBCSRV
*srv
= NULL
;
6575 char *server
= NULL
;
6578 char *password
= NULL
;
6579 char *workgroup
= NULL
;
6581 TALLOC_CTX
*frame
= talloc_stackframe();
6583 if (!context
|| !context
->internal
||
6584 !context
->internal
->_initialized
) {
6596 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname
));
6598 if (smbc_parse_path(frame
,
6613 if (!user
|| user
[0] == (char)0) {
6614 user
= talloc_strdup(frame
, context
->user
);
6622 srv
= smbc_server(frame
, context
, True
,
6623 server
, share
, &workgroup
, &user
, &password
);
6627 return -1; /* errno set by smbc_server */
6630 if (cli_print_queue(srv
->cli
,
6631 (void (*)(struct print_job_info
*))fn
) < 0) {
6632 errno
= smbc_errno(context
, srv
->cli
);
6643 * Delete a print job from a remote printer share
6647 smbc_unlink_print_job_ctx(SMBCCTX
*context
,
6651 SMBCSRV
*srv
= NULL
;
6652 char *server
= NULL
;
6655 char *password
= NULL
;
6656 char *workgroup
= NULL
;
6659 TALLOC_CTX
*frame
= talloc_stackframe();
6661 if (!context
|| !context
->internal
||
6662 !context
->internal
->_initialized
) {
6674 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname
));
6676 if (smbc_parse_path(frame
,
6691 if (!user
|| user
[0] == (char)0) {
6692 user
= talloc_strdup(frame
, context
->user
);
6700 srv
= smbc_server(frame
, context
, True
,
6701 server
, share
, &workgroup
, &user
, &password
);
6706 return -1; /* errno set by smbc_server */
6710 if ((err
= cli_printjob_del(srv
->cli
, id
)) != 0) {
6713 errno
= smbc_errno(context
, srv
->cli
);
6714 else if (err
== ERRnosuchprintjob
)
6727 * Get a new empty handle to fill in with your own info
6730 smbc_new_context(void)
6734 context
= SMB_MALLOC_P(SMBCCTX
);
6740 ZERO_STRUCTP(context
);
6742 context
->internal
= SMB_MALLOC_P(struct smbc_internal_data
);
6743 if (!context
->internal
) {
6749 ZERO_STRUCTP(context
->internal
);
6751 /* ADD REASONABLE DEFAULTS */
6753 context
->timeout
= 20000; /* 20 seconds */
6755 context
->options
.browse_max_lmb_count
= 3; /* # LMBs to query */
6756 context
->options
.urlencode_readdir_entries
= False
;/* backward compat */
6757 context
->options
.one_share_per_server
= False
;/* backward compat */
6758 context
->internal
->_share_mode
= SMBC_SHAREMODE_DENY_NONE
;
6759 /* backward compat */
6761 context
->open
= smbc_open_ctx
;
6762 context
->creat
= smbc_creat_ctx
;
6763 context
->read
= smbc_read_ctx
;
6764 context
->write
= smbc_write_ctx
;
6765 context
->close_fn
= smbc_close_ctx
;
6766 context
->unlink
= smbc_unlink_ctx
;
6767 context
->rename
= smbc_rename_ctx
;
6768 context
->lseek
= smbc_lseek_ctx
;
6769 context
->stat
= smbc_stat_ctx
;
6770 context
->fstat
= smbc_fstat_ctx
;
6771 context
->opendir
= smbc_opendir_ctx
;
6772 context
->closedir
= smbc_closedir_ctx
;
6773 context
->readdir
= smbc_readdir_ctx
;
6774 context
->getdents
= smbc_getdents_ctx
;
6775 context
->mkdir
= smbc_mkdir_ctx
;
6776 context
->rmdir
= smbc_rmdir_ctx
;
6777 context
->telldir
= smbc_telldir_ctx
;
6778 context
->lseekdir
= smbc_lseekdir_ctx
;
6779 context
->fstatdir
= smbc_fstatdir_ctx
;
6780 context
->ftruncate
= smbc_ftruncate_ctx
;
6781 context
->chmod
= smbc_chmod_ctx
;
6782 context
->utimes
= smbc_utimes_ctx
;
6783 context
->setxattr
= smbc_setxattr_ctx
;
6784 context
->getxattr
= smbc_getxattr_ctx
;
6785 context
->removexattr
= smbc_removexattr_ctx
;
6786 context
->listxattr
= smbc_listxattr_ctx
;
6787 context
->open_print_job
= smbc_open_print_job_ctx
;
6788 context
->print_file
= smbc_print_file_ctx
;
6789 context
->list_print_jobs
= smbc_list_print_jobs_ctx
;
6790 context
->unlink_print_job
= smbc_unlink_print_job_ctx
;
6792 context
->callbacks
.check_server_fn
= smbc_check_server
;
6793 context
->callbacks
.remove_unused_server_fn
= smbc_remove_unused_server
;
6795 smbc_default_cache_functions(context
);
6803 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6804 * and thus you'll be leaking memory if not handled properly.
6808 smbc_free_context(SMBCCTX
*context
,
6818 DEBUG(1,("Performing aggressive shutdown.\n"));
6820 f
= context
->internal
->_files
;
6822 (context
->close_fn
)(context
, f
);
6825 context
->internal
->_files
= NULL
;
6827 /* First try to remove the servers the nice way. */
6828 if (context
->callbacks
.purge_cached_fn(context
)) {
6831 DEBUG(1, ("Could not purge all servers, "
6832 "Nice way shutdown failed.\n"));
6833 s
= context
->internal
->_servers
;
6835 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6837 cli_shutdown(s
->cli
);
6838 (context
->callbacks
.remove_cached_srv_fn
)(context
,
6841 DLIST_REMOVE(context
->internal
->_servers
, s
);
6845 context
->internal
->_servers
= NULL
;
6849 /* This is the polite way */
6850 if ((context
->callbacks
.purge_cached_fn
)(context
)) {
6851 DEBUG(1, ("Could not purge all servers, "
6852 "free_context failed.\n"));
6856 if (context
->internal
->_servers
) {
6857 DEBUG(1, ("Active servers in context, "
6858 "free_context failed.\n"));
6862 if (context
->internal
->_files
) {
6863 DEBUG(1, ("Active files in context, "
6864 "free_context failed.\n"));
6870 /* Things we have to clean up */
6871 SAFE_FREE(context
->workgroup
);
6872 SAFE_FREE(context
->netbios_name
);
6873 SAFE_FREE(context
->user
);
6875 DEBUG(3, ("Context %p successfully freed\n", context
));
6876 SAFE_FREE(context
->internal
);
6883 * Each time the context structure is changed, we have binary backward
6884 * compatibility issues. Instead of modifying the public portions of the
6885 * context structure to add new options, instead, we put them in the internal
6886 * portion of the context structure and provide a set function for these new
6890 smbc_option_set(SMBCCTX
*context
,
6892 ... /* option_value */)
6898 smbc_get_auth_data_with_context_fn auth_fn
;
6903 va_start(ap
, option_name
);
6905 if (strcmp(option_name
, "debug_to_stderr") == 0) {
6907 * Log to standard error instead of standard output.
6909 option_value
.b
= (bool) va_arg(ap
, int);
6910 context
->internal
->_debug_stderr
= option_value
.b
;
6912 } else if (strcmp(option_name
, "full_time_names") == 0) {
6914 * Use new-style time attribute names, e.g. WRITE_TIME rather
6915 * than the old-style names such as M_TIME. This allows also
6916 * setting/getting CREATE_TIME which was previously
6917 * unimplemented. (Note that the old C_TIME was supposed to
6918 * be CHANGE_TIME but was confused and sometimes referred to
6921 option_value
.b
= (bool) va_arg(ap
, int);
6922 context
->internal
->_full_time_names
= option_value
.b
;
6924 } else if (strcmp(option_name
, "open_share_mode") == 0) {
6926 * The share mode to use for files opened with
6927 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6929 option_value
.i
= va_arg(ap
, int);
6930 context
->internal
->_share_mode
=
6931 (smbc_share_mode
) option_value
.i
;
6933 } else if (strcmp(option_name
, "auth_function") == 0) {
6935 * Use the new-style authentication function which includes
6938 option_value
.auth_fn
=
6939 va_arg(ap
, smbc_get_auth_data_with_context_fn
);
6940 context
->internal
->_auth_fn_with_context
=
6941 option_value
.auth_fn
;
6942 } else if (strcmp(option_name
, "user_data") == 0) {
6944 * Save a user data handle which may be retrieved by the user
6945 * with smbc_option_get()
6947 option_value
.v
= va_arg(ap
, void *);
6948 context
->internal
->_user_data
= option_value
.v
;
6949 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
6951 * Save an encoded value for encryption level.
6952 * 0 = off, 1 = attempt, 2 = required.
6954 option_value
.s
= va_arg(ap
, const char *);
6955 if (strcmp(option_value
.s
, "none") == 0) {
6956 context
->internal
->_smb_encryption_level
= 0;
6957 } else if (strcmp(option_value
.s
, "request") == 0) {
6958 context
->internal
->_smb_encryption_level
= 1;
6959 } else if (strcmp(option_value
.s
, "require") == 0) {
6960 context
->internal
->_smb_encryption_level
= 2;
6969 * Retrieve the current value of an option
6972 smbc_option_get(SMBCCTX
*context
,
6975 if (strcmp(option_name
, "debug_stderr") == 0) {
6977 * Log to standard error instead of standard output.
6979 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6980 return (void *) (intptr_t) context
->internal
->_debug_stderr
;
6982 return (void *) context
->internal
->_debug_stderr
;
6984 } else if (strcmp(option_name
, "full_time_names") == 0) {
6986 * Use new-style time attribute names, e.g. WRITE_TIME rather
6987 * than the old-style names such as M_TIME. This allows also
6988 * setting/getting CREATE_TIME which was previously
6989 * unimplemented. (Note that the old C_TIME was supposed to
6990 * be CHANGE_TIME but was confused and sometimes referred to
6993 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6994 return (void *) (intptr_t) context
->internal
->_full_time_names
;
6996 return (void *) context
->internal
->_full_time_names
;
6999 } else if (strcmp(option_name
, "auth_function") == 0) {
7001 * Use the new-style authentication function which includes
7004 return (void *) context
->internal
->_auth_fn_with_context
;
7005 } else if (strcmp(option_name
, "user_data") == 0) {
7007 * Save a user data handle which may be retrieved by the user
7008 * with smbc_option_get()
7010 return context
->internal
->_user_data
;
7011 } else if (strcmp(option_name
, "smb_encrypt_level") == 0) {
7013 * Return the current smb encrypt negotiate option as a string.
7015 switch (context
->internal
->_smb_encryption_level
) {
7017 return (void *) "none";
7019 return (void *) "request";
7021 return (void *) "require";
7023 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
7025 * Return the current smb encrypt status option as a bool.
7026 * false = off, true = on. We don't know what server is
7027 * being requested, so we only return true if all servers
7028 * are using an encrypted connection.
7031 unsigned int num_servers
= 0;
7033 for (s
= context
->internal
->_servers
; s
; s
= s
->next
) {
7035 if (s
->cli
->trans_enc_state
== NULL
) {
7036 return (void *)false;
7039 return (void *) (bool) (num_servers
> 0);
7047 * Initialise the library etc
7049 * We accept a struct containing handle information.
7050 * valid values for info->debug from 0 to 100,
7051 * and insist that info->fn must be non-null.
7054 smbc_init_context(SMBCCTX
*context
)
7060 if (!context
|| !context
->internal
) {
7065 /* Do not initialise the same client twice */
7066 if (context
->internal
->_initialized
) {
7070 if ((!context
->callbacks
.auth_fn
&&
7071 !context
->internal
->_auth_fn_with_context
) ||
7072 context
->debug
< 0 ||
7073 context
->debug
> 100) {
7080 if (!smbc_initialized
) {
7082 * Do some library-wide intializations the first time we get
7085 bool conf_loaded
= False
;
7086 TALLOC_CTX
*frame
= talloc_stackframe();
7088 /* Set this to what the user wants */
7089 DEBUGLEVEL
= context
->debug
;
7093 setup_logging("libsmbclient", True
);
7094 if (context
->internal
->_debug_stderr
) {
7096 x_setbuf(x_stderr
, NULL
);
7099 /* Here we would open the smb.conf file if needed ... */
7101 in_client
= True
; /* FIXME, make a param */
7103 home
= getenv("HOME");
7106 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
7107 if (lp_load(conf
, True
, False
, False
, True
)) {
7110 DEBUG(5, ("Could not load config file: %s\n",
7119 * Well, if that failed, try the get_dyn_CONFIGFILE
7120 * Which points to the standard locn, and if that
7121 * fails, silently ignore it and use the internal
7125 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, False
)) {
7126 DEBUG(5, ("Could not load config file: %s\n",
7127 get_dyn_CONFIGFILE()));
7131 * We loaded the global config file. Now lets
7132 * load user-specific modifications to the
7136 "%s/.smb/smb.conf.append",
7138 if (!lp_load(conf
, True
, False
, False
, False
)) {
7140 ("Could not append config file: "
7149 load_interfaces(); /* Load the list of interfaces ... */
7151 reopen_logs(); /* Get logging working ... */
7154 * Block SIGPIPE (from lib/util_sock.c: write())
7155 * It is not needed and should not stop execution
7157 BlockSignals(True
, SIGPIPE
);
7159 /* Done with one-time initialisation */
7160 smbc_initialized
= 1;
7165 if (!context
->user
) {
7167 * FIXME: Is this the best way to get the user info?
7169 user
= getenv("USER");
7170 /* walk around as "guest" if no username can be found */
7171 if (!user
) context
->user
= SMB_STRDUP("guest");
7172 else context
->user
= SMB_STRDUP(user
);
7175 if (!context
->netbios_name
) {
7177 * We try to get our netbios name from the config. If that
7178 * fails we fall back on constructing our netbios name from
7181 if (global_myname()) {
7182 context
->netbios_name
= SMB_STRDUP(global_myname());
7186 * Hmmm, I want to get hostname as well, but I am too
7187 * lazy for the moment
7190 context
->netbios_name
= (char *)SMB_MALLOC(17);
7191 if (!context
->netbios_name
) {
7195 slprintf(context
->netbios_name
, 16,
7196 "smbc%s%d", context
->user
, pid
);
7200 DEBUG(1, ("Using netbios name %s.\n", context
->netbios_name
));
7202 if (!context
->workgroup
) {
7203 if (lp_workgroup()) {
7204 context
->workgroup
= SMB_STRDUP(lp_workgroup());
7207 /* TODO: Think about a decent default workgroup */
7208 context
->workgroup
= SMB_STRDUP("samba");
7212 DEBUG(1, ("Using workgroup %s.\n", context
->workgroup
));
7214 /* shortest timeout is 1 second */
7215 if (context
->timeout
> 0 && context
->timeout
< 1000)
7216 context
->timeout
= 1000;
7219 * FIXME: Should we check the function pointers here?
7222 context
->internal
->_initialized
= True
;
7228 /* Return the verion of samba, and thus libsmbclient */
7232 return samba_version_string();