This commit was generated by cvs2svn to compensate for changes in r30,
[samba.git] / source4 / include / context.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 user_context {
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 vuid;
38
39         /* the domain name, user name etc - mostly used in % substitutions */
40         struct userdom_struct *user;
41
42         struct user_struct *vuser;
43 };
44
45 /* the context for a single SMB request. This is passed to any request-context 
46    functions */
47 struct request_context {
48         /* the server_context contains all context specific to this SMB socket */
49         struct server_context *smb;
50
51         /* conn is only set for operations that have a valid TID */
52         struct tcon_context *conn;
53
54         /* the user context is derived from the vuid plus smb.conf options */
55         struct user_context *user_ctx;
56
57         /* a talloc context for the lifetime of this request */
58         TALLOC_CTX *mem_ctx;
59
60         /* a set of flags to control usage of the request. See REQ_CONTROL_* */
61         unsigned control_flags;
62
63         /* the smb pid is needed for locking contexts */
64         uint16 smbpid;
65
66         /* the flags from the SMB request, in raw form (host byte order) */
67         uint16 flags, flags2;
68
69         /* the system time when the request arrived */
70         struct timeval request_time;
71
72         /* this can contain a fnum from an earlier part of a chained
73          * message (such as an SMBOpenX), or -1 */
74         int chained_fnum;
75
76         /* how far through the chain of SMB commands have we gone? */
77         unsigned chain_count;
78
79         /* the async structure allows backend functions to delay
80            replying to requests. To use this, the front end must set
81            async.send_fn to a function to be called by the backend
82            when the reply is finally ready to be sent. The backend
83            must set async.status to the status it wants in the
84            reply. The backend must set the REQ_CONTROL_ASYNC
85            control_flag on the request to indicate that it wishes to
86            delay the reply
87
88            If async.send_fn is NULL then the backend cannot ask for a
89            delayed reply for this request
90
91            note that the async.private pointer is private to the front
92            end not the backend. The backend must not change it.
93         */
94         struct {
95                 void (*send_fn)(struct request_context *);
96                 void *private;
97                 NTSTATUS status;
98         } async;
99
100         struct {
101                 /* the raw SMB buffer, including the 4 byte length header */
102                 char *buffer;
103                 
104                 /* the size of the raw buffer, including 4 byte header */
105                 unsigned size;
106
107                 /* how much has been allocated - on reply the buffer is over-allocated to 
108                    prevent too many realloc() calls 
109                 */
110                 unsigned allocated;
111
112                 /* the start of the SMB header - this is always buffer+4 */
113                 char *hdr;
114
115                 /* the command words and command word count. vwv points
116                    into the raw buffer */
117                 char *vwv;
118                 unsigned wct;
119
120                 /* the data buffer and size. data points into the raw buffer */
121                 char *data;
122                 unsigned data_size;
123
124                 /* ptr is used as a moving pointer into the data area
125                  * of the packet. The reason its here and not a local
126                  * variable in each function is that when a realloc of
127                  * a reply packet is done we need to move this
128                  * pointer */
129                 char *ptr;
130         } in, out;
131 };
132
133
134
135 /* the context associated with open files on an smb socket */
136 struct files_context {
137         struct files_struct *files; /* open files */
138         struct bitmap *file_bmap; /* bitmap used to allocate file handles */
139
140         /* a fsp to use when chaining */
141         struct files_struct *chain_fsp;
142
143         /* a fsp to use to save when breaking an oplock. */
144         struct files_struct *oplock_save_chain_fsp;
145
146         /* how many files are open */
147         int files_used;
148
149         /* limit for maximum open files */
150         int real_max_open_files;
151 };
152
153
154 /* the context associated with open tree connects on a smb socket */
155 struct tree_context {
156         struct tcon_context *connections;
157
158         /* number of open connections */
159         struct bitmap *bmap;
160         int num_open;
161 };
162
163 /* context associated with currently valid session setups */
164 struct users_context {
165         /* users from session setup */
166         char *session_users; /* was a pstring */
167
168         /* this holds info on user ids that are already validated for this VC */
169         struct user_struct *validated_users;
170         int next_vuid; /* initialise to VUID_OFFSET */
171         int num_validated_vuids;
172 };
173
174
175 /* this contains variables that should be used in % substitutions for
176  * smb.conf parameters */
177 struct substitute_context {
178         char *remote_arch;
179
180         /* our local netbios name, as give to us by the client */
181         char *local_machine;
182
183         /* the remote netbios name, as give to us by the client */
184         char *remote_machine;
185
186         /* the select remote protocol */
187         char *remote_proto;     
188
189         /* the name of the client as should be displayed in
190          * smbstatus. Can be an IP or a netbios name */
191         char *client_name; 
192
193         /* the username for %U */
194         char *user_name;
195 };
196
197 /* context that has been negotiated between the client and server */
198 struct negotiate_context {
199         /* have we already done the NBT session establishment? */
200         BOOL done_nbt_session;
201
202         /* only one negprot per connection is allowed */
203         BOOL done_negprot;
204
205         /* multiple session setups are allowed, but some parameters are
206            ignored in any but the first */
207         BOOL done_sesssetup;
208         
209         /* 
210          * Size of data we can send to client. Set
211          *  by the client for all protocols above CORE.
212          *  Set by us for CORE protocol.
213          */
214         unsigned max_send; /* init to BUFFER_SIZE */
215
216         /*
217          * Size of the data we can receive. Set by us.
218          * Can be modified by the max xmit parameter.
219          */
220         unsigned max_recv; /* init to BUFFER_SIZE */
221
222         /* a guess at the remote architecture. Try not to rely on this - in almost
223            all cases using these values is the wrong thing to do */
224         enum remote_arch_types ra_type;
225
226         /* the negotiatiated protocol */
227         enum protocol_types protocol;
228
229         /* authentication context for multi-part negprot */
230         struct auth_context *auth_context;
231
232         /* state of NTLMSSP auth */
233         struct auth_ntlmssp_state *ntlmssp_state;
234
235         /* did we tell the client we support encrypted passwords? */
236         BOOL encrypted_passwords;
237
238         /* did we send an extended security negprot reply? */
239         BOOL spnego_negotiated;
240
241         /* client capabilities */
242         uint32 client_caps;
243 };
244         
245 /* this is the context for a SMB socket associated with the socket itself */
246 struct socket_context {
247         /* the open file descriptor */
248         int fd; 
249
250         /* the last read error on the socket, if any (replaces smb_read_error global) */
251         int read_error;
252
253         /* a count of the number of packets we have received. We
254          * actually only care about zero/non-zero at this stage */
255         unsigned pkt_count;
256
257         /* the network address of the client */
258         char *client_addr;
259 };
260
261
262 /* this holds long term state specific to the printing subsystem */
263 struct printing_context {
264         struct notify_queue *notify_queue_head;
265 };
266
267
268 /* the server_context holds a linked list of pending requests,
269  * this is used for blocking locks and requests blocked due to oplock
270  * break requests */
271 struct pending_request {
272         struct pending_request *next, *prev;
273
274         /* the request itself - needs to be freed */
275         struct request_context *request;
276 };
277
278 /* the timers context contains info on when we last did various
279  * functions */
280 struct timers_context {
281         /* when did we last do timeout processing? */
282         time_t last_timeout_processing;
283
284         /* when did we last sent a keepalive */
285         time_t last_keepalive_sent;
286         
287         /* when we last checked the smb.conf for auto-reload */
288         time_t last_smb_conf_reload;
289 };
290
291
292 /* the process model operations structure - contains function pointers to 
293    the model-specific implementations of each operation */
294 struct model_ops {
295         /* called at startup when the model is selected */
296         void (*model_startup)(void);
297
298         /* function to accept new connection */
299         void (*accept_connection)(struct event_context *, struct fd_event *, time_t, uint16);
300                                 
301         /* function to terminate a connection */
302         void (*terminate_connection)(struct server_context *smb, const char *reason);
303         
304         /* function to exit server */
305         void (*exit_server)(struct server_context *smb, const char *reason);
306         
307         /* returns process or thread id */
308         int (*get_id)(struct request_context *req);
309 };
310
311
312 /* smb context structure. This should contain all the state
313  * information associated with a SMB server */
314 struct server_context {
315         /* a talloc context for all data in this structure */
316         TALLOC_CTX *mem_ctx;
317
318         struct negotiate_context negotiate;
319
320         struct substitute_context substitute;
321
322         struct socket_context socket;
323
324         struct files_context file;
325
326         struct tree_context tree;
327
328         struct users_context users;
329
330         struct printing_context print;
331
332         struct timers_context timers;
333
334         /* the pid of the process handling this session */
335         pid_t pid;
336         
337         /* pointer make to smbd daemon context */
338         struct smbd_context *smbd;
339         
340         /* pointer to list of events that we are waiting on */
341         struct event_context *events;
342
343         /* process model specific operations */
344         struct model_ops *model_ops;
345 };
346