r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / libsmbclient.c
index c4e36012f924e0559494d14041827c789082e823..90cde9100af4d33ca01ba7ea26bdcb53ee3a8e22 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)
 typedef struct DOS_ATTR_DESC {
        int mode;
        SMB_OFF_T size;
-       time_t a_time;
-       time_t c_time;
-       time_t m_time;
+       time_t create_time;
+       time_t access_time;
+       time_t write_time;
+       time_t change_time;
        SMB_INO_T inode;
 } DOS_ATTR_DESC;
 
@@ -181,7 +184,8 @@ smbc_urldecode(char *dest, char * src, size_t max_dest_len)
 
         *p = '\0';
 
-        strncpy(dest, temp, max_dest_len);
+        strncpy(dest, temp, max_dest_len - 1);
+        dest[max_dest_len - 1] = '\0';
 
         return err_count;
 }
@@ -235,7 +239,7 @@ smbc_urlencode(char * dest, char * src, int max_dest_len)
  *
  *
  * We accept:
- *  smb://[[[domain;]user[:password@]]server[/share[/path[/file]]]][?options]
+ *  smb://[[[domain;]user[:password]@]server[/share[/path[/file]]]][?options]
  *
  * Meaning of URLs:
  *
@@ -268,6 +272,7 @@ static const char *smbc_prefix = "smb:";
 static int
 smbc_parse_path(SMBCCTX *context,
                 const char *fname,
+                char *workgroup, int workgroup_len,
                 char *server, int server_len,
                 char *share, int share_len,
                 char *path, int path_len,
@@ -282,6 +287,16 @@ smbc_parse_path(SMBCCTX *context,
        int len;
 
        server[0] = share[0] = path[0] = user[0] = password[0] = (char)0;
+
+        /*
+         * Assume we wont find an authentication domain to parse, so default
+         * to the workgroup in the provided context.
+         */
+        if (workgroup != NULL) {
+                strncpy(workgroup, context->workgroup, workgroup_len - 1);
+                workgroup[workgroup_len - 1] = '\0';
+        }
+
         if (options != NULL && options_len > 0) {
                 options[0] = (char)0;
         }
@@ -323,13 +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));
+               if (wl > 16) {
+                       wl = 16;
+               }
+
+               strncpy(server, context->workgroup, wl);
+                server[wl] = '\0';
                return 0;
-               
        }
 
        /*
@@ -346,19 +363,19 @@ smbc_parse_path(SMBCCTX *context,
                pstring username, passwd, domain;
                const char *u = userinfo;
 
-               next_token(&p, userinfo, "@", sizeof(fstring));
+               next_token_no_ltrim(&p, userinfo, "@", sizeof(fstring));
 
                username[0] = passwd[0] = domain[0] = 0;
 
                if (strchr_m(u, ';')) {
       
-                       next_token(&u, domain, ";", sizeof(fstring));
+                       next_token_no_ltrim(&u, domain, ";", sizeof(fstring));
 
                }
 
                if (strchr_m(u, ':')) {
 
-                       next_token(&u, username, ":", sizeof(fstring));
+                       next_token_no_ltrim(&u, username, ":", sizeof(fstring));
 
                        pstrcpy(passwd, u);
 
@@ -369,11 +386,20 @@ smbc_parse_path(SMBCCTX *context,
 
                }
 
-               if (username[0])
-                       strncpy(user, username, user_len);  /* FIXME, domain */
+                if (domain[0] && workgroup) {
+                        strncpy(workgroup, domain, workgroup_len - 1);
+                        workgroup[workgroup_len - 1] = '\0';
+                }
+
+               if (username[0]) {
+                       strncpy(user, username, user_len - 1);
+                        user[user_len - 1] = '\0';
+                }
 
-               if (passwd[0])
-                       strncpy(password, passwd, password_len);
+               if (passwd[0]) {
+                       strncpy(password, passwd, password_len - 1);
+                        password[password_len - 1] = '\0';
+                }
 
        }
 
@@ -391,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);
 
@@ -465,8 +499,30 @@ static int
 smbc_check_server(SMBCCTX * context,
                   SMBCSRV * server) 
 {
-       if ( send_keepalive(server->cli.fd) == False )
-               return 1;
+        socklen_t size;
+        struct sockaddr addr;
+
+        /*
+         * Although the use of port 139 is not a guarantee that we're using
+         * netbios, we assume so.  We don't want to send a keepalive packet if
+         * not netbios because it's not valid, and Vista, at least,
+         * disconnects the client on such a request.
+         */
+        if (server->cli->port == 139) {
+                /* Assuming netbios.  Send a keepalive packet */
+                if ( send_keepalive(server->cli->fd) == False ) {
+                        return 1;
+                }
+        } else {
+                /*
+                 * Assuming not netbios.  Try a different method to detect if
+                 * the connection is still alive.
+                 */
+                size = sizeof(addr);
+                if (getpeername(server->cli->fd, &addr, &size) == -1) {
+                        return 1;
+                }
+        }
 
        /* connection is ok */
        return 0;
@@ -502,7 +558,8 @@ smbc_remove_unused_server(SMBCCTX * context,
 
        DLIST_REMOVE(context->internal->_servers, srv);
 
-       cli_shutdown(&srv->cli);
+       cli_shutdown(srv->cli);
+       srv->cli = NULL;
 
        DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv));
 
@@ -530,10 +587,21 @@ find_server(SMBCCTX *context,
                                                   workgroup, username);
 
        if (!auth_called && !srv && (!username[0] || !password[0])) {
-               context->callbacks.auth_fn(server, share,
-                                           workgroup, sizeof(fstring),
-                                           username, sizeof(fstring),
-                                           password, sizeof(fstring));
+                if (context->internal->_auth_fn_with_context != NULL) {
+                         context->internal->_auth_fn_with_context(
+                                context,
+                                server, share,
+                                workgroup, sizeof(fstring),
+                                username, sizeof(fstring),
+                                password, sizeof(fstring));
+                } else {
+                        context->callbacks.auth_fn(
+                                server, share,
+                                workgroup, sizeof(fstring),
+                                username, sizeof(fstring),
+                                password, sizeof(fstring));
+                }
+
                /*
                  * However, smbc_auth_fn may have picked up info relating to
                  * an existing connection, so try for an existing connection
@@ -589,6 +657,7 @@ find_server(SMBCCTX *context,
 
 static SMBCSRV *
 smbc_server(SMBCCTX *context,
+            BOOL connect_if_not_found,
             const char *server,
             const char *share, 
             fstring workgroup,
@@ -596,7 +665,7 @@ smbc_server(SMBCCTX *context,
             fstring password)
 {
        SMBCSRV *srv=NULL;
-       struct cli_state c;
+       struct cli_state *c;
        struct nmb_name called, calling;
        const char *server_n = server;
        pstring ipenv;
@@ -605,7 +674,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);
 
@@ -632,25 +702,41 @@ smbc_server(SMBCCTX *context,
                  * disconnect if the requested share is not the same as the
                  * one that was already connected.
                  */
-                if (srv->cli.cnum == (uint16) -1) {
+                if (srv->cli->cnum == (uint16) -1) {
                         /* Ensure we have accurate auth info */
-                        context->callbacks.auth_fn(server, share,
-                                                   workgroup, sizeof(fstring),
-                                                   username, sizeof(fstring),
-                                                   password, sizeof(fstring));
+                        if (context->internal->_auth_fn_with_context != NULL) {
+                                context->internal->_auth_fn_with_context(
+                                        context,
+                                        server, share,
+                                        workgroup, sizeof(fstring),
+                                        username, sizeof(fstring),
+                                        password, sizeof(fstring));
+                        } else {
+                                context->callbacks.auth_fn(
+                                        server, share,
+                                        workgroup, sizeof(fstring),
+                                        username, sizeof(fstring),
+                                        password, sizeof(fstring));
+                        }
 
-                        if (! cli_send_tconX(&srv->cli, share, "?????",
+                        if (! cli_send_tconX(srv->cli, share, "?????",
                                              password, strlen(password)+1)) {
                         
-                                errno = smbc_errno(context, &srv->cli);
-                                cli_shutdown(&srv->cli);
-                                context->callbacks.remove_cached_srv_fn(context, srv);
+                                errno = smbc_errno(context, srv->cli);
+                                cli_shutdown(srv->cli);
+                               srv->cli = NULL;
+                                context->callbacks.remove_cached_srv_fn(context,
+                                                                        srv);
                                 srv = NULL;
                         }
 
-                        /* Regenerate the dev value since it's based on both server and share */
+                        /*
+                         * Regenerate the dev value since it's based on both
+                         * server and share
+                         */
                         if (srv) {
-                                srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
+                                srv->dev = (dev_t)(str_checksum(server) ^
+                                                   str_checksum(share));
                         }
                 }
         }
@@ -662,6 +748,12 @@ smbc_server(SMBCCTX *context,
                 return srv;
         }
 
+        /* If we're not asked to connect when a connection doesn't exist... */
+        if (! connect_if_not_found) {
+                /* ... then we're done here. */
+                return NULL;
+        }
+
        make_nmb_name(&calling, context->netbios_name, 0x0);
        make_nmb_name(&called , server, 0x20);
 
@@ -675,19 +767,19 @@ smbc_server(SMBCCTX *context,
        zero_ip(&ip);
 
        /* have to open a new connection */
-       if (!cli_initialise(&c)) {
+       if ((c = cli_initialise()) == NULL) {
                errno = ENOMEM;
                return NULL;
        }
 
        if (context->flags & SMB_CTX_FLAG_USE_KERBEROS) {
-               c.use_kerberos = True;
+               c->use_kerberos = True;
        }
        if (context->flags & SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS) {
-               c.fallback_after_kerberos = True;
+               c->fallback_after_kerberos = True;
        }
 
-       c.timeout = context->timeout;
+       c->timeout = context->timeout;
 
         /*
          * Force use of port 139 for first try if share is $IPC, empty, or
@@ -701,49 +793,49 @@ smbc_server(SMBCCTX *context,
                 port_try_next = 139;
         }
 
-        c.port = port_try_first;
+        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;
+                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);
+       if (!cli_session_request(c, &calling, &called)) {
+               cli_shutdown(c);
                if (strcmp(called.name, "*SMBSERVER")) {
                        make_nmb_name(&called , "*SMBSERVER", 0x20);
                        goto again;
-               }
-               else {  /* Try one more time, but ensure we don't loop */
-
-                 /* Only try this if server is an IP address ... */
-
-                 if (is_ipaddress(server) && !tried_reverse) {
-                   fstring remote_name;
-                   struct in_addr rem_ip;
+               } else {  /* Try one more time, but ensure we don't loop */
 
-                   if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
-                     DEBUG(4, ("Could not convert IP address %s to struct in_addr\n", server));
-                     errno = ETIMEDOUT;
-                     return NULL;
-                   }
+                       /* Only try this if server is an IP address ... */
 
-                   tried_reverse++; /* Yuck */
+                       if (is_ipaddress(server) && !tried_reverse) {
+                               fstring remote_name;
+                               struct in_addr rem_ip;
 
-                   if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
-                     make_nmb_name(&called, remote_name, 0x20);
-                     goto again;
-                   }
+                               if ((rem_ip.s_addr=inet_addr(server)) == INADDR_NONE) {
+                                       DEBUG(4, ("Could not convert IP address "
+                                               "%s to struct in_addr\n", server));
+                                       errno = ETIMEDOUT;
+                                       return NULL;
+                               }
 
+                               tried_reverse++; /* Yuck */
 
-                 }
+                               if (name_status_find("*", 0, 0, rem_ip, remote_name)) {
+                                       make_nmb_name(&called, remote_name, 0x20);
+                                       goto again;
+                               }
+                       }
                }
                errno = ETIMEDOUT;
                return NULL;
@@ -751,29 +843,29 @@ smbc_server(SMBCCTX *context,
   
        DEBUG(4,(" session request ok\n"));
   
-       if (!cli_negprot(&c)) {
-               cli_shutdown(&c);
+       if (!cli_negprot(c)) {
+               cli_shutdown(c);
                errno = ETIMEDOUT;
                return NULL;
        }
 
         username_used = username;
 
-       if (!cli_session_setup(&c, username_used, 
-                              password, strlen(password),
-                              password, strlen(password),
-                              workgroup)) {
+       if (!NT_STATUS_IS_OK(cli_session_setup(c, username_used, 
+                                              password, strlen(password),
+                                              password, strlen(password),
+                                              workgroup))) {
                 
                 /* Failed.  Try an anonymous login, if allowed by flags. */
                 username_used = "";
 
                 if ((context->flags & SMBCCTX_FLAG_NO_AUTO_ANONYMOUS_LOGON) ||
-                     !cli_session_setup(&c, username_used,
-                                        password, 1,
-                                        password, 0,
-                                        workgroup)) {
+                     !NT_STATUS_IS_OK(cli_session_setup(c, username_used,
+                                                       password, 1,
+                                                       password, 0,
+                                                       workgroup))) {
 
-                        cli_shutdown(&c);
+                        cli_shutdown(c);
                         errno = EPERM;
                         return NULL;
                 }
@@ -781,10 +873,10 @@ smbc_server(SMBCCTX *context,
 
        DEBUG(4,(" session setup ok\n"));
 
-       if (!cli_send_tconX(&c, share, "?????",
+       if (!cli_send_tconX(c, share, "?????",
                            password, strlen(password)+1)) {
-               errno = smbc_errno(context, &c);
-               cli_shutdown(&c);
+               errno = smbc_errno(context, c);
+               cli_shutdown(c);
                return NULL;
        }
   
@@ -803,7 +895,6 @@ smbc_server(SMBCCTX *context,
 
        ZERO_STRUCTP(srv);
        srv->cli = c;
-        srv->cli.allocated = False;
        srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
         srv->no_pathinfo = False;
         srv->no_pathinfo2 = False;
@@ -829,8 +920,10 @@ smbc_server(SMBCCTX *context,
        return srv;
 
  failed:
-       cli_shutdown(&c);
-       if (!srv) return NULL;
+       cli_shutdown(c);
+       if (!srv) {
+               return NULL;
+       }
   
        SAFE_FREE(srv);
        return NULL;
@@ -849,6 +942,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;
@@ -867,18 +961,33 @@ smbc_attr_server(SMBCCTX *context,
                 /* We didn't find a cached connection.  Get the password */
                 if (*password == '\0') {
                         /* ... then retrieve it now. */
-                        context->callbacks.auth_fn(server, share,
-                                                   workgroup, sizeof(fstring),
-                                                   username, sizeof(fstring),
-                                                   password, sizeof(fstring));
+                        if (context->internal->_auth_fn_with_context != NULL) {
+                                context->internal->_auth_fn_with_context(
+                                        context,
+                                        server, share,
+                                        workgroup, sizeof(fstring),
+                                        username, sizeof(fstring),
+                                        password, sizeof(fstring));
+                        } else {
+                                context->callbacks.auth_fn(
+                                        server, share,
+                                        workgroup, sizeof(fstring),
+                                        username, sizeof(fstring),
+                                        password, sizeof(fstring));
+                        }
                 }
         
+                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",
@@ -895,19 +1004,16 @@ smbc_attr_server(SMBCCTX *context,
                 }
 
                 ZERO_STRUCTP(ipc_srv);
-                ipc_srv->cli = *ipc_cli;
-                ipc_srv->cli.allocated = False;
-
-                free(ipc_cli);
+                ipc_srv->cli = ipc_cli;
 
                 if (pol) {
-                        pipe_hnd = cli_rpc_pipe_open_noauth(&ipc_srv->cli,
+                        pipe_hnd = cli_rpc_pipe_open_noauth(ipc_srv->cli,
                                                             PI_LSARPC,
                                                             &nt_status);
                         if (!pipe_hnd) {
                                 DEBUG(1, ("cli_nt_session_open fail!\n"));
                                 errno = ENOTSUP;
-                                cli_shutdown(&ipc_srv->cli);
+                                cli_shutdown(ipc_srv->cli);
                                 free(ipc_srv);
                                 return NULL;
                         }
@@ -920,14 +1026,14 @@ smbc_attr_server(SMBCCTX *context,
         
                         nt_status = rpccli_lsa_open_policy(
                                 pipe_hnd,
-                                ipc_srv->cli.mem_ctx,
+                                ipc_srv->cli->mem_ctx,
                                 True, 
                                 GENERIC_EXECUTE_ACCESS,
                                 pol);
         
                         if (!NT_STATUS_IS_OK(nt_status)) {
-                                errno = smbc_errno(context, &ipc_srv->cli);
-                                cli_shutdown(&ipc_srv->cli);
+                                errno = smbc_errno(context, ipc_srv->cli);
+                                cli_shutdown(ipc_srv->cli);
                                 return NULL;
                         }
                 }
@@ -944,7 +1050,7 @@ smbc_attr_server(SMBCCTX *context,
                         if (errno == 0) {
                                 errno = ENOMEM;
                         }
-                        cli_shutdown(&ipc_srv->cli);
+                        cli_shutdown(ipc_srv->cli);
                         free(ipc_srv);
                         return NULL;
                 }
@@ -989,6 +1095,7 @@ smbc_open_ctx(SMBCCTX *context,
        }
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -1001,9 +1108,8 @@ smbc_open_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
 
@@ -1033,21 +1139,16 @@ smbc_open_ctx(SMBCCTX *context,
                ZERO_STRUCTP(file);
 
                /*d_printf(">>>open: resolving %s\n", path);*/
-               if (!cli_resolve_path( "", &srv->cli, path, &targetcli, targetpath))
+               if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
                {
                        d_printf("Could not resolve %s\n", path);
+                       SAFE_FREE(file);
                        return NULL;
                }
                /*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 ... */
 
@@ -1106,7 +1207,7 @@ smbc_open_ctx(SMBCCTX *context,
        if (fd == -1) {
                int eno = 0;
 
-               eno = smbc_errno(context, &srv->cli);
+               eno = smbc_errno(context, srv->cli);
                file = context->opendir(context, fname);
                if (!file) errno = eno;
                return file;
@@ -1165,7 +1266,7 @@ smbc_read_ctx(SMBCCTX *context,
          * the call to cli_read() instead of file->offset fixes a problem
          * retrieving data at an offset greater than 4GB.
          */
-        off_t offset = file->offset;
+        off_t offset;
 
        if (!context || !context->internal ||
            !context->internal->_initialized) {
@@ -1184,6 +1285,8 @@ smbc_read_ctx(SMBCCTX *context,
 
        }
 
+       offset = file->offset;
+
        /* Check that the buffer exists ... */
 
        if (buf == NULL) {
@@ -1195,6 +1298,7 @@ smbc_read_ctx(SMBCCTX *context,
 
        /*d_printf(">>>read: parsing %s\n", file->fname);*/
        if (smbc_parse_path(context, file->fname,
+                            NULL, 0,
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -1206,7 +1310,7 @@ smbc_read_ctx(SMBCCTX *context,
         }
        
        /*d_printf(">>>read: resolving %s\n", path);*/
-       if (!cli_resolve_path("", &file->srv->cli, path,
+       if (!cli_resolve_path("", file->srv->cli, path,
                               &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
@@ -1214,7 +1318,7 @@ smbc_read_ctx(SMBCCTX *context,
        }
        /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
        
-       ret = cli_read(targetcli, file->cli_fd, buf, offset, count);
+       ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
 
        if (ret < 0) {
 
@@ -1247,8 +1351,8 @@ smbc_write_ctx(SMBCCTX *context,
        pstring path, targetpath;
        struct cli_state *targetcli;
 
-        offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
-
+       /* First check all pointers before dereferencing them */
+       
        if (!context || !context->internal ||
            !context->internal->_initialized) {
 
@@ -1273,8 +1377,11 @@ smbc_write_ctx(SMBCCTX *context,
 
        }
 
+        offset = file->offset; /* See "offset" comment in smbc_read_ctx() */
+
        /*d_printf(">>>write: parsing %s\n", file->fname);*/
        if (smbc_parse_path(context, file->fname,
+                            NULL, 0,
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -1286,7 +1393,7 @@ smbc_write_ctx(SMBCCTX *context,
         }
        
        /*d_printf(">>>write: resolving %s\n", path);*/
-       if (!cli_resolve_path("", &file->srv->cli, path,
+       if (!cli_resolve_path("", file->srv->cli, path,
                               &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
@@ -1295,7 +1402,7 @@ smbc_write_ctx(SMBCCTX *context,
        /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
 
 
-       ret = cli_write(targetcli, file->cli_fd, 0, buf, offset, count);
+       ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
 
        if (ret <= 0) {
 
@@ -1346,6 +1453,7 @@ smbc_close_ctx(SMBCCTX *context,
 
        /*d_printf(">>>close: parsing %s\n", file->fname);*/
        if (smbc_parse_path(context, file->fname,
+                            NULL, 0,
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -1357,7 +1465,7 @@ smbc_close_ctx(SMBCCTX *context,
         }
        
        /*d_printf(">>>close: resolving %s\n", path);*/
-       if (!cli_resolve_path("", &file->srv->cli, path,
+       if (!cli_resolve_path("", file->srv->cli, path,
                               &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
@@ -1399,14 +1507,16 @@ smbc_getatr(SMBCCTX * context,
             char *path, 
             uint16 *mode,
             SMB_OFF_T *size, 
-            time_t *c_time,
-            time_t *a_time,
-            time_t *m_time,
+            struct timespec *create_time_ts,
+            struct timespec *access_time_ts,
+            struct timespec *write_time_ts,
+            struct timespec *change_time_ts,
             SMB_INO_T *ino)
 {
        pstring fixedpath;
        pstring targetpath;
        struct cli_state *targetcli;
+       time_t write_time;
 
        if (!context || !context->internal ||
            !context->internal->_initialized) {
@@ -1427,23 +1537,19 @@ smbc_getatr(SMBCCTX * context,
        }
        DEBUG(4,("smbc_getatr: sending qpathinfo\n"));
   
-       if (!cli_resolve_path( "", &srv->cli, fixedpath, &targetcli, targetpath))
+       if (!cli_resolve_path( "", srv->cli, fixedpath, &targetcli, targetpath))
        {
                d_printf("Couldn't resolve %s\n", path);
                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,
-                           c_time, a_time, m_time, NULL, size, mode, ino)) {
+                           create_time_ts,
+                           access_time_ts,
+                           write_time_ts,
+                           change_time_ts,
+                           size, mode, ino)) {
             return True;
         }
 
@@ -1453,11 +1559,28 @@ smbc_getatr(SMBCCTX * context,
                 return False;
         }
 
-       if (cli_getatr(targetcli, targetpath, mode, size, m_time)) {
-                if (m_time != NULL) {
-                        if (a_time != NULL) *a_time = *m_time;
-                        if (c_time != NULL) *c_time = *m_time;
+       if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
+
+                struct timespec w_time_ts;
+
+                w_time_ts = convert_time_t_to_timespec(write_time);
+
+                if (write_time_ts != NULL) {
+                       *write_time_ts = w_time_ts;
+                }
+
+                if (create_time_ts != NULL) {
+                        *create_time_ts = w_time_ts;
+                }
+                
+                if (access_time_ts != NULL) {
+                        *access_time_ts = w_time_ts;
                 }
+                
+                if (change_time_ts != NULL) {
+                        *change_time_ts = w_time_ts;
+                }
+
                srv->no_pathinfo2 = True;
                return True;
        }
@@ -1479,45 +1602,15 @@ smbc_getatr(SMBCCTX * context,
  */
 static BOOL
 smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path, 
-            time_t c_time, time_t a_time, time_t m_time,
+            time_t create_time,
+            time_t access_time,
+            time_t write_time,
+            time_t change_time,
             uint16 mode)
 {
         int fd;
         int ret;
 
-        /*
-         * Get the create time of the file (if not provided); we'll need it in
-         * the set call.
-         */
-        if (! srv->no_pathinfo && c_time == 0) {
-                if (! cli_qpathinfo(&srv->cli, path,
-                                    &c_time, NULL, NULL, NULL, NULL)) {
-                        /* qpathinfo not available */
-                        srv->no_pathinfo = True;
-                } else {
-                        /*
-                         * We got a creation time.  Some OS versions don't
-                         * return a valid create time, though.  If we got an
-                         * invalid time, start with the current time instead.
-                         */
-                        if (c_time == 0 || c_time == (time_t) -1) {
-                                c_time = time(NULL);
-                        }
-
-                        /*
-                         * We got a creation time.  For sanity sake, since
-                         * there is no POSIX function to set the create time
-                         * of a file, if the existing create time is greater
-                         * than either of access time or modification time,
-                         * set create time to the smallest of those.  This
-                         * ensure that the create time of a file is never
-                         * greater than its last access or modification time.
-                         */
-                        if (c_time > a_time) c_time = a_time;
-                        if (c_time > m_time) c_time = m_time;
-                }
-        }
-
         /*
          * First, try setpathinfo (if qpathinfo succeeded), for it is the
          * modern function for "new code" to be using, and it works given a
@@ -1525,7 +1618,12 @@ smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
          * attributes manipulated.
          */
         if (srv->no_pathinfo ||
-            ! cli_setpathinfo(&srv->cli, path, c_time, a_time, m_time, mode)) {
+            ! cli_setpathinfo(srv->cli, path,
+                              create_time,
+                              access_time,
+                              write_time,
+                              change_time,
+                              mode)) {
 
                 /*
                  * setpathinfo is not supported; go to plan B. 
@@ -1541,48 +1639,20 @@ smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
                 srv->no_pathinfo = True;
 
                 /* Open the file */
-                if ((fd = cli_open(&srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
+                if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
 
-                        errno = smbc_errno(context, &srv->cli);
+                        errno = smbc_errno(context, srv->cli);
                         return -1;
                 }
 
-                /*
-                 * Get the creat time of the file (if it wasn't provided).
-                 * We'll need it in the set call
-                 */
-                if (c_time == 0) {
-                        ret = cli_getattrE(&srv->cli, fd,
-                                           NULL, NULL,
-                                           &c_time, NULL, NULL);
-                } else {
-                        ret = True;
-                }
-                    
-                /* If we got create time, set times */
-                if (ret) {
-                        /* Some OS versions don't support create time */
-                        if (c_time == 0 || c_time == -1) {
-                                c_time = time(NULL);
-                        }
+                /* Set the new attributes */
+                ret = cli_setattrE(srv->cli, fd,
+                                   change_time,
+                                   access_time,
+                                   write_time);
 
-                        /*
-                         * For sanity sake, since there is no POSIX function
-                         * to set the create time of a file, if the existing
-                         * create time is greater than either of access time
-                         * or modification time, set create time to the
-                         * smallest of those.  This ensure that the create
-                         * time of a file is never greater than its last
-                         * access or modification time.
-                         */
-                        if (c_time > a_time) c_time = a_time;
-                        if (c_time > m_time) c_time = m_time;
-                        
-                        /* Set the new attributes */
-                        ret = cli_setattrE(&srv->cli, fd,
-                                           c_time, a_time, m_time);
-                        cli_close(&srv->cli, fd);
-                }
+                /* Close the file */
+                cli_close(srv->cli, fd);
 
                 /*
                  * Unfortunately, setattrE() doesn't have a provision for
@@ -1591,11 +1661,11 @@ smbc_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
                  * seems to work on win98.
                  */
                 if (ret && mode != (uint16) -1) {
-                        ret = cli_setatr(&srv->cli, path, mode, 0);
+                        ret = cli_setatr(srv->cli, path, mode, 0);
                 }
 
                 if (! ret) {
-                        errno = smbc_errno(context, &srv->cli);
+                        errno = smbc_errno(context, srv->cli);
                         return False;
                 }
         }
@@ -1632,6 +1702,7 @@ smbc_unlink_ctx(SMBCCTX *context,
        }
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -1644,9 +1715,8 @@ smbc_unlink_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
 
@@ -1655,7 +1725,7 @@ smbc_unlink_ctx(SMBCCTX *context,
        }
 
        /*d_printf(">>>unlink: resolving %s\n", path);*/
-       if (!cli_resolve_path( "", &srv->cli, path, &targetcli, targetpath))
+       if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
                return -1;
@@ -1671,11 +1741,17 @@ smbc_unlink_ctx(SMBCCTX *context,
                        int saverr = errno;
                        SMB_OFF_T size = 0;
                        uint16 mode = 0;
-                       time_t m_time = 0, a_time = 0, c_time = 0;
+                       struct timespec write_time_ts;
+                        struct timespec access_time_ts;
+                        struct timespec change_time_ts;
                        SMB_INO_T ino = 0;
 
                        if (!smbc_getatr(context, srv, path, &mode, &size,
-                                        &c_time, &a_time, &m_time, &ino)) {
+                                        NULL,
+                                         &access_time_ts,
+                                         &write_time_ts,
+                                         &change_time_ts,
+                                         &ino)) {
 
                                /* Hmmm, bad error ... What? */
 
@@ -1748,6 +1824,7 @@ smbc_rename_ctx(SMBCCTX *ocontext,
        DEBUG(4, ("smbc_rename(%s,%s)\n", oname, nname));
 
        smbc_parse_path(ocontext, oname,
+                        workgroup, sizeof(workgroup),
                         server1, sizeof(server1),
                         share1, sizeof(share1),
                         path1, sizeof(path1),
@@ -1758,6 +1835,7 @@ smbc_rename_ctx(SMBCCTX *ocontext,
        if (user1[0] == (char)0) fstrcpy(user1, ocontext->user);
 
        smbc_parse_path(ncontext, nname,
+                        NULL, 0,
                         server2, sizeof(server2),
                         share2, sizeof(share2),
                         path2, sizeof(path2),
@@ -1777,10 +1855,8 @@ smbc_rename_ctx(SMBCCTX *ocontext,
 
        }
 
-       fstrcpy(workgroup, ocontext->workgroup);
-
-       srv = smbc_server(ocontext, server1, share1, workgroup,
-                          user1, password1);
+       srv = smbc_server(ocontext, True,
+                          server1, share1, workgroup, user1, password1);
        if (!srv) {
 
                return -1;
@@ -1788,14 +1864,14 @@ smbc_rename_ctx(SMBCCTX *ocontext,
        }
 
        /*d_printf(">>>rename: resolving %s\n", path1);*/
-       if (!cli_resolve_path( "", &srv->cli, path1, &targetcli1, targetpath1))
+       if (!cli_resolve_path( "", srv->cli, path1, &targetcli1, targetpath1))
        {
                d_printf("Could not resolve %s\n", path1);
                return -1;
        }
        /*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
        /*d_printf(">>>rename: resolving %s\n", path2);*/
-       if (!cli_resolve_path( "", &srv->cli, path2, &targetcli2, targetpath2))
+       if (!cli_resolve_path( "", srv->cli, path2, &targetcli2, targetpath2))
        {
                d_printf("Could not resolve %s\n", path2);
                return -1;
@@ -1877,6 +1953,7 @@ smbc_lseek_ctx(SMBCCTX *context,
        case SEEK_END:
                /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
                if (smbc_parse_path(context, file->fname,
+                                    NULL, 0,
                                     server, sizeof(server),
                                     share, sizeof(share),
                                     path, sizeof(path),
@@ -1889,7 +1966,7 @@ smbc_lseek_ctx(SMBCCTX *context,
                        }
                
                /*d_printf(">>>lseek: resolving %s\n", path);*/
-               if (!cli_resolve_path("", &file->srv->cli, path,
+               if (!cli_resolve_path("", file->srv->cli, path,
                                       &targetcli, targetpath))
                {
                        d_printf("Could not resolve %s\n", path);
@@ -2010,9 +2087,9 @@ smbc_stat_ctx(SMBCCTX *context,
         fstring password;
         fstring workgroup;
        pstring path;
-       time_t m_time = 0;
-        time_t a_time = 0;
-        time_t c_time = 0;
+       struct timespec write_time_ts;
+        struct timespec access_time_ts;
+        struct timespec change_time_ts;
        SMB_OFF_T size = 0;
        uint16 mode = 0;
        SMB_INO_T ino = 0;
@@ -2035,6 +2112,7 @@ smbc_stat_ctx(SMBCCTX *context,
        DEBUG(4, ("smbc_stat(%s)\n", fname));
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -2047,18 +2125,21 @@ smbc_stat_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
                return -1;  /* errno set by smbc_server */
        }
 
        if (!smbc_getatr(context, srv, path, &mode, &size, 
-                        &c_time, &a_time, &m_time, &ino)) {
+                        NULL,
+                         &access_time_ts,
+                         &write_time_ts,
+                         &change_time_ts,
+                         &ino)) {
 
-               errno = smbc_errno(context, &srv->cli);
+               errno = smbc_errno(context, srv->cli);
                return -1;
                
        }
@@ -2067,9 +2148,9 @@ smbc_stat_ctx(SMBCCTX *context,
 
        smbc_setup_stat(context, st, path, size, mode);
 
-       st->st_atime = a_time;
-       st->st_ctime = c_time;
-       st->st_mtime = m_time;
+       set_atimespec(st, access_time_ts);
+       set_ctimespec(st, change_time_ts);
+       set_mtimespec(st, write_time_ts);
        st->st_dev   = srv->dev;
 
        return 0;
@@ -2085,9 +2166,9 @@ smbc_fstat_ctx(SMBCCTX *context,
                SMBCFILE *file,
                struct stat *st)
 {
-       time_t c_time;
-        time_t a_time;
-        time_t m_time;
+       struct timespec change_time_ts;
+        struct timespec access_time_ts;
+        struct timespec write_time_ts;
        SMB_OFF_T size;
        uint16 mode;
        fstring server;
@@ -2122,6 +2203,7 @@ smbc_fstat_ctx(SMBCCTX *context,
 
        /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
        if (smbc_parse_path(context, file->fname,
+                            NULL, 0,
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -2133,7 +2215,7 @@ smbc_fstat_ctx(SMBCCTX *context,
         }
        
        /*d_printf(">>>fstat: resolving %s\n", path);*/
-       if (!cli_resolve_path("", &file->srv->cli, path,
+       if (!cli_resolve_path("", file->srv->cli, path,
                               &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
@@ -2142,22 +2224,33 @@ smbc_fstat_ctx(SMBCCTX *context,
        /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
 
        if (!cli_qfileinfo(targetcli, file->cli_fd, &mode, &size,
-                           &c_time, &a_time, &m_time, NULL, &ino)) {
-           if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
-                              &c_time, &a_time, &m_time)) {
+                           NULL,
+                           &access_time_ts,
+                           &write_time_ts,
+                           &change_time_ts,
+                           &ino)) {
 
-               errno = EINVAL;
-               return -1;
-           }
+               time_t change_time, access_time, write_time;
+
+               if (!cli_getattrE(targetcli, file->cli_fd, &mode, &size,
+                               &change_time, &access_time, &write_time)) {
+
+                       errno = EINVAL;
+                       return -1;
+               }
+
+               change_time_ts = convert_time_t_to_timespec(change_time);
+               access_time_ts = convert_time_t_to_timespec(access_time);
+               write_time_ts = convert_time_t_to_timespec(write_time);
        }
 
        st->st_ino = ino;
 
        smbc_setup_stat(context, st, file->fname, size, mode);
 
-       st->st_atime = a_time;
-       st->st_ctime = c_time;
-       st->st_mtime = m_time;
+       set_atimespec(st, access_time_ts);
+       set_ctimespec(st, change_time_ts);
+       set_mtimespec(st, write_time_ts);
        st->st_dev = file->srv->dev;
 
        return 0;
@@ -2206,7 +2299,7 @@ add_dirent(SMBCFILE *dir,
 
        size = sizeof(struct smbc_dirent) + name_length + comment_len + 2;
     
-       dirent = SMB_MALLOC(size);
+       dirent = (struct smbc_dirent *)SMB_MALLOC(size);
 
        if (!dirent) {
 
@@ -2255,8 +2348,12 @@ add_dirent(SMBCFILE *dir,
        dirent->commentlen = comment_len;
        dirent->dirlen = size;
   
+        /*
+         * dirent->namelen + 1 includes the null (no null termination needed)
+         * Ditto for dirent->commentlen.
+         * The space for the two null bytes was allocated.
+         */
        strncpy(dirent->name, (name?name:""), dirent->namelen + 1);
-
        dirent->comment = (char *)(&dirent->name + dirent->namelen + 1);
        strncpy(dirent->comment, (comment?comment:""), dirent->commentlen + 1);
        
@@ -2394,16 +2491,15 @@ net_share_enum_rpc(struct cli_state *cli,
                    void *state)
 {
         int i;
-       WERROR result;
-       ENUM_HND enum_hnd;
+       NTSTATUS result;
+       uint32 enum_hnd;
         uint32 info_level = 1;
        uint32 preferred_len = 0xffffffff;
-        uint32 type;
-       SRV_SHARE_INFO_CTR ctr;
-       fstring name = "";
-        fstring comment = "";
+       struct srvsvc_NetShareCtr1 ctr1;
+       union srvsvc_NetShareCtr ctr;
         void *mem_ctx;
        struct rpc_pipe_client *pipe_hnd;
+       uint32 numentries;
         NTSTATUS nt_status;
 
         /* Open the server service pipe */
@@ -2421,37 +2517,28 @@ net_share_enum_rpc(struct cli_state *cli,
                 return -1; 
         }
 
+       ZERO_STRUCT(ctr1);
+       ctr.ctr1 = &ctr1;
+
         /* Issue the NetShareEnum RPC call and retrieve the response */
-       init_enum_hnd(&enum_hnd, 0);
-       result = rpccli_srvsvc_net_share_enum(pipe_hnd,
-                                              mem_ctx,
-                                              info_level,
-                                              &ctr,
-                                              preferred_len,
-                                              &enum_hnd);
+       enum_hnd = 0;
+       result = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
+                                           &info_level, &ctr, preferred_len,
+                                           &numentries, &enum_hnd);
 
         /* Was it successful? */
-       if (!W_ERROR_IS_OK(result) || ctr.num_entries == 0) {
+       if (!NT_STATUS_IS_OK(result) || numentries == 0) {
                 /*  Nope.  Go clean up. */
                goto done;
         }
 
         /* For each returned entry... */
-        for (i = 0; i < ctr.num_entries; i++) {
-
-                /* pull out the share name */
-                rpcstr_pull_unistr2_fstring(
-                        name, &ctr.share.info1[i].info_1_str.uni_netname);
-
-                /* pull out the share's comment */
-                rpcstr_pull_unistr2_fstring(
-                        comment, &ctr.share.info1[i].info_1_str.uni_remark);
-
-                /* Get the type value */
-                type = ctr.share.info1[i].info_1.type;
+        for (i = 0; i < numentries; i++) {
 
                 /* Add this share to the list */
-                (*fn)(name, type, comment, state);
+                (*fn)(ctr.ctr1->array[i].name, 
+                                         ctr.ctr1->array[i].type, 
+                                         ctr.ctr1->array[i].comment, state);
         }
 
 done:
@@ -2459,10 +2546,10 @@ done:
         cli_rpc_pipe_close(pipe_hnd);
 
         /* Free all memory which was allocated for this request */
-        talloc_free(mem_ctx);
+        TALLOC_FREE(mem_ctx);
 
         /* Tell 'em if it worked */
-        return W_ERROR_IS_OK(result) ? 0 : -1;
+        return NT_STATUS_IS_OK(result) ? 0 : -1;
 }
 
 
@@ -2471,6 +2558,7 @@ static SMBCFILE *
 smbc_opendir_ctx(SMBCCTX *context,
                  const char *fname)
 {
+        int saved_errno;
        fstring server, share, user, password, options;
        pstring workgroup;
        pstring path;
@@ -2478,6 +2566,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 ||
@@ -2495,6 +2584,7 @@ smbc_opendir_ctx(SMBCCTX *context,
        }
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -2519,8 +2609,6 @@ smbc_opendir_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       pstrcpy(workgroup, context->workgroup);
-
        dir = SMB_MALLOC_P(SMBCFILE);
 
        if (!dir) {
@@ -2583,6 +2671,10 @@ smbc_opendir_ctx(SMBCCTX *context,
 
                         if (!find_master_ip(workgroup, &server_addr.ip)) {
 
+                               if (dir) {
+                                       SAFE_FREE(dir->fname);
+                                       SAFE_FREE(dir);
+                               }
                                 errno = ENOENT;
                                 return NULL;
                         }
@@ -2616,8 +2708,8 @@ smbc_opendir_ctx(SMBCCTX *context,
                          * workgroups/domains that it knows about.
                          */
                 
-                        srv = smbc_server(context, server,
-                                          "IPC$", workgroup, user, password);
+                        srv = smbc_server(context, True, server, "IPC$",
+                                          workgroup, user, password);
                         if (!srv) {
                                 continue;
                         }
@@ -2627,7 +2719,7 @@ smbc_opendir_ctx(SMBCCTX *context,
 
                         /* Now, list the stuff ... */
                         
-                        if (!cli_NetServerEnum(&srv->cli,
+                        if (!cli_NetServerEnum(srv->cli,
                                                workgroup,
                                                SV_TYPE_DOMAIN_ENUM,
                                                list_unique_wg_fn,
@@ -2664,8 +2756,12 @@ smbc_opendir_ctx(SMBCCTX *context,
                          * to see if <server> is an IP address first.
                          */
 
-                        /* See if we have an existing server */
-                        srv = smbc_server(context, server, "IPC$",
+                        /*
+                         * See if we have an existing server.  Do not
+                         * establish a connection if one does not already
+                         * exist.
+                         */
+                        srv = smbc_server(context, False, server, "IPC$",
                                           workgroup, user, password);
 
                         /*
@@ -2690,6 +2786,10 @@ smbc_opendir_ctx(SMBCCTX *context,
                                         DEBUG(0, ("Could not get name of "
                                                   "local/domain master browser "
                                                   "for server %s\n", server));
+                                       if (dir) {
+                                               SAFE_FREE(dir->fname);
+                                               SAFE_FREE(dir);
+                                       }
                                        errno = EPERM;
                                        return NULL;
 
@@ -2699,7 +2799,8 @@ smbc_opendir_ctx(SMBCCTX *context,
                                  * Get a connection to IPC$ on the server if
                                  * we do not already have one
                                  */
-                               srv = smbc_server(context, buserver, "IPC$",
+                               srv = smbc_server(context, True,
+                                                  buserver, "IPC$",
                                                   workgroup, user, password);
                                if (!srv) {
                                        DEBUG(0, ("got no contact to IPC$\n"));
@@ -2714,7 +2815,7 @@ smbc_opendir_ctx(SMBCCTX *context,
                                dir->srv = srv;
 
                                /* Now, list the servers ... */
-                               if (!cli_NetServerEnum(&srv->cli, server,
+                               if (!cli_NetServerEnum(srv->cli, server,
                                                        0x0000FFFE, list_fn,
                                                       (void *)dir)) {
 
@@ -2729,8 +2830,9 @@ smbc_opendir_ctx(SMBCCTX *context,
                                 
                                 /* If we hadn't found the server, get one now */
                                 if (!srv) {
-                                        srv = smbc_server(context, server,
-                                                          "IPC$", workgroup,
+                                        srv = smbc_server(context, True,
+                                                          server, "IPC$",
+                                                          workgroup,
                                                           user, password);
                                 }
 
@@ -2749,15 +2851,15 @@ smbc_opendir_ctx(SMBCCTX *context,
                                 /* List the shares ... */
 
                                 if (net_share_enum_rpc(
-                                            &srv->cli,
+                                            srv->cli,
                                             list_fn,
                                             (void *) dir) < 0 &&
                                     cli_RNetShareEnum(
-                                            &srv->cli,
+                                            srv->cli,
                                             list_fn, 
                                             (void *)dir) < 0) {
                                                 
-                                        errno = cli_errno(&srv->cli);
+                                        errno = cli_errno(srv->cli);
                                         if (dir) {
                                                 SAFE_FREE(dir->fname);
                                                 SAFE_FREE(dir);
@@ -2787,7 +2889,7 @@ smbc_opendir_ctx(SMBCCTX *context,
                        /* We connect to the server and list the directory */
                        dir->dir_type = SMBC_FILE_SHARE;
 
-                       srv = smbc_server(context, server, share,
+                       srv = smbc_server(context, True, server, share,
                                           workgroup, user, password);
 
                        if (!srv) {
@@ -2807,10 +2909,14 @@ smbc_opendir_ctx(SMBCCTX *context,
                         p = path + strlen(path);
                        pstrcat(path, "\\*");
 
-                       if (!cli_resolve_path("", &srv->cli, path,
+                       if (!cli_resolve_path("", srv->cli, path,
                                               &targetcli, targetpath))
                        {
                                d_printf("Could not resolve %s\n", path);
+                               if (dir) {
+                                       SAFE_FREE(dir->fname);
+                                       SAFE_FREE(dir);
+                               }
                                return NULL;
                        }
                        
@@ -2822,9 +2928,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
@@ -2835,17 +2941,39 @@ smbc_opendir_ctx(SMBCCTX *context,
 
                                     if (smbc_getatr(context, srv, path,
                                                     &mode, NULL,
-                                                    NULL, NULL, NULL,
+                                                    NULL, NULL, NULL, NULL,
                                                     NULL) &&
                                         ! 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;
                        }
                }
 
@@ -2913,14 +3041,8 @@ smbc_readdir_internal(SMBCCTX * context,
                 dest->comment = dest->name + dest->namelen + 1;
 
                 /* Copy the comment */
-                strncpy(dest->comment, src->comment, max_namebuf_len);
-
-                /* Ensure the comment is null terminated */
-                if (max_namebuf_len > src->commentlen) {
-                        dest->comment[src->commentlen] = '\0';
-                } else {
-                        dest->comment[max_namebuf_len - 1] = '\0';
-                }
+                strncpy(dest->comment, src->comment, max_namebuf_len - 1);
+                dest->comment[max_namebuf_len - 1] = '\0';
 
                 /* Save other fields */
                 dest->smbc_type = src->smbc_type;
@@ -3135,6 +3257,7 @@ smbc_mkdir_ctx(SMBCCTX *context,
        DEBUG(4, ("smbc_mkdir(%s)\n", fname));
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -3147,9 +3270,8 @@ smbc_mkdir_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
 
@@ -3158,7 +3280,7 @@ smbc_mkdir_ctx(SMBCCTX *context,
        }
 
        /*d_printf(">>>mkdir: resolving %s\n", path);*/
-       if (!cli_resolve_path( "", &srv->cli, path, &targetcli, targetpath))
+       if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
                return -1;
@@ -3231,6 +3353,7 @@ smbc_rmdir_ctx(SMBCCTX *context,
        DEBUG(4, ("smbc_rmdir(%s)\n", fname));
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -3244,9 +3367,8 @@ smbc_rmdir_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
 
@@ -3255,7 +3377,7 @@ smbc_rmdir_ctx(SMBCCTX *context,
        }
 
        /*d_printf(">>>rmdir: resolving %s\n", path);*/
-       if (!cli_resolve_path( "", &srv->cli, path, &targetcli, targetpath))
+       if (!cli_resolve_path( "", srv->cli, path, &targetcli, targetpath))
        {
                d_printf("Could not resolve %s\n", path);
                return -1;
@@ -3484,6 +3606,7 @@ smbc_chmod_ctx(SMBCCTX *context,
        DEBUG(4, ("smbc_chmod(%s, 0%3o)\n", fname, newmode));
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -3496,9 +3619,8 @@ smbc_chmod_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
                return -1;  /* errno set by smbc_server */
@@ -3511,8 +3633,8 @@ smbc_chmod_ctx(SMBCCTX *context,
        if ((newmode & S_IXGRP) && lp_map_system(-1)) mode |= aSYSTEM;
        if ((newmode & S_IXOTH) && lp_map_hidden(-1)) mode |= aHIDDEN;
 
-       if (!cli_setatr(&srv->cli, path, mode, 0)) {
-               errno = smbc_errno(context, &srv->cli);
+       if (!cli_setatr(srv->cli, path, mode, 0)) {
+               errno = smbc_errno(context, srv->cli);
                return -1;
        }
        
@@ -3531,8 +3653,8 @@ smbc_utimes_ctx(SMBCCTX *context,
         fstring password;
         fstring workgroup;
        pstring path;
-        time_t a_time;
-        time_t m_time;
+        time_t access_time;
+        time_t write_time;
 
        if (!context || !context->internal ||
            !context->internal->_initialized) {
@@ -3550,10 +3672,10 @@ smbc_utimes_ctx(SMBCCTX *context,
        }
   
         if (tbuf == NULL) {
-                a_time = m_time = time(NULL);
+                access_time = write_time = time(NULL);
         } else {
-                a_time = tbuf[0].tv_sec;
-                m_time = tbuf[1].tv_sec;
+                access_time = tbuf[0].tv_sec;
+                write_time = tbuf[1].tv_sec;
         }
 
         if (DEBUGLVL(4)) 
@@ -3562,13 +3684,13 @@ smbc_utimes_ctx(SMBCCTX *context,
                 char atimebuf[32];
                 char mtimebuf[32];
 
-                strncpy(atimebuf, ctime(&a_time), sizeof(atimebuf));
+                strncpy(atimebuf, ctime(&access_time), sizeof(atimebuf) - 1);
                 atimebuf[sizeof(atimebuf) - 1] = '\0';
                 if ((p = strchr(atimebuf, '\n')) != NULL) {
                         *p = '\0';
                 }
 
-                strncpy(mtimebuf, ctime(&m_time), sizeof(mtimebuf));
+                strncpy(mtimebuf, ctime(&write_time), sizeof(mtimebuf) - 1);
                 mtimebuf[sizeof(mtimebuf) - 1] = '\0';
                 if ((p = strchr(mtimebuf, '\n')) != NULL) {
                         *p = '\0';
@@ -3579,6 +3701,7 @@ smbc_utimes_ctx(SMBCCTX *context,
         }
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -3591,15 +3714,15 @@ smbc_utimes_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
        if (!srv) {
                return -1;      /* errno set by smbc_server */
        }
 
-        if (!smbc_setatr(context, srv, path, 0, a_time, m_time, 0)) {
+        if (!smbc_setatr(context, srv, path,
+                         0, access_time, write_time, 0, 0)) {
                 return -1;      /* errno set by smbc_setatr */
         }
 
@@ -3628,8 +3751,8 @@ ace_compare(SEC_ACE *ace1,
        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) 
                return ace1->size - ace2->size;
@@ -3644,14 +3767,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 {
@@ -3670,7 +3793,7 @@ convert_sid_to_string(struct cli_state *ipc_cli,
 {
        char **domains = NULL;
        char **names = NULL;
-       uint32 *types = NULL;
+       enum lsa_SidType *types = NULL;
        struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
        sid_to_string(str, sid);
 
@@ -3706,7 +3829,7 @@ convert_string_to_sid(struct cli_state *ipc_cli,
                       DOM_SID *sid,
                       const char *str)
 {
-       uint32 *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);
@@ -3725,7 +3848,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, &sids, 
+                                                 pol, 1, &str, NULL, 1, &sids, 
                                                  &types))) {
                result = False;
                goto done;
@@ -3856,7 +3979,7 @@ parse_ace(struct cli_state *ipc_cli,
        }
 
  done:
-       mask.mask = amask;
+       mask = amask;
        init_sec_ace(ace, &sid, atype, mask, aflags);
        return True;
 }
@@ -3875,8 +3998,10 @@ add_ace(SEC_ACL **the_acl,
                return True;
        }
 
-       aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces);
-       memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
+       if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
+               return False;
+       }
+       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);
@@ -3896,7 +4021,7 @@ sec_desc_parse(TALLOC_CTX *ctx,
 {
        const char *p = str;
        fstring tok;
-       SEC_DESC *ret;
+       SEC_DESC *ret = NULL;
        size_t sd_size;
        DOM_SID *grp_sid=NULL;
         DOM_SID *owner_sid=NULL;
@@ -3911,49 +4036,65 @@ sec_desc_parse(TALLOC_CTX *ctx,
                }
 
                if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
+                       if (owner_sid) {
+                               DEBUG(5, ("OWNER specified more than once!\n"));
+                               goto done;
+                       }
                        owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
                        if (!owner_sid ||
                            !convert_string_to_sid(ipc_cli, pol,
                                                    numeric,
                                                    owner_sid, tok+6)) {
                                DEBUG(5, ("Failed to parse owner sid\n"));
-                               return NULL;
+                               goto done;
                        }
                        continue;
                }
 
                if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
+                       if (owner_sid) {
+                               DEBUG(5, ("OWNER specified more than once!\n"));
+                               goto done;
+                       }
                        owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
                        if (!owner_sid ||
                            !convert_string_to_sid(ipc_cli, pol,
                                                    False,
                                                    owner_sid, tok+7)) {
                                DEBUG(5, ("Failed to parse owner sid\n"));
-                               return NULL;
+                               goto done;
                        }
                        continue;
                }
 
                if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
+                       if (grp_sid) {
+                               DEBUG(5, ("GROUP specified more than once!\n"));
+                               goto done;
+                       }
                        grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
                        if (!grp_sid ||
                            !convert_string_to_sid(ipc_cli, pol,
                                                    numeric,
                                                    grp_sid, tok+6)) {
                                DEBUG(5, ("Failed to parse group sid\n"));
-                               return NULL;
+                               goto done;
                        }
                        continue;
                }
 
                if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
+                       if (grp_sid) {
+                               DEBUG(5, ("GROUP specified more than once!\n"));
+                               goto done;
+                       }
                        grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
                        if (!grp_sid ||
                            !convert_string_to_sid(ipc_cli, pol,
                                                    False,
                                                    grp_sid, tok+6)) {
                                DEBUG(5, ("Failed to parse group sid\n"));
-                               return NULL;
+                               goto done;
                        }
                        continue;
                }
@@ -3962,11 +4103,11 @@ sec_desc_parse(TALLOC_CTX *ctx,
                        SEC_ACE ace;
                        if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
                                DEBUG(5, ("Failed to parse ACL %s\n", tok));
-                               return NULL;
+                               goto done;
                        }
                        if(!add_ace(&dacl, &ace, ctx)) {
                                DEBUG(5, ("Failed to add ACL %s\n", tok));
-                               return NULL;
+                               goto done;
                        }
                        continue;
                }
@@ -3975,22 +4116,23 @@ sec_desc_parse(TALLOC_CTX *ctx,
                        SEC_ACE ace;
                        if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
                                DEBUG(5, ("Failed to parse ACL %s\n", tok));
-                               return NULL;
+                               goto done;
                        }
                        if(!add_ace(&dacl, &ace, ctx)) {
                                DEBUG(5, ("Failed to add ACL %s\n", tok));
-                               return NULL;
+                               goto done;
                        }
                        continue;
                }
 
                DEBUG(5, ("Failed to parse security descriptor\n"));
-               return NULL;
+               goto done;
        }
 
        ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE, 
                            owner_sid, grp_sid, NULL, dacl, &sd_size);
 
+  done:
        SAFE_FREE(grp_sid);
        SAFE_FREE(owner_sid);
 
@@ -4005,7 +4147,10 @@ dos_attr_query(SMBCCTX *context,
                const char *filename,
                SMBCSRV *srv)
 {
-        time_t m_time = 0, a_time = 0, c_time = 0;
+        struct timespec create_time_ts;
+        struct timespec write_time_ts;
+        struct timespec access_time_ts;
+        struct timespec change_time_ts;
         SMB_OFF_T size = 0;
         uint16 mode = 0;
        SMB_INO_T inode = 0;
@@ -4020,9 +4165,13 @@ dos_attr_query(SMBCCTX *context,
         /* Obtain the DOS attributes */
         if (!smbc_getatr(context, srv, CONST_DISCARD(char *, filename),
                          &mode, &size, 
-                         &c_time, &a_time, &m_time, &inode)) {
+                         &create_time_ts,
+                         &access_time_ts,
+                         &write_time_ts,
+                         &change_time_ts, 
+                         &inode)) {
         
-                errno = smbc_errno(context, &srv->cli);
+                errno = smbc_errno(context, srv->cli);
                 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
                 return NULL;
         
@@ -4030,9 +4179,10 @@ dos_attr_query(SMBCCTX *context,
                 
         ret->mode = mode;
         ret->size = size;
-        ret->a_time = a_time;
-        ret->c_time = c_time;
-        ret->m_time = m_time;
+        ret->create_time = convert_timespec_to_time_t(create_time_ts);
+        ret->access_time = convert_timespec_to_time_t(access_time_ts);
+        ret->write_time = convert_timespec_to_time_t(write_time_ts);
+        ret->change_time = convert_timespec_to_time_t(change_time_ts);
         ret->inode = inode;
 
         return ret;
@@ -4046,8 +4196,40 @@ dos_attr_parse(SMBCCTX *context,
                SMBCSRV *srv,
                char *str)
 {
-       const char *p = str;
+        int n;
+        const char *p = str;
        fstring tok;
+        struct {
+                const char * create_time_attr;
+                const char * access_time_attr;
+                const char * write_time_attr;
+                const char * change_time_attr;
+        } attr_strings;
+
+        /* Determine whether to use old-style or new-style attribute names */
+        if (context->internal->_full_time_names) {
+                /* new-style names */
+                attr_strings.create_time_attr = "CREATE_TIME";
+                attr_strings.access_time_attr = "ACCESS_TIME";
+                attr_strings.write_time_attr = "WRITE_TIME";
+                attr_strings.change_time_attr = "CHANGE_TIME";
+        } else {
+                /* old-style names */
+                attr_strings.create_time_attr = NULL;
+                attr_strings.access_time_attr = "A_TIME";
+                attr_strings.write_time_attr = "M_TIME";
+                attr_strings.change_time_attr = "C_TIME";
+        }
+
+        /* if this is to set the entire ACL... */
+        if (*str == '*') {
+                /* ... then increment past the first colon if there is one */
+                if ((p = strchr(str, ':')) != NULL) {
+                        ++p;
+                } else {
+                        p = str;
+                }
+        }
 
        while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
 
@@ -4061,21 +4243,34 @@ dos_attr_parse(SMBCCTX *context,
                        continue;
                }
 
-               if (StrnCaseCmp(tok, "A_TIME:", 7) == 0) {
-                        dad->a_time = (time_t)strtol(tok+7, NULL, 10);
+                n = strlen(attr_strings.access_time_attr);
+                if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
+                        dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
                        continue;
                }
 
-               if (StrnCaseCmp(tok, "C_TIME:", 7) == 0) {
-                        dad->c_time = (time_t)strtol(tok+7, NULL, 10);
+                n = strlen(attr_strings.change_time_attr);
+                if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
+                        dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
                        continue;
                }
 
-               if (StrnCaseCmp(tok, "M_TIME:", 7) == 0) {
-                        dad->m_time = (time_t)strtol(tok+7, NULL, 10);
+                n = strlen(attr_strings.write_time_attr);
+                if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
+                        dad->write_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) {
                         dad->inode = (SMB_INO_T)atof(tok+6);
                        continue;
@@ -4113,9 +4308,10 @@ cacl_get(SMBCCTX *context,
         BOOL exclude_nt_acl = False;
         BOOL exclude_dos_mode = False;
         BOOL exclude_dos_size = False;
-        BOOL exclude_dos_ctime = False;
-        BOOL exclude_dos_atime = False;
-        BOOL exclude_dos_mtime = False;
+        BOOL exclude_dos_create_time = False;
+        BOOL exclude_dos_access_time = False;
+        BOOL exclude_dos_write_time = False;
+        BOOL exclude_dos_change_time = False;
         BOOL exclude_dos_inode = False;
         BOOL numeric = True;
         BOOL determine_size = (bufsize == 0);
@@ -4126,11 +4322,55 @@ cacl_get(SMBCCTX *context,
         char *name;
         char *pExclude;
         char *p;
-       time_t m_time = 0, a_time = 0, c_time = 0;
+        struct timespec create_time_ts;
+       struct timespec write_time_ts;
+        struct timespec access_time_ts;
+        struct timespec change_time_ts;
+       time_t create_time = (time_t)0;
+       time_t write_time = (time_t)0;
+        time_t access_time = (time_t)0;
+        time_t change_time = (time_t)0;
        SMB_OFF_T size = 0;
        uint16 mode = 0;
        SMB_INO_T ino = 0;
-        struct cli_state *cli = &srv->cli;
+        struct cli_state *cli = srv->cli;
+        struct {
+                const char * create_time_attr;
+                const char * access_time_attr;
+                const char * write_time_attr;
+                const char * change_time_attr;
+        } attr_strings;
+        struct {
+                const char * create_time_attr;
+                const char * access_time_attr;
+                const char * write_time_attr;
+                const char * change_time_attr;
+        } excl_attr_strings;
+
+        /* Determine whether to use old-style or new-style attribute names */
+        if (context->internal->_full_time_names) {
+                /* new-style names */
+                attr_strings.create_time_attr = "CREATE_TIME";
+                attr_strings.access_time_attr = "ACCESS_TIME";
+                attr_strings.write_time_attr = "WRITE_TIME";
+                attr_strings.change_time_attr = "CHANGE_TIME";
+
+                excl_attr_strings.create_time_attr = "CREATE_TIME";
+                excl_attr_strings.access_time_attr = "ACCESS_TIME";
+                excl_attr_strings.write_time_attr = "WRITE_TIME";
+                excl_attr_strings.change_time_attr = "CHANGE_TIME";
+        } else {
+                /* old-style names */
+                attr_strings.create_time_attr = NULL;
+                attr_strings.access_time_attr = "A_TIME";
+                attr_strings.write_time_attr = "M_TIME";
+                attr_strings.change_time_attr = "C_TIME";
+
+                excl_attr_strings.create_time_attr = NULL;
+                excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
+                excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
+                excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
+        }
 
         /* Copy name so we can strip off exclusions (if any are specified) */
         strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
@@ -4188,14 +4428,22 @@ cacl_get(SMBCCTX *context,
                 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
                     exclude_dos_size = True;
                 }
-                else if (StrCaseCmp(pExclude, "dos_attr.c_time") == 0) {
-                    exclude_dos_ctime = True;
+                else if (excl_attr_strings.create_time_attr != NULL &&
+                         StrCaseCmp(pExclude,
+                                    excl_attr_strings.change_time_attr) == 0) {
+                    exclude_dos_create_time = True;
                 }
-                else if (StrCaseCmp(pExclude, "dos_attr.a_time") == 0) {
-                    exclude_dos_atime = True;
+                else if (StrCaseCmp(pExclude,
+                                    excl_attr_strings.access_time_attr) == 0) {
+                    exclude_dos_access_time = True;
                 }
-                else if (StrCaseCmp(pExclude, "dos_attr.m_time") == 0) {
-                    exclude_dos_mtime = True;
+                else if (StrCaseCmp(pExclude,
+                                    excl_attr_strings.write_time_attr) == 0) {
+                    exclude_dos_write_time = True;
+                }
+                else if (StrCaseCmp(pExclude,
+                                    excl_attr_strings.change_time_attr) == 0) {
+                    exclude_dos_change_time = True;
                 }
                 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
                     exclude_dos_inode = True;
@@ -4300,7 +4548,7 @@ cacl_get(SMBCCTX *context,
                                                 return -1;
                                         }
                                         n = strlen(p);
-                                } else {
+                                } else if (sidstr[0] != '\0') {
                                         n = snprintf(buf, bufsize,
                                                      ",OWNER:%s", sidstr);
                                 }
@@ -4328,10 +4576,10 @@ cacl_get(SMBCCTX *context,
                 }
 
                 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, "");
                         }
@@ -4345,7 +4593,7 @@ cacl_get(SMBCCTX *context,
                                                 return -1;
                                         }
                                         n = strlen(p);
-                                } else {
+                                } else if (sidstr[0] != '\0') {
                                         n = snprintf(buf, bufsize,
                                                      ",GROUP:%s", sidstr);
                                 }
@@ -4376,7 +4624,7 @@ cacl_get(SMBCCTX *context,
                         /* 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);
@@ -4390,7 +4638,7 @@ cacl_get(SMBCCTX *context,
                                                         sidstr,
                                                         ace->type,
                                                         ace->flags,
-                                                        ace->info.mask);
+                                                        ace->access_mask);
                                                 if (!p) {
                                                         errno = ENOMEM;
                                                         return -1;
@@ -4403,7 +4651,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) ||
@@ -4415,7 +4663,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;
@@ -4426,7 +4674,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) {
@@ -4437,7 +4685,7 @@ cacl_get(SMBCCTX *context,
                                                         sidstr,
                                                         ace->type,
                                                         ace->flags,
-                                                        ace->info.mask);
+                                                        ace->access_mask);
                                                 if (!p) {
                                                         errno = ENOMEM;
                                                         return -1;
@@ -4450,10 +4698,10 @@ 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;
                                 }
@@ -4473,13 +4721,22 @@ cacl_get(SMBCCTX *context,
 
                 /* Obtain the DOS attributes */
                 if (!smbc_getatr(context, srv, filename, &mode, &size, 
-                                 &c_time, &a_time, &m_time, &ino)) {
+                                 &create_time_ts,
+                                 &access_time_ts,
+                                 &write_time_ts,
+                                 &change_time_ts,
+                                 &ino)) {
                         
-                        errno = smbc_errno(context, &srv->cli);
+                        errno = smbc_errno(context, srv->cli);
                         return -1;
                         
                 }
-                
+
+                create_time = convert_timespec_to_time_t(create_time_ts);
+                access_time = convert_timespec_to_time_t(access_time_ts);
+                write_time = convert_timespec_to_time_t(write_time_ts);
+                change_time = convert_timespec_to_time_t(change_time_ts);
+
                 if (! exclude_dos_mode) {
                         if (all || all_dos) {
                                 if (determine_size) {
@@ -4571,12 +4828,55 @@ cacl_get(SMBCCTX *context,
                         bufsize -= n;
                 }
 
-                if (! exclude_dos_ctime) {
+                if (! exclude_dos_create_time &&
+                    attr_strings.create_time_attr != NULL) {
+                        if (all || all_dos) {
+                                if (determine_size) {
+                                        p = talloc_asprintf(ctx,
+                                                            ",%s:%lu",
+                                                            attr_strings.create_time_attr,
+                                                            create_time);
+                                        if (!p) {
+                                                errno = ENOMEM;
+                                                return -1;
+                                        }
+                                        n = strlen(p);
+                                } else {
+                                        n = snprintf(buf, bufsize,
+                                                     ",%s:%lu",
+                                                     attr_strings.create_time_attr,
+                                                     create_time);
+                                }
+                        } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
+                                if (determine_size) {
+                                        p = talloc_asprintf(ctx, "%lu", create_time);
+                                        if (!p) {
+                                                errno = ENOMEM;
+                                                return -1;
+                                        }
+                                        n = strlen(p);
+                                } else {
+                                        n = snprintf(buf, bufsize,
+                                                     "%lu", create_time);
+                                }
+                        }
+        
+                        if (!determine_size && n > bufsize) {
+                                errno = ERANGE;
+                                return -1;
+                        }
+                        buf += n;
+                        n_used += n;
+                        bufsize -= n;
+                }
+
+                if (! exclude_dos_access_time) {
                         if (all || all_dos) {
                                 if (determine_size) {
                                         p = talloc_asprintf(ctx,
-                                                            ",C_TIME:%lu",
-                                                            c_time);
+                                                            ",%s:%lu",
+                                                            attr_strings.access_time_attr,
+                                                            access_time);
                                         if (!p) {
                                                 errno = ENOMEM;
                                                 return -1;
@@ -4584,11 +4884,13 @@ cacl_get(SMBCCTX *context,
                                         n = strlen(p);
                                 } else {
                                         n = snprintf(buf, bufsize,
-                                                     ",C_TIME:%lu", c_time);
+                                                     ",%s:%lu",
+                                                     attr_strings.access_time_attr,
+                                                     access_time);
                                 }
-                        } else if (StrCaseCmp(name, "c_time") == 0) {
+                        } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
                                 if (determine_size) {
-                                        p = talloc_asprintf(ctx, "%lu", c_time);
+                                        p = talloc_asprintf(ctx, "%lu", access_time);
                                         if (!p) {
                                                 errno = ENOMEM;
                                                 return -1;
@@ -4596,7 +4898,7 @@ cacl_get(SMBCCTX *context,
                                         n = strlen(p);
                                 } else {
                                         n = snprintf(buf, bufsize,
-                                                     "%lu", c_time);
+                                                     "%lu", access_time);
                                 }
                         }
         
@@ -4609,12 +4911,13 @@ cacl_get(SMBCCTX *context,
                         bufsize -= n;
                 }
 
-                if (! exclude_dos_atime) {
+                if (! exclude_dos_write_time) {
                         if (all || all_dos) {
                                 if (determine_size) {
                                         p = talloc_asprintf(ctx,
-                                                            ",A_TIME:%lu",
-                                                            a_time);
+                                                            ",%s:%lu",
+                                                            attr_strings.write_time_attr,
+                                                            write_time);
                                         if (!p) {
                                                 errno = ENOMEM;
                                                 return -1;
@@ -4622,11 +4925,13 @@ cacl_get(SMBCCTX *context,
                                         n = strlen(p);
                                 } else {
                                         n = snprintf(buf, bufsize,
-                                                     ",A_TIME:%lu", a_time);
+                                                     ",%s:%lu",
+                                                     attr_strings.write_time_attr,
+                                                     write_time);
                                 }
-                        } else if (StrCaseCmp(name, "a_time") == 0) {
+                        } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
                                 if (determine_size) {
-                                        p = talloc_asprintf(ctx, "%lu", a_time);
+                                        p = talloc_asprintf(ctx, "%lu", write_time);
                                         if (!p) {
                                                 errno = ENOMEM;
                                                 return -1;
@@ -4634,7 +4939,7 @@ cacl_get(SMBCCTX *context,
                                         n = strlen(p);
                                 } else {
                                         n = snprintf(buf, bufsize,
-                                                     "%lu", a_time);
+                                                     "%lu", write_time);
                                 }
                         }
         
@@ -4647,12 +4952,13 @@ cacl_get(SMBCCTX *context,
                         bufsize -= n;
                 }
 
-                if (! exclude_dos_mtime) {
+                if (! exclude_dos_change_time) {
                         if (all || all_dos) {
                                 if (determine_size) {
                                         p = talloc_asprintf(ctx,
-                                                            ",M_TIME:%lu",
-                                                            m_time);
+                                                            ",%s:%lu",
+                                                            attr_strings.change_time_attr,
+                                                            change_time);
                                         if (!p) {
                                                 errno = ENOMEM;
                                                 return -1;
@@ -4660,11 +4966,13 @@ cacl_get(SMBCCTX *context,
                                         n = strlen(p);
                                 } else {
                                         n = snprintf(buf, bufsize,
-                                                     ",M_TIME:%lu", m_time);
+                                                     ",%s:%lu",
+                                                     attr_strings.change_time_attr,
+                                                     change_time);
                                 }
-                        } else if (StrCaseCmp(name, "m_time") == 0) {
+                        } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
                                 if (determine_size) {
-                                        p = talloc_asprintf(ctx, "%lu", m_time);
+                                        p = talloc_asprintf(ctx, "%lu", change_time);
                                         if (!p) {
                                                 errno = ENOMEM;
                                                 return -1;
@@ -4672,7 +4980,7 @@ cacl_get(SMBCCTX *context,
                                         n = strlen(p);
                                 } else {
                                         n = snprintf(buf, bufsize,
-                                                     "%lu", m_time);
+                                                     "%lu", change_time);
                                 }
                         }
         
@@ -4783,11 +5091,19 @@ cacl_set(TALLOC_CTX *ctx,
                                     CONST_DISCARD(char *, the_acl));
 
                 if (!sd) {
-                        errno = EINVAL;
-                        return -1;
+                       errno = EINVAL;
+                       return -1;
                 }
         }
 
+       /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
+          that doesn't deref sd */
+
+       if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
+               errno = EINVAL;
+               return -1;
+       }
+
        /* The desired access below is the only one I could find that works
           with NT4, W2KP and Samba */
 
@@ -4813,9 +5129,9 @@ 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->aces);
                 SAFE_FREE(old->dacl);
-                old->off_dacl = 0;
+                old->dacl = NULL;
                 dacl = old->dacl;
                 break;
 
@@ -4824,18 +5140,18 @@ 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->aces);
                                                SAFE_FREE(old->dacl);
-                                               old->off_dacl = 0;
+                                               old->dacl = NULL;
                                        }
                                        found = True;
                                         dacl = old->dacl;
@@ -4856,14 +5172,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;
                                }
@@ -4876,7 +5192,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;
@@ -4885,7 +5201,7 @@ cacl_set(TALLOC_CTX *ctx,
        case SMBC_XATTR_MODE_SET:
                old = sd;
                 owner_sid = old->owner_sid;
-                grp_sid = old->grp_sid;
+                grp_sid = old->group_sid;
                 dacl = old->dacl;
                break;
 
@@ -4894,7 +5210,7 @@ cacl_set(TALLOC_CTX *ctx,
                 break;
 
         case SMBC_XATTR_MODE_CHGRP:
-                grp_sid = sd->grp_sid;
+                grp_sid = sd->group_sid;
                 break;
        }
 
@@ -4954,6 +5270,12 @@ smbc_setxattr_ctx(SMBCCTX *context,
         TALLOC_CTX *ctx;
         POLICY_HND pol;
         DOS_ATTR_DESC *dad;
+        struct {
+                const char * create_time_attr;
+                const char * access_time_attr;
+                const char * write_time_attr;
+                const char * change_time_attr;
+        } attr_strings;
 
        if (!context || !context->internal ||
            !context->internal->_initialized) {
@@ -4974,6 +5296,7 @@ smbc_setxattr_ctx(SMBCCTX *context,
                   fname, name, (int) size, (const char*)value));
 
        if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -4986,9 +5309,8 @@ smbc_setxattr_ctx(SMBCCTX *context,
 
        if (user[0] == (char)0) fstrcpy(user, context->user);
 
-       fstrcpy(workgroup, context->workgroup);
-
-       srv = smbc_server(context, server, share, workgroup, user, password);
+       srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
        if (!srv) {
                return -1;  /* errno set by smbc_server */
        }
@@ -4997,7 +5319,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;
         }
@@ -5024,8 +5348,8 @@ smbc_setxattr_ctx(SMBCCTX *context,
                 }
 
                 if (ipc_srv) {
-                        ret = cacl_set(ctx, &srv->cli,
-                                       &ipc_srv->cli, &pol, path,
+                        ret = cacl_set(ctx, srv->cli,
+                                       ipc_srv->cli, &pol, path,
                                        namevalue,
                                        (*namevalue == '*'
                                         ? SMBC_XATTR_MODE_SET
@@ -5043,9 +5367,10 @@ smbc_setxattr_ctx(SMBCCTX *context,
 
                         /* Set the new DOS attributes */
                         if (! smbc_setatr(context, srv, path,
-                                          dad->c_time,
-                                          dad->a_time,
-                                          dad->m_time,
+                                          dad->create_time,
+                                          dad->access_time,
+                                          dad->write_time,
+                                          dad->change_time,
                                           dad->mode)) {
 
                                 /* cause failure if NT failed too */
@@ -5087,8 +5412,8 @@ smbc_setxattr_ctx(SMBCCTX *context,
                         errno = ENOMEM;
                         ret = -1;
                 } else {
-                        ret = cacl_set(ctx, &srv->cli,
-                                       &ipc_srv->cli, &pol, path,
+                        ret = cacl_set(ctx, srv->cli,
+                                       ipc_srv->cli, &pol, path,
                                        namevalue,
                                        (*namevalue == '*'
                                         ? SMBC_XATTR_MODE_SET
@@ -5118,8 +5443,8 @@ smbc_setxattr_ctx(SMBCCTX *context,
                         errno = ENOMEM;
                         ret = -1;
                 } else {
-                        ret = cacl_set(ctx, &srv->cli,
-                                       &ipc_srv->cli, &pol, path,
+                        ret = cacl_set(ctx, srv->cli,
+                                       ipc_srv->cli, &pol, path,
                                        namevalue, SMBC_XATTR_MODE_CHOWN, 0);
                 }
                 talloc_destroy(ctx);
@@ -5145,22 +5470,39 @@ smbc_setxattr_ctx(SMBCCTX *context,
                         errno = ENOMEM;
                         ret = -1;
                 } else {
-                        ret = cacl_set(ctx, &srv->cli,
-                                       &ipc_srv->cli, &pol, path,
+                        ret = cacl_set(ctx, srv->cli,
+                                       ipc_srv->cli, &pol, path,
                                        namevalue, SMBC_XATTR_MODE_CHOWN, 0);
                 }
                 talloc_destroy(ctx);
                 return ret;
         }
 
+        /* Determine whether to use old-style or new-style attribute names */
+        if (context->internal->_full_time_names) {
+                /* new-style names */
+                attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
+                attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
+                attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
+                attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
+        } else {
+                /* old-style names */
+                attr_strings.create_time_attr = NULL;
+                attr_strings.access_time_attr = "system.dos_attr.A_TIME";
+                attr_strings.write_time_attr = "system.dos_attr.M_TIME";
+                attr_strings.change_time_attr = "system.dos_attr.C_TIME";
+        }
+
         /*
          * Are they asking to set a DOS attribute?
          */
         if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
-            StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
-            StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
-            StrCaseCmp(name, "system.dos_attr.m_time") == 0) {
+            (attr_strings.create_time_attr != NULL &&
+             StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
+            StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
+            StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
+            StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
 
                 /* get a DOS Attribute Descriptor with current attributes */
                 dad = dos_attr_query(context, ctx, path, srv);
@@ -5177,9 +5519,10 @@ smbc_setxattr_ctx(SMBCCTX *context,
 
                                 /* Set the new DOS attributes */
                                 ret2 = smbc_setatr(context, srv, path,
-                                                   dad->c_time,
-                                                   dad->a_time,
-                                                   dad->m_time,
+                                                   dad->create_time,
+                                                   dad->access_time,
+                                                   dad->write_time,
+                                                   dad->change_time,
                                                    dad->mode);
 
                                 /* ret2 has True (success) / False (failure) */
@@ -5221,6 +5564,12 @@ smbc_getxattr_ctx(SMBCCTX *context,
         pstring path;
         TALLOC_CTX *ctx;
         POLICY_HND pol;
+        struct {
+                const char * create_time_attr;
+                const char * access_time_attr;
+                const char * write_time_attr;
+                const char * change_time_attr;
+        } attr_strings;
 
 
         if (!context || !context->internal ||
@@ -5241,6 +5590,7 @@ smbc_getxattr_ctx(SMBCCTX *context,
         DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
 
         if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -5253,9 +5603,8 @@ smbc_getxattr_ctx(SMBCCTX *context,
 
         if (user[0] == (char)0) fstrcpy(user, context->user);
 
-        fstrcpy(workgroup, context->workgroup);
-
-        srv = smbc_server(context, server, share, workgroup, user, password);
+        srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
         if (!srv) {
                 return -1;  /* errno set by smbc_server */
         }
@@ -5277,6 +5626,21 @@ smbc_getxattr_ctx(SMBCCTX *context,
                 return -1;
         }
 
+        /* Determine whether to use old-style or new-style attribute names */
+        if (context->internal->_full_time_names) {
+                /* new-style names */
+                attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
+                attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
+                attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
+                attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
+        } else {
+                /* old-style names */
+                attr_strings.create_time_attr = NULL;
+                attr_strings.access_time_attr = "system.dos_attr.A_TIME";
+                attr_strings.write_time_attr = "system.dos_attr.M_TIME";
+                attr_strings.change_time_attr = "system.dos_attr.C_TIME";
+        }
+
         /* Are they requesting a supported attribute? */
         if (StrCaseCmp(name, "system.*") == 0 ||
             StrnCaseCmp(name, "system.*!", 9) == 0 ||
@@ -5297,19 +5661,21 @@ smbc_getxattr_ctx(SMBCCTX *context,
             StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
             StrCaseCmp(name, "system.dos_attr.size") == 0 ||
-            StrCaseCmp(name, "system.dos_attr.c_time") == 0 ||
-            StrCaseCmp(name, "system.dos_attr.a_time") == 0 ||
-            StrCaseCmp(name, "system.dos_attr.m_time") == 0 ||
+            (attr_strings.create_time_attr != NULL &&
+             StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
+            StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
+            StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
+            StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
             StrCaseCmp(name, "system.dos_attr.inode") == 0) {
 
                 /* Yup. */
                 ret = cacl_get(context, ctx, srv,
-                               ipc_srv == NULL ? NULL : &ipc_srv->cli, 
+                               ipc_srv == NULL ? NULL : ipc_srv->cli, 
                                &pol, path,
                                CONST_DISCARD(char *, name),
                                CONST_DISCARD(char *, value), size);
                 if (ret < 0 && errno == 0) {
-                        errno = smbc_errno(context, &srv->cli);
+                        errno = smbc_errno(context, srv->cli);
                 }
                 talloc_destroy(ctx);
                 return ret;
@@ -5357,6 +5723,7 @@ smbc_removexattr_ctx(SMBCCTX *context,
         DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
 
         if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -5369,9 +5736,8 @@ smbc_removexattr_ctx(SMBCCTX *context,
 
         if (user[0] == (char)0) fstrcpy(user, context->user);
 
-        fstrcpy(workgroup, context->workgroup);
-
-        srv = smbc_server(context, server, share, workgroup, user, password);
+        srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
         if (!srv) {
                 return -1;  /* errno set by smbc_server */
         }
@@ -5380,7 +5746,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;
         }
@@ -5400,8 +5768,8 @@ smbc_removexattr_ctx(SMBCCTX *context,
             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
 
                 /* Yup. */
-                ret = cacl_set(ctx, &srv->cli,
-                               &ipc_srv->cli, &pol, path,
+                ret = cacl_set(ctx, srv->cli,
+                               ipc_srv->cli, &pol, path,
                                NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
                 talloc_destroy(ctx);
                 return ret;
@@ -5420,8 +5788,8 @@ smbc_removexattr_ctx(SMBCCTX *context,
             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
 
                 /* Yup. */
-                ret = cacl_set(ctx, &srv->cli,
-                               &ipc_srv->cli, &pol, path,
+                ret = cacl_set(ctx, srv->cli,
+                               ipc_srv->cli, &pol, path,
                                name + 19, SMBC_XATTR_MODE_REMOVE, 0);
                 talloc_destroy(ctx);
                 return ret;
@@ -5444,7 +5812,7 @@ smbc_listxattr_ctx(SMBCCTX *context,
          * the complete set of attribute names, always, rather than only those
          * attribute names which actually exist for a file.  Hmmm...
          */
-        const char supported[] =
+        const char supported_old[] =
                 "system.*\0"
                 "system.*+\0"
                 "system.nt_sec_desc.revision\0"
@@ -5463,6 +5831,33 @@ smbc_listxattr_ctx(SMBCCTX *context,
                 "system.dos_attr.a_time\0"
                 "system.dos_attr.m_time\0"
                 ;
+        const char supported_new[] =
+                "system.*\0"
+                "system.*+\0"
+                "system.nt_sec_desc.revision\0"
+                "system.nt_sec_desc.owner\0"
+                "system.nt_sec_desc.owner+\0"
+                "system.nt_sec_desc.group\0"
+                "system.nt_sec_desc.group+\0"
+                "system.nt_sec_desc.acl.*\0"
+                "system.nt_sec_desc.acl\0"
+                "system.nt_sec_desc.acl+\0"
+                "system.nt_sec_desc.*\0"
+                "system.nt_sec_desc.*+\0"
+                "system.dos_attr.*\0"
+                "system.dos_attr.mode\0"
+                "system.dos_attr.create_time\0"
+                "system.dos_attr.access_time\0"
+                "system.dos_attr.write_time\0"
+                "system.dos_attr.change_time\0"
+                ;
+        const char * supported;
+
+        if (context->internal->_full_time_names) {
+                supported = supported_new;
+        } else {
+                supported = supported_old;
+        }
 
         if (size == 0) {
                 return sizeof(supported);
@@ -5511,6 +5906,7 @@ smbc_open_print_job_ctx(SMBCCTX *context,
         DEBUG(4, ("smbc_open_print_job_ctx(%s)\n", fname));
 
         if (smbc_parse_path(context, fname,
+                            NULL, 0,
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -5648,6 +6044,7 @@ smbc_list_print_jobs_ctx(SMBCCTX *context,
         DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
 
         if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -5660,9 +6057,8 @@ smbc_list_print_jobs_ctx(SMBCCTX *context,
 
         if (user[0] == (char)0) fstrcpy(user, context->user);
         
-        fstrcpy(workgroup, context->workgroup);
-
-        srv = smbc_server(context, server, share, workgroup, user, password);
+        srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
         if (!srv) {
 
@@ -5670,10 +6066,10 @@ smbc_list_print_jobs_ctx(SMBCCTX *context,
 
         }
 
-        if (cli_print_queue(&srv->cli,
+        if (cli_print_queue(srv->cli,
                             (void (*)(struct print_job_info *))fn) < 0) {
 
-                errno = smbc_errno(context, &srv->cli);
+                errno = smbc_errno(context, srv->cli);
                 return -1;
 
         }
@@ -5718,6 +6114,7 @@ smbc_unlink_print_job_ctx(SMBCCTX *context,
         DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
 
         if (smbc_parse_path(context, fname,
+                            workgroup, sizeof(workgroup),
                             server, sizeof(server),
                             share, sizeof(share),
                             path, sizeof(path),
@@ -5730,9 +6127,8 @@ smbc_unlink_print_job_ctx(SMBCCTX *context,
 
         if (user[0] == (char)0) fstrcpy(user, context->user);
 
-        fstrcpy(workgroup, context->workgroup);
-
-        srv = smbc_server(context, server, share, workgroup, user, password);
+        srv = smbc_server(context, True,
+                          server, share, workgroup, user, password);
 
         if (!srv) {
 
@@ -5740,10 +6136,10 @@ smbc_unlink_print_job_ctx(SMBCCTX *context,
 
         }
 
-        if ((err = cli_printjob_del(&srv->cli, id)) != 0) {
+        if ((err = cli_printjob_del(srv->cli, id)) != 0) {
 
                 if (err < 0)
-                        errno = smbc_errno(context, &srv->cli);
+                        errno = smbc_errno(context, srv->cli);
                 else if (err == ERRnosuchprintjob)
                         errno = EINVAL;
                 return -1;
@@ -5772,6 +6168,7 @@ smbc_new_context(void)
 
         context->internal = SMB_MALLOC_P(struct smbc_internal_data);
         if (!context->internal) {
+               SAFE_FREE(context);
                 errno = ENOMEM;
                 return NULL;
         }
@@ -5786,6 +6183,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;
@@ -5861,8 +6260,8 @@ smbc_free_context(SMBCCTX *context,
                         s = context->internal->_servers;
                         while (s) {
                                 DEBUG(1, ("Forced shutdown: %p (fd=%d)\n",
-                                          s, s->cli.fd));
-                                cli_shutdown(&s->cli);
+                                          s, s->cli->fd));
+                                cli_shutdown(s->cli);
                                 context->callbacks.remove_cached_srv_fn(context,
                                                                         s);
                                 next = s->next;
@@ -5907,6 +6306,127 @@ smbc_free_context(SMBCCTX *context,
 }
 
 
+/*
+ * Each time the context structure is changed, we have binary backward
+ * compatibility issues.  Instead of modifying the public portions of the
+ * context structure to add new options, instead, we put them in the internal
+ * portion of the context structure and provide a set function for these new
+ * options.
+ */
+void
+smbc_option_set(SMBCCTX *context,
+                char *option_name,
+                ... /* option_value */)
+{
+        va_list ap;
+        union {
+                int i;
+                BOOL b;
+                smbc_get_auth_data_with_context_fn auth_fn;
+                void *v;
+        } option_value;
+
+        va_start(ap, option_name);
+
+        if (strcmp(option_name, "debug_to_stderr") == 0) {
+                /*
+                 * Log to standard error instead of standard output.
+                 */
+                option_value.b = (BOOL) va_arg(ap, int);
+                context->internal->_debug_stderr = option_value.b;
+
+        } else if (strcmp(option_name, "full_time_names") == 0) {
+                /*
+                 * Use new-style time attribute names, e.g. WRITE_TIME rather
+                 * than the old-style names such as M_TIME.  This allows also
+                 * setting/getting CREATE_TIME which was previously
+                 * unimplemented.  (Note that the old C_TIME was supposed to
+                 * be CHANGE_TIME but was confused and sometimes referred to
+                 * CREATE_TIME.)
+                 */
+                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
+                 * the context.
+                 */
+                option_value.auth_fn =
+                        va_arg(ap, smbc_get_auth_data_with_context_fn);
+                context->internal->_auth_fn_with_context =
+                        option_value.auth_fn;
+        } else if (strcmp(option_name, "user_data") == 0) {
+                /*
+                 * Save a user data handle which may be retrieved by the user
+                 * with smbc_option_get()
+                 */
+                option_value.v = va_arg(ap, void *);
+                context->internal->_user_data = option_value.v;
+        }
+
+        va_end(ap);
+}
+
+
+/*
+ * Retrieve the current value of an option
+ */
+void *
+smbc_option_get(SMBCCTX *context,
+                char *option_name)
+{
+        if (strcmp(option_name, "debug_stderr") == 0) {
+                /*
+                 * Log to standard error instead of standard output.
+                 */
+#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
+               return (void *) (intptr_t) context->internal->_debug_stderr;
+#else
+               return (void *) context->internal->_debug_stderr;
+#endif
+        } else if (strcmp(option_name, "full_time_names") == 0) {
+                /*
+                 * Use new-style time attribute names, e.g. WRITE_TIME rather
+                 * than the old-style names such as M_TIME.  This allows also
+                 * setting/getting CREATE_TIME which was previously
+                 * unimplemented.  (Note that the old C_TIME was supposed to
+                 * be CHANGE_TIME but was confused and sometimes referred to
+                 * CREATE_TIME.)
+                 */
+#if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
+               return (void *) (intptr_t) context->internal->_full_time_names;
+#else
+               return (void *) context->internal->_full_time_names;
+#endif
+
+        } else if (strcmp(option_name, "auth_function") == 0) {
+                /*
+                 * Use the new-style authentication function which includes
+                 * the context.
+                 */
+                return (void *) context->internal->_auth_fn_with_context;
+        } else if (strcmp(option_name, "user_data") == 0) {
+                /*
+                 * Save a user data handle which may be retrieved by the user
+                 * with smbc_option_get()
+                 */
+                return context->internal->_user_data;
+        }
+
+        return NULL;
+}
+
+
 /*
  * Initialise the library etc 
  *
@@ -5932,7 +6452,8 @@ smbc_init_context(SMBCCTX *context)
                 return 0;
         }
 
-        if (!context->callbacks.auth_fn ||
+        if ((!context->callbacks.auth_fn &&
+             !context->internal->_auth_fn_with_context) ||
             context->debug < 0 ||
             context->debug > 100) {
 
@@ -5951,7 +6472,13 @@ smbc_init_context(SMBCCTX *context)
                 /* Set this to what the user wants */
                 DEBUGLEVEL = context->debug;
                 
-                setup_logging( "libsmbclient", True);
+                load_case_tables();
+
+                setup_logging("libsmbclient", True);
+                if (context->internal->_debug_stderr) {
+                        dbf = x_stderr;
+                        x_setbuf(x_stderr, NULL);
+                }
 
                 /* Here we would open the smb.conf file if needed ... */
                 
@@ -5960,7 +6487,7 @@ smbc_init_context(SMBCCTX *context)
                 home = getenv("HOME");
                if (home) {
                        slprintf(conf, sizeof(conf), "%s/.smb/smb.conf", home);
-                       if (lp_load(conf, True, False, False)) {
+                       if (lp_load(conf, True, False, False, True)) {
                                conf_loaded = True;
                        } else {
                                 DEBUG(5, ("Could not load config file: %s\n",
@@ -5976,7 +6503,7 @@ smbc_init_context(SMBCCTX *context)
                          * defaults ...
                          */
 
-                        if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
+                        if (!lp_load(dyn_CONFIGFILE, True, False, False, False)) {
                                 DEBUG(5, ("Could not load config file: %s\n",
                                           dyn_CONFIGFILE));
                         } else if (home) {
@@ -5987,7 +6514,7 @@ smbc_init_context(SMBCCTX *context)
                                  */
                                 slprintf(conf, sizeof(conf),
                                          "%s/.smb/smb.conf.append", home);
-                                if (!lp_load(conf, True, False, False)) {
+                                if (!lp_load(conf, True, False, False, False)) {
                                         DEBUG(10,
                                               ("Could not append config file: "
                                                "%s\n",
@@ -6036,7 +6563,7 @@ smbc_init_context(SMBCCTX *context)
                          * lazy for the moment
                          */
                         pid = sys_getpid();
-                        context->netbios_name = SMB_MALLOC(17);
+                        context->netbios_name = (char *)SMB_MALLOC(17);
                         if (!context->netbios_name) {
                                 errno = ENOMEM;
                                 return NULL;
@@ -6068,7 +6595,7 @@ smbc_init_context(SMBCCTX *context)
          * FIXME: Should we check the function pointers here? 
          */
 
-        context->internal->_initialized = 1;
+        context->internal->_initialized = True;
         
         return context;
 }