auth: Log the transport connection for the authorization
[Samba.git] / source3 / utils / net_notify.c
blobeddfb0ee5a8fa77a1dd88ccfe0d3c0c7aecc793f
1 /*
2 * Samba Unix/Linux notifyd client code
3 * Copyright (C) 2015 Volker Lendecke <vl@samba.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "lib/util/server_id.h"
22 #include "lib/util/tevent_unix.h"
23 #include "lib/util/server_id_db.h"
24 #include "messages.h"
25 #include "source3/smbd/notifyd/notifyd.h"
27 static void net_notify_got_event(struct messaging_context *msg,
28 void *private_data,
29 uint32_t msg_type,
30 struct server_id server_id,
31 DATA_BLOB *data)
33 struct notify_event_msg *event_msg;
35 if (data->length < offsetof(struct notify_event_msg, path) + 1) {
36 d_fprintf(stderr, "message too short\n");
37 return;
39 if (data->data[data->length-1] != 0) {
40 d_fprintf(stderr, "path not 0-terminated\n");
41 return;
44 event_msg = (struct notify_event_msg *)data->data;
46 d_printf("%u %s\n", (unsigned)event_msg->action,
47 event_msg->path);
50 static int net_notify_listen(struct net_context *c, int argc,
51 const char **argv)
53 struct messaging_context *msg_ctx = c->msg_ctx;
54 struct tevent_context *ev = messaging_tevent_context(msg_ctx);
55 struct server_id_db *names_db = messaging_names_db(msg_ctx);
56 struct server_id notifyd;
57 struct server_id_buf idbuf;
58 struct notify_rec_change_msg msg;
59 struct iovec iov[2];
60 NTSTATUS status;
61 bool ok;
63 if (argc != 3) {
64 d_printf("Usage: net notify listen <path> <filter> "
65 "<subdir-filter>\n");
66 return -1;
69 ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
70 if (!ok) {
71 fprintf(stderr, "no notify daemon found\n");
72 return -1;
75 printf("notify daemon: %s\n", server_id_str_buf(notifyd, &idbuf));
77 msg = (struct notify_rec_change_msg) {
78 .instance.filter = atoi(argv[1]),
79 .instance.subdir_filter = atoi(argv[2])
81 iov[0] = (struct iovec) {
82 .iov_base = &msg,
83 .iov_len = offsetof(struct notify_rec_change_msg, path)
85 iov[1] = (struct iovec) {
86 .iov_base = discard_const_p(char, argv[0]),
87 .iov_len = strlen(argv[0])+1
90 status = messaging_register(c->msg_ctx, NULL, MSG_PVFS_NOTIFY,
91 net_notify_got_event);
92 if (!NT_STATUS_IS_OK(status)) {
93 d_fprintf(stderr, "messaging_register failed: %s\n",
94 nt_errstr(status));
95 return -1;
98 status = messaging_send_iov(
99 c->msg_ctx, notifyd, MSG_SMB_NOTIFY_REC_CHANGE,
100 iov, ARRAY_SIZE(iov), NULL, 0);
101 if (!NT_STATUS_IS_OK(status)) {
102 d_fprintf(stderr, "Sending rec_change to %s returned %s\n",
103 server_id_str_buf(notifyd, &idbuf),
104 nt_errstr(status));
105 return -1;
108 while (true) {
109 int ret;
111 ret = tevent_loop_once(ev);
112 if (ret != 0) {
113 d_fprintf(stderr, "tevent_loop_once failed: %s\n",
114 strerror(errno));
115 break;
119 return 0;
122 static int net_notify_trigger(struct net_context *c, int argc,
123 const char **argv)
125 struct messaging_context *msg_ctx = c->msg_ctx;
126 struct server_id_db *names_db = messaging_names_db(msg_ctx);
127 struct server_id notifyd;
128 struct server_id_buf idbuf;
129 struct notify_trigger_msg msg;
130 struct iovec iov[2];
131 NTSTATUS status;
132 bool ok;
134 if (argc != 3) {
135 d_printf("Usage: net notify trigger <path> <action> "
136 "<filter>\n");
137 return -1;
140 ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
141 if (!ok) {
142 fprintf(stderr, "no notify daemon found\n");
143 return -1;
146 printf("notify daemon: %s\n", server_id_str_buf(notifyd, &idbuf));
148 msg = (struct notify_trigger_msg) {
149 .action = atoi(argv[1]), .filter = atoi(argv[2])
152 iov[0] = (struct iovec) {
153 .iov_base = &msg,
154 .iov_len = offsetof(struct notify_trigger_msg, path)
156 iov[1] = (struct iovec) {
157 .iov_base = discard_const_p(char, argv[0]),
158 .iov_len = strlen(argv[0])+1
161 status = messaging_send_iov(
162 c->msg_ctx, notifyd, MSG_SMB_NOTIFY_TRIGGER,
163 iov, ARRAY_SIZE(iov), NULL, 0);
164 if (!NT_STATUS_IS_OK(status)) {
165 d_printf("Sending rec_change to %s returned %s\n",
166 server_id_str_buf(notifyd, &idbuf),
167 nt_errstr(status));
168 return -1;
171 return 0;
174 int net_notify(struct net_context *c, int argc, const char **argv)
176 struct functable func[] = {
177 { "listen",
178 net_notify_listen,
179 NET_TRANSPORT_LOCAL,
180 N_("Register for a path and listen for changes"),
181 N_("net notify listen <path>")
183 { "trigger",
184 net_notify_trigger,
185 NET_TRANSPORT_LOCAL,
186 N_("Simulate a trigger action"),
187 N_("net notify trigger <path> <action> <filter>")
189 {NULL, NULL, 0, NULL, NULL}
192 if (c->msg_ctx == NULL) {
193 d_fprintf(stderr, "No connection to messaging, need to run "
194 "as root\n");
195 return -1;
198 return net_run_function(c, argc, argv, "net notify", func);