LCOV - code coverage report
Current view: top level - source3/smbd/notifyd - notifyd_db.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 49 79 62.0 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This program is free software; you can redistribute it and/or modify
       3             :  * it under the terms of the GNU General Public License as published by
       4             :  * the Free Software Foundation; either version 3 of the License, or
       5             :  * (at your option) any later version.
       6             :  *
       7             :  * This program is distributed in the hope that it will be useful,
       8             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :  * GNU General Public License for more details.
      11             :  *
      12             :  * You should have received a copy of the GNU General Public License
      13             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             :  */
      15             : 
      16             : #include "replace.h"
      17             : #include "lib/util/debug.h"
      18             : #include "lib/util/server_id_db.h"
      19             : #include "notifyd_private.h"
      20             : #include "notifyd_db.h"
      21             : 
      22             : struct notifyd_parse_db_state {
      23             :         bool (*fn)(const char *path,
      24             :                    struct server_id server,
      25             :                    const struct notify_instance *instance,
      26             :                    void *private_data);
      27             :         void *private_data;
      28             : };
      29             : 
      30           2 : static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
      31             :                                     void *private_data)
      32           2 : {
      33           2 :         struct notifyd_parse_db_state *state = private_data;
      34           2 :         char path[key.dsize+1];
      35           2 :         struct notifyd_instance *instances = NULL;
      36           2 :         size_t num_instances = 0;
      37           0 :         size_t i;
      38           0 :         bool ok;
      39             : 
      40           2 :         memcpy(path, key.dptr, key.dsize);
      41           2 :         path[key.dsize] = 0;
      42             : 
      43           2 :         ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
      44             :                                  &num_instances);
      45           2 :         if (!ok) {
      46           0 :                 DBG_DEBUG("Could not parse entry for path %s\n", path);
      47           0 :                 return true;
      48             :         }
      49             : 
      50           4 :         for (i=0; i<num_instances; i++) {
      51           2 :                 ok = state->fn(path, instances[i].client,
      52           2 :                                &instances[i].instance,
      53             :                                state->private_data);
      54           2 :                 if (!ok) {
      55           0 :                         return false;
      56             :                 }
      57             :         }
      58             : 
      59           2 :         return true;
      60             : }
      61             : 
      62           6 : static NTSTATUS notifyd_parse_db(
      63             :         const uint8_t *buf,
      64             :         size_t buflen,
      65             :         uint64_t *log_index,
      66             :         bool (*fn)(const char *path,
      67             :                    struct server_id server,
      68             :                    const struct notify_instance *instance,
      69             :                    void *private_data),
      70             :         void *private_data)
      71             : {
      72           6 :         struct notifyd_parse_db_state state = {
      73             :                 .fn = fn, .private_data = private_data
      74             :         };
      75           0 :         NTSTATUS status;
      76             : 
      77           6 :         if (buflen < 8) {
      78           0 :                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
      79             :         }
      80           6 :         *log_index = BVAL(buf, 0);
      81             : 
      82           6 :         buf += 8;
      83           6 :         buflen -= 8;
      84             : 
      85           6 :         status = dbwrap_parse_marshall_buf(
      86             :                 buf, buflen, notifyd_parse_db_parser, &state);
      87           6 :         return status;
      88             : }
      89             : 
      90           6 : NTSTATUS notify_walk(struct messaging_context *msg_ctx,
      91             :                      bool (*fn)(const char *path, struct server_id server,
      92             :                                 const struct notify_instance *instance,
      93             :                                 void *private_data),
      94             :                      void *private_data)
      95             : {
      96           6 :         struct server_id_db *names_db = NULL;
      97           6 :         struct server_id notifyd = { .pid = 0, };
      98           6 :         struct tevent_context *ev = NULL;
      99           6 :         struct tevent_req *req = NULL;
     100           6 :         struct messaging_rec *rec = NULL;
     101           0 :         uint64_t log_idx;
     102           0 :         NTSTATUS status;
     103           0 :         int ret;
     104           0 :         bool ok;
     105             : 
     106           6 :         names_db = messaging_names_db(msg_ctx);
     107           6 :         ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
     108           6 :         if (!ok) {
     109           0 :                 DBG_WARNING("No notify daemon around\n");
     110           0 :                 return NT_STATUS_SERVER_UNAVAILABLE;
     111             :         }
     112             : 
     113           6 :         ev = samba_tevent_context_init(msg_ctx);
     114           6 :         if (ev == NULL) {
     115           0 :                 return NT_STATUS_NO_MEMORY;
     116             :         }
     117             : 
     118           6 :         req = messaging_read_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY_DB);
     119           6 :         if (req == NULL) {
     120           0 :                 TALLOC_FREE(ev);
     121           0 :                 return NT_STATUS_NO_MEMORY;
     122             :         }
     123             : 
     124           6 :         ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0));
     125           6 :         if (!ok) {
     126           0 :                 TALLOC_FREE(ev);
     127           0 :                 return NT_STATUS_NO_MEMORY;
     128             :         }
     129             : 
     130           6 :         status = messaging_send_buf(
     131             :                 msg_ctx, notifyd, MSG_SMB_NOTIFY_GET_DB, NULL, 0);
     132           6 :         if (!NT_STATUS_IS_OK(status)) {
     133           0 :                 DBG_DEBUG("messaging_send_buf failed: %s\n",
     134             :                           nt_errstr(status));
     135           0 :                 TALLOC_FREE(ev);
     136           0 :                 return status;
     137             :         }
     138             : 
     139           6 :         ok = tevent_req_poll(req, ev);
     140           6 :         if (!ok) {
     141           0 :                 DBG_DEBUG("tevent_req_poll failed\n");
     142           0 :                 TALLOC_FREE(ev);
     143           0 :                 return NT_STATUS_INTERNAL_ERROR;
     144             :         }
     145             : 
     146           6 :         ret = messaging_read_recv(req, ev, &rec);
     147           6 :         if (ret != 0) {
     148           0 :                 DBG_DEBUG("messaging_read_recv failed: %s\n",
     149             :                           strerror(ret));
     150           0 :                 TALLOC_FREE(ev);
     151           0 :                 return map_nt_error_from_unix(ret);
     152             :         }
     153             : 
     154           6 :         status = notifyd_parse_db(
     155           6 :                 rec->buf.data, rec->buf.length, &log_idx, fn, private_data);
     156           6 :         if (!NT_STATUS_IS_OK(status)) {
     157           0 :                 DBG_DEBUG("notifyd_parse_db failed: %s\n",
     158             :                           nt_errstr(status));
     159           0 :                 TALLOC_FREE(ev);
     160           0 :                 return status;
     161             :         }
     162             : 
     163           6 :         TALLOC_FREE(ev);
     164           6 :         return NT_STATUS_OK;
     165             : }

Generated by: LCOV version 1.14