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,
1044 struct sockaddr_storage ss;
1045 struct cli_state *ipc_cli;
1046 struct rpc_pipe_client *pipe_hnd;
1048 SMBCSRV *ipc_srv=NULL;
1051 * See if we've already created this special connection. Reference
1052 * our "special" share name '*IPC$', which is an impossible real share
1053 * name due to the leading asterisk.
1055 ipc_srv = find_server(ctx, context, server, "*IPC$",
1056 pp_workgroup, pp_username, pp_password);
1059 /* We didn't find a cached connection. Get the password */
1060 if (!*pp_password || (*pp_password)[0] == '\0') {
1061 /* ... then retrieve it now. */
1062 call_auth_fn(ctx, context, server, share,
1063 pp_workgroup, pp_username, pp_password);
1064 if (!*pp_workgroup || !*pp_username || !*pp_password) {
1071 if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
1072 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
1076 nt_status = cli_full_connection(&ipc_cli,
1077 global_myname(), server,
1078 &ss, 0, "IPC$", "?????",
1084 if (! NT_STATUS_IS_OK(nt_status)) {
1085 DEBUG(1,("cli_full_connection failed! (%s)\n",
1086 nt_errstr(nt_status)));
1091 if (context->internal->_smb_encryption_level) {
1092 /* Attempt UNIX smb encryption. */
1093 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli,
1099 * context->internal->_smb_encryption_level == 1
1100 * means don't fail if encryption can't be negotiated,
1101 * == 2 means fail if encryption can't be negotiated.
1104 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
1106 if (context->internal->_smb_encryption_level == 2) {
1107 cli_shutdown(ipc_cli);
1112 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
1115 ipc_srv = SMB_MALLOC_P(SMBCSRV);
1118 cli_shutdown(ipc_cli);
1122 ZERO_STRUCTP(ipc_srv);
1123 ipc_srv->cli = ipc_cli;
1126 pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
1130 DEBUG(1, ("cli_nt_session_open fail!\n"));
1132 cli_shutdown(ipc_srv->cli);
1138 * Some systems don't support
1139 * SEC_RIGHTS_MAXIMUM_ALLOWED, but NT sends 0x2000000
1140 * so we might as well do it too.
1143 nt_status = rpccli_lsa_open_policy(
1147 GENERIC_EXECUTE_ACCESS,
1150 if (!NT_STATUS_IS_OK(nt_status)) {
1151 errno = smbc_errno(context, ipc_srv->cli);
1152 cli_shutdown(ipc_srv->cli);
1157 /* now add it to the cache (internal or external) */
1159 errno = 0; /* let cache function set errno if it likes */
1160 if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
1165 DEBUG(3, (" Failed to add server to cache\n"));
1169 cli_shutdown(ipc_srv->cli);
1174 DLIST_ADD(context->internal->_servers, ipc_srv);
1181 * Routine to open() a file ...
1185 smbc_open_ctx(SMBCCTX *context,
1190 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
1192 char *targetpath = NULL;
1193 struct cli_state *targetcli = NULL;
1194 SMBCSRV *srv = NULL;
1195 SMBCFILE *file = NULL;
1197 TALLOC_CTX *frame = talloc_stackframe();
1199 if (!context || !context->internal ||
1200 !context->internal->_initialized) {
1202 errno = EINVAL; /* Best I can think of ... */
1216 if (smbc_parse_path(frame,
1231 if (!user || user[0] == (char)0) {
1232 user = talloc_strdup(frame, context->user);
1240 srv = smbc_server(frame, context, True,
1241 server, share, &workgroup, &user, &password);
1244 if (errno == EPERM) errno = EACCES;
1246 return NULL; /* smbc_server sets errno */
1249 /* Hmmm, the test for a directory is suspect here ... FIXME */
1251 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
1254 file = SMB_MALLOC_P(SMBCFILE);
1264 /*d_printf(">>>open: resolving %s\n", path);*/
1265 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
1266 d_printf("Could not resolve %s\n", path);
1271 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
1273 if ((fd = cli_open(targetcli, targetpath, flags,
1274 context->internal->_share_mode)) < 0) {
1276 /* Handle the error ... */
1279 errno = smbc_errno(context, targetcli);
1285 /* Fill in file struct */
1288 file->fname = SMB_STRDUP(fname);
1293 DLIST_ADD(context->internal->_files, file);
1296 * If the file was opened in O_APPEND mode, all write
1297 * operations should be appended to the file. To do that,
1298 * though, using this protocol, would require a getattrE()
1299 * call for each and every write, to determine where the end
1300 * of the file is. (There does not appear to be an append flag
1301 * in the protocol.) Rather than add all of that overhead of
1302 * retrieving the current end-of-file offset prior to each
1303 * write operation, we'll assume that most append operations
1304 * will continuously write, so we'll just set the offset to
1305 * the end of the file now and hope that's adequate.
1307 * Note to self: If this proves inadequate, and O_APPEND
1308 * should, in some cases, be forced for each write, add a
1309 * field in the context options structure, for
1310 * "strict_append_mode" which would select between the current
1311 * behavior (if FALSE) or issuing a getattrE() prior to each
1312 * write and forcing the write to the end of the file (if
1313 * TRUE). Adding that capability will likely require adding
1314 * an "append" flag into the _SMBCFILE structure to track
1315 * whether a file was opened in O_APPEND mode. -- djl
1317 if (flags & O_APPEND) {
1318 if (smbc_lseek_ctx(context, file, 0, SEEK_END) < 0) {
1319 (void) smbc_close_ctx(context, file);
1331 /* Check if opendir needed ... */
1336 eno = smbc_errno(context, srv->cli);
1337 file = (context->opendir)(context, fname);
1338 if (!file) errno = eno;
1344 errno = EINVAL; /* FIXME, correct errno ? */
1351 * Routine to create a file
1354 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
1357 smbc_creat_ctx(SMBCCTX *context,
1362 if (!context || !context->internal ||
1363 !context->internal->_initialized) {
1370 return smbc_open_ctx(context, path, creat_bits, mode);
1374 * Routine to read() a file ...
1378 smbc_read_ctx(SMBCCTX *context,
1384 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
1386 char *targetpath = NULL;
1387 struct cli_state *targetcli = NULL;
1388 TALLOC_CTX *frame = talloc_stackframe();
1393 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
1394 * appears to pass file->offset (which is type off_t) differently than
1395 * a local variable of type off_t. Using local variable "offset" in
1396 * the call to cli_read() instead of file->offset fixes a problem
1397 * retrieving data at an offset greater than 4GB.
1401 if (!context || !context->internal ||
1402 !context->internal->_initialized) {
1409 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
1411 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1418 offset = file->offset;
1420 /* Check that the buffer exists ... */
1429 /*d_printf(">>>read: parsing %s\n", file->fname);*/
1430 if (smbc_parse_path(frame,
1445 /*d_printf(">>>read: resolving %s\n", path);*/
1446 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1447 &targetcli, &targetpath)) {
1448 d_printf("Could not resolve %s\n", path);
1452 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
1454 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
1458 errno = smbc_errno(context, targetcli);
1464 file->offset += ret;
1466 DEBUG(4, (" --> %d\n", ret));
1469 return ret; /* Success, ret bytes of data ... */
1474 * Routine to write() a file ...
1478 smbc_write_ctx(SMBCCTX *context,
1485 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
1487 char *targetpath = NULL;
1488 struct cli_state *targetcli = NULL;
1489 TALLOC_CTX *frame = talloc_stackframe();
1491 /* First check all pointers before dereferencing them */
1493 if (!context || !context->internal ||
1494 !context->internal->_initialized) {
1501 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1507 /* Check that the buffer exists ... */
1516 offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
1518 /*d_printf(">>>write: parsing %s\n", file->fname);*/
1519 if (smbc_parse_path(frame,
1534 /*d_printf(">>>write: resolving %s\n", path);*/
1535 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1536 &targetcli, &targetpath)) {
1537 d_printf("Could not resolve %s\n", path);
1541 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
1543 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
1546 errno = smbc_errno(context, targetcli);
1552 file->offset += ret;
1555 return ret; /* Success, 0 bytes of data ... */
1559 * Routine to close() a file ...
1563 smbc_close_ctx(SMBCCTX *context,
1567 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
1569 char *targetpath = NULL;
1570 struct cli_state *targetcli = NULL;
1571 TALLOC_CTX *frame = talloc_stackframe();
1573 if (!context || !context->internal ||
1574 !context->internal->_initialized) {
1581 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
1590 return (context->closedir)(context, file);
1593 /*d_printf(">>>close: parsing %s\n", file->fname);*/
1594 if (smbc_parse_path(frame,
1609 /*d_printf(">>>close: resolving %s\n", path);*/
1610 if (!cli_resolve_path(frame, "", file->srv->cli, path,
1611 &targetcli, &targetpath)) {
1612 d_printf("Could not resolve %s\n", path);
1616 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
1618 if (!cli_close(targetcli, file->cli_fd)) {
1620 DEBUG(3, ("cli_close failed on %s. purging server.\n",
1622 /* Deallocate slot and remove the server
1623 * from the server cache if unused */
1624 errno = smbc_errno(context, targetcli);
1626 DLIST_REMOVE(context->internal->_files, file);
1627 SAFE_FREE(file->fname);
1629 (context->callbacks.remove_unused_server_fn)(context, srv);
1635 DLIST_REMOVE(context->internal->_files, file);
1636 SAFE_FREE(file->fname);
1644 * Get info from an SMB server on a file. Use a qpathinfo call first
1645 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
1648 smbc_getatr(SMBCCTX * context,
1653 struct timespec *create_time_ts,
1654 struct timespec *access_time_ts,
1655 struct timespec *write_time_ts,
1656 struct timespec *change_time_ts,
1659 char *fixedpath = NULL;
1660 char *targetpath = NULL;
1661 struct cli_state *targetcli = NULL;
1663 TALLOC_CTX *frame = talloc_stackframe();
1665 if (!context || !context->internal ||
1666 !context->internal->_initialized) {
1672 /* path fixup for . and .. */
1673 if (strequal(path, ".") || strequal(path, "..")) {
1674 fixedpath = talloc_strdup(frame, "\\");
1681 fixedpath = talloc_strdup(frame, path);
1687 trim_string(fixedpath, NULL, "\\..");
1688 trim_string(fixedpath, NULL, "\\.");
1690 DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
1692 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
1693 &targetcli, &targetpath)) {
1694 d_printf("Couldn't resolve %s\n", path);
1699 if (!srv->no_pathinfo2 &&
1700 cli_qpathinfo2(targetcli, targetpath,
1710 /* if this is NT then don't bother with the getatr */
1711 if (targetcli->capabilities & CAP_NT_SMBS) {
1717 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
1719 struct timespec w_time_ts;
1721 w_time_ts = convert_time_t_to_timespec(write_time);
1723 if (write_time_ts != NULL) {
1724 *write_time_ts = w_time_ts;
1727 if (create_time_ts != NULL) {
1728 *create_time_ts = w_time_ts;
1731 if (access_time_ts != NULL) {
1732 *access_time_ts = w_time_ts;
1735 if (change_time_ts != NULL) {
1736 *change_time_ts = w_time_ts;
1739 srv->no_pathinfo2 = True;
1751 * Set file info on an SMB server. Use setpathinfo call first. If that
1752 * fails, use setattrE..
1754 * Access and modification time parameters are always used and must be
1755 * provided. Create time, if zero, will be determined from the actual create
1756 * time of the file. If non-zero, the create time will be set as well.
1758 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
1761 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
1770 TALLOC_CTX *frame = talloc_stackframe();
1773 * First, try setpathinfo (if qpathinfo succeeded), for it is the
1774 * modern function for "new code" to be using, and it works given a
1775 * filename rather than requiring that the file be opened to have its
1776 * attributes manipulated.
1778 if (srv->no_pathinfo ||
1779 ! cli_setpathinfo(srv->cli, path,
1787 * setpathinfo is not supported; go to plan B.
1789 * cli_setatr() does not work on win98, and it also doesn't
1790 * support setting the access time (only the modification
1791 * time), so in all cases, we open the specified file and use
1792 * cli_setattrE() which should work on all OS versions, and
1793 * supports both times.
1796 /* Don't try {q,set}pathinfo() again, with this server */
1797 srv->no_pathinfo = True;
1800 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
1802 errno = smbc_errno(context, srv->cli);
1807 /* Set the new attributes */
1808 ret = cli_setattrE(srv->cli, fd,
1813 /* Close the file */
1814 cli_close(srv->cli, fd);
1817 * Unfortunately, setattrE() doesn't have a provision for
1818 * setting the access mode (attributes). We'll have to try
1819 * cli_setatr() for that, and with only this parameter, it
1820 * seems to work on win98.
1822 if (ret && mode != (uint16) -1) {
1823 ret = cli_setatr(srv->cli, path, mode, 0);
1827 errno = smbc_errno(context, srv->cli);
1838 * Routine to unlink() a file
1842 smbc_unlink_ctx(SMBCCTX *context,
1845 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
1847 char *targetpath = NULL;
1848 struct cli_state *targetcli = NULL;
1849 SMBCSRV *srv = NULL;
1850 TALLOC_CTX *frame = talloc_stackframe();
1852 if (!context || !context->internal ||
1853 !context->internal->_initialized) {
1854 errno = EINVAL; /* Best I can think of ... */
1867 if (smbc_parse_path(frame,
1882 if (!user || user[0] == (char)0) {
1883 user = talloc_strdup(frame, context->user);
1891 srv = smbc_server(frame, context, True,
1892 server, share, &workgroup, &user, &password);
1896 return -1; /* smbc_server sets errno */
1900 /*d_printf(">>>unlink: resolving %s\n", path);*/
1901 if (!cli_resolve_path(frame, "", srv->cli, path,
1902 &targetcli, &targetpath)) {
1903 d_printf("Could not resolve %s\n", path);
1907 /*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
1909 if (!cli_unlink(targetcli, targetpath)) {
1911 errno = smbc_errno(context, targetcli);
1913 if (errno == EACCES) { /* Check if the file is a directory */
1918 struct timespec write_time_ts;
1919 struct timespec access_time_ts;
1920 struct timespec change_time_ts;
1923 if (!smbc_getatr(context, srv, path, &mode, &size,
1930 /* Hmmm, bad error ... What? */
1932 errno = smbc_errno(context, targetcli);
1939 if (IS_DOS_DIR(mode))
1942 errno = saverr; /* Restore this */
1953 return 0; /* Success ... */
1958 * Routine to rename() a file
1962 smbc_rename_ctx(SMBCCTX *ocontext,
1967 char *server1 = NULL;
1968 char *share1 = NULL;
1969 char *server2 = NULL;
1970 char *share2 = NULL;
1973 char *password1 = NULL;
1974 char *password2 = NULL;
1975 char *workgroup = NULL;
1978 char *targetpath1 = NULL;
1979 char *targetpath2 = NULL;
1980 struct cli_state *targetcli1 = NULL;
1981 struct cli_state *targetcli2 = NULL;
1982 SMBCSRV *srv = NULL;
1983 TALLOC_CTX *frame = talloc_stackframe();
1985 if (!ocontext || !ncontext ||
1986 !ocontext->internal || !ncontext->internal ||
1987 !ocontext->internal->_initialized ||
1988 !ncontext->internal->_initialized) {
1989 errno = EINVAL; /* Best I can think of ... */
1994 if (!oname || !nname) {
2000 DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
2002 if (smbc_parse_path(frame,
2017 if (!user1 || user1[0] == (char)0) {
2018 user1 = talloc_strdup(frame, ocontext->user);
2026 if (smbc_parse_path(frame,
2041 if (!user2 || user2[0] == (char)0) {
2042 user2 = talloc_strdup(frame, ncontext->user);
2050 if (strcmp(server1, server2) || strcmp(share1, share2) ||
2051 strcmp(user1, user2)) {
2052 /* Can't rename across file systems, or users?? */
2058 srv = smbc_server(frame, ocontext, True,
2059 server1, share1, &workgroup, &user1, &password1);
2066 /*d_printf(">>>rename: resolving %s\n", path1);*/
2067 if (!cli_resolve_path(frame, "", srv->cli, path1,
2068 &targetcli1, &targetpath1)) {
2069 d_printf("Could not resolve %s\n", path1);
2073 /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
2074 /*d_printf(">>>rename: resolving %s\n", path2);*/
2075 if (!cli_resolve_path(frame, "", srv->cli, path2,
2076 &targetcli2, &targetpath2)) {
2077 d_printf("Could not resolve %s\n", path2);
2081 /*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2083 if (strcmp(targetcli1->desthost, targetcli2->desthost) ||
2084 strcmp(targetcli1->share, targetcli2->share))
2086 /* can't rename across file systems */
2092 if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
2093 int eno = smbc_errno(ocontext, targetcli1);
2095 if (eno != EEXIST ||
2096 !cli_unlink(targetcli1, targetpath2) ||
2097 !cli_rename(targetcli1, targetpath1, targetpath2)) {
2107 return 0; /* Success */
2111 * A routine to lseek() a file
2115 smbc_lseek_ctx(SMBCCTX *context,
2121 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
2123 char *targetpath = NULL;
2124 struct cli_state *targetcli = NULL;
2125 TALLOC_CTX *frame = talloc_stackframe();
2127 if (!context || !context->internal ||
2128 !context->internal->_initialized) {
2134 if (!file || !DLIST_CONTAINS(context->internal->_files, file)) {
2146 return -1; /* Can't lseek a dir ... */
2152 file->offset = offset;
2156 file->offset += offset;
2160 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
2161 if (smbc_parse_path(frame,
2176 /*d_printf(">>>lseek: resolving %s\n", path);*/
2177 if (!cli_resolve_path(frame, "", file->srv->cli, path,
2178 &targetcli, &targetpath)) {
2179 d_printf("Could not resolve %s\n", path);
2183 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
2185 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
2186 &size, NULL, NULL, NULL, NULL, NULL))
2188 SMB_OFF_T b_size = size;
2189 if (!cli_getattrE(targetcli, file->cli_fd,
2190 NULL, &b_size, NULL, NULL, NULL))
2198 file->offset = size + offset;
2208 return file->offset;
2213 * Generate an inode number from file name for those things that need it
2217 smbc_inode(SMBCCTX *context,
2220 if (!context || !context->internal ||
2221 !context->internal->_initialized) {
2228 if (!*name) return 2; /* FIXME, why 2 ??? */
2229 return (ino_t)str_checksum(name);
2234 * Routine to put basic stat info into a stat structure ... Used by stat and
2239 smbc_setup_stat(SMBCCTX *context,
2245 TALLOC_CTX *frame = talloc_stackframe();
2249 if (IS_DOS_DIR(mode)) {
2250 st->st_mode = SMBC_DIR_MODE;
2252 st->st_mode = SMBC_FILE_MODE;
2255 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
2256 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
2257 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
2258 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
2261 #ifdef HAVE_STAT_ST_BLKSIZE
2262 st->st_blksize = 512;
2264 #ifdef HAVE_STAT_ST_BLOCKS
2265 st->st_blocks = (size+511)/512;
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, path, 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 open a directory
2487 * We accept the URL syntax explained in smbc_parse_path(), above.
2491 smbc_remove_dir(SMBCFILE *dir)
2493 struct smbc_dir_list *d,*f;
2500 SAFE_FREE(f->dirent);
2505 dir->dir_list = dir->dir_end = dir->dir_next = NULL;
2510 add_dirent(SMBCFILE *dir,
2512 const char *comment,
2515 struct smbc_dirent *dirent;
2517 int name_length = (name == NULL ? 0 : strlen(name));
2518 int comment_len = (comment == NULL ? 0 : strlen(comment));
2521 * Allocate space for the dirent, which must be increased by the
2522 * size of the name and the comment and 1 each for the null terminator.
2525 size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
2527 dirent = (struct smbc_dirent *)SMB_MALLOC(size);
2531 dir->dir_error = ENOMEM;
2536 ZERO_STRUCTP(dirent);
2538 if (dir->dir_list == NULL) {
2540 dir->dir_list = SMB_MALLOC_P(struct smbc_dir_list);
2541 if (!dir->dir_list) {
2544 dir->dir_error = ENOMEM;
2548 ZERO_STRUCTP(dir->dir_list);
2550 dir->dir_end = dir->dir_next = dir->dir_list;
2554 dir->dir_end->next = SMB_MALLOC_P(struct smbc_dir_list);
2556 if (!dir->dir_end->next) {
2559 dir->dir_error = ENOMEM;
2563 ZERO_STRUCTP(dir->dir_end->next);
2565 dir->dir_end = dir->dir_end->next;
2568 dir->dir_end->next = NULL;
2569 dir->dir_end->dirent = dirent;
2571 dirent->smbc_type = type;
2572 dirent->namelen = name_length;
2573 dirent->commentlen = comment_len;
2574 dirent->dirlen = size;
2577 * dirent->namelen + 1 includes the null (no null termination needed)
2578 * Ditto for dirent->commentlen.
2579 * The space for the two null bytes was allocated.
2581 strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
2582 dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
2583 strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
2590 list_unique_wg_fn(const char *name,
2592 const char *comment,
2595 SMBCFILE *dir = (SMBCFILE *)state;
2596 struct smbc_dir_list *dir_list;
2597 struct smbc_dirent *dirent;
2601 dirent_type = dir->dir_type;
2603 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2605 /* An error occurred, what do we do? */
2606 /* FIXME: Add some code here */
2609 /* Point to the one just added */
2610 dirent = dir->dir_end->dirent;
2612 /* See if this was a duplicate */
2613 for (dir_list = dir->dir_list;
2614 dir_list != dir->dir_end;
2615 dir_list = dir_list->next) {
2617 strcmp(dir_list->dirent->name, dirent->name) == 0) {
2618 /* Duplicate. End end of list need to be removed. */
2622 if (do_remove && dir_list->next == dir->dir_end) {
2623 /* Found the end of the list. Remove it. */
2624 dir->dir_end = dir_list;
2625 free(dir_list->next);
2627 dir_list->next = NULL;
2634 list_fn(const char *name,
2636 const char *comment,
2639 SMBCFILE *dir = (SMBCFILE *)state;
2643 * We need to process the type a little ...
2645 * Disk share = 0x00000000
2646 * Print share = 0x00000001
2647 * Comms share = 0x00000002 (obsolete?)
2648 * IPC$ share = 0x00000003
2650 * administrative shares:
2651 * ADMIN$, IPC$, C$, D$, E$ ... are type |= 0x80000000
2654 if (dir->dir_type == SMBC_FILE_SHARE) {
2656 case 0 | 0x80000000:
2658 dirent_type = SMBC_FILE_SHARE;
2662 dirent_type = SMBC_PRINTER_SHARE;
2666 dirent_type = SMBC_COMMS_SHARE;
2669 case 3 | 0x80000000:
2671 dirent_type = SMBC_IPC_SHARE;
2675 dirent_type = SMBC_FILE_SHARE; /* FIXME, error? */
2680 dirent_type = dir->dir_type;
2683 if (add_dirent(dir, name, comment, dirent_type) < 0) {
2685 /* An error occurred, what do we do? */
2686 /* FIXME: Add some code here */
2692 dir_list_fn(const char *mnt,
2698 if (add_dirent((SMBCFILE *)state, finfo->name, "",
2699 (finfo->mode&aDIR?SMBC_DIR:SMBC_FILE)) < 0) {
2701 /* Handle an error ... */
2703 /* FIXME: Add some code ... */
2710 net_share_enum_rpc(struct cli_state *cli,
2711 void (*fn)(const char *name,
2713 const char *comment,
2720 uint32 info_level = 1;
2721 uint32 preferred_len = 0xffffffff;
2723 SRV_SHARE_INFO_CTR ctr;
2725 fstring comment = "";
2726 struct rpc_pipe_client *pipe_hnd;
2729 /* Open the server service pipe */
2730 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &nt_status);
2732 DEBUG(1, ("net_share_enum_rpc pipe open fail!\n"));
2736 /* Issue the NetShareEnum RPC call and retrieve the response */
2737 init_enum_hnd(&enum_hnd, 0);
2738 result = rpccli_srvsvc_net_share_enum(pipe_hnd,
2745 /* Was it successful? */
2746 if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
2747 /* Nope. Go clean up. */
2751 /* For each returned entry... */
2752 for (i = 0; i < ctr.num_entries; i++) {
2754 /* pull out the share name */
2755 rpcstr_pull_unistr2_fstring(
2756 name, &ctr.share.info1[i].info_1_str.uni_netname);
2758 /* pull out the share's comment */
2759 rpcstr_pull_unistr2_fstring(
2760 comment, &ctr.share.info1[i].info_1_str.uni_remark);
2762 /* Get the type value */
2763 type = ctr.share.info1[i].info_1.type;
2765 /* Add this share to the list */
2766 (*fn)(name, type, comment, state);
2770 /* Close the server service pipe */
2771 cli_rpc_pipe_close(pipe_hnd);
2773 /* Tell 'em if it worked */
2774 return W_ERROR_IS_OK(result) ? 0 : -1;
2780 smbc_opendir_ctx(SMBCCTX *context,
2784 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *options = NULL;
2785 char *workgroup = NULL;
2789 SMBCSRV *srv = NULL;
2790 SMBCFILE *dir = NULL;
2791 struct _smbc_callbacks *cb = NULL;
2792 struct sockaddr_storage rem_ss;
2793 TALLOC_CTX *frame = talloc_stackframe();
2795 if (!context || !context->internal ||
2796 !context->internal->_initialized) {
2797 DEBUG(4, ("no valid context\n"));
2798 errno = EINVAL + 8192;
2805 DEBUG(4, ("no valid fname\n"));
2806 errno = EINVAL + 8193;
2811 if (smbc_parse_path(frame,
2821 DEBUG(4, ("no valid path\n"));
2822 errno = EINVAL + 8194;
2827 DEBUG(4, ("parsed path: fname='%s' server='%s' share='%s' "
2828 "path='%s' options='%s'\n",
2829 fname, server, share, path, options));
2831 /* Ensure the options are valid */
2832 if (smbc_check_options(server, share, path, options)) {
2833 DEBUG(4, ("unacceptable options (%s)\n", options));
2834 errno = EINVAL + 8195;
2839 if (!user || user[0] == (char)0) {
2840 user = talloc_strdup(frame, context->user);
2848 dir = SMB_MALLOC_P(SMBCFILE);
2859 dir->fname = SMB_STRDUP(fname);
2863 dir->dir_list = dir->dir_next = dir->dir_end = NULL;
2865 if (server[0] == (char)0) {
2870 struct ip_service *ip_list;
2871 struct ip_service server_addr;
2872 struct user_auth_info u_info;
2874 if (share[0] != (char)0 || path[0] != (char)0) {
2876 errno = EINVAL + 8196;
2878 SAFE_FREE(dir->fname);
2885 /* Determine how many local master browsers to query */
2886 max_lmb_count = (context->options.browse_max_lmb_count == 0
2888 : context->options.browse_max_lmb_count);
2890 memset(&u_info, '\0', sizeof(u_info));
2891 u_info.username = talloc_strdup(frame,user);
2892 u_info.password = talloc_strdup(frame,password);
2893 if (!u_info.username || !u_info.password) {
2895 SAFE_FREE(dir->fname);
2903 * We have server and share and path empty but options
2904 * requesting that we scan all master browsers for their list
2905 * of workgroups/domains. This implies that we must first try
2906 * broadcast queries to find all master browsers, and if that
2907 * doesn't work, then try our other methods which return only
2908 * a single master browser.
2912 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
2918 if (!find_master_ip(workgroup, &server_addr.ss)) {
2921 SAFE_FREE(dir->fname);
2929 ip_list = (struct ip_service *)memdup(
2930 &server_addr, sizeof(server_addr));
2931 if (ip_list == NULL) {
2939 for (i = 0; i < count && i < max_lmb_count; i++) {
2940 char addr[INET6_ADDRSTRLEN];
2941 char *wg_ptr = NULL;
2942 struct cli_state *cli = NULL;
2944 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
2945 DEBUG(99, ("Found master browser %d of %d: %s\n",
2946 i+1, MAX(count, max_lmb_count),
2949 cli = get_ipc_connect_master_ip(talloc_tos(),
2953 /* cli == NULL is the master browser refused to talk or
2954 could not be found */
2959 workgroup = talloc_strdup(frame, wg_ptr);
2960 server = talloc_strdup(frame, cli->desthost);
2964 if (!workgroup || !server) {
2970 DEBUG(4, ("using workgroup %s %s\n",
2971 workgroup, server));
2974 * For each returned master browser IP address, get a
2975 * connection to IPC$ on the server if we do not
2976 * already have one, and determine the
2977 * workgroups/domains that it knows about.
2980 srv = smbc_server(frame, context, True, server, "IPC$",
2981 &workgroup, &user, &password);
2987 dir->dir_type = SMBC_WORKGROUP;
2989 /* Now, list the stuff ... */
2991 if (!cli_NetServerEnum(srv->cli,
2993 SV_TYPE_DOMAIN_ENUM,
3003 * Server not an empty string ... Check the rest and see what
3006 if (*share == '\0') {
3007 if (*path != '\0') {
3009 /* Should not have empty share with path */
3010 errno = EINVAL + 8197;
3012 SAFE_FREE(dir->fname);
3021 * We don't know if <server> is really a server name
3022 * or is a workgroup/domain name. If we already have
3023 * a server structure for it, we'll use it.
3024 * Otherwise, check to see if <server><1D>,
3025 * <server><1B>, or <server><20> translates. We check
3026 * to see if <server> is an IP address first.
3030 * See if we have an existing server. Do not
3031 * establish a connection if one does not already
3034 srv = smbc_server(frame, context, False, server, "IPC$",
3035 &workgroup, &user, &password);
3038 * If no existing server and not an IP addr, look for
3042 !is_ipaddress(server) &&
3043 (resolve_name(server, &rem_ss, 0x1d) || /* LMB */
3044 resolve_name(server, &rem_ss, 0x1b) )) { /* DMB */
3048 dir->dir_type = SMBC_SERVER;
3051 * Get the backup list ...
3053 if (!name_status_find(server, 0, 0,
3054 &rem_ss, buserver)) {
3056 DEBUG(0, ("Could not get name of "
3057 "local/domain master browser "
3058 "for server %s\n", server));
3060 SAFE_FREE(dir->fname);
3070 * Get a connection to IPC$ on the server if
3071 * we do not already have one
3073 srv = smbc_server(frame, context, True,
3075 &workgroup, &user, &password);
3077 DEBUG(0, ("got no contact to IPC$\n"));
3079 SAFE_FREE(dir->fname);
3089 /* Now, list the servers ... */
3090 if (!cli_NetServerEnum(srv->cli, server,
3091 0x0000FFFE, list_fn,
3095 SAFE_FREE(dir->fname);
3102 (resolve_name(server, &rem_ss, 0x20))) {
3104 /* If we hadn't found the server, get one now */
3106 srv = smbc_server(frame, context, True,
3114 SAFE_FREE(dir->fname);
3122 dir->dir_type = SMBC_FILE_SHARE;
3125 /* List the shares ... */
3127 if (net_share_enum_rpc(
3130 (void *) dir) < 0 &&
3136 errno = cli_errno(srv->cli);
3138 SAFE_FREE(dir->fname);
3146 /* Neither the workgroup nor server exists */
3147 errno = ECONNREFUSED;
3149 SAFE_FREE(dir->fname);
3159 * The server and share are specified ... work from
3163 struct cli_state *targetcli;
3165 /* We connect to the server and list the directory */
3166 dir->dir_type = SMBC_FILE_SHARE;
3168 srv = smbc_server(frame, context, True, server, share,
3169 &workgroup, &user, &password);
3173 SAFE_FREE(dir->fname);
3182 /* Now, list the files ... */
3184 p = path + strlen(path);
3185 path = talloc_asprintf_append(path, "\\*");
3188 SAFE_FREE(dir->fname);
3195 if (!cli_resolve_path(frame, "", srv->cli, path,
3196 &targetcli, &targetpath)) {
3197 d_printf("Could not resolve %s\n", path);
3199 SAFE_FREE(dir->fname);
3206 if (cli_list(targetcli, targetpath,
3207 aDIR | aSYSTEM | aHIDDEN,
3208 dir_list_fn, (void *)dir) < 0) {
3211 SAFE_FREE(dir->fname);
3214 saved_errno = smbc_errno(context, targetcli);
3216 if (saved_errno == EINVAL) {
3218 * See if they asked to opendir something
3219 * other than a directory. If so, the
3220 * converted error value we got would have
3221 * been EINVAL rather than ENOTDIR.
3223 *p = '\0'; /* restore original path */
3225 if (smbc_getatr(context, srv, path,
3227 NULL, NULL, NULL, NULL,
3229 ! IS_DOS_DIR(mode)) {
3231 /* It is. Correct the error value */
3232 saved_errno = ENOTDIR;
3237 * If there was an error and the server is no
3240 cb = &context->callbacks;
3241 if (cli_is_error(targetcli) &&
3242 (cb->check_server_fn)(context, srv)) {
3244 /* ... then remove it. */
3245 if ((cb->remove_unused_server_fn)(context,
3248 * We could not remove the
3249 * server completely, remove
3250 * it from the cache so we
3251 * will not get it again. It
3252 * will be removed when the
3253 * last file/dir is closed.
3255 (cb->remove_cached_srv_fn)(context,
3260 errno = saved_errno;
3268 DLIST_ADD(context->internal->_files, dir);
3275 * Routine to close a directory
3279 smbc_closedir_ctx(SMBCCTX *context,
3282 TALLOC_CTX *frame = talloc_stackframe();
3284 if (!context || !context->internal ||
3285 !context->internal->_initialized) {
3291 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3297 smbc_remove_dir(dir); /* Clean it up */
3299 DLIST_REMOVE(context->internal->_files, dir);
3303 SAFE_FREE(dir->fname);
3304 SAFE_FREE(dir); /* Free the space too */
3313 smbc_readdir_internal(SMBCCTX * context,
3314 struct smbc_dirent *dest,
3315 struct smbc_dirent *src,
3316 int max_namebuf_len)
3318 if (context->options.urlencode_readdir_entries) {
3320 /* url-encode the name. get back remaining buffer space */
3322 smbc_urlencode(dest->name, src->name, max_namebuf_len);
3324 /* We now know the name length */
3325 dest->namelen = strlen(dest->name);
3327 /* Save the pointer to the beginning of the comment */
3328 dest->comment = dest->name + dest->namelen + 1;
3330 /* Copy the comment */
3331 strncpy(dest->comment, src->comment, max_namebuf_len - 1);
3332 dest->comment[max_namebuf_len - 1] = '\0';
3334 /* Save other fields */
3335 dest->smbc_type = src->smbc_type;
3336 dest->commentlen = strlen(dest->comment);
3337 dest->dirlen = ((dest->comment + dest->commentlen + 1) -
3341 /* No encoding. Just copy the entry as is. */
3342 memcpy(dest, src, src->dirlen);
3343 dest->comment = (char *)(&dest->name + src->namelen + 1);
3349 * Routine to get a directory entry
3352 struct smbc_dirent *
3353 smbc_readdir_ctx(SMBCCTX *context,
3357 struct smbc_dirent *dirp, *dirent;
3358 TALLOC_CTX *frame = talloc_stackframe();
3360 /* Check that all is ok first ... */
3362 if (!context || !context->internal ||
3363 !context->internal->_initialized) {
3366 DEBUG(0, ("Invalid context in smbc_readdir_ctx()\n"));
3372 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3375 DEBUG(0, ("Invalid dir in smbc_readdir_ctx()\n"));
3381 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3384 DEBUG(0, ("Found file vs directory in smbc_readdir_ctx()\n"));
3390 if (!dir->dir_next) {
3395 dirent = dir->dir_next->dirent;
3404 dirp = (struct smbc_dirent *)context->internal->_dirent;
3405 maxlen = (sizeof(context->internal->_dirent) -
3406 sizeof(struct smbc_dirent));
3408 smbc_readdir_internal(context, dirp, dirent, maxlen);
3410 dir->dir_next = dir->dir_next->next;
3417 * Routine to get directory entries
3421 smbc_getdents_ctx(SMBCCTX *context,
3423 struct smbc_dirent *dirp,
3429 char *ndir = (char *)dirp;
3430 struct smbc_dir_list *dirlist;
3431 TALLOC_CTX *frame = talloc_stackframe();
3433 /* Check that all is ok first ... */
3435 if (!context || !context->internal ||
3436 !context->internal->_initialized) {
3444 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3452 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3461 * Now, retrieve the number of entries that will fit in what was passed
3462 * We have to figure out if the info is in the list, or we need to
3463 * send a request to the server to get the info.
3466 while ((dirlist = dir->dir_next)) {
3467 struct smbc_dirent *dirent;
3469 if (!dirlist->dirent) {
3471 errno = ENOENT; /* Bad error */
3477 /* Do urlencoding of next entry, if so selected */
3478 dirent = (struct smbc_dirent *)context->internal->_dirent;
3479 maxlen = (sizeof(context->internal->_dirent) -
3480 sizeof(struct smbc_dirent));
3481 smbc_readdir_internal(context, dirent, dirlist->dirent, maxlen);
3483 reqd = dirent->dirlen;
3487 if (rem < count) { /* We managed to copy something */
3494 else { /* Nothing copied ... */
3496 errno = EINVAL; /* Not enough space ... */
3504 memcpy(ndir, dirent, reqd); /* Copy the data in ... */
3506 ((struct smbc_dirent *)ndir)->comment =
3507 (char *)(&((struct smbc_dirent *)ndir)->name +
3515 dir->dir_next = dirlist = dirlist -> next;
3528 * Routine to create a directory ...
3532 smbc_mkdir_ctx(SMBCCTX *context,
3536 SMBCSRV *srv = NULL;
3537 char *server = NULL;
3540 char *password = NULL;
3541 char *workgroup = NULL;
3543 char *targetpath = NULL;
3544 struct cli_state *targetcli = NULL;
3545 TALLOC_CTX *frame = talloc_stackframe();
3547 if (!context || !context->internal ||
3548 !context->internal->_initialized) {
3560 DEBUG(4, ("smbc_mkdir(%s)\n", fname));
3562 if (smbc_parse_path(frame,
3577 if (!user || user[0] == (char)0) {
3578 user = talloc_strdup(frame, context->user);
3586 srv = smbc_server(frame, context, True,
3587 server, share, &workgroup, &user, &password);
3592 return -1; /* errno set by smbc_server */
3596 /*d_printf(">>>mkdir: resolving %s\n", path);*/
3597 if (!cli_resolve_path(frame, "", srv->cli, path,
3598 &targetcli, &targetpath)) {
3599 d_printf("Could not resolve %s\n", path);
3603 /*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
3605 if (!cli_mkdir(targetcli, targetpath)) {
3607 errno = smbc_errno(context, targetcli);
3619 * Our list function simply checks to see if a directory is not empty
3622 static int smbc_rmdir_dirempty = True;
3625 rmdir_list_fn(const char *mnt,
3630 if (strncmp(finfo->name, ".", 1) != 0 &&
3631 strncmp(finfo->name, "..", 2) != 0) {
3632 smbc_rmdir_dirempty = False;
3637 * Routine to remove a directory
3641 smbc_rmdir_ctx(SMBCCTX *context,
3644 SMBCSRV *srv = NULL;
3645 char *server = NULL;
3648 char *password = NULL;
3649 char *workgroup = NULL;
3651 char *targetpath = NULL;
3652 struct cli_state *targetcli = NULL;
3653 TALLOC_CTX *frame = talloc_stackframe();
3655 if (!context || !context->internal ||
3656 !context->internal->_initialized) {
3668 DEBUG(4, ("smbc_rmdir(%s)\n", fname));
3670 if (smbc_parse_path(frame,
3685 if (!user || user[0] == (char)0) {
3686 user = talloc_strdup(frame, context->user);
3694 srv = smbc_server(frame, context, True,
3695 server, share, &workgroup, &user, &password);
3700 return -1; /* errno set by smbc_server */
3704 /*d_printf(">>>rmdir: resolving %s\n", path);*/
3705 if (!cli_resolve_path(frame, "", srv->cli, path,
3706 &targetcli, &targetpath)) {
3707 d_printf("Could not resolve %s\n", path);
3711 /*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
3714 if (!cli_rmdir(targetcli, targetpath)) {
3716 errno = smbc_errno(context, targetcli);
3718 if (errno == EACCES) { /* Check if the dir empty or not */
3720 /* Local storage to avoid buffer overflows */
3723 smbc_rmdir_dirempty = True; /* Make this so ... */
3725 lpath = talloc_asprintf(frame, "%s\\*",
3733 if (cli_list(targetcli, lpath,
3734 aDIR | aSYSTEM | aHIDDEN,
3735 rmdir_list_fn, NULL) < 0) {
3737 /* Fix errno to ignore latest error ... */
3738 DEBUG(5, ("smbc_rmdir: "
3739 "cli_list returned an error: %d\n",
3740 smbc_errno(context, targetcli)));
3745 if (smbc_rmdir_dirempty)
3763 * Routine to return the current directory position
3767 smbc_telldir_ctx(SMBCCTX *context,
3770 TALLOC_CTX *frame = talloc_stackframe();
3772 if (!context || !context->internal ||
3773 !context->internal->_initialized) {
3781 if (!dir || !DLIST_CONTAINS(context->internal->_files, dir)) {
3789 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3797 /* See if we're already at the end. */
3798 if (dir->dir_next == NULL) {
3805 * We return the pointer here as the offset
3808 return (off_t)(long)dir->dir_next->dirent;
3812 * A routine to run down the list and see if the entry is OK
3815 struct smbc_dir_list *
3816 smbc_check_dir_ent(struct smbc_dir_list *list,
3817 struct smbc_dirent *dirent)
3820 /* Run down the list looking for what we want */
3824 struct smbc_dir_list *tmp = list;
3828 if (tmp->dirent == dirent)
3837 return NULL; /* Not found, or an error */
3843 * Routine to seek on a directory
3847 smbc_lseekdir_ctx(SMBCCTX *context,
3851 long int l_offset = offset; /* Handle problems of size */
3852 struct smbc_dirent *dirent = (struct smbc_dirent *)l_offset;
3853 struct smbc_dir_list *list_ent = (struct smbc_dir_list *)NULL;
3854 TALLOC_CTX *frame = talloc_stackframe();
3856 if (!context || !context->internal ||
3857 !context->internal->_initialized) {
3865 if (dir->file != False) { /* FIXME, should be dir, perhaps */
3873 /* Now, check what we were passed and see if it is OK ... */
3875 if (dirent == NULL) { /* Seek to the begining of the list */
3877 dir->dir_next = dir->dir_list;
3883 if (offset == -1) { /* Seek to the end of the list */
3884 dir->dir_next = NULL;
3889 /* Now, run down the list and make sure that the entry is OK */
3890 /* This may need to be changed if we change the format of the list */
3892 if ((list_ent = smbc_check_dir_ent(dir->dir_list, dirent)) == NULL) {
3893 errno = EINVAL; /* Bad entry */
3898 dir->dir_next = list_ent;
3905 * Routine to fstat a dir
3909 smbc_fstatdir_ctx(SMBCCTX *context,
3914 if (!context || !context->internal ||
3915 !context->internal->_initialized) {
3920 /* No code yet ... */
3925 smbc_chmod_ctx(SMBCCTX *context,
3929 SMBCSRV *srv = NULL;
3930 char *server = NULL;
3933 char *password = NULL;
3934 char *workgroup = NULL;
3937 TALLOC_CTX *frame = talloc_stackframe();
3939 if (!context || !context->internal ||
3940 !context->internal->_initialized) {
3941 errno = EINVAL; /* Best I can think of ... */
3952 DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
3954 if (smbc_parse_path(frame,
3969 if (!user || user[0] == (char)0) {
3970 user = talloc_strdup(frame, context->user);
3978 srv = smbc_server(frame, context, True,
3979 server, share, &workgroup, &user, &password);
3983 return -1; /* errno set by smbc_server */
3988 if (!(newmode & (S_IWUSR | S_IWGRP | S_IWOTH))) mode |= aRONLY;
3989 if ((newmode & S_IXUSR) && lp_map_archive(-1)) mode |= aARCH;
3990 if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
3991 if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
3993 if (!cli_setatr(srv->cli, path, mode, 0)) {
3994 errno = smbc_errno(context, srv->cli);
4004 smbc_utimes_ctx(SMBCCTX *context,
4006 struct timeval *tbuf)
4008 SMBCSRV *srv = NULL;
4009 char *server = NULL;
4012 char *password = NULL;
4013 char *workgroup = NULL;
4017 TALLOC_CTX *frame = talloc_stackframe();
4019 if (!context || !context->internal ||
4020 !context->internal->_initialized) {
4021 errno = EINVAL; /* Best I can think of ... */
4033 access_time = write_time = time(NULL);
4035 access_time = tbuf[0].tv_sec;
4036 write_time = tbuf[1].tv_sec;
4044 strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
4045 atimebuf[sizeof(atimebuf) - 1] = '\0';
4046 if ((p = strchr(atimebuf, '\n')) != NULL) {
4050 strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
4051 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
4052 if ((p = strchr(mtimebuf, '\n')) != NULL) {
4056 dbgtext("smbc_utimes(%s, atime = %s mtime = %s)\n",
4057 fname, atimebuf, mtimebuf);
4060 if (smbc_parse_path(frame,
4075 if (!user || user[0] == (char)0) {
4076 user = talloc_strdup(frame, context->user);
4084 srv = smbc_server(frame, context, True,
4085 server, share, &workgroup, &user, &password);
4089 return -1; /* errno set by smbc_server */
4092 if (!smbc_setatr(context, srv, path,
4093 0, access_time, write_time, 0, 0)) {
4095 return -1; /* errno set by smbc_setatr */
4104 * Sort ACEs according to the documentation at
4105 * http://support.microsoft.com/kb/269175, at least as far as it defines the
4110 ace_compare(SEC_ACE *ace1,
4116 /* If the ACEs are equal, we have nothing more to do. */
4117 if (sec_ace_equal(ace1, ace2)) {
4121 /* Inherited follow non-inherited */
4122 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4123 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
4125 return (b1 ? 1 : -1);
4129 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
4130 * sort them after DENY and ALLOW.
4132 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4133 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4134 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4135 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4136 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
4137 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
4138 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
4139 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4141 return (b1 ? 1 : -1);
4144 /* Allowed ACEs follow denied ACEs */
4145 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4146 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4147 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
4148 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
4150 return (b1 ? 1 : -1);
4154 * ACEs applying to an entity's object follow those applying to the
4157 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4158 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4159 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
4160 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
4162 return (b1 ? 1 : -1);
4166 * If we get this far, the ACEs are similar as far as the
4167 * characteristics we typically care about (those defined by the
4168 * referenced MS document). We'll now sort by characteristics that
4169 * just seems reasonable.
4172 if (ace1->type != ace2->type) {
4173 return ace2->type - ace1->type;
4176 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
4177 return sid_compare(&ace1->trustee, &ace2->trustee);
4180 if (ace1->flags != ace2->flags) {
4181 return ace1->flags - ace2->flags;
4184 if (ace1->access_mask != ace2->access_mask) {
4185 return ace1->access_mask - ace2->access_mask;
4188 if (ace1->size != ace2->size) {
4189 return ace1->size - ace2->size;
4192 return memcmp(ace1, ace2, sizeof(SEC_ACE));
4197 sort_acl(SEC_ACL *the_acl)
4200 if (!the_acl) return;
4202 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
4203 QSORT_CAST ace_compare);
4205 for (i=1;i<the_acl->num_aces;) {
4206 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
4208 for (j=i; j<the_acl->num_aces-1; j++) {
4209 the_acl->aces[j] = the_acl->aces[j+1];
4211 the_acl->num_aces--;
4218 /* convert a SID to a string, either numeric or username/group */
4220 convert_sid_to_string(struct cli_state *ipc_cli,
4226 char **domains = NULL;
4227 char **names = NULL;
4228 enum lsa_SidType *types = NULL;
4229 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4232 sid_to_fstring(str, sid);
4235 return; /* no lookup desired */
4242 /* Ask LSA to convert the sid to a name */
4244 ctx = talloc_stackframe();
4246 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
4247 pol, 1, sid, &domains,
4249 !domains || !domains[0] || !names || !names[0]) {
4257 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
4258 domains[0], lp_winbind_separator(),
4262 /* convert a string to a SID, either numeric or username/group */
4264 convert_string_to_sid(struct cli_state *ipc_cli,
4270 enum lsa_SidType *types = NULL;
4271 DOM_SID *sids = NULL;
4273 TALLOC_CTX *ctx = NULL;
4274 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
4281 if (strncmp(str, "S-", 2) == 0) {
4282 return string_to_sid(sid, str);
4289 ctx = talloc_stackframe();
4290 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
4291 pol, 1, &str, NULL, 1, &sids,
4297 sid_copy(sid, &sids[0]);
4305 /* parse an ACE in the same format as print_ace() */
4307 parse_ace(struct cli_state *ipc_cli,
4317 unsigned int aflags;
4321 const struct perm_value *v;
4326 TALLOC_CTX *frame = talloc_stackframe();
4328 /* These values discovered by inspection */
4329 static const struct perm_value special_values[] = {
4330 { "R", 0x00120089 },
4331 { "W", 0x00120116 },
4332 { "X", 0x001200a0 },
4333 { "D", 0x00010000 },
4334 { "P", 0x00040000 },
4335 { "O", 0x00080000 },
4339 static const struct perm_value standard_values[] = {
4340 { "READ", 0x001200a9 },
4341 { "CHANGE", 0x001301bf },
4342 { "FULL", 0x001f01ff },
4348 p = strchr_m(str,':');
4355 /* Try to parse numeric form */
4357 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
4358 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4362 /* Try to parse text form */
4364 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
4370 if (!next_token_talloc(frame, &cp, &tok, "/")) {
4375 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
4376 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
4377 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
4378 atype = SEC_ACE_TYPE_ACCESS_DENIED;
4384 /* Only numeric form accepted for flags at present */
4386 if (!(next_token_talloc(frame, &cp, &tok, "/") &&
4387 sscanf(tok, "%i", &aflags))) {
4392 if (!next_token_talloc(frame, &cp, &tok, "/")) {
4397 if (strncmp(tok, "0x", 2) == 0) {
4398 if (sscanf(tok, "%i", &amask) != 1) {
4405 for (v = standard_values; v->perm; v++) {
4406 if (strcmp(tok, v->perm) == 0) {
4417 for (v = special_values; v->perm; v++) {
4418 if (v->perm[0] == *p) {
4438 init_sec_ace(ace, &sid, atype, mask, aflags);
4443 /* add an ACE to a list of ACEs in a SEC_ACL */
4445 add_ace(SEC_ACL **the_acl,
4453 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
4457 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
4460 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
4461 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
4462 newacl = make_sec_acl(ctx, (*the_acl)->revision,
4463 1+(*the_acl)->num_aces, aces);
4465 (*the_acl) = newacl;
4470 /* parse a ascii version of a security descriptor */
4472 sec_desc_parse(TALLOC_CTX *ctx,
4473 struct cli_state *ipc_cli,
4478 const char *p = str;
4480 SEC_DESC *ret = NULL;
4482 DOM_SID *group_sid=NULL;
4483 DOM_SID *owner_sid=NULL;
4487 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
4489 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
4490 revision = strtol(tok+9, NULL, 16);
4494 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
4496 DEBUG(5, ("OWNER specified more than once!\n"));
4499 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4501 !convert_string_to_sid(ipc_cli, pol,
4503 owner_sid, tok+6)) {
4504 DEBUG(5, ("Failed to parse owner sid\n"));
4510 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
4512 DEBUG(5, ("OWNER specified more than once!\n"));
4515 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4517 !convert_string_to_sid(ipc_cli, pol,
4519 owner_sid, tok+7)) {
4520 DEBUG(5, ("Failed to parse owner sid\n"));
4526 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
4528 DEBUG(5, ("GROUP specified more than once!\n"));
4531 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4533 !convert_string_to_sid(ipc_cli, pol,
4535 group_sid, tok+6)) {
4536 DEBUG(5, ("Failed to parse group sid\n"));
4542 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
4544 DEBUG(5, ("GROUP specified more than once!\n"));
4547 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
4549 !convert_string_to_sid(ipc_cli, pol,
4551 group_sid, tok+6)) {
4552 DEBUG(5, ("Failed to parse group sid\n"));
4558 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
4560 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
4561 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4564 if(!add_ace(&dacl, &ace, ctx)) {
4565 DEBUG(5, ("Failed to add ACL %s\n", tok));
4571 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
4573 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
4574 DEBUG(5, ("Failed to parse ACL %s\n", tok));
4577 if(!add_ace(&dacl, &ace, ctx)) {
4578 DEBUG(5, ("Failed to add ACL %s\n", tok));
4584 DEBUG(5, ("Failed to parse security descriptor\n"));
4588 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
4589 owner_sid, group_sid, NULL, dacl, &sd_size);
4592 SAFE_FREE(group_sid);
4593 SAFE_FREE(owner_sid);
4599 /* Obtain the current dos attributes */
4600 static DOS_ATTR_DESC *
4601 dos_attr_query(SMBCCTX *context,
4603 const char *filename,
4606 struct timespec create_time_ts;
4607 struct timespec write_time_ts;
4608 struct timespec access_time_ts;
4609 struct timespec change_time_ts;
4612 SMB_INO_T inode = 0;
4615 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
4621 /* Obtain the DOS attributes */
4622 if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
4629 errno = smbc_errno(context, srv->cli);
4630 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
4636 ret->create_time = convert_timespec_to_time_t(create_time_ts);
4637 ret->access_time = convert_timespec_to_time_t(access_time_ts);
4638 ret->write_time = convert_timespec_to_time_t(write_time_ts);
4639 ret->change_time = convert_timespec_to_time_t(change_time_ts);
4646 /* parse a ascii version of a security descriptor */
4648 dos_attr_parse(SMBCCTX *context,
4654 const char *p = str;
4656 TALLOC_CTX *frame = NULL;
4658 const char * create_time_attr;
4659 const char * access_time_attr;
4660 const char * write_time_attr;
4661 const char * change_time_attr;
4664 /* Determine whether to use old-style or new-style attribute names */
4665 if (context->internal->_full_time_names) {
4666 /* new-style names */
4667 attr_strings.create_time_attr = "CREATE_TIME";
4668 attr_strings.access_time_attr = "ACCESS_TIME";
4669 attr_strings.write_time_attr = "WRITE_TIME";
4670 attr_strings.change_time_attr = "CHANGE_TIME";
4672 /* old-style names */
4673 attr_strings.create_time_attr = NULL;
4674 attr_strings.access_time_attr = "A_TIME";
4675 attr_strings.write_time_attr = "M_TIME";
4676 attr_strings.change_time_attr = "C_TIME";
4679 /* if this is to set the entire ACL... */
4681 /* ... then increment past the first colon if there is one */
4682 if ((p = strchr(str, ':')) != NULL) {
4689 frame = talloc_stackframe();
4690 while (next_token_talloc(frame, &p, &tok, "\t,\r\n")) {
4691 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
4692 dad->mode = strtol(tok+5, NULL, 16);
4696 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
4697 dad->size = (SMB_OFF_T)atof(tok+5);
4701 n = strlen(attr_strings.access_time_attr);
4702 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
4703 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
4707 n = strlen(attr_strings.change_time_attr);
4708 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
4709 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
4713 n = strlen(attr_strings.write_time_attr);
4714 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
4715 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
4719 if (attr_strings.create_time_attr != NULL) {
4720 n = strlen(attr_strings.create_time_attr);
4721 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
4723 dad->create_time = (time_t)strtol(tok+n+1,
4729 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
4730 dad->inode = (SMB_INO_T)atof(tok+6);
4737 /*****************************************************
4738 Retrieve the acls for a file.
4739 *******************************************************/
4742 cacl_get(SMBCCTX *context,
4745 struct cli_state *ipc_cli,
4761 bool exclude_nt_revision = False;
4762 bool exclude_nt_owner = False;
4763 bool exclude_nt_group = False;
4764 bool exclude_nt_acl = False;
4765 bool exclude_dos_mode = False;
4766 bool exclude_dos_size = False;
4767 bool exclude_dos_create_time = False;
4768 bool exclude_dos_access_time = False;
4769 bool exclude_dos_write_time = False;
4770 bool exclude_dos_change_time = False;
4771 bool exclude_dos_inode = False;
4772 bool numeric = True;
4773 bool determine_size = (bufsize == 0);
4777 fstring name_sandbox;
4781 struct timespec create_time_ts;
4782 struct timespec write_time_ts;
4783 struct timespec access_time_ts;
4784 struct timespec change_time_ts;
4785 time_t create_time = (time_t)0;
4786 time_t write_time = (time_t)0;
4787 time_t access_time = (time_t)0;
4788 time_t change_time = (time_t)0;
4792 struct cli_state *cli = srv->cli;
4794 const char * create_time_attr;
4795 const char * access_time_attr;
4796 const char * write_time_attr;
4797 const char * change_time_attr;
4800 const char * create_time_attr;
4801 const char * access_time_attr;
4802 const char * write_time_attr;
4803 const char * change_time_attr;
4804 } excl_attr_strings;
4806 /* Determine whether to use old-style or new-style attribute names */
4807 if (context->internal->_full_time_names) {
4808 /* new-style names */
4809 attr_strings.create_time_attr = "CREATE_TIME";
4810 attr_strings.access_time_attr = "ACCESS_TIME";
4811 attr_strings.write_time_attr = "WRITE_TIME";
4812 attr_strings.change_time_attr = "CHANGE_TIME";
4814 excl_attr_strings.create_time_attr = "CREATE_TIME";
4815 excl_attr_strings.access_time_attr = "ACCESS_TIME";
4816 excl_attr_strings.write_time_attr = "WRITE_TIME";
4817 excl_attr_strings.change_time_attr = "CHANGE_TIME";
4819 /* old-style names */
4820 attr_strings.create_time_attr = NULL;
4821 attr_strings.access_time_attr = "A_TIME";
4822 attr_strings.write_time_attr = "M_TIME";
4823 attr_strings.change_time_attr = "C_TIME";
4825 excl_attr_strings.create_time_attr = NULL;
4826 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
4827 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
4828 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
4831 /* Copy name so we can strip off exclusions (if any are specified) */
4832 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
4834 /* Ensure name is null terminated */
4835 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
4837 /* Play in the sandbox */
4838 name = name_sandbox;
4840 /* If there are any exclusions, point to them and mask them from name */
4841 if ((pExclude = strchr(name, '!')) != NULL)
4846 all = (StrnCaseCmp(name, "system.*", 8) == 0);
4847 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
4848 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
4849 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
4850 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
4851 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
4852 numeric = (* (name + strlen(name) - 1) != '+');
4854 /* Look for exclusions from "all" requests */
4855 if (all || all_nt || all_dos) {
4857 /* Exclusions are delimited by '!' */
4860 pExclude = (p == NULL ? NULL : p + 1)) {
4862 /* Find end of this exclusion name */
4863 if ((p = strchr(pExclude, '!')) != NULL)
4868 /* Which exclusion name is this? */
4869 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
4870 exclude_nt_revision = True;
4872 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
4873 exclude_nt_owner = True;
4875 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
4876 exclude_nt_group = True;
4878 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
4879 exclude_nt_acl = True;
4881 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
4882 exclude_dos_mode = True;
4884 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
4885 exclude_dos_size = True;
4887 else if (excl_attr_strings.create_time_attr != NULL &&
4888 StrCaseCmp(pExclude,
4889 excl_attr_strings.change_time_attr) == 0) {
4890 exclude_dos_create_time = True;
4892 else if (StrCaseCmp(pExclude,
4893 excl_attr_strings.access_time_attr) == 0) {
4894 exclude_dos_access_time = True;
4896 else if (StrCaseCmp(pExclude,
4897 excl_attr_strings.write_time_attr) == 0) {
4898 exclude_dos_write_time = True;
4900 else if (StrCaseCmp(pExclude,
4901 excl_attr_strings.change_time_attr) == 0) {
4902 exclude_dos_change_time = True;
4904 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
4905 exclude_dos_inode = True;
4908 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
4919 * If we are (possibly) talking to an NT or new system and some NT
4920 * attributes have been requested...
4922 if (ipc_cli && (all || some_nt || all_nt_acls)) {
4923 /* Point to the portion after "system.nt_sec_desc." */
4924 name += 19; /* if (all) this will be invalid but unused */
4926 /* ... then obtain any NT attributes which were requested */
4927 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
4930 DEBUG(5, ("cacl_get failed to open %s: %s\n",
4931 filename, cli_errstr(cli)));
4936 sd = cli_query_secdesc(cli, fnum, ctx);
4940 ("cacl_get Failed to query old descriptor\n"));
4945 cli_close(cli, fnum);
4947 if (! exclude_nt_revision) {
4948 if (all || all_nt) {
4949 if (determine_size) {
4950 p = talloc_asprintf(ctx,
4959 n = snprintf(buf, bufsize,
4963 } else if (StrCaseCmp(name, "revision") == 0) {
4964 if (determine_size) {
4965 p = talloc_asprintf(ctx, "%d",
4973 n = snprintf(buf, bufsize, "%d",
4978 if (!determine_size && n > bufsize) {
4988 if (! exclude_nt_owner) {
4989 /* Get owner and group sid */
4990 if (sd->owner_sid) {
4991 convert_sid_to_string(ipc_cli, pol,
4996 fstrcpy(sidstr, "");
4999 if (all || all_nt) {
5000 if (determine_size) {
5001 p = talloc_asprintf(ctx, ",OWNER:%s",
5008 } else if (sidstr[0] != '\0') {
5009 n = snprintf(buf, bufsize,
5010 ",OWNER:%s", sidstr);
5012 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
5013 if (determine_size) {
5014 p = talloc_asprintf(ctx, "%s", sidstr);
5021 n = snprintf(buf, bufsize, "%s",
5026 if (!determine_size && n > bufsize) {
5036 if (! exclude_nt_group) {
5037 if (sd->group_sid) {
5038 convert_sid_to_string(ipc_cli, pol,
5042 fstrcpy(sidstr, "");
5045 if (all || all_nt) {
5046 if (determine_size) {
5047 p = talloc_asprintf(ctx, ",GROUP:%s",
5054 } else if (sidstr[0] != '\0') {
5055 n = snprintf(buf, bufsize,
5056 ",GROUP:%s", sidstr);
5058 } else if (StrnCaseCmp(name, "group", 5) == 0) {
5059 if (determine_size) {
5060 p = talloc_asprintf(ctx, "%s", sidstr);
5067 n = snprintf(buf, bufsize,
5072 if (!determine_size && n > bufsize) {
5082 if (! exclude_nt_acl) {
5083 /* Add aces to value buffer */
5084 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
5086 SEC_ACE *ace = &sd->dacl->aces[i];
5087 convert_sid_to_string(ipc_cli, pol,
5091 if (all || all_nt) {
5092 if (determine_size) {
5093 p = talloc_asprintf(
5109 ",ACL:%s:%d/%d/0x%08x",
5115 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
5116 StrCaseCmp(name+3, sidstr) == 0) ||
5117 (StrnCaseCmp(name, "acl+", 4) == 0 &&
5118 StrCaseCmp(name+4, sidstr) == 0)) {
5119 if (determine_size) {
5120 p = talloc_asprintf(
5132 n = snprintf(buf, bufsize,
5138 } else if (all_nt_acls) {
5139 if (determine_size) {
5140 p = talloc_asprintf(
5142 "%s%s:%d/%d/0x%08x",
5154 n = snprintf(buf, bufsize,
5155 "%s%s:%d/%d/0x%08x",
5163 if (!determine_size && n > bufsize) {
5174 /* Restore name pointer to its original value */
5178 if (all || some_dos) {
5179 /* Point to the portion after "system.dos_attr." */
5180 name += 16; /* if (all) this will be invalid but unused */
5182 /* Obtain the DOS attributes */
5183 if (!smbc_getatr(context, srv, filename, &mode, &size,
5190 errno = smbc_errno(context, srv->cli);
5195 create_time = convert_timespec_to_time_t(create_time_ts);
5196 access_time = convert_timespec_to_time_t(access_time_ts);
5197 write_time = convert_timespec_to_time_t(write_time_ts);
5198 change_time = convert_timespec_to_time_t(change_time_ts);
5200 if (! exclude_dos_mode) {
5201 if (all || all_dos) {
5202 if (determine_size) {
5203 p = talloc_asprintf(ctx,
5216 n = snprintf(buf, bufsize,
5224 } else if (StrCaseCmp(name, "mode") == 0) {
5225 if (determine_size) {
5226 p = talloc_asprintf(ctx, "0x%x", mode);
5233 n = snprintf(buf, bufsize,
5238 if (!determine_size && n > bufsize) {
5248 if (! exclude_dos_size) {
5249 if (all || all_dos) {
5250 if (determine_size) {
5251 p = talloc_asprintf(
5261 n = snprintf(buf, bufsize,
5265 } else if (StrCaseCmp(name, "size") == 0) {
5266 if (determine_size) {
5267 p = talloc_asprintf(
5277 n = snprintf(buf, bufsize,
5283 if (!determine_size && n > bufsize) {
5293 if (! exclude_dos_create_time &&
5294 attr_strings.create_time_attr != NULL) {
5295 if (all || all_dos) {
5296 if (determine_size) {
5297 p = talloc_asprintf(ctx,
5299 attr_strings.create_time_attr,
5307 n = snprintf(buf, bufsize,
5309 attr_strings.create_time_attr,
5312 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
5313 if (determine_size) {
5314 p = talloc_asprintf(ctx, "%lu", create_time);
5321 n = snprintf(buf, bufsize,
5322 "%lu", create_time);
5326 if (!determine_size && n > bufsize) {
5336 if (! exclude_dos_access_time) {
5337 if (all || all_dos) {
5338 if (determine_size) {
5339 p = talloc_asprintf(ctx,
5341 attr_strings.access_time_attr,
5349 n = snprintf(buf, bufsize,
5351 attr_strings.access_time_attr,
5354 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
5355 if (determine_size) {
5356 p = talloc_asprintf(ctx, "%lu", access_time);
5363 n = snprintf(buf, bufsize,
5364 "%lu", access_time);
5368 if (!determine_size && n > bufsize) {
5378 if (! exclude_dos_write_time) {
5379 if (all || all_dos) {
5380 if (determine_size) {
5381 p = talloc_asprintf(ctx,
5383 attr_strings.write_time_attr,
5391 n = snprintf(buf, bufsize,
5393 attr_strings.write_time_attr,
5396 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
5397 if (determine_size) {
5398 p = talloc_asprintf(ctx, "%lu", write_time);
5405 n = snprintf(buf, bufsize,
5410 if (!determine_size && n > bufsize) {
5420 if (! exclude_dos_change_time) {
5421 if (all || all_dos) {
5422 if (determine_size) {
5423 p = talloc_asprintf(ctx,
5425 attr_strings.change_time_attr,
5433 n = snprintf(buf, bufsize,
5435 attr_strings.change_time_attr,
5438 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5439 if (determine_size) {
5440 p = talloc_asprintf(ctx, "%lu", change_time);
5447 n = snprintf(buf, bufsize,
5448 "%lu", change_time);
5452 if (!determine_size && n > bufsize) {
5462 if (! exclude_dos_inode) {
5463 if (all || all_dos) {
5464 if (determine_size) {
5465 p = talloc_asprintf(
5475 n = snprintf(buf, bufsize,
5479 } else if (StrCaseCmp(name, "inode") == 0) {
5480 if (determine_size) {
5481 p = talloc_asprintf(
5491 n = snprintf(buf, bufsize,
5497 if (!determine_size && n > bufsize) {
5507 /* Restore name pointer to its original value */
5519 /*****************************************************
5520 set the ACLs on a file given an ascii description
5521 *******************************************************/
5523 cacl_set(TALLOC_CTX *ctx,
5524 struct cli_state *cli,
5525 struct cli_state *ipc_cli,
5527 const char *filename,
5528 const char *the_acl,
5534 SEC_DESC *sd = NULL, *old;
5535 SEC_ACL *dacl = NULL;
5536 DOM_SID *owner_sid = NULL;
5537 DOM_SID *group_sid = NULL;
5542 bool numeric = True;
5544 /* the_acl will be null for REMOVE_ALL operations */
5546 numeric = ((p = strchr(the_acl, ':')) != NULL &&
5550 /* if this is to set the entire ACL... */
5551 if (*the_acl == '*') {
5552 /* ... then increment past the first colon */
5556 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
5557 CONST_DISCARD(char *, the_acl));
5565 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
5566 that doesn't deref sd */
5568 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
5573 /* The desired access below is the only one I could find that works
5574 with NT4, W2KP and Samba */
5576 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
5579 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5580 filename, cli_errstr(cli)));
5585 old = cli_query_secdesc(cli, fnum, ctx);
5588 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
5593 cli_close(cli, fnum);
5596 case SMBC_XATTR_MODE_REMOVE_ALL:
5597 old->dacl->num_aces = 0;
5601 case SMBC_XATTR_MODE_REMOVE:
5602 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5605 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5606 if (sec_ace_equal(&sd->dacl->aces[i],
5607 &old->dacl->aces[j])) {
5609 for (k=j; k<old->dacl->num_aces-1;k++) {
5610 old->dacl->aces[k] =
5611 old->dacl->aces[k+1];
5613 old->dacl->num_aces--;
5628 case SMBC_XATTR_MODE_ADD:
5629 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5632 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
5633 if (sid_equal(&sd->dacl->aces[i].trustee,
5634 &old->dacl->aces[j].trustee)) {
5635 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
5640 old->dacl->aces[j] = sd->dacl->aces[i];
5646 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
5652 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
5653 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
5659 case SMBC_XATTR_MODE_SET:
5661 owner_sid = old->owner_sid;
5662 group_sid = old->group_sid;
5666 case SMBC_XATTR_MODE_CHOWN:
5667 owner_sid = sd->owner_sid;
5670 case SMBC_XATTR_MODE_CHGRP:
5671 group_sid = sd->group_sid;
5675 /* Denied ACE entries must come before allowed ones */
5676 sort_acl(old->dacl);
5678 /* Create new security descriptor and set it */
5679 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
5680 owner_sid, group_sid, NULL, dacl, &sd_size);
5682 fnum = cli_nt_create(cli, filename,
5683 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
5686 DEBUG(5, ("cacl_set failed to open %s: %s\n",
5687 filename, cli_errstr(cli)));
5692 if (!cli_set_secdesc(cli, fnum, sd)) {
5693 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
5700 cli_close(cli, fnum);
5711 smbc_setxattr_ctx(SMBCCTX *context,
5720 SMBCSRV *srv = NULL;
5721 SMBCSRV *ipc_srv = NULL;
5722 char *server = NULL;
5725 char *password = NULL;
5726 char *workgroup = NULL;
5729 DOS_ATTR_DESC *dad = NULL;
5731 const char * create_time_attr;
5732 const char * access_time_attr;
5733 const char * write_time_attr;
5734 const char * change_time_attr;
5736 TALLOC_CTX *frame = talloc_stackframe();
5738 if (!context || !context->internal ||
5739 !context->internal->_initialized) {
5740 errno = EINVAL; /* Best I can think of ... */
5751 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
5752 fname, name, (int) size, (const char*)value));
5754 if (smbc_parse_path(frame,
5769 if (!user || user[0] == (char)0) {
5770 user = talloc_strdup(frame, context->user);
5778 srv = smbc_server(frame, context, True,
5779 server, share, &workgroup, &user, &password);
5782 return -1; /* errno set by smbc_server */
5785 if (! srv->no_nt_session) {
5786 ipc_srv = smbc_attr_server(frame, context, server, share,
5787 &workgroup, &user, &password,
5790 srv->no_nt_session = True;
5797 * Are they asking to set the entire set of known attributes?
5799 if (StrCaseCmp(name, "system.*") == 0 ||
5800 StrCaseCmp(name, "system.*+") == 0) {
5803 talloc_asprintf(talloc_tos(), "%s:%s",
5804 name+7, (const char *) value);
5813 ret = cacl_set(talloc_tos(), srv->cli,
5814 ipc_srv->cli, &pol, path,
5817 ? SMBC_XATTR_MODE_SET
5818 : SMBC_XATTR_MODE_ADD),
5824 /* get a DOS Attribute Descriptor with current attributes */
5825 dad = dos_attr_query(context, talloc_tos(), path, srv);
5827 /* Overwrite old with new, using what was provided */
5828 dos_attr_parse(context, dad, srv, namevalue);
5830 /* Set the new DOS attributes */
5831 if (! smbc_setatr(context, srv, path,
5838 /* cause failure if NT failed too */
5843 /* we only fail if both NT and DOS sets failed */
5844 if (ret < 0 && ! dad) {
5845 ret = -1; /* in case dad was null */
5856 * Are they asking to set an access control element or to set
5857 * the entire access control list?
5859 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
5860 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
5861 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
5862 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
5863 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
5867 talloc_asprintf(talloc_tos(), "%s:%s",
5868 name+19, (const char *) value);
5871 ret = -1; /* errno set by smbc_server() */
5873 else if (! namevalue) {
5877 ret = cacl_set(talloc_tos(), srv->cli,
5878 ipc_srv->cli, &pol, path,
5881 ? SMBC_XATTR_MODE_SET
5882 : SMBC_XATTR_MODE_ADD),
5890 * Are they asking to set the owner?
5892 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
5893 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
5897 talloc_asprintf(talloc_tos(), "%s:%s",
5898 name+19, (const char *) value);
5901 ret = -1; /* errno set by smbc_server() */
5903 else if (! namevalue) {
5907 ret = cacl_set(talloc_tos(), srv->cli,
5908 ipc_srv->cli, &pol, path,
5909 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5916 * Are they asking to set the group?
5918 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
5919 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
5923 talloc_asprintf(talloc_tos(), "%s:%s",
5924 name+19, (const char *) value);
5927 /* errno set by smbc_server() */
5930 else if (! namevalue) {
5934 ret = cacl_set(talloc_tos(), srv->cli,
5935 ipc_srv->cli, &pol, path,
5936 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
5942 /* Determine whether to use old-style or new-style attribute names */
5943 if (context->internal->_full_time_names) {
5944 /* new-style names */
5945 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
5946 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
5947 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
5948 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
5950 /* old-style names */
5951 attr_strings.create_time_attr = NULL;
5952 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
5953 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
5954 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
5958 * Are they asking to set a DOS attribute?
5960 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
5961 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
5962 (attr_strings.create_time_attr != NULL &&
5963 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
5964 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
5965 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
5966 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
5968 /* get a DOS Attribute Descriptor with current attributes */
5969 dad = dos_attr_query(context, talloc_tos(), path, srv);
5972 talloc_asprintf(talloc_tos(), "%s:%s",
5973 name+16, (const char *) value);
5978 /* Overwrite old with provided new params */
5979 dos_attr_parse(context, dad, srv, namevalue);
5981 /* Set the new DOS attributes */
5982 ret2 = smbc_setatr(context, srv, path,
5989 /* ret2 has True (success) / False (failure) */
6004 /* Unsupported attribute name */
6011 smbc_getxattr_ctx(SMBCCTX *context,
6018 SMBCSRV *srv = NULL;
6019 SMBCSRV *ipc_srv = NULL;
6020 char *server = NULL;
6023 char *password = NULL;
6024 char *workgroup = NULL;
6028 const char * create_time_attr;
6029 const char * access_time_attr;
6030 const char * write_time_attr;
6031 const char * change_time_attr;
6033 TALLOC_CTX *frame = talloc_stackframe();
6035 if (!context || !context->internal ||
6036 !context->internal->_initialized) {
6037 errno = EINVAL; /* Best I can think of ... */
6048 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
6050 if (smbc_parse_path(frame,
6065 if (!user || user[0] == (char)0) {
6066 user = talloc_strdup(frame, context->user);
6074 srv = smbc_server(frame, context, True,
6075 server, share, &workgroup, &user, &password);
6078 return -1; /* errno set by smbc_server */
6081 if (! srv->no_nt_session) {
6082 ipc_srv = smbc_attr_server(frame, context, server, share,
6083 &workgroup, &user, &password,
6086 srv->no_nt_session = True;
6092 /* Determine whether to use old-style or new-style attribute names */
6093 if (context->internal->_full_time_names) {
6094 /* new-style names */
6095 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
6096 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
6097 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
6098 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
6100 /* old-style names */
6101 attr_strings.create_time_attr = NULL;
6102 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
6103 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
6104 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
6107 /* Are they requesting a supported attribute? */
6108 if (StrCaseCmp(name, "system.*") == 0 ||
6109 StrnCaseCmp(name, "system.*!", 9) == 0 ||
6110 StrCaseCmp(name, "system.*+") == 0 ||
6111 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
6112 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
6113 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
6114 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
6115 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
6116 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6117 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6118 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6119 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6120 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6121 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6122 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
6123 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
6124 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
6125 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
6126 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
6127 (attr_strings.create_time_attr != NULL &&
6128 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
6129 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
6130 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
6131 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
6132 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
6135 ret = cacl_get(context, talloc_tos(), srv,
6136 ipc_srv == NULL ? NULL : ipc_srv->cli,
6138 CONST_DISCARD(char *, name),
6139 CONST_DISCARD(char *, value), size);
6140 if (ret < 0 && errno == 0) {
6141 errno = smbc_errno(context, srv->cli);
6147 /* Unsupported attribute name */
6155 smbc_removexattr_ctx(SMBCCTX *context,
6160 SMBCSRV *srv = NULL;
6161 SMBCSRV *ipc_srv = NULL;
6162 char *server = NULL;
6165 char *password = NULL;
6166 char *workgroup = NULL;
6169 TALLOC_CTX *frame = talloc_stackframe();
6171 if (!context || !context->internal ||
6172 !context->internal->_initialized) {
6173 errno = EINVAL; /* Best I can think of ... */
6184 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
6186 if (smbc_parse_path(frame,
6201 if (!user || user[0] == (char)0) {
6202 user = talloc_strdup(frame, context->user);
6210 srv = smbc_server(frame, context, True,
6211 server, share, &workgroup, &user, &password);
6214 return -1; /* errno set by smbc_server */
6217 if (! srv->no_nt_session) {
6218 ipc_srv = smbc_attr_server(frame, context, server, share,
6219 &workgroup, &user, &password,
6222 srv->no_nt_session = True;
6230 return -1; /* errno set by smbc_attr_server */
6233 /* Are they asking to set the entire ACL? */
6234 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
6235 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
6238 ret = cacl_set(talloc_tos(), srv->cli,
6239 ipc_srv->cli, &pol, path,
6240 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
6246 * Are they asking to remove one or more spceific security descriptor
6249 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
6250 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
6251 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
6252 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
6253 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
6254 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
6255 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
6258 ret = cacl_set(talloc_tos(), srv->cli,
6259 ipc_srv->cli, &pol, path,
6260 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
6265 /* Unsupported attribute name */
6272 smbc_listxattr_ctx(SMBCCTX *context,
6278 * This isn't quite what listxattr() is supposed to do. This returns
6279 * the complete set of attribute names, always, rather than only those
6280 * attribute names which actually exist for a file. Hmmm...
6282 const char supported_old[] =
6285 "system.nt_sec_desc.revision\0"
6286 "system.nt_sec_desc.owner\0"
6287 "system.nt_sec_desc.owner+\0"
6288 "system.nt_sec_desc.group\0"
6289 "system.nt_sec_desc.group+\0"
6290 "system.nt_sec_desc.acl.*\0"
6291 "system.nt_sec_desc.acl\0"
6292 "system.nt_sec_desc.acl+\0"
6293 "system.nt_sec_desc.*\0"
6294 "system.nt_sec_desc.*+\0"
6295 "system.dos_attr.*\0"
6296 "system.dos_attr.mode\0"
6297 "system.dos_attr.c_time\0"
6298 "system.dos_attr.a_time\0"
6299 "system.dos_attr.m_time\0"
6301 const char supported_new[] =
6304 "system.nt_sec_desc.revision\0"
6305 "system.nt_sec_desc.owner\0"
6306 "system.nt_sec_desc.owner+\0"
6307 "system.nt_sec_desc.group\0"
6308 "system.nt_sec_desc.group+\0"
6309 "system.nt_sec_desc.acl.*\0"
6310 "system.nt_sec_desc.acl\0"
6311 "system.nt_sec_desc.acl+\0"
6312 "system.nt_sec_desc.*\0"
6313 "system.nt_sec_desc.*+\0"
6314 "system.dos_attr.*\0"
6315 "system.dos_attr.mode\0"
6316 "system.dos_attr.create_time\0"
6317 "system.dos_attr.access_time\0"
6318 "system.dos_attr.write_time\0"
6319 "system.dos_attr.change_time\0"
6321 const char * supported;
6323 if (context->internal->_full_time_names) {
6324 supported = supported_new;
6326 supported = supported_old;
6330 return sizeof(supported);
6333 if (sizeof(supported) > size) {
6338 /* this can't be strcpy() because there are embedded null characters */
6339 memcpy(list, supported, sizeof(supported));
6340 return sizeof(supported);
6345 * Open a print file to be written to by other calls
6349 smbc_open_print_job_ctx(SMBCCTX *context,
6352 char *server = NULL;
6355 char *password = NULL;
6357 TALLOC_CTX *frame = talloc_stackframe();
6359 if (!context || !context->internal ||
6360 !context->internal->_initialized) {
6372 DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
6374 if (smbc_parse_path(frame,
6389 /* What if the path is empty, or the file exists? */
6392 return (context->open)(context, fname, O_WRONLY, 666);
6396 * Routine to print a file on a remote server ...
6398 * We open the file, which we assume to be on a remote server, and then
6399 * copy it to a print file on the share specified by printq.
6403 smbc_print_file_ctx(SMBCCTX *c_file,
6414 TALLOC_CTX *frame = talloc_stackframe();
6416 if (!c_file || !c_file->internal->_initialized || !c_print ||
6417 !c_print->internal->_initialized) {
6425 if (!fname && !printq) {
6433 /* Try to open the file for reading ... */
6435 if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
6436 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
6438 return -1; /* smbc_open sets errno */
6441 /* Now, try to open the printer file for writing */
6443 if ((long)(fid2 = (c_print->open_print_job)(c_print, printq)) < 0) {
6445 saverr = errno; /* Save errno */
6446 (c_file->close_fn)(c_file, fid1);
6453 while ((bytes = (c_file->read)(c_file, fid1, buf, sizeof(buf))) > 0) {
6457 if (((c_print->write)(c_print, fid2, buf, bytes)) < 0) {
6460 (c_file->close_fn)(c_file, fid1);
6461 (c_print->close_fn)(c_print, fid2);
6470 (c_file->close_fn)(c_file, fid1); /* We have to close these anyway */
6471 (c_print->close_fn)(c_print, fid2);
6487 * Routine to list print jobs on a printer share ...
6491 smbc_list_print_jobs_ctx(SMBCCTX *context,
6493 smbc_list_print_job_fn fn)
6495 SMBCSRV *srv = NULL;
6496 char *server = NULL;
6499 char *password = NULL;
6500 char *workgroup = NULL;
6502 TALLOC_CTX *frame = talloc_stackframe();
6504 if (!context || !context->internal ||
6505 !context->internal->_initialized) {
6517 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
6519 if (smbc_parse_path(frame,
6534 if (!user || user[0] == (char)0) {
6535 user = talloc_strdup(frame, context->user);
6543 srv = smbc_server(frame, context, True,
6544 server, share, &workgroup, &user, &password);
6548 return -1; /* errno set by smbc_server */
6551 if (cli_print_queue(srv->cli,
6552 (void (*)(struct print_job_info *))fn) < 0) {
6553 errno = smbc_errno(context, srv->cli);
6564 * Delete a print job from a remote printer share
6568 smbc_unlink_print_job_ctx(SMBCCTX *context,
6572 SMBCSRV *srv = NULL;
6573 char *server = NULL;
6576 char *password = NULL;
6577 char *workgroup = NULL;
6580 TALLOC_CTX *frame = talloc_stackframe();
6582 if (!context || !context->internal ||
6583 !context->internal->_initialized) {
6595 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
6597 if (smbc_parse_path(frame,
6612 if (!user || user[0] == (char)0) {
6613 user = talloc_strdup(frame, context->user);
6621 srv = smbc_server(frame, context, True,
6622 server, share, &workgroup, &user, &password);
6627 return -1; /* errno set by smbc_server */
6631 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
6634 errno = smbc_errno(context, srv->cli);
6635 else if (err == ERRnosuchprintjob)
6648 * Get a new empty handle to fill in with your own info
6651 smbc_new_context(void)
6655 context = SMB_MALLOC_P(SMBCCTX);
6661 ZERO_STRUCTP(context);
6663 context->internal = SMB_MALLOC_P(struct smbc_internal_data);
6664 if (!context->internal) {
6670 ZERO_STRUCTP(context->internal);
6672 /* ADD REASONABLE DEFAULTS */
6674 context->timeout = 20000; /* 20 seconds */
6676 context->options.browse_max_lmb_count = 3; /* # LMBs to query */
6677 context->options.urlencode_readdir_entries = False;/* backward compat */
6678 context->options.one_share_per_server = False;/* backward compat */
6679 context->internal->_share_mode = SMBC_SHAREMODE_DENY_NONE;
6680 /* backward compat */
6682 context->open = smbc_open_ctx;
6683 context->creat = smbc_creat_ctx;
6684 context->read = smbc_read_ctx;
6685 context->write = smbc_write_ctx;
6686 context->close_fn = smbc_close_ctx;
6687 context->unlink = smbc_unlink_ctx;
6688 context->rename = smbc_rename_ctx;
6689 context->lseek = smbc_lseek_ctx;
6690 context->stat = smbc_stat_ctx;
6691 context->fstat = smbc_fstat_ctx;
6692 context->opendir = smbc_opendir_ctx;
6693 context->closedir = smbc_closedir_ctx;
6694 context->readdir = smbc_readdir_ctx;
6695 context->getdents = smbc_getdents_ctx;
6696 context->mkdir = smbc_mkdir_ctx;
6697 context->rmdir = smbc_rmdir_ctx;
6698 context->telldir = smbc_telldir_ctx;
6699 context->lseekdir = smbc_lseekdir_ctx;
6700 context->fstatdir = smbc_fstatdir_ctx;
6701 context->chmod = smbc_chmod_ctx;
6702 context->utimes = smbc_utimes_ctx;
6703 context->setxattr = smbc_setxattr_ctx;
6704 context->getxattr = smbc_getxattr_ctx;
6705 context->removexattr = smbc_removexattr_ctx;
6706 context->listxattr = smbc_listxattr_ctx;
6707 context->open_print_job = smbc_open_print_job_ctx;
6708 context->print_file = smbc_print_file_ctx;
6709 context->list_print_jobs = smbc_list_print_jobs_ctx;
6710 context->unlink_print_job = smbc_unlink_print_job_ctx;
6712 context->callbacks.check_server_fn = smbc_check_server;
6713 context->callbacks.remove_unused_server_fn = smbc_remove_unused_server;
6715 smbc_default_cache_functions(context);
6723 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
6724 * and thus you'll be leaking memory if not handled properly.
6728 smbc_free_context(SMBCCTX *context,
6738 DEBUG(1,("Performing aggressive shutdown.\n"));
6740 f = context->internal->_files;
6742 (context->close_fn)(context, f);
6745 context->internal->_files = NULL;
6747 /* First try to remove the servers the nice way. */
6748 if (context->callbacks.purge_cached_fn(context)) {
6751 DEBUG(1, ("Could not purge all servers, "
6752 "Nice way shutdown failed.\n"));
6753 s = context->internal->_servers;
6755 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
6757 cli_shutdown(s->cli);
6758 (context->callbacks.remove_cached_srv_fn)(context,
6761 DLIST_REMOVE(context->internal->_servers, s);
6765 context->internal->_servers = NULL;
6769 /* This is the polite way */
6770 if ((context->callbacks.purge_cached_fn)(context)) {
6771 DEBUG(1, ("Could not purge all servers, "
6772 "free_context failed.\n"));
6776 if (context->internal->_servers) {
6777 DEBUG(1, ("Active servers in context, "
6778 "free_context failed.\n"));
6782 if (context->internal->_files) {
6783 DEBUG(1, ("Active files in context, "
6784 "free_context failed.\n"));
6790 /* Things we have to clean up */
6791 SAFE_FREE(context->workgroup);
6792 SAFE_FREE(context->netbios_name);
6793 SAFE_FREE(context->user);
6795 DEBUG(3, ("Context %p succesfully freed\n", context));
6796 SAFE_FREE(context->internal);
6803 * Each time the context structure is changed, we have binary backward
6804 * compatibility issues. Instead of modifying the public portions of the
6805 * context structure to add new options, instead, we put them in the internal
6806 * portion of the context structure and provide a set function for these new
6810 smbc_option_set(SMBCCTX *context,
6812 ... /* option_value */)
6818 smbc_get_auth_data_with_context_fn auth_fn;
6823 va_start(ap, option_name);
6825 if (strcmp(option_name, "debug_to_stderr") == 0) {
6827 * Log to standard error instead of standard output.
6829 option_value.b = (bool) va_arg(ap, int);
6830 context->internal->_debug_stderr = option_value.b;
6832 } else if (strcmp(option_name, "full_time_names") == 0) {
6834 * Use new-style time attribute names, e.g. WRITE_TIME rather
6835 * than the old-style names such as M_TIME. This allows also
6836 * setting/getting CREATE_TIME which was previously
6837 * unimplemented. (Note that the old C_TIME was supposed to
6838 * be CHANGE_TIME but was confused and sometimes referred to
6841 option_value.b = (bool) va_arg(ap, int);
6842 context->internal->_full_time_names = option_value.b;
6844 } else if (strcmp(option_name, "open_share_mode") == 0) {
6846 * The share mode to use for files opened with
6847 * smbc_open_ctx(). The default is SMBC_SHAREMODE_DENY_NONE.
6849 option_value.i = va_arg(ap, int);
6850 context->internal->_share_mode =
6851 (smbc_share_mode) option_value.i;
6853 } else if (strcmp(option_name, "auth_function") == 0) {
6855 * Use the new-style authentication function which includes
6858 option_value.auth_fn =
6859 va_arg(ap, smbc_get_auth_data_with_context_fn);
6860 context->internal->_auth_fn_with_context =
6861 option_value.auth_fn;
6862 } else if (strcmp(option_name, "user_data") == 0) {
6864 * Save a user data handle which may be retrieved by the user
6865 * with smbc_option_get()
6867 option_value.v = va_arg(ap, void *);
6868 context->internal->_user_data = option_value.v;
6869 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
6871 * Save an encoded value for encryption level.
6872 * 0 = off, 1 = attempt, 2 = required.
6874 option_value.s = va_arg(ap, const char *);
6875 if (strcmp(option_value.s, "none") == 0) {
6876 context->internal->_smb_encryption_level = 0;
6877 } else if (strcmp(option_value.s, "request") == 0) {
6878 context->internal->_smb_encryption_level = 1;
6879 } else if (strcmp(option_value.s, "require") == 0) {
6880 context->internal->_smb_encryption_level = 2;
6889 * Retrieve the current value of an option
6892 smbc_option_get(SMBCCTX *context,
6895 if (strcmp(option_name, "debug_stderr") == 0) {
6897 * Log to standard error instead of standard output.
6899 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6900 return (void *) (intptr_t) context->internal->_debug_stderr;
6902 return (void *) context->internal->_debug_stderr;
6904 } else if (strcmp(option_name, "full_time_names") == 0) {
6906 * Use new-style time attribute names, e.g. WRITE_TIME rather
6907 * than the old-style names such as M_TIME. This allows also
6908 * setting/getting CREATE_TIME which was previously
6909 * unimplemented. (Note that the old C_TIME was supposed to
6910 * be CHANGE_TIME but was confused and sometimes referred to
6913 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
6914 return (void *) (intptr_t) context->internal->_full_time_names;
6916 return (void *) context->internal->_full_time_names;
6919 } else if (strcmp(option_name, "auth_function") == 0) {
6921 * Use the new-style authentication function which includes
6924 return (void *) context->internal->_auth_fn_with_context;
6925 } else if (strcmp(option_name, "user_data") == 0) {
6927 * Save a user data handle which may be retrieved by the user
6928 * with smbc_option_get()
6930 return context->internal->_user_data;
6931 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
6933 * Return the current smb encrypt negotiate option as a string.
6935 switch (context->internal->_smb_encryption_level) {
6937 return (void *) "none";
6939 return (void *) "request";
6941 return (void *) "require";
6943 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
6945 * Return the current smb encrypt status option as a bool.
6946 * false = off, true = on. We don't know what server is
6947 * being requested, so we only return true if all servers
6948 * are using an encrypted connection.
6951 unsigned int num_servers = 0;
6953 for (s = context->internal->_servers; s; s = s->next) {
6955 if (s->cli->trans_enc_state == NULL) {
6956 return (void *)false;
6959 return (void *) (bool) (num_servers > 0);
6967 * Initialise the library etc
6969 * We accept a struct containing handle information.
6970 * valid values for info->debug from 0 to 100,
6971 * and insist that info->fn must be non-null.
6974 smbc_init_context(SMBCCTX *context)
6980 if (!context || !context->internal) {
6985 /* Do not initialise the same client twice */
6986 if (context->internal->_initialized) {
6990 if ((!context->callbacks.auth_fn &&
6991 !context->internal->_auth_fn_with_context) ||
6992 context->debug < 0 ||
6993 context->debug > 100) {
7000 if (!smbc_initialized) {
7002 * Do some library-wide intializations the first time we get
7005 bool conf_loaded = False;
7006 TALLOC_CTX *frame = talloc_stackframe();
7008 /* Set this to what the user wants */
7009 DEBUGLEVEL = context->debug;
7013 setup_logging("libsmbclient", True);
7014 if (context->internal->_debug_stderr) {
7016 x_setbuf(x_stderr, NULL);
7019 /* Here we would open the smb.conf file if needed ... */
7021 in_client = True; /* FIXME, make a param */
7023 home = getenv("HOME");
7026 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
7027 if (lp_load(conf, True, False, False, True)) {
7030 DEBUG(5, ("Could not load config file: %s\n",
7039 * Well, if that failed, try the get_dyn_CONFIGFILE
7040 * Which points to the standard locn, and if that
7041 * fails, silently ignore it and use the internal
7045 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
7046 DEBUG(5, ("Could not load config file: %s\n",
7047 get_dyn_CONFIGFILE()));
7051 * We loaded the global config file. Now lets
7052 * load user-specific modifications to the
7056 "%s/.smb/smb.conf.append",
7058 if (!lp_load(conf, True, False, False, False)) {
7060 ("Could not append config file: "
7069 load_interfaces(); /* Load the list of interfaces ... */
7071 reopen_logs(); /* Get logging working ... */
7074 * Block SIGPIPE (from lib/util_sock.c: write())
7075 * It is not needed and should not stop execution
7077 BlockSignals(True, SIGPIPE);
7079 /* Done with one-time initialisation */
7080 smbc_initialized = 1;
7085 if (!context->user) {
7087 * FIXME: Is this the best way to get the user info?
7089 user = getenv("USER");
7090 /* walk around as "guest" if no username can be found */
7091 if (!user) context->user = SMB_STRDUP("guest");
7092 else context->user = SMB_STRDUP(user);
7095 if (!context->netbios_name) {
7097 * We try to get our netbios name from the config. If that
7098 * fails we fall back on constructing our netbios name from
7101 if (global_myname()) {
7102 context->netbios_name = SMB_STRDUP(global_myname());
7106 * Hmmm, I want to get hostname as well, but I am too
7107 * lazy for the moment
7110 context->netbios_name = (char *)SMB_MALLOC(17);
7111 if (!context->netbios_name) {
7115 slprintf(context->netbios_name, 16,
7116 "smbc%s%d", context->user, pid);
7120 DEBUG(1, ("Using netbios name %s.\n", context->netbios_name));
7122 if (!context->workgroup) {
7123 if (lp_workgroup()) {
7124 context->workgroup = SMB_STRDUP(lp_workgroup());
7127 /* TODO: Think about a decent default workgroup */
7128 context->workgroup = SMB_STRDUP("samba");
7132 DEBUG(1, ("Using workgroup %s.\n", context->workgroup));
7134 /* shortest timeout is 1 second */
7135 if (context->timeout > 0 && context->timeout < 1000)
7136 context->timeout = 1000;
7139 * FIXME: Should we check the function pointers here?
7142 context->internal->_initialized = True;
7148 /* Return the verion of samba, and thus libsmbclient */
7152 return samba_version_string();