LCOV - code coverage report
Current view: top level - source3/smbd - notify.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 306 416 73.6 %
Date: 2024-04-21 15:09:00 Functions: 20 23 87.0 %

          Line data    Source code
       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             : #include "libcli/security/privileges.h"
      28             : #include "libcli/security/security.h"
      29             : 
      30             : struct notify_change_event {
      31             :         struct timespec when;
      32             :         uint32_t action;
      33             :         const char *name;
      34             : };
      35             : 
      36             : struct notify_change_buf {
      37             :         /*
      38             :          * Filters for reinitializing after notifyd has been restarted
      39             :          */
      40             :         uint32_t filter;
      41             :         uint32_t subdir_filter;
      42             : 
      43             :         /*
      44             :          * If no requests are pending, changes are queued here. Simple array,
      45             :          * we only append.
      46             :          */
      47             : 
      48             :         uint32_t max_buffer_size;
      49             : 
      50             :         /*
      51             :          * num_changes == -1 means that we have got a catch-all change, when
      52             :          * asked we just return NT_STATUS_OK without specific changes.
      53             :          */
      54             :         int num_changes;
      55             :         struct notify_change_event *changes;
      56             : 
      57             :         /*
      58             :          * If no changes are around requests are queued here. Using a linked
      59             :          * list, because we have to append at the end and delete from the top.
      60             :          */
      61             :         struct notify_change_request *requests;
      62             : };
      63             : 
      64             : struct notify_change_request {
      65             :         struct notify_change_request *prev, *next;
      66             :         struct files_struct *fsp;       /* backpointer for cancel by mid */
      67             :         struct smb_request *req;
      68             :         uint32_t filter;
      69             :         uint32_t max_param;
      70             :         void (*reply_fn)(struct smb_request *req,
      71             :                          NTSTATUS error_code,
      72             :                          uint8_t *buf, size_t len);
      73             :         struct notify_mid_map *mid_map;
      74             :         void *backend_data;
      75             : };
      76             : 
      77             : static void notify_fsp(files_struct *fsp, struct timespec when,
      78             :                        uint32_t action, const char *name);
      79             : 
      80        2780 : bool change_notify_fsp_has_changes(struct files_struct *fsp)
      81             : {
      82        2780 :         if (fsp == NULL) {
      83           0 :                 return false;
      84             :         }
      85             : 
      86        2780 :         if (fsp->notify == NULL) {
      87           0 :                 return false;
      88             :         }
      89             : 
      90        2780 :         if (fsp->notify->num_changes == 0) {
      91        2672 :                 return false;
      92             :         }
      93             : 
      94         108 :         return true;
      95             : }
      96             : 
      97             : /*
      98             :  * For NTCancel, we need to find the notify_change_request indexed by
      99             :  * mid. Separate list here.
     100             :  */
     101             : 
     102             : struct notify_mid_map {
     103             :         struct notify_mid_map *prev, *next;
     104             :         struct notify_change_request *req;
     105             :         uint64_t mid;
     106             : };
     107             : 
     108        1080 : static bool notify_change_record_identical(struct notify_change_event *c1,
     109             :                                            struct notify_change_event *c2)
     110             : {
     111             :         /* Note this is deliberately case sensitive. */
     112        1080 :         if (c1->action == c2->action &&
     113         300 :                         strcmp(c1->name, c2->name) == 0) {
     114           0 :                 return True;
     115             :         }
     116        1080 :         return False;
     117             : }
     118             : 
     119        3290 : static int compare_notify_change_events(const void *p1, const void *p2)
     120             : {
     121        3290 :         const struct notify_change_event *e1 = p1;
     122        3290 :         const struct notify_change_event *e2 = p2;
     123             : 
     124        3290 :         return timespec_compare(&e1->when, &e2->when);
     125             : }
     126             : 
     127         242 : static bool notify_marshall_changes(int num_changes,
     128             :                                 uint32_t max_offset,
     129             :                                 struct notify_change_event *changes,
     130             :                                 DATA_BLOB *final_blob)
     131             : {
     132           0 :         int i;
     133             : 
     134         242 :         if (num_changes == -1) {
     135           0 :                 return false;
     136             :         }
     137             : 
     138             :         /*
     139             :          * Sort the notifies by timestamp when the event happened to avoid
     140             :          * coalescing and thus dropping events.
     141             :          */
     142             : 
     143         242 :         qsort(changes, num_changes,
     144             :               sizeof(*changes), compare_notify_change_events);
     145             : 
     146        1554 :         for (i=0; i<num_changes; i++) {
     147           0 :                 enum ndr_err_code ndr_err;
     148           0 :                 struct notify_change_event *c;
     149           0 :                 struct FILE_NOTIFY_INFORMATION m;
     150           0 :                 DATA_BLOB blob;
     151        1318 :                 uint16_t pad = 0;
     152             : 
     153             :                 /* Coalesce any identical records. */
     154        2398 :                 while (i+1 < num_changes &&
     155        1080 :                         notify_change_record_identical(&changes[i],
     156        1080 :                                                 &changes[i+1])) {
     157           0 :                         i++;
     158             :                 }
     159             : 
     160        1318 :                 c = &changes[i];
     161             : 
     162        1318 :                 m.FileName1 = c->name;
     163        1318 :                 m.FileNameLength = strlen_m(c->name)*2;
     164        1318 :                 m.Action = c->action;
     165             : 
     166        1318 :                 m._pad = data_blob_null;
     167             : 
     168             :                 /*
     169             :                  * Offset to next entry, only if there is one
     170             :                  */
     171             : 
     172        1318 :                 if (i == (num_changes-1)) {
     173         238 :                         m.NextEntryOffset = 0;
     174             :                 } else {
     175        1080 :                         if ((m.FileNameLength % 4) == 2) {
     176         456 :                                 m._pad = data_blob_const(&pad, 2);
     177             :                         }
     178        1080 :                         m.NextEntryOffset =
     179        1080 :                                 ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
     180             :                 }
     181             : 
     182        1318 :                 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
     183             :                         (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
     184        1318 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     185           6 :                         return false;
     186             :                 }
     187             : 
     188        1318 :                 if (DEBUGLEVEL >= 10) {
     189           0 :                         NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
     190             :                 }
     191             : 
     192        1318 :                 if (!data_blob_append(talloc_tos(), final_blob,
     193        1318 :                                       blob.data, blob.length)) {
     194           0 :                         data_blob_free(&blob);
     195           0 :                         return false;
     196             :                 }
     197             : 
     198        1318 :                 data_blob_free(&blob);
     199             : 
     200        1318 :                 if (final_blob->length > max_offset) {
     201             :                         /* Too much data for client. */
     202           6 :                         DEBUG(10, ("Client only wanted %d bytes, trying to "
     203             :                                    "marshall %d bytes\n", (int)max_offset,
     204             :                                    (int)final_blob->length));
     205           6 :                         return False;
     206             :                 }
     207             :         }
     208             : 
     209         236 :         return True;
     210             : }
     211             : 
     212             : /****************************************************************************
     213             :  Setup the common parts of the return packet and send it.
     214             : *****************************************************************************/
     215             : 
     216        2780 : void change_notify_reply(struct smb_request *req,
     217             :                          NTSTATUS error_code,
     218             :                          uint32_t max_param,
     219             :                          struct notify_change_buf *notify_buf,
     220             :                          void (*reply_fn)(struct smb_request *req,
     221             :                                           NTSTATUS error_code,
     222             :                                           uint8_t *buf, size_t len))
     223             : {
     224        2780 :         DATA_BLOB blob = data_blob_null;
     225             : 
     226        2780 :         if (!NT_STATUS_IS_OK(error_code)) {
     227        2528 :                 reply_fn(req, error_code, NULL, 0);
     228        2554 :                 return;
     229             :         }
     230             : 
     231         252 :         if (notify_buf == NULL) {
     232          10 :                 reply_fn(req, NT_STATUS_OK, NULL, 0);
     233          10 :                 return;
     234             :         }
     235             : 
     236         242 :         max_param = MIN(max_param, notify_buf->max_buffer_size);
     237             : 
     238         242 :         if (!notify_marshall_changes(notify_buf->num_changes, max_param,
     239             :                                         notify_buf->changes, &blob)) {
     240             :                 /*
     241             :                  * We exceed what the client is willing to accept. Send
     242             :                  * nothing.
     243             :                  */
     244           6 :                 data_blob_free(&blob);
     245             :         }
     246             : 
     247         242 :         reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
     248             : 
     249         242 :         data_blob_free(&blob);
     250             : 
     251         242 :         TALLOC_FREE(notify_buf->changes);
     252         242 :         notify_buf->num_changes = 0;
     253             : }
     254             : 
     255             : struct notify_fsp_state {
     256             :         struct files_struct *notified_fsp;
     257             :         struct timespec when;
     258             :         const struct notify_event *e;
     259             : };
     260             : 
     261        9412 : static struct files_struct *notify_fsp_cb(struct files_struct *fsp,
     262             :                                           void *private_data)
     263             : {
     264        9412 :         struct notify_fsp_state *state = private_data;
     265             : 
     266        9412 :         if (fsp == state->notified_fsp) {
     267        1969 :                 DBG_DEBUG("notify_callback called for %s\n", fsp_str_dbg(fsp));
     268        1969 :                 notify_fsp(fsp, state->when, state->e->action, state->e->path);
     269        1969 :                 return fsp;
     270             :         }
     271             : 
     272        7443 :         return NULL;
     273             : }
     274             : 
     275        1969 : void notify_callback(struct smbd_server_connection *sconn,
     276             :                      void *private_data, struct timespec when,
     277             :                      const struct notify_event *e)
     278             : {
     279        1969 :         struct notify_fsp_state state = {
     280             :                 .notified_fsp = private_data, .when = when, .e = e
     281             :         };
     282        1969 :         files_forall(sconn, notify_fsp_cb, &state);
     283        1969 : }
     284             : 
     285        1868 : NTSTATUS change_notify_create(struct files_struct *fsp,
     286             :                               uint32_t max_buffer_size,
     287             :                               uint32_t filter,
     288             :                               bool recursive)
     289        1868 : {
     290        1868 :         size_t len = fsp_fullbasepath(fsp, NULL, 0);
     291        1868 :         char fullpath[len+1];
     292        1868 :         NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
     293             : 
     294             :         /*
     295             :          * Setting a changenotify needs READ/LIST access
     296             :          * on the directory handle.
     297             :          */
     298        1868 :         status = check_any_access_fsp(fsp, SEC_DIR_LIST);
     299        1868 :         if (!NT_STATUS_IS_OK(status)) {
     300           2 :                 return status;
     301             :         }
     302             : 
     303        1866 :         if (fsp->notify != NULL) {
     304           0 :                 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
     305             :                           "fname = %s\n", fsp->fsp_name->base_name));
     306           0 :                 return NT_STATUS_INVALID_PARAMETER;
     307             :         }
     308             : 
     309        1866 :         if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
     310           0 :                 DEBUG(0, ("talloc failed\n"));
     311           0 :                 return NT_STATUS_NO_MEMORY;
     312             :         }
     313        1866 :         fsp->notify->filter = filter;
     314        1866 :         fsp->notify->subdir_filter = recursive ? filter : 0;
     315        1866 :         fsp->notify->max_buffer_size = max_buffer_size;
     316             : 
     317        1866 :         fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
     318             : 
     319        1866 :         if ((fsp->notify->filter != 0) ||
     320           0 :             (fsp->notify->subdir_filter != 0)) {
     321        1866 :                 status = notify_add(fsp->conn->sconn->notify_ctx,
     322        1850 :                                     fullpath, fsp->notify->filter,
     323        1850 :                                     fsp->notify->subdir_filter, fsp);
     324             :         }
     325             : 
     326        1866 :         return status;
     327             : }
     328             : 
     329        2672 : NTSTATUS change_notify_add_request(struct smb_request *req,
     330             :                                 uint32_t max_param,
     331             :                                 uint32_t filter, bool recursive,
     332             :                                 struct files_struct *fsp,
     333             :                                 void (*reply_fn)(struct smb_request *req,
     334             :                                         NTSTATUS error_code,
     335             :                                         uint8_t *buf, size_t len))
     336             : {
     337        2672 :         struct notify_change_request *request = NULL;
     338        2672 :         struct notify_mid_map *map = NULL;
     339        2672 :         struct smbd_server_connection *sconn = req->sconn;
     340             : 
     341        2672 :         DEBUG(10, ("change_notify_add_request: Adding request for %s: "
     342             :                    "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
     343             : 
     344        2672 :         if (!(request = talloc(NULL, struct notify_change_request))
     345        2672 :             || !(map = talloc(request, struct notify_mid_map))) {
     346           0 :                 TALLOC_FREE(request);
     347           0 :                 return NT_STATUS_NO_MEMORY;
     348             :         }
     349             : 
     350        2672 :         request->mid_map = map;
     351        2672 :         map->req = request;
     352             : 
     353        2672 :         request->req = talloc_move(request, &req);
     354        2672 :         request->max_param = max_param;
     355        2672 :         request->filter = filter;
     356        2672 :         request->fsp = fsp;
     357        2672 :         request->reply_fn = reply_fn;
     358        2672 :         request->backend_data = NULL;
     359             : 
     360        2672 :         DLIST_ADD_END(fsp->notify->requests, request);
     361             : 
     362        2672 :         map->mid = request->req->mid;
     363        2672 :         DLIST_ADD(sconn->notify_mid_maps, map);
     364             : 
     365        2672 :         return NT_STATUS_OK;
     366             : }
     367             : 
     368        2672 : static void change_notify_remove_request(struct smbd_server_connection *sconn,
     369             :                                          struct notify_change_request *remove_req)
     370             : {
     371          16 :         files_struct *fsp;
     372          16 :         struct notify_change_request *req;
     373             : 
     374             :         /*
     375             :          * Paranoia checks, the fsp referenced must must have the request in
     376             :          * its list of pending requests
     377             :          */
     378             : 
     379        2672 :         fsp = remove_req->fsp;
     380        2672 :         SMB_ASSERT(fsp->notify != NULL);
     381             : 
     382        2672 :         for (req = fsp->notify->requests; req; req = req->next) {
     383        2672 :                 if (req == remove_req) {
     384        2656 :                         break;
     385             :                 }
     386             :         }
     387             : 
     388        2672 :         if (req == NULL) {
     389           0 :                 smb_panic("notify_req not found in fsp's requests");
     390             :         }
     391             : 
     392        2672 :         DLIST_REMOVE(fsp->notify->requests, req);
     393        2672 :         DLIST_REMOVE(sconn->notify_mid_maps, req->mid_map);
     394        2672 :         TALLOC_FREE(req);
     395        2672 : }
     396             : 
     397        2500 : static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
     398             : {
     399        2500 :         struct smb_request *smbreq = map->req->req;
     400        2500 :         struct smbd_server_connection *sconn = smbreq->sconn;
     401        2500 :         struct smbd_smb2_request *smb2req = smbreq->smb2req;
     402        2500 :         NTSTATUS notify_status = NT_STATUS_CANCELLED;
     403             : 
     404        2500 :         if (smb2req != NULL) {
     405          16 :                 NTSTATUS sstatus;
     406             : 
     407        1578 :                 if (smb2req->session == NULL) {
     408           0 :                         sstatus = NT_STATUS_USER_SESSION_DELETED;
     409             :                 } else {
     410        1578 :                         sstatus = smb2req->session->status;
     411             :                 }
     412             : 
     413        1578 :                 if (NT_STATUS_EQUAL(sstatus, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
     414          10 :                         sstatus = NT_STATUS_OK;
     415             :                 }
     416             : 
     417        1578 :                 if (!NT_STATUS_IS_OK(sstatus)) {
     418           6 :                         notify_status = NT_STATUS_NOTIFY_CLEANUP;
     419        1572 :                 } else if (smb2req->tcon == NULL) {
     420           0 :                         notify_status = NT_STATUS_NOTIFY_CLEANUP;
     421        1572 :                 } else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
     422           2 :                         notify_status = NT_STATUS_NOTIFY_CLEANUP;
     423             :                 }
     424             :         }
     425             : 
     426        2500 :         change_notify_reply(smbreq, notify_status,
     427        2484 :                             0, NULL, map->req->reply_fn);
     428        2500 :         change_notify_remove_request(sconn, map->req);
     429        2500 : }
     430             : 
     431             : /****************************************************************************
     432             :  Delete entries by mid from the change notify pending queue. Always send reply.
     433             : *****************************************************************************/
     434             : 
     435         989 : bool remove_pending_change_notify_requests_by_mid(
     436             :         struct smbd_server_connection *sconn, uint64_t mid)
     437             : {
     438           0 :         struct notify_mid_map *map;
     439             : 
     440         993 :         for (map = sconn->notify_mid_maps; map; map = map->next) {
     441         926 :                 if (map->mid == mid) {
     442         922 :                         break;
     443             :                 }
     444             :         }
     445             : 
     446         989 :         if (map == NULL) {
     447          67 :                 return false;
     448             :         }
     449             : 
     450         922 :         smbd_notify_cancel_by_map(map);
     451         922 :         return true;
     452             : }
     453             : 
     454        1578 : void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
     455             : {
     456        1578 :         struct smbd_server_connection *sconn = smbreq->sconn;
     457          16 :         struct notify_mid_map *map;
     458             : 
     459        1578 :         for (map = sconn->notify_mid_maps; map; map = map->next) {
     460        1578 :                 if (map->req->req == smbreq) {
     461        1562 :                         break;
     462             :                 }
     463             :         }
     464             : 
     465        1578 :         if (map == NULL) {
     466           0 :                 return;
     467             :         }
     468             : 
     469        1578 :         smbd_notify_cancel_by_map(map);
     470             : }
     471             : 
     472      212155 : static struct files_struct *smbd_notify_cancel_deleted_fn(
     473             :         struct files_struct *fsp, void *private_data)
     474             : {
     475      212155 :         struct file_id *fid = talloc_get_type_abort(
     476             :                 private_data, struct file_id);
     477             : 
     478      212155 :         if (file_id_equal(&fsp->file_id, fid)) {
     479        4023 :                 remove_pending_change_notify_requests_by_fid(
     480        4023 :                         fsp, NT_STATUS_DELETE_PENDING);
     481             :         }
     482      212155 :         return NULL;
     483             : }
     484             : 
     485      150449 : void smbd_notify_cancel_deleted(struct messaging_context *msg,
     486             :                                 void *private_data, uint32_t msg_type,
     487             :                                 struct server_id server_id, DATA_BLOB *data)
     488             : {
     489      150449 :         struct smbd_server_connection *sconn = talloc_get_type_abort(
     490             :                 private_data, struct smbd_server_connection);
     491         355 :         struct file_id *fid;
     492         355 :         enum ndr_err_code ndr_err;
     493             : 
     494      150449 :         fid = talloc(talloc_tos(), struct file_id);
     495      150449 :         if (fid == NULL) {
     496           0 :                 DEBUG(1, ("talloc failed\n"));
     497           0 :                 return;
     498             :         }
     499             : 
     500      150449 :         ndr_err = ndr_pull_struct_blob_all(
     501             :                 data, fid, fid, (ndr_pull_flags_fn_t)ndr_pull_file_id);
     502      150449 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     503           0 :                 DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__,
     504             :                            ndr_errstr(ndr_err)));
     505           0 :                 goto done;
     506             :         }
     507             : 
     508      150449 :         files_forall(sconn, smbd_notify_cancel_deleted_fn, fid);
     509             : 
     510      150449 : done:
     511      150449 :         TALLOC_FREE(fid);
     512             : }
     513             : 
     514           0 : static struct files_struct *smbd_notifyd_reregister(struct files_struct *fsp,
     515             :                                                     void *private_data)
     516             : {
     517           0 :         DBG_DEBUG("reregister %s\n", fsp->fsp_name->base_name);
     518             : 
     519           0 :         if ((fsp->conn->sconn->notify_ctx != NULL) &&
     520           0 :             (fsp->notify != NULL) &&
     521           0 :             ((fsp->notify->filter != 0) ||
     522           0 :              (fsp->notify->subdir_filter != 0))) {
     523           0 :                 size_t len = fsp_fullbasepath(fsp, NULL, 0);
     524           0 :                 char fullpath[len+1];
     525             : 
     526           0 :                 NTSTATUS status;
     527             : 
     528           0 :                 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
     529             : 
     530           0 :                 status = notify_add(fsp->conn->sconn->notify_ctx,
     531           0 :                                     fullpath, fsp->notify->filter,
     532           0 :                                     fsp->notify->subdir_filter, fsp);
     533           0 :                 if (!NT_STATUS_IS_OK(status)) {
     534           0 :                         DBG_DEBUG("notify_add failed: %s\n",
     535             :                                   nt_errstr(status));
     536             :                 }
     537             :         }
     538           0 :         return NULL;
     539             : }
     540             : 
     541           0 : void smbd_notifyd_restarted(struct messaging_context *msg,
     542             :                             void *private_data, uint32_t msg_type,
     543             :                             struct server_id server_id, DATA_BLOB *data)
     544             : {
     545           0 :         struct smbd_server_connection *sconn = talloc_get_type_abort(
     546             :                 private_data, struct smbd_server_connection);
     547             : 
     548           0 :         TALLOC_FREE(sconn->notify_ctx);
     549             : 
     550           0 :         sconn->notify_ctx = notify_init(sconn, sconn->msg_ctx,
     551             :                                         sconn, notify_callback);
     552           0 :         if (sconn->notify_ctx == NULL) {
     553           0 :                 DBG_DEBUG("notify_init failed\n");
     554           0 :                 return;
     555             :         }
     556             : 
     557           0 :         files_forall(sconn, smbd_notifyd_reregister, sconn->notify_ctx);
     558             : }
     559             : 
     560             : /****************************************************************************
     561             :  Delete entries by fnum from the change notify pending queue.
     562             : *****************************************************************************/
     563             : 
     564       88489 : void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
     565             :                                                   NTSTATUS status)
     566             : {
     567       88489 :         if (fsp->notify == NULL) {
     568       86236 :                 return;
     569             :         }
     570             : 
     571        1916 :         while (fsp->notify->requests != NULL) {
     572          38 :                 change_notify_reply(fsp->notify->requests->req,
     573             :                                     status, 0, NULL,
     574          38 :                                     fsp->notify->requests->reply_fn);
     575          38 :                 change_notify_remove_request(fsp->conn->sconn,
     576          38 :                                              fsp->notify->requests);
     577             :         }
     578             : }
     579             : 
     580      359797 : void notify_fname(connection_struct *conn, uint32_t action, uint32_t filter,
     581             :                   const char *path)
     582             : {
     583      359797 :         struct notify_context *notify_ctx = conn->sconn->notify_ctx;
     584             : 
     585      359797 :         if (path[0] == '.' && path[1] == '/') {
     586           0 :                 path += 2;
     587             :         }
     588             : 
     589      359797 :         notify_trigger(notify_ctx, action, filter, conn->connectpath, path);
     590      359797 : }
     591             : 
     592          12 : static bool user_can_stat_name_under_fsp(files_struct *fsp, const char *name)
     593             : {
     594           0 :         uint32_t rights;
     595          12 :         struct smb_filename *fname = NULL;
     596          12 :         char *filepath = NULL;
     597           0 :         NTSTATUS status;
     598          12 :         char *p = NULL;
     599             : 
     600             :         /*
     601             :          * Assume we get filepath (relative to the share)
     602             :          * like this:
     603             :          *
     604             :          *  'dir1/dir2/dir3/file'
     605             :          *
     606             :          * We start with LIST and TRAVERSE on the
     607             :          * direct parent ('dir1/dir2/dir3')
     608             :          *
     609             :          * Then we switch to just TRAVERSE for
     610             :          * the rest: 'dir1/dir2', 'dir1', '.'
     611             :          *
     612             :          * For a file in the share root, we'll have
     613             :          *  'file'
     614             :          * and would just check '.' with LIST and TRAVERSE.
     615             :          *
     616             :          * It's important to always check '.' as the last step,
     617             :          * which means we check the permissions of the share root
     618             :          * directory.
     619             :          */
     620             : 
     621          12 :         if (ISDOT(fsp->fsp_name->base_name)) {
     622           6 :                 filepath = talloc_strdup(talloc_tos(), name);
     623             :         } else {
     624           6 :                 filepath = talloc_asprintf(talloc_tos(),
     625             :                         "%s/%s",
     626           6 :                         fsp->fsp_name->base_name,
     627             :                         name);
     628             :         }
     629          12 :         if (filepath == NULL) {
     630           0 :                 DBG_ERR("Memory allocation failed\n");
     631           0 :                 return false;
     632             :         }
     633             : 
     634          12 :         rights = SEC_DIR_LIST|SEC_DIR_TRAVERSE;
     635          12 :         p = strrchr_m(filepath, '/');
     636             :         /*
     637             :          * Check each path component, excluding the share root.
     638             :          *
     639             :          * We could check all components including root using
     640             :          * a do { .. } while() loop, but IMHO the logic is clearer
     641             :          * having the share root check separately afterwards.
     642             :          */
     643          16 :         while (p != NULL) {
     644          12 :                 *p = '\0';
     645          12 :                 status = synthetic_pathref(talloc_tos(),
     646          12 :                                            fsp->conn->cwd_fsp,
     647             :                                            filepath,
     648             :                                            NULL,
     649             :                                            NULL,
     650             :                                            0,
     651             :                                            0,
     652             :                                            &fname);
     653          12 :                 if (!NT_STATUS_IS_OK(status)) {
     654           0 :                         DBG_ERR("synthetic_pathref failed for %s, error %s\n",
     655             :                                 filepath,
     656             :                                 nt_errstr(status));
     657           0 :                         TALLOC_FREE(fname);
     658           0 :                         TALLOC_FREE(filepath);
     659           0 :                         return false;
     660             :                 }
     661             : 
     662          12 :                 status = smbd_check_access_rights_fsp(fsp->conn->cwd_fsp,
     663          12 :                                                   fname->fsp,
     664             :                                                   false,
     665             :                                                   rights);
     666          12 :                 if (!NT_STATUS_IS_OK(status)) {
     667           8 :                         DBG_DEBUG("Access rights for %s/%s: %s\n",
     668             :                                   fsp->conn->connectpath,
     669             :                                   filepath,
     670             :                                   nt_errstr(status));
     671           8 :                         TALLOC_FREE(fname);
     672           8 :                         TALLOC_FREE(filepath);
     673           8 :                         return false;
     674             :                 }
     675             : 
     676           4 :                 TALLOC_FREE(fname);
     677           4 :                 rights = SEC_DIR_TRAVERSE;
     678           4 :                 p = strrchr_m(filepath, '/');
     679             :         }
     680             : 
     681           4 :         TALLOC_FREE(filepath);
     682             : 
     683             :         /* Finally check share root. */
     684           4 :         filepath = talloc_strdup(talloc_tos(), ".");
     685           4 :         if (filepath == NULL) {
     686           0 :                 DBG_ERR("Memory allocation failed\n");
     687           0 :                 return false;
     688             :         }
     689           4 :         status = synthetic_pathref(talloc_tos(),
     690           4 :                                    fsp->conn->cwd_fsp,
     691             :                                    filepath,
     692             :                                    NULL,
     693             :                                    NULL,
     694             :                                    0,
     695             :                                    0,
     696             :                                    &fname);
     697           4 :         if (!NT_STATUS_IS_OK(status)) {
     698           0 :                 DBG_ERR("synthetic_pathref failed for %s, error %s\n",
     699             :                         filepath,
     700             :                         nt_errstr(status));
     701           0 :                 TALLOC_FREE(fname);
     702           0 :                 TALLOC_FREE(filepath);
     703           0 :                 return false;
     704             :         }
     705           4 :         status = smbd_check_access_rights_fsp(fsp->conn->cwd_fsp,
     706           4 :                                           fname->fsp,
     707             :                                           false,
     708             :                                           rights);
     709           4 :         if (!NT_STATUS_IS_OK(status)) {
     710           0 :                 DBG_DEBUG("TRAVERSE access rights for %s failed with %s\n",
     711             :                           fsp->conn->connectpath,
     712             :                           nt_errstr(status));
     713           0 :                 TALLOC_FREE(fname);
     714           0 :                 TALLOC_FREE(filepath);
     715           0 :                 return false;
     716             :         }
     717           4 :         TALLOC_FREE(fname);
     718           4 :         TALLOC_FREE(filepath);
     719           4 :         return true;
     720             : }
     721             : 
     722        1969 : static void notify_fsp(files_struct *fsp, struct timespec when,
     723             :                        uint32_t action, const char *name)
     724             : {
     725          14 :         struct notify_change_event *change, *changes;
     726          14 :         char *tmp;
     727             : 
     728        1969 :         if (fsp->notify == NULL) {
     729             :                 /*
     730             :                  * Nobody is waiting, don't queue
     731             :                  */
     732        1835 :                 return;
     733             :         }
     734             : 
     735        1969 :         if (lp_honor_change_notify_privilege(SNUM(fsp->conn))) {
     736           0 :                 bool has_sec_change_notify_privilege;
     737          38 :                 bool expose = false;
     738             : 
     739          38 :                 has_sec_change_notify_privilege = security_token_has_privilege(
     740          38 :                         fsp->conn->session_info->security_token,
     741             :                         SEC_PRIV_CHANGE_NOTIFY);
     742             : 
     743          38 :                 if (has_sec_change_notify_privilege) {
     744          26 :                         expose = true;
     745             :                 } else {
     746           0 :                         bool ok;
     747             : 
     748          12 :                         ok = become_user_without_service_by_fsp(fsp);
     749          12 :                         if (ok) {
     750          12 :                                 expose = user_can_stat_name_under_fsp(fsp, name);
     751          12 :                                 unbecome_user_without_service();
     752             :                         }
     753             :                 }
     754          38 :                 DBG_DEBUG("has_sec_change_notify_privilege=%s "
     755             :                           "expose=%s for %s notify %s\n",
     756             :                           has_sec_change_notify_privilege ? "true" : "false",
     757             :                           expose ? "true" : "false",
     758             :                           fsp->fsp_name->base_name, name);
     759          38 :                 if (!expose) {
     760           8 :                         return;
     761             :                 }
     762             :         }
     763             : 
     764             :         /*
     765             :          * Someone has triggered a notify previously, queue the change for
     766             :          * later.
     767             :          */
     768             : 
     769        1961 :         if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
     770             :                 /*
     771             :                  * The real number depends on the client buf, just provide a
     772             :                  * guard against a DoS here.  If name == NULL the CN backend is
     773             :                  * alerting us to a problem.  Possibly dropped events.  Clear
     774             :                  * queued changes and send the catch-all response to the client
     775             :                  * if a request is pending.
     776             :                  */
     777           0 :                 TALLOC_FREE(fsp->notify->changes);
     778           0 :                 fsp->notify->num_changes = -1;
     779           0 :                 if (fsp->notify->requests != NULL) {
     780           0 :                         change_notify_reply(fsp->notify->requests->req,
     781           0 :                                             NT_STATUS_OK,
     782           0 :                                             fsp->notify->requests->max_param,
     783             :                                             fsp->notify,
     784           0 :                                             fsp->notify->requests->reply_fn);
     785           0 :                         change_notify_remove_request(fsp->conn->sconn,
     786           0 :                                                      fsp->notify->requests);
     787             :                 }
     788           0 :                 return;
     789             :         }
     790             : 
     791             :         /* If we've exceeded the server side queue or received a NULL name
     792             :          * from the underlying CN implementation, don't queue up any more
     793             :          * requests until we can send a catch-all response to the client */
     794        1961 :         if (fsp->notify->num_changes == -1) {
     795           0 :                 return;
     796             :         }
     797             : 
     798        1961 :         if (!(changes = talloc_realloc(
     799             :                       fsp->notify, fsp->notify->changes,
     800             :                       struct notify_change_event,
     801             :                       fsp->notify->num_changes+1))) {
     802           0 :                 DEBUG(0, ("talloc_realloc failed\n"));
     803           0 :                 return;
     804             :         }
     805             : 
     806        1961 :         fsp->notify->changes = changes;
     807             : 
     808        1961 :         change = &(fsp->notify->changes[fsp->notify->num_changes]);
     809             : 
     810        1961 :         if (!(tmp = talloc_strdup(changes, name))) {
     811           0 :                 DEBUG(0, ("talloc_strdup failed\n"));
     812           0 :                 return;
     813             :         }
     814             : 
     815        1961 :         string_replace(tmp, '/', '\\');
     816        1961 :         change->name = tmp;
     817             : 
     818        1961 :         change->when = when;
     819        1961 :         change->action = action;
     820        1961 :         fsp->notify->num_changes += 1;
     821             : 
     822        1961 :         if (fsp->notify->requests == NULL) {
     823             :                 /*
     824             :                  * Nobody is waiting, so don't send anything. The ot
     825             :                  */
     826        1807 :                 return;
     827             :         }
     828             : 
     829         140 :         if (action == NOTIFY_ACTION_OLD_NAME) {
     830             :                 /*
     831             :                  * We have to send the two rename events in one reply. So hold
     832             :                  * the first part back.
     833             :                  */
     834           6 :                 return;
     835             :         }
     836             : 
     837             :         /*
     838             :          * Someone is waiting for the change, trigger the reply immediately.
     839             :          *
     840             :          * TODO: do we have to walk the lists of requests pending?
     841             :          */
     842             : 
     843         134 :         change_notify_reply(fsp->notify->requests->req,
     844         134 :                             NT_STATUS_OK,
     845         134 :                             fsp->notify->requests->max_param,
     846             :                             fsp->notify,
     847         134 :                             fsp->notify->requests->reply_fn);
     848             : 
     849         134 :         change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
     850             : }
     851             : 
     852        1050 : char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32_t filter)
     853             : {
     854        1050 :         char *result = NULL;
     855             : 
     856        1050 :         result = talloc_strdup(mem_ctx, "");
     857        1050 :         if (result == NULL) {
     858           0 :                 return NULL;
     859             :         }
     860             : 
     861        1050 :         if (filter & FILE_NOTIFY_CHANGE_FILE_NAME) {
     862         168 :                 result = talloc_asprintf_append(result, "FILE_NAME|");
     863         168 :                 if (result == NULL) {
     864           0 :                         return NULL;
     865             :                 }
     866             :         }
     867        1050 :         if (filter & FILE_NOTIFY_CHANGE_DIR_NAME) {
     868         160 :                 result = talloc_asprintf_append(result, "DIR_NAME|");
     869         160 :                 if (result == NULL) {
     870           0 :                         return NULL;
     871             :                 }
     872             :         }
     873        1050 :         if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES) {
     874          44 :                 result = talloc_asprintf_append(result, "ATTRIBUTES|");
     875          44 :                 if (result == NULL) {
     876           0 :                         return NULL;
     877             :                 }
     878             :         }
     879        1050 :         if (filter & FILE_NOTIFY_CHANGE_SIZE) {
     880          28 :                 result = talloc_asprintf_append(result, "SIZE|");
     881          28 :                 if (result == NULL) {
     882           0 :                         return NULL;
     883             :                 }
     884             :         }
     885        1050 :         if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE) {
     886          28 :                 result = talloc_asprintf_append(result, "LAST_WRITE|");
     887          28 :                 if (result == NULL) {
     888           0 :                         return NULL;
     889             :                 }
     890             :         }
     891        1050 :         if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS) {
     892          28 :                 result = talloc_asprintf_append(result, "LAST_ACCESS|");
     893          28 :                 if (result == NULL) {
     894           0 :                         return NULL;
     895             :                 }
     896             :         }
     897        1050 :         if (filter & FILE_NOTIFY_CHANGE_CREATION) {
     898          36 :                 result = talloc_asprintf_append(result, "CREATION|");
     899          36 :                 if (result == NULL) {
     900           0 :                         return NULL;
     901             :                 }
     902             :         }
     903        1050 :         if (filter & FILE_NOTIFY_CHANGE_EA) {
     904          28 :                 result = talloc_asprintf_append(result, "EA|");
     905          28 :                 if (result == NULL) {
     906           0 :                         return NULL;
     907             :                 }
     908             :         }
     909        1050 :         if (filter & FILE_NOTIFY_CHANGE_SECURITY) {
     910          28 :                 result = talloc_asprintf_append(result, "SECURITY|");
     911          28 :                 if (result == NULL) {
     912           0 :                         return NULL;
     913             :                 }
     914             :         }
     915        1050 :         if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME) {
     916          30 :                 result = talloc_asprintf_append(result, "STREAM_NAME|");
     917          30 :                 if (result == NULL) {
     918           0 :                         return NULL;
     919             :                 }
     920             :         }
     921        1050 :         if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE) {
     922          28 :                 result = talloc_asprintf_append(result, "STREAM_SIZE|");
     923          28 :                 if (result == NULL) {
     924           0 :                         return NULL;
     925             :                 }
     926             :         }
     927        1050 :         if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE) {
     928          28 :                 result = talloc_asprintf_append(result, "STREAM_WRITE|");
     929          28 :                 if (result == NULL) {
     930           0 :                         return NULL;
     931             :                 }
     932             :         }
     933             : 
     934        1050 :         if (*result == '\0') return result;
     935             : 
     936         486 :         result[strlen(result)-1] = '\0';
     937         486 :         return result;
     938             : }
     939             : 
     940           0 : struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
     941             :                                                      struct tevent_context *ev)
     942             : {
     943           0 :         struct sys_notify_context *ctx;
     944             : 
     945           0 :         if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
     946           0 :                 DEBUG(0, ("talloc failed\n"));
     947           0 :                 return NULL;
     948             :         }
     949             : 
     950           0 :         ctx->ev = ev;
     951           0 :         ctx->private_data = NULL;
     952           0 :         return ctx;
     953             : }

Generated by: LCOV version 1.14