-/*
+/*
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
+static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
+ struct loadparm_context *lp_ctx);
+
/*
a pending irpc call
*/
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 {
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:""));
/*
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);
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;
/* 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];
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;
/* 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;
/*
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,
DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
}
- EVENT_FD_WRITEABLE(msg->event.fde);
+ TEVENT_FD_WRITEABLE(msg->event.fde);
}
/*
/* 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);
}
}
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);
}
talloc_free(rec);
}
if (msg->pending == NULL) {
- EVENT_FD_NOT_WRITEABLE(msg->event.fde);
+ TEVENT_FD_NOT_WRITEABLE(msg->event.fde);
}
}
/* 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);
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;
{
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);
}
}
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);
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);
}
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;
/*
- 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]);
/*
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;
/* 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 */
- mkdir(dir, 0700);
- msg->base_path = talloc_reference(msg, dir);
+ msg->base_path = lpcfg_imessaging_path(msg, lp_ctx);
+ if (msg->base_path == NULL) {
+ goto fail;
+ }
+
+ 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 */
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);
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,
- 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
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;
/*
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);
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;
}
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);
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);
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)
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);
}
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;
}
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);
}
hs->table = table;
hs->timeout = IRPC_CALL_TIMEOUT;
- dcerpc_binding_handle_set_sync_ev(h, msg_ctx->event.ev);
-
return h;
}
errno = EADDRNOTAVAIL;
return NULL;
}
- if (sids[0].pid == 0) {
+ if (server_id_is_disconnected(&sids[0])) {
talloc_free(sids);
errno = EADDRNOTAVAIL;
return NULL;