s3: Change fsp->fsp_name to be an smb_filename struct!
[samba.git] / source3 / smbd / notify.c
index a17afc7dee89523c1acefc5bfc78896fd2252487..8f37923865bedfaa444490203bfa7b171ae2e57c 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,6 +67,10 @@ 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++) {
@@ -81,7 +88,7 @@ static bool notify_marshall_changes(int num_changes,
 
                c = &changes[i];
 
-               if (!convert_string_allocate(talloc_tos(), CH_UNIX, CH_UTF16LE,
+               if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE,
                        c->name, strlen(c->name)+1, &uni_name.buffer,
                        &namelen, True) || (uni_name.buffer == NULL)) {
                        goto fail;
@@ -131,37 +138,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 +167,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 +182,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 +200,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 +227,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 +252,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 +268,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 +289,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 +300,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 +312,8 @@ 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);
 }
 
@@ -329,8 +329,9 @@ 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);
        }
 }
@@ -339,17 +340,37 @@ void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
                  const char *path)
 {
        char *fullpath;
+       char *parent;
+       const char *name;
 
        if (path[0] == '.' && path[1] == '/') {
                path += 2;
        }
-       if (asprintf(&fullpath, "%s/%s", conn->connectpath, path) == -1) {
+       if (parent_dirname(talloc_tos(), path, &parent, &name)) {
+               struct smb_filename *smb_fname_parent = NULL;
+               NTSTATUS status;
+
+               status = create_synthetic_smb_fname(talloc_tos(), parent, NULL,
+                                                   NULL, &smb_fname_parent);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return;
+               }
+               if (SMB_VFS_STAT(conn, smb_fname_parent) != -1) {
+                       notify_onelevel(conn->notify_ctx, action, filter,
+                           SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent->st),
+                           name);
+               }
+               TALLOC_FREE(smb_fname_parent);
+       }
+
+       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 +403,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 +464,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);
 }