xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_locking_andx.c (revision 3db3f65c6274eb042354801a308c8e9bc4994553)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * SMB: locking_andx
30  *
31  * SMB_COM_LOCKING_ANDX allows both locking and/or unlocking of file range(s).
32  *
33  *  Client Request                     Description
34  *  ================================== =================================
35  *
36  *  UCHAR WordCount;                   Count of parameter words = 8
37  *  UCHAR AndXCommand;                 Secondary (X) command;  0xFF = none
38  *  UCHAR AndXReserved;                Reserved (must be 0)
39  *  USHORT AndXOffset;                 Offset to next command WordCount
40  *  USHORT Fid;                        File handle
41  *  UCHAR LockType;                    See LockType table below
42  *  UCHAR OplockLevel;                 The new oplock level
43  *  ULONG Timeout;                     Milliseconds to wait for unlock
44  *  USHORT NumberOfUnlocks;            Num. unlock range structs following
45  *  USHORT NumberOfLocks;              Num. lock range structs following
46  *  USHORT ByteCount;                  Count of data bytes
47  *  LOCKING_ANDX_RANGE Unlocks[];      Unlock ranges
48  *  LOCKING_ANDX_RANGE Locks[];        Lock ranges
49  *
50  *  LockType Flag Name            Value Description
51  *  ============================  ===== ================================
52  *
53  *  LOCKING_ANDX_SHARED_LOCK      0x01  Read-only lock
54  *  LOCKING_ANDX_OPLOCK_RELEASE   0x02  Oplock break notification
55  *  LOCKING_ANDX_CHANGE_LOCKTYPE  0x04  Change lock type
56  *  LOCKING_ANDX_CANCEL_LOCK      0x08  Cancel outstanding request
57  *  LOCKING_ANDX_LARGE_FILES      0x10  Large file locking format
58  *
59  *  LOCKING_ANDX_RANGE Format
60  *  =====================================================================
61  *
62  *  USHORT Pid;                        PID of process "owning" lock
63  *  ULONG Offset;                      Offset to bytes to [un]lock
64  *  ULONG Length;                      Number of bytes to [un]lock
65  *
66  *  Large File LOCKING_ANDX_RANGE Format
67  *  =====================================================================
68  *
69  *  USHORT Pid;                        PID of process "owning" lock
70  *  USHORT Pad;                        Pad to DWORD align (mbz)
71  *  ULONG OffsetHigh;                  Offset to bytes to [un]lock
72  *                                      (high)
73  *  ULONG OffsetLow;                   Offset to bytes to [un]lock (low)
74  *  ULONG LengthHigh;                  Number of bytes to [un]lock
75  *                                      (high)
76  *  ULONG LengthLow;                   Number of bytes to [un]lock (low)
77  *
78  *  Server Response                    Description
79  *  ================================== =================================
80  *
81  *  UCHAR WordCount;                   Count of parameter words = 2
82  *  UCHAR AndXCommand;                 Secondary (X) command;  0xFF =
83  *                                      none
84  *  UCHAR AndXReserved;                Reserved (must be 0)
85  *  USHORT AndXOffset;                 Offset to next command WordCount
86  *  USHORT ByteCount;                  Count of data bytes = 0
87  *
88  * Locking is a simple mechanism for excluding other processes read/write
89  * access to regions of a file.  The locked regions can be anywhere in the
90  * logical file.  Locking beyond end-of-file is permitted.  Any process
91  * using the Fid specified in this request's Fid has access to the locked
92  * bytes, other processes will be denied the locking of the same bytes.
93  *
94  * The proper method for using locks is not to rely on being denied read or
95  * write access on any of the read/write protocols but rather to attempt
96  * the locking protocol and proceed with the read/write only if the locks
97  * succeeded.
98  *
99  * Locking a range of bytes will fail if any subranges or overlapping
100  * ranges are locked.  In other words, if any of the specified bytes are
101  * already locked, the lock will fail.
102  *
103  * If NumberOfUnlocks is non-zero, the Unlocks vector contains
104  * NumberOfUnlocks elements.  Each element requests that a lock at Offset
105  * of Length be released.  If NumberOfLocks is nonzero, the Locks vector
106  * contains NumberOfLocks elements.  Each element requests the acquisition
107  * of a lock at Offset of Length.
108  *
109  * Timeout is the maximum amount of time to wait for the byte range(s)
110  * specified to become unlocked.  A timeout value of 0 indicates that the
111  * server should fail immediately if any lock range specified is locked.  A
112  *
113  * timeout value of -1 indicates that the server should wait as long as it
114  * takes for each byte range specified to become unlocked so that it may be
115  * again locked by this protocol.  Any other value of smb_timeout specifies
116  * the maximum number of milliseconds to wait for all lock range(s)
117  * specified to become available.
118  *
119  * If any of the lock ranges timeout because of the area to be locked is
120  * already locked (or the lock fails), the other ranges in the protocol
121  * request which were successfully locked as a result of this protocol will
122  * be unlocked (either all requested ranges will be locked when this
123  * protocol returns to the client or none).
124  *
125  * If LockType has the LOCKING_ANDX_SHARED_LOCK flag set, the lock is
126  * specified as a shared lock.  Locks for both read and write (where
127  * LOCKING_ANDX_SHARED_LOCK is clear) should be prohibited, but other
128  * shared locks should be permitted.  If shared locks can not be supported
129  * by a server, the server should map the lock to a lock for both read and
130  * write.  Closing a file with locks still in force causes the locks to be
131  * released in no defined order.
132  *
133  * If LockType has the LOCKING_ANDX_LARGE_FILES flag set and if the
134  * negotiated protocol is NT LM 0.12 or later, then the Locks and Unlocks
135  * vectors are in the Large File LOCKING_ANDX_RANGE format.  This allows
136  * specification of 64 bit offsets for very large files.
137  *
138  * If the one and only member of the Locks vector has the
139  * LOCKING_ANDX_CANCEL_LOCK flag set in the LockType field, the client is
140  * requesting the server to cancel a previously requested, but not yet
141  * responded to, lock.
142  *
143  * If LockType has the LOCKING_ANDX_CHANGE_LOCKTYPE flag set, the client is
144  * requesting that the server atomically change the lock type from a shared
145  * lock to an exclusive lock or vice versa.  If the server can not do this
146  * in an atomic fashion, the server must reject this request.  NT and W95
147  * servers do not support this capability.
148  *
149  * Oplocks are described in the "Opportunistic Locks" section elsewhere in
150  * this document.  A client requests an oplock by setting the appropriate
151  * bit in the SMB_COM_OPEN_ANDX request when the file is being opened in a
152  * mode which is not exclusive.  The server responds by setting the
153  * appropriate bit in the response SMB indicating whether or not the oplock
154  * was granted.  By granting the oplock, the server tells the client the
155  * file is currently only being used by this one client process at the
156  * current time.  The client can therefore safely do read ahead and write
157  * behind as well as local caching of file locks knowing that the file will
158  * not be accessed/changed in any way by another process while the oplock
159  * is in effect.  The client will be notified when any other process
160  * attempts to open or modify the oplocked file.
161  *
162  * When another user attempts to open or otherwise modify the file which a
163  * client has oplocked, the server delays the second attempt and notifies
164  * the client via an SMB_LOCKING_ANDX SMB asynchronously sent from the
165  * server to the client.  This message has the LOCKING_ANDX_OPLOCK_RELEASE
166  * flag set indicating to the client that the oplock is being broken.
167  *
168  * OplockLevel indicates the type of oplock the client now owns. If
169  * OplockLevel is 0, the client possesses no oplocks on the file at all, if
170  * OplockLevel is 1 the client possesses a Level II oplock.  The client is
171  * expected to flush any dirty buffers to the server, submit any file locks
172  * and respond to the server with either an SMB_LOCKING_ANDX SMB having the
173  * LOCKING_ANDX_OPLOCK_RELEASE flag set, or with a file close if the file
174  * is no longer in use by the client.  If the client sends an
175  * SMB_LOCKING_ANDX SMB with the LOCKING_ANDX_OPLOCK_RELEASE flag set and
176  * NumberOfLocks is zero, the server does not send a response.  Since a
177  * close being sent to the server and break oplock notification from the
178  * server could cross on the wire, if the client gets an oplock
179  * notification on a file which it does not have open, that notification
180  * should be ignored.
181  *
182  * Due to timing, the client could get an "oplock broken" notification in a
183  * user's data buffer as a result of this notification crossing on the wire
184  * with a SMB_COM_READ_RAW request.  The client must detect this (use
185  * length of msg, "FFSMB", MID of -1 and Command of SMB_COM_LOCKING_ANDX)
186  * and honor the "oplock broken" notification as usual.  The server must
187  * also note on receipt of an SMB_COM_READ_RAW request that there is an
188  * outstanding (unanswered) "oplock broken" notification to the client and
189  * return a zero length response denoting failure of the read raw request.
190  * The client should (after responding to the "oplock broken"
191  * notification), use a standard read protocol to redo the read request.
192  * This allows a file to actually contain data matching an "oplock broken"
193  * notification and still be read correctly.
194  *
195  * The entire message sent and received including the optional second
196  * protocol must fit in the negotiated maximum transfer size.  The
197  * following are the only valid SMB commands for AndXCommand for
198  * SMB_COM_LOCKING_ANDX:
199  *
200  *     SMB_COM_READ       SMB_COM_READ_ANDX
201  *     SMB_COM_WRITE      SMB_COM_WRITE_ANDX
202  *     SMB_COM_FLUSH
203  *
204  * 4.2.6.1   Errors
205  *
206  * ERRDOS/ERRbadfile
207  * ERRDOS/ERRbadfid
208  * ERRDOS/ERRlock
209  * ERRDOS/ERRinvdevice
210  * ERRSRV/ERRinvid
211  * ERRSRV/ERRbaduid
212  */
213 
214 #include <smbsrv/smb_incl.h>
215 
216 smb_sdrc_t
217 smb_pre_locking_andx(smb_request_t *sr)
218 {
219 	DTRACE_SMB_1(op__LockingX__start, smb_request_t *, sr);
220 	return (SDRC_SUCCESS);
221 }
222 
223 void
224 smb_post_locking_andx(smb_request_t *sr)
225 {
226 	DTRACE_SMB_1(op__LockingX__done, smb_request_t *, sr);
227 }
228 
229 smb_sdrc_t
230 smb_com_locking_andx(smb_request_t *sr)
231 {
232 	unsigned short	i;
233 	unsigned char	lock_type;	/* See lock_type table above */
234 	unsigned char	oplock_level;	/* The new oplock level */
235 	uint32_t	timeout;	/* Milliseconds to wait for lock */
236 	unsigned short	unlock_num;	/* # unlock range structs */
237 	unsigned short	lock_num;	/* # lock range structs */
238 	unsigned short	pid;		/* Process Id of owner */
239 	uint32_t	offset32, length32;
240 	uint64_t	offset64;
241 	uint64_t	length64;
242 	DWORD		result;
243 	int 		rc;
244 	uint32_t	ltype;
245 
246 	rc = smbsr_decode_vwv(sr, "4.wbblww", &sr->smb_fid, &lock_type,
247 	    &oplock_level, &timeout, &unlock_num, &lock_num);
248 	if (rc != 0)
249 		return (SDRC_ERROR);
250 
251 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
252 	if (sr->fid_ofile == NULL) {
253 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
254 		return (SDRC_ERROR);
255 	}
256 
257 	if (lock_type & LOCKING_ANDX_SHARED_LOCK)
258 		ltype = SMB_LOCK_TYPE_READONLY;
259 	else
260 		ltype = SMB_LOCK_TYPE_READWRITE;
261 
262 	pid = sr->smb_pid;	/* Save the original pid */
263 
264 	if (lock_type & LOCKING_ANDX_OPLOCK_RELEASE) {
265 		smb_oplock_release(sr->fid_ofile->f_node, B_FALSE);
266 
267 		/*
268 		 * According to the protocol:
269 		 *
270 		 * If the client sends an SMB_LOCKING_ANDX request with the
271 		 * LOCKING_ANDX_OPLOCK_RELEASE flag set
272 		 * and NumberOfLocks is zero,
273 		 * the server does not send a response.
274 		 *
275 		 * I'm not sure if it's going to break anything if I change
276 		 * it according to the protocol. So, I leave it unchanged
277 		 * for now.
278 		 */
279 		if (unlock_num == 0 && lock_num == 0)
280 			return (SDRC_NO_REPLY);
281 	}
282 
283 	/*
284 	 * No support for changing locktype (although we could probably
285 	 * implement this)
286 	 */
287 	if (lock_type & LOCKING_ANDX_CHANGE_LOCK_TYPE) {
288 		smbsr_error(sr, 0, ERRDOS, ERRnoatomiclocks);
289 		return (SDRC_ERROR);
290 	}
291 
292 	/*
293 	 * No support for cancel lock (smbtorture expects this)
294 	 */
295 	if (lock_type & LOCKING_ANDX_CANCEL_LOCK) {
296 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
297 		    ERRDOS, ERROR_INVALID_PARAMETER);
298 		return (SDRC_ERROR);
299 	}
300 
301 	if (lock_type & LOCKING_ANDX_LARGE_FILES) {
302 		/*
303 		 * negotiated protocol should be NT LM 0.12 or later
304 		 */
305 		if (sr->session->dialect < NT_LM_0_12) {
306 			smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
307 			    ERRDOS, ERROR_INVALID_PARAMETER);
308 			return (SDRC_ERROR);
309 		}
310 
311 		for (i = 0; i < unlock_num; i++) {
312 			rc = smb_mbc_decodef(&sr->smb_data, "w2.QQ",
313 			    &sr->smb_pid, &offset64, &length64);
314 			if (rc) {
315 				/*
316 				 * This is the error returned by Windows 2000
317 				 * even when STATUS32 has been negotiated.
318 				 */
319 				smbsr_error(sr, 0, ERRSRV, ERRerror);
320 				return (SDRC_ERROR);
321 			}
322 
323 			result = smb_unlock_range(sr, sr->fid_ofile->f_node,
324 			    offset64, length64);
325 			if (result != NT_STATUS_SUCCESS) {
326 				smbsr_error(sr, NT_STATUS_RANGE_NOT_LOCKED,
327 				    ERRDOS, ERRnotlocked);
328 				return (SDRC_ERROR);
329 			}
330 		}
331 
332 		for (i = 0; i < lock_num; i++) {
333 			rc = smb_mbc_decodef(&sr->smb_data, "w2.QQ",
334 			    &sr->smb_pid, &offset64, &length64);
335 			if (rc) {
336 				smbsr_error(sr, 0, ERRSRV, ERRerror);
337 				return (SDRC_ERROR);
338 			}
339 
340 			result = smb_lock_range(sr, offset64, length64, timeout,
341 			    ltype);
342 			if (result != NT_STATUS_SUCCESS) {
343 				smb_lock_range_error(sr, result);
344 				return (SDRC_ERROR);
345 			}
346 		}
347 	} else {
348 		for (i = 0; i < unlock_num; i++) {
349 			rc = smb_mbc_decodef(&sr->smb_data, "wll", &sr->smb_pid,
350 			    &offset32, &length32);
351 			if (rc) {
352 				smbsr_error(sr, 0, ERRSRV, ERRerror);
353 				return (SDRC_ERROR);
354 			}
355 
356 			result = smb_unlock_range(sr, sr->fid_ofile->f_node,
357 			    (uint64_t)offset32, (uint64_t)length32);
358 			if (result != NT_STATUS_SUCCESS) {
359 				smbsr_error(sr, NT_STATUS_RANGE_NOT_LOCKED,
360 				    ERRDOS, ERRnotlocked);
361 				return (SDRC_ERROR);
362 			}
363 		}
364 
365 		for (i = 0; i < lock_num; i++) {
366 			rc = smb_mbc_decodef(&sr->smb_data, "wll", &sr->smb_pid,
367 			    &offset32, &length32);
368 			if (rc) {
369 				smbsr_error(sr, 0, ERRSRV, ERRerror);
370 				return (SDRC_ERROR);
371 			}
372 
373 			result = smb_lock_range(sr, (uint64_t)offset32,
374 			    (uint64_t)length32, timeout, ltype);
375 			if (result != NT_STATUS_SUCCESS) {
376 				smb_lock_range_error(sr, result);
377 				return (SDRC_ERROR);
378 			}
379 		}
380 	}
381 
382 	sr->smb_pid = pid;
383 	if (smbsr_encode_result(sr, 2, 0, "bb.ww", 2, sr->andx_com, 7, 0))
384 		return (SDRC_ERROR);
385 	return (SDRC_SUCCESS);
386 }
387