s3:smbd: fix max_buffer handling of initial notify requests
[samba.git] / source3 / smbd / notify.c
index c824ced536823231385c2908fd089693cdda6f15..bf3fff7b97db7f00e54bfb5422b869610c28f0f0 100644 (file)
@@ -32,11 +32,19 @@ struct notify_change_event {
 };
 
 struct notify_change_buf {
+       /*
+        * Filters for reinitializing after notifyd has been restarted
+        */
+       uint32_t filter;
+       uint32_t subdir_filter;
+
        /*
         * If no requests are pending, changes are queued here. Simple array,
         * we only append.
         */
 
+       uint32_t max_buffer_size;
+
        /*
         * num_changes == -1 means that we have got a catch-all change, when
         * asked we just return NT_STATUS_OK without specific changes.
@@ -218,11 +226,13 @@ void change_notify_reply(struct smb_request *req,
                return;
        }
 
-       if (max_param == 0 || notify_buf == NULL) {
+       if (notify_buf == NULL) {
                reply_fn(req, NT_STATUS_OK, NULL, 0);
                return;
        }
 
+       max_param = MIN(max_param, notify_buf->max_buffer_size);
+
        if (!notify_marshall_changes(notify_buf->num_changes, max_param,
                                        notify_buf->changes, &blob)) {
                /*
@@ -240,20 +250,43 @@ void change_notify_reply(struct smb_request *req,
        notify_buf->num_changes = 0;
 }
 
-void notify_callback(void *private_data, struct timespec when,
+struct notify_fsp_state {
+       struct files_struct *notified_fsp;
+       struct timespec when;
+       const struct notify_event *e;
+};
+
+static struct files_struct *notify_fsp_cb(struct files_struct *fsp,
+                                         void *private_data)
+{
+       struct notify_fsp_state *state = private_data;
+
+       if (fsp == state->notified_fsp) {
+               DBG_DEBUG("notify_callback called for %s\n", fsp_str_dbg(fsp));
+               notify_fsp(fsp, state->when, state->e->action, state->e->path);
+               return fsp;
+       }
+
+       return NULL;
+}
+
+void notify_callback(struct smbd_server_connection *sconn,
+                    void *private_data, struct timespec when,
                     const struct notify_event *e)
 {
-       files_struct *fsp = (files_struct *)private_data;
-       DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
-       notify_fsp(fsp, when, e->action, e->path);
+       struct notify_fsp_state state = {
+               .notified_fsp = private_data, .when = when, .e = e
+       };
+       files_forall(sconn, notify_fsp_cb, &state);
 }
 
-NTSTATUS change_notify_create(struct files_struct *fsp, uint32_t filter,
+NTSTATUS change_notify_create(struct files_struct *fsp,
+                             uint32_t max_buffer_size,
+                             uint32_t filter,
                              bool recursive)
 {
        size_t len = fsp_fullbasepath(fsp, NULL, 0);
        char fullpath[len+1];
-       uint32_t subdir_filter;
        NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
 
        if (fsp->notify != NULL) {
@@ -266,6 +299,9 @@ NTSTATUS change_notify_create(struct files_struct *fsp, uint32_t filter,
                DEBUG(0, ("talloc failed\n"));
                return NT_STATUS_NO_MEMORY;
        }
+       fsp->notify->filter = filter;
+       fsp->notify->subdir_filter = recursive ? filter : 0;
+       fsp->notify->max_buffer_size = max_buffer_size;
 
        fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
 
@@ -276,12 +312,11 @@ NTSTATUS change_notify_create(struct files_struct *fsp, uint32_t filter,
                fullpath[len-2] = '\0';
        }
 
-       subdir_filter = recursive ? filter : 0;
-
-       if ((filter != 0) || (subdir_filter != 0)) {
+       if ((fsp->notify->filter != 0) ||
+           (fsp->notify->subdir_filter != 0)) {
                status = notify_add(fsp->conn->sconn->notify_ctx,
-                                   fullpath, filter, subdir_filter,
-                                   notify_callback, fsp);
+                                   fullpath, fsp->notify->filter,
+                                   fsp->notify->subdir_filter, fsp);
        }
 
        return status;
@@ -363,12 +398,21 @@ static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
        NTSTATUS notify_status = NT_STATUS_CANCELLED;
 
        if (smb2req != NULL) {
+               NTSTATUS sstatus;
+
                if (smb2req->session == NULL) {
-                       notify_status = STATUS_NOTIFY_CLEANUP;
-               } else if (!NT_STATUS_IS_OK(smb2req->session->status)) {
-                       notify_status = STATUS_NOTIFY_CLEANUP;
+                       sstatus = NT_STATUS_USER_SESSION_DELETED;
+               } else {
+                       sstatus = smb2req->session->status;
+               }
+
+               if (NT_STATUS_EQUAL(sstatus, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
+                       sstatus = NT_STATUS_OK;
                }
-               if (smb2req->tcon == NULL) {
+
+               if (!NT_STATUS_IS_OK(sstatus)) {
+                       notify_status = STATUS_NOTIFY_CLEANUP;
+               } else if (smb2req->tcon == NULL) {
                        notify_status = STATUS_NOTIFY_CLEANUP;
                } else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
                        notify_status = STATUS_NOTIFY_CLEANUP;
@@ -462,6 +506,56 @@ done:
        TALLOC_FREE(fid);
 }
 
+static struct files_struct *smbd_notifyd_reregister(struct files_struct *fsp,
+                                                   void *private_data)
+{
+       DBG_DEBUG("reregister %s\n", fsp->fsp_name->base_name);
+
+       if ((fsp->conn->sconn->notify_ctx != NULL) &&
+           (fsp->notify != NULL) &&
+           ((fsp->notify->filter != 0) ||
+            (fsp->notify->subdir_filter != 0))) {
+               size_t len = fsp_fullbasepath(fsp, NULL, 0);
+               char fullpath[len+1];
+
+               NTSTATUS status;
+
+               fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
+               if (len > 1 && fullpath[len-1] == '.' &&
+                   fullpath[len-2] == '/') {
+                       fullpath[len-2] = '\0';
+               }
+
+               status = notify_add(fsp->conn->sconn->notify_ctx,
+                                   fullpath, fsp->notify->filter,
+                                   fsp->notify->subdir_filter, fsp);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DBG_DEBUG("notify_add failed: %s\n",
+                                 nt_errstr(status));
+               }
+       }
+       return NULL;
+}
+
+void smbd_notifyd_restarted(struct messaging_context *msg,
+                           void *private_data, uint32_t msg_type,
+                           struct server_id server_id, DATA_BLOB *data)
+{
+       struct smbd_server_connection *sconn = talloc_get_type_abort(
+               private_data, struct smbd_server_connection);
+
+       TALLOC_FREE(sconn->notify_ctx);
+
+       sconn->notify_ctx = notify_init(sconn, sconn->msg_ctx,
+                                       sconn, notify_callback);
+       if (sconn->notify_ctx == NULL) {
+               DBG_DEBUG("notify_init failed\n");
+               return;
+       }
+
+       files_forall(sconn, smbd_notifyd_reregister, sconn->notify_ctx);
+}
+
 /****************************************************************************
  Delete entries by fnum from the change notify pending queue.
 *****************************************************************************/