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