r1281: move include/context.h to smb_server/smb_server.h
[kai/samba.git] / source4 / smb_server / smb_server.h
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Andrew Tridgell              2003
5    Copyright (C) James J Myers                2003 <myersjj@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*
23   this header declares the core context structures associated with smb
24   sockets, tree connects, requests etc
25
26   the idea is that we will eventually get rid of all our global
27   variables and instead store our stang from structures hanging off
28   these basic elements
29 */
30
31 /* the current user context for a request */
32 struct smbsrv_user {
33         /* the vuid is used to specify the security context for this
34            request. Note that this may not be the same vuid as we
35            received on the wire (for example, for share mode or guest
36            access) */
37         uint16_t vuid;
38
39         struct user_struct *vuser;
40 };
41
42
43 /* each backend has to be one one of the following 3 basic types. In
44  * earlier versions of Samba backends needed to handle all types, now
45  * we implement them separately. */
46 enum ntvfs_type {NTVFS_DISK, NTVFS_PRINT, NTVFS_IPC};
47
48 /* we need a forward declaration of the ntvfs_ops strucutre to prevent
49    include recursion */
50 struct ntvfs_ops;
51
52 struct smbsrv_tcon {
53         struct smbsrv_tcon *next, *prev;
54
55         /* the server context that this was created on */
56         struct smbsrv_context *smb_ctx;
57
58         /* a talloc context for all data in this structure */
59         TALLOC_CTX *mem_ctx;
60
61         /* a private structure used by the active NTVFS backend */
62         void *ntvfs_private;
63
64         uint16_t cnum; /* an index passed over the wire (the TID) */
65         int service;
66         enum ntvfs_type type;
67         BOOL read_only;
68         BOOL admin_user;
69
70         /* the NTVFS operations - see source/ntvfs/ and include/ntvfs.h for details */
71         const struct ntvfs_ops *ntvfs_ops;
72
73         /* the reported filesystem type */
74         char *fs_type;
75
76         /* the reported device type */
77         char *dev_type;
78 };
79
80 /* the context for a single SMB request. This is passed to any request-context 
81    functions */
82 struct smbsrv_request {
83         /* the server_context contains all context specific to this SMB socket */
84         struct smbsrv_context *smb_ctx;
85
86         /* conn is only set for operations that have a valid TID */
87         struct smbsrv_tcon *tcon;
88
89         /* the user context is derived from the vuid plus smb.conf options */
90         struct smbsrv_user *user_ctx;
91
92         /* a talloc context for the lifetime of this request */
93         TALLOC_CTX *mem_ctx;
94
95         /* a set of flags to control usage of the request. See REQ_CONTROL_* */
96         unsigned control_flags;
97
98         /* the smb pid is needed for locking contexts */
99         uint16_t smbpid;
100
101         /* the flags from the SMB request, in raw form (host byte order) */
102         uint16_t flags, flags2;
103
104         /* the system time when the request arrived */
105         struct timeval request_time;
106
107         /* this can contain a fnum from an earlier part of a chained
108          * message (such as an SMBOpenX), or -1 */
109         int chained_fnum;
110
111         /* how far through the chain of SMB commands have we gone? */
112         unsigned chain_count;
113
114         /* the sequence number for signing */
115         uint64_t seq_num;
116
117         /* the async structure allows backend functions to delay
118            replying to requests. To use this, the front end must set
119            async.send_fn to a function to be called by the backend
120            when the reply is finally ready to be sent. The backend
121            must set async.status to the status it wants in the
122            reply. The backend must set the REQ_CONTROL_ASYNC
123            control_flag on the request to indicate that it wishes to
124            delay the reply
125
126            If async.send_fn is NULL then the backend cannot ask for a
127            delayed reply for this request
128
129            note that the async.private pointer is private to the front
130            end not the backend. The backend must not change it.
131         */
132         struct {
133                 void (*send_fn)(struct smbsrv_request *);
134                 void *private;
135                 NTSTATUS status;
136         } async;
137
138         struct {
139                 /* the raw SMB buffer, including the 4 byte length header */
140                 char *buffer;
141                 
142                 /* the size of the raw buffer, including 4 byte header */
143                 unsigned size;
144
145                 /* how much has been allocated - on reply the buffer is over-allocated to 
146                    prevent too many realloc() calls 
147                 */
148                 unsigned allocated;
149
150                 /* the start of the SMB header - this is always buffer+4 */
151                 char *hdr;
152
153                 /* the command words and command word count. vwv points
154                    into the raw buffer */
155                 char *vwv;
156                 unsigned wct;
157
158                 /* the data buffer and size. data points into the raw buffer */
159                 char *data;
160                 unsigned data_size;
161
162                 /* ptr is used as a moving pointer into the data area
163                  * of the packet. The reason its here and not a local
164                  * variable in each function is that when a realloc of
165                  * a reply packet is done we need to move this
166                  * pointer */
167                 char *ptr;
168         } in, out;
169 };
170
171
172
173 /* the context associated with open files on an smb socket */
174 struct files_context {
175         struct files_struct *files; /* open files */
176         struct bitmap *file_bmap; /* bitmap used to allocate file handles */
177
178         /* a fsp to use when chaining */
179         struct files_struct *chain_fsp;
180
181         /* a fsp to use to save when breaking an oplock. */
182         struct files_struct *oplock_save_chain_fsp;
183
184         /* how many files are open */
185         int files_used;
186
187         /* limit for maximum open files */
188         int real_max_open_files;
189 };
190
191
192 /* the context associated with open tree connects on a smb socket */
193 struct tree_context {
194         struct smbsrv_tcon *tcons;
195
196         /* number of open connections */
197         struct bitmap *bmap;
198         int num_open;
199 };
200
201 /* context associated with currently valid session setups */
202 struct users_context {
203         /* users from session setup */
204         char *session_users; /* was a pstring */
205
206         /* this holds info on user ids that are already validated for this VC */
207         struct user_struct *validated_users;
208         int next_vuid; /* initialise to VUID_OFFSET */
209         int num_validated_vuids;
210 };
211
212
213 /* this contains variables that should be used in % substitutions for
214  * smb.conf parameters */
215 struct substitute_context {
216         char *remote_arch;
217
218         /* our local netbios name, as give to us by the client */
219         char *local_machine;
220
221         /* the remote netbios name, as give to us by the client */
222         char *remote_machine;
223
224         /* the select remote protocol */
225         char *remote_proto;     
226
227         /* the name of the client as should be displayed in
228          * smbstatus. Can be an IP or a netbios name */
229         char *client_name; 
230
231         /* the username for %U */
232         char *user_name;
233 };
234
235 /* context that has been negotiated between the client and server */
236 struct negotiate_context {
237         /* have we already done the NBT session establishment? */
238         BOOL done_nbt_session;
239
240         /* only one negprot per connection is allowed */
241         BOOL done_negprot;
242
243         /* multiple session setups are allowed, but some parameters are
244            ignored in any but the first */
245         BOOL done_sesssetup;
246         
247         /* 
248          * Size of data we can send to client. Set
249          *  by the client for all protocols above CORE.
250          *  Set by us for CORE protocol.
251          */
252         unsigned max_send; /* init to BUFFER_SIZE */
253
254         /*
255          * Size of the data we can receive. Set by us.
256          * Can be modified by the max xmit parameter.
257          */
258         unsigned max_recv; /* init to BUFFER_SIZE */
259
260         /* a guess at the remote architecture. Try not to rely on this - in almost
261            all cases using these values is the wrong thing to do */
262         enum remote_arch_types ra_type;
263
264         /* the negotiatiated protocol */
265         enum protocol_types protocol;
266
267         /* authentication context for multi-part negprot */
268         struct auth_context *auth_context;
269
270         /* state of NTLMSSP auth */
271         struct auth_ntlmssp_state *ntlmssp_state;
272
273         /* did we tell the client we support encrypted passwords? */
274         BOOL encrypted_passwords;
275
276         /* did we send an extended security negprot reply? */
277         BOOL spnego_negotiated;
278
279         /* client capabilities */
280         uint32_t client_caps;
281
282         /* the timezone we sent to the client */
283         int zone_offset;
284 };
285         
286 /* this is the context for a SMB socket associated with the socket itself */
287 struct socket_context {
288         /* the open file descriptor */
289         int fd; 
290
291         /* the last read error on the socket, if any (replaces smb_read_error global) */
292         int read_error;
293
294         /* a count of the number of packets we have received. We
295          * actually only care about zero/non-zero at this stage */
296         unsigned pkt_count;
297
298         /* the network address of the client */
299         char *client_addr;
300 };
301
302
303 /* this holds long term state specific to the printing subsystem */
304 struct printing_context {
305         struct notify_queue *notify_queue_head;
306 };
307
308
309 /* the server_context holds a linked list of pending requests,
310  * this is used for blocking locks and requests blocked due to oplock
311  * break requests */
312 struct pending_request {
313         struct pending_request *next, *prev;
314
315         /* the request itself - needs to be freed */
316         struct smbsrv_request *request;
317 };
318
319 /* the timers context contains info on when we last did various
320  * functions */
321 struct timers_context {
322         /* when did we last do timeout processing? */
323         time_t last_timeout_processing;
324
325         /* when did we last sent a keepalive */
326         time_t last_keepalive_sent;
327         
328         /* when we last checked the smb.conf for auto-reload */
329         time_t last_smb_conf_reload;
330 };
331
332
333 struct signing_context {
334         DATA_BLOB mac_key;
335         uint64_t next_seq_num;
336         enum smb_signing_state signing_state;
337 };
338
339 #include "smbd/process_model.h"
340
341 /* smb server context structure. This should contain all the state
342  * information associated with a SMB server */
343 struct smbsrv_context {
344         /* a talloc context for all data in this structure */
345         TALLOC_CTX *mem_ctx;
346
347         struct negotiate_context negotiate;
348
349         struct substitute_context substitute;
350
351         struct socket_context socket;
352
353         struct files_context file;
354
355         struct tree_context tree;
356
357         struct users_context users;
358
359         struct printing_context print;
360
361         struct timers_context timers;
362
363         struct dcesrv_context dcesrv;
364
365         struct signing_context signing;
366
367         /* the pid of the process handling this session */
368         pid_t pid;
369         
370         /* pointer to list of events that we are waiting on */
371         struct event_context *events;
372
373         /* process model specific operations */
374         const struct model_ops *model_ops;
375 };