s3:smbd: fix max_buffer handling of initial notify requests
[samba.git] / source3 / smbd / notify.c
index 3f2d07cf6670bf3695bdb935691018f42cdb0264..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.
@@ -138,6 +146,7 @@ static bool notify_marshall_changes(int num_changes,
                struct notify_change_event *c;
                struct FILE_NOTIFY_INFORMATION m;
                DATA_BLOB blob;
+               uint16_t pad = 0;
 
                /* Coalesce any identical records. */
                while (i+1 < num_changes &&
@@ -151,12 +160,23 @@ static bool notify_marshall_changes(int num_changes,
                m.FileName1 = c->name;
                m.FileNameLength = strlen_m(c->name)*2;
                m.Action = c->action;
-               m.NextEntryOffset = (i == num_changes-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
+
+               m._pad = data_blob_null;
 
                /*
                 * Offset to next entry, only if there is one
                 */
 
+               if (i == (num_changes-1)) {
+                       m.NextEntryOffset = 0;
+               } else {
+                       if ((m.FileNameLength % 4) == 2) {
+                               m._pad = data_blob_const(&pad, 2);
+                       }
+                       m.NextEntryOffset =
+                               ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
+               }
+
                ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
                        (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -206,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)) {
                /*
@@ -228,29 +250,43 @@ void change_notify_reply(struct smb_request *req,
        notify_buf->num_changes = 0;
 }
 
-static void notify_callback(void *private_data, struct timespec when,
-                           const struct notify_event *e)
+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)
 {
-       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 = 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;
 }
 
-static void sys_notify_callback(struct sys_notify_context *ctx,
-                               void *private_data,
-                               struct notify_event *e)
+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, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp)));
-       notify_fsp(fsp, timespec_current(), 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)
 {
-       char *fullpath;
-       size_t len;
-       uint32_t subdir_filter;
+       size_t len = fsp_fullbasepath(fsp, NULL, 0);
+       char fullpath[len+1];
        NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
 
        if (fsp->notify != NULL) {
@@ -263,46 +299,26 @@ 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;
 
-       /* Do notify operations on the base_name. */
-       fullpath = talloc_asprintf(
-               talloc_tos(), "%s/%s", fsp->conn->connectpath,
-               fsp->fsp_name->base_name);
-       if (fullpath == NULL) {
-               DEBUG(0, ("talloc_asprintf failed\n"));
-               TALLOC_FREE(fsp->notify);
-               return NT_STATUS_NO_MEMORY;
-       }
+       fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
 
        /*
         * Avoid /. at the end of the path name. notify can't deal with it.
         */
-       len = strlen(fullpath);
        if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
                fullpath[len-2] = '\0';
        }
 
-       subdir_filter = recursive ? filter : 0;
-
-       if (fsp->conn->sconn->sys_notify_ctx != NULL) {
-               void *sys_notify_handle = NULL;
-
-               status = SMB_VFS_NOTIFY_WATCH(
-                       fsp->conn, fsp->conn->sconn->sys_notify_ctx,
-                       fullpath, &filter, &subdir_filter,
-                       sys_notify_callback, fsp, &sys_notify_handle);
-
-               if (NT_STATUS_IS_OK(status)) {
-                       talloc_steal(fsp->notify, sys_notify_handle);
-               }
-       }
-
-       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);
        }
-       TALLOC_FREE(fullpath);
+
        return status;
 }
 
@@ -337,8 +353,7 @@ NTSTATUS change_notify_add_request(struct smb_request *req,
        request->reply_fn = reply_fn;
        request->backend_data = NULL;
 
-       DLIST_ADD_END(fsp->notify->requests, request,
-                     struct notify_change_request *);
+       DLIST_ADD_END(fsp->notify->requests, request);
 
        map->mid = request->req->mid;
        DLIST_ADD(sconn->smb1.notify_mid_maps, map);
@@ -375,6 +390,40 @@ static void change_notify_remove_request(struct smbd_server_connection *sconn,
        TALLOC_FREE(req);
 }
 
+static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
+{
+       struct smb_request *smbreq = map->req->req;
+       struct smbd_server_connection *sconn = smbreq->sconn;
+       struct smbd_smb2_request *smb2req = smbreq->smb2req;
+       NTSTATUS notify_status = NT_STATUS_CANCELLED;
+
+       if (smb2req != NULL) {
+               NTSTATUS sstatus;
+
+               if (smb2req->session == NULL) {
+                       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 (!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;
+               }
+       }
+
+       change_notify_reply(smbreq, notify_status,
+                           0, NULL, map->req->reply_fn);
+       change_notify_remove_request(sconn, map->req);
+}
+
 /****************************************************************************
  Delete entries by mid from the change notify pending queue. Always send reply.
 *****************************************************************************/
@@ -394,9 +443,7 @@ void remove_pending_change_notify_requests_by_mid(
                return;
        }
 
-       change_notify_reply(map->req->req,
-                           NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
-       change_notify_remove_request(sconn, map->req);
+       smbd_notify_cancel_by_map(map);
 }
 
 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
@@ -414,9 +461,7 @@ void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
                return;
        }
 
-       change_notify_reply(map->req->req,
-                           NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
-       change_notify_remove_request(sconn, map->req);
+       smbd_notify_cancel_by_map(map);
 }
 
 static struct files_struct *smbd_notify_cancel_deleted_fn(
@@ -461,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.
 *****************************************************************************/