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