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