2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 #define __SMB_H_ENV__ 1
14 #include "netbios95.h"
19 /* basic core protocol SMB structure */
26 unsigned char errHigh;
29 unsigned short res[6];
35 unsigned char vdata[1];
39 #define SMB_NOPCODES 256 /* # of opcodes in the dispatch table */
42 #define SMB_THREADSPERVC 4 /* threads per VC */
44 /* flags for functions */
45 #define SMB_FLAG_CREATE 1 /* create the structure if necessary */
47 /* max # of bytes we'll receive in an incoming SMB message */
48 /* the maximum is 2^18-1 for NBT and 2^25-1 for Raw transport messages */
49 /* we will use something smaller but large enough to be efficient */
50 #define SMB_PACKETSIZE 32768 /* was 8400 */
51 /* raw mode is considered obsolete and cannot be used with message signing */
52 #define SMB_MAXRAWSIZE 65536
54 /* Negotiate protocol constants */
56 #define NEGOTIATE_SECURITY_USER_LEVEL 0x01
57 #define NEGOTIATE_SECURITY_CHALLENGE_RESPONSE 0x02
58 #define NEGOTIATE_SECURITY_SIGNATURES_ENABLED 0x04
59 #define NEGOTIATE_SECURITY_SIGNATURES_REQUIRED 0x08
62 #define NTNEGOTIATE_CAPABILITY_RAWMODE 0x00000001L
63 #define NTNEGOTIATE_CAPABILITY_MPXMODE 0x00000002L
64 #define NTNEGOTIATE_CAPABILITY_UNICODE 0x00000004L
65 #define NTNEGOTIATE_CAPABILITY_LARGEFILES 0x00000008L
66 #define NTNEGOTIATE_CAPABILITY_NTSMB 0x00000010L
67 #define NTNEGOTIATE_CAPABILITY_RPCAPI 0x00000020L
68 #define NTNEGOTIATE_CAPABILITY_NTSTATUS 0x00000040L
69 #define NTNEGOTIATE_CAPABILITY_LEVEL_II_OPLOCKS 0x00000080L
70 #define NTNEGOTIATE_CAPABILITY_LOCK_AND_READ 0x00000100L
71 #define NTNEGOTIATE_CAPABILITY_NTFIND 0x00000200L
72 #define NTNEGOTIATE_CAPABILITY_DFS 0x00001000L
73 #define NTNEGOTIATE_CAPABILITY_NT_INFO_PASSTHRU 0x00002000L
74 #define NTNEGOTIATE_CAPABILITY_BULK_TRANSFER 0x20000000L
75 #define NTNEGOTIATE_CAPABILITY_COMPRESSED 0x40000000L
76 #define NTNEGOTIATE_CAPABILITY_EXTENDED_SECURITY 0x80000000L
78 /* a packet structure for receiving SMB messages; locked by smb_globalLock.
79 * Most of the work involved is in handling chained requests and responses.
81 * When handling input, inWctp points to the current request's wct field (and
82 * the other parameters and request data can be found from this field). The
83 * opcode, unfortunately, isn't available there, so is instead copied to the
84 * packet's inCom field. It is initially set to com, but each chained
85 * operation sets it, also.
86 * The function smb_AdvanceInput advances an input packet to the next request
87 * in the chain. The inCom field is set to 0xFF when there are no more
88 * requests. The inCount field is 0 if this is the first request, and
89 * otherwise counts which request it is.
91 * When handling output, we also have to chain all of the responses together.
92 * The function smb_GetResponsePacket will setup outWctp to point to the right
95 #define SMB_PACKETMAGIC 0x7436353 /* magic # for packets */
96 typedef struct smb_packet {
97 char data[SMB_PACKETSIZE];
98 struct smb_packet *nextp; /* in free list, or whatever */
100 cm_space_t *spacep; /* use this for stripping last component */
101 NCB *ncbp; /* use this for sending */
103 unsigned long resumeCode;
104 unsigned short inCount;
105 unsigned short fid; /* for calls bundled with openAndX */
108 unsigned char oddByte;
109 unsigned short ncb_length;
113 unsigned int dos_pkt_sel;
117 /* smb_packet flags */
118 #define SMB_PACKETFLAG_PROFILE_UPDATE_OK 1
119 #define SMB_PACKETFLAG_NOSEND 2
120 #define SMB_PACKETFLAG_SUSPENDED 4
122 /* a structure for making Netbios calls; locked by smb_globalLock */
123 #define SMB_NCBMAGIC 0x2334344
124 typedef struct myncb {
125 NCB ncb; /* ncb to use */
126 struct myncb *nextp; /* when on free list */
130 smb_packet_t *orig_pkt;
131 unsigned int dos_ncb_sel;
135 /* structures representing environments from kernel / SMB network.
136 * Most have their own locks, but the tree connection fields and
137 * reference counts are locked by the smb_rctLock. Those fields will
138 * be marked in comments.
141 /* one per virtual circuit */
142 typedef struct smb_vc {
143 struct smb_vc *nextp; /* not used */
144 int refCount; /* the reference count */
145 long flags; /* the flags, if any; locked by mx */
146 osi_mutex_t mx; /* the mutex */
147 long vcID; /* VC id */
148 unsigned short lsn; /* the NCB LSN associated with this */
149 unsigned short uidCounter; /* session ID counter */
150 unsigned short tidCounter; /* tree ID counter */
151 unsigned short fidCounter; /* file handle ID counter */
152 struct smb_tid *tidsp; /* the first child in the tid list */
153 struct smb_user *usersp; /* the first child in the user session list */
154 struct smb_fid *fidsp; /* the first child in the open file list */
155 struct smb_user *justLoggedOut; /* ready for profile upload? */
156 time_t logoffTime; /* tick count when logged off */
157 /*struct cm_user *logonDLLUser; /* integrated logon user */
158 unsigned char errorCount;
161 char encKey[MSV1_0_CHALLENGE_LENGTH]; /* MSV1_0_CHALLENGE_LENGTH is 8 */
162 void * secCtx; /* security context when negotiating SMB extended auth
163 * valid when SMB_VCFLAG_AUTH_IN_PROGRESS is set
167 /* have we negotiated ... */
168 #define SMB_VCFLAG_USEV3 1 /* ... version 3 of the protocol */
169 #define SMB_VCFLAG_USECORE 2 /* ... the core protocol */
170 #define SMB_VCFLAG_USENT 4 /* ... NT LM 0.12 or beyond */
171 #define SMB_VCFLAG_STATUS32 8 /* use 32-bit NT status codes */
172 #define SMB_VCFLAG_REMOTECONN 0x10 /* bad: remote conns not allowed */
173 #define SMB_VCFLAG_ALREADYDEAD 0x20 /* do not get tokens from this vc */
174 #define SMB_VCFLAG_SESSX_RCVD 0x40 /* we received at least one session setups on this vc */
175 #define SMB_VCFLAG_AUTH_IN_PROGRESS 0x80 /* a SMB NT extended authentication is in progress */
177 /* one per user session */
178 typedef struct smb_user {
179 struct smb_user *nextp; /* next sibling */
180 long refCount; /* ref count */
181 long flags; /* flags; locked by mx */
183 long userID; /* the session identifier */
184 struct smb_vc *vcp; /* back ptr to virtual circuit */
185 struct smb_username *unp; /* user name struct */
188 typedef struct smb_username {
189 struct smb_username *nextp; /* next sibling */
190 long refCount; /* ref count */
191 long flags; /* flags; locked by mx */
193 struct cm_user *userp; /* CM user structure */
194 char *name; /* user name */
195 char *machine; /* machine name */
198 #define SMB_USERFLAG_DELETE 1 /* delete struct when ref count zero */
200 #define SMB_MAX_USERNAME_LENGTH 256
202 /* one per tree-connect */
203 typedef struct smb_tid {
204 struct smb_tid *nextp; /* next sibling */
207 osi_mutex_t mx; /* for non-tree-related stuff */
208 unsigned short tid; /* the tid */
209 struct smb_vc *vcp; /* back ptr */
210 struct cm_user *userp; /* user logged in at the
211 * tree connect level (base) */
212 char *pathname; /* pathname derived from sharename */
215 #define SMB_TIDFLAG_DELETE 1 /* delete struct when ref count zero */
216 #define SMB_TIDFLAG_IPC 2 /* IPC$ */
218 /* one per process ID */
219 typedef struct smb_pid {
220 struct smb_pid *nextp; /* next sibling */
223 osi_mutex_t mx; /* for non-tree-related stuff */
224 unsigned short pid; /* the pid */
225 struct smb_tid *tidp; /* back ptr */
228 /* ioctl parameter, while being assembled and/or processed */
229 typedef struct smb_ioctl {
231 char *inDatap; /* ioctl func's current position
232 * in input parameter block */
233 char *inAllocp; /* allocated input parameter block */
234 long inCopied; /* # of input bytes copied in so far
236 cm_space_t *prefix; /* prefix for subst drives */
237 char *tidPathp; /* Pathname associated with Tree ID */
240 char *outDatap; /* output results assembled so far */
241 char *outAllocp; /* output results assembled so far */
242 long outCopied; /* # of output bytes copied back so far
249 struct smb_fid *fidp;
256 /* flags for smb_ioctl_t */
257 #define SMB_IOCTLFLAG_DATAIN 1 /* reading data from client to server */
258 #define SMB_IOCTLFLAG_LOGON 2 /* got tokens from integrated logon */
260 /* one per file ID; these are really file descriptors */
261 typedef struct smb_fid {
265 osi_mutex_t mx; /* for non-tree-related stuff */
266 unsigned short fid; /* the file ID */
267 struct smb_vc *vcp; /* back ptr */
268 struct cm_scache *scp; /* scache of open file */
269 long offset; /* our file pointer */
270 smb_ioctl_t *ioctlp; /* ptr to ioctl structure */
271 /* Under NT, we may need to know the
272 * parent directory and pathname used
273 * to open the file, either to delete
274 * the file on close, or to do a
275 * change notification */
276 struct cm_scache *NTopen_dscp; /* parent directory (NT) */
277 char *NTopen_pathp; /* path used in open (NT) */
278 char *NTopen_wholepathp; /* entire path, not just last name */
279 int curr_chunk; /* chunk being read */
280 int prev_chunk; /* previous chunk read */
281 int raw_writers; /* pending async raw writes */
282 EVENT_HANDLE raw_write_event; /* signal this when raw_writers zero */
285 #define SMB_FID_OPENREAD 1 /* open for reading */
286 #define SMB_FID_OPENWRITE 2 /* open for writing */
287 #define SMB_FID_DELETE 4 /* delete struct on ref count 0 */
288 #define SMB_FID_IOCTL 8 /* a file descriptor for the
289 * magic ioctl file */
290 #define SMB_FID_OPENDELETE 0x10 /* open for deletion (NT) */
291 #define SMB_FID_DELONCLOSE 0x20 /* marked for deletion */
293 * Now some special flags to work around a bug in NT Client
295 #define SMB_FID_LENGTHSETDONE 0x40 /* have done 0-length write */
296 #define SMB_FID_MTIMESETDONE 0x80 /* have set modtime via Tr2 */
297 #define SMB_FID_LOOKSLIKECOPY (SMB_FID_LENGTHSETDONE | SMB_FID_MTIMESETDONE)
298 #define SMB_FID_NTOPEN 0x100 /* have dscp and pathp */
301 * SMB file attributes
303 #define SMB_ATTR_ARCHIVE 0x20
304 #define SMB_ATTR_COMPRESSED 0x800 /* file or dir is compressed */
305 #define SMB_ATTR_NORMAL 0x80 /* normal file. Only valid if used alone */
306 #define SMB_ATTR_HIDDEN 0x2 /* hidden file for the purpose of dir listings */
307 #define SMB_ATTR_READONLY 0x1
308 #define SMB_ATTR_TEMPORARY 0x100
309 #define SMB_ATTR_DIRECTORY 0x10
310 #define SMB_ATTR_SYSTEM 0x4
312 /* for tracking in-progress directory searches */
313 typedef struct smb_dirSearch {
314 osi_queue_t q; /* queue of all outstanding cookies */
315 osi_mutex_t mx; /* just in case the caller screws up */
316 int refCount; /* reference count */
317 long cookie; /* value returned to the caller */
318 struct cm_scache *scp; /* vnode of the dir we're searching */
319 time_t lastTime; /* last time we used this */
320 long flags; /* flags (see below);
321 * locked by smb_globalLock */
322 unsigned short attribute; /* search attribute
323 * (used for extended protocol) */
324 char mask[256]; /* search mask for V3 */
327 #define SMB_DIRSEARCH_DELETE 1 /* delete struct when ref count zero */
328 #define SMB_DIRSEARCH_HITEOF 2 /* perhaps useful for advisory later */
329 #define SMB_DIRSEARCH_SMALLID 4 /* cookie can only be 8 bits, not 16 */
330 #define SMB_DIRSEARCH_BULKST 8 /* get bulk stat info */
332 /* type for patching directory listings */
333 typedef struct smb_dirListPatch {
335 char *dptr; /* ptr to attr, time, data, sizel, sizeh */
336 long flags; /* flags. See below */
338 cm_dirEntry_t *dep; /* temp */
339 } smb_dirListPatch_t;
341 /* dirListPatch Flags */
342 #define SMB_DIRLISTPATCH_DOTFILE 1
343 /* the file referenced is a dot file
344 * Note: will not be set if smb_hideDotFiles is false
347 /* waiting lock list elements */
348 typedef struct smb_waitingLock {
353 time_t timeRemaining;
357 extern smb_waitingLock_t *smb_allWaitingLocks;
359 typedef long (smb_proc_t)(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp);
361 typedef struct smb_dispatch {
362 smb_proc_t *procp; /* proc to call */
363 int flags; /* flags describing function */
366 #define SMB_DISPATCHFLAG_CHAINED 1 /* this is an _AND_X function */
367 #define SMB_DISPATCHFLAG_NORESPONSE 2 /* don't send the response
368 * packet, typically because
369 * the response was already
372 #define SMB_MAX_PATH 256 /* max path length */
376 extern void smb_Init(osi_log_t *logp, char *smbNamep, int useV3, int LANadapt,
383 extern void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, time_t unixTime);
385 extern void smb_UnixTimeFromLargeSearchTime(time_t *unixTimep, FILETIME *largeTimep);
387 extern void smb_SearchTimeFromUnixTime(time_t *dosTimep, time_t unixTime);
389 extern void smb_UnixTimeFromSearchTime(time_t *unixTimep, time_t searchTime);
391 extern void smb_DosUTimeFromUnixTime(time_t *dosUTimep, time_t unixTime);
393 extern void smb_UnixTimeFromDosUTime(time_t *unixTimep, time_t dosUTime);
395 extern smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana);
397 extern void smb_ReleaseVC(smb_vc_t *vcp);
399 extern smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags);
401 extern void smb_ReleaseTID(smb_tid_t *tidp);
403 extern smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags);
405 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
407 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern);
409 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
411 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern);
413 extern void smb_ReleaseUID(smb_user_t *uidp);
415 extern cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp);
417 extern long smb_LookupTIDPath(smb_vc_t *vcp, unsigned short tid, char ** tidPathp);
419 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
421 extern void smb_ReleaseFID(smb_fid_t *fidp);
423 extern int smb_FindShare(smb_vc_t *vcp, smb_user_t *uidp, char *shareName, char **pathNamep);
425 extern int smb_FindShareCSCPolicy(char *shareName);
427 extern smb_dirSearch_t *smb_FindDirSearchNL(long cookie);
429 extern void smb_DeleteDirSearch(smb_dirSearch_t *dsp);
431 extern void smb_ReleaseDirSearch(smb_dirSearch_t *dsp);
433 extern smb_dirSearch_t *smb_FindDirSearch(long cookie);
435 extern smb_dirSearch_t *smb_NewDirSearch(int isV3);
437 extern smb_packet_t *smb_CopyPacket(smb_packet_t *packetp);
439 extern void smb_FreePacket(smb_packet_t *packetp);
441 extern unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp);
443 extern void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize);
445 extern unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm);
447 extern unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset);
449 extern void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue);
451 extern void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue);
453 extern void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep);
455 extern void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue);
457 extern void smb_StripLastComponent(char *outPathp, char **lastComponentp,
460 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
462 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp,
465 extern smb_packet_t *smb_GetResponsePacket(smb_vc_t *vcp, smb_packet_t *inp);
467 extern void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp);
469 extern void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
470 unsigned char *classp);
472 extern void smb_MapNTError(long code, unsigned long *NTStatusp);
474 extern void smb_HoldVC(smb_vc_t *vcp);
476 /* some globals, too */
477 extern char *smb_localNamep;
478 extern int loggedOut;
479 extern time_t loggedOutTime;
480 extern char *loggedOutName;
481 extern smb_user_t *loggedOutUserp;
483 extern osi_log_t *smb_logp;
485 extern osi_rwlock_t smb_globalLock;
487 extern osi_rwlock_t smb_rctLock;
489 extern int smb_LogoffTokenTransfer;
490 extern time_t smb_LogoffTransferTimeout;
492 extern int smb_maxVCPerServer; /* max # of VCs per server */
493 extern int smb_maxMpxRequests; /* max # of mpx requests */
495 extern int smb_hideDotFiles;
496 extern unsigned int smb_IsDotFile(char *lastComp);
498 /* the following are used for smb auth */
499 extern int smb_authType; /* Type of SMB authentication to be used. One from below. */
501 #define SMB_AUTH_NONE 0
502 #define SMB_AUTH_NTLM 1
503 #define SMB_AUTH_EXTENDED 2
505 extern HANDLE smb_lsaHandle; /* LSA handle obtained during smb_init if using SMB auth */
506 extern ULONG smb_lsaSecPackage; /* LSA security package id. Set during smb_init */
507 extern char smb_ServerDomainName[];
508 extern int smb_ServerDomainNameLength;
509 extern char smb_ServerOS[];
510 extern int smb_ServerOSLength;
511 extern char smb_ServerLanManager[];
512 extern int smb_ServerLanManagerLength;
513 extern GUID smb_ServerGUID;
514 extern LSA_STRING smb_lsaLogonOrigin;
516 /* used for getting a challenge for SMB auth */
517 typedef struct _MSV1_0_LM20_CHALLENGE_REQUEST {
518 MSV1_0_PROTOCOL_MESSAGE_TYPE MessageType;
519 } MSV1_0_LM20_CHALLENGE_REQUEST, *PMSV1_0_LM20_CHALLENGE_REQUEST;
521 typedef struct _MSV1_0_LM20_CHALLENGE_RESPONSE {
522 MSV1_0_PROTOCOL_MESSAGE_TYPE MessageType;
523 UCHAR ChallengeToClient[MSV1_0_CHALLENGE_LENGTH];
524 } MSV1_0_LM20_CHALLENGE_RESPONSE, *PMSV1_0_LM20_CHALLENGE_RESPONSE;
527 extern long smb_AuthenticateUserLM(smb_vc_t *vcp, char * accountName, char * primaryDomain, char * ciPwd, unsigned ciPwdLength, char * csPwd, unsigned csPwdLength);
529 extern long smb_GetNormalizedUsername(char * usern, const char * accountName, const char * domainName);
531 extern void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp,
534 extern char *myCrt_Dispatch(int i);
536 extern char *myCrt_2Dispatch(int i);
538 extern char *myCrt_RapDispatch(int i);
540 extern unsigned int smb_Attributes(cm_scache_t *scp);
542 extern int smb_ChainFID(int fid, smb_packet_t *inp);
544 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
546 extern void smb_ReleaseFID(smb_fid_t *fidp);
548 extern unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp);
550 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
552 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp);
554 extern int smb_SUser(cm_user_t *userp);
557 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
558 char *op, cm_user_t *userp, long *readp);
560 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
561 char *op, cm_user_t *userp, long *readp, int dosflag);
564 extern BOOL smb_IsLegalFilename(char *filename);
566 extern char *smb_GetSharename(void);
568 /* include other include files */
570 #include "smb_ioctl.h"
571 #include "smb_iocons.h"
573 cm_user_t *smb_FindOrCreateUser(smb_vc_t *vcp, char *usern);
576 extern void smb_LogPacket(smb_packet_t *packet);
577 #endif /* NOTSERVICE */
578 #endif /* whole file */