LCOV - code coverage report
Current view: top level - source4/smb_server - session.c (source / functions) Hit Total Coverage
Test: coverage report for master 469b22b8 Lines: 48 58 82.8 %
Date: 2024-06-10 12:05:21 Functions: 6 6 100.0 %

          Line data    Source code
       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/idtree_random.h"
      25             : 
      26             : /*
      27             :  * init the sessions structures
      28             :  */
      29        3648 : NTSTATUS smbsrv_init_sessions(struct smbsrv_connection *smb_conn, uint64_t limit)
      30             : {
      31             :         /* 
      32             :          * the idr_* functions take 'int' as limit,
      33             :          * and only work with a max limit 0x00FFFFFF
      34             :          */
      35        3648 :         limit &= 0x00FFFFFF;
      36             : 
      37        3648 :         smb_conn->sessions.idtree_vuid       = idr_init(smb_conn);
      38        3648 :         NT_STATUS_HAVE_NO_MEMORY(smb_conn->sessions.idtree_vuid);
      39        3648 :         smb_conn->sessions.idtree_limit      = limit;
      40        3648 :         smb_conn->sessions.list              = NULL;
      41             : 
      42        3648 :         return NT_STATUS_OK;
      43             : }
      44             : 
      45             : /*
      46             :  * Find the session structure associated with a VUID
      47             :  * (not one from an in-progress session setup)
      48             :  */
      49      833554 : struct smbsrv_session *smbsrv_session_find(struct smbsrv_connection *smb_conn,
      50             :                                            uint64_t vuid, struct timeval request_time)
      51             : {
      52           0 :         void *p;
      53           0 :         struct smbsrv_session *sess;
      54             : 
      55      833554 :         if (vuid == 0) return NULL;
      56             : 
      57      828542 :         if (vuid > smb_conn->sessions.idtree_limit) return NULL;
      58             : 
      59      828542 :         p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
      60      828542 :         if (!p) return NULL;
      61             : 
      62             :         /* only return a finished session */
      63      828518 :         sess = talloc_get_type(p, struct smbsrv_session);
      64      828518 :         if (sess && sess->session_info) {
      65      828256 :                 sess->statistics.last_request_time = request_time;
      66      828256 :                 return sess;
      67             :         }
      68             : 
      69         262 :         return NULL;
      70             : }
      71             : 
      72             : /*
      73             :  * Find the session structure associated with a VUID
      74             :  * (associated with an in-progress session setup)
      75             :  */
      76         265 : struct smbsrv_session *smbsrv_session_find_sesssetup(struct smbsrv_connection *smb_conn, uint64_t vuid)
      77             : {
      78           0 :         void *p;
      79           0 :         struct smbsrv_session *sess;
      80             : 
      81         265 :         if (vuid == 0) return NULL;
      82             : 
      83         265 :         if (vuid > smb_conn->sessions.idtree_limit) return NULL;
      84             : 
      85         265 :         p = idr_find(smb_conn->sessions.idtree_vuid, vuid);
      86         265 :         if (!p) return NULL;
      87             : 
      88         264 :         sess = talloc_get_type_abort(p, struct smbsrv_session);
      89             : 
      90         264 :         return sess;
      91             : }
      92             : 
      93             : /*
      94             :  * the session will be marked as valid for usage
      95             :  * by attaching a auth_session_info to the session.
      96             :  *
      97             :  * session_info will be talloc_stealed
      98             :  */
      99        2501 : NTSTATUS smbsrv_session_sesssetup_finished(struct smbsrv_session *sess,
     100             :                                            struct auth_session_info *session_info)
     101             : {
     102             :         /* this check is to catch programmer errors */
     103        2501 :         if (!session_info) {
     104           0 :                 talloc_free(sess);
     105           0 :                 return NT_STATUS_ACCESS_DENIED;
     106             :         }
     107             : 
     108             :         /* mark the session as successful authenticated */
     109        2501 :         sess->session_info = talloc_steal(sess, session_info);
     110             : 
     111             :         /* now fill in some statistics */
     112        2501 :         sess->statistics.auth_time = timeval_current();
     113             : 
     114        2501 :         return NT_STATUS_OK;
     115             : }
     116             : 
     117             : /****************************************************************************
     118             : destroy a session structure
     119             : ****************************************************************************/
     120        2513 : static int smbsrv_session_destructor(struct smbsrv_session *sess)
     121             : {
     122        2513 :         struct smbsrv_connection *smb_conn = sess->smb_conn;
     123             : 
     124        2513 :         idr_remove(smb_conn->sessions.idtree_vuid, sess->vuid);
     125        2513 :         DLIST_REMOVE(smb_conn->sessions.list, sess);
     126        2513 :         return 0;
     127             : }
     128             : 
     129             : /*
     130             :  * allocate a new session structure with a VUID.
     131             :  * gensec_ctx is optional, but talloc_steal'ed when present
     132             :  */
     133        2513 : struct smbsrv_session *smbsrv_session_new(struct smbsrv_connection *smb_conn,
     134             :                                           TALLOC_CTX *mem_ctx,
     135             :                                           struct gensec_security *gensec_ctx)
     136             : {
     137        2513 :         struct smbsrv_session *sess = NULL;
     138           0 :         int i;
     139             : 
     140        2513 :         sess = talloc_zero(mem_ctx, struct smbsrv_session);
     141        2513 :         if (!sess) return NULL;
     142        2513 :         sess->smb_conn = smb_conn;
     143             : 
     144        2513 :         i = idr_get_new_random(
     145             :                 smb_conn->sessions.idtree_vuid,
     146             :                 sess,
     147             :                 1,
     148        2513 :                 smb_conn->sessions.idtree_limit);
     149        2513 :         if (i == -1) {
     150           0 :                 DEBUG(1,("ERROR! Out of connection structures\n"));
     151           0 :                 talloc_free(sess);
     152           0 :                 return NULL;
     153             :         }
     154        2513 :         sess->vuid = i;
     155             : 
     156             :         /* use this to keep tabs on all our info from the authentication */
     157        2513 :         sess->gensec_ctx = talloc_steal(sess, gensec_ctx);
     158             : 
     159        2513 :         DLIST_ADD(smb_conn->sessions.list, sess);
     160        2513 :         talloc_set_destructor(sess, smbsrv_session_destructor);
     161             : 
     162             :         /* now fill in some statistics */
     163        2513 :         sess->statistics.connect_time = timeval_current();
     164             : 
     165        2513 :         return sess;
     166             : }

Generated by: LCOV version 1.14