e0663b7029818eabd008c70697ef16f9374e86d3
[openafs.git] / src / WINNT / afsd / smb.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #ifndef __SMB_H_ENV__
11 #define __SMB_H_ENV__ 1
12
13 #ifdef DJGPP
14 #include "netbios95.h"
15 #endif /* DJGPP */
16
17 /* basic core protocol SMB structure */
18 typedef struct smb {
19         unsigned char id[4];
20         unsigned char com;
21         unsigned char rcls;
22         unsigned char reh;
23         unsigned char errLow;
24         unsigned char errHigh;
25         unsigned char reb;
26         unsigned short flg2;
27         unsigned short res[6];
28         unsigned short tid;
29         unsigned short pid;
30         unsigned short uid;
31         unsigned short mid;
32         unsigned char wct;
33         unsigned char vdata[1];
34 } smb_t;
35
36 /* more defines */
37 #define SMB_NOPCODES            256     /* # of opcodes in the dispatch table */
38
39 /* threads per VC */
40 #define SMB_THREADSPERVC        4       /* threads per VC */
41
42 /* flags for functions */
43 #define SMB_FLAG_CREATE         1       /* create the structure if necessary */
44
45 /* max # of bytes we'll receive in an incoming SMB message */
46 #define SMB_PACKETSIZE  8400
47
48 /* a packet structure for receiving SMB messages; locked by smb_globalLock.
49  * Most of the work involved is in handling chained requests and responses.
50  *
51  * When handling input, inWctp points to the current request's wct field (and
52  * the other parameters and request data can be found from this field).  The
53  * opcode, unfortunately, isn't available there, so is instead copied to the
54  * packet's inCom field.  It is initially set to com, but each chained
55  * operation sets it, also.
56  * The function smb_AdvanceInput advances an input packet to the next request
57  * in the chain.  The inCom field is set to 0xFF when there are no more
58  * requests.  The inCount field is 0 if this is the first request, and
59  * otherwise counts which request it is.
60  *
61  * When handling output, we also have to chain all of the responses together.
62  * The function smb_GetResponsePacket will setup outWctp to point to the right
63  * place.
64  */
65 #define SMB_PACKETMAGIC 0x7436353       /* magic # for packets */
66 typedef struct smb_packet {
67         char data[SMB_PACKETSIZE];
68         struct smb_packet *nextp;       /* in free list, or whatever */
69         long magic;
70         cm_space_t *spacep;             /* use this for stripping last component */
71         NCB *ncbp;                      /* use this for sending */
72         struct smb_vc *vcp;
73         unsigned long resumeCode;
74         unsigned short inCount;
75         unsigned short fid;             /* for calls bundled with openAndX */
76         unsigned char *wctp;
77         unsigned char inCom;
78         unsigned char oddByte;
79         unsigned short ncb_length;
80         unsigned char flags;
81 #ifdef DJGPP
82         dos_ptr dos_pkt;
83         unsigned int dos_pkt_sel;
84 #endif /* DJGPP */
85 } smb_packet_t;
86
87 /* smb_packet flags */
88 #define SMB_PACKETFLAG_PROFILE_UPDATE_OK        1
89 #define SMB_PACKETFLAG_NOSEND                   2
90 #define SMB_PACKETFLAG_SUSPENDED                4
91
92 /* a structure for making Netbios calls; locked by smb_globalLock */
93 #define SMB_NCBMAGIC    0x2334344
94 typedef struct myncb {
95         NCB ncb;                        /* ncb to use */
96         struct myncb *nextp;            /* when on free list */
97         long magic;
98 #ifdef DJGPP
99         dos_ptr dos_ncb;
100         smb_packet_t *orig_pkt;
101         unsigned int dos_ncb_sel;
102 #endif /* DJGPP */
103 } smb_ncb_t;
104
105 /* structures representing environments from kernel / SMB network.
106  * Most have their own locks, but the tree connection fields and
107  * reference counts are locked by the smb_rctLock.  Those fields will
108  * be marked in comments.
109  */
110
111 /* one per virtual circuit */
112 typedef struct smb_vc {
113         struct smb_vc *nextp;           /* not used */
114         int refCount;                   /* the reference count */
115         long flags;                     /* the flags, if any; locked by mx */
116         osi_mutex_t mx;                 /* the mutex */
117         long vcID;                      /* VC id */
118         unsigned short lsn;             /* the NCB LSN associated with this */
119         unsigned short uidCounter;      /* session ID counter */
120         unsigned short tidCounter;      /* tree ID counter */
121         unsigned short fidCounter;      /* file handle ID counter */
122         struct smb_tid *tidsp;          /* the first child in the tid list */
123         struct smb_user *usersp;        /* the first child in the user session list */
124         struct smb_fid *fidsp;          /* the first child in the open file list */
125         struct smb_user *justLoggedOut; /* ready for profile upload? */
126         unsigned long logoffTime;       /* tick count when logged off */
127         /*struct cm_user *logonDLLUser; /* integrated logon user */
128         unsigned char errorCount;
129         char rname[17];
130         int lana;
131 } smb_vc_t;
132
133                                         /* have we negotiated ... */
134 #define SMB_VCFLAG_USEV3        1       /* ... version 3 of the protocol */
135 #define SMB_VCFLAG_USECORE      2       /* ... the core protocol */
136 #define SMB_VCFLAG_USENT        4       /* ... NT LM 0.12 or beyond */
137 #define SMB_VCFLAG_STATUS32     8       /* use 32-bit NT status codes */
138 #define SMB_VCFLAG_REMOTECONN   0x10    /* bad: remote conns not allowed */
139 #define SMB_VCFLAG_ALREADYDEAD  0x20    /* do not get tokens from this vc */
140
141 /* one per user session */
142 typedef struct smb_user {
143         struct smb_user *nextp;         /* next sibling */
144         long refCount;                  /* ref count */
145         long flags;                     /* flags; locked by mx */
146         osi_mutex_t mx;
147         long userID;                    /* the session identifier */
148         struct smb_vc *vcp;             /* back ptr to virtual circuit */
149   struct smb_username *unp;        /* user name struct */
150 } smb_user_t;
151
152 typedef struct smb_username {
153         struct smb_username *nextp;             /* next sibling */
154         long refCount;                  /* ref count */
155         long flags;                     /* flags; locked by mx */
156         osi_mutex_t mx;
157         struct cm_user *userp;          /* CM user structure */
158         char *name;                     /* user name */
159   char *machine;                  /* machine name */
160 } smb_username_t;
161
162 #define SMB_USERFLAG_DELETE     1       /* delete struct when ref count zero */
163
164 /* one per tree-connect */
165 typedef struct smb_tid {
166         struct smb_tid *nextp;          /* next sibling */
167         long refCount;
168         long flags;
169         osi_mutex_t mx;                 /* for non-tree-related stuff */
170         unsigned short tid;             /* the tid */
171         struct smb_vc *vcp;             /* back ptr */
172         struct cm_user *userp;          /* user logged in at the
173                                          * tree connect level (base) */
174         char *pathname;                 /* pathname derived from sharename */
175 } smb_tid_t;
176
177 #define SMB_TIDFLAG_DELETE      1       /* delete struct when ref count zero */
178
179 /* one per process ID */
180 typedef struct smb_pid {
181         struct smb_pid *nextp;          /* next sibling */
182         long refCount;
183         long flags;
184         osi_mutex_t mx;                 /* for non-tree-related stuff */
185         unsigned short pid;             /* the pid */
186         struct smb_tid *tidp;           /* back ptr */
187 } smb_pid_t;
188
189 /* ioctl parameter, while being assembled and/or processed */
190 typedef struct smb_ioctl {
191         /* input side */
192         char *inDatap;                  /* ioctl func's current position
193                                          * in input parameter block */
194         char *inAllocp;                 /* allocated input parameter block */
195         long inCopied;                  /* # of input bytes copied in so far
196                                          * by write calls */
197         cm_space_t *prefix;             /* prefix for subst drives */
198         char *tidPathp;                 /* Pathname associated with Tree ID */
199
200         /* output side */
201         char *outDatap;                 /* output results assembled so far */
202         char *outAllocp;                /* output results assembled so far */
203         long outCopied;                 /* # of output bytes copied back so far
204                                          * by read calls */
205         
206         /* flags */
207         long flags;
208
209         /* fid pointer */
210         struct smb_fid *fidp;
211
212   /* uid pointer */
213   smb_user_t *uidp;
214
215 } smb_ioctl_t;
216
217 /* flags for smb_ioctl_t */
218 #define SMB_IOCTLFLAG_DATAIN    1       /* reading data from client to server */
219 #define SMB_IOCTLFLAG_LOGON     2       /* got tokens from integrated logon */
220
221 /* one per file ID; these are really file descriptors */
222 typedef struct smb_fid {
223         osi_queue_t q;
224         long refCount;
225         long flags;
226         osi_mutex_t mx;                 /* for non-tree-related stuff */
227         unsigned short fid;             /* the file ID */
228         struct smb_vc *vcp;             /* back ptr */
229         struct cm_scache *scp;          /* scache of open file */
230         long offset;                    /* our file pointer */
231         smb_ioctl_t *ioctlp;            /* ptr to ioctl structure */
232                                         /* Under NT, we may need to know the
233                                          * parent directory and pathname used
234                                          * to open the file, either to delete
235                                          * the file on close, or to do a
236                                          * change notification */
237         struct cm_scache *NTopen_dscp;  /* parent directory (NT) */
238         char *NTopen_pathp;             /* path used in open (NT) */
239         char *NTopen_wholepathp;        /* entire path, not just last name */
240         int curr_chunk;                 /* chunk being read */
241         int prev_chunk;                 /* previous chunk read */
242         int raw_writers;                /* pending async raw writes */
243         EVENT_HANDLE raw_write_event;   /* signal this when raw_writers zero */
244 } smb_fid_t;
245
246 #define SMB_FID_OPENREAD                1       /* open for reading */
247 #define SMB_FID_OPENWRITE               2       /* open for writing */
248 #define SMB_FID_DELETE                  4       /* delete struct on ref count 0 */
249 #define SMB_FID_IOCTL                   8       /* a file descriptor for the
250                                                  * magic ioctl file */
251 #define SMB_FID_OPENDELETE              0x10    /* open for deletion (NT) */
252 #define SMB_FID_DELONCLOSE              0x20    /* marked for deletion */
253 /*
254  * Now some special flags to work around a bug in NT Client
255  */
256 #define SMB_FID_LENGTHSETDONE           0x40    /* have done 0-length write */
257 #define SMB_FID_MTIMESETDONE            0x80    /* have set modtime via Tr2 */
258 #define SMB_FID_LOOKSLIKECOPY   (SMB_FID_LENGTHSETDONE | SMB_FID_MTIMESETDONE)
259 #define SMB_FID_NTOPEN                  0x100   /* have dscp and pathp */
260
261 /*
262  * SMB file attributes
263  */
264 #define SMB_ATTR_ARCHIVE  0x20
265 #define SMB_ATTR_COMPRESSED 0x800 /* file or dir is compressed */
266 #define SMB_ATTR_NORMAL 0x80 /* normal file. Only valid if used alone */
267 #define SMB_ATTR_HIDDEN 0x2 /* hidden file for the purpose of dir listings */
268 #define SMB_ATTR_READONLY 0x1
269 #define SMB_ATTR_TEMPORARY 0x100
270 #define SMB_ATTR_DIRECTORY 0x10
271 #define SMB_ATTR_SYSTEM 0x4
272
273 /* for tracking in-progress directory searches */
274 typedef struct smb_dirSearch {
275         osi_queue_t q;                  /* queue of all outstanding cookies */
276         osi_mutex_t mx;                 /* just in case the caller screws up */
277         int refCount;                   /* reference count */
278         long cookie;                    /* value returned to the caller */
279         struct cm_scache *scp;          /* vnode of the dir we're searching */
280         long lastTime;                  /* last time we used this */
281         long flags;                     /* flags (see below);
282                                          * locked by smb_globalLock */
283         unsigned short attribute;       /* search attribute
284                                          * (used for extended protocol) */
285         char mask[256];                 /* search mask for V3 */
286 } smb_dirSearch_t;
287
288 #define SMB_DIRSEARCH_DELETE    1       /* delete struct when ref count zero */
289 #define SMB_DIRSEARCH_HITEOF    2       /* perhaps useful for advisory later */
290 #define SMB_DIRSEARCH_SMALLID   4       /* cookie can only be 8 bits, not 16 */
291 #define SMB_DIRSEARCH_BULKST    8       /* get bulk stat info */
292
293 /* type for patching directory listings */
294 typedef struct smb_dirListPatch {
295         osi_queue_t q;
296     char *dptr;         /* ptr to attr, time, data, sizel, sizeh */
297     long flags;     /* flags.  See below */
298         cm_fid_t fid;
299   cm_dirEntry_t *dep;   /* temp */
300 } smb_dirListPatch_t;
301
302 /* dirListPatch Flags */
303 #define SMB_DIRLISTPATCH_DOTFILE 1  /* the file referenced is a dot file 
304                                                                            Note: will not be set if smb_hideDotFiles is false */
305
306 /* waiting lock list elements */
307 typedef struct smb_waitingLock {
308         osi_queue_t q;
309         smb_vc_t *vcp;
310         smb_packet_t *inp;
311         smb_packet_t *outp;
312         u_long timeRemaining;
313         void *lockp;
314 } smb_waitingLock_t;
315
316 extern smb_waitingLock_t *smb_allWaitingLocks;
317
318 typedef long (smb_proc_t)(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp);
319
320 typedef struct smb_dispatch {
321         smb_proc_t *procp;      /* proc to call */
322         int flags;              /* flags describing function */
323 } smb_dispatch_t;
324
325 #define SMB_DISPATCHFLAG_CHAINED        1       /* this is an _AND_X function */
326 #define SMB_DISPATCHFLAG_NORESPONSE     2       /* don't send the response
327                                                  * packet, typically because
328                                                  * the response was already
329                                                  * sent.
330                                                  */
331 #define SMB_MAX_PATH                    256     /* max path length */
332
333 /* prototypes */
334
335 extern void smb_Init(osi_log_t *logp, char *smbNamep, int useV3, int LANadapt,
336         int nThreads
337 #ifndef DJGPP
338         , void *aMBfunc
339 #endif
340   );
341
342 extern void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime);
343
344 extern void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep);
345
346 extern void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime);
347
348 extern void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime);
349
350 extern void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime);
351
352 extern void smb_UnixTimeFromDosUTime(long *unixTimep, long dosUTime);
353
354 extern smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana);
355
356 extern void smb_ReleaseVC(smb_vc_t *vcp);
357
358 extern smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags);
359
360 extern void smb_ReleaseTID(smb_tid_t *tidp);
361
362 extern smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags);
363
364 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
365
366 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern); 
367
368 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
369
370 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern);
371
372 extern void smb_ReleaseUID(smb_user_t *uidp);
373
374 extern cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp);
375
376 extern char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid);
377
378 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
379
380 extern void smb_ReleaseFID(smb_fid_t *fidp);
381
382 extern int smb_FindShare(smb_vc_t *vcp, smb_packet_t *inp, char *shareName, char **pathNamep);
383
384 extern smb_dirSearch_t *smb_FindDirSearchNL(long cookie);
385
386 extern void smb_DeleteDirSearch(smb_dirSearch_t *dsp);
387
388 extern void smb_ReleaseDirSearch(smb_dirSearch_t *dsp);
389
390 extern smb_dirSearch_t *smb_FindDirSearch(long cookie);
391
392 extern smb_dirSearch_t *smb_NewDirSearch(int isV3);
393
394 extern smb_packet_t *smb_CopyPacket(smb_packet_t *packetp);
395
396 extern void smb_FreePacket(smb_packet_t *packetp);
397
398 extern unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp);
399
400 extern void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize);
401
402 extern unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm);
403
404 extern unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset);
405
406 extern void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue);
407
408 extern void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue);
409
410 extern void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep);
411
412 extern void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue);
413
414 extern void smb_StripLastComponent(char *outPathp, char **lastComponentp,
415         char *inPathp);
416
417 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
418
419 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp,
420         int *lengthp);
421
422 extern smb_packet_t *smb_GetResponsePacket(smb_vc_t *vcp, smb_packet_t *inp);
423
424 extern void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp);
425
426 extern void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
427         unsigned char *classp);
428
429 extern void smb_MapNTError(long code, unsigned long *NTStatusp);
430
431 extern void smb_HoldVC(smb_vc_t *vcp);
432
433 /* some globals, too */
434 extern int loggedOut;
435 extern unsigned long loggedOutTime;
436 extern char *loggedOutName;
437 extern smb_user_t *loggedOutUserp;
438
439 extern osi_log_t *smb_logp;
440
441 extern osi_rwlock_t smb_globalLock;
442
443 extern osi_rwlock_t smb_rctLock;
444
445 extern int smb_LogoffTokenTransfer;
446 extern unsigned long smb_LogoffTransferTimeout;
447
448 extern int smb_maxVCPerServer; /* max # of VCs per server */
449 extern int smb_maxMpxRequests; /* max # of mpx requests */
450
451 extern int smb_hideDotFiles;
452 extern unsigned int smb_IsDotFile(char *lastComp);
453
454 extern void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp,
455         smb_packet_t *op);
456
457 extern char *myCrt_2Dispatch(int i); 
458
459 extern char *myCrt_2Dispatch(int i);
460
461 extern unsigned int smb_Attributes(cm_scache_t *scp);
462
463 extern int smb_ChainFID(int fid, smb_packet_t *inp);
464
465 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
466
467 extern void smb_ReleaseFID(smb_fid_t *fidp);
468
469 extern unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp);
470
471 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
472
473 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp);
474
475 extern int smb_SUser(cm_user_t *userp);
476
477 #ifndef DJGPP
478 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
479         char *op, cm_user_t *userp, long *readp);
480 #else /* DJGPP */
481 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
482         char *op, cm_user_t *userp, long *readp, int dosflag);
483 #endif /* !DJGPP */
484
485 extern BOOL smb_IsLegalFilename(char *filename);
486
487 extern char *smb_GetSharename(void);
488
489 /* include other include files */
490 #include "smb3.h"
491 #include "smb_ioctl.h"
492 #include "smb_iocons.h"
493
494 cm_user_t *smb_FindOrCreateUser(smb_vc_t *vcp, char *usern);
495
496 #ifdef NOTSERVICE
497 extern void smb_LogPacket(smb_packet_t *packet);
498 #endif /* NOTSERVICE */
499 #endif /* whole file */