1 /*
2    Unix SMB/CIFS implementation.
3    change notify handling
4    Copyright (C) Andrew Tridgell 2000
5    Copyright (C) Jeremy Allison 1994-1998
6    Copyright (C) Volker Lendecke 2007
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 "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/ndr_notify.h"
26 #include "librpc/gen_ndr/ndr_file_id.h"
27 
28 struct notify_change_event {
29 	struct timespec when;
30 	uint32_t action;
31 	const char *name;
32 };
33 
34 struct notify_change_buf {
35 	/*
36 	 * Filters for reinitializing after notifyd has been restarted
37 	 */
38 	uint32_t filter;
39 	uint32_t subdir_filter;
40 
41 	/*
42 	 * If no requests are pending, changes are queued here. Simple array,
43 	 * we only append.
44 	 */
45 
46 	uint32_t max_buffer_size;
47 
48 	/*
49 	 * num_changes == -1 means that we have got a catch-all change, when
50 	 * asked we just return NT_STATUS_OK without specific changes.
51 	 */
52 	int num_changes;
53 	struct notify_change_event *changes;
54 
55 	/*
56 	 * If no changes are around requests are queued here. Using a linked
57 	 * list, because we have to append at the end and delete from the top.
58 	 */
59 	struct notify_change_request *requests;
60 };
61 
62 struct notify_change_request {
63 	struct notify_change_request *prev, *next;
64 	struct files_struct *fsp;	/* backpointer for cancel by mid */
65 	struct smb_request *req;
66 	uint32_t filter;
67 	uint32_t max_param;
68 	void (*reply_fn)(struct smb_request *req,
69 			 NTSTATUS error_code,
70 			 uint8_t *buf, size_t len);
71 	struct notify_mid_map *mid_map;
72 	void *backend_data;
73 };
74 
75 static void notify_fsp(files_struct *fsp, struct timespec when,
76 		       uint32_t action, const char *name);
77 
change_notify_fsp_has_changes(struct files_struct * fsp)78 bool change_notify_fsp_has_changes(struct files_struct *fsp)
79 {
80 	if (fsp == NULL) {
81 		return false;
82 	}
83 
84 	if (fsp->notify == NULL) {
85 		return false;
86 	}
87 
88 	if (fsp->notify->num_changes == 0) {
89 		return false;
90 	}
91 
92 	return true;
93 }
94 
95 /*
96  * For NTCancel, we need to find the notify_change_request indexed by
97  * mid. Separate list here.
98  */
99 
100 struct notify_mid_map {
101 	struct notify_mid_map *prev, *next;
102 	struct notify_change_request *req;
103 	uint64_t mid;
104 };
105 
notify_change_record_identical(struct notify_change_event * c1,struct notify_change_event * c2)106 static bool notify_change_record_identical(struct notify_change_event *c1,
107 					   struct notify_change_event *c2)
108 {
109 	/* Note this is deliberately case sensitive. */
110 	if (c1->action == c2->action &&
111 			strcmp(c1->name, c2->name) == 0) {
112 		return True;
113 	}
114 	return False;
115 }
116 
compare_notify_change_events(const void * p1,const void * p2)117 static int compare_notify_change_events(const void *p1, const void *p2)
118 {
119 	const struct notify_change_event *e1 = p1;
120 	const struct notify_change_event *e2 = p2;
121 
122 	return timespec_compare(&e1->when, &e2->when);
123 }
124 
notify_marshall_changes(int num_changes,uint32_t max_offset,struct notify_change_event * changes,DATA_BLOB * final_blob)125 static bool notify_marshall_changes(int num_changes,
126 				uint32_t max_offset,
127 				struct notify_change_event *changes,
128 				DATA_BLOB *final_blob)
129 {
130 	int i;
131 
132 	if (num_changes == -1) {
133 		return false;
134 	}
135 
136 	/*
137 	 * Sort the notifies by timestamp when the event happened to avoid
138 	 * coalescing and thus dropping events.
139 	 */
140 
141 	qsort(changes, num_changes,
142 	      sizeof(*changes), compare_notify_change_events);
143 
144 	for (i=0; i<num_changes; i++) {
145 		enum ndr_err_code ndr_err;
146 		struct notify_change_event *c;
147 		struct FILE_NOTIFY_INFORMATION m;
148 		DATA_BLOB blob;
149 		uint16_t pad = 0;
150 
151 		/* Coalesce any identical records. */
152 		while (i+1 < num_changes &&
153 			notify_change_record_identical(&changes[i],
154 						&changes[i+1])) {
155 			i++;
156 		}
157 
158 		c = &changes[i];
159 
160 		m.FileName1 = c->name;
161 		m.FileNameLength = strlen_m(c->name)*2;
162 		m.Action = c->action;
163 
164 		m._pad = data_blob_null;
165 
166 		/*
167 		 * Offset to next entry, only if there is one
168 		 */
169 
170 		if (i == (num_changes-1)) {
171 			m.NextEntryOffset = 0;
172 		} else {
173 			if ((m.FileNameLength % 4) == 2) {
174 				m._pad = data_blob_const(&pad, 2);
175 			}
176 			m.NextEntryOffset =
177 				ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
178 		}
179 
180 		ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
181 			(ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
182 		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
183 			return false;
184 		}
185 
186 		if (DEBUGLEVEL >= 10) {
187 			NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
188 		}
189 
190 		if (!data_blob_append(talloc_tos(), final_blob,
191 				      blob.data, blob.length)) {
192 			data_blob_free(&blob);
193 			return false;
194 		}
195 
196 		data_blob_free(&blob);
197 
198 		if (final_blob->length > max_offset) {
199 			/* Too much data for client. */
200 			DEBUG(10, ("Client only wanted %d bytes, trying to "
201 				   "marshall %d bytes\n", (int)max_offset,
202 				   (int)final_blob->length));
203 			return False;
204 		}
205 	}
206 
207 	return True;
208 }
209 
210 /****************************************************************************
211  Setup the common parts of the return packet and send it.
212 *****************************************************************************/
213 
change_notify_reply(struct smb_request * req,NTSTATUS error_code,uint32_t max_param,struct notify_change_buf * notify_buf,void (* reply_fn)(struct smb_request * req,NTSTATUS error_code,uint8_t * buf,size_t len))214 void change_notify_reply(struct smb_request *req,
215 			 NTSTATUS error_code,
216 			 uint32_t max_param,
217 			 struct notify_change_buf *notify_buf,
218 			 void (*reply_fn)(struct smb_request *req,
219 					  NTSTATUS error_code,
220 					  uint8_t *buf, size_t len))
221 {
222 	DATA_BLOB blob = data_blob_null;
223 
224 	if (!NT_STATUS_IS_OK(error_code)) {
225 		reply_fn(req, error_code, NULL, 0);
226 		return;
227 	}
228 
229 	if (notify_buf == NULL) {
230 		reply_fn(req, NT_STATUS_OK, NULL, 0);
231 		return;
232 	}
233 
234 	max_param = MIN(max_param, notify_buf->max_buffer_size);
235 
236 	if (!notify_marshall_changes(notify_buf->num_changes, max_param,
237 					notify_buf->changes, &blob)) {
238 		/*
239 		 * We exceed what the client is willing to accept. Send
240 		 * nothing.
241 		 */
242 		data_blob_free(&blob);
243 	}
244 
245 	reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
246 
247 	data_blob_free(&blob);
248 
249 	TALLOC_FREE(notify_buf->changes);
250 	notify_buf->num_changes = 0;
251 }
252 
253 struct notify_fsp_state {
254 	struct files_struct *notified_fsp;
255 	struct timespec when;
256 	const struct notify_event *e;
257 };
258 
notify_fsp_cb(struct files_struct * fsp,void * private_data)259 static struct files_struct *notify_fsp_cb(struct files_struct *fsp,
260 					  void *private_data)
261 {
262 	struct notify_fsp_state *state = private_data;
263 
264 	if (fsp == state->notified_fsp) {
265 		DBG_DEBUG("notify_callback called for %s\n", fsp_str_dbg(fsp));
266 		notify_fsp(fsp, state->when, state->e->action, state->e->path);
267 		return fsp;
268 	}
269 
270 	return NULL;
271 }
272 
notify_callback(struct smbd_server_connection * sconn,void * private_data,struct timespec when,const struct notify_event * e)273 void notify_callback(struct smbd_server_connection *sconn,
274 		     void *private_data, struct timespec when,
275 		     const struct notify_event *e)
276 {
277 	struct notify_fsp_state state = {
278 		.notified_fsp = private_data, .when = when, .e = e
279 	};
280 	files_forall(sconn, notify_fsp_cb, &state);
281 }
282 
change_notify_create(struct files_struct * fsp,uint32_t max_buffer_size,uint32_t filter,bool recursive)283 NTSTATUS change_notify_create(struct files_struct *fsp,
284 			      uint32_t max_buffer_size,
285 			      uint32_t filter,
286 			      bool recursive)
287 {
288 	size_t len = fsp_fullbasepath(fsp, NULL, 0);
289 	char fullpath[len+1];
290 	NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
291 
292 	/*
293 	 * Setting a changenotify needs READ/LIST access
294 	 * on the directory handle.
295 	 */
296 	if (!(fsp->access_mask & SEC_DIR_LIST)) {
297 		return NT_STATUS_ACCESS_DENIED;
298 	}
299 
300 	if (fsp->notify != NULL) {
301 		DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
302 			  "fname = %s\n", fsp->fsp_name->base_name));
303 		return NT_STATUS_INVALID_PARAMETER;
304 	}
305 
306 	if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
307 		DEBUG(0, ("talloc failed\n"));
308 		return NT_STATUS_NO_MEMORY;
309 	}
310 	fsp->notify->filter = filter;
311 	fsp->notify->subdir_filter = recursive ? filter : 0;
312 	fsp->notify->max_buffer_size = max_buffer_size;
313 
314 	fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
315 
316 	/*
317 	 * Avoid /. at the end of the path name. notify can't deal with it.
318 	 */
319 	if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
320 		fullpath[len-2] = '\0';
321 	}
322 
323 	if ((fsp->notify->filter != 0) ||
324 	    (fsp->notify->subdir_filter != 0)) {
325 		status = notify_add(fsp->conn->sconn->notify_ctx,
326 				    fullpath, fsp->notify->filter,
327 				    fsp->notify->subdir_filter, fsp);
328 	}
329 
330 	return status;
331 }
332 
change_notify_add_request(struct smb_request * req,uint32_t max_param,uint32_t filter,bool recursive,struct files_struct * fsp,void (* reply_fn)(struct smb_request * req,NTSTATUS error_code,uint8_t * buf,size_t len))333 NTSTATUS change_notify_add_request(struct smb_request *req,
334 				uint32_t max_param,
335 				uint32_t filter, bool recursive,
336 				struct files_struct *fsp,
337 				void (*reply_fn)(struct smb_request *req,
338 					NTSTATUS error_code,
339 					uint8_t *buf, size_t len))
340 {
341 	struct notify_change_request *request = NULL;
342 	struct notify_mid_map *map = NULL;
343 	struct smbd_server_connection *sconn = req->sconn;
344 
345 	DEBUG(10, ("change_notify_add_request: Adding request for %s: "
346 		   "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
347 
348 	if (!(request = talloc(NULL, struct notify_change_request))
349 	    || !(map = talloc(request, struct notify_mid_map))) {
350 		TALLOC_FREE(request);
351 		return NT_STATUS_NO_MEMORY;
352 	}
353 
354 	request->mid_map = map;
355 	map->req = request;
356 
357 	request->req = talloc_move(request, &req);
358 	request->max_param = max_param;
359 	request->filter = filter;
360 	request->fsp = fsp;
361 	request->reply_fn = reply_fn;
362 	request->backend_data = NULL;
363 
364 	DLIST_ADD_END(fsp->notify->requests, request);
365 
366 	map->mid = request->req->mid;
367 	DLIST_ADD(sconn->smb1.notify_mid_maps, map);
368 
369 	return NT_STATUS_OK;
370 }
371 
change_notify_remove_request(struct smbd_server_connection * sconn,struct notify_change_request * remove_req)372 static void change_notify_remove_request(struct smbd_server_connection *sconn,
373 					 struct notify_change_request *remove_req)
374 {
375 	files_struct *fsp;
376 	struct notify_change_request *req;
377 
378 	/*
379 	 * Paranoia checks, the fsp referenced must must have the request in
380 	 * its list of pending requests
381 	 */
382 
383 	fsp = remove_req->fsp;
384 	SMB_ASSERT(fsp->notify != NULL);
385 
386 	for (req = fsp->notify->requests; req; req = req->next) {
387 		if (req == remove_req) {
388 			break;
389 		}
390 	}
391 
392 	if (req == NULL) {
393 		smb_panic("notify_req not found in fsp's requests");
394 	}
395 
396 	DLIST_REMOVE(fsp->notify->requests, req);
397 	DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
398 	TALLOC_FREE(req);
399 }
400 
smbd_notify_cancel_by_map(struct notify_mid_map * map)401 static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
402 {
403 	struct smb_request *smbreq = map->req->req;
404 	struct smbd_server_connection *sconn = smbreq->sconn;
405 	struct smbd_smb2_request *smb2req = smbreq->smb2req;
406 	NTSTATUS notify_status = NT_STATUS_CANCELLED;
407 
408 	if (smb2req != NULL) {
409 		NTSTATUS sstatus;
410 
411 		if (smb2req->session == NULL) {
412 			sstatus = NT_STATUS_USER_SESSION_DELETED;
413 		} else {
414 			sstatus = smb2req->session->status;
415 		}
416 
417 		if (NT_STATUS_EQUAL(sstatus, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
418 			sstatus = NT_STATUS_OK;
419 		}
420 
421 		if (!NT_STATUS_IS_OK(sstatus)) {
422 			notify_status = STATUS_NOTIFY_CLEANUP;
423 		} else if (smb2req->tcon == NULL) {
424 			notify_status = STATUS_NOTIFY_CLEANUP;
425 		} else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
426 			notify_status = STATUS_NOTIFY_CLEANUP;
427 		}
428 	}
429 
430 	change_notify_reply(smbreq, notify_status,
431 			    0, NULL, map->req->reply_fn);
432 	change_notify_remove_request(sconn, map->req);
433 }
434 
435 /****************************************************************************
436  Delete entries by mid from the change notify pending queue. Always send reply.
437 *****************************************************************************/
438 
remove_pending_change_notify_requests_by_mid(struct smbd_server_connection * sconn,uint64_t mid)439 bool remove_pending_change_notify_requests_by_mid(
440 	struct smbd_server_connection *sconn, uint64_t mid)
441 {
442 	struct notify_mid_map *map;
443 
444 	for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
445 		if (map->mid == mid) {
446 			break;
447 		}
448 	}
449 
450 	if (map == NULL) {
451 		return false;
452 	}
453 
454 	smbd_notify_cancel_by_map(map);
455 	return true;
456 }
457 
smbd_notify_cancel_by_smbreq(const struct smb_request * smbreq)458 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
459 {
460 	struct smbd_server_connection *sconn = smbreq->sconn;
461 	struct notify_mid_map *map;
462 
463 	for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
464 		if (map->req->req == smbreq) {
465 			break;
466 		}
467 	}
468 
469 	if (map == NULL) {
470 		return;
471 	}
472 
473 	smbd_notify_cancel_by_map(map);
474 }
475 
smbd_notify_cancel_deleted_fn(struct files_struct * fsp,void * private_data)476 static struct files_struct *smbd_notify_cancel_deleted_fn(
477 	struct files_struct *fsp, void *private_data)
478 {
479 	struct file_id *fid = talloc_get_type_abort(
480 		private_data, struct file_id);
481 
482 	if (file_id_equal(&fsp->file_id, fid)) {
483 		remove_pending_change_notify_requests_by_fid(
484 			fsp, NT_STATUS_DELETE_PENDING);
485 	}
486 	return NULL;
487 }
488 
smbd_notify_cancel_deleted(struct messaging_context * msg,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)489 void smbd_notify_cancel_deleted(struct messaging_context *msg,
490 				void *private_data, uint32_t msg_type,
491 				struct server_id server_id, DATA_BLOB *data)
492 {
493 	struct smbd_server_connection *sconn = talloc_get_type_abort(
494 		private_data, struct smbd_server_connection);
495 	struct file_id *fid;
496 	enum ndr_err_code ndr_err;
497 
498 	fid = talloc(talloc_tos(), struct file_id);
499 	if (fid == NULL) {
500 		DEBUG(1, ("talloc failed\n"));
501 		return;
502 	}
503 
504 	ndr_err = ndr_pull_struct_blob_all(
505 		data, fid, fid, (ndr_pull_flags_fn_t)ndr_pull_file_id);
506 	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
507 		DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__,
508 			   ndr_errstr(ndr_err)));
509 		goto done;
510 	}
511 
512 	files_forall(sconn, smbd_notify_cancel_deleted_fn, fid);
513 
514 done:
515 	TALLOC_FREE(fid);
516 }
517 
smbd_notifyd_reregister(struct files_struct * fsp,void * private_data)518 static struct files_struct *smbd_notifyd_reregister(struct files_struct *fsp,
519 						    void *private_data)
520 {
521 	DBG_DEBUG("reregister %s\n", fsp->fsp_name->base_name);
522 
523 	if ((fsp->conn->sconn->notify_ctx != NULL) &&
524 	    (fsp->notify != NULL) &&
525 	    ((fsp->notify->filter != 0) ||
526 	     (fsp->notify->subdir_filter != 0))) {
527 		size_t len = fsp_fullbasepath(fsp, NULL, 0);
528 		char fullpath[len+1];
529 
530 		NTSTATUS status;
531 
532 		fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
533 		if (len > 1 && fullpath[len-1] == '.' &&
534 		    fullpath[len-2] == '/') {
535 			fullpath[len-2] = '\0';
536 		}
537 
538 		status = notify_add(fsp->conn->sconn->notify_ctx,
539 				    fullpath, fsp->notify->filter,
540 				    fsp->notify->subdir_filter, fsp);
541 		if (!NT_STATUS_IS_OK(status)) {
542 			DBG_DEBUG("notify_add failed: %s\n",
543 				  nt_errstr(status));
544 		}
545 	}
546 	return NULL;
547 }
548 
smbd_notifyd_restarted(struct messaging_context * msg,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)549 void smbd_notifyd_restarted(struct messaging_context *msg,
550 			    void *private_data, uint32_t msg_type,
551 			    struct server_id server_id, DATA_BLOB *data)
552 {
553 	struct smbd_server_connection *sconn = talloc_get_type_abort(
554 		private_data, struct smbd_server_connection);
555 
556 	TALLOC_FREE(sconn->notify_ctx);
557 
558 	sconn->notify_ctx = notify_init(sconn, sconn->msg_ctx,
559 					sconn, notify_callback);
560 	if (sconn->notify_ctx == NULL) {
561 		DBG_DEBUG("notify_init failed\n");
562 		return;
563 	}
564 
565 	files_forall(sconn, smbd_notifyd_reregister, sconn->notify_ctx);
566 }
567 
568 /****************************************************************************
569  Delete entries by fnum from the change notify pending queue.
570 *****************************************************************************/
571 
remove_pending_change_notify_requests_by_fid(files_struct * fsp,NTSTATUS status)572 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
573 						  NTSTATUS status)
574 {
575 	if (fsp->notify == NULL) {
576 		return;
577 	}
578 
579 	while (fsp->notify->requests != NULL) {
580 		change_notify_reply(fsp->notify->requests->req,
581 				    status, 0, NULL,
582 				    fsp->notify->requests->reply_fn);
583 		change_notify_remove_request(fsp->conn->sconn,
584 					     fsp->notify->requests);
585 	}
586 }
587 
notify_fname(connection_struct * conn,uint32_t action,uint32_t filter,const char * path)588 void notify_fname(connection_struct *conn, uint32_t action, uint32_t filter,
589 		  const char *path)
590 {
591 	struct notify_context *notify_ctx = conn->sconn->notify_ctx;
592 
593 	if (path[0] == '.' && path[1] == '/') {
594 		path += 2;
595 	}
596 
597 	notify_trigger(notify_ctx, action, filter, conn->connectpath, path);
598 }
599 
notify_fsp(files_struct * fsp,struct timespec when,uint32_t action,const char * name)600 static void notify_fsp(files_struct *fsp, struct timespec when,
601 		       uint32_t action, const char *name)
602 {
603 	struct notify_change_event *change, *changes;
604 	char *tmp;
605 
606 	if (fsp->notify == NULL) {
607 		/*
608 		 * Nobody is waiting, don't queue
609 		 */
610 		return;
611 	}
612 
613 	/*
614 	 * Someone has triggered a notify previously, queue the change for
615 	 * later.
616 	 */
617 
618 	if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
619 		/*
620 		 * The real number depends on the client buf, just provide a
621 		 * guard against a DoS here.  If name == NULL the CN backend is
622 		 * alerting us to a problem.  Possibly dropped events.  Clear
623 		 * queued changes and send the catch-all response to the client
624 		 * if a request is pending.
625 		 */
626 		TALLOC_FREE(fsp->notify->changes);
627 		fsp->notify->num_changes = -1;
628 		if (fsp->notify->requests != NULL) {
629 			change_notify_reply(fsp->notify->requests->req,
630 					    NT_STATUS_OK,
631 					    fsp->notify->requests->max_param,
632 					    fsp->notify,
633 					    fsp->notify->requests->reply_fn);
634 			change_notify_remove_request(fsp->conn->sconn,
635 						     fsp->notify->requests);
636 		}
637 		return;
638 	}
639 
640 	/* If we've exceeded the server side queue or received a NULL name
641 	 * from the underlying CN implementation, don't queue up any more
642 	 * requests until we can send a catch-all response to the client */
643 	if (fsp->notify->num_changes == -1) {
644 		return;
645 	}
646 
647 	if (!(changes = talloc_realloc(
648 		      fsp->notify, fsp->notify->changes,
649 		      struct notify_change_event,
650 		      fsp->notify->num_changes+1))) {
651 		DEBUG(0, ("talloc_realloc failed\n"));
652 		return;
653 	}
654 
655 	fsp->notify->changes = changes;
656 
657 	change = &(fsp->notify->changes[fsp->notify->num_changes]);
658 
659 	if (!(tmp = talloc_strdup(changes, name))) {
660 		DEBUG(0, ("talloc_strdup failed\n"));
661 		return;
662 	}
663 
664 	string_replace(tmp, '/', '\\');
665 	change->name = tmp;
666 
667 	change->when = when;
668 	change->action = action;
669 	fsp->notify->num_changes += 1;
670 
671 	if (fsp->notify->requests == NULL) {
672 		/*
673 		 * Nobody is waiting, so don't send anything. The ot
674 		 */
675 		return;
676 	}
677 
678 	if (action == NOTIFY_ACTION_OLD_NAME) {
679 		/*
680 		 * We have to send the two rename events in one reply. So hold
681 		 * the first part back.
682 		 */
683 		return;
684 	}
685 
686 	/*
687 	 * Someone is waiting for the change, trigger the reply immediately.
688 	 *
689 	 * TODO: do we have to walk the lists of requests pending?
690 	 */
691 
692 	change_notify_reply(fsp->notify->requests->req,
693 			    NT_STATUS_OK,
694 			    fsp->notify->requests->max_param,
695 			    fsp->notify,
696 			    fsp->notify->requests->reply_fn);
697 
698 	change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
699 }
700 
notify_filter_string(TALLOC_CTX * mem_ctx,uint32_t filter)701 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32_t filter)
702 {
703 	char *result = NULL;
704 
705 	result = talloc_strdup(mem_ctx, "");
706 
707 	if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
708 		result = talloc_asprintf_append(result, "FILE_NAME|");
709 	if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
710 		result = talloc_asprintf_append(result, "DIR_NAME|");
711 	if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
712 		result = talloc_asprintf_append(result, "ATTRIBUTES|");
713 	if (filter & FILE_NOTIFY_CHANGE_SIZE)
714 		result = talloc_asprintf_append(result, "SIZE|");
715 	if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
716 		result = talloc_asprintf_append(result, "LAST_WRITE|");
717 	if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
718 		result = talloc_asprintf_append(result, "LAST_ACCESS|");
719 	if (filter & FILE_NOTIFY_CHANGE_CREATION)
720 		result = talloc_asprintf_append(result, "CREATION|");
721 	if (filter & FILE_NOTIFY_CHANGE_EA)
722 		result = talloc_asprintf_append(result, "EA|");
723 	if (filter & FILE_NOTIFY_CHANGE_SECURITY)
724 		result = talloc_asprintf_append(result, "SECURITY|");
725 	if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
726 		result = talloc_asprintf_append(result, "STREAM_NAME|");
727 	if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
728 		result = talloc_asprintf_append(result, "STREAM_SIZE|");
729 	if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
730 		result = talloc_asprintf_append(result, "STREAM_WRITE|");
731 
732 	if (result == NULL) return NULL;
733 	if (*result == '\0') return result;
734 
735 	result[strlen(result)-1] = '\0';
736 	return result;
737 }
738 
sys_notify_context_create(TALLOC_CTX * mem_ctx,struct tevent_context * ev)739 struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
740 						     struct tevent_context *ev)
741 {
742 	struct sys_notify_context *ctx;
743 
744 	if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
745 		DEBUG(0, ("talloc failed\n"));
746 		return NULL;
747 	}
748 
749 	ctx->ev = ev;
750 	ctx->private_data = NULL;
751 	return ctx;
752 }
753