Fix for bug #8998 - Notify code can miss a ChDir.
[samba.git] / source3 / smbd / notify.c
index d141fb218077a34945c57b00ddea3f07bdfa2608..1f664d01d7748ae44ea958bb08b388ebc125c04b 100644 (file)
@@ -28,6 +28,9 @@ struct notify_change_request {
        struct smb_request *req;
        uint32 filter;
        uint32 max_param;
+       void (*reply_fn)(struct smb_request *req,
+                        NTSTATUS error_code,
+                        uint8_t *buf, size_t len);
        struct notify_mid_map *mid_map;
        void *backend_data;
 };
@@ -64,11 +67,16 @@ static bool notify_marshall_changes(int num_changes,
        int i;
        UNISTR uni_name;
 
+       if (num_changes == -1) {
+               return false;
+       }
+
        uni_name.buffer = NULL;
 
        for (i=0; i<num_changes; i++) {
                struct notify_change *c;
                size_t namelen;
+               int    rem = 0;
                uint32 u32_tmp; /* Temp arg to prs_uint32 to avoid
                                 * signed/unsigned issues */
 
@@ -94,6 +102,11 @@ static bool notify_marshall_changes(int num_changes,
                 */
 
                u32_tmp = (i == num_changes-1) ? 0 : namelen + 12;
+
+               /* Align on 4-byte boundary according to MS-CIFS 2.2.7.4.2 */
+               if ((rem = u32_tmp % 4 ) != 0)
+                       u32_tmp += 4 - rem;
+
                if (!prs_uint32("offset", ps, 1, &u32_tmp)) goto fail;
 
                u32_tmp = c->action;
@@ -109,6 +122,10 @@ static bool notify_marshall_changes(int num_changes,
                 */
                prs_set_offset(ps, prs_offset(ps)-2);
 
+               if (rem != 0) {
+                       if (!prs_align_custom(ps, 4)) goto fail;
+               }
+
                TALLOC_FREE(uni_name.buffer);
 
                if (prs_offset(ps) > max_offset) {
@@ -131,37 +148,24 @@ static bool notify_marshall_changes(int num_changes,
  Setup the common parts of the return packet and send it.
 *****************************************************************************/
 
-static void change_notify_reply_packet(connection_struct *conn,
-                                      struct smb_request *req,
-                                      NTSTATUS error_code)
+void change_notify_reply(connection_struct *conn,
+                        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))
 {
-       reply_outbuf(req, 18, 0);
+       prs_struct ps;
 
        if (!NT_STATUS_IS_OK(error_code)) {
-               error_packet_set((char *)req->outbuf, 0, 0, error_code,
-                                __LINE__,__FILE__);
-       }
-
-       show_msg((char *)req->outbuf);
-       if (!srv_send_smb(smbd_server_fd(),
-                         (char *)req->outbuf,
-                         true, req->seqnum+1,
-                         req->encrypted, &req->pcd)) {
-               exit_server_cleanly("change_notify_reply_packet: srv_send_smb "
-                                   "failed.");
+               reply_fn(req, error_code, NULL, 0);
+               return;
        }
-       TALLOC_FREE(req->outbuf);
-}
 
-void change_notify_reply(connection_struct *conn,
-                        struct smb_request *req, uint32 max_param,
-                        struct notify_change_buf *notify_buf)
-{
-       prs_struct ps;
-
-       if (notify_buf->num_changes == -1) {
-               change_notify_reply_packet(conn, req, NT_STATUS_OK);
-               notify_buf->num_changes = 0;
+       if (max_param == 0 || notify_buf == NULL) {
+               reply_fn(req, NT_STATUS_OK, NULL, 0);
                return;
        }
 
@@ -173,14 +177,12 @@ void change_notify_reply(connection_struct *conn,
                 * We exceed what the client is willing to accept. Send
                 * nothing.
                 */
-               change_notify_reply_packet(conn, req, NT_STATUS_OK);
-               goto done;
+               prs_mem_free(&ps);
+               prs_init_empty(&ps, NULL, MARSHALL);
        }
 
-       send_nt_replies(conn, req, NT_STATUS_OK, prs_data_p(&ps),
-                       prs_offset(&ps), NULL, 0);
+       reply_fn(req, NT_STATUS_OK, (uint8_t *)prs_data_p(&ps), prs_offset(&ps));
 
- done:
        prs_mem_free(&ps);
 
        TALLOC_FREE(notify_buf->changes);
@@ -190,7 +192,7 @@ void change_notify_reply(connection_struct *conn,
 static void notify_callback(void *private_data, const struct notify_event *e)
 {
        files_struct *fsp = (files_struct *)private_data;
-       DEBUG(10, ("notify_callback called for %s\n", fsp->fsp_name));
+       DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
        notify_fsp(fsp, e->action, e->path);
 }
 
@@ -208,8 +210,9 @@ NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
                return NT_STATUS_NO_MEMORY;
        }
 
+       /* Do notify operations on the base_name. */
        if (asprintf(&fullpath, "%s/%s", fsp->conn->connectpath,
-                    fsp->fsp_name) == -1) {
+                    fsp->fsp_name->base_name) == -1) {
                DEBUG(0, ("asprintf failed\n"));
                TALLOC_FREE(fsp->notify);
                return NT_STATUS_NO_MEMORY;
@@ -234,13 +237,17 @@ NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
 NTSTATUS change_notify_add_request(struct smb_request *req,
                                uint32 max_param,
                                uint32 filter, bool recursive,
-                               struct files_struct *fsp)
+                               struct files_struct *fsp,
+                               void (*reply_fn)(struct smb_request *req,
+                                       NTSTATUS error_code,
+                                       uint8_t *buf, size_t len))
 {
        struct notify_change_request *request = NULL;
        struct notify_mid_map *map = NULL;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        DEBUG(10, ("change_notify_add_request: Adding request for %s: "
-                  "max_param = %d\n", fsp->fsp_name, (int)max_param));
+                  "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
 
        if (!(request = talloc(NULL, struct notify_change_request))
            || !(map = talloc(request, struct notify_mid_map))) {
@@ -255,13 +262,14 @@ NTSTATUS change_notify_add_request(struct smb_request *req,
        request->max_param = max_param;
        request->filter = filter;
        request->fsp = fsp;
+       request->reply_fn = reply_fn;
        request->backend_data = NULL;
 
        DLIST_ADD_END(fsp->notify->requests, request,
                      struct notify_change_request *);
 
        map->mid = request->req->mid;
-       DLIST_ADD(notify_changes_by_mid, map);
+       DLIST_ADD(sconn->smb1.notify_mid_maps, map);
 
        return NT_STATUS_OK;
 }
@@ -270,6 +278,7 @@ static void change_notify_remove_request(struct notify_change_request *remove_re
 {
        files_struct *fsp;
        struct notify_change_request *req;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        /*
         * Paranoia checks, the fsp referenced must must have the request in
@@ -290,7 +299,7 @@ static void change_notify_remove_request(struct notify_change_request *remove_re
        }
 
        DLIST_REMOVE(fsp->notify->requests, req);
-       DLIST_REMOVE(notify_changes_by_mid, req->mid_map);
+       DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
        TALLOC_FREE(req);
 }
 
@@ -301,8 +310,9 @@ static void change_notify_remove_request(struct notify_change_request *remove_re
 void remove_pending_change_notify_requests_by_mid(uint16 mid)
 {
        struct notify_mid_map *map;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
-       for (map = notify_changes_by_mid; map; map = map->next) {
+       for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
                if (map->mid == mid) {
                        break;
                }
@@ -312,8 +322,28 @@ void remove_pending_change_notify_requests_by_mid(uint16 mid)
                return;
        }
 
-       change_notify_reply_packet(map->req->fsp->conn, map->req->req,
-                                  NT_STATUS_CANCELLED);
+       change_notify_reply(map->req->fsp->conn, map->req->req,
+                           NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
+       change_notify_remove_request(map->req);
+}
+
+void smbd_notify_cancel_by_smbreq(struct smbd_server_connection *sconn,
+                                 const struct smb_request *smbreq)
+{
+       struct notify_mid_map *map;
+
+       for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
+               if (map->req->req == smbreq) {
+                       break;
+               }
+       }
+
+       if (map == NULL) {
+               return;
+       }
+
+       change_notify_reply(map->req->fsp->conn, map->req->req,
+                           NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
        change_notify_remove_request(map->req);
 }
 
@@ -329,27 +359,71 @@ void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
        }
 
        while (fsp->notify->requests != NULL) {
-               change_notify_reply_packet(
-                       fsp->conn, fsp->notify->requests->req, status);
+               change_notify_reply(fsp->conn, fsp->notify->requests->req,
+                                   status, 0, NULL,
+                                   fsp->notify->requests->reply_fn);
                change_notify_remove_request(fsp->notify->requests);
        }
 }
 
+static void notify_parent_dir(connection_struct *conn,
+                               uint32 action, uint32 filter,
+                               const char *path)
+{
+       struct smb_filename smb_fname_parent;
+       char *parent;
+       const char *name;
+       char *oldwd;
+
+       if (!parent_dirname(talloc_tos(), path, &parent, &name)) {
+               DEBUG(1, ("Can't get parent dirname, giving up\n"));
+               return;
+       }
+
+       ZERO_STRUCT(smb_fname_parent);
+       smb_fname_parent.base_name = parent;
+
+       oldwd = vfs_GetWd(parent, conn);
+       if (oldwd == NULL) {
+               DEBUG(1, ("vfs_GetWd failed!\n"));
+               goto done;
+       }
+       if (vfs_ChDir(conn, conn->connectpath) == -1) {
+               DEBUG(1, ("Could not chdir to connect path!\n"));
+               goto done;
+       }
+
+       if (SMB_VFS_STAT(conn, &smb_fname_parent) == -1) {
+               goto chdir_done;
+       }
+
+       notify_onelevel(conn->notify_ctx, action, filter,
+                       SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent.st),
+                       name);
+chdir_done:
+       vfs_ChDir(conn, oldwd);
+done:
+       TALLOC_FREE(parent);
+}
+
 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
                  const char *path)
 {
-       char *fullpath;
+       char *fullpath = NULL;
 
        if (path[0] == '.' && path[1] == '/') {
                path += 2;
        }
-       if (asprintf(&fullpath, "%s/%s", conn->connectpath, path) == -1) {
+       notify_parent_dir(conn, action, filter, path);
+
+       fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
+                                  path);
+       if (fullpath == NULL) {
                DEBUG(0, ("asprintf failed\n"));
                return;
        }
-
        notify_trigger(conn->notify_ctx, action, filter, fullpath);
-       SAFE_FREE(fullpath);
+       TALLOC_FREE(fullpath);
 }
 
 static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
@@ -382,8 +456,10 @@ static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
                if (fsp->notify->requests != NULL) {
                        change_notify_reply(fsp->conn,
                                            fsp->notify->requests->req,
+                                           NT_STATUS_OK,
                                            fsp->notify->requests->max_param,
-                                           fsp->notify);
+                                           fsp->notify,
+                                           fsp->notify->requests->reply_fn);
                        change_notify_remove_request(fsp->notify->requests);
                }
                return;
@@ -441,8 +517,10 @@ static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
 
        change_notify_reply(fsp->conn,
                            fsp->notify->requests->req,
+                           NT_STATUS_OK,
                            fsp->notify->requests->max_param,
-                           fsp->notify);
+                           fsp->notify,
+                           fsp->notify->requests->reply_fn);
 
        change_notify_remove_request(fsp->notify->requests);
 }