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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "smb_server/smb_server.h"
25 #include "lib/util/dlinklist.h"
29 * init the sessions structures
31 NTSTATUS
smbsrv_init_sessions(struct smbsrv_connection
*smb_conn
, uint64_t limit
)
34 * the idr_* functions take 'int' as limit,
35 * and only work with a max limit 0x00FFFFFF
39 smb_conn
->sessions
.idtree_vuid
= idr_init(smb_conn
);
40 NT_STATUS_HAVE_NO_MEMORY(smb_conn
->sessions
.idtree_vuid
);
41 smb_conn
->sessions
.idtree_limit
= limit
;
42 smb_conn
->sessions
.list
= NULL
;
48 * Find the session structure assoicated with a VUID
49 * (not one from an in-progress session setup)
51 struct smbsrv_session
*smbsrv_session_find(struct smbsrv_connection
*smb_conn
,
52 uint64_t vuid
, struct timeval request_time
)
55 struct smbsrv_session
*sess
;
57 if (vuid
== 0) return NULL
;
59 if (vuid
> smb_conn
->sessions
.idtree_limit
) return NULL
;
61 p
= idr_find(smb_conn
->sessions
.idtree_vuid
, vuid
);
64 /* only return a finished session */
65 sess
= talloc_get_type(p
, struct smbsrv_session
);
66 if (sess
&& sess
->session_info
) {
67 sess
->statistics
.last_request_time
= request_time
;
75 * Find the session structure assoicated with a VUID
76 * (assoicated with an in-progress session setup)
78 struct smbsrv_session
*smbsrv_session_find_sesssetup(struct smbsrv_connection
*smb_conn
, uint64_t vuid
)
81 struct smbsrv_session
*sess
;
83 if (vuid
== 0) return NULL
;
85 if (vuid
> smb_conn
->sessions
.idtree_limit
) return NULL
;
87 p
= idr_find(smb_conn
->sessions
.idtree_vuid
, vuid
);
90 /* only return an unfinished session */
91 sess
= talloc_get_type(p
, struct smbsrv_session
);
92 if (sess
&& !sess
->session_info
) {
99 * the session will be marked as valid for usage
100 * by attaching a auth_session_info to the session.
102 * session_info will be talloc_stealed
104 NTSTATUS
smbsrv_session_sesssetup_finished(struct smbsrv_session
*sess
,
105 struct auth_session_info
*session_info
)
107 /* this check is to catch programmer errors */
110 return NT_STATUS_ACCESS_DENIED
;
113 /* mark the session as successful authenticated */
114 sess
->session_info
= talloc_steal(sess
, session_info
);
116 /* now fill in some statistics */
117 sess
->statistics
.auth_time
= timeval_current();
122 /****************************************************************************
123 destroy a session structure
124 ****************************************************************************/
125 static int smbsrv_session_destructor(struct smbsrv_session
*sess
)
127 struct smbsrv_connection
*smb_conn
= sess
->smb_conn
;
129 idr_remove(smb_conn
->sessions
.idtree_vuid
, sess
->vuid
);
130 DLIST_REMOVE(smb_conn
->sessions
.list
, sess
);
135 * allocate a new session structure with a VUID.
136 * gensec_ctx is optional, but talloc_steal'ed when present
138 struct smbsrv_session
*smbsrv_session_new(struct smbsrv_connection
*smb_conn
,
139 struct gensec_security
*gensec_ctx
)
141 struct smbsrv_session
*sess
= NULL
;
144 /* Ensure no vuid gets registered in share level security. */
145 if (smb_conn
->config
.security
== SEC_SHARE
) return NULL
;
147 sess
= talloc_zero(smb_conn
, struct smbsrv_session
);
148 if (!sess
) return NULL
;
149 sess
->smb_conn
= smb_conn
;
151 i
= idr_get_new_random(smb_conn
->sessions
.idtree_vuid
, sess
, smb_conn
->sessions
.idtree_limit
);
153 DEBUG(1,("ERROR! Out of connection structures\n"));
159 /* use this to keep tabs on all our info from the authentication */
160 sess
->gensec_ctx
= talloc_steal(sess
, gensec_ctx
);
162 DLIST_ADD(smb_conn
->sessions
.list
, sess
);
163 talloc_set_destructor(sess
, smbsrv_session_destructor
);
165 /* now fill in some statistics */
166 sess
->statistics
.connect_time
= timeval_current();