summaryrefslogtreecommitdiff
path: root/source4/smb_server/handle.c
blob: c2fea9e91c4f66e690b6f09197e09463ed53189a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 
   Unix SMB/CIFS implementation.
   Manage smbsrv_handle structures
   Copyright (C) Stefan Metzmacher 2006
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "dlinklist.h"
#include "smb_server/smb_server.h"
#include "ntvfs/ntvfs.h"


/****************************************************************************
init the handle structures
****************************************************************************/
NTSTATUS smbsrv_init_handles(struct smbsrv_tcon *tcon, uint64_t limit)
{
	/* 
	 * the idr_* functions take 'int' as limit,
	 * and only work with a max limit 0x00FFFFFF
	 */
	limit &= 0x00FFFFFF;

	tcon->handles.idtree_hid	= idr_init(tcon);
	NT_STATUS_HAVE_NO_MEMORY(tcon->handles.idtree_hid);
	tcon->handles.idtree_limit	= limit;
	tcon->handles.list		= NULL;

	return NT_STATUS_OK;
}

/****************************************************************************
find a handle given a handle id
****************************************************************************/
static struct smbsrv_handle *smbsrv_handle_find(struct smbsrv_handles_context *handles_ctx,
						uint64_t hid, struct timeval request_time)
{
	void *p;
	struct smbsrv_handle *handle;

	if (hid == 0) return NULL;

	if (hid > handles_ctx->idtree_limit) return NULL;

	p = idr_find(handles_ctx->idtree_hid, hid);
	if (!p) return NULL;

	handle = talloc_get_type(p, struct smbsrv_handle);
	if (!handle) return NULL;

	/* only give it away when the ntvfs subsystem has made the handle valid */
	if (!handle->ntvfs) return NULL;

	handle->statistics.last_use_time = request_time;

	return handle;
}

struct smbsrv_handle *smbsrv_smb_handle_find(struct smbsrv_tcon *smb_tcon,
					     uint16_t fnum, struct timeval request_time)
{
	return smbsrv_handle_find(&smb_tcon->handles, fnum, request_time);
}

/*
  destroy a connection structure
*/
static int smbsrv_handle_destructor(void *ptr)
{
	struct smbsrv_handle *handle = talloc_get_type(ptr, struct smbsrv_handle);
	struct smbsrv_handles_context *handles_ctx;

	handles_ctx = &handle->tcon->handles;

	idr_remove(handles_ctx->idtree_hid, handle->hid);
	DLIST_REMOVE(handles_ctx->list, handle);
	DLIST_REMOVE(handle->session->handles, &handle->session_item);

	/* tell the ntvfs backend that we are disconnecting */
	if (handle->ntvfs) {
		talloc_free(handle->ntvfs);
		handle->ntvfs = NULL;
	}

	return 0;
}

/*
  find first available handle slot
*/
struct smbsrv_handle *smbsrv_handle_new(struct smbsrv_request *req)
{
	struct smbsrv_handles_context *handles_ctx = &req->tcon->handles;
	struct smbsrv_handle *handle;
	int i;

	handle = talloc_zero(req, struct smbsrv_handle);
	if (!handle) return NULL;
	handle->tcon	= req->tcon;
	handle->session	= req->session;
	
	i = idr_get_new_above(handles_ctx->idtree_hid, handle, 1, handles_ctx->idtree_limit);
	if (i == -1) {
		DEBUG(1,("ERROR! Out of handle structures\n"));
		goto failed;
	}
	handle->hid = i;
	handle->session_item.handle = handle;

	DLIST_ADD(handles_ctx->list, handle);
	DLIST_ADD(handle->session->handles, &handle->session_item);
	talloc_set_destructor(handle, smbsrv_handle_destructor);

	/* now fill in some statistics */
	handle->statistics.open_time		= req->request_time;
	handle->statistics.last_use_time	= req->request_time;

	return handle;

failed:
	talloc_free(handle);
	return NULL;
}