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