tdb_wrap: Remove tdb_wrap_open_ again
[kai/samba-autobuild/.git] / source4 / lib / messaging / messaging.c
index 4d69b9424bdfea4eb50f4b148f1d7b756df7bbf2..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/>.
 */
@@ -122,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);
@@ -130,7 +130,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)
@@ -159,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];
@@ -196,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;
@@ -221,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;
@@ -239,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,
@@ -271,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 = 
+                                       msg->retry_te =
                                                tevent_add_timer(msg->event.ev, msg,
-                                                               timeval_current_ofs(1, 0), 
+                                                               timeval_current_ofs(1, 0),
                                                                msg_retry_timer, msg);
                                }
                        }
@@ -285,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", 
+                       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);
                }
@@ -313,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);
@@ -349,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;
@@ -444,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);
@@ -493,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);
        }
 
@@ -576,6 +576,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;
@@ -603,7 +604,11 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
 
        msg->base_path     = lpcfg_imessaging_path(msg, lp_ctx);
 
-       mkdir(msg->base_path, 0700);
+       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;
@@ -617,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);
@@ -646,7 +651,7 @@ 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);
@@ -654,8 +659,8 @@ 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,
                                                  struct loadparm_context *lp_ctx,
@@ -689,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;
@@ -894,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, msg_ctx->lp_ctx);
+       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
@@ -977,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);
@@ -985,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
 */
@@ -1023,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);
@@ -1343,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;