summaryrefslogtreecommitdiff
path: root/source4/ntvfs/cifs/vfs_cifs.c
blob: 3903203505a3f46ce3d6aaefe45d222365dac037 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/* 
   Unix SMB/CIFS implementation.

   CIFS-on-CIFS NTVFS filesystem backend

   Copyright (C) Andrew Tridgell 2003
   Copyright (C) James J Myers 2003 <myersjj@samba.org>

   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 a CIFS->CIFS NTVFS filesystem backend. 
  
*/

#include "includes.h"

/* this is stored in ntvfs_private */
struct cvfs_private {
	struct cli_tree *tree;
	struct cli_transport *transport;
	struct tcon_context *conn;
	const char *map_calls;
};


/* a structure used to pass information to an async handler */
struct async_info {
	struct request_context *req;
	void *parms;
};

/*
  an idle function to cope with messages from the smbd client while 
  waiting for a reply from the server
  this function won't be needed once all of the cifs backend
  and the core of smbd is converted to use async calls
*/
static void idle_func(struct cli_transport *transport, void *p_private)
{
	struct cvfs_private *private = p_private;
	if (socket_pending(private->conn->smb->socket.fd)) {
		smbd_process_async(private->conn->smb);
	}
}

/*
  a handler for oplock break events from the server - these need to be passed
  along to the client
 */
static BOOL oplock_handler(struct cli_transport *transport, uint16 tid, uint16 fnum, uint8 level, void *p_private)
{
	struct cvfs_private *private = p_private;
	
	DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
	return req_send_oplock_break(private->conn, fnum, level);
}

/*
  a handler for read events on a connection to a backend server
*/
static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16 flags)
{
	struct tcon_context *conn = fde->private;
	struct cvfs_private *private = conn->ntvfs_private;

	DEBUG(5,("cifs_socket_handler event on fd %d\n", fde->fd));

	if (!cli_request_receive_next(private->transport)) {
		/* the connection to our server is dead */
		close_cnum(conn);
	}
}

/*
  connect to a share - used when a tree_connect operation comes in.
*/
static NTSTATUS cvfs_connect(struct request_context *req, const char *sharename)
{
	struct tcon_context *conn = req->conn;
	NTSTATUS status;
	struct cvfs_private *private;
	char *map_calls;
	struct fd_event fde;
	const char *host, *user, *pass, *domain, *remote_share;

	/* Here we need to determine which server to connect to.
	 * For now we use parametric options, type cifs.
	 * Later we will use security=server and auth_server.c.
	 */
	host = lp_parm_string(req->conn->service, "cifs", "server");
	user = lp_parm_string(req->conn->service, "cifs", "user");
	pass = lp_parm_string(req->conn->service, "cifs", "password");
	domain = lp_parm_string(req->conn->service, "cifs", "domain");
	remote_share = lp_parm_string(req->conn->service, "cifs", "share");
	if (!remote_share) {
		remote_share = sharename;
	}

	if (!host || !user || !pass || !domain) {
		DEBUG(1,("CIFS backend: You must supply server, user, password and domain\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}
	
	private = talloc(req->conn->mem_ctx, sizeof(struct cvfs_private));
	if (!private) {
		return NT_STATUS_NO_MEMORY;
	}
	ZERO_STRUCTP(private);

	req->conn->ntvfs_private = (void *)private;

	status = cli_tree_full_connection(&private->tree, 
					  "vfs_cifs",
					  host,
					  0,
					  remote_share, "?????",
					  user, domain,
					  pass);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	private->transport = private->tree->session->transport;
	private->tree->session->pid = SVAL(req->in.hdr, HDR_PID);
	private->conn = req->conn;

	conn->fs_type = talloc_strdup(conn->mem_ctx, "NTFS");
	conn->dev_type = talloc_strdup(conn->mem_ctx, "A:");
	
	map_calls = lp_parm_string(req->conn->service, "cifs", "map calls");
	if (map_calls) {
		private->map_calls = talloc_strdup(conn->mem_ctx, map_calls);
	}

	/* if we are mapping trans2, then we need to not give a trans2
	   pointer in the operations structure */
	if (private->map_calls && in_list("trans2", private->map_calls, True)) {
		conn->ntvfs_ops->trans2 = NULL;
	}	  

	/* we need to tell the event loop that we wish to receive read events
	   on our SMB connection to the server */
	fde.fd = private->transport->socket->fd;
	fde.flags = EVENT_FD_READ;
	fde.private = req->conn;
	fde.handler = cifs_socket_handler;

	event_add_fd(conn->smb->events, &fde);

	/* we need to receive oplock break requests from the server */
	cli_oplock_handler(private->transport, oplock_handler, private);
	cli_transport_idle_handler(private->transport, idle_func, 100, private);

	return NT_STATUS_OK;
}

/*
  disconnect from a share
*/
static NTSTATUS cvfs_disconnect(struct tcon_context *conn)
{
	struct cvfs_private *private = conn->ntvfs_private;

	event_remove_fd_all(conn->smb->events, private->transport->socket->fd);
	smb_tree_disconnect(private->tree);
	cli_tree_close(private->tree);

	return NT_STATUS_OK;
}

/*
  a handler for simple async replies
  this handler can only be used for functions that don't return any
  parameters (those that just return a status code)
 */
static void async_simple(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = cli_request_simple_recv(c_req);
	req->async.send_fn(req);
}


/* save some typing for the simple functions */
#define ASYNC_RECV_TAIL(io, async_fn) do { \
	if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
	{ \
		struct async_info *async = c_req->async.private; \
		async = talloc(req->mem_ctx, sizeof(*async)); \
		if (!async) return NT_STATUS_NO_MEMORY; \
		async->parms = io; \
		async->req = req; \
		c_req->async.private = async; \
	} \
	c_req->async.fn = async_fn; \
	req->control_flags |= REQ_CONTROL_ASYNC; \
	return NT_STATUS_OK; \
} while (0)

#define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)

/*
  delete a file - the dirtype specifies the file types to include in the search. 
  The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
*/
static NTSTATUS cvfs_unlink(struct request_context *req, struct smb_unlink *unl)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	/* see if the front end will allow us to perform this
	   function asynchronously.  */
	if (!req->async.send_fn) {
		return smb_raw_unlink(private->tree, unl);
	}

	c_req = smb_raw_unlink_send(private->tree, unl);

	SIMPLE_ASYNC_TAIL;
}

/*
  a handler for async ioctl replies
 */
static void async_ioctl(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_ioctl_recv(c_req, req->mem_ctx, async->parms);
	req->async.send_fn(req);
}

/*
  ioctl interface
*/
static NTSTATUS cvfs_ioctl(struct request_context *req, struct smb_ioctl *io)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	/* see if the front end will allow us to perform this
	   function asynchronously.  */
	if (!req->async.send_fn) {
		return smb_raw_ioctl(private->tree, req->mem_ctx, io);
	}

	c_req = smb_raw_ioctl_send(private->tree, io);

	ASYNC_RECV_TAIL(io, async_ioctl);
}

/*
  check if a directory exists
*/
static NTSTATUS cvfs_chkpath(struct request_context *req, struct smb_chkpath *cp)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_chkpath(private->tree, cp);
	}

	c_req = smb_raw_chkpath_send(private->tree, cp);

	SIMPLE_ASYNC_TAIL;
}

/*
  a handler for async qpathinfo replies
 */
static void async_qpathinfo(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_pathinfo_recv(c_req, req->mem_ctx, async->parms);
	req->async.send_fn(req);
}

/*
  return info on a pathname
*/
static NTSTATUS cvfs_qpathinfo(struct request_context *req, union smb_fileinfo *info)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_pathinfo(private->tree, req->mem_ctx, info);
	}

	c_req = smb_raw_pathinfo_send(private->tree, info);

	ASYNC_RECV_TAIL(info, async_qpathinfo);
}

/*
  a handler for async qfileinfo replies
 */
static void async_qfileinfo(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_fileinfo_recv(c_req, req->mem_ctx, async->parms);
	req->async.send_fn(req);
}

/*
  query info on a open file
*/
static NTSTATUS cvfs_qfileinfo(struct request_context *req, union smb_fileinfo *info)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_fileinfo(private->tree, req->mem_ctx, info);
	}

	c_req = smb_raw_fileinfo_send(private->tree, info);

	ASYNC_RECV_TAIL(info, async_qfileinfo);
}


/*
  set info on a pathname
*/
static NTSTATUS cvfs_setpathinfo(struct request_context *req, union smb_setfileinfo *st)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_setpathinfo(private->tree, st);
	}

	c_req = smb_raw_setpathinfo_send(private->tree, st);

	SIMPLE_ASYNC_TAIL;
}


/*
  a handler for async open replies
 */
static void async_open(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_open_recv(c_req, req->mem_ctx, async->parms);
	req->async.send_fn(req);
}

/*
  open a file
*/
static NTSTATUS cvfs_open(struct request_context *req, union smb_open *io)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (private->map_calls && in_list("open", private->map_calls, True) &&
	    io->generic.level != RAW_OPEN_GENERIC) {
		return ntvfs_map_open(req, io);
	}

	if (!req->async.send_fn) {
		return smb_raw_open(private->tree, req->mem_ctx, io);
	}

	c_req = smb_raw_open_send(private->tree, io);

	ASYNC_RECV_TAIL(io, async_open);
}

/*
  create a directory
*/
static NTSTATUS cvfs_mkdir(struct request_context *req, union smb_mkdir *md)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_mkdir(private->tree, md);
	}

	c_req = smb_raw_mkdir_send(private->tree, md);

	SIMPLE_ASYNC_TAIL;
}

/*
  remove a directory
*/
static NTSTATUS cvfs_rmdir(struct request_context *req, struct smb_rmdir *rd)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_rmdir(private->tree, rd);
	}
	c_req = smb_raw_rmdir_send(private->tree, rd);

	SIMPLE_ASYNC_TAIL;
}

/*
  rename a set of files
*/
static NTSTATUS cvfs_rename(struct request_context *req, union smb_rename *ren)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_rename(private->tree, ren);
	}

	c_req = smb_raw_rename_send(private->tree, ren);

	SIMPLE_ASYNC_TAIL;
}

/*
  copy a set of files
*/
static NTSTATUS cvfs_copy(struct request_context *req, struct smb_copy *cp)
{
	return NT_STATUS_NOT_SUPPORTED;
}

/*
  a handler for async read replies
 */
static void async_read(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_read_recv(c_req, async->parms);
	req->async.send_fn(req);
}

/*
  read from a file
*/
static NTSTATUS cvfs_read(struct request_context *req, union smb_read *rd)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_read(private->tree, rd);
	}

	c_req = smb_raw_read_send(private->tree, rd);

	ASYNC_RECV_TAIL(rd, async_read);
}

/*
  a handler for async write replies
 */
static void async_write(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_write_recv(c_req, async->parms);
	req->async.send_fn(req);
}

/*
  write to a file
*/
static NTSTATUS cvfs_write(struct request_context *req, union smb_write *wr)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_write(private->tree, wr);
	}

	c_req = smb_raw_write_send(private->tree, wr);

	ASYNC_RECV_TAIL(wr, async_write);
}

/*
  seek in a file
*/
static NTSTATUS cvfs_seek(struct request_context *req, struct smb_seek *io)
{
	return NT_STATUS_NOT_SUPPORTED;
}

/*
  flush a file
*/
static NTSTATUS cvfs_flush(struct request_context *req, struct smb_flush *io)
{
	return NT_STATUS_OK;
}

/*
  close a file
*/
static NTSTATUS cvfs_close(struct request_context *req, union smb_close *io)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_close(private->tree, io);
	}

	c_req = smb_raw_close_send(private->tree, io);

	SIMPLE_ASYNC_TAIL;
}

/*
  exit - closing files?
*/
static NTSTATUS cvfs_exit(struct request_context *req)
{
	return NT_STATUS_NOT_SUPPORTED;
}

/*
  lock a byte range
*/
static NTSTATUS cvfs_lock(struct request_context *req, union smb_lock *lck)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_lock(private->tree, lck);
	}

	c_req = smb_raw_lock_send(private->tree, lck);
	SIMPLE_ASYNC_TAIL;
}

/*
  set info on a open file
*/
static NTSTATUS cvfs_setfileinfo(struct request_context *req, 
				 union smb_setfileinfo *info)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_setfileinfo(private->tree, info);
	}
	c_req = smb_raw_setfileinfo_send(private->tree, info);

	SIMPLE_ASYNC_TAIL;
}


/*
  a handler for async fsinfo replies
 */
static void async_fsinfo(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_fsinfo_recv(c_req, req->mem_ctx, async->parms);
	req->async.send_fn(req);
}

/*
  return filesystem space info
*/
static NTSTATUS cvfs_fsinfo(struct request_context *req, union smb_fsinfo *fs)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_fsinfo(private->tree, req->mem_ctx, fs);
	}

	c_req = smb_raw_fsinfo_send(private->tree, req->mem_ctx, fs);

	ASYNC_RECV_TAIL(fs, async_fsinfo);
}

/*
  return print queue info
*/
static NTSTATUS cvfs_lpq(struct request_context *req, union smb_lpq *lpq)
{
	return NT_STATUS_NOT_SUPPORTED;
}

/* 
   list files in a directory matching a wildcard pattern
*/
static NTSTATUS cvfs_search_first(struct request_context *req, union smb_search_first *io, 
				  void *search_private, 
				  BOOL (*callback)(void *, union smb_search_data *))
{
	struct cvfs_private *private = req->conn->ntvfs_private;

	return smb_raw_search_first(private->tree, req->mem_ctx, io, search_private, callback);
}

/* continue a search */
static NTSTATUS cvfs_search_next(struct request_context *req, union smb_search_next *io, 
				 void *search_private, 
				 BOOL (*callback)(void *, union smb_search_data *))
{
	struct cvfs_private *private = req->conn->ntvfs_private;

	return smb_raw_search_next(private->tree, req->mem_ctx, io, search_private, callback);
}

/* close a search */
static NTSTATUS cvfs_search_close(struct request_context *req, union smb_search_close *io)
{
	struct cvfs_private *private = req->conn->ntvfs_private;

	return smb_raw_search_close(private->tree, io);
}

/*
  a handler for async trans2 replies
 */
static void async_trans2(struct cli_request *c_req)
{
	struct async_info *async = c_req->async.private;
	struct request_context *req = async->req;
	req->async.status = smb_raw_trans2_recv(c_req, req->mem_ctx, async->parms);
	req->async.send_fn(req);
}

/* raw trans2 */
static NTSTATUS cvfs_trans2(struct request_context *req, struct smb_trans2 *trans2)
{
	struct cvfs_private *private = req->conn->ntvfs_private;
	struct cli_request *c_req;

	if (!req->async.send_fn) {
		return smb_raw_trans2(private->tree, req->mem_ctx, trans2);
	}

	c_req = smb_raw_trans2_send(private->tree, trans2);

	ASYNC_RECV_TAIL(trans2, async_trans2);
}

/*
  initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
 */
NTSTATUS ntvfs_cifs_init(void)
{
	NTSTATUS ret;
	struct ntvfs_ops ops;

	ZERO_STRUCT(ops);

	ops.name = "cifs";
	ops.type = NTVFS_DISK;
	
	/* fill in all the operations */
	ops.connect = cvfs_connect;
	ops.disconnect = cvfs_disconnect;
	ops.unlink = cvfs_unlink;
	ops.chkpath = cvfs_chkpath;
	ops.qpathinfo = cvfs_qpathinfo;
	ops.setpathinfo = cvfs_setpathinfo;
	ops.open = cvfs_open;
	ops.mkdir = cvfs_mkdir;
	ops.rmdir = cvfs_rmdir;
	ops.rename = cvfs_rename;
	ops.copy = cvfs_copy;
	ops.ioctl = cvfs_ioctl;
	ops.read = cvfs_read;
	ops.write = cvfs_write;
	ops.seek = cvfs_seek;
	ops.flush = cvfs_flush;	
	ops.close = cvfs_close;
	ops.exit = cvfs_exit;
	ops.lock = cvfs_lock;
	ops.setfileinfo = cvfs_setfileinfo;
	ops.qfileinfo = cvfs_qfileinfo;
	ops.fsinfo = cvfs_fsinfo;
	ops.lpq = cvfs_lpq;
	ops.search_first = cvfs_search_first;
	ops.search_next = cvfs_search_next;
	ops.search_close = cvfs_search_close;

	/* only define this one for trans2 testing */
	ops.trans2 = cvfs_trans2;

	/* register ourselves with the NTVFS subsystem. We register under the name 'cifs'. */

	ret = register_backend("ntvfs", &ops);

	if (!NT_STATUS_IS_OK(ret)) {
		DEBUG(0,("Failed to register CIFS backend!\n"));
	}
	
	return ret;
}