root/source4/smb_server/session.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. smbsrv_init_sessions
  2. smbsrv_session_find
  3. smbsrv_session_find_sesssetup
  4. smbsrv_session_sesssetup_finished
  5. smbsrv_session_destructor
  6. smbsrv_session_new

   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
   7 
   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.
  12    
  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.
  17    
  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/>.
  20 */
  21 
  22 #include "includes.h"
  23 #include "smb_server/smb_server.h"
  24 #include "../lib/util/dlinklist.h"
  25 
  26 
  27 /*
  28  * init the sessions structures
  29  */
  30 NTSTATUS smbsrv_init_sessions(struct smbsrv_connection *smb_conn, uint64_t limit)
     /* [<][>][^][v][top][bottom][index][help] */
  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,
     /* [<][>][^][v][top][bottom][index][help] */
  51                                            uint64_t vuid, struct timeval request_time)
  52 {
  53         void *p;
  54         struct smbsrv_session *sess;
  55 
  56         if (vuid == 0) return NULL;
  57 
  58         if (vuid > smb_conn->sessions.idtree_limit) return NULL;
  59 
  60         p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
  61         if (!p) return NULL;
  62 
  63         /* only return a finished session */
  64         sess = talloc_get_type(p, struct smbsrv_session);
  65         if (sess && sess->session_info) {
  66                 sess->statistics.last_request_time = request_time;
  67                 return sess;
  68         }
  69 
  70         return NULL;
  71 }
  72 
  73 /*
  74  * Find the session structure assoicated with a VUID
  75  * (assoicated with an in-progress session setup)
  76  */
  77 struct smbsrv_session *smbsrv_session_find_sesssetup(struct smbsrv_connection *smb_conn, uint64_t vuid)
     /* [<][>][^][v][top][bottom][index][help] */
  78 {
  79         void *p;
  80         struct smbsrv_session *sess;
  81 
  82         if (vuid == 0) return NULL;
  83 
  84         if (vuid > smb_conn->sessions.idtree_limit) return NULL;
  85 
  86         p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
  87         if (!p) return NULL;
  88 
  89         /* only return an unfinished session */
  90         sess = talloc_get_type(p, struct smbsrv_session);
  91         if (sess && !sess->session_info) {
  92                 return sess;
  93         }
  94         return NULL;
  95 }
  96 
  97 /*
  98  * the session will be marked as valid for usage
  99  * by attaching a auth_session_info to the session.
 100  *
 101  * session_info will be talloc_stealed
 102  */
 103 NTSTATUS smbsrv_session_sesssetup_finished(struct smbsrv_session *sess,
     /* [<][>][^][v][top][bottom][index][help] */
 104                                            struct auth_session_info *session_info)
 105 {
 106         /* this check is to catch programmer errors */
 107         if (!session_info) {
 108                 talloc_free(sess);
 109                 return NT_STATUS_ACCESS_DENIED;
 110         }
 111 
 112         /* mark the session as successful authenticated */
 113         sess->session_info = talloc_steal(sess, session_info);
 114 
 115         /* now fill in some statistics */
 116         sess->statistics.auth_time = timeval_current();
 117 
 118         return NT_STATUS_OK;
 119 }
 120 
 121 /****************************************************************************
 122 destroy a session structure
 123 ****************************************************************************/
 124 static int smbsrv_session_destructor(struct smbsrv_session *sess)
     /* [<][>][^][v][top][bottom][index][help] */
 125 {
 126         struct smbsrv_connection *smb_conn = sess->smb_conn;
 127 
 128         idr_remove(smb_conn->sessions.idtree_vuid, sess->vuid);
 129         DLIST_REMOVE(smb_conn->sessions.list, sess);
 130         return 0;
 131 }
 132 
 133 /*
 134  * allocate a new session structure with a VUID.
 135  * gensec_ctx is optional, but talloc_steal'ed when present
 136  */
 137 struct smbsrv_session *smbsrv_session_new(struct smbsrv_connection *smb_conn,
     /* [<][>][^][v][top][bottom][index][help] */
 138                                           TALLOC_CTX *mem_ctx,
 139                                           struct gensec_security *gensec_ctx)
 140 {
 141         struct smbsrv_session *sess = NULL;
 142         int i;
 143 
 144         /* Ensure no vuid gets registered in share level security. */
 145         if (smb_conn->config.security == SEC_SHARE) return NULL;
 146 
 147         sess = talloc_zero(mem_ctx, struct smbsrv_session);
 148         if (!sess) return NULL;
 149         sess->smb_conn = smb_conn;
 150 
 151         i = idr_get_new_random(smb_conn->sessions.idtree_vuid, sess, smb_conn->sessions.idtree_limit);
 152         if (i == -1) {
 153                 DEBUG(1,("ERROR! Out of connection structures\n"));
 154                 talloc_free(sess);
 155                 return NULL;
 156         }
 157         sess->vuid = i;
 158 
 159         /* use this to keep tabs on all our info from the authentication */
 160         sess->gensec_ctx = talloc_steal(sess, gensec_ctx);
 161 
 162         DLIST_ADD(smb_conn->sessions.list, sess);
 163         talloc_set_destructor(sess, smbsrv_session_destructor);
 164 
 165         /* now fill in some statistics */
 166         sess->statistics.connect_time = timeval_current();
 167 
 168         return sess;
 169 }

/* [<][>][^][v][top][bottom][index][help] */