e3a9c2076d84bc5eb2d74f559b8fdf6b6c2d111a
[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 } smb_ioctl_t;
212
213 /* flags for smb_ioctl_t */
214 #define SMB_IOCTLFLAG_DATAIN    1       /* reading data from client to server */
215 #define SMB_IOCTLFLAG_LOGON     2       /* got tokens from integrated logon */
216
217 /* one per file ID; these are really file descriptors */
218 typedef struct smb_fid {
219         osi_queue_t q;
220         long refCount;
221         long flags;
222         osi_mutex_t mx;                 /* for non-tree-related stuff */
223         unsigned short fid;             /* the file ID */
224         struct smb_vc *vcp;             /* back ptr */
225         struct cm_scache *scp;          /* scache of open file */
226         long offset;                    /* our file pointer */
227         smb_ioctl_t *ioctlp;            /* ptr to ioctl structure */
228                                         /* Under NT, we may need to know the
229                                          * parent directory and pathname used
230                                          * to open the file, either to delete
231                                          * the file on close, or to do a
232                                          * change notification */
233         struct cm_scache *NTopen_dscp;  /* parent directory (NT) */
234         char *NTopen_pathp;             /* path used in open (NT) */
235         char *NTopen_wholepathp;        /* entire path, not just last name */
236         int curr_chunk;                 /* chunk being read */
237         int prev_chunk;                 /* previous chunk read */
238         int raw_writers;                /* pending async raw writes */
239         EVENT_HANDLE raw_write_event;   /* signal this when raw_writers zero */
240 } smb_fid_t;
241
242 #define SMB_FID_OPENREAD                1       /* open for reading */
243 #define SMB_FID_OPENWRITE               2       /* open for writing */
244 #define SMB_FID_DELETE                  4       /* delete struct on ref count 0 */
245 #define SMB_FID_IOCTL                   8       /* a file descriptor for the
246                                                  * magic ioctl file */
247 #define SMB_FID_OPENDELETE              0x10    /* open for deletion (NT) */
248 #define SMB_FID_DELONCLOSE              0x20    /* marked for deletion */
249 /*
250  * Now some special flags to work around a bug in NT Client
251  */
252 #define SMB_FID_LENGTHSETDONE           0x40    /* have done 0-length write */
253 #define SMB_FID_MTIMESETDONE            0x80    /* have set modtime via Tr2 */
254 #define SMB_FID_LOOKSLIKECOPY   (SMB_FID_LENGTHSETDONE | SMB_FID_MTIMESETDONE)
255 #define SMB_FID_NTOPEN                  0x100   /* have dscp and pathp */
256
257 /* for tracking in-progress directory searches */
258 typedef struct smb_dirSearch {
259         osi_queue_t q;                  /* queue of all outstanding cookies */
260         osi_mutex_t mx;                 /* just in case the caller screws up */
261         int refCount;                   /* reference count */
262         long cookie;                    /* value returned to the caller */
263         struct cm_scache *scp;          /* vnode of the dir we're searching */
264         long lastTime;                  /* last time we used this */
265         long flags;                     /* flags (see below);
266                                          * locked by smb_globalLock */
267         unsigned short attribute;       /* search attribute
268                                          * (used for extended protocol) */
269         char mask[256];                 /* search mask for V3 */
270 } smb_dirSearch_t;
271
272 #define SMB_DIRSEARCH_DELETE    1       /* delete struct when ref count zero */
273 #define SMB_DIRSEARCH_HITEOF    2       /* perhaps useful for advisory later */
274 #define SMB_DIRSEARCH_SMALLID   4       /* cookie can only be 8 bits, not 16 */
275 #define SMB_DIRSEARCH_BULKST    8       /* get bulk stat info */
276
277 /* type for patching directory listings */
278 typedef struct smb_dirListPatch {
279         osi_queue_t q;
280         char *dptr;             /* ptr to attr, time, data, sizel, sizeh */
281         cm_fid_t fid;
282   cm_dirEntry_t *dep;   /* temp */
283 } smb_dirListPatch_t;
284
285 /* waiting lock list elements */
286 typedef struct smb_waitingLock {
287         osi_queue_t q;
288         smb_vc_t *vcp;
289         smb_packet_t *inp;
290         smb_packet_t *outp;
291         u_long timeRemaining;
292         void *lockp;
293 } smb_waitingLock_t;
294
295 smb_waitingLock_t *smb_allWaitingLocks;
296
297 typedef long (smb_proc_t)(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp);
298
299 typedef struct smb_dispatch {
300         smb_proc_t *procp;      /* proc to call */
301         int flags;              /* flags describing function */
302 } smb_dispatch_t;
303
304 #define SMB_DISPATCHFLAG_CHAINED        1       /* this is an _AND_X function */
305 #define SMB_DISPATCHFLAG_NORESPONSE     2       /* don't send the response
306                                                  * packet, typically because
307                                                  * the response was already
308                                                  * sent.
309                                                  */
310 #define SMB_MAX_PATH                    256     /* max path length */
311
312 /* prototypes */
313
314 extern void smb_Init(osi_log_t *logp, char *smbNamep, int useV3, int LANadapt,
315         int nThreads
316 #ifndef DJGPP
317         , void *aMBfunc
318 #endif
319   );
320
321 extern void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime);
322
323 extern void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep);
324
325 extern void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime);
326
327 extern void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime);
328
329 extern void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime);
330
331 extern void smb_UnixTimeFromDosUTime(long *unixTimep, long dosUTime);
332
333 extern smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana);\r
334
335 extern void smb_ReleaseVC(smb_vc_t *vcp);
336
337 extern smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags);
338
339 extern void smb_ReleaseTID(smb_tid_t *tidp);
340
341 extern smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags);
342
343 extern void smb_ReleaseUID(smb_user_t *uidp);
344
345 extern cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp);
346
347 extern char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid);
348
349 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
350
351 extern void smb_ReleaseFID(smb_fid_t *fidp);
352
353 extern int smb_FindShare(smb_vc_t *vcp, smb_packet_t *inp, char *shareName, char **pathNamep);
354
355 extern smb_dirSearch_t *smb_FindDirSearchNL(long cookie);
356
357 extern void smb_DeleteDirSearch(smb_dirSearch_t *dsp);
358
359 extern void smb_ReleaseDirSearch(smb_dirSearch_t *dsp);
360
361 extern smb_dirSearch_t *smb_FindDirSearch(long cookie);
362
363 extern smb_dirSearch_t *smb_NewDirSearch(int isV3);
364
365 extern smb_packet_t *smb_CopyPacket(smb_packet_t *packetp);
366
367 extern void smb_FreePacket(smb_packet_t *packetp);
368
369 extern unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp);
370
371 extern void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize);
372
373 extern unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm);
374
375 extern unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset);
376
377 extern void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue);
378
379 extern void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue);
380
381 extern void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep);
382
383 extern void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue);
384
385 extern void smb_StripLastComponent(char *outPathp, char **lastComponentp,
386         char *inPathp);
387
388 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
389
390 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp,
391         int *lengthp);
392
393 extern smb_packet_t *smb_GetResponsePacket(smb_vc_t *vcp, smb_packet_t *inp);
394
395 extern void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp);
396
397 extern void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
398         unsigned char *classp);
399
400 extern void smb_MapNTError(long code, unsigned long *NTStatusp);
401
402 extern void smb_HoldVC(smb_vc_t *vcp);
403
404 /* some globals, too */
405 extern int loggedOut;
406 extern unsigned long loggedOutTime;
407 extern char *loggedOutName;
408 extern smb_user_t *loggedOutUserp;
409
410 extern osi_log_t *smb_logp;
411
412 extern osi_rwlock_t smb_globalLock;
413
414 extern osi_rwlock_t smb_rctLock;
415
416 extern int smb_LogoffTokenTransfer;
417 extern unsigned long smb_LogoffTransferTimeout;
418
419 extern void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp,
420         smb_packet_t *op);
421
422 extern unsigned int smb_Attributes(cm_scache_t *scp);
423
424 extern int smb_ChainFID(int fid, smb_packet_t *inp);
425
426 extern smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags);
427
428 extern void smb_ReleaseFID(smb_fid_t *fidp);
429
430 extern unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp);
431
432 extern unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp);
433
434 extern unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp);
435
436 extern int smb_SUser(cm_user_t *userp);
437
438 #ifndef DJGPP
439 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
440         char *op, cm_user_t *userp, long *readp);
441 #else /* DJGPP */
442 extern long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count,
443         char *op, cm_user_t *userp, long *readp, int dosflag);
444 #endif /* !DJGPP */
445
446 extern BOOL smb_IsLegalFilename(char *filename);
447
448 /* include other include files */
449 #include "smb3.h"
450 #include "smb_ioctl.h"
451 #include "smb_iocons.h"
452 \r
453 cm_user_t *smb_FindOrCreateUser(smb_vc_t *vcp, char *usern);\r
454
455 #endif /* whole file */