summaryrefslogtreecommitdiff
path: root/source4/ntvfs/ntvfs_util.c
blob: de19a9c3525f68fc6b11a8211a7351e67e420fcf (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* 
   Unix SMB/CIFS implementation.
   NTVFS utility code
   Copyright (C) Stefan Metzmacher 2004

   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.
*/
/*
  this implements common utility functions that many NTVFS backends may wish to use
*/

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


_PUBLIC_ struct ntvfs_request *ntvfs_request_create(struct ntvfs_context *ctx, TALLOC_CTX *mem_ctx,
						    struct auth_session_info *session_info,
						    uint16_t smbpid, uint16_t smbmid,
						    struct timeval request_time,
						    void *private_data,
						    void (*send_fn)(struct ntvfs_request *),
						    uint32_t state)
{
	struct ntvfs_request *req;
	struct ntvfs_async_state *async;

	req = talloc(mem_ctx, struct ntvfs_request);
	if (!req) return NULL;
	req->ctx			= ctx;
	req->async_states		= NULL;
	req->session_info		= session_info;
	req->smbpid			= smbpid;
	req->smbmid			= smbmid;
	req->statistics.request_time	= request_time;

	async = talloc(req, struct ntvfs_async_state);
	if (!async) goto failed;

	async->state		= state;
	async->private_data	= private_data;
	async->send_fn		= send_fn;
	async->status		= NT_STATUS_INTERNAL_ERROR;
	async->ntvfs		= NULL;

	DLIST_ADD(req->async_states, async);

	return req;

failed:
	return NULL;
}

_PUBLIC_ NTSTATUS ntvfs_async_state_push(struct ntvfs_module_context *ntvfs,
					 struct ntvfs_request *req,
					 void *private_data,
					 void (*send_fn)(struct ntvfs_request *))
{
	struct ntvfs_async_state *async;

	async = talloc(req, struct ntvfs_async_state);
	NT_STATUS_HAVE_NO_MEMORY(async);

	async->state		= req->async_states->state;
	async->private_data	= private_data;
	async->send_fn		= send_fn;
	async->status		= NT_STATUS_INTERNAL_ERROR;

	async->ntvfs		= ntvfs;

	DLIST_ADD(req->async_states, async);

	return NT_STATUS_OK;
}

_PUBLIC_ void ntvfs_async_state_pop(struct ntvfs_request *req)
{
	struct ntvfs_async_state *async;

	async = req->async_states;

	DLIST_REMOVE(req->async_states, async);

	req->async_states->state	= async->state;
	req->async_states->status	= async->status;

	talloc_free(async);
}

_PUBLIC_ NTSTATUS ntvfs_handle_new(struct ntvfs_module_context *ntvfs,
				   struct ntvfs_request *req,
				   struct ntvfs_handle **h)
{
	return ntvfs->ctx->handles.create_new(ntvfs->ctx->handles.private_data, req, h);
}

_PUBLIC_ NTSTATUS ntvfs_handle_set_backend_data(struct ntvfs_handle *h,
						struct ntvfs_module_context *ntvfs,
						TALLOC_CTX *private_data)
{
	struct ntvfs_handle_data *d;
	BOOL first_time = h->backend_data?False:True;

	for (d=h->backend_data; d; d = d->next) {
		if (d->owner != ntvfs) continue;
		d->private_data = talloc_steal(d, private_data);
		return NT_STATUS_OK;
	}

	d = talloc(h, struct ntvfs_handle_data);
	NT_STATUS_HAVE_NO_MEMORY(d);
	d->owner = ntvfs;
	d->private_data = talloc_steal(d, private_data);

	DLIST_ADD(h->backend_data, d);

	if (first_time) {
		NTSTATUS status;
		status = h->ctx->handles.make_valid(h->ctx->handles.private_data, h);
		NT_STATUS_NOT_OK_RETURN(status);
	}

	return NT_STATUS_OK;
}

_PUBLIC_ void *ntvfs_handle_get_backend_data(struct ntvfs_handle *h,
					     struct ntvfs_module_context *ntvfs)
{
	struct ntvfs_handle_data *d;

	for (d=h->backend_data; d; d = d->next) {
		if (d->owner != ntvfs) continue;
		return d->private_data;
	}

	return NULL;
}

_PUBLIC_ void ntvfs_handle_remove_backend_data(struct ntvfs_handle *h,
					       struct ntvfs_module_context *ntvfs)
{
	struct ntvfs_handle_data *d,*n;

	for (d=h->backend_data; d; d = n) {
		n = d->next;
		if (d->owner != ntvfs) continue;
		DLIST_REMOVE(h->backend_data, d);
		talloc_free(d);
		d = NULL;
	}

	if (h->backend_data) return;

	/* if there's no backend_data anymore, destroy the handle */
	h->ctx->handles.destroy(h->ctx->handles.private_data, h);
}

_PUBLIC_ struct ntvfs_handle *ntvfs_handle_search_by_wire_key(struct ntvfs_module_context *ntvfs,
							      struct ntvfs_request *req,
							      const DATA_BLOB *key)
{
	return ntvfs->ctx->handles.search_by_wire_key(ntvfs->ctx->handles.private_data, req, key);
}

_PUBLIC_ DATA_BLOB ntvfs_handle_get_wire_key(struct ntvfs_handle *h, TALLOC_CTX *mem_ctx)
{
	return h->ctx->handles.get_wire_key(h->ctx->handles.private_data, h, mem_ctx);
}

_PUBLIC_ NTSTATUS ntvfs_set_handle_callbacks(struct ntvfs_context *ntvfs,
					     NTSTATUS (*create_new)(void *private_data, struct ntvfs_request *req, struct ntvfs_handle **h),
					     NTSTATUS (*make_valid)(void *private_data, struct ntvfs_handle *h),
					     void (*destroy)(void *private_data, struct ntvfs_handle *h),
					     struct ntvfs_handle *(*search_by_wire_key)(void *private_data, struct ntvfs_request *req, const DATA_BLOB *key),
					     DATA_BLOB (*get_wire_key)(void *private_data, struct ntvfs_handle *handle, TALLOC_CTX *mem_ctx),
					     void *private_data)
{
	ntvfs->handles.create_new		= create_new;
	ntvfs->handles.make_valid		= make_valid;
	ntvfs->handles.destroy			= destroy;
	ntvfs->handles.search_by_wire_key	= search_by_wire_key;
	ntvfs->handles.get_wire_key		= get_wire_key;
	ntvfs->handles.private_data		= private_data;
	return NT_STATUS_OK;
}