tdb_wrap: Remove tdb_wrap_open_ again
[kai/samba-autobuild/.git] / source4 / lib / messaging / messaging.c
index 1b0fc1291c6e69e051532f43ed232a50c5b21ae4..ba1c5bd727cc606d10aeb99e96d2d3e24e2fa978 100644 (file)
@@ -1,20 +1,20 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
 
    Samba internal messaging functions
 
    Copyright (C) Andrew Tridgell 2004
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "lib/socket/socket.h"
 #include "librpc/gen_ndr/ndr_irpc.h"
 #include "lib/messaging/irpc.h"
-#include "tdb_wrap.h"
+#include "lib/tdb_wrap/tdb_wrap.h"
 #include "../lib/util/unix_privs.h"
 #include "librpc/rpc/dcerpc.h"
-#include <tdb.h>
+#include "../lib/tdb_compat/tdb_compat.h"
 #include "../lib/util/util_tdb.h"
 #include "cluster/cluster.h"
 #include "../lib/util/tevent_ntstatus.h"
+#include "lib/param/param.h"
 
 /* change the message version with any incompatible changes in the protocol */
 #define IMESSAGING_VERSION 1
@@ -55,6 +56,7 @@ struct imessaging_context {
        struct socket_context *sock;
        const char *base_path;
        const char *path;
+       struct loadparm_context *lp_ctx;
        struct dispatch_fn **dispatch;
        uint32_t num_types;
        struct idr_context *dispatch_tree;
@@ -109,7 +111,7 @@ static void irpc_handler(struct imessaging_context *, void *,
 static void ping_message(struct imessaging_context *msg, void *private_data,
                         uint32_t msg_type, struct server_id src, DATA_BLOB *data)
 {
-       char *task_id = cluster_id_string(NULL, src);
+       char *task_id = server_id_str(NULL, &src);
        DEBUG(1,("INFO: Received PING message from server %s [%.*s]\n",
                 task_id, (int)data->length,
                 data->data?(const char *)data->data:""));
@@ -120,7 +122,7 @@ static void ping_message(struct imessaging_context *msg, void *private_data,
 /*
   return uptime of messaging server via irpc
 */
-static NTSTATUS irpc_uptime(struct irpc_message *msg, 
+static NTSTATUS irpc_uptime(struct irpc_message *msg,
                            struct irpc_uptime *r)
 {
        struct imessaging_context *ctx = talloc_get_type(msg->private_data, struct imessaging_context);
@@ -128,13 +130,13 @@ static NTSTATUS irpc_uptime(struct irpc_message *msg,
        return NT_STATUS_OK;
 }
 
-/* 
+/*
    return the path to a messaging socket
 */
 static char *imessaging_path(struct imessaging_context *msg, struct server_id server_id)
 {
        TALLOC_CTX *tmp_ctx = talloc_new(msg);
-       const char *id = cluster_id_string(tmp_ctx, server_id);
+       const char *id = server_id_str(tmp_ctx, &server_id);
        char *s;
        if (id == NULL) {
                return NULL;
@@ -157,7 +159,7 @@ static void imessaging_dispatch(struct imessaging_context *msg, struct imessagin
 
        /* temporary IDs use an idtree, the rest use a array of pointers */
        if (rec->header->msg_type >= MSG_TMP_BASE) {
-               d = (struct dispatch_fn *)idr_find(msg->dispatch_tree, 
+               d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
                                                   rec->header->msg_type);
        } else if (rec->header->msg_type < msg->num_types) {
                d = msg->dispatch[rec->header->msg_type];
@@ -194,7 +196,7 @@ static void cluster_message_handler(struct imessaging_context *msg, DATA_BLOB pa
        rec->retries       = 0;
 
        if (packet.length != sizeof(*rec->header) + rec->header->length) {
-               DEBUG(0,("messaging: bad message header size %d should be %d\n", 
+               DEBUG(0,("messaging: bad message header size %d should be %d\n",
                         rec->header->length, (int)(packet.length - sizeof(*rec->header))));
                talloc_free(rec);
                return;
@@ -219,7 +221,7 @@ static NTSTATUS try_send(struct imessaging_rec *rec)
 
        /* rec->path is the path of the *other* socket, where we want
         * this to end up */
-       path = socket_address_from_strings(msg, msg->sock->backend_name, 
+       path = socket_address_from_strings(msg, msg->sock->backend_name,
                                           rec->path, 0);
        if (!path) {
                return NT_STATUS_NO_MEMORY;
@@ -237,7 +239,7 @@ static NTSTATUS try_send(struct imessaging_rec *rec)
 /*
   retry backed off messages
 */
-static void msg_retry_timer(struct tevent_context *ev, struct tevent_timer *te, 
+static void msg_retry_timer(struct tevent_context *ev, struct tevent_timer *te,
                            struct timeval t, void *private_data)
 {
        struct imessaging_context *msg = talloc_get_type(private_data,
@@ -251,7 +253,7 @@ static void msg_retry_timer(struct tevent_context *ev, struct tevent_timer *te,
                DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
        }
 
-       EVENT_FD_WRITEABLE(msg->event.fde);     
+       TEVENT_FD_WRITEABLE(msg->event.fde);
 }
 
 /*
@@ -269,12 +271,12 @@ static void imessaging_send_handler(struct imessaging_context *msg)
                                /* we're getting continuous write errors -
                                   backoff this record */
                                DLIST_REMOVE(msg->pending, rec);
-                               DLIST_ADD_END(msg->retry_queue, rec, 
+                               DLIST_ADD_END(msg->retry_queue, rec,
                                              struct imessaging_rec *);
                                if (msg->retry_te == NULL) {
-                                       msg->retry_te = 
-                                               event_add_timed(msg->event.ev, msg, 
-                                                               timeval_current_ofs(1, 0), 
+                                       msg->retry_te =
+                                               tevent_add_timer(msg->event.ev, msg,
+                                                               timeval_current_ofs(1, 0),
                                                                msg_retry_timer, msg);
                                }
                        }
@@ -283,10 +285,10 @@ static void imessaging_send_handler(struct imessaging_context *msg)
                rec->retries = 0;
                if (!NT_STATUS_IS_OK(status)) {
                        TALLOC_CTX *tmp_ctx = talloc_new(msg);
-                       DEBUG(1,("messaging: Lost message from %s to %s of type %u - %s\n", 
-                                cluster_id_string(tmp_ctx, rec->header->from),
-                                cluster_id_string(tmp_ctx, rec->header->to),
-                                rec->header->msg_type, 
+                       DEBUG(1,("messaging: Lost message from %s to %s of type %u - %s\n",
+                                server_id_str(tmp_ctx, &rec->header->from),
+                                server_id_str(tmp_ctx, &rec->header->to),
+                                rec->header->msg_type,
                                 nt_errstr(status)));
                        talloc_free(tmp_ctx);
                }
@@ -294,7 +296,7 @@ static void imessaging_send_handler(struct imessaging_context *msg)
                talloc_free(rec);
        }
        if (msg->pending == NULL) {
-               EVENT_FD_NOT_WRITEABLE(msg->event.fde);
+               TEVENT_FD_NOT_WRITEABLE(msg->event.fde);
        }
 }
 
@@ -311,17 +313,17 @@ static void imessaging_recv_handler(struct imessaging_context *msg)
        /* see how many bytes are in the next packet */
        status = socket_pending(msg->sock, &msize);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("socket_pending failed in messaging - %s\n", 
+               DEBUG(0,("socket_pending failed in messaging - %s\n",
                         nt_errstr(status)));
                return;
        }
-       
+
        packet = data_blob_talloc(msg, NULL, msize);
        if (packet.data == NULL) {
                /* assume this is temporary and retry */
                return;
        }
-           
+
        status = socket_recv(msg->sock, packet.data, msize, &msize);
        if (!NT_STATUS_IS_OK(status)) {
                data_blob_free(&packet);
@@ -347,7 +349,7 @@ static void imessaging_recv_handler(struct imessaging_context *msg)
        rec->retries       = 0;
 
        if (msize != sizeof(*rec->header) + rec->header->length) {
-               DEBUG(0,("messaging: bad message header size %d should be %d\n", 
+               DEBUG(0,("messaging: bad message header size %d should be %d\n",
                         rec->header->length, (int)(msize - sizeof(*rec->header))));
                talloc_free(rec);
                return;
@@ -366,10 +368,10 @@ static void imessaging_handler(struct tevent_context *ev, struct tevent_fd *fde,
 {
        struct imessaging_context *msg = talloc_get_type(private_data,
                                                        struct imessaging_context);
-       if (flags & EVENT_FD_WRITE) {
+       if (flags & TEVENT_FD_WRITE) {
                imessaging_send_handler(msg);
        }
-       if (flags & EVENT_FD_READ) {
+       if (flags & TEVENT_FD_READ) {
                imessaging_recv_handler(msg);
        }
 }
@@ -442,7 +444,7 @@ void imessaging_deregister(struct imessaging_context *msg, uint32_t msg_type, vo
        struct dispatch_fn *d, *next;
 
        if (msg_type >= msg->num_types) {
-               d = (struct dispatch_fn *)idr_find(msg->dispatch_tree, 
+               d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
                                                   msg_type);
                if (!d) return;
                idr_remove(msg->dispatch_tree, msg_type);
@@ -491,7 +493,7 @@ NTSTATUS imessaging_send(struct imessaging_context *msg, struct server_id server
        rec->header->to       = server;
        rec->header->length   = dlength;
        if (dlength != 0) {
-               memcpy(rec->packet.data + sizeof(*rec->header), 
+               memcpy(rec->packet.data + sizeof(*rec->header),
                       data->data, dlength);
        }
 
@@ -514,7 +516,7 @@ NTSTATUS imessaging_send(struct imessaging_context *msg, struct server_id server
 
        if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
                if (msg->pending == NULL) {
-                       EVENT_FD_WRITEABLE(msg->event.fde);
+                       TEVENT_FD_WRITEABLE(msg->event.fde);
                }
                DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
                return NT_STATUS_OK;
@@ -541,10 +543,15 @@ NTSTATUS imessaging_send_ptr(struct imessaging_context *msg, struct server_id se
 
 
 /*
-  destroy the messaging context
+  remove our messaging socket and database entry
 */
-static int imessaging_destructor(struct imessaging_context *msg)
+int imessaging_cleanup(struct imessaging_context *msg)
 {
+       if (!msg) {
+               return 0;
+       }
+
+       DEBUG(5,("imessaging: cleaning up %s\n", msg->path));
        unlink(msg->path);
        while (msg->names && msg->names[0]) {
                irpc_remove_name(msg, msg->names[0]);
@@ -554,15 +561,22 @@ static int imessaging_destructor(struct imessaging_context *msg)
 
 /*
   create the listening socket and setup the dispatcher
+
+  use temporary=true when you want a destructor to remove the
+  associated messaging socket and database entry on talloc free. Don't
+  use this in processes that may fork and a child may talloc free this
+  memory
 */
 struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
-                                        const char *dir,
-                                        struct server_id server_id, 
-                                        struct tevent_context *ev)
+                                          struct loadparm_context *lp_ctx,
+                                          struct server_id server_id,
+                                          struct tevent_context *ev,
+                                          bool auto_remove)
 {
        struct imessaging_context *msg;
        NTSTATUS status;
        struct socket_address *path;
+       bool ok;
 
        if (ev == NULL) {
                return NULL;
@@ -581,9 +595,21 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        }
 
        /* create the messaging directory if needed */
-       mkdir(dir, 0700);
 
-       msg->base_path     = talloc_reference(msg, dir);
+       msg->lp_ctx = talloc_reference(msg, lp_ctx);
+       if (!msg->lp_ctx) {
+               talloc_free(msg);
+               return NULL;
+       }
+
+       msg->base_path     = lpcfg_imessaging_path(msg, lp_ctx);
+
+       ok = directory_create_or_exist_strict(msg->base_path, geteuid(), 0700);
+       if (!ok) {
+               talloc_free(msg);
+               return NULL;
+       }
+
        msg->path          = imessaging_path(msg, server_id);
        msg->server_id     = server_id;
        msg->idr           = idr_init(msg);
@@ -596,11 +622,11 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       /* by stealing here we ensure that the socket is cleaned up (and even 
+       /* by stealing here we ensure that the socket is cleaned up (and even
           deleted) on exit */
        talloc_steal(msg, msg->sock);
 
-       path = socket_address_from_strings(msg, msg->sock->backend_name, 
+       path = socket_address_from_strings(msg, msg->sock->backend_name,
                                           msg->path, 0);
        if (!path) {
                talloc_free(msg);
@@ -618,12 +644,14 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        set_blocking(socket_get_fd(msg->sock), false);
 
        msg->event.ev   = ev;
-       msg->event.fde  = event_add_fd(ev, msg, socket_get_fd(msg->sock), 
-                                      EVENT_FD_READ, imessaging_handler, msg);
+       msg->event.fde  = tevent_add_fd(ev, msg, socket_get_fd(msg->sock),
+                                       TEVENT_FD_READ, imessaging_handler, msg);
        tevent_fd_set_auto_close(msg->event.fde);
 
-       talloc_set_destructor(msg, imessaging_destructor);
-       
+       if (auto_remove) {
+               talloc_set_destructor(msg, imessaging_cleanup);
+       }
+
        imessaging_register(msg, NULL, MSG_PING, ping_message);
        imessaging_register(msg, NULL, MSG_IRPC, irpc_handler);
        IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
@@ -631,17 +659,23 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        return msg;
 }
 
-/* 
-   A hack, for the short term until we get 'client only' messaging in place 
+/*
+   A hack, for the short term until we get 'client only' messaging in place
 */
 struct imessaging_context *imessaging_client_init(TALLOC_CTX *mem_ctx,
-                                               const char *dir,
+                                                 struct loadparm_context *lp_ctx,
                                                struct tevent_context *ev)
 {
        struct server_id id;
        ZERO_STRUCT(id);
-       id.pid = random() % 0x10000000;
-       return imessaging_init(mem_ctx, dir, id, ev);
+       id.pid = getpid();
+       id.task_id = generate_random();
+       id.vnn = NONCLUSTER_VNN;
+
+       /* This is because we are not in the s3 serverid database */
+       id.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
+
+       return imessaging_init(mem_ctx, lp_ctx, id, ev, true);
 }
 /*
   a list of registered irpc server functions
@@ -660,7 +694,7 @@ struct irpc_list {
   register a irpc server function
 */
 NTSTATUS irpc_register(struct imessaging_context *msg_ctx,
-                      const struct ndr_interface_table *table, 
+                      const struct ndr_interface_table *table,
                       int callnum, irpc_function_t fn, void *private_data)
 {
        struct irpc_list *irpc;
@@ -865,11 +899,14 @@ static struct tdb_wrap *irpc_namedb_open(struct imessaging_context *msg_ctx)
        if (path == NULL) {
                return NULL;
        }
-       t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
+       t = tdb_wrap_open(msg_ctx, path,
+                         lpcfg_tdb_hash_size(msg_ctx->lp_ctx, path),
+                         lpcfg_tdb_flags(msg_ctx->lp_ctx, 0),
+                         O_RDWR|O_CREAT, 0660);
        talloc_free(path);
        return t;
 }
-       
+
 
 /*
   add a string name that this irpc server can be called on
@@ -948,7 +985,7 @@ struct server_id *irpc_servers_byname(struct imessaging_context *msg_ctx,
        for (i=0;i<count;i++) {
                ret[i] = ((struct server_id *)rec.dptr)[i];
        }
-       ret[i] = cluster_id(0, 0);
+       server_id_set_disconnected(&ret[i]);
        free(rec.dptr);
        tdb_unlock_bystring(t->tdb, name);
        talloc_free(t);
@@ -956,6 +993,77 @@ struct server_id *irpc_servers_byname(struct imessaging_context *msg_ctx,
        return ret;
 }
 
+static int all_servers_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
+{
+       struct irpc_name_records *name_records = talloc_get_type(state, struct irpc_name_records);
+       struct irpc_name_record *name_record;
+       int i;
+
+       name_records->names
+               = talloc_realloc(name_records, name_records->names,
+                                struct irpc_name_record *, name_records->num_records+1);
+       if (!name_records->names) {
+               return -1;
+       }
+
+       name_records->names[name_records->num_records] = name_record
+               = talloc(name_records->names,
+                        struct irpc_name_record);
+       if (!name_record) {
+               return -1;
+       }
+
+       name_records->num_records++;
+
+       name_record->name
+               = talloc_strndup(name_record,
+                                (const char *)key.dptr, key.dsize);
+       if (!name_record->name) {
+               return -1;
+       }
+
+       name_record->count = data.dsize / sizeof(struct server_id);
+       name_record->ids = talloc_array(name_record,
+                                       struct server_id,
+                                       name_record->count);
+       if (name_record->ids == NULL) {
+               return -1;
+       }
+       for (i=0;i<name_record->count;i++) {
+               name_record->ids[i] = ((struct server_id *)data.dptr)[i];
+       }
+       return 0;
+}
+
+/*
+  return a list of server ids for a server name
+*/
+struct irpc_name_records *irpc_all_servers(struct imessaging_context *msg_ctx,
+                                          TALLOC_CTX *mem_ctx)
+{
+       struct tdb_wrap *t;
+       int ret;
+       struct irpc_name_records *name_records = talloc_zero(mem_ctx, struct irpc_name_records);
+       if (name_records == NULL) {
+               return NULL;
+       }
+
+       t = irpc_namedb_open(msg_ctx);
+       if (t == NULL) {
+               return NULL;
+       }
+
+       ret = tdb_traverse_read(t->tdb, all_servers_func, name_records);
+       if (ret == -1) {
+               talloc_free(t);
+               return NULL;
+       }
+
+       talloc_free(t);
+
+       return name_records;
+}
+
 /*
   remove a name from a messaging context
 */
@@ -994,7 +1102,7 @@ void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name)
        for (i=0;i<count;i++) {
                if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
                        if (i < count-1) {
-                               memmove(ids+i, ids+i+1, 
+                               memmove(ids+i, ids+i+1,
                                        sizeof(struct server_id) * (count-(i+1)));
                        }
                        rec.dsize -= sizeof(struct server_id);
@@ -1086,7 +1194,7 @@ static struct tevent_req *irpc_bh_raw_call_send(TALLOC_CTX *mem_ctx,
 
        ok = irpc_bh_is_connected(h);
        if (!ok) {
-               tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
+               tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
                return tevent_req_post(req, ev);
        }
 
@@ -1178,7 +1286,7 @@ static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
                m->ndr->data + m->ndr->offset,
                m->ndr->data_size - m->ndr->offset);
        if ((m->ndr->data_size - m->ndr->offset) > 0 && !state->out_data.data) {
-               tevent_req_nomem(NULL, req);
+               tevent_req_oom(req);
                return;
        }
 
@@ -1230,7 +1338,7 @@ static struct tevent_req *irpc_bh_disconnect_send(TALLOC_CTX *mem_ctx,
 
        ok = irpc_bh_is_connected(h);
        if (!ok) {
-               tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
+               tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
                return tevent_req_post(req, ev);
        }
 
@@ -1314,7 +1422,7 @@ struct dcerpc_binding_handle *irpc_binding_handle_by_name(TALLOC_CTX *mem_ctx,
                errno = EADDRNOTAVAIL;
                return NULL;
        }
-       if (sids[0].pid == 0) {
+       if (server_id_is_disconnected(&sids[0])) {
                talloc_free(sids);
                errno = EADDRNOTAVAIL;
                return NULL;