Found by Guenther - fix up our fallback paths from krb5 to NTLMSSP when using SMB2.
[Samba/ekacnet.git] / source4 / smb_server / session.c
blob53193c5312e95136dc1d4e92902244ed2e4ab5ea
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 Copyright (C) Stefan Metzmacher 2005-2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smb_server/smb_server.h"
27 * init the sessions structures
29 NTSTATUS smbsrv_init_sessions(struct smbsrv_connection *smb_conn, uint64_t limit)
31 /*
32 * the idr_* functions take 'int' as limit,
33 * and only work with a max limit 0x00FFFFFF
35 limit &= 0x00FFFFFF;
37 smb_conn->sessions.idtree_vuid = idr_init(smb_conn);
38 NT_STATUS_HAVE_NO_MEMORY(smb_conn->sessions.idtree_vuid);
39 smb_conn->sessions.idtree_limit = limit;
40 smb_conn->sessions.list = NULL;
42 return NT_STATUS_OK;
46 * Find the session structure assoicated with a VUID
47 * (not one from an in-progress session setup)
49 struct smbsrv_session *smbsrv_session_find(struct smbsrv_connection *smb_conn,
50 uint64_t vuid, struct timeval request_time)
52 void *p;
53 struct smbsrv_session *sess;
55 if (vuid == 0) return NULL;
57 if (vuid > smb_conn->sessions.idtree_limit) return NULL;
59 p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
60 if (!p) return NULL;
62 /* only return a finished session */
63 sess = talloc_get_type(p, struct smbsrv_session);
64 if (sess && sess->session_info) {
65 sess->statistics.last_request_time = request_time;
66 return sess;
69 return NULL;
73 * Find the session structure assoicated with a VUID
74 * (assoicated with an in-progress session setup)
76 struct smbsrv_session *smbsrv_session_find_sesssetup(struct smbsrv_connection *smb_conn, uint64_t vuid)
78 void *p;
79 struct smbsrv_session *sess;
81 if (vuid == 0) return NULL;
83 if (vuid > smb_conn->sessions.idtree_limit) return NULL;
85 p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
86 if (!p) return NULL;
88 /* only return an unfinished session */
89 sess = talloc_get_type(p, struct smbsrv_session);
90 if (sess && !sess->session_info) {
91 return sess;
93 return NULL;
97 * the session will be marked as valid for usage
98 * by attaching a auth_session_info to the session.
100 * session_info will be talloc_stealed
102 NTSTATUS smbsrv_session_sesssetup_finished(struct smbsrv_session *sess,
103 struct auth_session_info *session_info)
105 /* this check is to catch programmer errors */
106 if (!session_info) {
107 talloc_free(sess);
108 return NT_STATUS_ACCESS_DENIED;
111 /* mark the session as successful authenticated */
112 sess->session_info = talloc_steal(sess, session_info);
114 /* now fill in some statistics */
115 sess->statistics.auth_time = timeval_current();
117 return NT_STATUS_OK;
120 /****************************************************************************
121 destroy a session structure
122 ****************************************************************************/
123 static int smbsrv_session_destructor(struct smbsrv_session *sess)
125 struct smbsrv_connection *smb_conn = sess->smb_conn;
127 idr_remove(smb_conn->sessions.idtree_vuid, sess->vuid);
128 DLIST_REMOVE(smb_conn->sessions.list, sess);
129 return 0;
133 * allocate a new session structure with a VUID.
134 * gensec_ctx is optional, but talloc_steal'ed when present
136 struct smbsrv_session *smbsrv_session_new(struct smbsrv_connection *smb_conn,
137 TALLOC_CTX *mem_ctx,
138 struct gensec_security *gensec_ctx)
140 struct smbsrv_session *sess = NULL;
141 int i;
143 /* Ensure no vuid gets registered in share level security. */
144 if (smb_conn->config.security == SEC_SHARE) return NULL;
146 sess = talloc_zero(mem_ctx, struct smbsrv_session);
147 if (!sess) return NULL;
148 sess->smb_conn = smb_conn;
150 i = idr_get_new_random(smb_conn->sessions.idtree_vuid, sess, smb_conn->sessions.idtree_limit);
151 if (i == -1) {
152 DEBUG(1,("ERROR! Out of connection structures\n"));
153 talloc_free(sess);
154 return NULL;
156 sess->vuid = i;
158 /* use this to keep tabs on all our info from the authentication */
159 sess->gensec_ctx = talloc_steal(sess, gensec_ctx);
161 DLIST_ADD(smb_conn->sessions.list, sess);
162 talloc_set_destructor(sess, smbsrv_session_destructor);
164 /* now fill in some statistics */
165 sess->statistics.connect_time = timeval_current();
167 return sess;