1 /*
2    Unix SMB/CIFS implementation.
3    SMB transaction2 handling
4 
5    Copyright (C) James Peach 2007
6    Copyright (C) Jeremy Allison 1994-2002.
7 
8    Extensively modified by Andrew Tridgell, 1995
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #ifndef __SMB_UNIX_EXT_H__
25 #define __SMB_UNIX_EXT_H__
26 
27 /* UNIX CIFS Extensions - created by HP */
28 /*
29  * UNIX CIFS Extensions have the range 0x200 - 0x2FF reserved.
30  * Supposedly Microsoft have agreed to this.
31  */
32 
33 #define MIN_UNIX_INFO_LEVEL 0x200
34 #define MAX_UNIX_INFO_LEVEL 0x2FF
35 
36 #define INFO_LEVEL_IS_UNIX(level) (((level) >= MIN_UNIX_INFO_LEVEL) && ((level) <= MAX_UNIX_INFO_LEVEL))
37 
38 #define SMB_QUERY_FILE_UNIX_BASIC      0x200   /* UNIX File Info*/
39 #define SMB_SET_FILE_UNIX_BASIC        0x200
40 #define SMB_SET_FILE_UNIX_INFO2        0x20B   /* UNIX File Info2 */
41 
42 #define SMB_MODE_NO_CHANGE                 0xFFFFFFFF     /* file mode value which */
43                                               /* means "don't change it" */
44 #define SMB_UID_NO_CHANGE                  0xFFFFFFFF
45 #define SMB_GID_NO_CHANGE                  0xFFFFFFFF
46 
47 #define SMB_SIZE_NO_CHANGE_LO              0xFFFFFFFF
48 #define SMB_SIZE_NO_CHANGE_HI              0xFFFFFFFF
49 
50 #define SMB_TIME_NO_CHANGE_LO              0xFFFFFFFF
51 #define SMB_TIME_NO_CHANGE_HI              0xFFFFFFFF
52 
53 /*
54 Offset Size         Name
55 0      LARGE_INTEGER EndOfFile                File size
56 8      LARGE_INTEGER Blocks                   Number of bytes used on disk (st_blocks).
57 16     LARGE_INTEGER CreationTime             Creation time
58 24     LARGE_INTEGER LastAccessTime           Last access time
59 32     LARGE_INTEGER LastModificationTime     Last modification time
60 40     LARGE_INTEGER Uid                      Numeric user id for the owner
61 48     LARGE_INTEGER Gid                      Numeric group id of owner
62 56     ULONG Type                             Enumeration specifying the pathname type:
63                                               0 -- File
64                                               1 -- Directory
65                                               2 -- Symbolic link
66                                               3 -- Character device
67                                               4 -- Block device
68                                               5 -- FIFO (named pipe)
69                                               6 -- Unix domain socket
70 
71 60     LARGE_INTEGER devmajor                 Major device number if type is device
72 68     LARGE_INTEGER devminor                 Minor device number if type is device
73 76     LARGE_INTEGER uniqueid                 This is a server-assigned unique id for the file. The client
74                                               will typically map this onto an inode number. The scope of
75                                               uniqueness is the share.
76 84     LARGE_INTEGER permissions              Standard UNIX file permissions  - see below.
77 92     LARGE_INTEGER nlinks                   The number of directory entries that map to this entry
78                                               (number of hard links)
79 
80 100 - end.
81 */
82 
83 #define SMB_FILE_UNIX_BASIC_SIZE 100
84 
85 /* UNIX filetype mappings. */
86 
87 #define UNIX_TYPE_FILE 0
88 #define UNIX_TYPE_DIR 1
89 #define UNIX_TYPE_SYMLINK 2
90 #define UNIX_TYPE_CHARDEV 3
91 #define UNIX_TYPE_BLKDEV 4
92 #define UNIX_TYPE_FIFO 5
93 #define UNIX_TYPE_SOCKET 6
94 #define UNIX_TYPE_UNKNOWN 0xFFFFFFFF
95 
96 /*
97  * Oh this is fun. "Standard UNIX permissions" has no
98  * meaning in POSIX. We need to define the mapping onto
99  * and off the wire as this was not done in the original HP
100  * spec. JRA.
101  */
102 
103 #define UNIX_X_OTH			0000001
104 #define UNIX_W_OTH			0000002
105 #define UNIX_R_OTH			0000004
106 #define UNIX_X_GRP			0000010
107 #define UNIX_W_GRP                      0000020
108 #define UNIX_R_GRP                      0000040
109 #define UNIX_X_USR                      0000100
110 #define UNIX_W_USR                      0000200
111 #define UNIX_R_USR                      0000400
112 #define UNIX_STICKY                     0001000
113 #define UNIX_SET_GID                    0002000
114 #define UNIX_SET_UID                    0004000
115 
116 /* Masks for the above */
117 #define UNIX_OTH_MASK                   0000007
118 #define UNIX_GRP_MASK                   0000070
119 #define UNIX_USR_MASK                   0000700
120 #define UNIX_PERM_MASK                  0000777
121 #define UNIX_EXTRA_MASK                 0007000
122 #define UNIX_ALL_MASK                   0007777
123 
124 /* Flags for chflags (CIFS_UNIX_EXTATTR_CAP capability) and
125  * SMB_QUERY_FILE_UNIX_INFO2.
126  */
127 #define EXT_SECURE_DELETE               0x00000001
128 #define EXT_ENABLE_UNDELETE             0x00000002
129 #define EXT_SYNCHRONOUS                 0x00000004
130 #define EXT_IMMUTABLE			0x00000008
131 #define EXT_OPEN_APPEND_ONLY            0x00000010
132 #define EXT_DO_NOT_BACKUP               0x00000020
133 #define EXT_NO_UPDATE_ATIME             0x00000040
134 #define EXT_HIDDEN			0x00000080
135 
136 #define SMB_QUERY_FILE_UNIX_LINK       0x201
137 #define SMB_SET_FILE_UNIX_LINK         0x201
138 #define SMB_SET_FILE_UNIX_HLINK        0x203
139 /* SMB_QUERY_POSIX_ACL 0x204 see below */
140 #define SMB_QUERY_XATTR                0x205 /* need for non-user XATTRs */
141 #define SMB_QUERY_ATTR_FLAGS           0x206 /* chflags, chattr */
142 #define SMB_SET_ATTR_FLAGS             0x206
143 #define SMB_QUERY_POSIX_PERMISSION     0x207
144 /* Only valid for qfileinfo */
145 #define SMB_QUERY_POSIX_LOCK	       0x208
146 /* Only valid for setfileinfo */
147 #define SMB_SET_POSIX_LOCK	       0x208
148 
149 /* The set info levels for POSIX path operations. */
150 #define SMB_POSIX_PATH_OPEN	       0x209
151 #define SMB_POSIX_PATH_UNLINK	       0x20A
152 
153 #define SMB_QUERY_FILE_UNIX_INFO2      0x20B   /* UNIX File Info2 */
154 #define SMB_SET_FILE_UNIX_INFO2        0x20B
155 
156 /*
157 SMB_QUERY_FILE_UNIX_INFO2 is SMB_QUERY_FILE_UNIX_BASIC with create
158 time and file flags appended. The corresponding info level for
159 findfirst/findnext is SMB_FIND_FILE_UNIX_INFO2.
160     Size    Offset  Value
161     ---------------------
162     0      LARGE_INTEGER EndOfFile  File size
163     8      LARGE_INTEGER Blocks     Number of blocks used on disk
164     16     LARGE_INTEGER ChangeTime Attribute change time
165     24     LARGE_INTEGER LastAccessTime           Last access time
166     32     LARGE_INTEGER LastModificationTime     Last modification time
167     40     LARGE_INTEGER Uid        Numeric user id for the owner
168     48     LARGE_INTEGER Gid        Numeric group id of owner
169     56     ULONG Type               Enumeration specifying the file type
170     60     LARGE_INTEGER devmajor   Major device number if type is device
171     68     LARGE_INTEGER devminor   Minor device number if type is device
172     76     LARGE_INTEGER uniqueid   This is a server-assigned unique id
173     84     LARGE_INTEGER permissions		Standard UNIX permissions
174     92     LARGE_INTEGER nlinks			Number of hard links
175     100    LARGE_INTEGER CreationTime		Create/birth time
176     108    ULONG FileFlags          File flags enumeration
177     112    ULONG FileFlagsMask      Mask of valid flags
178 */
179 
180 /* Transact 2 Find First levels */
181 #define SMB_FIND_FILE_UNIX             0x202
182 #define SMB_FIND_FILE_UNIX_INFO2       0x20B /* UNIX File Info2 */
183 
184 #define SMB_FILE_UNIX_INFO2_SIZE 116
185 
186 /*
187  Info level for TRANS2_QFSINFO - returns version of CIFS UNIX extensions, plus
188  64-bits worth of capability fun :-).
189  Use the same info level for TRANS2_SETFSINFO
190 */
191 
192 #define SMB_QUERY_CIFS_UNIX_INFO      0x200
193 #define SMB_SET_CIFS_UNIX_INFO        0x200
194 
195 /* Returns or sets the following.
196 
197   UINT16             major version number
198   UINT16             minor version number
199   LARGE_INTEGER      capability bitfield
200 
201 */
202 
203 #define CIFS_UNIX_MAJOR_VERSION 1
204 #define CIFS_UNIX_MINOR_VERSION 0
205 
206 #define CIFS_UNIX_FCNTL_LOCKS_CAP           0x1
207 #define CIFS_UNIX_POSIX_ACLS_CAP            0x2
208 #define CIFS_UNIX_XATTTR_CAP	            0x4 /* for support of other xattr
209 						namespaces such as system,
210 						security and trusted */
211 #define CIFS_UNIX_EXTATTR_CAP		    0x8 /* for support of chattr
212 						(chflags) and lsattr */
213 #define CIFS_UNIX_POSIX_PATHNAMES_CAP	   0x10 /* Use POSIX pathnames on the wire. */
214 #define CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP	   0x20 /* We can cope with POSIX open/mkdir/unlink etc. */
215 #define CIFS_UNIX_LARGE_READ_CAP           0x40 /* We can cope with 24 bit reads in readX. */
216 #define CIFS_UNIX_LARGE_WRITE_CAP          0x80 /* We can cope with 24 bit writes in writeX. */
217 #define CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP      0x100 /* We can do SPNEGO negotiations for encryption. */
218 #define CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP    0x200 /* We *must* SPNEGO negotiations for encryption. */
219 
220 #define SMB_QUERY_POSIX_FS_INFO     0x201
221 
222 /* Returns FILE_SYSTEM_POSIX_INFO struct as follows
223       (NB   For undefined values return -1 in that field)
224    le32 OptimalTransferSize;    bsize on some os, iosize on other os, This
225 				is a hint to the client about best size. Server
226 				can return -1 if no preference, ie if SMB
227 				negotiated size is adequate for optimal
228 				read/write performance
229    le32 BlockSize; (often 512 bytes) NB: BlockSize * TotalBlocks = disk space
230    le64 TotalBlocks;  redundant with other infolevels but easy to ret here
231    le64 BlocksAvail;  although redundant, easy to return
232    le64 UserBlocksAvail;      bavail
233    le64 TotalFileNodes;
234    le64 FreeFileNodes;
235    le64 FileSysIdentifier;    fsid
236    (NB statfs field Namelen comes from FILE_SYSTEM_ATTRIBUTE_INFO call)
237    (NB statfs field flags can come from FILE_SYSTEM_DEVICE_INFO call)
238 */
239 
240 #define SMB_QUERY_POSIX_WHO_AM_I  0x202 /* QFS Info */
241 /* returns:
242         __u32 flags;  0 = Authenticated user 1 = GUEST
243         __u32 mask;  which flags bits server understands ie 0x0001
244         __u64 unix_user_id;
245         __u64 unix_user_gid;
246         __u32 number_of_supplementary_gids;  may be zero
247         __u32 number_of_sids;  may be zero
248         __u32 length_of_sid_array;  in bytes - may be zero
249         __u32 pad;  reserved - MBZ
250         __u64 gid_array[0];  may be empty
251         __u8 * psid_list  may be empty
252 */
253 
254 /* ... more as we think of them :-). */
255 
256 /* SMB POSIX ACL definitions. */
257 /* Wire format is (all little endian) :
258 
259 [2 bytes]              -     Version number.
260 [2 bytes]              -     Number of ACE entries to follow.
261 [2 bytes]              -     Number of default ACE entries to follow.
262 -------------------------------------
263 ^
264 |
265 ACE entries
266 |
267 v
268 -------------------------------------
269 ^
270 |
271 Default ACE entries
272 |
273 v
274 -------------------------------------
275 
276 Where an ACE entry looks like :
277 
278 [1 byte]           - Entry type.
279 
280 Entry types are :
281 
282 ACL_USER_OBJ            0x01
283 ACL_USER                0x02
284 ACL_GROUP_OBJ           0x04
285 ACL_GROUP               0x08
286 ACL_MASK                0x10
287 ACL_OTHER               0x20
288 
289 [1 byte]          - permissions (perm_t)
290 
291 perm_t types are :
292 
293 ACL_READ                0x04
294 ACL_WRITE               0x02
295 ACL_EXECUTE             0x01
296 
297 [8 bytes]         - uid/gid to apply this permission to.
298 
299 In the same format as the uid/gid fields in the other
300 UNIX extensions definitions. Use 0xFFFFFFFFFFFFFFFF for
301 the MASK and OTHER entry types.
302 
303 If the Number of ACE entries for either file or default ACE's
304 is set to 0xFFFF this means ignore this kind of ACE (and the
305 number of entries sent will be zero.
306 
307 */
308 
309 #define SMB_QUERY_POSIX_WHOAMI     0x202
310 
311 enum smb_whoami_flags {
312     SMB_WHOAMI_GUEST = 0x1 /* Logged in as (or squashed to) guest */
313 };
314 
315 /* Mask of which WHOAMI bits are valid. This should make it easier for clients
316  * to cope with servers that have different sets of WHOAMI flags (as more get
317  * added).
318  */
319 #define SMB_WHOAMI_MASK 0x00000001
320 
321 /*
322    SMBWhoami - Query the user mapping performed by the server for the
323    connected tree. This is a subcommand of the TRANS2_QFSINFO.
324 
325    Returns:
326 	4 bytes unsigned -	mapping flags (smb_whoami_flags)
327 	4 bytes unsigned -	flags mask
328 
329 	8 bytes unsigned -	primary UID
330 	8 bytes unsigned -	primary GID
331 	4 bytes unsigned -	number of supplementary GIDs
332 	4 bytes unsigned -	number of SIDs
333 	4 bytes unsigned -	SID list byte count
334 	4 bytes -		pad / reserved (must be zero)
335 
336 	8 bytes unsigned[] -	list of GIDs (may be empty)
337 	struct dom_sid[] -		list of SIDs (may be empty)
338 */
339 
340 /*
341  * The following trans2 is done between client and server
342  * as a FSINFO call to set up the encryption state for transport
343  * encryption.
344  * This is a subcommand of the TRANS2_QFSINFO.
345  *
346  * The request looks like :
347  *
348  * [data block] -> SPNEGO framed GSSAPI request.
349  *
350  * The reply looks like :
351  *
352  * [data block] -> SPNEGO framed GSSAPI reply - if error
353  *                 is NT_STATUS_OK then we're done, if it's
354  *                 NT_STATUS_MORE_PROCESSING_REQUIRED then the
355  *                 client needs to keep going. If it's an
356  *                 error it can be any NT_STATUS error.
357  *
358  */
359 
360 #define SMB_REQUEST_TRANSPORT_ENCRYPTION     0x203 /* QFSINFO */
361 #define SMB_ENCRYPTION_GSSAPI                0x8000
362 
363 /* The query/set info levels for POSIX ACLs. */
364 #define SMB_QUERY_POSIX_ACL  0x204
365 #define SMB_SET_POSIX_ACL  0x204
366 
367 /* Current on the wire ACL version. */
368 #define SMB_POSIX_ACL_VERSION 1
369 
370 /* ACE entry type. */
371 #define SMB_POSIX_ACL_USER_OBJ            0x01
372 #define SMB_POSIX_ACL_USER                0x02
373 #define SMB_POSIX_ACL_GROUP_OBJ           0x04
374 #define SMB_POSIX_ACL_GROUP               0x08
375 #define SMB_POSIX_ACL_MASK                0x10
376 #define SMB_POSIX_ACL_OTHER               0x20
377 
378 /* perm_t types. */
379 #define SMB_POSIX_ACL_READ                0x04
380 #define SMB_POSIX_ACL_WRITE               0x02
381 #define SMB_POSIX_ACL_EXECUTE             0x01
382 
383 #define SMB_POSIX_ACL_HEADER_SIZE         6
384 #define SMB_POSIX_ACL_ENTRY_SIZE         10
385 
386 #define SMB_POSIX_IGNORE_ACE_ENTRIES	0xFFFF
387 
388 /* Definition of data block of SMB_SET_POSIX_LOCK */
389 /*
390   [2 bytes] lock_type - 0 = Read, 1 = Write, 2 = Unlock
391   [2 bytes] lock_flags - 1 = Wait (only valid for setlock)
392   [4 bytes] pid = locking context.
393   [8 bytes] start = unsigned 64 bits.
394   [8 bytes] length = unsigned 64 bits.
395 */
396 
397 #define POSIX_LOCK_TYPE_OFFSET 0
398 #define POSIX_LOCK_FLAGS_OFFSET 2
399 #define POSIX_LOCK_PID_OFFSET 4
400 #define POSIX_LOCK_START_OFFSET 8
401 #define POSIX_LOCK_LEN_OFFSET 16
402 #define POSIX_LOCK_DATA_SIZE 24
403 
404 #define POSIX_LOCK_FLAG_NOWAIT 0
405 #define POSIX_LOCK_FLAG_WAIT 1
406 
407 #define POSIX_LOCK_TYPE_READ 0
408 #define POSIX_LOCK_TYPE_WRITE 1
409 #define POSIX_LOCK_TYPE_UNLOCK 2
410 
411 /* SMB_POSIX_PATH_OPEN "open_mode" definitions. */
412 #define SMB_O_RDONLY			  0x1
413 #define SMB_O_WRONLY			  0x2
414 #define SMB_O_RDWR			  0x4
415 
416 #define SMB_ACCMODE			  0x7
417 
418 #define SMB_O_CREAT			 0x10
419 #define SMB_O_EXCL			 0x20
420 #define SMB_O_TRUNC			 0x40
421 #define SMB_O_APPEND			 0x80
422 #define SMB_O_SYNC			0x100
423 #define SMB_O_DIRECTORY			0x200
424 #define SMB_O_NOFOLLOW			0x400
425 #define SMB_O_DIRECT			0x800
426 
427 /* Definition of request data block for SMB_POSIX_PATH_OPEN */
428 /*
429   [4 bytes] flags (as smb_ntcreate_Flags).
430   [4 bytes] open_mode			- SMB_O_xxx flags above.
431   [8 bytes] mode_t (permissions)	- same encoding as "Standard UNIX permissions" above in SMB_SET_FILE_UNIX_BASIC.
432   [2 bytes] ret_info_level	- optimization. Info level to be returned.
433 */
434 
435 /* Definition of reply data block for SMB_POSIX_PATH_OPEN */
436 
437 #define SMB_NO_INFO_LEVEL_RETURNED 0xFFFF
438 
439 /*
440   [2 bytes] - flags field. Identical to flags reply for oplock response field in SMBNTCreateX)
441   [2 bytes] - FID returned.
442   [4 bytes] - CreateAction (same as in NTCreateX response).
443   [2 bytes] - reply info level    - as requested or 0xFFFF if not available.
444   [2 bytes] - padding (must be zero)
445   [n bytes] - info level reply  - if available.
446 */
447 
448 /* Definition of request data block for SMB_POSIX_UNLINK */
449 /*
450   [2 bytes] flags (defined below).
451 */
452 
453 #define SMB_POSIX_UNLINK_FILE_TARGET 0
454 #define SMB_POSIX_UNLINK_DIRECTORY_TARGET 1
455 
456 #endif /* __SMB_UNIX_EXT_H__ */
457