s4:imessaging: Remove dcerpc_binding_handle_set_sync_ev() call from irpc_binding_handle()
[samba.git] / source4 / lib / messaging / messaging.c
index 51fc8e26edfeca0546295ef06306dba37cde2235..a466cc086850777ecf69a524f593e2d48d6faed1 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/>.
 */
@@ -27,7 +27,7 @@
 #include "lib/socket/socket.h"
 #include "librpc/gen_ndr/ndr_irpc.h"
 #include "lib/messaging/irpc.h"
-#include "lib/util/tdb_wrap.h"
+#include "lib/tdb_wrap/tdb_wrap.h"
 #include "../lib/util/unix_privs.h"
 #include "librpc/rpc/dcerpc.h"
 #include "../lib/tdb_compat/tdb_compat.h"
@@ -39,6 +39,9 @@
 /* change the message version with any incompatible changes in the protocol */
 #define IMESSAGING_VERSION 1
 
+static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
+                                        struct loadparm_context *lp_ctx);
+
 /*
   a pending irpc call
 */
@@ -64,6 +67,7 @@ struct imessaging_context {
        struct irpc_list *irpc;
        struct idr_context *idr;
        const char **names;
+       struct tdb_wrap *names_db;
        struct timeval start_time;
        struct tevent_timer *retry_te;
        struct {
@@ -121,7 +125,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);
@@ -129,7 +133,7 @@ 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)
@@ -158,7 +162,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];
@@ -195,7 +199,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;
@@ -220,7 +224,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;
@@ -238,7 +242,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,
@@ -270,12 +274,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 = 
+                                       msg->retry_te =
                                                tevent_add_timer(msg->event.ev, msg,
-                                                               timeval_current_ofs(1, 0), 
+                                                               timeval_current_ofs(1, 0),
                                                                msg_retry_timer, msg);
                                }
                        }
@@ -284,10 +288,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", 
+                       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, 
+                                rec->header->msg_type,
                                 nt_errstr(status)));
                        talloc_free(tmp_ctx);
                }
@@ -312,17 +316,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);
@@ -348,7 +352,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;
@@ -443,7 +447,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);
@@ -492,7 +496,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);
        }
 
@@ -575,6 +579,7 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        struct imessaging_context *msg;
        NTSTATUS status;
        struct socket_address *path;
+       bool ok;
 
        if (ev == NULL) {
                return NULL;
@@ -588,44 +593,63 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        /* setup a handler for messages from other cluster nodes, if appropriate */
        status = cluster_message_init(msg, server_id, cluster_message_handler);
        if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(msg);
-               return NULL;
+               goto fail;
        }
 
        /* create the messaging directory if needed */
 
        msg->base_path     = lpcfg_imessaging_path(msg, lp_ctx);
+       if (msg->base_path == NULL) {
+               goto fail;
+       }
 
-       mkdir(msg->base_path, 0700);
+       ok = directory_create_or_exist_strict(msg->base_path, geteuid(), 0700);
+       if (!ok) {
+               goto fail;
+       }
 
        msg->path          = imessaging_path(msg, server_id);
+       if (msg->path == NULL) {
+               goto fail;
+       }
+
        msg->server_id     = server_id;
        msg->idr           = idr_init(msg);
+       if (msg->idr == NULL) {
+               goto fail;
+       }
+
        msg->dispatch_tree = idr_init(msg);
+       if (msg->dispatch_tree == NULL) {
+               goto fail;
+       }
+
        msg->start_time    = timeval_current();
 
+       msg->names_db = irpc_namedb_open(msg, msg->base_path, lp_ctx);
+       if (msg->names_db == NULL) {
+               goto fail;
+       }
+
        status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
        if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(msg);
-               return NULL;
+               goto fail;
        }
 
-       /* 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);
-               return NULL;
+               goto fail;
        }
 
        status = socket_listen(msg->sock, path, 50, 0);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
-               talloc_free(msg);
-               return NULL;
+               goto fail;
        }
 
        /* it needs to be non blocking for sends */
@@ -639,16 +663,19 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        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);
 
        return msg;
+fail:
+       talloc_free(msg);
+       return NULL;
 }
 
-/* 
-   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,
                                                  struct loadparm_context *lp_ctx,
@@ -656,7 +683,13 @@ struct imessaging_context *imessaging_client_init(TALLOC_CTX *mem_ctx,
 {
        struct server_id id;
        ZERO_STRUCT(id);
-       id.pid = random() % 0x10000000;
+       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);
 }
 /*
@@ -676,7 +709,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;
@@ -874,34 +907,32 @@ static int irpc_destructor(struct irpc_request *irpc)
 /*
   open the naming database
 */
-static struct tdb_wrap *irpc_namedb_open(struct imessaging_context *msg_ctx)
+static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
+                                        struct loadparm_context *lp_ctx)
 {
        struct tdb_wrap *t;
-       char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
+       char *path = talloc_asprintf(mem_ctx, "%s/names.tdb", base_path);
        if (path == NULL) {
                return NULL;
        }
-       t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
+       t = tdb_wrap_open(mem_ctx, path, lpcfg_tdb_hash_size(lp_ctx, path),
+                         lpcfg_tdb_flags(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
 */
 NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name)
 {
-       struct tdb_wrap *t;
+       struct tdb_wrap *t = msg_ctx->names_db;
        TDB_DATA rec;
        int count;
        NTSTATUS status = NT_STATUS_OK;
 
-       t = irpc_namedb_open(msg_ctx);
-       NT_STATUS_HAVE_NO_MEMORY(t);
-
        if (tdb_lock_bystring(t->tdb, name) != 0) {
-               talloc_free(t);
                return NT_STATUS_LOCK_NOT_GRANTED;
        }
        rec = tdb_fetch_bystring(t->tdb, name);
@@ -910,7 +941,6 @@ NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name)
        rec.dsize += sizeof(struct server_id);
        if (rec.dptr == NULL) {
                tdb_unlock_bystring(t->tdb, name);
-               talloc_free(t);
                return NT_STATUS_NO_MEMORY;
        }
        ((struct server_id *)rec.dptr)[count] = msg_ctx->server_id;
@@ -919,7 +949,6 @@ NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name)
        }
        free(rec.dptr);
        tdb_unlock_bystring(t->tdb, name);
-       talloc_free(t);
 
        msg_ctx->names = str_list_add(msg_ctx->names, name);
        talloc_steal(msg_ctx, msg_ctx->names);
@@ -934,83 +963,129 @@ struct server_id *irpc_servers_byname(struct imessaging_context *msg_ctx,
                                      TALLOC_CTX *mem_ctx,
                                      const char *name)
 {
-       struct tdb_wrap *t;
+       struct tdb_wrap *t = msg_ctx->names_db;
        TDB_DATA rec;
        int count, i;
        struct server_id *ret;
 
-       t = irpc_namedb_open(msg_ctx);
-       if (t == NULL) {
-               return NULL;
-       }
-
        if (tdb_lock_bystring(t->tdb, name) != 0) {
-               talloc_free(t);
                return NULL;
        }
        rec = tdb_fetch_bystring(t->tdb, name);
        if (rec.dptr == NULL) {
                tdb_unlock_bystring(t->tdb, name);
-               talloc_free(t);
                return NULL;
        }
        count = rec.dsize / sizeof(struct server_id);
        ret = talloc_array(mem_ctx, struct server_id, count+1);
        if (ret == NULL) {
                tdb_unlock_bystring(t->tdb, name);
-               talloc_free(t);
                return NULL;
        }
        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);
 
        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 = msg_ctx->names_db;
+       int ret;
+       struct irpc_name_records *name_records = talloc_zero(mem_ctx, struct irpc_name_records);
+       if (name_records == NULL) {
+               return NULL;
+       }
+
+       ret = tdb_traverse_read(t->tdb, all_servers_func, name_records);
+       if (ret == -1) {
+               return NULL;
+       }
+
+       return name_records;
+}
+
 /*
   remove a name from a messaging context
 */
 void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name)
 {
-       struct tdb_wrap *t;
+       struct tdb_wrap *t = msg_ctx->names_db;
        TDB_DATA rec;
        int count, i;
        struct server_id *ids;
 
        str_list_remove(msg_ctx->names, name);
 
-       t = irpc_namedb_open(msg_ctx);
-       if (t == NULL) {
-               return;
-       }
-
        if (tdb_lock_bystring(t->tdb, name) != 0) {
-               talloc_free(t);
                return;
        }
        rec = tdb_fetch_bystring(t->tdb, name);
        if (rec.dptr == NULL) {
                tdb_unlock_bystring(t->tdb, name);
-               talloc_free(t);
                return;
        }
        count = rec.dsize / sizeof(struct server_id);
        if (count == 0) {
                free(rec.dptr);
                tdb_unlock_bystring(t->tdb, name);
-               talloc_free(t);
                return;
        }
        ids = (struct server_id *)rec.dptr;
        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);
@@ -1020,7 +1095,6 @@ void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name)
        tdb_store_bystring(t->tdb, name, rec, 0);
        free(rec.dptr);
        tdb_unlock_bystring(t->tdb, name);
-       talloc_free(t);
 }
 
 struct server_id imessaging_get_server_id(struct imessaging_context *msg_ctx)
@@ -1310,8 +1384,6 @@ struct dcerpc_binding_handle *irpc_binding_handle(TALLOC_CTX *mem_ctx,
        hs->table = table;
        hs->timeout = IRPC_CALL_TIMEOUT;
 
-       dcerpc_binding_handle_set_sync_ev(h, msg_ctx->event.ev);
-
        return h;
 }
 
@@ -1330,7 +1402,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;