avoid-long-windows-shell-timeouts-20040105
[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 /* for tracking in-progress directory searches */
262 typedef struct smb_dirSearch {
263         osi_queue_t q;                  /* queue of all outstanding cookies */
264         osi_mutex_t mx;                 /* just in case the caller screws up */
265         int refCount;                   /* reference count */
266         long cookie;                    /* value returned to the caller */
267         struct cm_scache *scp;          /* vnode of the dir we're searching */
268         long lastTime;                  /* last time we used this */
269         long flags;                     /* flags (see below);
270                                          * locked by smb_globalLock */
271         unsigned short attribute;       /* search attribute
272                                          * (used for extended protocol) */
273         char mask[256];                 /* search mask for V3 */
274 } smb_dirSearch_t;
275
276 #define SMB_DIRSEARCH_DELETE    1       /* delete struct when ref count zero */
277 #define SMB_DIRSEARCH_HITEOF    2       /* perhaps useful for advisory later */
278 #define SMB_DIRSEARCH_SMALLID   4       /* cookie can only be 8 bits, not 16 */
279 #define SMB_DIRSEARCH_BULKST    8       /* get bulk stat info */
280
281 /* type for patching directory listings */
282 typedef struct smb_dirListPatch {
283         osi_queue_t q;
284         char *dptr;             /* ptr to attr, time, data, sizel, sizeh */
285         cm_fid_t fid;
286   cm_dirEntry_t *dep;   /* temp */
287 } smb_dirListPatch_t;
288
289 /* waiting lock list elements */
290 typedef struct smb_waitingLock {
291         osi_queue_t q;
292         smb_vc_t *vcp;
293         smb_packet_t *inp;
294         smb_packet_t *outp;
295         u_long timeRemaining;
296         void *lockp;
297 } smb_waitingLock_t;
298
299 extern smb_waitingLock_t *smb_allWaitingLocks;
300
301 typedef long (smb_proc_t)(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp);
302
303 typedef struct smb_dispatch {
304         smb_proc_t *procp;      /* proc to call */
305         int flags;              /* flags describing function */
306 } smb_dispatch_t;
307
308 #define SMB_DISPATCHFLAG_CHAINED        1       /* this is an _AND_X function */
309 #define SMB_DISPATCHFLAG_NORESPONSE     2       /* don't send the response
310                                                  * packet, typically because
311                                                  * the response was already
312                                                  * sent.
313                                                  */
314 #define SMB_MAX_PATH                    256     /* max path length */
315
316 /* prototypes */
317
318 extern void smb_Init(osi_log_t *logp, char *smbNamep, int useV3, int LANadapt,
319         int nThreads
320 #ifndef DJGPP
321         , void *aMBfunc
322 #endif
323   );
324
325 extern void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime);
326
327 extern void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep);
328
329 extern void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime);
330
331 extern void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime);
332
333 extern void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime);
334
335 extern void smb_UnixTimeFromDosUTime(long *unixTimep, long dosUTime);
336
337 extern smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana);
338
339 extern void smb_ReleaseVC(smb_vc_t *vcp);
340
341 extern smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags);
342
343 extern void smb_ReleaseTID(smb_tid_t *tidp);
344
345 extern smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags);
346
347 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
348
349 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern); 
350
351 extern smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags);
352
353 extern smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern);
354
355 extern void smb_ReleaseUID(smb_user_t *uidp);
356
357 extern cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp);
358
359 extern char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid);
360
361 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
362
363 extern void smb_ReleaseFID(smb_fid_t *fidp);
364
365 extern int smb_FindShare(smb_vc_t *vcp, smb_packet_t *inp, char *shareName, char **pathNamep);
366
367 extern smb_dirSearch_t *smb_FindDirSearchNL(long cookie);
368
369 extern void smb_DeleteDirSearch(smb_dirSearch_t *dsp);
370
371 extern void smb_ReleaseDirSearch(smb_dirSearch_t *dsp);
372
373 extern smb_dirSearch_t *smb_FindDirSearch(long cookie);
374
375 extern smb_dirSearch_t *smb_NewDirSearch(int isV3);
376
377 extern smb_packet_t *smb_CopyPacket(smb_packet_t *packetp);
378
379 extern void smb_FreePacket(smb_packet_t *packetp);
380
381 extern unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp);
382
383 extern void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize);
384
385 extern unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm);
386
387 extern unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset);
388
389 extern void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue);
390
391 extern void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue);
392
393 extern void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep);
394
395 extern void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue);
396
397 extern void smb_StripLastComponent(char *outPathp, char **lastComponentp,
398         char *inPathp);
399
400 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
401
402 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp,
403         int *lengthp);
404
405 extern smb_packet_t *smb_GetResponsePacket(smb_vc_t *vcp, smb_packet_t *inp);
406
407 extern void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp);
408
409 extern void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
410         unsigned char *classp);
411
412 extern void smb_MapNTError(long code, unsigned long *NTStatusp);
413
414 extern void smb_HoldVC(smb_vc_t *vcp);
415
416 /* some globals, too */
417 extern int loggedOut;
418 extern unsigned long loggedOutTime;
419 extern char *loggedOutName;
420 extern smb_user_t *loggedOutUserp;
421
422 extern osi_log_t *smb_logp;
423
424 extern osi_rwlock_t smb_globalLock;
425
426 extern osi_rwlock_t smb_rctLock;
427
428 extern int smb_LogoffTokenTransfer;
429 extern unsigned long smb_LogoffTransferTimeout;
430
431 extern void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp,
432         smb_packet_t *op);
433
434 extern char *myCrt_2Dispatch(int i); 
435
436 extern char *myCrt_2Dispatch(int i);
437
438 extern unsigned int smb_Attributes(cm_scache_t *scp);
439
440 extern int smb_ChainFID(int fid, smb_packet_t *inp);
441
442 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
443
444 extern void smb_ReleaseFID(smb_fid_t *fidp);
445
446 extern unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp);
447
448 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
449
450 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp);
451
452 extern int smb_SUser(cm_user_t *userp);
453
454 #ifndef DJGPP
455 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
456         char *op, cm_user_t *userp, long *readp);
457 #else /* DJGPP */
458 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
459         char *op, cm_user_t *userp, long *readp, int dosflag);
460 #endif /* !DJGPP */
461
462 extern BOOL smb_IsLegalFilename(char *filename);
463
464 /* include other include files */
465 #include "smb3.h"
466 #include "smb_ioctl.h"
467 #include "smb_iocons.h"
468
469 cm_user_t *smb_FindOrCreateUser(smb_vc_t *vcp, char *usern);
470
471 #endif /* whole file */