r13317: Create a new function messaging_client_init() which can be used when
[bbaumbach/samba-autobuild/.git] / source4 / lib / messaging / messaging.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Samba internal messaging functions
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "system/filesys.h"
26 #include "messaging/messaging.h"
27 #include "dlinklist.h"
28 #include "lib/socket/socket.h"
29 #include "librpc/gen_ndr/ndr_irpc.h"
30 #include "lib/messaging/irpc.h"
31 #include "db_wrap.h"
32 #include "lib/tdb/include/tdbutil.h"
33
34 /* change the message version with any incompatible changes in the protocol */
35 #define MESSAGING_VERSION 1
36
37 struct messaging_context {
38         uint32_t server_id;
39         struct socket_context *sock;
40         const char *base_path;
41         const char *path;
42         struct dispatch_fn *dispatch;
43         struct messaging_rec *pending;
44         struct irpc_list *irpc;
45         struct idr_context *idr;
46         const char **names;
47         struct timeval start_time;
48
49         struct {
50                 struct event_context *ev;
51                 struct fd_event *fde;
52         } event;
53 };
54
55 /* we have a linked list of dispatch handlers that this messaging
56    server can deal with */
57 struct dispatch_fn {
58         struct dispatch_fn *next, *prev;
59         uint32_t msg_type;
60         void *private;
61         void (*fn)(struct messaging_context *msg, void *private, 
62                    uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
63 };
64
65 /* an individual message */
66 struct messaging_rec {
67         struct messaging_rec *next, *prev;
68         struct messaging_context *msg;
69         const char *path;
70
71         struct messaging_header {
72                 uint32_t version;
73                 uint32_t msg_type;
74                 uint32_t from;
75                 uint32_t to;
76                 uint32_t length;
77         } *header;
78
79         DATA_BLOB packet;
80 };
81
82
83 static void irpc_handler(struct messaging_context *, void *, 
84                          uint32_t, uint32_t, DATA_BLOB *);
85
86
87 /*
88  A useful function for testing the message system.
89 */
90 static void ping_message(struct messaging_context *msg, void *private, 
91                          uint32_t msg_type, uint32_t src, DATA_BLOB *data)
92 {
93         DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
94                  (uint_t)src, (int)data->length, 
95                  data->data?(const char *)data->data:""));
96         messaging_send(msg, src, MSG_PONG, data);
97 }
98
99 /*
100   return uptime of messaging server via irpc
101 */
102 static NTSTATUS irpc_uptime(struct irpc_message *msg, 
103                             struct irpc_uptime *r)
104 {
105         struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
106         *r->out.start_time = timeval_to_nttime(&ctx->start_time);
107         return NT_STATUS_OK;
108 }
109
110 /* 
111    return the path to a messaging socket
112 */
113 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
114 {
115         return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
116 }
117
118 /*
119   dispatch a fully received message
120
121   note that this deliberately can match more than one message handler
122   per message. That allows a single messasging context to register
123   (for example) a debug handler for more than one piece of code
124 */
125 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
126 {
127         struct dispatch_fn *d, *next;
128         for (d=msg->dispatch;d;d=next) {
129                 next = d->next;
130                 if (d->msg_type == rec->header->msg_type) {
131                         DATA_BLOB data;
132                         data.data = rec->packet.data + sizeof(*rec->header);
133                         data.length = rec->header->length;
134                         d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
135                 }
136         }
137         rec->header->length = 0;
138 }
139
140
141 /*
142   try to send the message
143 */
144 static NTSTATUS try_send(struct messaging_rec *rec)
145 {
146         struct messaging_context *msg = rec->msg;
147         size_t nsent;
148         void *priv;
149         NTSTATUS status;
150         struct socket_address *path;
151
152         /* rec->path is the path of the *other* socket, where we want
153          * this to end up */
154         path = socket_address_from_strings(msg, msg->sock->backend_name, 
155                                            rec->path, 0);
156         if (!path) {
157                 return NT_STATUS_NO_MEMORY;
158         }
159
160         /* we send with privileges so messages work from any context */
161         priv = root_privileges();
162         status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, path);
163         talloc_free(path);
164         talloc_free(priv);
165
166         return status;
167 }
168
169 /*
170   handle a socket write event
171 */
172 static void messaging_send_handler(struct messaging_context *msg)
173 {
174         while (msg->pending) {
175                 struct messaging_rec *rec = msg->pending;
176                 NTSTATUS status;
177                 status = try_send(rec);
178                 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
179                         break;
180                 }
181                 if (!NT_STATUS_IS_OK(status)) {
182                         DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n", 
183                                  rec->header->from, rec->header->to, rec->header->msg_type, 
184                                  nt_errstr(status)));
185                 }
186                 DLIST_REMOVE(msg->pending, rec);
187                 talloc_free(rec);
188         }
189         if (msg->pending == NULL) {
190                 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
191         }
192 }
193
194 /*
195   handle a new incoming packet
196 */
197 static void messaging_recv_handler(struct messaging_context *msg)
198 {
199         struct messaging_rec *rec;
200         NTSTATUS status;
201         DATA_BLOB packet;
202         size_t msize;
203
204         /* see how many bytes are in the next packet */
205         status = socket_pending(msg->sock, &msize);
206         if (!NT_STATUS_IS_OK(status)) {
207                 DEBUG(0,("socket_pending failed in messaging - %s\n", 
208                          nt_errstr(status)));
209                 return;
210         }
211         
212         packet = data_blob_talloc(msg, NULL, msize);
213         if (packet.data == NULL) {
214                 /* assume this is temporary and retry */
215                 return;
216         }
217             
218         status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
219         if (!NT_STATUS_IS_OK(status)) {
220                 data_blob_free(&packet);
221                 return;
222         }
223
224         if (msize < sizeof(*rec->header)) {
225                 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
226                 data_blob_free(&packet);
227                 return;
228         }
229
230         rec = talloc(msg, struct messaging_rec);
231         if (rec == NULL) {
232                 smb_panic("Unable to allocate messaging_rec");
233         }
234
235         talloc_steal(rec, packet.data);
236         rec->msg           = msg;
237         rec->path          = msg->path;
238         rec->header        = (struct messaging_header *)packet.data;
239         rec->packet        = packet;
240
241         if (msize != sizeof(*rec->header) + rec->header->length) {
242                 DEBUG(0,("messaging: bad message header size %d should be %d\n", 
243                          rec->header->length, (int)(msize - sizeof(*rec->header))));
244                 talloc_free(rec);
245                 return;
246         }
247
248         messaging_dispatch(msg, rec);
249         talloc_free(rec);
250 }
251
252
253 /*
254   handle a socket event
255 */
256 static void messaging_handler(struct event_context *ev, struct fd_event *fde, 
257                               uint16_t flags, void *private)
258 {
259         struct messaging_context *msg = talloc_get_type(private, 
260                                                         struct messaging_context);
261         if (flags & EVENT_FD_WRITE) {
262                 messaging_send_handler(msg);
263         }
264         if (flags & EVENT_FD_READ) {
265                 messaging_recv_handler(msg);
266         }
267 }
268
269
270 /*
271   Register a dispatch function for a particular message type.
272 */
273 void messaging_register(struct messaging_context *msg, void *private,
274                         uint32_t msg_type, 
275                         void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
276 {
277         struct dispatch_fn *d;
278
279         d = talloc(msg, struct dispatch_fn);
280         d->msg_type = msg_type;
281         d->private = private;
282         d->fn = fn;
283         DLIST_ADD(msg->dispatch, d);
284 }
285
286 /*
287   De-register the function for a particular message type.
288 */
289 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
290 {
291         struct dispatch_fn *d, *next;
292
293         for (d = msg->dispatch; d; d = next) {
294                 next = d->next;
295                 if (d->msg_type == msg_type && 
296                     d->private == private) {
297                         DLIST_REMOVE(msg->dispatch, d);
298                         talloc_free(d);
299                 }
300         }       
301 }
302
303
304 /*
305   Send a message to a particular server
306 */
307 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server, 
308                         uint32_t msg_type, DATA_BLOB *data)
309 {
310         struct messaging_rec *rec;
311         NTSTATUS status;
312         size_t dlength = data?data->length:0;
313
314         rec = talloc(msg, struct messaging_rec);
315         if (rec == NULL) {
316                 return NT_STATUS_NO_MEMORY;
317         }
318
319         rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
320         if (rec->packet.data == NULL) {
321                 talloc_free(rec);
322                 return NT_STATUS_NO_MEMORY;
323         }
324
325         rec->msg              = msg;
326         rec->header           = (struct messaging_header *)rec->packet.data;
327         rec->header->version  = MESSAGING_VERSION;
328         rec->header->msg_type = msg_type;
329         rec->header->from     = msg->server_id;
330         rec->header->to       = server;
331         rec->header->length   = dlength;
332         if (dlength != 0) {
333                 memcpy(rec->packet.data + sizeof(*rec->header), 
334                        data->data, dlength);
335         }
336
337         rec->path = messaging_path(msg, server);
338         talloc_steal(rec, rec->path);
339
340         if (msg->pending != NULL) {
341                 status = STATUS_MORE_ENTRIES;
342         } else {
343                 status = try_send(rec);
344         }
345
346         if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
347                 if (msg->pending == NULL) {
348                         EVENT_FD_WRITEABLE(msg->event.fde);
349                 }
350                 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
351                 return NT_STATUS_OK;
352         }
353
354         talloc_free(rec);
355
356         return status;
357 }
358
359 /*
360   Send a message to a particular server, with the message containing a single pointer
361 */
362 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server, 
363                             uint32_t msg_type, void *ptr)
364 {
365         DATA_BLOB blob;
366
367         blob.data = (void *)&ptr;
368         blob.length = sizeof(void *);
369
370         return messaging_send(msg, server, msg_type, &blob);
371 }
372
373
374 /*
375   destroy the messaging context
376 */
377 static int messaging_destructor(void *ptr)
378 {
379         struct messaging_context *msg = ptr;
380         unlink(msg->path);
381         while (msg->names && msg->names[0]) {
382                 irpc_remove_name(msg, msg->names[0]);
383         }
384         return 0;
385 }
386
387 /*
388   create the listening socket and setup the dispatcher
389 */
390 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id, 
391                                          struct event_context *ev)
392 {
393         struct messaging_context *msg;
394         NTSTATUS status;
395         struct socket_address *path;
396         char *dir;
397
398         msg = talloc(mem_ctx, struct messaging_context);
399         if (msg == NULL) {
400                 return NULL;
401         }
402
403         if (ev == NULL) {
404                 ev = event_context_init(msg);
405         }
406
407         /* create the messaging directory if needed */
408         dir = smbd_tmp_path(msg, "messaging");
409         mkdir(dir, 0700);
410         talloc_free(dir);
411
412         msg->base_path  = smbd_tmp_path(msg, "messaging");
413         msg->path       = messaging_path(msg, server_id);
414         msg->server_id  = server_id;
415         msg->dispatch   = NULL;
416         msg->pending    = NULL;
417         msg->idr        = idr_init(msg);
418         msg->irpc       = NULL;
419         msg->names      = NULL;
420         msg->start_time = timeval_current();
421
422         status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
423         if (!NT_STATUS_IS_OK(status)) {
424                 talloc_free(msg);
425                 return NULL;
426         }
427
428         /* by stealing here we ensure that the socket is cleaned up (and even 
429            deleted) on exit */
430         talloc_steal(msg, msg->sock);
431
432         path = socket_address_from_strings(msg, msg->sock->backend_name, 
433                                            msg->path, 0);
434         if (!path) {
435                 talloc_free(msg);
436                 return NULL;
437         }
438
439         status = socket_listen(msg->sock, path, 50, 0);
440         if (!NT_STATUS_IS_OK(status)) {
441                 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
442                 talloc_free(msg);
443                 return NULL;
444         }
445
446         /* it needs to be non blocking for sends */
447         set_blocking(socket_get_fd(msg->sock), False);
448
449         msg->event.ev   = talloc_reference(msg, ev);
450         msg->event.fde  = event_add_fd(ev, msg, socket_get_fd(msg->sock), 
451                                        EVENT_FD_READ, messaging_handler, msg);
452
453         talloc_set_destructor(msg, messaging_destructor);
454         
455         messaging_register(msg, NULL, MSG_PING, ping_message);
456         messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
457         IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
458
459         return msg;
460 }
461
462 /* 
463    A hack, for the short term until we get 'client only' messaging in place 
464 */
465 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx, 
466                                                 struct event_context *ev)
467 {
468         return messaging_init(mem_ctx, random() % 0x10000000, ev);
469 }
470 /*
471   a list of registered irpc server functions
472 */
473 struct irpc_list {
474         struct irpc_list *next, *prev;
475         struct GUID uuid;
476         const struct dcerpc_interface_table *table;
477         int callnum;
478         irpc_function_t fn;
479         void *private;
480 };
481
482
483 /*
484   register a irpc server function
485 */
486 NTSTATUS irpc_register(struct messaging_context *msg_ctx, 
487                        const struct dcerpc_interface_table *table, 
488                        int callnum, irpc_function_t fn, void *private)
489 {
490         struct irpc_list *irpc;
491
492         /* override an existing handler, if any */
493         for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
494                 if (irpc->table == table && irpc->callnum == callnum) {
495                         break;
496                 }
497         }
498         if (irpc == NULL) {
499                 irpc = talloc(msg_ctx, struct irpc_list);
500                 NT_STATUS_HAVE_NO_MEMORY(irpc);
501                 DLIST_ADD(msg_ctx->irpc, irpc);
502         }
503
504         irpc->table   = table;
505         irpc->callnum = callnum;
506         irpc->fn      = fn;
507         irpc->private = private;
508         irpc->uuid = irpc->table->uuid;
509
510         return NT_STATUS_OK;
511 }
512
513
514 /*
515   handle an incoming irpc reply message
516 */
517 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
518 {
519         struct irpc_request *irpc;
520
521         irpc = idr_find(msg_ctx->idr, m->header.callid);
522         if (irpc == NULL) return;
523
524         /* parse the reply data */
525         irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
526         if (NT_STATUS_IS_OK(irpc->status)) {
527                 irpc->status = m->header.status;
528                 talloc_steal(irpc->mem_ctx, m);
529         } else {
530                 talloc_steal(irpc, m);
531         }
532         irpc->done = True;
533         if (irpc->async.fn) {
534                 irpc->async.fn(irpc);
535         }
536 }
537
538 /*
539   send a irpc reply
540 */
541 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
542 {
543         struct ndr_push *push;
544         DATA_BLOB packet;
545
546         m->header.status = status;
547
548         /* setup the reply */
549         push = ndr_push_init_ctx(m->ndr);
550         if (push == NULL) {
551                 status = NT_STATUS_NO_MEMORY;
552                 goto failed;
553         }
554
555         m->header.flags |= IRPC_FLAG_REPLY;
556
557         /* construct the packet */
558         status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
559         if (!NT_STATUS_IS_OK(status)) goto failed;
560
561         status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
562         if (!NT_STATUS_IS_OK(status)) goto failed;
563
564         /* send the reply message */
565         packet = ndr_push_blob(push);
566         status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
567         if (!NT_STATUS_IS_OK(status)) goto failed;
568
569 failed:
570         talloc_free(m);
571         return status;
572 }
573
574 /*
575   handle an incoming irpc request message
576 */
577 static void irpc_handler_request(struct messaging_context *msg_ctx, 
578                                  struct irpc_message *m)
579 {
580         struct irpc_list *i;
581         void *r;
582         NTSTATUS status;
583
584         for (i=msg_ctx->irpc; i; i=i->next) {
585                 if (GUID_equal(&i->uuid, &m->header.uuid) &&
586                     i->table->if_version == m->header.if_version &&
587                     i->callnum == m->header.callnum) {
588                         break;
589                 }
590         }
591
592         if (i == NULL) {
593                 /* no registered handler for this message */
594                 talloc_free(m);
595                 return;
596         }
597
598         /* allocate space for the structure */
599         r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
600         if (r == NULL) goto failed;
601
602         /* parse the request data */
603         status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
604         if (!NT_STATUS_IS_OK(status)) goto failed;
605
606         /* make the call */
607         m->private     = i->private;
608         m->defer_reply = False;
609         m->msg_ctx     = msg_ctx;
610         m->irpc        = i;
611         m->data        = r;
612         m->ev          = msg_ctx->event.ev;
613
614         m->header.status = i->fn(m, r);
615
616         if (m->defer_reply) {
617                 /* the server function has asked to defer the reply to later */
618                 talloc_steal(msg_ctx, m);
619                 return;
620         }
621
622         irpc_send_reply(m, m->header.status);
623         return;
624
625 failed:
626         talloc_free(m);
627 }
628
629 /*
630   handle an incoming irpc message
631 */
632 static void irpc_handler(struct messaging_context *msg_ctx, void *private, 
633                          uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
634 {
635         struct irpc_message *m;
636         NTSTATUS status;
637
638         m = talloc(msg_ctx, struct irpc_message);
639         if (m == NULL) goto failed;
640
641         m->from = src;
642
643         m->ndr = ndr_pull_init_blob(packet, m);
644         if (m->ndr == NULL) goto failed;
645
646         m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
647
648         status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
649         if (!NT_STATUS_IS_OK(status)) goto failed;
650
651         if (m->header.flags & IRPC_FLAG_REPLY) {
652                 irpc_handler_reply(msg_ctx, m);
653         } else {
654                 irpc_handler_request(msg_ctx, m);
655         }
656         return;
657
658 failed:
659         talloc_free(m);
660 }
661
662
663 /*
664   destroy a irpc request
665 */
666 static int irpc_destructor(void *ptr)
667 {
668         struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
669         idr_remove(irpc->msg_ctx->idr, irpc->callid);
670         return 0;
671 }
672
673 /*
674   timeout a irpc request
675 */
676 static void irpc_timeout(struct event_context *ev, struct timed_event *te, 
677                          struct timeval t, void *private)
678 {
679         struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
680         irpc->status = NT_STATUS_IO_TIMEOUT;
681         irpc->done = True;
682         if (irpc->async.fn) {
683                 irpc->async.fn(irpc);
684         }
685 }
686
687
688 /*
689   make a irpc call - async send
690 */
691 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx, 
692                                     uint32_t server_id, 
693                                     const struct dcerpc_interface_table *table, 
694                                     int callnum, void *r, TALLOC_CTX *ctx)
695 {
696         struct irpc_header header;
697         struct ndr_push *ndr;
698         NTSTATUS status;
699         DATA_BLOB packet;
700         struct irpc_request *irpc;
701
702         irpc = talloc(msg_ctx, struct irpc_request);
703         if (irpc == NULL) goto failed;
704
705         irpc->msg_ctx  = msg_ctx;
706         irpc->table    = table;
707         irpc->callnum  = callnum;
708         irpc->callid   = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
709         if (irpc->callid == -1) goto failed;
710         irpc->r        = r;
711         irpc->done     = False;
712         irpc->async.fn = NULL;
713         irpc->mem_ctx  = ctx;
714
715         talloc_set_destructor(irpc, irpc_destructor);
716
717         /* setup the header */
718         header.uuid = table->uuid;
719
720         header.if_version = table->if_version;
721         header.callid     = irpc->callid;
722         header.callnum    = callnum;
723         header.flags      = 0;
724         header.status     = NT_STATUS_OK;
725
726         /* construct the irpc packet */
727         ndr = ndr_push_init_ctx(irpc);
728         if (ndr == NULL) goto failed;
729
730         status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
731         if (!NT_STATUS_IS_OK(status)) goto failed;
732
733         status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
734         if (!NT_STATUS_IS_OK(status)) goto failed;
735
736         /* and send it */
737         packet = ndr_push_blob(ndr);
738         status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
739         if (!NT_STATUS_IS_OK(status)) goto failed;
740
741         event_add_timed(msg_ctx->event.ev, irpc, 
742                         timeval_current_ofs(IRPC_CALL_TIMEOUT, 0), 
743                         irpc_timeout, irpc);
744
745         talloc_free(ndr);
746         return irpc;
747
748 failed:
749         talloc_free(irpc);
750         return NULL;
751 }
752
753 /*
754   wait for a irpc reply
755 */
756 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
757 {
758         NTSTATUS status;
759
760         NT_STATUS_HAVE_NO_MEMORY(irpc);
761
762         while (!irpc->done) {
763                 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
764                         return NT_STATUS_CONNECTION_DISCONNECTED;
765                 }
766         }
767         status = irpc->status;
768         talloc_free(irpc);
769         return status;
770 }
771
772 /*
773   perform a synchronous irpc request
774 */
775 NTSTATUS irpc_call(struct messaging_context *msg_ctx, 
776                    uint32_t server_id, 
777                    const struct dcerpc_interface_table *table, 
778                    int callnum, void *r,
779                    TALLOC_CTX *mem_ctx)
780 {
781         struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id, 
782                                                    table, callnum, r, mem_ctx);
783         return irpc_call_recv(irpc);
784 }
785
786 /*
787   open the naming database
788 */
789 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
790 {
791         struct tdb_wrap *t;
792         char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
793         if (path == NULL) {
794                 return NULL;
795         }
796         t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
797         talloc_free(path);
798         return t;
799 }
800         
801
802 /*
803   add a string name that this irpc server can be called on
804 */
805 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
806 {
807         struct tdb_wrap *t;
808         TDB_DATA rec;
809         int count;
810         NTSTATUS status = NT_STATUS_OK;
811
812         t = irpc_namedb_open(msg_ctx);
813         NT_STATUS_HAVE_NO_MEMORY(t);
814
815         if (tdb_lock_bystring(t->tdb, name) != 0) {
816                 talloc_free(t);
817                 return NT_STATUS_LOCK_NOT_GRANTED;
818         }
819         rec = tdb_fetch_bystring(t->tdb, name);
820         count = rec.dsize / sizeof(uint32_t);
821         rec.dptr = (unsigned char *)realloc_p(rec.dptr, uint32_t, count+1);
822         rec.dsize += sizeof(uint32_t);
823         if (rec.dptr == NULL) {
824                 tdb_unlock_bystring(t->tdb, name);
825                 talloc_free(t);
826                 return NT_STATUS_NO_MEMORY;
827         }
828         ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
829         if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
830                 status = NT_STATUS_INTERNAL_ERROR;
831         }
832         free(rec.dptr);
833         tdb_unlock_bystring(t->tdb, name);
834         talloc_free(t);
835
836         msg_ctx->names = str_list_add(msg_ctx->names, name);
837         talloc_steal(msg_ctx, msg_ctx->names);
838
839         return status;
840 }
841
842 /*
843   return a list of server ids for a server name
844 */
845 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
846 {
847         struct tdb_wrap *t;
848         TDB_DATA rec;
849         int count, i;
850         uint32_t *ret;
851
852         t = irpc_namedb_open(msg_ctx);
853         if (t == NULL) {
854                 return NULL;
855         }
856
857         if (tdb_lock_bystring(t->tdb, name) != 0) {
858                 talloc_free(t);
859                 return NULL;
860         }
861         rec = tdb_fetch_bystring(t->tdb, name);
862         if (rec.dptr == NULL) {
863                 tdb_unlock_bystring(t->tdb, name);
864                 talloc_free(t);
865                 return NULL;
866         }
867         count = rec.dsize / sizeof(uint32_t);
868         ret = talloc_array(msg_ctx, uint32_t, count+1);
869         if (ret == NULL) {
870                 tdb_unlock_bystring(t->tdb, name);
871                 talloc_free(t);
872                 return NULL;
873         }
874         for (i=0;i<count;i++) {
875                 ret[i] = ((uint32_t *)rec.dptr)[i];
876         }
877         ret[i] = 0;
878         free(rec.dptr);
879         tdb_unlock_bystring(t->tdb, name);
880         talloc_free(t);
881
882         return ret;
883 }
884
885 /*
886   remove a name from a messaging context
887 */
888 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
889 {
890         struct tdb_wrap *t;
891         TDB_DATA rec;
892         int count, i;
893         uint32_t *ids;
894
895         str_list_remove(msg_ctx->names, name);
896
897         t = irpc_namedb_open(msg_ctx);
898         if (t == NULL) {
899                 return;
900         }
901
902         if (tdb_lock_bystring(t->tdb, name) != 0) {
903                 talloc_free(t);
904                 return;
905         }
906         rec = tdb_fetch_bystring(t->tdb, name);
907         count = rec.dsize / sizeof(uint32_t);
908         if (count == 0) {
909                 tdb_unlock_bystring(t->tdb, name);
910                 talloc_free(t);
911                 return;
912         }
913         ids = (uint32_t *)rec.dptr;
914         for (i=0;i<count;i++) {
915                 if (ids[i] == msg_ctx->server_id) {
916                         if (i < count-1) {
917                                 memmove(ids+i, ids+i+1, count-(i+1));
918                         }
919                         rec.dsize -= sizeof(uint32_t);
920                         break;
921                 }
922         }
923         tdb_store_bystring(t->tdb, name, rec, 0);
924         free(rec.dptr);
925         tdb_unlock_bystring(t->tdb, name);
926         talloc_free(t);
927 }