2 Unix SMB/CIFS implementation.
4 Send messages to other Samba daemons
6 Copyright (C) Tim Potter 2003
7 Copyright (C) Andrew Tridgell 1994-1998
8 Copyright (C) Martin Pool 2001-2002
9 Copyright (C) Simo Sorce 2002
10 Copyright (C) James Peach 2006
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/filesys.h"
28 #include "popt_common.h"
29 #include "librpc/gen_ndr/spoolss.h"
30 #include "nt_printing.h"
31 #include "printing/notify.h"
32 #include "libsmb/nmblib.h"
35 #include "../lib/util/pidfile.h"
38 #include <libunwind.h>
41 #if HAVE_LIBUNWIND_PTRACE_H
42 #include <libunwind-ptrace.h>
46 #include <sys/ptrace.h>
49 /* Default timeout value when waiting for replies (in seconds) */
51 #define DEFAULT_TIMEOUT 10
53 static int timeout
= DEFAULT_TIMEOUT
;
54 static int num_replies
; /* Used by message callback fns */
56 /* Send a message to a destination pid. Zero means broadcast smbd. */
58 static bool send_message(struct messaging_context
*msg_ctx
,
59 struct server_id pid
, int msg_type
,
60 const void *buf
, int len
)
65 if (procid_to_pid(&pid
) != 0)
66 return NT_STATUS_IS_OK(
67 messaging_send_buf(msg_ctx
, pid
, msg_type
,
68 (const uint8
*)buf
, len
));
70 ret
= message_send_all(msg_ctx
, msg_type
, buf
, len
, &n_sent
);
71 DEBUG(10,("smbcontrol/send_message: broadcast message to "
72 "%d processes\n", n_sent
));
77 static void smbcontrol_timeout(struct tevent_context
*event_ctx
,
78 struct tevent_timer
*te
,
82 bool *timed_out
= (bool *)private_data
;
87 /* Wait for one or more reply messages */
89 static void wait_replies(struct tevent_context
*ev_ctx
,
90 struct messaging_context
*msg_ctx
,
91 bool multiple_replies
)
93 struct tevent_timer
*te
;
94 bool timed_out
= False
;
96 if (!(te
= tevent_add_timer(ev_ctx
, NULL
,
97 timeval_current_ofs(timeout
, 0),
98 smbcontrol_timeout
, (void *)&timed_out
))) {
99 DEBUG(0, ("tevent_add_timer failed\n"));
105 if (num_replies
> 0 && !multiple_replies
)
107 ret
= tevent_loop_once(ev_ctx
);
114 /* Message handler callback that displays the PID and a string on stdout */
116 static void print_pid_string_cb(struct messaging_context
*msg
,
119 struct server_id pid
,
124 pidstr
= server_id_str(talloc_tos(), &pid
);
125 printf("PID %s: %.*s", pidstr
, (int)data
->length
,
126 (const char *)data
->data
);
131 /* Message handler callback that displays a string on stdout */
133 static void print_string_cb(struct messaging_context
*msg
,
136 struct server_id pid
,
139 printf("%*s", (int)data
->length
, (const char *)data
->data
);
143 /* Send no message. Useful for testing. */
145 static bool do_noop(struct tevent_context
*ev_ctx
,
146 struct messaging_context
*msg_ctx
,
147 const struct server_id pid
,
148 const int argc
, const char **argv
)
151 fprintf(stderr
, "Usage: smbcontrol <dest> noop\n");
155 /* Move along, nothing to see here */
160 /* Send a debug string */
162 static bool do_debug(struct tevent_context
*ev_ctx
,
163 struct messaging_context
*msg_ctx
,
164 const struct server_id pid
,
165 const int argc
, const char **argv
)
168 fprintf(stderr
, "Usage: smbcontrol <dest> debug "
173 return send_message(msg_ctx
, pid
, MSG_DEBUG
, argv
[1],
174 strlen(argv
[1]) + 1);
178 static bool do_idmap(struct tevent_context
*ev
,
179 struct messaging_context
*msg_ctx
,
180 const struct server_id pid
,
181 const int argc
, const char **argv
)
183 static const char* usage
= "Usage: "
184 "smbcontrol <dest> idmap <cmd> [arg]\n"
186 "\tdelete \"UID <uid>\"|\"GID <gid>\"|<sid>\n"
187 "\t\tkill \"UID <uid>\"|\"GID <gid>\"|<sid>\n";
188 const char* arg
= NULL
;
197 arglen
= strlen(arg
) + 1;
200 fprintf(stderr
, "%s", usage
);
204 if (strcmp(argv
[1], "delete") == 0) {
205 msg_type
= ID_CACHE_DELETE
;
207 else if (strcmp(argv
[1], "kill") == 0) {
208 msg_type
= ID_CACHE_KILL
;
210 else if (strcmp(argv
[1], "help") == 0) {
211 fprintf(stdout
, "%s", usage
);
215 fprintf(stderr
, "%s", usage
);
219 return send_message(msg_ctx
, pid
, msg_type
, arg
, arglen
);
223 #if defined(HAVE_LIBUNWIND_PTRACE) && defined(HAVE_LINUX_PTRACE)
225 /* Return the name of a process given it's PID. This will only work on Linux,
226 * but that's probably moot since this whole stack tracing implementatino is
227 * Linux-specific anyway.
229 static const char * procname(pid_t pid
, char * buf
, size_t bufsz
)
234 snprintf(path
, sizeof(path
), "/proc/%llu/cmdline",
235 (unsigned long long)pid
);
236 if ((fp
= fopen(path
, "r")) == NULL
) {
240 fgets(buf
, bufsz
, fp
);
246 static void print_stack_trace(pid_t pid
, int * count
)
249 unw_addr_space_t aspace
= NULL
;
258 if (ptrace(PTRACE_ATTACH
, pid
, NULL
, NULL
) < 0) {
260 "Failed to attach to process %llu: %s\n",
261 (unsigned long long)pid
, strerror(errno
));
265 /* Wait until the attach is complete. */
266 waitpid(pid
, NULL
, 0);
268 if (((pinfo
= _UPT_create(pid
)) == NULL
) ||
269 ((aspace
= unw_create_addr_space(&_UPT_accessors
, 0)) == NULL
)) {
270 /* Probably out of memory. */
272 "Unable to initialize stack unwind for process %llu\n",
273 (unsigned long long)pid
);
277 if ((ret
= unw_init_remote(&cursor
, aspace
, pinfo
))) {
279 "Unable to unwind stack for process %llu: %s\n",
280 (unsigned long long)pid
, unw_strerror(ret
));
288 if (procname(pid
, nbuf
, sizeof(nbuf
))) {
289 printf("Stack trace for process %llu (%s):\n",
290 (unsigned long long)pid
, nbuf
);
292 printf("Stack trace for process %llu:\n",
293 (unsigned long long)pid
);
296 while (unw_step(&cursor
) > 0) {
298 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
299 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
301 ret
= unw_get_proc_name(&cursor
, nbuf
, sizeof(nbuf
), &off
);
302 if (ret
!= 0 && ret
!= -UNW_ENOMEM
) {
303 snprintf(nbuf
, sizeof(nbuf
), "<unknown symbol>");
305 printf(" %s + %#llx [ip=%#llx] [sp=%#llx]\n",
306 nbuf
, (long long)off
, (long long)ip
,
314 unw_destroy_addr_space(aspace
);
321 ptrace(PTRACE_DETACH
, pid
, NULL
, NULL
);
324 static int stack_trace_connection(const struct connections_key
*key
,
325 const struct connections_data
*crec
,
328 print_stack_trace(procid_to_pid(&crec
->pid
), (int *)priv
);
333 static bool do_daemon_stack_trace(struct tevent_context
*ev_ctx
,
334 struct messaging_context
*msg_ctx
,
335 const struct server_id pid
,
336 const int argc
, const char **argv
)
342 fprintf(stderr
, "Usage: smbcontrol <dest> stacktrace\n");
346 dest
= procid_to_pid(&pid
);
349 /* It would be nice to be able to make sure that this PID is
350 * the PID of a smbd/winbind/nmbd process, not some random PID
351 * the user liked the look of. It doesn't seem like it's worth
352 * the effort at the moment, however.
354 print_stack_trace(dest
, &count
);
356 connections_forall_read(stack_trace_connection
, &count
);
362 #else /* defined(HAVE_LIBUNWIND_PTRACE) && defined(HAVE_LINUX_PTRACE) */
364 static bool do_daemon_stack_trace(struct tevent_context
*ev_ctx
,
365 struct messaging_context
*msg_ctx
,
366 const struct server_id pid
,
367 const int argc
, const char **argv
)
370 "Daemon stack tracing is not supported on this platform\n");
374 #endif /* defined(HAVE_LIBUNWIND_PTRACE) && defined(HAVE_LINUX_PTRACE) */
376 /* Inject a fault (fatal signal) into a running smbd */
378 static bool do_inject_fault(struct tevent_context
*ev_ctx
,
379 struct messaging_context
*msg_ctx
,
380 const struct server_id pid
,
381 const int argc
, const char **argv
)
384 fprintf(stderr
, "Usage: smbcontrol <dest> inject "
385 "<bus|hup|term|internal|segv>\n");
390 fprintf(stderr
, "Fault injection is only available in "
391 "developer builds\n");
393 #else /* DEVELOPER */
397 if (strcmp(argv
[1], "bus") == 0) {
399 } else if (strcmp(argv
[1], "hup") == 0) {
401 } else if (strcmp(argv
[1], "term") == 0) {
403 } else if (strcmp(argv
[1], "segv") == 0) {
405 } else if (strcmp(argv
[1], "internal") == 0) {
406 /* Force an internal error, ie. an unclean exit. */
409 fprintf(stderr
, "Unknown signal name '%s'\n", argv
[1]);
413 return send_message(msg_ctx
, pid
, MSG_SMB_INJECT_FAULT
,
416 #endif /* DEVELOPER */
419 /* Force a browser election */
421 static bool do_election(struct tevent_context
*ev_ctx
,
422 struct messaging_context
*msg_ctx
,
423 const struct server_id pid
,
424 const int argc
, const char **argv
)
427 fprintf(stderr
, "Usage: smbcontrol <dest> force-election\n");
431 return send_message(msg_ctx
, pid
, MSG_FORCE_ELECTION
, NULL
, 0);
434 /* Ping a samba daemon process */
436 static void pong_cb(struct messaging_context
*msg
,
439 struct server_id pid
,
442 char *src_string
= server_id_str(NULL
, &pid
);
443 printf("PONG from pid %s\n", src_string
);
444 TALLOC_FREE(src_string
);
448 static bool do_ping(struct tevent_context
*ev_ctx
,
449 struct messaging_context
*msg_ctx
,
450 const struct server_id pid
,
451 const int argc
, const char **argv
)
454 fprintf(stderr
, "Usage: smbcontrol <dest> ping\n");
458 /* Send a message and register our interest in a reply */
460 if (!send_message(msg_ctx
, pid
, MSG_PING
, NULL
, 0))
463 messaging_register(msg_ctx
, NULL
, MSG_PONG
, pong_cb
);
465 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
467 /* No replies were received within the timeout period */
469 if (num_replies
== 0)
470 printf("No replies received\n");
472 messaging_deregister(msg_ctx
, MSG_PONG
, NULL
);
477 /* Set profiling options */
479 static bool do_profile(struct tevent_context
*ev_ctx
,
480 struct messaging_context
*msg_ctx
,
481 const struct server_id pid
,
482 const int argc
, const char **argv
)
487 fprintf(stderr
, "Usage: smbcontrol <dest> profile "
488 "<off|count|on|flush>\n");
492 if (strcmp(argv
[1], "off") == 0) {
494 } else if (strcmp(argv
[1], "count") == 0) {
496 } else if (strcmp(argv
[1], "on") == 0) {
498 } else if (strcmp(argv
[1], "flush") == 0) {
501 fprintf(stderr
, "Unknown profile command '%s'\n", argv
[1]);
505 return send_message(msg_ctx
, pid
, MSG_PROFILE
, &v
, sizeof(int));
508 /* Return the profiling level */
510 static void profilelevel_cb(struct messaging_context
*msg_ctx
,
513 struct server_id pid
,
521 if (data
->length
!= sizeof(int)) {
522 fprintf(stderr
, "invalid message length %ld returned\n",
523 (unsigned long)data
->length
);
527 memcpy(&level
, data
->data
, sizeof(int));
540 s
= "count and time";
547 printf("Profiling %s on pid %u\n",s
,(unsigned int)procid_to_pid(&pid
));
550 static void profilelevel_rqst(struct messaging_context
*msg_ctx
,
553 struct server_id pid
,
558 /* Send back a dummy reply */
560 send_message(msg_ctx
, pid
, MSG_PROFILELEVEL
, &v
, sizeof(int));
563 static bool do_profilelevel(struct tevent_context
*ev_ctx
,
564 struct messaging_context
*msg_ctx
,
565 const struct server_id pid
,
566 const int argc
, const char **argv
)
569 fprintf(stderr
, "Usage: smbcontrol <dest> profilelevel\n");
573 /* Send a message and register our interest in a reply */
575 if (!send_message(msg_ctx
, pid
, MSG_REQ_PROFILELEVEL
, NULL
, 0))
578 messaging_register(msg_ctx
, NULL
, MSG_PROFILELEVEL
, profilelevel_cb
);
579 messaging_register(msg_ctx
, NULL
, MSG_REQ_PROFILELEVEL
,
582 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
584 /* No replies were received within the timeout period */
586 if (num_replies
== 0)
587 printf("No replies received\n");
589 messaging_deregister(msg_ctx
, MSG_PROFILE
, NULL
);
594 /* Display debug level settings */
596 static bool do_debuglevel(struct tevent_context
*ev_ctx
,
597 struct messaging_context
*msg_ctx
,
598 const struct server_id pid
,
599 const int argc
, const char **argv
)
602 fprintf(stderr
, "Usage: smbcontrol <dest> debuglevel\n");
606 /* Send a message and register our interest in a reply */
608 if (!send_message(msg_ctx
, pid
, MSG_REQ_DEBUGLEVEL
, NULL
, 0))
611 messaging_register(msg_ctx
, NULL
, MSG_DEBUGLEVEL
, print_pid_string_cb
);
613 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
615 /* No replies were received within the timeout period */
617 if (num_replies
== 0)
618 printf("No replies received\n");
620 messaging_deregister(msg_ctx
, MSG_DEBUGLEVEL
, NULL
);
625 /* Send a print notify message */
627 static bool do_printnotify(struct tevent_context
*ev_ctx
,
628 struct messaging_context
*msg_ctx
,
629 const struct server_id pid
,
630 const int argc
, const char **argv
)
634 /* Check for subcommand */
637 fprintf(stderr
, "Must specify subcommand:\n");
638 fprintf(stderr
, "\tqueuepause <printername>\n");
639 fprintf(stderr
, "\tqueueresume <printername>\n");
640 fprintf(stderr
, "\tjobpause <printername> <unix jobid>\n");
641 fprintf(stderr
, "\tjobresume <printername> <unix jobid>\n");
642 fprintf(stderr
, "\tjobdelete <printername> <unix jobid>\n");
643 fprintf(stderr
, "\tprinter <printername> <comment|port|"
644 "driver> <value>\n");
651 if (strcmp(cmd
, "queuepause") == 0) {
654 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
655 " queuepause <printername>\n");
659 notify_printer_status_byname(ev_ctx
, msg_ctx
, argv
[2],
660 PRINTER_STATUS_PAUSED
);
664 } else if (strcmp(cmd
, "queueresume") == 0) {
667 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
668 " queuereume <printername>\n");
672 notify_printer_status_byname(ev_ctx
, msg_ctx
, argv
[2],
677 } else if (strcmp(cmd
, "jobpause") == 0) {
681 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
682 " jobpause <printername> <unix-jobid>\n");
686 jobid
= atoi(argv
[3]);
688 notify_job_status_byname(
690 argv
[2], jobid
, JOB_STATUS_PAUSED
,
691 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
695 } else if (strcmp(cmd
, "jobresume") == 0) {
699 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
700 " jobpause <printername> <unix-jobid>\n");
704 jobid
= atoi(argv
[3]);
706 notify_job_status_byname(
708 argv
[2], jobid
, JOB_STATUS_QUEUED
,
709 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
713 } else if (strcmp(cmd
, "jobdelete") == 0) {
717 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
718 " jobpause <printername> <unix-jobid>\n");
722 jobid
= atoi(argv
[3]);
724 notify_job_status_byname(
726 argv
[2], jobid
, JOB_STATUS_DELETING
,
727 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
729 notify_job_status_byname(
731 argv
[2], jobid
, JOB_STATUS_DELETING
|
733 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
737 } else if (strcmp(cmd
, "printer") == 0) {
741 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify "
742 "printer <printername> <comment|port|driver> "
747 if (strcmp(argv
[3], "comment") == 0) {
748 attribute
= PRINTER_NOTIFY_FIELD_COMMENT
;
749 } else if (strcmp(argv
[3], "port") == 0) {
750 attribute
= PRINTER_NOTIFY_FIELD_PORT_NAME
;
751 } else if (strcmp(argv
[3], "driver") == 0) {
752 attribute
= PRINTER_NOTIFY_FIELD_DRIVER_NAME
;
754 fprintf(stderr
, "Invalid printer command '%s'\n",
759 notify_printer_byname(ev_ctx
, msg_ctx
, argv
[2], attribute
,
760 discard_const_p(char, argv
[4]));
765 fprintf(stderr
, "Invalid subcommand '%s'\n", cmd
);
769 print_notify_send_messages(msg_ctx
, 0);
775 static bool do_closeshare(struct tevent_context
*ev_ctx
,
776 struct messaging_context
*msg_ctx
,
777 const struct server_id pid
,
778 const int argc
, const char **argv
)
781 fprintf(stderr
, "Usage: smbcontrol <dest> close-share "
786 return send_message(msg_ctx
, pid
, MSG_SMB_FORCE_TDIS
, argv
[1],
787 strlen(argv
[1]) + 1);
790 /* Tell winbindd an IP got dropped */
792 static bool do_ip_dropped(struct tevent_context
*ev_ctx
,
793 struct messaging_context
*msg_ctx
,
794 const struct server_id pid
,
795 const int argc
, const char **argv
)
798 fprintf(stderr
, "Usage: smbcontrol <dest> ip-dropped "
803 return send_message(msg_ctx
, pid
, MSG_WINBIND_IP_DROPPED
, argv
[1],
804 strlen(argv
[1]) + 1);
807 /* force a blocking lock retry */
809 static bool do_lockretry(struct tevent_context
*ev_ctx
,
810 struct messaging_context
*msg_ctx
,
811 const struct server_id pid
,
812 const int argc
, const char **argv
)
815 fprintf(stderr
, "Usage: smbcontrol <dest> lockretry\n");
819 return send_message(msg_ctx
, pid
, MSG_SMB_UNLOCK
, NULL
, 0);
822 /* force a validation of all brl entries, including re-sends. */
824 static bool do_brl_revalidate(struct tevent_context
*ev_ctx
,
825 struct messaging_context
*msg_ctx
,
826 const struct server_id pid
,
827 const int argc
, const char **argv
)
830 fprintf(stderr
, "Usage: smbcontrol <dest> brl-revalidate\n");
834 return send_message(msg_ctx
, pid
, MSG_SMB_BRL_VALIDATE
, NULL
, 0);
837 /* Display talloc pool usage */
839 static bool do_poolusage(struct tevent_context
*ev_ctx
,
840 struct messaging_context
*msg_ctx
,
841 const struct server_id pid
,
842 const int argc
, const char **argv
)
845 fprintf(stderr
, "Usage: smbcontrol <dest> pool-usage\n");
849 messaging_register(msg_ctx
, NULL
, MSG_POOL_USAGE
, print_string_cb
);
851 /* Send a message and register our interest in a reply */
853 if (!send_message(msg_ctx
, pid
, MSG_REQ_POOL_USAGE
, NULL
, 0))
856 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
858 /* No replies were received within the timeout period */
860 if (num_replies
== 0)
861 printf("No replies received\n");
863 messaging_deregister(msg_ctx
, MSG_POOL_USAGE
, NULL
);
868 /* Perform a dmalloc mark */
870 static bool do_dmalloc_mark(struct tevent_context
*ev_ctx
,
871 struct messaging_context
*msg_ctx
,
872 const struct server_id pid
,
873 const int argc
, const char **argv
)
876 fprintf(stderr
, "Usage: smbcontrol <dest> dmalloc-mark\n");
880 return send_message(msg_ctx
, pid
, MSG_REQ_DMALLOC_MARK
, NULL
, 0);
883 /* Perform a dmalloc changed */
885 static bool do_dmalloc_changed(struct tevent_context
*ev_ctx
,
886 struct messaging_context
*msg_ctx
,
887 const struct server_id pid
,
888 const int argc
, const char **argv
)
891 fprintf(stderr
, "Usage: smbcontrol <dest> "
892 "dmalloc-log-changed\n");
896 return send_message(msg_ctx
, pid
, MSG_REQ_DMALLOC_LOG_CHANGED
,
900 /* Shutdown a server process */
902 static bool do_shutdown(struct tevent_context
*ev_ctx
,
903 struct messaging_context
*msg_ctx
,
904 const struct server_id pid
,
905 const int argc
, const char **argv
)
908 fprintf(stderr
, "Usage: smbcontrol <dest> shutdown\n");
912 return send_message(msg_ctx
, pid
, MSG_SHUTDOWN
, NULL
, 0);
915 /* Notify a driver upgrade */
917 static bool do_drvupgrade(struct tevent_context
*ev_ctx
,
918 struct messaging_context
*msg_ctx
,
919 const struct server_id pid
,
920 const int argc
, const char **argv
)
923 fprintf(stderr
, "Usage: smbcontrol <dest> drvupgrade "
928 return send_message(msg_ctx
, pid
, MSG_DEBUG
, argv
[1],
929 strlen(argv
[1]) + 1);
932 static bool do_winbind_online(struct tevent_context
*ev_ctx
,
933 struct messaging_context
*msg_ctx
,
934 const struct server_id pid
,
935 const int argc
, const char **argv
)
940 fprintf(stderr
, "Usage: smbcontrol winbindd online\n");
944 /* Remove the entry in the winbindd_cache tdb to tell a later
945 starting winbindd that we're online. */
947 tdb
= tdb_open_log(state_path("winbindd_cache.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600);
949 fprintf(stderr
, "Cannot open the tdb %s for writing.\n",
950 state_path("winbindd_cache.tdb"));
954 tdb_delete_bystring(tdb
, "WINBINDD_OFFLINE");
957 return send_message(msg_ctx
, pid
, MSG_WINBIND_ONLINE
, NULL
, 0);
960 static bool do_winbind_offline(struct tevent_context
*ev_ctx
,
961 struct messaging_context
*msg_ctx
,
962 const struct server_id pid
,
963 const int argc
, const char **argv
)
970 fprintf(stderr
, "Usage: smbcontrol winbindd offline\n");
974 /* Create an entry in the winbindd_cache tdb to tell a later
975 starting winbindd that we're offline. We may actually create
978 tdb
= tdb_open_log(state_path("winbindd_cache.tdb"),
979 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE
,
980 TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
/* TDB_CLEAR_IF_FIRST */,
981 O_RDWR
|O_CREAT
, 0600);
984 fprintf(stderr
, "Cannot open the tdb %s for writing.\n",
985 state_path("winbindd_cache.tdb"));
989 /* There's a potential race condition that if a child
990 winbindd detects a domain is online at the same time
991 we're trying to tell it to go offline that it might
992 delete the record we add between us adding it and
993 sending the message. Minimize this by retrying up to
996 for (retry
= 0; retry
< 5; retry
++) {
1002 SIVAL(buf
, 0, time(NULL
));
1006 tdb_store_bystring(tdb
, "WINBINDD_OFFLINE", d
, TDB_INSERT
);
1008 ret
= send_message(msg_ctx
, pid
, MSG_WINBIND_OFFLINE
,
1011 /* Check that the entry "WINBINDD_OFFLINE" still exists. */
1012 d
= tdb_fetch_bystring( tdb
, "WINBINDD_OFFLINE" );
1014 if (!d
.dptr
|| d
.dsize
!= 4) {
1016 DEBUG(10,("do_winbind_offline: offline state not set - retrying.\n"));
1027 static bool do_winbind_onlinestatus(struct tevent_context
*ev_ctx
,
1028 struct messaging_context
*msg_ctx
,
1029 const struct server_id pid
,
1030 const int argc
, const char **argv
)
1032 struct server_id myid
;
1034 myid
= messaging_server_id(msg_ctx
);
1037 fprintf(stderr
, "Usage: smbcontrol winbindd onlinestatus\n");
1041 messaging_register(msg_ctx
, NULL
, MSG_WINBIND_ONLINESTATUS
,
1042 print_pid_string_cb
);
1044 if (!send_message(msg_ctx
, pid
, MSG_WINBIND_ONLINESTATUS
, &myid
,
1048 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
1050 /* No replies were received within the timeout period */
1052 if (num_replies
== 0)
1053 printf("No replies received\n");
1055 messaging_deregister(msg_ctx
, MSG_WINBIND_ONLINESTATUS
, NULL
);
1060 static bool do_dump_event_list(struct tevent_context
*ev_ctx
,
1061 struct messaging_context
*msg_ctx
,
1062 const struct server_id pid
,
1063 const int argc
, const char **argv
)
1066 fprintf(stderr
, "Usage: smbcontrol <dest> dump-event-list\n");
1070 return send_message(msg_ctx
, pid
, MSG_DUMP_EVENT_LIST
, NULL
, 0);
1073 static bool do_winbind_dump_domain_list(struct tevent_context
*ev_ctx
,
1074 struct messaging_context
*msg_ctx
,
1075 const struct server_id pid
,
1076 const int argc
, const char **argv
)
1078 const char *domain
= NULL
;
1080 struct server_id myid
;
1081 uint8_t *buf
= NULL
;
1084 myid
= messaging_server_id(msg_ctx
);
1086 if (argc
< 1 || argc
> 2) {
1087 fprintf(stderr
, "Usage: smbcontrol <dest> dump-domain-list "
1094 domain_len
= strlen(argv
[1]) + 1;
1097 messaging_register(msg_ctx
, NULL
, MSG_WINBIND_DUMP_DOMAIN_LIST
,
1098 print_pid_string_cb
);
1100 buf_len
= sizeof(myid
)+domain_len
;
1101 buf
= SMB_MALLOC_ARRAY(uint8_t, buf_len
);
1106 memcpy(buf
, &myid
, sizeof(myid
));
1107 memcpy(&buf
[sizeof(myid
)], domain
, domain_len
);
1109 if (!send_message(msg_ctx
, pid
, MSG_WINBIND_DUMP_DOMAIN_LIST
,
1116 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
1118 /* No replies were received within the timeout period */
1121 if (num_replies
== 0) {
1122 printf("No replies received\n");
1125 messaging_deregister(msg_ctx
, MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1130 static void winbind_validate_cache_cb(struct messaging_context
*msg
,
1133 struct server_id pid
,
1136 char *src_string
= server_id_str(NULL
, &pid
);
1137 printf("Winbindd cache is %svalid. (answer from pid %s)\n",
1138 (*(data
->data
) == 0 ? "" : "NOT "), src_string
);
1139 TALLOC_FREE(src_string
);
1143 static bool do_winbind_validate_cache(struct tevent_context
*ev_ctx
,
1144 struct messaging_context
*msg_ctx
,
1145 const struct server_id pid
,
1146 const int argc
, const char **argv
)
1148 struct server_id myid
;
1150 myid
= messaging_server_id(msg_ctx
);
1153 fprintf(stderr
, "Usage: smbcontrol winbindd validate-cache\n");
1157 messaging_register(msg_ctx
, NULL
, MSG_WINBIND_VALIDATE_CACHE
,
1158 winbind_validate_cache_cb
);
1160 if (!send_message(msg_ctx
, pid
, MSG_WINBIND_VALIDATE_CACHE
, &myid
,
1165 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
1167 if (num_replies
== 0) {
1168 printf("No replies received\n");
1171 messaging_deregister(msg_ctx
, MSG_WINBIND_VALIDATE_CACHE
, NULL
);
1176 static bool do_reload_config(struct tevent_context
*ev_ctx
,
1177 struct messaging_context
*msg_ctx
,
1178 const struct server_id pid
,
1179 const int argc
, const char **argv
)
1182 fprintf(stderr
, "Usage: smbcontrol <dest> reload-config\n");
1186 return send_message(msg_ctx
, pid
, MSG_SMB_CONF_UPDATED
, NULL
, 0);
1189 static bool do_reload_printers(struct tevent_context
*ev_ctx
,
1190 struct messaging_context
*msg_ctx
,
1191 const struct server_id pid
,
1192 const int argc
, const char **argv
)
1195 fprintf(stderr
, "Usage: smbcontrol <dest> reload-printers\n");
1199 return send_message(msg_ctx
, pid
, MSG_PRINTER_PCAP
, NULL
, 0);
1202 static void my_make_nmb_name( struct nmb_name
*n
, const char *name
, int type
)
1205 memset( (char *)n
, '\0', sizeof(struct nmb_name
) );
1206 fstrcpy(unix_name
, name
);
1207 (void)strupper_m(unix_name
);
1208 push_ascii(n
->name
, unix_name
, sizeof(n
->name
), STR_TERMINATE
);
1209 n
->name_type
= (unsigned int)type
& 0xFF;
1210 push_ascii(n
->scope
, lp_netbios_scope(), 64, STR_TERMINATE
);
1213 static bool do_nodestatus(struct tevent_context
*ev_ctx
,
1214 struct messaging_context
*msg_ctx
,
1215 const struct server_id pid
,
1216 const int argc
, const char **argv
)
1218 struct packet_struct p
;
1221 fprintf(stderr
, "Usage: smbcontrol nmbd nodestatus <ip>\n");
1227 p
.ip
= interpret_addr2(argv
[1]);
1229 p
.packet_type
= NMB_PACKET
;
1231 p
.packet
.nmb
.header
.name_trn_id
= 10;
1232 p
.packet
.nmb
.header
.opcode
= 0;
1233 p
.packet
.nmb
.header
.response
= False
;
1234 p
.packet
.nmb
.header
.nm_flags
.bcast
= False
;
1235 p
.packet
.nmb
.header
.nm_flags
.recursion_available
= False
;
1236 p
.packet
.nmb
.header
.nm_flags
.recursion_desired
= False
;
1237 p
.packet
.nmb
.header
.nm_flags
.trunc
= False
;
1238 p
.packet
.nmb
.header
.nm_flags
.authoritative
= False
;
1239 p
.packet
.nmb
.header
.rcode
= 0;
1240 p
.packet
.nmb
.header
.qdcount
= 1;
1241 p
.packet
.nmb
.header
.ancount
= 0;
1242 p
.packet
.nmb
.header
.nscount
= 0;
1243 p
.packet
.nmb
.header
.arcount
= 0;
1244 my_make_nmb_name(&p
.packet
.nmb
.question
.question_name
, "*", 0x00);
1245 p
.packet
.nmb
.question
.question_type
= 0x21;
1246 p
.packet
.nmb
.question
.question_class
= 0x1;
1248 return send_message(msg_ctx
, pid
, MSG_SEND_PACKET
, &p
, sizeof(p
));
1251 static bool do_notify_cleanup(struct tevent_context
*ev_ctx
,
1252 struct messaging_context
*msg_ctx
,
1253 const struct server_id pid
,
1254 const int argc
, const char **argv
)
1257 fprintf(stderr
, "Usage: smbcontrol smbd notify-cleanup\n");
1260 return send_message(msg_ctx
, pid
, MSG_SMB_NOTIFY_CLEANUP
, NULL
, 0);
1263 /* A list of message type supported */
1265 static const struct {
1266 const char *name
; /* Option name */
1267 bool (*fn
)(struct tevent_context
*ev_ctx
,
1268 struct messaging_context
*msg_ctx
,
1269 const struct server_id pid
,
1270 const int argc
, const char **argv
);
1271 const char *help
; /* Short help text */
1273 { "debug", do_debug
, "Set debuglevel" },
1274 { "idmap", do_idmap
, "Manipulate idmap cache" },
1275 { "force-election", do_election
,
1276 "Force a browse election" },
1277 { "ping", do_ping
, "Elicit a response" },
1278 { "profile", do_profile
, "" },
1279 { "inject", do_inject_fault
,
1280 "Inject a fatal signal into a running smbd"},
1281 { "stacktrace", do_daemon_stack_trace
,
1282 "Display a stack trace of a daemon" },
1283 { "profilelevel", do_profilelevel
, "" },
1284 { "debuglevel", do_debuglevel
, "Display current debuglevels" },
1285 { "printnotify", do_printnotify
, "Send a print notify message" },
1286 { "close-share", do_closeshare
, "Forcibly disconnect a share" },
1287 { "ip-dropped", do_ip_dropped
, "Tell winbind that an IP got dropped" },
1288 { "lockretry", do_lockretry
, "Force a blocking lock retry" },
1289 { "brl-revalidate", do_brl_revalidate
, "Revalidate all brl entries" },
1290 { "pool-usage", do_poolusage
, "Display talloc memory usage" },
1291 { "dmalloc-mark", do_dmalloc_mark
, "" },
1292 { "dmalloc-log-changed", do_dmalloc_changed
, "" },
1293 { "shutdown", do_shutdown
, "Shut down daemon" },
1294 { "drvupgrade", do_drvupgrade
, "Notify a printer driver has changed" },
1295 { "reload-config", do_reload_config
, "Force smbd or winbindd to reload config file"},
1296 { "reload-printers", do_reload_printers
, "Force smbd to reload printers"},
1297 { "nodestatus", do_nodestatus
, "Ask nmbd to do a node status request"},
1298 { "online", do_winbind_online
, "Ask winbind to go into online state"},
1299 { "offline", do_winbind_offline
, "Ask winbind to go into offline state"},
1300 { "onlinestatus", do_winbind_onlinestatus
, "Request winbind online status"},
1301 { "dump-event-list", do_dump_event_list
, "Dump event list"},
1302 { "validate-cache" , do_winbind_validate_cache
,
1303 "Validate winbind's credential cache" },
1304 { "dump-domain-list", do_winbind_dump_domain_list
, "Dump winbind domain list"},
1305 { "notify-cleanup", do_notify_cleanup
},
1306 { "noop", do_noop
, "Do nothing" },
1310 /* Display usage information */
1312 static void usage(poptContext pc
)
1316 poptPrintHelp(pc
, stderr
, 0);
1318 fprintf(stderr
, "\n");
1319 fprintf(stderr
, "<destination> is one of \"nmbd\", \"smbd\", \"winbindd\" or a "
1322 fprintf(stderr
, "\n");
1323 fprintf(stderr
, "<message-type> is one of:\n");
1325 for (i
= 0; msg_types
[i
].name
; i
++)
1326 fprintf(stderr
, "\t%-30s%s\n", msg_types
[i
].name
,
1329 fprintf(stderr
, "\n");
1334 /* Return the pid number for a string destination */
1336 static struct server_id
parse_dest(struct messaging_context
*msg
,
1339 struct server_id result
= {-1};
1342 /* Zero is a special return value for broadcast to all processes */
1344 if (strequal(dest
, "all")) {
1345 return interpret_pid(MSG_BROADCAST_PID_STR
);
1348 /* Try self - useful for testing */
1350 if (strequal(dest
, "self")) {
1351 return messaging_server_id(msg
);
1354 /* Fix winbind typo. */
1355 if (strequal(dest
, "winbind")) {
1359 /* Check for numeric pid number */
1360 result
= interpret_pid(dest
);
1362 /* Zero isn't valid if not "all". */
1363 if (result
.pid
&& procid_valid(&result
)) {
1367 /* Look up other destinations in pidfile directory */
1369 if ((pid
= pidfile_pid(lp_piddir(), dest
)) != 0) {
1370 return pid_to_procid(pid
);
1373 fprintf(stderr
,"Can't find pid for destination '%s'\n", dest
);
1378 /* Execute smbcontrol command */
1380 static bool do_command(struct tevent_context
*ev_ctx
,
1381 struct messaging_context
*msg_ctx
,
1382 int argc
, const char **argv
)
1384 const char *dest
= argv
[0], *command
= argv
[1];
1385 struct server_id pid
;
1388 /* Check destination */
1390 pid
= parse_dest(msg_ctx
, dest
);
1391 if (!procid_valid(&pid
)) {
1397 for (i
= 0; msg_types
[i
].name
; i
++) {
1398 if (strequal(command
, msg_types
[i
].name
))
1399 return msg_types
[i
].fn(ev_ctx
, msg_ctx
, pid
,
1400 argc
- 1, argv
+ 1);
1403 fprintf(stderr
, "smbcontrol: unknown command '%s'\n", command
);
1408 static void smbcontrol_help(poptContext pc
,
1409 enum poptCallbackReason preason
,
1410 struct poptOption
* poption
,
1414 if (poption
->shortName
!= '?') {
1415 poptPrintUsage(pc
, stdout
, 0);
1423 struct poptOption help_options
[] = {
1424 { NULL
, '\0', POPT_ARG_CALLBACK
, (void *)&smbcontrol_help
, '\0',
1426 { "help", '?', 0, NULL
, '?', "Show this help message", NULL
},
1427 { "usage", '\0', 0, NULL
, 'u', "Display brief usage message", NULL
},
1433 int main(int argc
, const char **argv
)
1437 struct tevent_context
*evt_ctx
;
1438 struct messaging_context
*msg_ctx
;
1440 static struct poptOption long_options
[] = {
1442 { NULL
, '\0', POPT_ARG_INCLUDE_TABLE
, help_options
,
1443 0, "Help options:", NULL
},
1444 { "timeout", 't', POPT_ARG_INT
, &timeout
, 't',
1445 "Set timeout value in seconds", "TIMEOUT" },
1450 TALLOC_CTX
*frame
= talloc_stackframe();
1455 setup_logging(argv
[0], DEBUG_STDOUT
);
1457 /* Parse command line arguments using popt */
1459 pc
= poptGetContext(
1460 "smbcontrol", argc
, (const char **)argv
, long_options
, 0);
1462 poptSetOtherOptionHelp(pc
, "[OPTION...] <destination> <message-type> "
1468 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1470 case 't': /* --timeout */
1473 fprintf(stderr
, "Invalid option\n");
1474 poptPrintHelp(pc
, stderr
, 0);
1479 /* We should now have the remaining command line arguments in
1480 argv. The argc parameter should have been decremented to the
1481 correct value in the above switch statement. */
1483 argv
= (const char **)poptGetArgs(pc
);
1486 while (argv
[argc
] != NULL
) {
1494 lp_load_global(get_dyn_CONFIGFILE());
1496 /* Need to invert sense of return code -- samba
1497 * routines mostly return True==1 for success, but
1500 if (!(evt_ctx
= tevent_context_init(NULL
)) ||
1501 !(msg_ctx
= messaging_init(NULL
, evt_ctx
))) {
1502 fprintf(stderr
, "could not init messaging context\n");
1507 ret
= !do_command(evt_ctx
, msg_ctx
, argc
, argv
);