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 unsigned long 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 */
217 /* one per process ID */
218 typedef struct smb_pid {
219 struct smb_pid *nextp; /* next sibling */
222 osi_mutex_t mx; /* for non-tree-related stuff */
223 unsigned short pid; /* the pid */
224 struct smb_tid *tidp; /* back ptr */
227 /* ioctl parameter, while being assembled and/or processed */
228 typedef struct smb_ioctl {
230 char *inDatap; /* ioctl func's current position
231 * in input parameter block */
232 char *inAllocp; /* allocated input parameter block */
233 long inCopied; /* # of input bytes copied in so far
235 cm_space_t *prefix; /* prefix for subst drives */
236 char *tidPathp; /* Pathname associated with Tree ID */
239 char *outDatap; /* output results assembled so far */
240 char *outAllocp; /* output results assembled so far */
241 long outCopied; /* # of output bytes copied back so far
248 struct smb_fid *fidp;
255 /* flags for smb_ioctl_t */
256 #define SMB_IOCTLFLAG_DATAIN 1 /* reading data from client to server */
257 #define SMB_IOCTLFLAG_LOGON 2 /* got tokens from integrated logon */
259 /* one per file ID; these are really file descriptors */
260 typedef struct smb_fid {
264 osi_mutex_t mx; /* for non-tree-related stuff */
265 unsigned short fid; /* the file ID */
266 struct smb_vc *vcp; /* back ptr */
267 struct cm_scache *scp; /* scache of open file */
268 long offset; /* our file pointer */
269 smb_ioctl_t *ioctlp; /* ptr to ioctl structure */
270 /* Under NT, we may need to know the
271 * parent directory and pathname used
272 * to open the file, either to delete
273 * the file on close, or to do a
274 * change notification */
275 struct cm_scache *NTopen_dscp; /* parent directory (NT) */
276 char *NTopen_pathp; /* path used in open (NT) */
277 char *NTopen_wholepathp; /* entire path, not just last name */
278 int curr_chunk; /* chunk being read */
279 int prev_chunk; /* previous chunk read */
280 int raw_writers; /* pending async raw writes */
281 EVENT_HANDLE raw_write_event; /* signal this when raw_writers zero */
284 #define SMB_FID_OPENREAD 1 /* open for reading */
285 #define SMB_FID_OPENWRITE 2 /* open for writing */
286 #define SMB_FID_DELETE 4 /* delete struct on ref count 0 */
287 #define SMB_FID_IOCTL 8 /* a file descriptor for the
288 * magic ioctl file */
289 #define SMB_FID_OPENDELETE 0x10 /* open for deletion (NT) */
290 #define SMB_FID_DELONCLOSE 0x20 /* marked for deletion */
292 * Now some special flags to work around a bug in NT Client
294 #define SMB_FID_LENGTHSETDONE 0x40 /* have done 0-length write */
295 #define SMB_FID_MTIMESETDONE 0x80 /* have set modtime via Tr2 */
296 #define SMB_FID_LOOKSLIKECOPY (SMB_FID_LENGTHSETDONE | SMB_FID_MTIMESETDONE)
297 #define SMB_FID_NTOPEN 0x100 /* have dscp and pathp */
300 * SMB file attributes
302 #define SMB_ATTR_ARCHIVE 0x20
303 #define SMB_ATTR_COMPRESSED 0x800 /* file or dir is compressed */
304 #define SMB_ATTR_NORMAL 0x80 /* normal file. Only valid if used alone */
305 #define SMB_ATTR_HIDDEN 0x2 /* hidden file for the purpose of dir listings */
306 #define SMB_ATTR_READONLY 0x1
307 #define SMB_ATTR_TEMPORARY 0x100
308 #define SMB_ATTR_DIRECTORY 0x10
309 #define SMB_ATTR_SYSTEM 0x4
311 /* for tracking in-progress directory searches */
312 typedef struct smb_dirSearch {
313 osi_queue_t q; /* queue of all outstanding cookies */
314 osi_mutex_t mx; /* just in case the caller screws up */
315 int refCount; /* reference count */
316 long cookie; /* value returned to the caller */
317 struct cm_scache *scp; /* vnode of the dir we're searching */
318 long lastTime; /* last time we used this */
319 long flags; /* flags (see below);
320 * locked by smb_globalLock */
321 unsigned short attribute; /* search attribute
322 * (used for extended protocol) */
323 char mask[256]; /* search mask for V3 */
326 #define SMB_DIRSEARCH_DELETE 1 /* delete struct when ref count zero */
327 #define SMB_DIRSEARCH_HITEOF 2 /* perhaps useful for advisory later */
328 #define SMB_DIRSEARCH_SMALLID 4 /* cookie can only be 8 bits, not 16 */
329 #define SMB_DIRSEARCH_BULKST 8 /* get bulk stat info */
331 /* type for patching directory listings */
332 typedef struct smb_dirListPatch {
334 char *dptr; /* ptr to attr, time, data, sizel, sizeh */
335 long flags; /* flags. See below */
337 cm_dirEntry_t *dep; /* temp */
338 } smb_dirListPatch_t;
340 /* dirListPatch Flags */
341 #define SMB_DIRLISTPATCH_DOTFILE 1 /* the file referenced is a dot file
342 Note: will not be set if smb_hideDotFiles is false */
344 /* waiting lock list elements */
345 typedef struct smb_waitingLock {
350 u_long timeRemaining;
354 extern smb_waitingLock_t *smb_allWaitingLocks;
356 typedef long (smb_proc_t)(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp);
358 typedef struct smb_dispatch {
359 smb_proc_t *procp; /* proc to call */
360 int flags; /* flags describing function */
363 #define SMB_DISPATCHFLAG_CHAINED 1 /* this is an _AND_X function */
364 #define SMB_DISPATCHFLAG_NORESPONSE 2 /* don't send the response
365 * packet, typically because
366 * the response was already
369 #define SMB_MAX_PATH 256 /* max path length */
373 extern void smb_Init(osi_log_t *logp, char *smbNamep, int useV3, int LANadapt,
380 extern void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime);
382 extern void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep);
384 extern void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime);
386 extern void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime);
388 extern void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime);
390 extern void smb_UnixTimeFromDosUTime(long *unixTimep, long dosUTime);
392 extern smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana);
394 extern void smb_ReleaseVC(smb_vc_t *vcp);
396 extern smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags);
398 extern void smb_ReleaseTID(smb_tid_t *tidp);
400 extern smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags);
402 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
404 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern);
406 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
408 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern);
410 extern void smb_ReleaseUID(smb_user_t *uidp);
412 extern cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp);
414 extern char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid);
416 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
418 extern void smb_ReleaseFID(smb_fid_t *fidp);
420 extern int smb_FindShare(smb_vc_t *vcp, smb_user_t *uidp, char *shareName, char **pathNamep);
422 extern int smb_FindShareCSCPolicy(char *shareName);
424 extern smb_dirSearch_t *smb_FindDirSearchNL(long cookie);
426 extern void smb_DeleteDirSearch(smb_dirSearch_t *dsp);
428 extern void smb_ReleaseDirSearch(smb_dirSearch_t *dsp);
430 extern smb_dirSearch_t *smb_FindDirSearch(long cookie);
432 extern smb_dirSearch_t *smb_NewDirSearch(int isV3);
434 extern smb_packet_t *smb_CopyPacket(smb_packet_t *packetp);
436 extern void smb_FreePacket(smb_packet_t *packetp);
438 extern unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp);
440 extern void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize);
442 extern unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm);
444 extern unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset);
446 extern void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue);
448 extern void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue);
450 extern void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep);
452 extern void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue);
454 extern void smb_StripLastComponent(char *outPathp, char **lastComponentp,
457 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
459 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp,
462 extern smb_packet_t *smb_GetResponsePacket(smb_vc_t *vcp, smb_packet_t *inp);
464 extern void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp);
466 extern void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
467 unsigned char *classp);
469 extern void smb_MapNTError(long code, unsigned long *NTStatusp);
471 extern void smb_HoldVC(smb_vc_t *vcp);
473 /* some globals, too */
474 extern int loggedOut;
475 extern unsigned long loggedOutTime;
476 extern char *loggedOutName;
477 extern smb_user_t *loggedOutUserp;
479 extern osi_log_t *smb_logp;
481 extern osi_rwlock_t smb_globalLock;
483 extern osi_rwlock_t smb_rctLock;
485 extern int smb_LogoffTokenTransfer;
486 extern unsigned long smb_LogoffTransferTimeout;
488 extern int smb_maxVCPerServer; /* max # of VCs per server */
489 extern int smb_maxMpxRequests; /* max # of mpx requests */
491 extern int smb_hideDotFiles;
492 extern unsigned int smb_IsDotFile(char *lastComp);
494 /* the following are used for smb auth */
495 extern int smb_authType; /* Type of SMB authentication to be used. One from below. */
497 #define SMB_AUTH_NONE 0
498 #define SMB_AUTH_NTLM 1
499 #define SMB_AUTH_EXTENDED 2
501 extern HANDLE smb_lsaHandle; /* LSA handle obtained during smb_init if using SMB auth */
502 extern ULONG smb_lsaSecPackage; /* LSA security package id. Set during smb_init */
503 extern char smb_ServerDomainName[];
504 extern int smb_ServerDomainNameLength;
505 extern char smb_ServerOS[];
506 extern int smb_ServerOSLength;
507 extern char smb_ServerLanManager[];
508 extern int smb_ServerLanManagerLength;
509 extern GUID smb_ServerGUID;
510 extern LSA_STRING smb_lsaLogonOrigin;
512 /* used for getting a challenge for SMB auth */
513 typedef struct _MSV1_0_LM20_CHALLENGE_REQUEST {
514 MSV1_0_PROTOCOL_MESSAGE_TYPE MessageType;
515 } MSV1_0_LM20_CHALLENGE_REQUEST, *PMSV1_0_LM20_CHALLENGE_REQUEST;
517 typedef struct _MSV1_0_LM20_CHALLENGE_RESPONSE {
518 MSV1_0_PROTOCOL_MESSAGE_TYPE MessageType;
519 UCHAR ChallengeToClient[MSV1_0_CHALLENGE_LENGTH];
520 } MSV1_0_LM20_CHALLENGE_RESPONSE, *PMSV1_0_LM20_CHALLENGE_RESPONSE;
523 extern long smb_AuthenticateUserLM(smb_vc_t *vcp, char * accountName, char * primaryDomain, char * ciPwd, unsigned ciPwdLength, char * csPwd, unsigned csPwdLength);
525 extern long smb_GetNormalizedUsername(char * usern, const char * accountName, const char * domainName);
527 extern void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp,
530 extern char *myCrt_2Dispatch(int i);
532 extern char *myCrt_2Dispatch(int i);
534 extern unsigned int smb_Attributes(cm_scache_t *scp);
536 extern int smb_ChainFID(int fid, smb_packet_t *inp);
538 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
540 extern void smb_ReleaseFID(smb_fid_t *fidp);
542 extern unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp);
544 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
546 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp);
548 extern int smb_SUser(cm_user_t *userp);
551 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
552 char *op, cm_user_t *userp, long *readp);
554 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
555 char *op, cm_user_t *userp, long *readp, int dosflag);
558 extern BOOL smb_IsLegalFilename(char *filename);
560 extern char *smb_GetSharename(void);
562 /* include other include files */
564 #include "smb_ioctl.h"
565 #include "smb_iocons.h"
567 cm_user_t *smb_FindOrCreateUser(smb_vc_t *vcp, char *usern);
570 extern void smb_LogPacket(smb_packet_t *packet);
571 #endif /* NOTSERVICE */
572 #endif /* whole file */