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