1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
24 */
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/uio.h>
29 #include <sys/statvfs.h>
30 #include <sys/vnode.h>
31 #include <sys/thread.h>
32 #include <sys/pathname.h>
33 #include <sys/cred.h>
34 #include <sys/extdirent.h>
35 #include <sys/nbmlock.h>
36 #include <sys/share.h>
37 #include <sys/fcntl.h>
38 #include <nfs/lm.h>
39
40 #include <smbsrv/smb_kproto.h>
41 #include <smbsrv/string.h>
42 #include <smbsrv/smb_vops.h>
43 #include <smbsrv/smb_fsops.h>
44
45 /*
46 * CATIA support
47 *
48 * CATIA V4 is a UNIX product and uses characters in filenames that
49 * are considered invalid by Windows. CATIA V5 is available on both
50 * UNIX and Windows. Thus, as CATIA customers migrate from V4 to V5,
51 * some V4 files could become inaccessible to windows clients if the
52 * filename contains the characters that are considered illegal in
53 * Windows. In order to address this issue an optional character
54 * translation is applied to filenames at the smb_vop interface.
55 *
56 * Character Translation Table
57 * ----------------------------------
58 * Unix-char (v4) | Windows-char (v5)
59 * ----------------------------------
60 * * | 0x00a4 Currency Sign
61 * | | 0x00a6 Broken Bar
62 * " | 0x00a8 Diaeresis
63 * < | 0x00ab Left-Pointing Double Angle Quotation Mark
64 * > | 0x00bb Right-Pointing Double Angle Quotation Mark
65 * ? | 0x00bf Inverted Question mark
66 * : | 0x00f7 Division Sign
67 * / | 0x00f8 Latin Small Letter o with stroke
68 * \ | 0x00ff Latin Small Letter Y with Diaeresis
69 *
70 *
71 * Two lookup tables are used to perform the character translation:
72 *
73 * smb_catia_v5_lookup - provides the mapping between UNIX ASCII (v4)
74 * characters and equivalent or translated wide characters.
75 * It is indexed by the decimal value of the ASCII character (0-127).
76 *
77 * smb_catia_v4_lookup - provides the mapping between wide characters
78 * in the range from 0x00A4 to 0x00FF and their UNIX (v4) equivalent
79 * (in wide character format). It is indexed by the decimal value of
80 * the wide character (164-255) with an offset of -164.
81 * If this translation produces a filename containing a '/' create, mkdir
82 * or rename (to the '/' name) operations will not be permitted. It is
83 * not valid to create a filename with a '/' in it. However, if such a
84 * file already exists other operations (e.g, lookup, delete, rename)
85 * are permitted on it.
86 */
87
88 /* number of characters mapped */
89 #define SMB_CATIA_NUM_MAPS 9
90
91 /* Windows Characters used in special character mapping */
92 #define SMB_CATIA_WIN_CURRENCY 0x00a4
93 #define SMB_CATIA_WIN_BROKEN_BAR 0x00a6
94 #define SMB_CATIA_WIN_DIAERESIS 0x00a8
95 #define SMB_CATIA_WIN_LEFT_ANGLE 0x00ab
96 #define SMB_CATIA_WIN_RIGHT_ANGLE 0x00bb
97 #define SMB_CATIA_WIN_INVERTED_QUESTION 0x00bf
98 #define SMB_CATIA_WIN_DIVISION 0x00f7
99 #define SMB_CATIA_WIN_LATIN_O 0x00f8
100 #define SMB_CATIA_WIN_LATIN_Y 0x00ff
101
102 #define SMB_CATIA_V4_LOOKUP_LOW SMB_CATIA_WIN_CURRENCY
103 #define SMB_CATIA_V4_LOOKUP_UPPER SMB_CATIA_WIN_LATIN_Y
104 #define SMB_CATIA_V4_LOOKUP_MAX \
105 (SMB_CATIA_V4_LOOKUP_UPPER - SMB_CATIA_V4_LOOKUP_LOW + 1)
106 #define SMB_CATIA_V5_LOOKUP_MAX 0x0080
107
108 typedef struct smb_catia_map
109 {
110 unsigned char unixchar; /* v4 */
111 smb_wchar_t winchar; /* v5 */
112 } smb_catia_map_t;
113
114 smb_catia_map_t const catia_maps[SMB_CATIA_NUM_MAPS] =
115 {
116 {'"', SMB_CATIA_WIN_DIAERESIS},
117 {'*', SMB_CATIA_WIN_CURRENCY},
118 {':', SMB_CATIA_WIN_DIVISION},
119 {'<', SMB_CATIA_WIN_LEFT_ANGLE},
120 {'>', SMB_CATIA_WIN_RIGHT_ANGLE},
121 {'?', SMB_CATIA_WIN_INVERTED_QUESTION},
122 {'\\', SMB_CATIA_WIN_LATIN_Y},
123 {'/', SMB_CATIA_WIN_LATIN_O},
124 {'|', SMB_CATIA_WIN_BROKEN_BAR}
125 };
126
127 static smb_wchar_t smb_catia_v5_lookup[SMB_CATIA_V5_LOOKUP_MAX];
128 static smb_wchar_t smb_catia_v4_lookup[SMB_CATIA_V4_LOOKUP_MAX];
129
130 static void smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr);
131 static void smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp);
132 static callb_cpr_t *smb_lock_frlock_callback(flk_cb_when_t, void *);
133 static void smb_vop_catia_init();
134
135 extern sysid_t lm_alloc_sysidt();
136
137 #define SMB_AT_MAX 16
138 static const uint_t smb_attrmap[SMB_AT_MAX] = {
139 0,
140 AT_TYPE,
141 AT_MODE,
142 AT_UID,
143 AT_GID,
144 AT_FSID,
145 AT_NODEID,
146 AT_NLINK,
147 AT_SIZE,
148 AT_ATIME,
149 AT_MTIME,
150 AT_CTIME,
151 AT_RDEV,
152 AT_BLKSIZE,
153 AT_NBLOCKS,
154 AT_SEQ
155 };
156
157 static boolean_t smb_vop_initialized = B_FALSE;
158 caller_context_t smb_ct;
159
160 /*
161 * smb_vop_init
162 *
163 * This function is not multi-thread safe. The caller must make sure only one
164 * thread makes the call.
165 */
166 int
smb_vop_init(void)167 smb_vop_init(void)
168 {
169 if (smb_vop_initialized)
170 return (0);
171 /*
172 * The caller_context will be used primarily for range locking.
173 * Since the CIFS server is mapping its locks to POSIX locks,
174 * only one pid is used for operations originating from the
175 * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
176 *
177 * XXX: Should smb_ct be per-zone?
178 */
179 smb_ct.cc_sysid = lm_alloc_sysidt();
180 if (smb_ct.cc_sysid == LM_NOSYSID)
181 return (ENOMEM);
182
183 smb_ct.cc_caller_id = fs_new_caller_id();
184 smb_ct.cc_pid = IGN_PID;
185 smb_ct.cc_flags = 0;
186 smb_vop_catia_init();
187
188 smb_vop_initialized = B_TRUE;
189 return (0);
190 }
191
192 /*
193 * smb_vop_fini
194 *
195 * This function is not multi-thread safe. The caller must make sure only one
196 * thread makes the call.
197 */
198 void
smb_vop_fini(void)199 smb_vop_fini(void)
200 {
201 if (!smb_vop_initialized)
202 return;
203
204 lm_free_sysidt(smb_ct.cc_sysid);
205 smb_ct.cc_pid = IGN_PID;
206 smb_ct.cc_sysid = LM_NOSYSID;
207 smb_vop_initialized = B_FALSE;
208 }
209
210 /*
211 * The smb_ct will be used primarily for range locking.
212 * Since the CIFS server is mapping its locks to POSIX locks,
213 * only one pid is used for operations originating from the
214 * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
215 */
216 int
smb_vop_open(vnode_t ** vpp,int mode,cred_t * cred)217 smb_vop_open(vnode_t **vpp, int mode, cred_t *cred)
218 {
219 return (VOP_OPEN(vpp, mode, cred, &smb_ct));
220 }
221
222 void
smb_vop_close(vnode_t * vp,int mode,cred_t * cred)223 smb_vop_close(vnode_t *vp, int mode, cred_t *cred)
224 {
225 (void) VOP_CLOSE(vp, mode, 1, (offset_t)0, cred, &smb_ct);
226 }
227
228 int
smb_vop_other_opens(vnode_t * vp,int mode)229 smb_vop_other_opens(vnode_t *vp, int mode)
230 {
231 return (((mode & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
232 (((mode & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
233 ((mode & FREAD) && vn_has_other_opens(vp, V_READ)) ||
234 (((mode & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
235 vn_is_mapped(vp, V_RDORWR));
236 }
237
238 /*
239 * The smb_vop_* functions have minimal knowledge of CIFS semantics and
240 * serve as an interface to the VFS layer.
241 *
242 * Only smb_fsop_* layer functions should call smb_vop_* layer functions.
243 * (Higher-level CIFS service code should never skip the smb_fsop_* layer
244 * to call smb_vop_* layer functions directly.)
245 */
246
247 /*
248 * XXX - Extended attributes support in the file system assumed.
249 * This is needed for full NT Streams functionality.
250 */
251
252 int
smb_vop_read(vnode_t * vp,uio_t * uiop,cred_t * cr)253 smb_vop_read(vnode_t *vp, uio_t *uiop, cred_t *cr)
254 {
255 int error;
256
257 (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
258 error = VOP_READ(vp, uiop, 0, cr, &smb_ct);
259 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
260 return (error);
261 }
262
263 int
smb_vop_write(vnode_t * vp,uio_t * uiop,int ioflag,uint32_t * lcount,cred_t * cr)264 smb_vop_write(vnode_t *vp, uio_t *uiop, int ioflag, uint32_t *lcount,
265 cred_t *cr)
266 {
267 int error;
268
269 *lcount = uiop->uio_resid;
270
271 uiop->uio_llimit = MAXOFFSET_T;
272
273 (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
274 error = VOP_WRITE(vp, uiop, ioflag, cr, &smb_ct);
275 VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
276
277 *lcount -= uiop->uio_resid;
278
279 return (error);
280 }
281
282 /*
283 * smb_vop_getattr()
284 *
285 * smb_fsop_getattr()/smb_vop_getattr() should always be called from the CIFS
286 * service (instead of calling VOP_GETATTR directly) to retrieve attributes
287 * due to special processing needed for streams files.
288 *
289 * All attributes are retrieved.
290 *
291 * When vp denotes a named stream, then unnamed_vp should be passed in (denoting
292 * the corresponding unnamed stream).
293 * A named stream's attributes (as far as CIFS is concerned) are those of the
294 * unnamed stream (minus the size attribute, and the type), plus the size of
295 * the named stream, and a type value of VREG.
296 * Although the file system may store other attributes with the named stream,
297 * these should not be used by CIFS for any purpose.
298 *
299 * File systems without VFSFT_XVATTR do not support DOS attributes or create
300 * time (crtime). In this case the mtime is used as the crtime.
301 * Likewise if VOP_GETATTR doesn't return any system attributes the dosattr
302 * is 0 and the mtime is used as the crtime.
303 */
304 int
smb_vop_getattr(vnode_t * vp,vnode_t * unnamed_vp,smb_attr_t * ret_attr,int flags,cred_t * cr)305 smb_vop_getattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *ret_attr,
306 int flags, cred_t *cr)
307 {
308 int error;
309 vnode_t *use_vp;
310 smb_attr_t tmp_attr;
311 xvattr_t tmp_xvattr;
312 xoptattr_t *xoap = NULL;
313
314 if (unnamed_vp)
315 use_vp = unnamed_vp;
316 else
317 use_vp = vp;
318
319 if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
320 xva_init(&tmp_xvattr);
321 xoap = xva_getxoptattr(&tmp_xvattr);
322 ASSERT(xoap);
323
324 smb_sa_to_va_mask(ret_attr->sa_mask,
325 &tmp_xvattr.xva_vattr.va_mask);
326
327 XVA_SET_REQ(&tmp_xvattr, XAT_READONLY);
328 XVA_SET_REQ(&tmp_xvattr, XAT_HIDDEN);
329 XVA_SET_REQ(&tmp_xvattr, XAT_SYSTEM);
330 XVA_SET_REQ(&tmp_xvattr, XAT_ARCHIVE);
331 XVA_SET_REQ(&tmp_xvattr, XAT_CREATETIME);
332 XVA_SET_REQ(&tmp_xvattr, XAT_REPARSE);
333 XVA_SET_REQ(&tmp_xvattr, XAT_OFFLINE);
334 XVA_SET_REQ(&tmp_xvattr, XAT_SPARSE);
335
336 error = VOP_GETATTR(use_vp, &tmp_xvattr.xva_vattr, flags,
337 cr, &smb_ct);
338 if (error != 0)
339 return (error);
340
341 ret_attr->sa_vattr = tmp_xvattr.xva_vattr;
342 ret_attr->sa_dosattr = 0;
343
344 if (tmp_xvattr.xva_vattr.va_mask & AT_XVATTR) {
345 xoap = xva_getxoptattr(&tmp_xvattr);
346 ASSERT(xoap);
347
348 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_READONLY)) &&
349 (xoap->xoa_readonly)) {
350 ret_attr->sa_dosattr |= FILE_ATTRIBUTE_READONLY;
351 }
352
353 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_HIDDEN)) &&
354 (xoap->xoa_hidden)) {
355 ret_attr->sa_dosattr |= FILE_ATTRIBUTE_HIDDEN;
356 }
357
358 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_SYSTEM)) &&
359 (xoap->xoa_system)) {
360 ret_attr->sa_dosattr |= FILE_ATTRIBUTE_SYSTEM;
361 }
362
363 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_ARCHIVE)) &&
364 (xoap->xoa_archive)) {
365 ret_attr->sa_dosattr |= FILE_ATTRIBUTE_ARCHIVE;
366 }
367
368 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_REPARSE)) &&
369 (xoap->xoa_reparse)) {
370 ret_attr->sa_dosattr |=
371 FILE_ATTRIBUTE_REPARSE_POINT;
372 }
373
374 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_OFFLINE)) &&
375 (xoap->xoa_offline)) {
376 ret_attr->sa_dosattr |= FILE_ATTRIBUTE_OFFLINE;
377 }
378
379 if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_SPARSE)) &&
380 (xoap->xoa_sparse)) {
381 ret_attr->sa_dosattr |=
382 FILE_ATTRIBUTE_SPARSE_FILE;
383 }
384
385 ret_attr->sa_crtime = xoap->xoa_createtime;
386 } else {
387 ret_attr->sa_crtime = ret_attr->sa_vattr.va_mtime;
388 }
389 } else {
390 /*
391 * Support for file systems without VFSFT_XVATTR
392 */
393 smb_sa_to_va_mask(ret_attr->sa_mask,
394 &ret_attr->sa_vattr.va_mask);
395
396 error = VOP_GETATTR(use_vp, &ret_attr->sa_vattr,
397 flags, cr, &smb_ct);
398 if (error != 0)
399 return (error);
400
401 ret_attr->sa_dosattr = 0;
402 ret_attr->sa_crtime = ret_attr->sa_vattr.va_mtime;
403 }
404
405 if (unnamed_vp) {
406 ret_attr->sa_vattr.va_type = VREG;
407
408 if (ret_attr->sa_mask & (SMB_AT_SIZE | SMB_AT_NBLOCKS)) {
409 tmp_attr.sa_vattr.va_mask = AT_SIZE | AT_NBLOCKS;
410
411 error = VOP_GETATTR(vp, &tmp_attr.sa_vattr,
412 flags, cr, &smb_ct);
413 if (error != 0)
414 return (error);
415
416 ret_attr->sa_vattr.va_size = tmp_attr.sa_vattr.va_size;
417 ret_attr->sa_vattr.va_nblocks =
418 tmp_attr.sa_vattr.va_nblocks;
419 }
420 }
421
422 if (ret_attr->sa_vattr.va_type == VDIR)
423 ret_attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
424
425 return (error);
426 }
427
428 /*
429 * smb_vop_setattr()
430 *
431 * smb_fsop_setattr()/smb_vop_setattr() should always be used instead of
432 * VOP_SETATTR() when calling from the CIFS service, due to special processing
433 * for streams files.
434 *
435 * Streams have a size but otherwise do not have separate attributes from
436 * the (unnamed stream) file, i.e., the security and ownership of the file
437 * applies to the stream. In contrast, extended attribute files, which are
438 * used to implement streams, are independent objects with their own
439 * attributes.
440 *
441 * For compatibility with streams, we set the size on the extended attribute
442 * file and apply other attributes to the (unnamed stream) file. The one
443 * exception is that the UID and GID can be set on the stream by passing a
444 * NULL unnamed_vp, which allows callers to synchronize stream ownership
445 * with the (unnamed stream) file.
446 */
447 int
smb_vop_setattr(vnode_t * vp,vnode_t * unnamed_vp,smb_attr_t * attr,int flags,cred_t * cr)448 smb_vop_setattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *attr,
449 int flags, cred_t *cr)
450 {
451 int error = 0;
452 int at_size = 0;
453 vnode_t *use_vp;
454 xvattr_t xvattr;
455 vattr_t *vap;
456
457 if (attr->sa_mask & SMB_AT_DOSATTR) {
458 attr->sa_dosattr &=
459 (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY |
460 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM |
461 FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_SPARSE_FILE);
462 }
463
464 if (unnamed_vp) {
465 use_vp = unnamed_vp;
466 if (attr->sa_mask & SMB_AT_SIZE) {
467 at_size = 1;
468 attr->sa_mask &= ~SMB_AT_SIZE;
469 }
470 } else {
471 use_vp = vp;
472 }
473
474 /*
475 * The caller should not be setting sa_vattr.va_mask,
476 * but rather sa_mask.
477 */
478
479 attr->sa_vattr.va_mask = 0;
480
481 if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
482 smb_vop_setup_xvattr(attr, &xvattr);
483 vap = &xvattr.xva_vattr;
484 } else {
485 smb_sa_to_va_mask(attr->sa_mask,
486 &attr->sa_vattr.va_mask);
487 vap = &attr->sa_vattr;
488 }
489
490 if ((error = VOP_SETATTR(use_vp, vap, flags, cr, &smb_ct)) != 0)
491 return (error);
492
493 if (at_size) {
494 attr->sa_vattr.va_mask = AT_SIZE;
495 error = VOP_SETATTR(vp, &attr->sa_vattr, flags,
496 zone_kcred(), &smb_ct);
497 }
498
499 return (error);
500 }
501
502 /*
503 * smb_vop_access
504 *
505 * This is a wrapper round VOP_ACCESS. VOP_ACCESS checks the given mode
506 * against file's ACL or Unix permissions. CIFS on the other hand needs to
507 * know if the requested operation can succeed for the given object, this
508 * requires more checks in case of DELETE bit since permissions on the parent
509 * directory are important as well. Based on Windows rules if parent's ACL
510 * grant FILE_DELETE_CHILD a file can be delete regardless of the file's
511 * permissions.
512 */
513 int
smb_vop_access(vnode_t * vp,int mode,int flags,vnode_t * dir_vp,cred_t * cr)514 smb_vop_access(vnode_t *vp, int mode, int flags, vnode_t *dir_vp, cred_t *cr)
515 {
516 int error = 0;
517
518 if (mode == 0)
519 return (0);
520
521 if ((flags == V_ACE_MASK) && (mode & ACE_DELETE)) {
522 if (dir_vp) {
523 error = VOP_ACCESS(dir_vp, ACE_DELETE_CHILD, flags,
524 cr, NULL);
525
526 if (error == 0)
527 mode &= ~ACE_DELETE;
528 }
529 }
530
531 if (mode) {
532 error = VOP_ACCESS(vp, mode, flags, cr, NULL);
533 }
534
535 return (error);
536 }
537
538 /*
539 * smb_vop_lookup
540 *
541 * dvp: directory vnode (in)
542 * name: name of file to be looked up (in)
543 * vpp: looked-up vnode (out)
544 * od_name: on-disk name of file (out).
545 * This parameter is optional. If a pointer is passed in, it
546 * must be allocated with MAXNAMELEN bytes
547 * rootvp: vnode of the tree root (in)
548 * This parameter is always passed in non-NULL except at the time
549 * of share set up.
550 * direntflags: dirent flags returned from VOP_LOOKUP
551 */
552 int
smb_vop_lookup(vnode_t * dvp,char * name,vnode_t ** vpp,char * od_name,int flags,int * direntflags,vnode_t * rootvp,smb_attr_t * attr,cred_t * cr)553 smb_vop_lookup(
554 vnode_t *dvp,
555 char *name,
556 vnode_t **vpp,
557 char *od_name,
558 int flags,
559 int *direntflags,
560 vnode_t *rootvp,
561 smb_attr_t *attr,
562 cred_t *cr)
563 {
564 int error = 0;
565 int option_flags = 0;
566 pathname_t rpn;
567 char *np = name;
568 char namebuf[MAXNAMELEN];
569
570 if (*name == '\0')
571 return (EINVAL);
572
573 ASSERT(vpp);
574 *vpp = NULL;
575 *direntflags = 0;
576
577 if ((name[0] == '.') && (name[1] == '.') && (name[2] == 0)) {
578 if (rootvp && (dvp == rootvp)) {
579 VN_HOLD(dvp);
580 *vpp = dvp;
581 return (0);
582 }
583
584 if (dvp->v_flag & VROOT) {
585 vfs_t *vfsp;
586 vnode_t *cvp = dvp;
587
588 /*
589 * Set dvp and check for races with forced unmount
590 * (see lookuppnvp())
591 */
592
593 vfsp = cvp->v_vfsp;
594 vfs_rlock_wait(vfsp);
595 if (((dvp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
596 (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
597 vfs_unlock(vfsp);
598 return (EIO);
599 }
600 vfs_unlock(vfsp);
601 }
602 }
603
604 if (flags & SMB_IGNORE_CASE)
605 option_flags = FIGNORECASE;
606
607 if (flags & SMB_CATIA)
608 np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
609
610 pn_alloc(&rpn);
611
612 error = VOP_LOOKUP(dvp, np, vpp, NULL, option_flags, NULL, cr,
613 &smb_ct, direntflags, &rpn);
614
615 if (error == 0) {
616 if (od_name) {
617 bzero(od_name, MAXNAMELEN);
618 np = (option_flags == FIGNORECASE) ? rpn.pn_buf : name;
619
620 if (flags & SMB_CATIA)
621 smb_vop_catia_v4tov5(np, od_name, MAXNAMELEN);
622 else
623 (void) strlcpy(od_name, np, MAXNAMELEN);
624 }
625
626 if (attr != NULL) {
627 attr->sa_mask = SMB_AT_ALL;
628 (void) smb_vop_getattr(*vpp, NULL, attr, 0,
629 zone_kcred());
630 }
631 }
632
633 pn_free(&rpn);
634 return (error);
635 }
636
637 int
smb_vop_create(vnode_t * dvp,char * name,smb_attr_t * attr,vnode_t ** vpp,int flags,cred_t * cr,vsecattr_t * vsap)638 smb_vop_create(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
639 int flags, cred_t *cr, vsecattr_t *vsap)
640 {
641 int error;
642 int option_flags = 0;
643 xvattr_t xvattr;
644 vattr_t *vap;
645 char *np = name;
646 char namebuf[MAXNAMELEN];
647
648 if (flags & SMB_IGNORE_CASE)
649 option_flags = FIGNORECASE;
650
651 attr->sa_vattr.va_mask = 0;
652
653 if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
654 smb_vop_setup_xvattr(attr, &xvattr);
655 vap = &xvattr.xva_vattr;
656 } else {
657 smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
658 vap = &attr->sa_vattr;
659 }
660
661 if (flags & SMB_CATIA) {
662 np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
663 if (strchr(np, '/') != NULL)
664 return (EILSEQ);
665 }
666
667 error = VOP_CREATE(dvp, np, vap, EXCL, attr->sa_vattr.va_mode,
668 vpp, cr, option_flags, &smb_ct, vsap);
669
670 return (error);
671 }
672
673 int
smb_vop_remove(vnode_t * dvp,char * name,int flags,cred_t * cr)674 smb_vop_remove(vnode_t *dvp, char *name, int flags, cred_t *cr)
675 {
676 int error;
677 int option_flags = 0;
678 char *np = name;
679 char namebuf[MAXNAMELEN];
680
681 if (flags & SMB_IGNORE_CASE)
682 option_flags = FIGNORECASE;
683
684 if (flags & SMB_CATIA)
685 np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
686
687 error = VOP_REMOVE(dvp, np, cr, &smb_ct, option_flags);
688
689 return (error);
690 }
691
692 /*
693 * smb_vop_link(target-dir-vp, source-file-vp, target-name)
694 *
695 * Create a link - same tree (identical TID) only.
696 */
697 int
smb_vop_link(vnode_t * to_dvp,vnode_t * from_vp,char * to_name,int flags,cred_t * cr)698 smb_vop_link(vnode_t *to_dvp, vnode_t *from_vp, char *to_name,
699 int flags, cred_t *cr)
700 {
701 int option_flags = 0;
702 char *np, *buf;
703 int rc;
704
705 if (flags & SMB_IGNORE_CASE)
706 option_flags = FIGNORECASE;
707
708 if (flags & SMB_CATIA) {
709 buf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
710 np = smb_vop_catia_v5tov4(to_name, buf, MAXNAMELEN);
711 if (strchr(np, '/') != NULL) {
712 kmem_free(buf, MAXNAMELEN);
713 return (EILSEQ);
714 }
715
716 rc = VOP_LINK(to_dvp, from_vp, np, cr, &smb_ct, option_flags);
717 kmem_free(buf, MAXNAMELEN);
718 return (rc);
719 }
720
721 rc = VOP_LINK(to_dvp, from_vp, to_name, cr, &smb_ct, option_flags);
722 return (rc);
723 }
724
725 /*
726 * smb_vop_rename()
727 *
728 * The rename is for files in the same tree (identical TID) only.
729 */
730 int
smb_vop_rename(vnode_t * from_dvp,char * from_name,vnode_t * to_dvp,char * to_name,int flags,cred_t * cr)731 smb_vop_rename(vnode_t *from_dvp, char *from_name, vnode_t *to_dvp,
732 char *to_name, int flags, cred_t *cr)
733 {
734 int error;
735 int option_flags = 0;
736 char *from, *to, *fbuf, *tbuf;
737
738 if (flags & SMB_IGNORE_CASE)
739 option_flags = FIGNORECASE;
740
741 if (flags & SMB_CATIA) {
742 tbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
743 to = smb_vop_catia_v5tov4(to_name, tbuf, MAXNAMELEN);
744 if (strchr(to, '/') != NULL) {
745 kmem_free(tbuf, MAXNAMELEN);
746 return (EILSEQ);
747 }
748
749 fbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
750 from = smb_vop_catia_v5tov4(from_name, fbuf, MAXNAMELEN);
751
752 error = VOP_RENAME(from_dvp, from, to_dvp, to, cr,
753 &smb_ct, option_flags);
754
755 kmem_free(tbuf, MAXNAMELEN);
756 kmem_free(fbuf, MAXNAMELEN);
757 return (error);
758 }
759
760 error = VOP_RENAME(from_dvp, from_name, to_dvp, to_name, cr,
761 &smb_ct, option_flags);
762
763 return (error);
764 }
765
766 int
smb_vop_mkdir(vnode_t * dvp,char * name,smb_attr_t * attr,vnode_t ** vpp,int flags,cred_t * cr,vsecattr_t * vsap)767 smb_vop_mkdir(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
768 int flags, cred_t *cr, vsecattr_t *vsap)
769 {
770 int error;
771 int option_flags = 0;
772 xvattr_t xvattr;
773 vattr_t *vap;
774 char *np = name;
775 char namebuf[MAXNAMELEN];
776
777 if (flags & SMB_IGNORE_CASE)
778 option_flags = FIGNORECASE;
779
780 attr->sa_vattr.va_mask = 0;
781
782 if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
783 smb_vop_setup_xvattr(attr, &xvattr);
784 vap = &xvattr.xva_vattr;
785 } else {
786 smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
787 vap = &attr->sa_vattr;
788 }
789
790 if (flags & SMB_CATIA) {
791 np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
792 if (strchr(np, '/') != NULL)
793 return (EILSEQ);
794 }
795
796 error = VOP_MKDIR(dvp, np, vap, vpp, cr, &smb_ct, option_flags, vsap);
797
798 return (error);
799 }
800
801 /*
802 * smb_vop_rmdir()
803 *
804 * Only simple rmdir supported, consistent with NT semantics
805 * (can only remove an empty directory).
806 *
807 * The third argument to VOP_RMDIR is the current directory of
808 * the process. It allows rmdir wants to EINVAL if one tries to
809 * remove ".". Since SMB servers do not know what their clients'
810 * current directories are, we fake it by supplying a vnode known
811 * to exist and illegal to remove (rootdir).
812 */
813 int
smb_vop_rmdir(vnode_t * dvp,char * name,int flags,cred_t * cr)814 smb_vop_rmdir(vnode_t *dvp, char *name, int flags, cred_t *cr)
815 {
816 int error;
817 int option_flags = 0;
818 char *np = name;
819 char namebuf[MAXNAMELEN];
820
821 if (flags & SMB_IGNORE_CASE)
822 option_flags = FIGNORECASE;
823
824 if (flags & SMB_CATIA)
825 np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
826
827 error = VOP_RMDIR(dvp, np, rootdir, cr, &smb_ct, option_flags);
828 return (error);
829 }
830
831 int
smb_vop_commit(vnode_t * vp,cred_t * cr)832 smb_vop_commit(vnode_t *vp, cred_t *cr)
833 {
834 return (VOP_FSYNC(vp, 1, cr, &smb_ct));
835 }
836
837 /*
838 * Some code in smb_node.c needs to know which DOS attributes
839 * we can actually store. Let's define a mask here of all the
840 * DOS attribute flags supported by the following function.
841 */
842 const uint32_t
843 smb_vop_dosattr_settable =
844 FILE_ATTRIBUTE_ARCHIVE |
845 FILE_ATTRIBUTE_SYSTEM |
846 FILE_ATTRIBUTE_HIDDEN |
847 FILE_ATTRIBUTE_READONLY |
848 FILE_ATTRIBUTE_OFFLINE |
849 FILE_ATTRIBUTE_SPARSE_FILE;
850
851 static void
smb_vop_setup_xvattr(smb_attr_t * smb_attr,xvattr_t * xvattr)852 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr)
853 {
854 xoptattr_t *xoap = NULL;
855 uint_t xva_mask;
856
857 /*
858 * Initialize xvattr, including bzero
859 */
860 xva_init(xvattr);
861 xoap = xva_getxoptattr(xvattr);
862
863 ASSERT(xoap);
864
865 /*
866 * Copy caller-specified classic attributes to xvattr.
867 * First save xvattr's mask (set in xva_init()), which
868 * contains AT_XVATTR. This is |'d in later if needed.
869 */
870
871 xva_mask = xvattr->xva_vattr.va_mask;
872 xvattr->xva_vattr = smb_attr->sa_vattr;
873
874 smb_sa_to_va_mask(smb_attr->sa_mask, &xvattr->xva_vattr.va_mask);
875
876 /*
877 * Do not set ctime (only the file system can do it)
878 */
879
880 xvattr->xva_vattr.va_mask &= ~AT_CTIME;
881
882 if (smb_attr->sa_mask & SMB_AT_DOSATTR) {
883
884 /*
885 * "|" in the original xva_mask, which contains
886 * AT_XVATTR
887 */
888
889 xvattr->xva_vattr.va_mask |= xva_mask;
890
891 XVA_SET_REQ(xvattr, XAT_ARCHIVE);
892 XVA_SET_REQ(xvattr, XAT_SYSTEM);
893 XVA_SET_REQ(xvattr, XAT_READONLY);
894 XVA_SET_REQ(xvattr, XAT_HIDDEN);
895 XVA_SET_REQ(xvattr, XAT_OFFLINE);
896 XVA_SET_REQ(xvattr, XAT_SPARSE);
897
898 /*
899 * smb_attr->sa_dosattr: If a given bit is not set,
900 * that indicates that the corresponding field needs
901 * to be updated with a "0" value. This is done
902 * implicitly as the xoap->xoa_* fields were bzero'd.
903 */
904
905 if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_ARCHIVE)
906 xoap->xoa_archive = 1;
907
908 if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SYSTEM)
909 xoap->xoa_system = 1;
910
911 if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_READONLY)
912 xoap->xoa_readonly = 1;
913
914 if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_HIDDEN)
915 xoap->xoa_hidden = 1;
916
917 if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_OFFLINE)
918 xoap->xoa_offline = 1;
919
920 if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE)
921 xoap->xoa_sparse = 1;
922 }
923
924 if (smb_attr->sa_mask & SMB_AT_CRTIME) {
925 /*
926 * "|" in the original xva_mask, which contains
927 * AT_XVATTR
928 */
929
930 xvattr->xva_vattr.va_mask |= xva_mask;
931 XVA_SET_REQ(xvattr, XAT_CREATETIME);
932 xoap->xoa_createtime = smb_attr->sa_crtime;
933 }
934 }
935
936 /*
937 * smb_vop_readdir()
938 *
939 * Collects an SMB_MINLEN_RDDIR_BUF "page" of directory entries.
940 * The directory entries are returned in an fs-independent format by the
941 * underlying file system. That is, the "page" of information returned is
942 * not literally stored on-disk in the format returned.
943 * If the file system supports extended directory entries (has features
944 * VFSFT_DIRENTFLAGS), set V_RDDIR_ENTFLAGS to cause the buffer to be
945 * filled with edirent_t structures, instead of dirent64_t structures.
946 * If the file system supports access based enumeration (abe), set
947 * V_RDDIR_ACCFILTER to filter directory entries based on user cred.
948 */
949 int
smb_vop_readdir(vnode_t * vp,uint32_t offset,void * buf,int * count,int * eof,uint32_t rddir_flag,cred_t * cr)950 smb_vop_readdir(vnode_t *vp, uint32_t offset,
951 void *buf, int *count, int *eof, uint32_t rddir_flag, cred_t *cr)
952 {
953 int error = 0;
954 int flags = 0;
955 int rdirent_size;
956 struct uio auio;
957 struct iovec aiov;
958
959 if (vp->v_type != VDIR)
960 return (ENOTDIR);
961
962 if (vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS)) {
963 flags |= V_RDDIR_ENTFLAGS;
964 rdirent_size = sizeof (edirent_t);
965 } else {
966 rdirent_size = sizeof (dirent64_t);
967 }
968
969 if (*count < rdirent_size)
970 return (EINVAL);
971
972 if (rddir_flag & SMB_ABE)
973 flags |= V_RDDIR_ACCFILTER;
974
975 aiov.iov_base = buf;
976 aiov.iov_len = *count;
977 auio.uio_iov = &aiov;
978 auio.uio_iovcnt = 1;
979 auio.uio_loffset = (uint64_t)offset;
980 auio.uio_segflg = UIO_SYSSPACE;
981 auio.uio_extflg = UIO_COPY_DEFAULT;
982 auio.uio_resid = *count;
983 auio.uio_fmode = 0;
984
985 (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
986 error = VOP_READDIR(vp, &auio, cr, eof, &smb_ct, flags);
987 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
988
989 if (error == 0)
990 *count = *count - auio.uio_resid;
991
992 return (error);
993 }
994
995 /*
996 * smb_sa_to_va_mask
997 *
998 * Set va_mask by running through the SMB_AT_* #define's and
999 * setting those bits that correspond to the SMB_AT_* bits
1000 * set in sa_mask.
1001 */
1002 void
smb_sa_to_va_mask(uint_t sa_mask,uint_t * va_maskp)1003 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
1004 {
1005 int i;
1006 uint_t smask;
1007
1008 smask = (sa_mask);
1009 for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
1010 if (smask & 1)
1011 *(va_maskp) |= smb_attrmap[i];
1012
1013 smask >>= 1;
1014 }
1015 }
1016
1017 /*
1018 * smb_vop_stream_lookup()
1019 *
1020 * The name returned in od_name is the on-disk name of the stream with the
1021 * SMB_STREAM_PREFIX stripped off. od_name should be allocated to MAXNAMELEN
1022 * by the caller.
1023 */
1024 int
smb_vop_stream_lookup(vnode_t * fvp,char * stream_name,vnode_t ** vpp,char * od_name,vnode_t ** xattrdirvpp,int flags,vnode_t * rootvp,cred_t * cr)1025 smb_vop_stream_lookup(
1026 vnode_t *fvp,
1027 char *stream_name,
1028 vnode_t **vpp,
1029 char *od_name,
1030 vnode_t **xattrdirvpp,
1031 int flags,
1032 vnode_t *rootvp,
1033 cred_t *cr)
1034 {
1035 char *solaris_stream_name;
1036 char *name;
1037 int error, tmpflgs;
1038
1039 if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1040 LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1041 return (error);
1042
1043 /*
1044 * Prepend SMB_STREAM_PREFIX to stream name
1045 */
1046
1047 solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1048 (void) snprintf(solaris_stream_name, MAXNAMELEN,
1049 "%s%s", SMB_STREAM_PREFIX, stream_name);
1050
1051 /*
1052 * "name" will hold the on-disk name returned from smb_vop_lookup
1053 * for the stream, including the SMB_STREAM_PREFIX.
1054 */
1055
1056 name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1057
1058 if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
1059 name, flags, &tmpflgs, rootvp, NULL, cr)) != 0) {
1060 VN_RELE(*xattrdirvpp);
1061 } else {
1062 (void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
1063 MAXNAMELEN);
1064 }
1065
1066 kmem_free(solaris_stream_name, MAXNAMELEN);
1067 kmem_free(name, MAXNAMELEN);
1068
1069 return (error);
1070 }
1071
1072 int
smb_vop_stream_create(vnode_t * fvp,char * stream_name,smb_attr_t * attr,vnode_t ** vpp,vnode_t ** xattrdirvpp,int flags,cred_t * cr)1073 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
1074 vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
1075 {
1076 char *solaris_stream_name;
1077 int error;
1078
1079 if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1080 LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1081 return (error);
1082
1083 /*
1084 * Prepend SMB_STREAM_PREFIX to stream name
1085 */
1086
1087 solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1088 (void) snprintf(solaris_stream_name, MAXNAMELEN,
1089 "%s%s", SMB_STREAM_PREFIX, stream_name);
1090
1091 if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
1092 vpp, flags, cr, NULL)) != 0)
1093 VN_RELE(*xattrdirvpp);
1094
1095 kmem_free(solaris_stream_name, MAXNAMELEN);
1096
1097 return (error);
1098 }
1099
1100 int
smb_vop_stream_remove(vnode_t * vp,char * stream_name,int flags,cred_t * cr)1101 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1102 {
1103 char *solaris_stream_name;
1104 vnode_t *xattrdirvp;
1105 int error;
1106
1107 error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr);
1108 if (error != 0)
1109 return (error);
1110
1111 /*
1112 * Prepend SMB_STREAM_PREFIX to stream name
1113 */
1114
1115 solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1116 (void) snprintf(solaris_stream_name, MAXNAMELEN,
1117 "%s%s", SMB_STREAM_PREFIX, stream_name);
1118
1119 /* XXX might have to use kcred */
1120 error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1121
1122 kmem_free(solaris_stream_name, MAXNAMELEN);
1123 VN_RELE(xattrdirvp);
1124
1125 return (error);
1126 }
1127
1128 int
smb_vop_lookup_xattrdir(vnode_t * fvp,vnode_t ** xattrdirvpp,int flags,cred_t * cr)1129 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1130 cred_t *cr)
1131 {
1132 int error;
1133
1134 error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1135 &smb_ct, NULL, NULL);
1136 return (error);
1137 }
1138
1139 /*
1140 * smb_vop_traverse_check()
1141 *
1142 * This function checks to see if the passed-in vnode has a file system
1143 * mounted on it. If it does, the mount point is "traversed" and the
1144 * vnode for the root of the file system is returned.
1145 */
1146 int
smb_vop_traverse_check(vnode_t ** vpp)1147 smb_vop_traverse_check(vnode_t **vpp)
1148 {
1149 int error;
1150
1151 if (vn_mountedvfs(*vpp) == 0)
1152 return (0);
1153
1154 /*
1155 * traverse() may return a different held vnode, even in the error case.
1156 * If it returns a different vnode, it will have released the original.
1157 */
1158
1159 error = traverse(vpp);
1160
1161 return (error);
1162 }
1163
1164 int /*ARGSUSED*/
smb_vop_statfs(vnode_t * vp,struct statvfs64 * statp,cred_t * cr)1165 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1166 {
1167 int error;
1168
1169 error = VFS_STATVFS(vp->v_vfsp, statp);
1170
1171 return (error);
1172 }
1173
1174 /*
1175 * smb_vop_acl_read
1176 *
1177 * Reads the ACL of the specified file into 'aclp'.
1178 * acl_type is the type of ACL which the filesystem supports.
1179 *
1180 * Caller has to free the allocated memory for aclp by calling
1181 * acl_free().
1182 */
1183 int
smb_vop_acl_read(vnode_t * vp,acl_t ** aclp,int flags,acl_type_t acl_type,cred_t * cr)1184 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1185 cred_t *cr)
1186 {
1187 int error;
1188 vsecattr_t vsecattr;
1189
1190 ASSERT(vp);
1191 ASSERT(aclp);
1192
1193 *aclp = NULL;
1194 bzero(&vsecattr, sizeof (vsecattr_t));
1195
1196 switch (acl_type) {
1197 case ACLENT_T:
1198 vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1199 VSA_DFACLCNT;
1200 break;
1201
1202 case ACE_T:
1203 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1204 break;
1205
1206 default:
1207 return (EINVAL);
1208 }
1209
1210 if (error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct))
1211 return (error);
1212
1213 *aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1214 if (vp->v_type == VDIR)
1215 (*aclp)->acl_flags |= ACL_IS_DIR;
1216
1217 return (0);
1218 }
1219
1220 /*
1221 * smb_vop_acl_write
1222 *
1223 * Writes the given ACL in aclp for the specified file.
1224 */
1225 int
smb_vop_acl_write(vnode_t * vp,acl_t * aclp,int flags,cred_t * cr)1226 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1227 {
1228 int error;
1229 vsecattr_t vsecattr;
1230 int aclbsize;
1231
1232 ASSERT(vp);
1233 ASSERT(aclp);
1234
1235 error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1236
1237 if (error == 0) {
1238 (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1239 error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1240 VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1241 }
1242
1243 if (aclbsize && vsecattr.vsa_aclentp)
1244 kmem_free(vsecattr.vsa_aclentp, aclbsize);
1245
1246 return (error);
1247 }
1248
1249 /*
1250 * smb_vop_acl_type
1251 *
1252 * Determines the ACL type for the given vnode.
1253 * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1254 */
1255 acl_type_t
smb_vop_acl_type(vnode_t * vp)1256 smb_vop_acl_type(vnode_t *vp)
1257 {
1258 int error;
1259 ulong_t whichacl;
1260
1261 error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl,
1262 zone_kcred(), NULL);
1263 if (error != 0) {
1264 /*
1265 * If we got an error, then the filesystem
1266 * likely does not understand the _PC_ACL_ENABLED
1267 * pathconf. In this case, we fall back to trying
1268 * POSIX-draft (aka UFS-style) ACLs.
1269 */
1270 whichacl = _ACL_ACLENT_ENABLED;
1271 }
1272
1273 if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1274 /*
1275 * If the file system supports neither ACE nor
1276 * ACLENT ACLs we will fall back to UFS-style ACLs
1277 * like we did above if there was an error upon
1278 * calling VOP_PATHCONF.
1279 *
1280 * ACE and ACLENT type ACLs are the only interfaces
1281 * supported thus far. If any other bits are set on
1282 * 'whichacl' upon return from VOP_PATHCONF, we will
1283 * ignore them.
1284 */
1285 whichacl = _ACL_ACLENT_ENABLED;
1286 }
1287
1288 if (whichacl == _ACL_ACLENT_ENABLED)
1289 return (ACLENT_T);
1290
1291 return (ACE_T);
1292 }
1293
1294 static const int zfs_perms[] = {
1295 ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1296 ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1297 ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1298 ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1299 };
1300
1301 static const int unix_perms[] = { VREAD, VWRITE, VEXEC };
1302 /*
1303 * smb_vop_eaccess
1304 *
1305 * Returns the effective permission of the given credential for the
1306 * specified object.
1307 *
1308 * This is just a workaround. We need VFS/FS support for this.
1309 */
1310 void
smb_vop_eaccess(vnode_t * vp,int * mode,int flags,vnode_t * dir_vp,cred_t * cr)1311 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1312 {
1313 int error, i;
1314 int pnum;
1315
1316 *mode = 0;
1317
1318 if (flags == V_ACE_MASK) {
1319 pnum = sizeof (zfs_perms) / sizeof (int);
1320
1321 for (i = 0; i < pnum; i++) {
1322 error = smb_vop_access(vp, zfs_perms[i], flags,
1323 dir_vp, cr);
1324 if (error == 0)
1325 *mode |= zfs_perms[i];
1326 }
1327 } else {
1328 pnum = sizeof (unix_perms) / sizeof (int);
1329
1330 for (i = 0; i < pnum; i++) {
1331 error = smb_vop_access(vp, unix_perms[i], flags,
1332 dir_vp, cr);
1333 if (error == 0)
1334 *mode |= unix_perms[i];
1335 }
1336 }
1337 }
1338
1339 /*
1340 * See comments for smb_fsop_shrlock()
1341 */
1342 int
smb_vop_shrlock(vnode_t * vp,uint32_t uniq_fid,uint32_t desired_access,uint32_t share_access,cred_t * cr)1343 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1344 uint32_t share_access, cred_t *cr)
1345 {
1346 struct shrlock shr;
1347 struct shr_locowner shr_own;
1348 short new_access = 0;
1349 short deny = 0;
1350 int flag = 0;
1351 int cmd;
1352
1353 /*
1354 * share locking is not supported for non-regular
1355 * objects in NBMAND mode.
1356 */
1357 if (nbl_need_check(vp)) {
1358 if (vp->v_type != VREG)
1359 return (0);
1360
1361 cmd = F_SHARE_NBMAND;
1362 } else {
1363 cmd = F_SHARE;
1364 }
1365
1366 if ((desired_access & FILE_DATA_ALL) == 0) {
1367 /* metadata access only */
1368 new_access |= F_MDACC;
1369 } else {
1370 if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1371 new_access |= F_RDACC;
1372 flag |= FREAD;
1373 }
1374
1375 if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1376 ACE_ADD_FILE)) {
1377 new_access |= F_WRACC;
1378 flag |= FWRITE;
1379 }
1380
1381 if (SMB_DENY_READ(share_access)) {
1382 deny |= F_RDDNY;
1383 }
1384
1385 if (SMB_DENY_WRITE(share_access)) {
1386 deny |= F_WRDNY;
1387 }
1388
1389 if (cmd == F_SHARE_NBMAND) {
1390 if (desired_access & ACE_DELETE)
1391 new_access |= F_RMACC;
1392
1393 if (SMB_DENY_DELETE(share_access)) {
1394 deny |= F_RMDNY;
1395 }
1396 }
1397 }
1398
1399 shr.s_access = new_access;
1400 shr.s_deny = deny;
1401 shr.s_sysid = smb_ct.cc_sysid;
1402 shr.s_pid = uniq_fid;
1403 shr.s_own_len = sizeof (shr_own);
1404 shr.s_owner = (caddr_t)&shr_own;
1405 shr_own.sl_id = shr.s_sysid;
1406 shr_own.sl_pid = shr.s_pid;
1407
1408 return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1409 }
1410
1411 int
smb_vop_unshrlock(vnode_t * vp,uint32_t uniq_fid,cred_t * cr)1412 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1413 {
1414 struct shrlock shr;
1415 struct shr_locowner shr_own;
1416
1417 /*
1418 * share locking is not supported for non-regular
1419 * objects in NBMAND mode.
1420 */
1421 if (nbl_need_check(vp) && (vp->v_type != VREG))
1422 return (0);
1423
1424 /*
1425 * For s_access and s_deny, we do not need to pass in the original
1426 * values.
1427 */
1428 shr.s_access = 0;
1429 shr.s_deny = 0;
1430 shr.s_sysid = smb_ct.cc_sysid;
1431 shr.s_pid = uniq_fid;
1432 shr.s_own_len = sizeof (shr_own);
1433 shr.s_owner = (caddr_t)&shr_own;
1434 shr_own.sl_id = shr.s_sysid;
1435 shr_own.sl_pid = shr.s_pid;
1436
1437 return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1438 }
1439
1440 int
smb_vop_frlock(vnode_t * vp,cred_t * cr,int flag,flock64_t * bf)1441 smb_vop_frlock(vnode_t *vp, cred_t *cr, int flag, flock64_t *bf)
1442 {
1443 int cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
1444 flk_callback_t flk_cb;
1445
1446 flk_init_callback(&flk_cb, smb_lock_frlock_callback, NULL);
1447
1448 return (VOP_FRLOCK(vp, cmd, bf, flag, 0, &flk_cb, cr, &smb_ct));
1449 }
1450
1451 static callb_cpr_t *
1452 /* ARGSUSED */
smb_lock_frlock_callback(flk_cb_when_t when,void * error)1453 smb_lock_frlock_callback(flk_cb_when_t when, void *error)
1454 {
1455 return (0);
1456 }
1457
1458 /*
1459 * smb_vop_catia_init_v4_lookup
1460 * Initialize mapping between wide characters in the range from
1461 * 0x00A4 to 0x00FF and their UNIX (v4) equivalent (wide character).
1462 * Indexed by the decimal value of the wide character (164-255)
1463 * with an offset of -164.
1464 */
1465 static void
smb_vop_catia_init_v4_lookup()1466 smb_vop_catia_init_v4_lookup()
1467 {
1468 int i, idx, offset = SMB_CATIA_V4_LOOKUP_LOW;
1469
1470 for (i = 0; i < SMB_CATIA_V4_LOOKUP_MAX; i++)
1471 smb_catia_v4_lookup[i] = (smb_wchar_t)(i + offset);
1472
1473 for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1474 idx = (int)catia_maps[i].winchar - offset;
1475 smb_catia_v4_lookup[idx] = (smb_wchar_t)catia_maps[i].unixchar;
1476 }
1477 }
1478
1479 /*
1480 * smb_vop_catia_init_v5_lookup
1481 * Initialize mapping between UNIX ASCII (v4) characters and equivalent
1482 * or translated wide characters.
1483 * Indexed by the decimal value of the ASCII character (0-127).
1484 */
1485 static void
smb_vop_catia_init_v5_lookup()1486 smb_vop_catia_init_v5_lookup()
1487 {
1488 int i, idx;
1489
1490 for (i = 0; i < SMB_CATIA_V5_LOOKUP_MAX; i++)
1491 smb_catia_v5_lookup[i] = (smb_wchar_t)i;
1492
1493 for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1494 idx = (int)catia_maps[i].unixchar;
1495 smb_catia_v5_lookup[idx] = catia_maps[i].winchar;
1496 }
1497 }
1498
1499 static void
smb_vop_catia_init()1500 smb_vop_catia_init()
1501 {
1502 smb_vop_catia_init_v4_lookup();
1503 smb_vop_catia_init_v5_lookup();
1504 }
1505
1506 /*
1507 * smb_vop_catia_v5tov4
1508 * (windows (v5) to unix (v4))
1509 *
1510 * Traverse each character in the given source filename and convert the
1511 * multibyte that is equivalent to any special Windows character listed
1512 * in the catia_maps table to the Unix ASCII character if any is
1513 * encountered in the filename. The translated name is returned in buf.
1514 *
1515 * If an error occurs the conversion terminates and name is returned,
1516 * otherwise buf is returned.
1517 */
1518 char *
smb_vop_catia_v5tov4(char * name,char * buf,int buflen)1519 smb_vop_catia_v5tov4(char *name, char *buf, int buflen)
1520 {
1521 int v4_idx, numbytes, inc;
1522 int space_left = buflen - 1; /* one byte reserved for null */
1523 smb_wchar_t wc;
1524 char mbstring[MTS_MB_CHAR_MAX];
1525 char *p, *src = name, *dst = buf;
1526
1527 ASSERT(name);
1528 ASSERT(buf);
1529
1530 if (!buf || !name)
1531 return (name);
1532
1533 bzero(buf, buflen);
1534
1535 while (*src) {
1536 if ((numbytes = smb_mbtowc(&wc, src, MTS_MB_CHAR_MAX)) < 0)
1537 return (name);
1538
1539 if (wc < SMB_CATIA_V4_LOOKUP_LOW ||
1540 wc > SMB_CATIA_V4_LOOKUP_UPPER) {
1541 inc = numbytes;
1542 p = src;
1543 } else {
1544 /* Lookup required. */
1545 v4_idx = (int)wc - SMB_CATIA_V4_LOOKUP_LOW;
1546 inc = smb_wctomb(mbstring, smb_catia_v4_lookup[v4_idx]);
1547 p = mbstring;
1548 }
1549
1550 if (space_left < inc)
1551 return (name);
1552
1553 (void) strncpy(dst, p, inc);
1554 dst += inc;
1555 space_left -= inc;
1556 src += numbytes;
1557 }
1558
1559 return (buf);
1560 }
1561
1562 /*
1563 * smb_vop_catia_v4tov5
1564 * (unix (v4) to windows (v5))
1565 *
1566 * Traverse each character in the given filename 'srcbuf' and convert
1567 * the special Unix character that is listed in the catia_maps table to
1568 * the UTF-8 encoding of the corresponding Windows character if any is
1569 * encountered in the filename.
1570 *
1571 * The translated name is returned in buf.
1572 * If an error occurs the conversion terminates and the original name
1573 * is returned in buf.
1574 */
1575 void
smb_vop_catia_v4tov5(char * name,char * buf,int buflen)1576 smb_vop_catia_v4tov5(char *name, char *buf, int buflen)
1577 {
1578 int v5_idx, numbytes;
1579 int space_left = buflen - 1; /* one byte reserved for null */
1580 smb_wchar_t wc;
1581 char mbstring[MTS_MB_CHAR_MAX];
1582 char *src = name, *dst = buf;
1583
1584 ASSERT(name);
1585 ASSERT(buf);
1586
1587 if (!buf || !name)
1588 return;
1589
1590 (void) bzero(buf, buflen);
1591 while (*src) {
1592 if (smb_isascii(*src)) {
1593 /* Lookup required */
1594 v5_idx = (int)*src++;
1595 numbytes = smb_wctomb(mbstring,
1596 smb_catia_v5_lookup[v5_idx]);
1597 if (space_left < numbytes)
1598 break;
1599 (void) strncpy(dst, mbstring, numbytes);
1600 } else {
1601 if ((numbytes = smb_mbtowc(&wc, src,
1602 MTS_MB_CHAR_MAX)) < 0)
1603 break;
1604 if (space_left < numbytes)
1605 break;
1606 (void) strncpy(dst, src, numbytes);
1607 src += numbytes;
1608 }
1609
1610 dst += numbytes;
1611 space_left -= numbytes;
1612 }
1613
1614 if (*src)
1615 (void) strlcpy(buf, name, buflen);
1616 }
1617