[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / libsmb / libsmbclient.c
index c74e18b13c152c91d70b7a29538e558748771006..45226a028cbc57289596cfd1b657cb91ea7310b2 100644 (file)
@@ -9,7 +9,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 
 #include "include/libsmb_internal.h"
 
+struct smbc_dirent *smbc_readdir_ctx(SMBCCTX *context, SMBCFILE *dir);
+struct smbc_dir_list *smbc_check_dir_ent(struct smbc_dir_list *list, 
+                                        struct smbc_dirent *dirent);
 
 /*
  * DOS Attribute values (used internally)
@@ -336,14 +338,15 @@ smbc_parse_path(SMBCCTX *context,
            goto decoding;
 
        if (*p == '/') {
+               int wl = strlen(context->workgroup);
 
-               strncpy(server, context->workgroup, 
-                       ((strlen(context->workgroup) < 16)
-                         ? strlen(context->workgroup)
-                         : 16));
-                server[server_len - 1] = '\0';
+               if (wl > 16) {
+                       wl = 16;
+               }
+
+               strncpy(server, context->workgroup, wl);
+                server[wl] = '\0';
                return 0;
-               
        }
 
        /*
@@ -414,7 +417,15 @@ smbc_parse_path(SMBCCTX *context,
 
        }
 
-        safe_strcpy(path, p, path_len - 1);
+        /*
+         * Prepend a leading slash if there's a file path, as required by
+         * NetApp filers.
+         */
+        *path = '\0';
+        if (*p != '\0') {
+                *path = '/';
+                safe_strcpy(path + 1, p, path_len - 2);
+        }
 
        all_string_sub(path, "/", "\\", 0);
 
@@ -488,11 +499,11 @@ static int
 smbc_check_server(SMBCCTX * context,
                   SMBCSRV * server) 
 {
-       if ( send_keepalive(server->cli->fd) == False )
-               return 1;
+        socklen_t size;
+        struct sockaddr addr;
 
-       /* connection is ok */
-       return 0;
+        size = sizeof(addr);
+        return (getpeername(server->cli->fd, &addr, &size) == -1);
 }
 
 /* 
@@ -530,7 +541,7 @@ smbc_remove_unused_server(SMBCCTX * context,
 
        DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
 
-       context->callbacks.remove_cached_srv_fn(context, srv);
+       (context->callbacks.remove_cached_srv_fn)(context, srv);
 
         SAFE_FREE(srv);
        
@@ -550,19 +561,19 @@ find_server(SMBCCTX *context,
         
  check_server_cache:
 
-       srv = context->callbacks.get_cached_srv_fn(context, server, share, 
-                                                  workgroup, username);
+       srv = (context->callbacks.get_cached_srv_fn)(context, server, share, 
+                                                     workgroup, username);
 
        if (!auth_called && !srv && (!username[0] || !password[0])) {
                 if (context->internal->_auth_fn_with_context != NULL) {
-                         context->internal->_auth_fn_with_context(
+                        (context->internal->_auth_fn_with_context)(
                                 context,
                                 server, share,
                                 workgroup, sizeof(fstring),
                                 username, sizeof(fstring),
                                 password, sizeof(fstring));
                 } else {
-                        context->callbacks.auth_fn(
+                        (context->callbacks.auth_fn)(
                                 server, share,
                                 workgroup, sizeof(fstring),
                                 username, sizeof(fstring),
@@ -580,22 +591,22 @@ find_server(SMBCCTX *context,
        }
        
        if (srv) {
-               if (context->callbacks.check_server_fn(context, srv)) {
+               if ((context->callbacks.check_server_fn)(context, srv)) {
                        /*
                          * This server is no good anymore 
                          * Try to remove it and check for more possible
                          * servers in the cache
                          */
-                       if (context->callbacks.remove_unused_server_fn(context,
-                                                                       srv)) { 
+                       if ((context->callbacks.remove_unused_server_fn)(context,
+                                                                         srv)) { 
                                 /*
                                  * We could not remove the server completely,
                                  * remove it from the cache so we will not get
                                  * it again. It will be removed when the last
                                  * file/dir is closed.
                                  */
-                               context->callbacks.remove_cached_srv_fn(context,
-                                                                        srv);
+                               (context->callbacks.remove_cached_srv_fn)(context,
+                                                                          srv);
                        }
                        
                        /*
@@ -641,7 +652,8 @@ smbc_server(SMBCCTX *context,
         int port_try_first;
         int port_try_next;
         const char *username_used;
-  
+       NTSTATUS status;
+
        zero_ip(&ip);
        ZERO_STRUCT(c);
 
@@ -671,14 +683,14 @@ smbc_server(SMBCCTX *context,
                 if (srv->cli->cnum == (uint16) -1) {
                         /* Ensure we have accurate auth info */
                         if (context->internal->_auth_fn_with_context != NULL) {
-                                context->internal->_auth_fn_with_context(
+                                (context->internal->_auth_fn_with_context)(
                                         context,
                                         server, share,
                                         workgroup, sizeof(fstring),
                                         username, sizeof(fstring),
                                         password, sizeof(fstring));
                         } else {
-                                context->callbacks.auth_fn(
+                                (context->callbacks.auth_fn)(
                                         server, share,
                                         workgroup, sizeof(fstring),
                                         username, sizeof(fstring),
@@ -691,8 +703,8 @@ smbc_server(SMBCCTX *context,
                                 errno = smbc_errno(context, srv->cli);
                                 cli_shutdown(srv->cli);
                                srv->cli = NULL;
-                                context->callbacks.remove_cached_srv_fn(context,
-                                                                        srv);
+                                (context->callbacks.remove_cached_srv_fn)(context,
+                                                                          srv);
                                 srv = NULL;
                         }
 
@@ -761,17 +773,19 @@ smbc_server(SMBCCTX *context,
 
         c->port = port_try_first;
 
-       if (!cli_connect(c, server_n, &ip)) {
+       status = cli_connect(c, server_n, &ip);
+       if (!NT_STATUS_IS_OK(status)) {
 
                 /* First connection attempt failed.  Try alternate port. */
                 c->port = port_try_next;
 
-                if (!cli_connect(c, server_n, &ip)) {
-                        cli_shutdown(c);
-                        errno = ETIMEDOUT;
-                        return NULL;
-                }
-       }
+                status = cli_connect(c, server_n, &ip);
+               if (!NT_STATUS_IS_OK(status)) {
+                       cli_shutdown(c);
+                       errno = ETIMEDOUT;
+                       return NULL;
+               }
+       }
 
        if (!cli_session_request(c, &calling, &called)) {
                cli_shutdown(c);
@@ -867,7 +881,9 @@ smbc_server(SMBCCTX *context,
        /* now add it to the cache (internal or external)  */
        /* Let the cache function set errno if it wants to */
        errno = 0;
-       if (context->callbacks.add_cached_srv_fn(context, srv, server, share, workgroup, username)) {
+       if ((context->callbacks.add_cached_srv_fn)(context, srv,
+                                                   server, share,
+                                                   workgroup, username)) {
                int saved_errno = errno;
                DEBUG(3, (" Failed to add server to cache\n"));
                errno = saved_errno;
@@ -906,6 +922,7 @@ smbc_attr_server(SMBCCTX *context,
                  fstring password,
                  POLICY_HND *pol)
 {
+        int flags;
         struct in_addr ip;
        struct cli_state *ipc_cli;
        struct rpc_pipe_client *pipe_hnd;
@@ -925,14 +942,14 @@ smbc_attr_server(SMBCCTX *context,
                 if (*password == '\0') {
                         /* ... then retrieve it now. */
                         if (context->internal->_auth_fn_with_context != NULL) {
-                                context->internal->_auth_fn_with_context(
+                                (context->internal->_auth_fn_with_context)(
                                         context,
                                         server, share,
                                         workgroup, sizeof(fstring),
                                         username, sizeof(fstring),
                                         password, sizeof(fstring));
                         } else {
-                                context->callbacks.auth_fn(
+                                (context->callbacks.auth_fn)(
                                         server, share,
                                         workgroup, sizeof(fstring),
                                         username, sizeof(fstring),
@@ -940,12 +957,17 @@ smbc_attr_server(SMBCCTX *context,
                         }
                 }
         
+                flags = 0;
+                if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
+                        flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
+                }
+
                 zero_ip(&ip);
                 nt_status = cli_full_connection(&ipc_cli,
                                                 global_myname(), server, 
                                                 &ip, 0, "IPC$", "?????",  
                                                 username, workgroup,
-                                                password, 0,
+                                                password, flags,
                                                 Undefined, NULL);
                 if (! NT_STATUS_IS_OK(nt_status)) {
                         DEBUG(1,("cli_full_connection failed! (%s)\n",
@@ -999,11 +1021,11 @@ smbc_attr_server(SMBCCTX *context,
                 /* now add it to the cache (internal or external) */
 
                 errno = 0;      /* let cache function set errno if it likes */
-                if (context->callbacks.add_cached_srv_fn(context, ipc_srv,
-                                                         server,
-                                                         "*IPC$",
-                                                         workgroup,
-                                                         username)) {
+                if ((context->callbacks.add_cached_srv_fn)(context, ipc_srv,
+                                                           server,
+                                                           "*IPC$",
+                                                           workgroup,
+                                                           username)) {
                         DEBUG(3, (" Failed to add server to cache\n"));
                         if (errno == 0) {
                                 errno = ENOMEM;
@@ -1105,14 +1127,8 @@ smbc_open_ctx(SMBCCTX *context,
                }
                /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
                
-               if ( targetcli->dfsroot )
-               {
-                       pstring temppath;
-                       pstrcpy(temppath, targetpath);
-                       cli_dfs_make_full_path( targetpath, targetcli->desthost, targetcli->share, temppath);
-               }
-               
-               if ((fd = cli_open(targetcli, targetpath, flags, DENY_NONE)) < 0) {
+               if ((fd = cli_open(targetcli, targetpath, flags,
+                                   context->internal->_share_mode)) < 0) {
 
                        /* Handle the error ... */
 
@@ -1172,7 +1188,7 @@ smbc_open_ctx(SMBCCTX *context,
                int eno = 0;
 
                eno = smbc_errno(context, srv->cli);
-               file = context->opendir(context, fname);
+               file = (context->opendir)(context, fname);
                if (!file) errno = eno;
                return file;
 
@@ -1411,7 +1427,7 @@ smbc_close_ctx(SMBCCTX *context,
        /* IS a dir ... */
        if (!file->file) {
                
-               return context->closedir(context, file);
+               return (context->closedir)(context, file);
 
        }
 
@@ -1448,7 +1464,7 @@ smbc_close_ctx(SMBCCTX *context,
                DLIST_REMOVE(context->internal->_files, file);
                SAFE_FREE(file->fname);
                SAFE_FREE(file);
-               context->callbacks.remove_unused_server_fn(context, srv);
+               (context->callbacks.remove_unused_server_fn)(context, srv);
 
                return -1;
 
@@ -1507,14 +1523,6 @@ smbc_getatr(SMBCCTX * context,
                return False;
        }
        
-       if ( targetcli->dfsroot )
-       {
-               pstring temppath;
-               pstrcpy(temppath, targetpath);
-               cli_dfs_make_full_path(targetpath, targetcli->desthost,
-                                       targetcli->share, temppath);
-       }
-  
        if (!srv->no_pathinfo2 &&
             cli_qpathinfo2(targetcli, targetpath,
                            create_time_ts,
@@ -2169,7 +2177,7 @@ smbc_fstat_ctx(SMBCCTX *context,
 
        if (!file->file) {
 
-               return context->fstatdir(context, file, st);
+               return (context->fstatdir)(context, file, st);
 
        }
 
@@ -2540,6 +2548,7 @@ static SMBCFILE *
 smbc_opendir_ctx(SMBCCTX *context,
                  const char *fname)
 {
+        int saved_errno;
        fstring server, share, user, password, options;
        pstring workgroup;
        pstring path;
@@ -2547,6 +2556,7 @@ smbc_opendir_ctx(SMBCCTX *context,
         char *p;
        SMBCSRV *srv  = NULL;
        SMBCFILE *dir = NULL;
+        struct _smbc_callbacks *cb;
        struct in_addr rem_ip;
 
        if (!context || !context->internal ||
@@ -2645,7 +2655,9 @@ smbc_opendir_ctx(SMBCCTX *context,
                  */
 
                 ip_list = NULL;
-                if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
+                if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
+                                    &count)))
+               {
 
                         SAFE_FREE(ip_list);
 
@@ -2908,9 +2920,9 @@ smbc_opendir_ctx(SMBCCTX *context,
                                        SAFE_FREE(dir->fname);
                                        SAFE_FREE(dir);
                                }
-                               errno = smbc_errno(context, targetcli);
+                               saved_errno = smbc_errno(context, targetcli);
 
-                                if (errno == EINVAL) {
+                                if (saved_errno == EINVAL) {
                                     /*
                                      * See if they asked to opendir something
                                      * other than a directory.  If so, the
@@ -2926,12 +2938,36 @@ smbc_opendir_ctx(SMBCCTX *context,
                                         ! IS_DOS_DIR(mode)) {
 
                                         /* It is.  Correct the error value */
-                                        errno = ENOTDIR;
+                                        saved_errno = ENOTDIR;
                                     }
                                 }
 
-                               return NULL;
+                                /*
+                                 * If there was an error and the server is no
+                                 * good any more...
+                                 */
+                                cb = &context->callbacks;
+                                if (cli_is_error(targetcli) &&
+                                    (cb->check_server_fn)(context, srv)) {
+
+                                        /* ... then remove it. */
+                                        if ((cb->remove_unused_server_fn)(context,
+                                                                          srv)) { 
+                                                /*
+                                                 * We could not remove the
+                                                 * server completely, remove
+                                                 * it from the cache so we
+                                                 * will not get it again. It
+                                                 * will be removed when the
+                                                 * last file/dir is closed.
+                                                 */
+                                                (cb->remove_cached_srv_fn)(context,
+                                                                           srv);
+                                        }
+                                }
 
+                                errno = saved_errno;
+                               return NULL;
                        }
                }
 
@@ -3392,8 +3428,6 @@ static off_t
 smbc_telldir_ctx(SMBCCTX *context,
                  SMBCFILE *dir)
 {
-       off_t ret_val; /* Squash warnings about cast */
-
        if (!context || !context->internal ||
            !context->internal->_initialized) {
 
@@ -3416,12 +3450,16 @@ smbc_telldir_ctx(SMBCCTX *context,
 
        }
 
+        /* See if we're already at the end. */
+        if (dir->dir_next == NULL) {
+                /* We are. */
+                return -1;
+        }
+
        /*
         * We return the pointer here as the offset
         */
-       ret_val = (off_t)(long)dir->dir_next;
-       return ret_val;
-
+        return (off_t)(long)dir->dir_next->dirent;
 }
 
 /*
@@ -3492,6 +3530,11 @@ smbc_lseekdir_ctx(SMBCCTX *context,
 
        }
 
+        if (offset == -1) {     /* Seek to the end of the list */
+                dir->dir_next = NULL;
+                return 0;
+        }
+
        /* Now, run down the list and make sure that the entry is OK       */
        /* This may need to be changed if we change the format of the list */
 
@@ -3688,32 +3731,94 @@ smbc_utimes_ctx(SMBCCTX *context,
 }
 
 
-/* The MSDN is contradictory over the ordering of ACE entries in an ACL.
-   However NT4 gives a "The information may have been modified by a
-   computer running Windows NT 5.0" if denied ACEs do not appear before
-   allowed ACEs. */
+/*
+ * Sort ACEs according to the documentation at
+ * http://support.microsoft.com/kb/269175, at least as far as it defines the
+ * order.
+ */
 
 static int
 ace_compare(SEC_ACE *ace1,
             SEC_ACE *ace2)
 {
-       if (sec_ace_equal(ace1, ace2)) 
+        BOOL b1;
+        BOOL b2;
+
+        /* If the ACEs are equal, we have nothing more to do. */
+        if (sec_ace_equal(ace1, ace2)) {
                return 0;
+        }
+
+        /* Inherited follow non-inherited */
+        b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
+        b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
+        if (b1 != b2) {
+                return (b1 ? 1 : -1);
+        }
+
+        /*
+         * What shall we do with AUDITs and ALARMs?  It's undefined.  We'll
+         * sort them after DENY and ALLOW.
+         */
+        b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
+              ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
+              ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
+              ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
+        b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
+              ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
+              ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
+              ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
+        if (b1 != b2) {
+                return (b1 ? 1 : -1);
+        }
+
+        /* Allowed ACEs follow denied ACEs */
+        b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
+              ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
+        b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
+              ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
+        if (b1 != b2) {
+                return (b1 ? 1 : -1);
+        }
+
+        /*
+         * ACEs applying to an entity's object follow those applying to the
+         * entity itself
+         */
+        b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
+              ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
+        b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
+              ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
+        if (b1 != b2) {
+                return (b1 ? 1 : -1);
+        }
 
-       if (ace1->type != ace2->type) 
+        /*
+         * If we get this far, the ACEs are similar as far as the
+         * characteristics we typically care about (those defined by the
+         * referenced MS document).  We'll now sort by characteristics that
+         * just seems reasonable.
+         */
+        
+       if (ace1->type != ace2->type) {
                return ace2->type - ace1->type;
+        }
 
-       if (sid_compare(&ace1->trustee, &ace2->trustee)) 
+       if (sid_compare(&ace1->trustee, &ace2->trustee)) {
                return sid_compare(&ace1->trustee, &ace2->trustee);
+        }
 
-       if (ace1->flags != ace2->flags) 
+       if (ace1->flags != ace2->flags) {
                return ace1->flags - ace2->flags;
+        }
 
-       if (ace1->info.mask != ace2->info.mask) 
-               return ace1->info.mask - ace2->info.mask;
+       if (ace1->access_mask != ace2->access_mask) {
+               return ace1->access_mask - ace2->access_mask;
+        }
 
-       if (ace1->size != ace2->size) 
+       if (ace1->size != ace2->size) {
                return ace1->size - ace2->size;
+        }
 
        return memcmp(ace1, ace2, sizeof(SEC_ACE));
 }
@@ -3725,14 +3830,14 @@ sort_acl(SEC_ACL *the_acl)
        uint32 i;
        if (!the_acl) return;
 
-       qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]),
+       qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
               QSORT_CAST ace_compare);
 
        for (i=1;i<the_acl->num_aces;) {
-               if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
+               if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
                        int j;
                        for (j=i; j<the_acl->num_aces-1; j++) {
-                               the_acl->ace[j] = the_acl->ace[j+1];
+                               the_acl->aces[j] = the_acl->aces[j+1];
                        }
                        the_acl->num_aces--;
                } else {
@@ -3751,7 +3856,7 @@ convert_sid_to_string(struct cli_state *ipc_cli,
 {
        char **domains = NULL;
        char **names = NULL;
-       enum SID_NAME_USE *types = NULL;
+       enum lsa_SidType *types = NULL;
        struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
        sid_to_string(str, sid);
 
@@ -3787,7 +3892,7 @@ convert_string_to_sid(struct cli_state *ipc_cli,
                       DOM_SID *sid,
                       const char *str)
 {
-       enum SID_NAME_USE *types = NULL;
+       enum lsa_SidType *types = NULL;
        DOM_SID *sids = NULL;
        BOOL result = True;
        struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
@@ -3806,7 +3911,7 @@ convert_string_to_sid(struct cli_state *ipc_cli,
         }
 
        if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ipc_cli->mem_ctx, 
-                                                 pol, 1, &str, NULL, &sids, 
+                                                 pol, 1, &str, NULL, 1, &sids, 
                                                  &types))) {
                result = False;
                goto done;
@@ -3937,7 +4042,7 @@ parse_ace(struct cli_state *ipc_cli,
        }
 
  done:
-       mask.mask = amask;
+       mask = amask;
        init_sec_ace(ace, &sid, atype, mask, aflags);
        return True;
 }
@@ -3959,7 +4064,7 @@ add_ace(SEC_ACL **the_acl,
        if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
                return False;
        }
-       memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
+       memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
        memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
        newacl = make_sec_acl(ctx, (*the_acl)->revision,
                               1+(*the_acl)->num_aces, aces);
@@ -3981,7 +4086,7 @@ sec_desc_parse(TALLOC_CTX *ctx,
        fstring tok;
        SEC_DESC *ret = NULL;
        size_t sd_size;
-       DOM_SID *grp_sid=NULL;
+       DOM_SID *group_sid=NULL;
         DOM_SID *owner_sid=NULL;
        SEC_ACL *dacl=NULL;
        int revision=1;
@@ -4026,15 +4131,15 @@ sec_desc_parse(TALLOC_CTX *ctx,
                }
 
                if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
-                       if (grp_sid) {
+                       if (group_sid) {
                                DEBUG(5, ("GROUP specified more than once!\n"));
                                goto done;
                        }
-                       grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
-                       if (!grp_sid ||
+                       group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
+                       if (!group_sid ||
                            !convert_string_to_sid(ipc_cli, pol,
                                                    numeric,
-                                                   grp_sid, tok+6)) {
+                                                   group_sid, tok+6)) {
                                DEBUG(5, ("Failed to parse group sid\n"));
                                goto done;
                        }
@@ -4042,15 +4147,15 @@ sec_desc_parse(TALLOC_CTX *ctx,
                }
 
                if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
-                       if (grp_sid) {
+                       if (group_sid) {
                                DEBUG(5, ("GROUP specified more than once!\n"));
                                goto done;
                        }
-                       grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
-                       if (!grp_sid ||
+                       group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
+                       if (!group_sid ||
                            !convert_string_to_sid(ipc_cli, pol,
                                                    False,
-                                                   grp_sid, tok+6)) {
+                                                   group_sid, tok+6)) {
                                DEBUG(5, ("Failed to parse group sid\n"));
                                goto done;
                        }
@@ -4088,10 +4193,10 @@ sec_desc_parse(TALLOC_CTX *ctx,
        }
 
        ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE, 
-                           owner_sid, grp_sid, NULL, dacl, &sd_size);
+                           owner_sid, group_sid, NULL, dacl, &sd_size);
 
   done:
-       SAFE_FREE(grp_sid);
+       SAFE_FREE(group_sid);
        SAFE_FREE(owner_sid);
 
        return ret;
@@ -4219,11 +4324,14 @@ dos_attr_parse(SMBCCTX *context,
                        continue;
                }
 
-                n = strlen(attr_strings.create_time_attr);
-                if (attr_strings.create_time_attr != NULL &&
-                    StrnCaseCmp(tok, attr_strings.create_time_attr, n) == 0) {
-                        dad->create_time = (time_t)strtol(tok+n+1, NULL, 10);
-                       continue;
+               if (attr_strings.create_time_attr != NULL) {
+                       n = strlen(attr_strings.create_time_attr);
+                       if (StrnCaseCmp(tok, attr_strings.create_time_attr,
+                                       n) == 0) {
+                               dad->create_time = (time_t)strtol(tok+n+1,
+                                                                 NULL, 10);
+                               continue;
+                       }
                }
 
                if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
@@ -4481,6 +4589,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_nt_owner) {
@@ -4503,7 +4612,7 @@ cacl_get(SMBCCTX *context,
                                                 return -1;
                                         }
                                         n = strlen(p);
-                                } else {
+                                } else if (sidstr[0] != '\0') {
                                         n = snprintf(buf, bufsize,
                                                      ",OWNER:%s", sidstr);
                                 }
@@ -4528,13 +4637,14 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_nt_group) {
-                        if (sd->grp_sid) {
+                        if (sd->group_sid) {
                                 convert_sid_to_string(ipc_cli, pol,
                                                       sidstr, numeric,
-                                                      sd->grp_sid);
+                                                      sd->group_sid);
                         } else {
                                 fstrcpy(sidstr, "");
                         }
@@ -4548,7 +4658,7 @@ cacl_get(SMBCCTX *context,
                                                 return -1;
                                         }
                                         n = strlen(p);
-                                } else {
+                                } else if (sidstr[0] != '\0') {
                                         n = snprintf(buf, bufsize,
                                                      ",GROUP:%s", sidstr);
                                 }
@@ -4573,13 +4683,14 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_nt_acl) {
                         /* Add aces to value buffer  */
                         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
 
-                                SEC_ACE *ace = &sd->dacl->ace[i];
+                                SEC_ACE *ace = &sd->dacl->aces[i];
                                 convert_sid_to_string(ipc_cli, pol,
                                                       sidstr, numeric,
                                                       &ace->trustee);
@@ -4593,7 +4704,7 @@ cacl_get(SMBCCTX *context,
                                                         sidstr,
                                                         ace->type,
                                                         ace->flags,
-                                                        ace->info.mask);
+                                                        ace->access_mask);
                                                 if (!p) {
                                                         errno = ENOMEM;
                                                         return -1;
@@ -4606,7 +4717,7 @@ cacl_get(SMBCCTX *context,
                                                         sidstr,
                                                         ace->type,
                                                         ace->flags,
-                                                        ace->info.mask);
+                                                        ace->access_mask);
                                         }
                                 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
                                             StrCaseCmp(name+3, sidstr) == 0) ||
@@ -4618,7 +4729,7 @@ cacl_get(SMBCCTX *context,
                                                         "%d/%d/0x%08x", 
                                                         ace->type,
                                                         ace->flags,
-                                                        ace->info.mask);
+                                                        ace->access_mask);
                                                 if (!p) {
                                                         errno = ENOMEM;
                                                         return -1;
@@ -4629,7 +4740,7 @@ cacl_get(SMBCCTX *context,
                                                              "%d/%d/0x%08x", 
                                                              ace->type,
                                                              ace->flags,
-                                                             ace->info.mask);
+                                                             ace->access_mask);
                                         }
                                 } else if (all_nt_acls) {
                                         if (determine_size) {
@@ -4640,7 +4751,7 @@ cacl_get(SMBCCTX *context,
                                                         sidstr,
                                                         ace->type,
                                                         ace->flags,
-                                                        ace->info.mask);
+                                                        ace->access_mask);
                                                 if (!p) {
                                                         errno = ENOMEM;
                                                         return -1;
@@ -4653,16 +4764,17 @@ cacl_get(SMBCCTX *context,
                                                              sidstr,
                                                              ace->type,
                                                              ace->flags,
-                                                             ace->info.mask);
+                                                             ace->access_mask);
                                         }
                                 }
-                                if (n > bufsize) {
+                                if (!determine_size && n > bufsize) {
                                         errno = ERANGE;
                                         return -1;
                                 }
                                 buf += n;
                                 n_used += n;
                                 bufsize -= n;
+                                n = 0;
                         }
                 }
 
@@ -4737,6 +4849,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_dos_size) {
@@ -4781,6 +4894,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_dos_create_time &&
@@ -4823,6 +4937,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_dos_access_time) {
@@ -4864,6 +4979,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_dos_write_time) {
@@ -4905,6 +5021,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_dos_change_time) {
@@ -4946,6 +5063,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 if (! exclude_dos_inode) {
@@ -4990,6 +5108,7 @@ cacl_get(SMBCCTX *context,
                         buf += n;
                         n_used += n;
                         bufsize -= n;
+                        n = 0;
                 }
 
                 /* Restore name pointer to its original value */
@@ -5023,7 +5142,7 @@ cacl_set(TALLOC_CTX *ctx,
        SEC_DESC *sd = NULL, *old;
         SEC_ACL *dacl = NULL;
        DOM_SID *owner_sid = NULL; 
-       DOM_SID *grp_sid = NULL;
+       DOM_SID *group_sid = NULL;
        uint32 i, j;
        size_t sd_size;
        int ret = 0;
@@ -5084,9 +5203,6 @@ cacl_set(TALLOC_CTX *ctx,
        switch (mode) {
        case SMBC_XATTR_MODE_REMOVE_ALL:
                 old->dacl->num_aces = 0;
-                SAFE_FREE(old->dacl->ace);
-                SAFE_FREE(old->dacl);
-                old->off_dacl = 0;
                 dacl = old->dacl;
                 break;
 
@@ -5095,19 +5211,14 @@ cacl_set(TALLOC_CTX *ctx,
                        BOOL found = False;
 
                        for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
-                                if (sec_ace_equal(&sd->dacl->ace[i],
-                                                  &old->dacl->ace[j])) {
+                                if (sec_ace_equal(&sd->dacl->aces[i],
+                                                  &old->dacl->aces[j])) {
                                        uint32 k;
                                        for (k=j; k<old->dacl->num_aces-1;k++) {
-                                               old->dacl->ace[k] =
-                                                        old->dacl->ace[k+1];
+                                               old->dacl->aces[k] =
+                                                        old->dacl->aces[k+1];
                                        }
                                        old->dacl->num_aces--;
-                                       if (old->dacl->num_aces == 0) {
-                                               SAFE_FREE(old->dacl->ace);
-                                               SAFE_FREE(old->dacl);
-                                               old->off_dacl = 0;
-                                       }
                                        found = True;
                                         dacl = old->dacl;
                                        break;
@@ -5127,14 +5238,14 @@ cacl_set(TALLOC_CTX *ctx,
                        BOOL found = False;
 
                        for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
-                               if (sid_equal(&sd->dacl->ace[i].trustee,
-                                             &old->dacl->ace[j].trustee)) {
+                               if (sid_equal(&sd->dacl->aces[i].trustee,
+                                             &old->dacl->aces[j].trustee)) {
                                         if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
                                                 err = EEXIST;
                                                 ret = -1;
                                                 goto failed;
                                         }
-                                        old->dacl->ace[j] = sd->dacl->ace[i];
+                                        old->dacl->aces[j] = sd->dacl->aces[i];
                                         ret = -1;
                                        found = True;
                                }
@@ -5147,7 +5258,7 @@ cacl_set(TALLOC_CTX *ctx,
                        }
                         
                         for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
-                                add_ace(&old->dacl, &sd->dacl->ace[i], ctx);
+                                add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
                         }
                }
                 dacl = old->dacl;
@@ -5156,7 +5267,7 @@ cacl_set(TALLOC_CTX *ctx,
        case SMBC_XATTR_MODE_SET:
                old = sd;
                 owner_sid = old->owner_sid;
-                grp_sid = old->grp_sid;
+                group_sid = old->group_sid;
                 dacl = old->dacl;
                break;
 
@@ -5165,7 +5276,7 @@ cacl_set(TALLOC_CTX *ctx,
                 break;
 
         case SMBC_XATTR_MODE_CHGRP:
-                grp_sid = sd->grp_sid;
+                group_sid = sd->group_sid;
                 break;
        }
 
@@ -5174,7 +5285,7 @@ cacl_set(TALLOC_CTX *ctx,
 
        /* Create new security descriptor and set it */
        sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE, 
-                          owner_sid, grp_sid, NULL, dacl, &sd_size);
+                          owner_sid, group_sid, NULL, dacl, &sd_size);
 
        fnum = cli_nt_create(cli, filename,
                              WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
@@ -5274,7 +5385,9 @@ smbc_setxattr_ctx(SMBCCTX *context,
                 ipc_srv = smbc_attr_server(context, server, share,
                                            workgroup, user, password,
                                            &pol);
-                srv->no_nt_session = True;
+                if (! ipc_srv) {
+                        srv->no_nt_session = True;
+                }
         } else {
                 ipc_srv = NULL;
         }
@@ -5699,7 +5812,9 @@ smbc_removexattr_ctx(SMBCCTX *context,
                 ipc_srv = smbc_attr_server(context, server, share,
                                            workgroup, user, password,
                                            &pol);
-                srv->no_nt_session = True;
+                if (! ipc_srv) {
+                        srv->no_nt_session = True;
+                }
         } else {
                 ipc_srv = NULL;
         }
@@ -5870,7 +5985,7 @@ smbc_open_print_job_ctx(SMBCCTX *context,
 
         /* What if the path is empty, or the file exists? */
 
-        return context->open(context, fname, O_WRONLY, 666);
+        return (context->open)(context, fname, O_WRONLY, 666);
 
 }
 
@@ -5911,7 +6026,7 @@ smbc_print_file_ctx(SMBCCTX *c_file,
 
         /* Try to open the file for reading ... */
 
-        if ((long)(fid1 = c_file->open(c_file, fname, O_RDONLY, 0666)) < 0) {
+        if ((long)(fid1 = (c_file->open)(c_file, fname, O_RDONLY, 0666)) < 0) {
                 
                 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
                 return -1;  /* smbc_open sets errno */
@@ -5920,24 +6035,24 @@ smbc_print_file_ctx(SMBCCTX *c_file,
 
         /* Now, try to open the printer file for writing */
 
-        if ((long)(fid2 = c_print->open_print_job(c_print, printq)) < 0) {
+        if ((long)(fid2 = (c_print->open_print_job)(c_print, printq)) < 0) {
 
                 saverr = errno;  /* Save errno */
-                c_file->close_fn(c_file, fid1);
+                (c_file->close_fn)(c_file, fid1);
                 errno = saverr;
                 return -1;
 
         }
 
-        while ((bytes = c_file->read(c_file, fid1, buf, sizeof(buf))) > 0) {
+        while ((bytes = (c_file->read)(c_file, fid1, buf, sizeof(buf))) > 0) {
 
                 tot_bytes += bytes;
 
-                if ((c_print->write(c_print, fid2, buf, bytes)) < 0) {
+                if (((c_print->write)(c_print, fid2, buf, bytes)) < 0) {
 
                         saverr = errno;
-                        c_file->close_fn(c_file, fid1);
-                        c_print->close_fn(c_print, fid2);
+                        (c_file->close_fn)(c_file, fid1);
+                        (c_print->close_fn)(c_print, fid2);
                         errno = saverr;
 
                 }
@@ -5946,8 +6061,8 @@ smbc_print_file_ctx(SMBCCTX *c_file,
 
         saverr = errno;
 
-        c_file->close_fn(c_file, fid1);  /* We have to close these anyway */
-        c_print->close_fn(c_print, fid2);
+        (c_file->close_fn)(c_file, fid1);  /* We have to close these anyway */
+        (c_print->close_fn)(c_print, fid2);
 
         if (bytes < 0) {
 
@@ -6134,6 +6249,8 @@ smbc_new_context(void)
        context->options.browse_max_lmb_count      = 3;    /* # LMBs to query */
        context->options.urlencode_readdir_entries = False;/* backward compat */
        context->options.one_share_per_server      = False;/* backward compat */
+        context->internal->_share_mode             = SMBC_SHAREMODE_DENY_NONE;
+                                /* backward compat */
 
         context->open                              = smbc_open_ctx;
         context->creat                             = smbc_creat_ctx;
@@ -6195,7 +6312,7 @@ smbc_free_context(SMBCCTX *context,
                 
                 f = context->internal->_files;
                 while (f) {
-                        context->close_fn(context, f);
+                        (context->close_fn)(context, f);
                         f = f->next;
                 }
                 context->internal->_files = NULL;
@@ -6211,8 +6328,8 @@ smbc_free_context(SMBCCTX *context,
                                 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
                                           s, s->cli->fd));
                                 cli_shutdown(s->cli);
-                                context->callbacks.remove_cached_srv_fn(context,
-                                                                        s);
+                                (context->callbacks.remove_cached_srv_fn)(context,
+                                                                          s);
                                 next = s->next;
                                 DLIST_REMOVE(context->internal->_servers, s);
                                 SAFE_FREE(s);
@@ -6223,7 +6340,7 @@ smbc_free_context(SMBCCTX *context,
         }
         else {
                 /* This is the polite way */    
-                if (context->callbacks.purge_cached_fn(context)) {
+                if ((context->callbacks.purge_cached_fn)(context)) {
                         DEBUG(1, ("Could not purge all servers, "
                                   "free_context failed.\n"));
                         errno = EBUSY;
@@ -6269,6 +6386,7 @@ smbc_option_set(SMBCCTX *context,
 {
         va_list ap;
         union {
+                int i;
                 BOOL b;
                 smbc_get_auth_data_with_context_fn auth_fn;
                 void *v;
@@ -6295,6 +6413,15 @@ smbc_option_set(SMBCCTX *context,
                 option_value.b = (BOOL) va_arg(ap, int);
                 context->internal->_full_time_names = option_value.b;
 
+        } else if (strcmp(option_name, "open_share_mode") == 0) {
+                /*
+                 * The share mode to use for files opened with
+                 * smbc_open_ctx().  The default is SMBC_SHAREMODE_DENY_NONE.
+                 */
+                option_value.i = va_arg(ap, int);
+                context->internal->_share_mode =
+                        (smbc_share_mode) option_value.i;
+
         } else if (strcmp(option_name, "auth_function") == 0) {
                 /*
                  * Use the new-style authentication function which includes