Print this page
NEX-14666 Need to provide SMB 2.1 Client
NEX-17187 panic in smbfs_acl_store
NEX-17231 smbfs create xattr files finds wrong file
NEX-17224 smbfs lookup EINVAL should be ENOENT
NEX-17260 SMB1 client fails to list directory after NEX-14666
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
and: (cleanup)
NEX-16824 SMB client connection setup rework
NEX-17232 SMB client reconnect failures
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
and: (improve debug)
NEX-16818 Add fksmbcl development tool
NEX-17264 SMB client test tp_smbutil_013 fails after NEX-14666
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
and: (fix ref leaks)
NEX-6309 Update NT status codes
Update ntstatus.h from [MS-ERREF] October 2015
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Kevin Crowe <kevin.crowe@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/uts/common/fs/smbclnt/netsmb/smb_subrs.c
          +++ new/usr/src/uts/common/fs/smbclnt/netsmb/smb_subrs.c
↓ open down ↓ 26 lines elided ↑ open up ↑
  27   27   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28   28   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29   29   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30   30   * SUCH DAMAGE.
  31   31   *
  32   32   * $Id: smb_subr.c,v 1.27.108.1 2005/06/02 00:55:39 lindak Exp $
  33   33   */
  34   34  
  35   35  /*
  36   36   * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
       37 + * Portions Copyright (C) 2001 - 2013 Apple Inc. All rights reserved.
       38 + * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  37   39   */
  38   40  
  39   41  #include <sys/param.h>
  40   42  #include <sys/systm.h>
  41   43  #include <sys/kmem.h>
  42   44  #include <sys/proc.h>
  43   45  #include <sys/lock.h>
  44   46  #include <sys/socket.h>
  45   47  #include <sys/isa_defs.h>
  46   48  #include <sys/stream.h>
↓ open down ↓ 3 lines elided ↑ open up ↑
  50   52  #include <sys/sdt.h>
  51   53  #include <sys/priv.h>
  52   54  #include <sys/u8_textprep.h>
  53   55  
  54   56  #include <netsmb/smb_osdep.h>
  55   57  #include <netsmb/smb.h>
  56   58  #include <netsmb/smb_conn.h>
  57   59  #include <netsmb/smb_rq.h>
  58   60  #include <netsmb/smb_subr.h>
  59   61  
  60      -/*
  61      - * XXX:This conversion might not be fully MS-Compatible
  62      - * for calculating hashes. The output length may differ
  63      - * for some locales and needs to be handled from where
  64      - * the call is made.
  65      - */
  66      -int
  67      -smb_toupper(const char *inbuf, char *outbuf, size_t outlen)
  68      -{
  69      -        int err = 0;
  70      -        size_t inlen, inrem, outrem;
  71      -
  72      -        inrem = inlen = strlen(inbuf);
  73      -        outrem = outlen;
  74      -        (void) u8_textprep_str((char *)inbuf, &inrem, outbuf, &outrem,
  75      -            U8_TEXTPREP_TOUPPER, U8_UNICODE_LATEST, &err);
  76      -        /* inrem, outrem are bytes unused, remaining */
  77      -        if (inrem) {
  78      -                SMBSDEBUG("input %d remains: %s\n", (int)inrem, inbuf);
  79      -                inlen -= inrem;
  80      -        }
  81      -        if (outrem) {
  82      -                outlen -= outrem;
  83      -                outbuf[outlen] = '\0';
  84      -        }
  85      -        if (outlen > inlen) {
  86      -                SMBSDEBUG("outlen > inlen! (%d > %d)\n",
  87      -                    (int)outlen, (int)inlen);
  88      -                /* Truncate to inlen here? */
  89      -        }
  90      -
  91      -        return (err);
  92      -}
  93      -
  94   62  void
  95   63  smb_credinit(struct smb_cred *scred, cred_t *cr)
  96   64  {
  97   65          /* cr arg is optional */
  98   66          if (cr == NULL)
  99   67                  cr = ddi_get_cred();
       68 +#ifdef  _KERNEL
 100   69          if (is_system_labeled()) {
 101   70                  cr = crdup(cr);
 102   71                  (void) setpflags(NET_MAC_AWARE, 1, cr);
 103      -        } else {
       72 +        } else
       73 +#endif
       74 +        {
 104   75                  crhold(cr);
 105   76          }
 106   77          scred->scr_cred = cr;
 107   78  }
 108   79  
 109   80  void
 110   81  smb_credrele(struct smb_cred *scred)
 111   82  {
 112   83          if (scred->scr_cred != NULL) {
 113   84                  crfree(scred->scr_cred);
 114   85                  scred->scr_cred = NULL;
 115   86          }
 116   87  }
 117   88  
       89 +#ifndef _KERNEL
       90 +/* ARGSUSED */
       91 +void
       92 +smb_debugmsg(const char *func, char *msg)
       93 +{
       94 +}
       95 +#endif  /* _KERNEL */
       96 +
 118   97  /*
 119   98   * Helper for the SMBERROR macro, etc.
 120   99   * This is also a good place for a breakpoint
 121  100   * or a dtrace probe, i.e. fbt:nsmb:smb_errmsg
 122  101   */
 123  102  void
 124  103  smb_errmsg(int cel, const char *func_name, const char *fmt, ...)
 125  104  {
 126  105          va_list adx;
 127  106          char buf[100];
↓ open down ↓ 2 lines elided ↑ open up ↑
 130  109          if (cel == CE_CONT) {
 131  110                  /*
 132  111                   * This is one of our xxxDEBUG macros.
 133  112                   * Don't bother to log these, but just
 134  113                   * fire a dtrace probe with the message.
 135  114                   */
 136  115                  (void) vsnprintf(buf, sizeof (buf), fmt, adx);
 137  116                  DTRACE_PROBE2(debugmsg2,
 138  117                      (char *), func_name,
 139  118                      (char *), buf);
      119 +#ifndef _KERNEL
      120 +                smb_debugmsg(func_name, buf);
      121 +#endif
 140  122          } else {
 141  123                  /*
 142  124                   * This is one of our xxxERROR macros.
 143  125                   * Add a prefix to the fmt string,
 144  126                   * then let vcmn_err do the args.
 145  127                   */
 146  128                  (void) snprintf(buf, sizeof (buf), "?%s: %s", func_name, fmt);
 147  129                  DTRACE_PROBE3(debugmsg3,
 148  130                      (char *), func_name,
 149  131                      (char *), buf,
↓ open down ↓ 33 lines elided ↑ open up ↑
 183  165  #endif
 184  166  #ifndef ECOMM
 185  167  #define ECOMM EIO
 186  168  #endif
 187  169  #ifndef ENOMEDIUM
 188  170  #define ENOMEDIUM ENXIO
 189  171  #endif
 190  172  #ifndef ETIME
 191  173  #define ETIME ETIMEDOUT
 192  174  #endif
      175 +#ifndef EMOREDATA
      176 +#define EMOREDATA (0x7fff)
      177 +#endif
 193  178  
 194  179  /*
 195  180   * Log any un-handled NT or DOS errors we encounter.
 196  181   * Make these log NOTICE in a debug build to ensure
 197  182   * they get noticed during tests.  In the field these
 198  183   * are unimportant, so just fire a Dtrace probe.
 199  184   */
 200  185  static int unknown_err_logpri =
 201  186  #ifdef  DEBUG
 202  187          CE_NOTE;
↓ open down ↓ 9 lines elided ↑ open up ↑
 212  197  static const nt2errno_t nt2errno[] = {
 213  198          /* Alphabetical order. */
 214  199          {NT_STATUS_ACCESS_DENIED,               EACCES},
 215  200          {NT_STATUS_ACCESS_VIOLATION,            EACCES},
 216  201          {NT_STATUS_ACCOUNT_DISABLED,            EACCES},
 217  202          {NT_STATUS_ACCOUNT_EXPIRED,             EACCES},
 218  203          {NT_STATUS_ACCOUNT_LOCKED_OUT,          EACCES},
 219  204          {NT_STATUS_ACCOUNT_RESTRICTION,         EACCES},
 220  205          {NT_STATUS_ADDRESS_ALREADY_EXISTS,      EADDRINUSE},
 221  206          {NT_STATUS_BAD_NETWORK_NAME,            ENOENT},
 222      -        {NT_STATUS_BUFFER_TOO_SMALL,            EMOREDATA},
      207 +        {NT_STATUS_BAD_NETWORK_PATH,            ENOENT},
      208 +        {NT_STATUS_BUFFER_TOO_SMALL,            E2BIG},
      209 +        {NT_STATUS_CANCELLED,                   ECANCELED},
 223  210          {NT_STATUS_CANNOT_DELETE,               EACCES},
 224  211          {NT_STATUS_CONFLICTING_ADDRESSES,       EADDRINUSE},
 225  212          {NT_STATUS_CONNECTION_ABORTED,          ECONNABORTED},
 226  213          {NT_STATUS_CONNECTION_DISCONNECTED,     ECONNABORTED},
 227  214          {NT_STATUS_CONNECTION_REFUSED,          ECONNREFUSED},
 228  215          {NT_STATUS_CONNECTION_RESET,            ENETRESET},
 229  216          {NT_STATUS_DELETE_PENDING,              EACCES},
 230  217          {NT_STATUS_DEVICE_DOES_NOT_EXIST,       ENODEV},
 231  218          {NT_STATUS_DEVICE_PROTOCOL_ERROR,       EPROTO},
 232  219          {NT_STATUS_DIRECTORY_NOT_EMPTY,         ENOTEMPTY},
 233  220          {NT_STATUS_DISK_FULL,                   ENOSPC},
 234  221          {NT_STATUS_DLL_NOT_FOUND,               ELIBACC},
 235  222          {NT_STATUS_DUPLICATE_NAME,              EINVAL},
      223 +        {NT_STATUS_EAS_NOT_SUPPORTED,           ENOTSUP},
      224 +        {NT_STATUS_EA_TOO_LARGE,                E2BIG},
 236  225          {NT_STATUS_END_OF_FILE,                 ENODATA},
      226 +        {NT_STATUS_FILE_CLOSED,                 EBADF},
      227 +        {NT_STATUS_FILE_DELETED,                ENOENT},
      228 +        {NT_STATUS_FILE_INVALID,                EIO},
 237  229          {NT_STATUS_FILE_IS_A_DIRECTORY,         EISDIR},
 238  230          {NT_STATUS_FILE_LOCK_CONFLICT,          EAGAIN},
      231 +        {NT_STATUS_FILE_RENAMED,                ENOENT},
 239  232          {NT_STATUS_FLOAT_INEXACT_RESULT,        ERANGE},
 240  233          {NT_STATUS_FLOAT_OVERFLOW,              ERANGE},
 241  234          {NT_STATUS_FLOAT_UNDERFLOW,             ERANGE},
 242  235          {NT_STATUS_HOST_UNREACHABLE,            EHOSTUNREACH},
 243      -        {NT_STATUS_ILL_FORMED_PASSWORD,         EACCES},
      236 +        {NT_STATUS_ILL_FORMED_PASSWORD,         EAUTH},
      237 +        {NT_STATUS_INFO_LENGTH_MISMATCH,        EINVAL},
      238 +        {NT_STATUS_INSUFFICIENT_RESOURCES,      EAGAIN},
      239 +        {NT_STATUS_INSUFF_SERVER_RESOURCES,     EAGAIN},
 244  240          {NT_STATUS_INTEGER_OVERFLOW,            ERANGE},
 245      -        {NT_STATUS_INVALID_ACCOUNT_NAME,        EACCES},
      241 +        {NT_STATUS_INVALID_ACCOUNT_NAME,        EAUTH},
      242 +        {NT_STATUS_INVALID_BUFFER_SIZE,         EIO},
      243 +        {NT_STATUS_INVALID_DEVICE_REQUEST,      EINVAL},
 246  244          {NT_STATUS_INVALID_HANDLE,              EBADF},
      245 +        {NT_STATUS_INVALID_INFO_CLASS,          EINVAL},
 247  246          {NT_STATUS_INVALID_LEVEL,               ENOTSUP},
 248      -        {NT_STATUS_INVALID_LOGON_HOURS,         EACCES},
      247 +        {NT_STATUS_INVALID_LOCK_SEQUENCE,       EINVAL},
      248 +        {NT_STATUS_INVALID_LOGON_HOURS,         EAUTH},
 249  249          {NT_STATUS_INVALID_OWNER,               EINVAL},
 250  250          {NT_STATUS_INVALID_PARAMETER,           EINVAL},
 251  251          {NT_STATUS_INVALID_PIPE_STATE,          EPIPE},
 252  252          {NT_STATUS_INVALID_PRIMARY_GROUP,       EINVAL},
 253  253          {NT_STATUS_INVALID_WORKSTATION,         EACCES},
 254  254          {NT_STATUS_IN_PAGE_ERROR,               EFAULT},
      255 +        {NT_STATUS_IO_DEVICE_ERROR,             EIO},
 255  256          {NT_STATUS_IO_TIMEOUT,                  ETIMEDOUT},
 256      -        {NT_STATUS_IP_ADDRESS_CONFLICT1,        ENOTUNIQ},
 257      -        {NT_STATUS_IP_ADDRESS_CONFLICT2,        ENOTUNIQ},
      257 +        {NT_STATUS_IP_ADDRESS_CONFLICT1,        EADDRINUSE},
      258 +        {NT_STATUS_IP_ADDRESS_CONFLICT2,        EADDRINUSE},
 258  259          {NT_STATUS_LICENSE_QUOTA_EXCEEDED,      EDQUOT},
 259  260          {NT_STATUS_LOCK_NOT_GRANTED,            EAGAIN},
 260      -        {NT_STATUS_LOGIN_TIME_RESTRICTION,      EACCES},
 261      -        {NT_STATUS_LOGON_FAILURE,               EACCES},
      261 +        {NT_STATUS_LOGIN_TIME_RESTRICTION,      EAUTH},
      262 +        {NT_STATUS_LOGON_FAILURE,               EAUTH},
      263 +        {NT_STATUS_LOGON_TYPE_NOT_GRANTED,      EAUTH},
 262  264          {NT_STATUS_MEDIA_WRITE_PROTECTED,       EROFS},
 263  265          {NT_STATUS_MEMORY_NOT_ALLOCATED,        EFAULT},
      266 +        {NT_STATUS_MORE_PROCESSING_REQUIRED,    EINPROGRESS},
 264  267          {NT_STATUS_NAME_TOO_LONG,               ENAMETOOLONG},
 265  268          {NT_STATUS_NETWORK_ACCESS_DENIED,       EACCES},
 266  269          {NT_STATUS_NETWORK_BUSY,                EBUSY},
      270 +        {NT_STATUS_NETWORK_NAME_DELETED,        ENOENT},
 267  271          {NT_STATUS_NETWORK_UNREACHABLE,         ENETUNREACH},
 268  272          {NT_STATUS_NET_WRITE_FAULT,             ECOMM},
      273 +        {NT_STATUS_NONEXISTENT_EA_ENTRY,        ENOENT},
 269  274          {NT_STATUS_NONEXISTENT_SECTOR,          ESPIPE},
 270  275          {NT_STATUS_NONE_MAPPED,                 EINVAL},
 271  276          {NT_STATUS_NOT_A_DIRECTORY,             ENOTDIR},
      277 +        {NT_STATUS_NOT_FOUND,                   ENOENT},
 272  278          {NT_STATUS_NOT_IMPLEMENTED,             ENOTSUP},
      279 +        {NT_STATUS_NOT_LOCKED,                  ENOLCK},
 273  280          {NT_STATUS_NOT_MAPPED_VIEW,             EINVAL},
 274  281          {NT_STATUS_NOT_SUPPORTED,               ENOTSUP},
      282 +        {NT_STATUS_NO_EAS_ON_FILE,              ENOENT},
      283 +        {NT_STATUS_NO_LOGON_SERVERS,            EAUTH},
 275  284          {NT_STATUS_NO_MEDIA,                    ENOMEDIUM},
 276  285          {NT_STATUS_NO_MEDIA_IN_DEVICE,          ENOMEDIUM},
 277  286          {NT_STATUS_NO_MEMORY,                   ENOMEM},
 278  287          {NT_STATUS_NO_SUCH_DEVICE,              ENODEV},
 279  288          {NT_STATUS_NO_SUCH_FILE,                ENOENT},
      289 +        {NT_STATUS_NO_SUCH_LOGON_SESSION,       EAUTH},
      290 +        {NT_STATUS_NO_SUCH_USER,                EAUTH},
      291 +        {NT_STATUS_NO_TRUST_LSA_SECRET,         EAUTH},
      292 +        {NT_STATUS_NO_TRUST_SAM_ACCOUNT,        EAUTH},
 280  293          {NT_STATUS_OBJECT_NAME_COLLISION,       EEXIST},
 281  294          {NT_STATUS_OBJECT_NAME_INVALID,         EINVAL},
 282  295          {NT_STATUS_OBJECT_NAME_NOT_FOUND,       ENOENT},
 283  296          {NT_STATUS_OBJECT_PATH_INVALID,         ENOTDIR},
 284  297          {NT_STATUS_OBJECT_PATH_NOT_FOUND,       ENOENT},
      298 +        {NT_STATUS_OBJECT_PATH_SYNTAX_BAD,      EINVAL},
      299 +        {NT_STATUS_OBJECT_TYPE_MISMATCH,        EBADF},
 285  300          {NT_STATUS_PAGEFILE_QUOTA,              EDQUOT},
 286      -        {NT_STATUS_PASSWORD_EXPIRED,            EACCES},
 287      -        {NT_STATUS_PASSWORD_MUST_CHANGE,        EACCES},
 288      -        {NT_STATUS_PASSWORD_RESTRICTION,        EACCES},
      301 +        {NT_STATUS_PASSWORD_EXPIRED,            EAUTH},
      302 +        {NT_STATUS_PASSWORD_MUST_CHANGE,        EAUTH},
      303 +        {NT_STATUS_PASSWORD_RESTRICTION,        EAUTH},
 289  304          {NT_STATUS_PATH_NOT_COVERED,            ENOENT},
 290  305          {NT_STATUS_PIPE_BROKEN,                 EPIPE},
 291  306          {NT_STATUS_PIPE_BUSY,                   EPIPE},
 292  307          {NT_STATUS_PIPE_CONNECTED,              EISCONN},
 293  308          {NT_STATUS_PIPE_DISCONNECTED,           EPIPE},
 294  309          {NT_STATUS_PIPE_NOT_AVAILABLE,          EBUSY},
 295  310          {NT_STATUS_PORT_CONNECTION_REFUSED,     ECONNREFUSED},
      311 +        {NT_STATUS_PORT_DISCONNECTED,           EBADF},
 296  312          {NT_STATUS_PORT_MESSAGE_TOO_LONG,       EMSGSIZE},
 297  313          {NT_STATUS_PORT_UNREACHABLE,            EHOSTUNREACH},
 298  314          {NT_STATUS_PROTOCOL_UNREACHABLE,        ENOPROTOOPT},
 299  315          {NT_STATUS_QUOTA_EXCEEDED,              EDQUOT},
 300      -        {NT_STATUS_RANGE_NOT_LOCKED,            EIO},
      316 +        {NT_STATUS_RANGE_NOT_LOCKED,            EAGAIN}, /* like F_SETLK */
 301  317          {NT_STATUS_REGISTRY_QUOTA_LIMIT,        EDQUOT},
 302  318          {NT_STATUS_REMOTE_DISCONNECT,           ESHUTDOWN},
 303  319          {NT_STATUS_REMOTE_NOT_LISTENING,        ECONNREFUSED},
 304  320          {NT_STATUS_REQUEST_NOT_ACCEPTED,        EACCES},
 305  321          {NT_STATUS_RETRY,                       EAGAIN},
 306  322          {NT_STATUS_SHARING_VIOLATION,           EBUSY},
 307  323          {NT_STATUS_TIMER_NOT_CANCELED,          ETIME},
 308  324          {NT_STATUS_TOO_MANY_LINKS,              EMLINK},
 309  325          {NT_STATUS_TOO_MANY_OPENED_FILES,       EMFILE},
      326 +        {NT_STATUS_TRUSTED_DOMAIN_FAILURE,      EAUTH},
      327 +        {NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE, EAUTH},
 310  328          {NT_STATUS_UNABLE_TO_FREE_VM,           EADDRINUSE},
 311  329          {NT_STATUS_UNSUCCESSFUL,                EINVAL},
 312      -        {NT_STATUS_WRONG_PASSWORD,              EACCES},
      330 +        {NT_STATUS_WRONG_PASSWORD,              EAUTH},
 313  331          {0,     0}
 314  332  };
 315  333  
 316  334  /*
 317  335   * Table for converting NT STATUS values to DOS class/code.
 318  336   * Rows ordered by integer value of last column (NT STATUS)
 319  337   */
 320  338  typedef struct nt2doserr {
 321  339          unsigned short dclass;
 322  340          unsigned short derr;
↓ open down ↓ 263 lines elided ↑ open up ↑
 586  604          {ERRHRD,        ERRgeneral,     NT_STATUS_LOGON_SESSION_COLLISION},
 587  605          {ERRDOS, ERROR_FILENAME_EXCED_RANGE,    NT_STATUS_NAME_TOO_LONG},
 588  606          {ERRDOS, NERR_OpenFiles,        NT_STATUS_FILES_OPEN},
 589  607          {ERRDOS, NERR_DevInUse,         NT_STATUS_CONNECTION_IN_USE},
 590  608          {ERRHRD,        ERRgeneral,     NT_STATUS_MESSAGE_NOT_FOUND},
 591  609          {ERRDOS,        ERRnoaccess,    NT_STATUS_PROCESS_IS_TERMINATING},
 592  610          {ERRHRD,        ERRgeneral,     NT_STATUS_INVALID_LOGON_TYPE},
 593  611          {ERRHRD,        ERRgeneral,     NT_STATUS_NO_GUID_TRANSLATION},
 594  612          {ERRHRD,        ERRgeneral,     NT_STATUS_CANNOT_IMPERSONATE},
 595  613          {ERRHRD,        ERRgeneral,     NT_STATUS_IMAGE_ALREADY_LOADED},
 596      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_NOT_PRESENT},
 597      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_LID_NOT_EXIST},
 598      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_LID_ALREADY_OWNED},
 599      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_NOT_LID_OWNER},
 600      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_INVALID_COMMAND},
 601      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_INVALID_LID},
 602      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_SELECTOR_NOT_AVAILABLE},
 603      -        {ERRHRD,        ERRgeneral,     NT_STATUS_ABIOS_INVALID_SELECTOR},
 604  614          {ERRHRD,        ERRgeneral,     NT_STATUS_NO_LDT},
 605  615          {ERRHRD,        ERRgeneral,     NT_STATUS_INVALID_LDT_SIZE},
 606  616          {ERRHRD,        ERRgeneral,     NT_STATUS_INVALID_LDT_OFFSET},
 607  617          {ERRHRD,        ERRgeneral,     NT_STATUS_INVALID_LDT_DESCRIPTOR},
 608  618          {ERRDOS, ERROR_BAD_EXE_FORMAT,  NT_STATUS_INVALID_IMAGE_NE_FORMAT},
 609  619          {ERRHRD,        ERRgeneral,     NT_STATUS_RXACT_INVALID_STATE},
 610  620          {ERRHRD,        ERRgeneral,     NT_STATUS_RXACT_COMMIT_FAILURE},
 611  621          {ERRHRD,        ERRgeneral,     NT_STATUS_MAPPED_FILE_SIZE_ZERO},
 612  622          {ERRDOS,        ERRnofids,      NT_STATUS_TOO_MANY_OPENED_FILES},
 613  623          {ERRHRD,        ERRgeneral,     NT_STATUS_CANCELLED},
↓ open down ↓ 247 lines elided ↑ open up ↑
 861  871          smb_errmsg(unknown_err_logpri, "smb_maperr32",
 862  872              "No direct map for 32 bit server error (0x%x)\n", nterr);
 863  873  
 864  874          /* ok, then try mapping to dos to unix */
 865  875          for (nt2d = nt2doserr; nt2d->nterr; nt2d++)
 866  876                  if (nt2d->nterr == nterr)
 867  877                          return (smb_maperror(nt2d->dclass, nt2d->derr));
 868  878          return (EIO);
 869  879  }
 870  880  
      881 +uint_t
      882 +smb_doserr2status(int dclass, int derr)
      883 +{
      884 +        const nt2doserr_t *nt2d;
 871  885  
      886 +        if (dclass == 0 && derr == 0)
      887 +                return (0);
      888 +
      889 +        for (nt2d = nt2doserr; nt2d->nterr; nt2d++)
      890 +                if (nt2d->dclass == dclass && nt2d->derr == derr)
      891 +                        return (nt2d->nterr);
      892 +        return (NT_STATUS_UNSUCCESSFUL);
      893 +}
      894 +
 872  895  int
 873  896  smb_maperror(int eclass, int eno)
 874  897  {
 875  898          if (eclass == 0 && eno == 0)
 876  899                  return (0);
 877  900          switch (eclass) {
 878  901          case ERRDOS:
 879  902                  switch (eno) {
 880  903                  case ERROR_INVALID_LEVEL:
 881  904                          return (ENOTSUP);
↓ open down ↓ 107 lines elided ↑ open up ↑
 989 1012                  case ERRgeneral:
 990 1013                          return (EIO);
 991 1014                  }
 992 1015          }
 993 1016  
 994 1017          smb_errmsg(unknown_err_logpri, "smb_maperror",
 995 1018              "Unknown DOS error %d/%d\n", eclass, eno);
 996 1019          return (EIO);
 997 1020  }
 998 1021  
 999      -#if defined(NOICONVSUPPORT) || defined(lint)
1000      -extern int iconv_conv(void *handle, const char **inbuf,
1001      -    size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
1002      -#endif
1003      -
1004 1022  #define SMALL_CONV 256
1005 1023  
     1024 +/*
     1025 + * Decode an SMB OTW string (Unicode or OEM chars)
     1026 + * converting to UTF-8 in the output buffer.
     1027 + * outlen is in/out (max size on input)
     1028 + * insize is the wire size (2 * chars if unicode)
     1029 + * The output string is null terminated.
     1030 + * Output length does not include the null.
     1031 + */
     1032 +int
     1033 +smb_get_dstring(struct mdchain *mdc, struct smb_vc *vcp,
     1034 +        char *outbuf, size_t *outlen, int insize)
     1035 +{
     1036 +        uint16_t convbuf[SMALL_CONV];
     1037 +        uint16_t *cbuf;
     1038 +        size_t cbufalloc, inlen, outsize;
     1039 +        int error;
     1040 +
     1041 +        if (insize <= 0)
     1042 +                return (0);
     1043 +        /* Note: inlen is UTF-16 symbols. */
     1044 +        inlen = insize / 2;
     1045 +
     1046 +        if (*outlen < 2)
     1047 +                return (EINVAL);
     1048 +        outsize = *outlen - 1; /* room for null */
     1049 +
     1050 +        /*
     1051 +         * Get a buffer for the conversion and fill it.
     1052 +         * Use stack buffer if the string is
     1053 +         * small enough, else allocate.
     1054 +         */
     1055 +        if (insize < sizeof (convbuf)) {
     1056 +                cbufalloc = 0;
     1057 +                cbuf = convbuf;
     1058 +        } else {
     1059 +                cbufalloc = insize + 2;
     1060 +                cbuf = kmem_alloc(cbufalloc, KM_SLEEP);
     1061 +        }
     1062 +        error = md_get_mem(mdc, cbuf, insize, MB_MSYSTEM);
     1063 +        if (error != 0)
     1064 +                goto out;
     1065 +        cbuf[inlen] = 0;
     1066 +
     1067 +        /*
     1068 +         * Handle the easy case (non-unicode).
     1069 +         * XXX: Technically, we should convert
     1070 +         * the string to OEM codeset first...
     1071 +         * Modern servers all use Unicode, so
     1072 +         * this is good enough.
     1073 +         */
     1074 +        if (SMB_UNICODE_STRINGS(vcp) == 0) {
     1075 +                *outlen = strlcpy(outbuf, (char *)cbuf, outsize);
     1076 +                if (*outlen > outsize) {
     1077 +                        *outlen = outsize;
     1078 +                        error = E2BIG;
     1079 +                }
     1080 +        } else {
     1081 +                /*
     1082 +                 * Convert from UTF-16 to UTF-8
     1083 +                 */
     1084 +                error = uconv_u16tou8(cbuf, &inlen,
     1085 +                    (uchar_t *)outbuf, outlen,
     1086 +                    UCONV_IN_LITTLE_ENDIAN);
     1087 +                if (error == 0) {
     1088 +                        outbuf[*outlen] = '\0';
     1089 +                }
     1090 +        }
     1091 +
     1092 +        ASSERT(*outlen == strlen(outbuf));
     1093 +
     1094 +out:
     1095 +        if (cbufalloc != 0)
     1096 +                kmem_free(cbuf, cbufalloc);
     1097 +
     1098 +        return (error);
     1099 +}
     1100 +
     1101 +/*
     1102 + * It's surprising that this function does utf8-ucs2 conversion.
     1103 + * One would expect only smb_put_dstring to do that.
     1104 + * Fixing that will require changing a bunch of callers. XXX
     1105 + */
1006 1106  /*ARGSUSED*/
1007 1107  int
1008 1108  smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
1009 1109          int size, int caseopt, int *lenp)
1010 1110  {
1011 1111          uint16_t convbuf[SMALL_CONV];
1012 1112          uint16_t *cbuf;
1013 1113          size_t cbufalloc, inlen, outlen;
1014 1114          int error;
1015 1115  
↓ open down ↓ 56 lines elided ↑ open up ↑
1072 1172          /*
1073 1173           * Let smb_put_dmem put both the string
1074 1174           * and the terminating null.
1075 1175           */
1076 1176          len = strlen(src) + 1;
1077 1177          error = smb_put_dmem(mbp, vcp, src, len, caseopt, NULL);
1078 1178          if (error)
1079 1179                  return (error);
1080 1180  
1081 1181          return (error);
     1182 +}
     1183 +int
     1184 +smb_smb_ntcreate(struct smb_share *ssp, struct mbchain *name_mb,
     1185 +        uint32_t crflag, uint32_t req_acc, uint32_t efa, uint32_t sh_acc,
     1186 +        uint32_t disp, uint32_t createopt,  uint32_t impersonate,
     1187 +        struct smb_cred *scrp, smb_fh_t *fhp,
     1188 +        uint32_t *cr_act_p, struct smbfattr *fap)
     1189 +{
     1190 +        int err;
     1191 +
     1192 +        if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
     1193 +                err = smb2_smb_ntcreate(ssp, name_mb, NULL, NULL,
     1194 +                   crflag, req_acc, efa, sh_acc, disp, createopt,
     1195 +                   impersonate, scrp, &fhp->fh_fid2, cr_act_p, fap);
     1196 +        } else {
     1197 +                err = smb1_smb_ntcreate(ssp, name_mb, crflag, req_acc,
     1198 +                    efa, sh_acc, disp, createopt,  impersonate, scrp,
     1199 +                    &fhp->fh_fid1, cr_act_p, fap);
     1200 +        }
     1201 +        return (err);
     1202 +}
     1203 +
     1204 +int
     1205 +smb_smb_close(struct smb_share *ssp, smb_fh_t *fhp,
     1206 +        struct smb_cred *scrp)
     1207 +{
     1208 +        int err;
     1209 +
     1210 +        if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
     1211 +                err = smb2_smb_close(ssp, &fhp->fh_fid2, scrp);
     1212 +        } else {
     1213 +                err = smb1_smb_close(ssp, fhp->fh_fid1, NULL, scrp);
     1214 +        }
     1215 +
     1216 +        return (err);
     1217 +}
     1218 +
     1219 +/*
     1220 + * Largest size to use with LARGE_READ/LARGE_WRITE.
     1221 + * Specs say up to 64k data bytes, but Windows traffic
     1222 + * uses 60k... no doubt for some good reason.
     1223 + * (Probably to keep 4k block alignment.)
     1224 + */
     1225 +uint32_t smb1_large_io_max = (60*1024);
     1226 +
     1227 +/*
     1228 + * Common function for read/write with UIO.
     1229 + * Called by netsmb smb_usr_rw,
     1230 + *  smbfs_readvnode, smbfs_writevnode
     1231 + */
     1232 +int
     1233 +smb_rwuio(smb_fh_t *fhp, uio_rw_t rw,
     1234 +        uio_t *uiop, smb_cred_t *scred, int timo)
     1235 +{
     1236 +        struct smb_share *ssp = FHTOSS(fhp);
     1237 +        struct smb_vc *vcp = SSTOVC(ssp);
     1238 +        ssize_t  save_resid;
     1239 +        uint32_t len, rlen, maxlen;
     1240 +        int error = 0;
     1241 +        int (*iofun)(smb_fh_t *, uint32_t *,
     1242 +            uio_t *, smb_cred_t *, int);
     1243 +
     1244 +        /* After reconnect, the fid is invalid. */
     1245 +        if (fhp->fh_vcgenid != ssp->ss_vcgenid)
     1246 +                return (ESTALE);
     1247 +
     1248 +        if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
     1249 +                if (rw == UIO_READ) {
     1250 +                        iofun = smb2_smb_read;
     1251 +                        maxlen = vcp->vc_sopt.sv2_maxread;
     1252 +                } else { /* UIO_WRITE */
     1253 +                        iofun = smb2_smb_write;
     1254 +                        maxlen = vcp->vc_sopt.sv2_maxwrite;
     1255 +                }
     1256 +        } else {
     1257 +                /*
     1258 +                 * Using NT LM 0.12, so readx, writex.
     1259 +                 * Make sure we can represent the offset.
     1260 +                 */
     1261 +                if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_FILES) == 0 &&
     1262 +                    (uiop->uio_loffset + uiop->uio_resid) > UINT32_MAX)
     1263 +                        return (EFBIG);
     1264 +
     1265 +                if (rw == UIO_READ) {
     1266 +                        iofun = smb_smb_readx;
     1267 +                        if (vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX)
     1268 +                                maxlen = smb1_large_io_max;
     1269 +                        else
     1270 +                                maxlen = vcp->vc_rxmax;
     1271 +                } else { /* UIO_WRITE */
     1272 +                        iofun = smb_smb_writex;
     1273 +                        if (vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX)
     1274 +                                maxlen = smb1_large_io_max;
     1275 +                        else
     1276 +                                maxlen = vcp->vc_wxmax;
     1277 +                }
     1278 +        }
     1279 +
     1280 +        save_resid = uiop->uio_resid;
     1281 +        while (uiop->uio_resid > 0) {
     1282 +                /* Lint: uio_resid may be 64-bits */
     1283 +                rlen = len = (uint32_t)min(maxlen, uiop->uio_resid);
     1284 +                error = (*iofun)(fhp, &rlen, uiop, scred, timo);
     1285 +
     1286 +                /*
     1287 +                 * Note: the iofun called uio_update, so
     1288 +                 * not doing that here as one might expect.
     1289 +                 *
     1290 +                 * Quit the loop either on error, or if we
     1291 +                 * transferred less then requested.
     1292 +                 */
     1293 +                if (error || (rlen < len))
     1294 +                        break;
     1295 +
     1296 +                timo = 0; /* only first I/O should wait */
     1297 +        }
     1298 +        if (error && (save_resid != uiop->uio_resid)) {
     1299 +                /*
     1300 +                 * Stopped on an error after having
     1301 +                 * successfully transferred data.
     1302 +                 * Suppress this error.
     1303 +                 */
     1304 +                SMBSDEBUG("error %d suppressed\n", error);
     1305 +                error = 0;
     1306 +        }
     1307 +
     1308 +        return (error);
1082 1309  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX