r14542: Remove librpc, libndr and libnbt from includes.h
[gd/samba-autobuild/.git] / source4 / smb_server / session.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Tridgell 1992-2005
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
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 #include "includes.h"
23 #include "smb_server/smb_server.h"
24 #include "dlinklist.h"
25
26
27 /*
28  * init the sessions structures
29  */
30 NTSTATUS smbsrv_init_sessions(struct smbsrv_connection *smb_conn, uint64_t limit)
31 {
32         /* 
33          * the idr_* functions take 'int' as limit,
34          * and only work with a max limit 0x00FFFFFF
35          */
36         limit &= 0x00FFFFFF;
37
38         smb_conn->sessions.idtree_vuid  = idr_init(smb_conn);
39         NT_STATUS_HAVE_NO_MEMORY(smb_conn->sessions.idtree_vuid);
40         smb_conn->sessions.idtree_limit = limit;
41         smb_conn->sessions.list         = NULL;
42
43         return NT_STATUS_OK;
44 }
45
46 /*
47  * Find the session structure assoicated with a VUID
48  * (not one from an in-progress session setup)
49  */
50 struct smbsrv_session *smbsrv_session_find(struct smbsrv_connection *smb_conn, uint64_t vuid)
51 {
52         void *p;
53         struct smbsrv_session *sess;
54
55         if (vuid == 0) return NULL;
56
57         if (vuid > smb_conn->sessions.idtree_limit) return NULL;
58
59         p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
60         if (!p) return NULL;
61
62         /* only return a finished session */
63         sess = talloc_get_type(p, struct smbsrv_session);
64         if (sess && sess->session_info) {
65                 return sess;
66         }
67
68         return NULL;
69 }
70
71 /*
72  * Find the session structure assoicated with a VUID
73  * (assoicated with an in-progress session setup)
74  */
75 struct smbsrv_session *smbsrv_session_find_sesssetup(struct smbsrv_connection *smb_conn, uint64_t vuid)
76 {
77         void *p;
78         struct smbsrv_session *sess;
79
80         if (vuid == 0) return NULL;
81
82         if (vuid > smb_conn->sessions.idtree_limit) return NULL;
83
84         p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
85         if (!p) return NULL;
86
87         /* only return an unfinished session */
88         sess = talloc_get_type(p, struct smbsrv_session);
89         if (sess && !sess->session_info) {
90                 return sess;
91         }
92         return NULL;
93 }
94
95 /*
96  * the session will be marked as valid for usage
97  * by attaching a auth_session_info to the session.
98  *
99  * session_info will be talloc_stealed
100  */
101 NTSTATUS smbsrv_session_sesssetup_finished(struct smbsrv_session *sess,
102                                            struct auth_session_info *session_info)
103 {
104         /* this check is to catch programmer errors */
105         if (!session_info) {
106                 talloc_free(sess);
107                 return NT_STATUS_ACCESS_DENIED;
108         }
109
110         /* mark the session as successful authenticated */
111         sess->session_info = talloc_steal(sess, session_info);
112
113         /* now fill in some statistics */
114         sess->statistics.auth_time = timeval_current();
115
116         return NT_STATUS_OK;
117 }
118
119 /****************************************************************************
120 destroy a session structure
121 ****************************************************************************/
122 static int smbsrv_session_destructor(void *p)
123 {
124         struct smbsrv_session *sess = talloc_get_type(p, struct smbsrv_session);
125         struct smbsrv_connection *smb_conn = sess->smb_conn;
126
127         idr_remove(smb_conn->sessions.idtree_vuid, sess->vuid);
128         DLIST_REMOVE(smb_conn->sessions.list, sess);
129         return 0;
130 }
131
132 /*
133  * allocate a new session structure with a VUID.
134  * gensec_ctx is optional, but talloc_steal'ed when present
135  */
136 struct smbsrv_session *smbsrv_session_new(struct smbsrv_connection *smb_conn,
137                                           struct gensec_security *gensec_ctx)
138 {
139         struct smbsrv_session *sess = NULL;
140         int i;
141
142         /* Ensure no vuid gets registered in share level security. */
143         if (smb_conn->config.security == SEC_SHARE) return NULL;
144
145         sess = talloc_zero(smb_conn, struct smbsrv_session);
146         if (!sess) return NULL;
147         sess->smb_conn = smb_conn;
148
149         i = idr_get_new_random(smb_conn->sessions.idtree_vuid, sess, smb_conn->sessions.idtree_limit);
150         if (i == -1) {
151                 DEBUG(1,("ERROR! Out of connection structures\n"));
152                 talloc_free(sess);
153                 return NULL;
154         }
155         sess->vuid = i;
156
157         /* use this to keep tabs on all our info from the authentication */
158         sess->gensec_ctx = talloc_steal(sess, gensec_ctx);
159
160         DLIST_ADD(smb_conn->sessions.list, sess);
161         talloc_set_destructor(sess, smbsrv_session_destructor);
162
163         /* now fill in some statistics */
164         sess->statistics.connect_time = timeval_current();
165
166         return sess;
167 }