messaging3: Introduce messaging_local_backend()
[samba.git] / source3 / lib / messages_dgm.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba internal messaging functions
4  * Copyright (C) 2013 by Volker Lendecke
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "lib/util/data_blob.h"
22 #include "lib/util/debug.h"
23 #include "lib/unix_msg/unix_msg.h"
24 #include "system/filesys.h"
25 #include "messages.h"
26 #include "lib/param/param.h"
27 #include "poll_funcs/poll_funcs_tevent.h"
28 #include "unix_msg/unix_msg.h"
29 #include "librpc/gen_ndr/messaging.h"
30
31 struct messaging_dgm_context {
32         struct messaging_context *msg_ctx;
33         struct poll_funcs *msg_callbacks;
34         void *tevent_handle;
35         struct unix_msg_ctx *dgm_ctx;
36         char *cache_dir;
37         int lockfile_fd;
38 };
39
40 struct messaging_dgm_hdr {
41         uint32_t msg_version;
42         enum messaging_type msg_type;
43         struct server_id dst;
44         struct server_id src;
45 };
46
47 static NTSTATUS messaging_dgm_send(struct server_id src,
48                                    struct server_id pid, int msg_type,
49                                    const struct iovec *iov, int iovlen,
50                                    struct messaging_backend *backend);
51 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
52                                uint8_t *msg, size_t msg_len,
53                                void *private_data);
54
55 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
56
57 static int messaging_dgm_lockfile_create(const char *cache_dir, pid_t pid,
58                                          int *plockfile_fd, uint64_t unique)
59 {
60         char buf[PATH_MAX];
61         char *dir, *to_free;
62         ssize_t dirlen;
63         char *lockfile_name;
64         int lockfile_fd;
65         struct flock lck = {};
66         int unique_len, ret;
67         ssize_t written;
68         bool ok;
69
70         dirlen = full_path_tos(cache_dir, "lck", buf, sizeof(buf),
71                                &dir, &to_free);
72         if (dirlen == -1) {
73                 return ENOMEM;
74         }
75
76         ok = directory_create_or_exist_strict(dir, sec_initial_uid(), 0755);
77         if (!ok) {
78                 ret = errno;
79                 DEBUG(1, ("%s: Could not create lock directory: %s\n",
80                           __func__, strerror(ret)));
81                 TALLOC_FREE(to_free);
82                 return ret;
83         }
84
85         lockfile_name = talloc_asprintf(talloc_tos(), "%s/%u", dir,
86                                         (unsigned)pid);
87         TALLOC_FREE(to_free);
88         if (lockfile_name == NULL) {
89                 DEBUG(1, ("%s: talloc_asprintf failed\n", __func__));
90                 return ENOMEM;
91         }
92
93         /* no O_EXCL, existence check is via the fcntl lock */
94
95         lockfile_fd = open(lockfile_name, O_NONBLOCK|O_CREAT|O_WRONLY, 0644);
96         if (lockfile_fd == -1) {
97                 ret = errno;
98                 DEBUG(1, ("%s: open failed: %s\n", __func__, strerror(errno)));
99                 goto fail_free;
100         }
101
102         lck.l_type = F_WRLCK;
103         lck.l_whence = SEEK_SET;
104         lck.l_start = 0;
105         lck.l_len = 0;
106
107         ret = fcntl(lockfile_fd, F_SETLK, &lck);
108         if (ret == -1) {
109                 ret = errno;
110                 DEBUG(1, ("%s: fcntl failed: %s\n", __func__, strerror(ret)));
111                 goto fail_close;
112         }
113
114         unique_len = snprintf(buf, sizeof(buf), "%"PRIu64, unique);
115
116         /* shorten a potentially preexisting file */
117
118         ret = ftruncate(lockfile_fd, unique_len);
119         if (ret == -1) {
120                 ret = errno;
121                 DEBUG(1, ("%s: ftruncate failed: %s\n", __func__,
122                           strerror(ret)));
123                 goto fail_unlink;
124         }
125
126         written = write(lockfile_fd, buf, unique_len);
127         if (written != unique_len) {
128                 ret = errno;
129                 DEBUG(1, ("%s: write failed: %s\n", __func__, strerror(ret)));
130                 goto fail_unlink;
131         }
132
133         *plockfile_fd = lockfile_fd;
134         return 0;
135
136 fail_unlink:
137         unlink(lockfile_name);
138 fail_close:
139         close(lockfile_fd);
140 fail_free:
141         TALLOC_FREE(lockfile_name);
142         return ret;
143 }
144
145 static int messaging_dgm_lockfile_remove(const char *cache_dir, pid_t pid)
146 {
147         fstring fname;
148         char buf[PATH_MAX];
149         char *lockfile_name, *to_free;
150         ssize_t len;
151         int ret;
152
153         fstr_sprintf(fname, "lck/%u", (unsigned)pid);
154
155         len = full_path_tos(cache_dir, fname, buf, sizeof(buf),
156                             &lockfile_name, &to_free);
157         if (len == -1) {
158                 return ENOMEM;
159         }
160
161         ret = unlink(lockfile_name);
162         if (ret == -1) {
163                 ret = errno;
164                 DEBUG(10, ("%s: unlink failed: %s\n", __func__,
165                            strerror(ret)));
166         }
167         TALLOC_FREE(to_free);
168         return ret;
169 }
170
171 NTSTATUS messaging_dgm_init(struct messaging_context *msg_ctx,
172                             TALLOC_CTX *mem_ctx,
173                             struct messaging_backend **presult)
174 {
175         struct messaging_backend *result;
176         struct messaging_dgm_context *ctx;
177         struct server_id pid = messaging_server_id(msg_ctx);
178         int ret;
179         bool ok;
180         const char *cache_dir;
181         char *socket_dir, *socket_name;
182         uint64_t cookie;
183
184         cache_dir = lp_cache_directory();
185         if (cache_dir == NULL) {
186                 NTSTATUS status = map_nt_error_from_unix(errno);
187                 return status;
188         }
189
190         result = talloc(mem_ctx, struct messaging_backend);
191         if (result == NULL) {
192                 goto fail_nomem;
193         }
194         ctx = talloc_zero(result, struct messaging_dgm_context);
195         if (ctx == NULL) {
196                 goto fail_nomem;
197         }
198
199         result->private_data = ctx;
200         result->send_fn = messaging_dgm_send;
201         ctx->msg_ctx = msg_ctx;
202
203         ctx->cache_dir = talloc_strdup(ctx, cache_dir);
204         if (ctx->cache_dir == NULL) {
205                 goto fail_nomem;
206         }
207         socket_dir = talloc_asprintf(ctx, "%s/msg", cache_dir);
208         if (socket_dir == NULL) {
209                 goto fail_nomem;
210         }
211         socket_name = talloc_asprintf(ctx, "%s/%u", socket_dir,
212                                       (unsigned)pid.pid);
213         if (socket_name == NULL) {
214                 goto fail_nomem;
215         }
216
217         sec_init();
218
219         ret = messaging_dgm_lockfile_create(cache_dir, pid.pid,
220                                             &ctx->lockfile_fd, pid.unique_id);
221         if (ret != 0) {
222                 DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
223                           __func__, strerror(ret)));
224                 TALLOC_FREE(result);
225                 return map_nt_error_from_unix(ret);
226         }
227
228         ctx->msg_callbacks = poll_funcs_init_tevent(ctx);
229         if (ctx->msg_callbacks == NULL) {
230                 TALLOC_FREE(result);
231                 return NT_STATUS_NO_MEMORY;
232         }
233
234         ctx->tevent_handle = poll_funcs_tevent_register(
235                 ctx, ctx->msg_callbacks, msg_ctx->event_ctx);
236         if (ctx->tevent_handle == NULL) {
237                 TALLOC_FREE(result);
238                 return NT_STATUS_NO_MEMORY;
239         }
240
241         ok = directory_create_or_exist_strict(socket_dir, sec_initial_uid(),
242                                               0700);
243         if (!ok) {
244                 DEBUG(1, ("Could not create socket directory\n"));
245                 TALLOC_FREE(result);
246                 return NT_STATUS_ACCESS_DENIED;
247         }
248         TALLOC_FREE(socket_dir);
249
250         unlink(socket_name);
251
252         generate_random_buffer((uint8_t *)&cookie, sizeof(cookie));
253
254         ret = unix_msg_init(socket_name, ctx->msg_callbacks, 1024, cookie,
255                             messaging_dgm_recv, ctx, &ctx->dgm_ctx);
256         TALLOC_FREE(socket_name);
257         if (ret != 0) {
258                 DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
259                 TALLOC_FREE(result);
260                 return map_nt_error_from_unix(ret);
261         }
262         talloc_set_destructor(ctx, messaging_dgm_context_destructor);
263
264         *presult = result;
265         return NT_STATUS_OK;
266
267 fail_nomem:
268         TALLOC_FREE(result);
269         return NT_STATUS_NO_MEMORY;
270 }
271
272 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
273 {
274         struct server_id pid = messaging_server_id(c->msg_ctx);
275
276         /*
277          * First delete the socket to avoid races. The lockfile is the
278          * indicator that we're still around.
279          */
280         unix_msg_free(c->dgm_ctx);
281
282         if (getpid() == pid.pid) {
283                 (void)messaging_dgm_lockfile_remove(c->cache_dir, pid.pid);
284         }
285         close(c->lockfile_fd);
286         return 0;
287 }
288
289 static NTSTATUS messaging_dgm_send(struct server_id src,
290                                    struct server_id pid, int msg_type,
291                                    const struct iovec *iov, int iovlen,
292                                    struct messaging_backend *backend)
293 {
294         struct messaging_dgm_context *ctx = talloc_get_type_abort(
295                 backend->private_data, struct messaging_dgm_context);
296         fstring pid_str;
297         char buf[PATH_MAX];
298         char *dst_sock, *to_free;
299         struct messaging_dgm_hdr hdr;
300         struct iovec iov2[iovlen + 1];
301         ssize_t pathlen;
302         int ret;
303
304         fstr_sprintf(pid_str, "msg/%u", (unsigned)pid.pid);
305
306         pathlen = full_path_tos(ctx->cache_dir, pid_str, buf, sizeof(buf),
307                                 &dst_sock, &to_free);
308         if (pathlen == -1) {
309                 return NT_STATUS_NO_MEMORY;
310         }
311
312         hdr.msg_version = MESSAGE_VERSION;
313         hdr.msg_type = msg_type & MSG_TYPE_MASK;
314         hdr.dst = pid;
315         hdr.src = src;
316
317         DEBUG(10, ("%s: Sending message 0x%x to %s\n", __func__,
318                    (unsigned)hdr.msg_type,
319                    server_id_str(talloc_tos(), &pid)));
320
321         iov2[0].iov_base = &hdr;
322         iov2[0].iov_len = sizeof(hdr);
323         memcpy(iov2+1, iov, iovlen*sizeof(struct iovec));
324
325         become_root();
326         ret = unix_msg_send(ctx->dgm_ctx, dst_sock, iov2, iovlen + 1);
327         unbecome_root();
328
329         TALLOC_FREE(to_free);
330
331         if (ret != 0) {
332                 return map_nt_error_from_unix(ret);
333         }
334         return NT_STATUS_OK;
335 }
336
337 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
338                                uint8_t *msg, size_t msg_len,
339                                void *private_data)
340 {
341         struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
342                 private_data, struct messaging_dgm_context);
343         struct messaging_dgm_hdr *hdr;
344         struct messaging_rec rec;
345
346         if (msg_len < sizeof(*hdr)) {
347                 DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
348                 return;
349         }
350
351         /*
352          * unix_msg guarantees alignment, so we can cast here
353          */
354         hdr = (struct messaging_dgm_hdr *)msg;
355
356         rec.msg_version = hdr->msg_version;
357         rec.msg_type = hdr->msg_type;
358         rec.dest = hdr->dst;
359         rec.src = hdr->src;
360         rec.buf.data = msg + sizeof(*hdr);
361         rec.buf.length = msg_len - sizeof(*hdr);
362
363         DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__,
364                    (unsigned)hdr->msg_type, (unsigned)rec.buf.length,
365                    server_id_str(talloc_tos(), &rec.src)));
366
367         messaging_dispatch_rec(dgm_ctx->msg_ctx, &rec);
368 }
369
370 NTSTATUS messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid)
371 {
372         struct messaging_backend *be = messaging_local_backend(msg_ctx);
373         struct messaging_dgm_context *ctx = talloc_get_type_abort(
374                 be->private_data, struct messaging_dgm_context);
375         char *lockfile_name, *socket_name;
376         int fd, ret;
377         struct flock lck = {};
378         NTSTATUS status = NT_STATUS_OK;
379
380         lockfile_name = talloc_asprintf(talloc_tos(), "%s/lck/%u",
381                                         ctx->cache_dir, (unsigned)pid);
382         if (lockfile_name == NULL) {
383                 return NT_STATUS_NO_MEMORY;
384         }
385         socket_name = talloc_asprintf(lockfile_name, "%s/msg/%u",
386                                       ctx->cache_dir, (unsigned)pid);
387         if (socket_name == NULL) {
388                 TALLOC_FREE(lockfile_name);
389                 return NT_STATUS_NO_MEMORY;
390         }
391
392         fd = open(lockfile_name, O_NONBLOCK|O_WRONLY, 0);
393         if (fd == -1) {
394                 status = map_nt_error_from_unix(errno);
395                 DEBUG(10, ("%s: open(%s) failed: %s\n", __func__,
396                            lockfile_name, strerror(errno)));
397                 return status;
398         }
399
400         lck.l_type = F_WRLCK;
401         lck.l_whence = SEEK_SET;
402         lck.l_start = 0;
403         lck.l_len = 0;
404
405         ret = fcntl(fd, F_SETLK, &lck);
406         if (ret != 0) {
407                 status = map_nt_error_from_unix(errno);
408                 DEBUG(10, ("%s: Could not get lock: %s\n", __func__,
409                            strerror(errno)));
410                 TALLOC_FREE(lockfile_name);
411                 close(fd);
412                 return status;
413         }
414
415         (void)unlink(socket_name);
416         (void)unlink(lockfile_name);
417         (void)close(fd);
418
419         TALLOC_FREE(lockfile_name);
420         return NT_STATUS_OK;
421 }
422
423 NTSTATUS messaging_dgm_wipe(struct messaging_context *msg_ctx)
424 {
425         struct messaging_backend *be = messaging_local_backend(msg_ctx);
426         struct messaging_dgm_context *ctx = talloc_get_type_abort(
427                 be->private_data, struct messaging_dgm_context);
428         char *msgdir_name;
429         DIR *msgdir;
430         struct dirent *dp;
431         pid_t our_pid = getpid();
432
433         /*
434          * We scan the socket directory and not the lock directory. Otherwise
435          * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
436          * and fcntl(SETLK).
437          */
438
439         msgdir_name = talloc_asprintf(talloc_tos(), "%s/msg", ctx->cache_dir);
440         if (msgdir_name == NULL) {
441                 return NT_STATUS_NO_MEMORY;
442         }
443
444         msgdir = opendir(msgdir_name);
445         TALLOC_FREE(msgdir_name);
446         if (msgdir == NULL) {
447                 return map_nt_error_from_unix(errno);
448         }
449
450         while ((dp = readdir(msgdir)) != NULL) {
451                 NTSTATUS status;
452                 unsigned long pid;
453
454                 pid = strtoul(dp->d_name, NULL, 10);
455                 if (pid == 0) {
456                         /*
457                          * . and .. and other malformed entries
458                          */
459                         continue;
460                 }
461                 if (pid == our_pid) {
462                         /*
463                          * fcntl(F_GETLK) will succeed for ourselves, we hold
464                          * that lock ourselves.
465                          */
466                         continue;
467                 }
468
469                 status = messaging_dgm_cleanup(msg_ctx, pid);
470                 DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
471                            pid, nt_errstr(status)));
472         }
473         closedir(msgdir);
474
475         return NT_STATUS_OK;
476 }
477
478 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
479                                             struct messaging_context *msg_ctx,
480                                             struct tevent_context *ev)
481 {
482         struct messaging_backend *be = messaging_local_backend(msg_ctx);
483         struct messaging_dgm_context *ctx = talloc_get_type_abort(
484                 be->private_data, struct messaging_dgm_context);
485         return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);
486 }