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_server(const struct server_id
*id
,
328 if (id
->vnn
== get_my_vnn()) {
329 print_stack_trace(procid_to_pid(&id
->pid
), (int *)priv
);
334 static bool do_daemon_stack_trace(struct tevent_context
*ev_ctx
,
335 struct messaging_context
*msg_ctx
,
336 const struct server_id pid
,
337 const int argc
, const char **argv
)
343 fprintf(stderr
, "Usage: smbcontrol <dest> stacktrace\n");
347 dest
= procid_to_pid(&pid
);
350 /* It would be nice to be able to make sure that this PID is
351 * the PID of a smbd/winbind/nmbd process, not some random PID
352 * the user liked the look of. It doesn't seem like it's worth
353 * the effort at the moment, however.
355 print_stack_trace(dest
, &count
);
357 serverid_traverse_read(stack_trace_server
, &count
);
363 #else /* defined(HAVE_LIBUNWIND_PTRACE) && defined(HAVE_LINUX_PTRACE) */
365 static bool do_daemon_stack_trace(struct tevent_context
*ev_ctx
,
366 struct messaging_context
*msg_ctx
,
367 const struct server_id pid
,
368 const int argc
, const char **argv
)
371 "Daemon stack tracing is not supported on this platform\n");
375 #endif /* defined(HAVE_LIBUNWIND_PTRACE) && defined(HAVE_LINUX_PTRACE) */
377 /* Inject a fault (fatal signal) into a running smbd */
379 static bool do_inject_fault(struct tevent_context
*ev_ctx
,
380 struct messaging_context
*msg_ctx
,
381 const struct server_id pid
,
382 const int argc
, const char **argv
)
385 fprintf(stderr
, "Usage: smbcontrol <dest> inject "
386 "<bus|hup|term|internal|segv>\n");
391 fprintf(stderr
, "Fault injection is only available in "
392 "developer builds\n");
394 #else /* DEVELOPER */
398 if (strcmp(argv
[1], "bus") == 0) {
400 } else if (strcmp(argv
[1], "hup") == 0) {
402 } else if (strcmp(argv
[1], "term") == 0) {
404 } else if (strcmp(argv
[1], "segv") == 0) {
406 } else if (strcmp(argv
[1], "internal") == 0) {
407 /* Force an internal error, ie. an unclean exit. */
410 fprintf(stderr
, "Unknown signal name '%s'\n", argv
[1]);
414 return send_message(msg_ctx
, pid
, MSG_SMB_INJECT_FAULT
,
417 #endif /* DEVELOPER */
420 /* Force a browser election */
422 static bool do_election(struct tevent_context
*ev_ctx
,
423 struct messaging_context
*msg_ctx
,
424 const struct server_id pid
,
425 const int argc
, const char **argv
)
428 fprintf(stderr
, "Usage: smbcontrol <dest> force-election\n");
432 return send_message(msg_ctx
, pid
, MSG_FORCE_ELECTION
, NULL
, 0);
435 /* Ping a samba daemon process */
437 static void pong_cb(struct messaging_context
*msg
,
440 struct server_id pid
,
443 char *src_string
= server_id_str(NULL
, &pid
);
444 printf("PONG from pid %s\n", src_string
);
445 TALLOC_FREE(src_string
);
449 static bool do_ping(struct tevent_context
*ev_ctx
,
450 struct messaging_context
*msg_ctx
,
451 const struct server_id pid
,
452 const int argc
, const char **argv
)
455 fprintf(stderr
, "Usage: smbcontrol <dest> ping\n");
459 /* Send a message and register our interest in a reply */
461 if (!send_message(msg_ctx
, pid
, MSG_PING
, NULL
, 0))
464 messaging_register(msg_ctx
, NULL
, MSG_PONG
, pong_cb
);
466 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
468 /* No replies were received within the timeout period */
470 if (num_replies
== 0)
471 printf("No replies received\n");
473 messaging_deregister(msg_ctx
, MSG_PONG
, NULL
);
478 /* Set profiling options */
480 static bool do_profile(struct tevent_context
*ev_ctx
,
481 struct messaging_context
*msg_ctx
,
482 const struct server_id pid
,
483 const int argc
, const char **argv
)
488 fprintf(stderr
, "Usage: smbcontrol <dest> profile "
489 "<off|count|on|flush>\n");
493 if (strcmp(argv
[1], "off") == 0) {
495 } else if (strcmp(argv
[1], "count") == 0) {
497 } else if (strcmp(argv
[1], "on") == 0) {
499 } else if (strcmp(argv
[1], "flush") == 0) {
502 fprintf(stderr
, "Unknown profile command '%s'\n", argv
[1]);
506 return send_message(msg_ctx
, pid
, MSG_PROFILE
, &v
, sizeof(int));
509 /* Return the profiling level */
511 static void profilelevel_cb(struct messaging_context
*msg_ctx
,
514 struct server_id pid
,
522 if (data
->length
!= sizeof(int)) {
523 fprintf(stderr
, "invalid message length %ld returned\n",
524 (unsigned long)data
->length
);
528 memcpy(&level
, data
->data
, sizeof(int));
541 s
= "count and time";
548 printf("Profiling %s on pid %u\n",s
,(unsigned int)procid_to_pid(&pid
));
551 static void profilelevel_rqst(struct messaging_context
*msg_ctx
,
554 struct server_id pid
,
559 /* Send back a dummy reply */
561 send_message(msg_ctx
, pid
, MSG_PROFILELEVEL
, &v
, sizeof(int));
564 static bool do_profilelevel(struct tevent_context
*ev_ctx
,
565 struct messaging_context
*msg_ctx
,
566 const struct server_id pid
,
567 const int argc
, const char **argv
)
570 fprintf(stderr
, "Usage: smbcontrol <dest> profilelevel\n");
574 /* Send a message and register our interest in a reply */
576 if (!send_message(msg_ctx
, pid
, MSG_REQ_PROFILELEVEL
, NULL
, 0))
579 messaging_register(msg_ctx
, NULL
, MSG_PROFILELEVEL
, profilelevel_cb
);
580 messaging_register(msg_ctx
, NULL
, MSG_REQ_PROFILELEVEL
,
583 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
585 /* No replies were received within the timeout period */
587 if (num_replies
== 0)
588 printf("No replies received\n");
590 messaging_deregister(msg_ctx
, MSG_PROFILE
, NULL
);
595 /* Display debug level settings */
597 static bool do_debuglevel(struct tevent_context
*ev_ctx
,
598 struct messaging_context
*msg_ctx
,
599 const struct server_id pid
,
600 const int argc
, const char **argv
)
603 fprintf(stderr
, "Usage: smbcontrol <dest> debuglevel\n");
607 /* Send a message and register our interest in a reply */
609 if (!send_message(msg_ctx
, pid
, MSG_REQ_DEBUGLEVEL
, NULL
, 0))
612 messaging_register(msg_ctx
, NULL
, MSG_DEBUGLEVEL
, print_pid_string_cb
);
614 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
616 /* No replies were received within the timeout period */
618 if (num_replies
== 0)
619 printf("No replies received\n");
621 messaging_deregister(msg_ctx
, MSG_DEBUGLEVEL
, NULL
);
626 /* Send a print notify message */
628 static bool do_printnotify(struct tevent_context
*ev_ctx
,
629 struct messaging_context
*msg_ctx
,
630 const struct server_id pid
,
631 const int argc
, const char **argv
)
635 /* Check for subcommand */
638 fprintf(stderr
, "Must specify subcommand:\n");
639 fprintf(stderr
, "\tqueuepause <printername>\n");
640 fprintf(stderr
, "\tqueueresume <printername>\n");
641 fprintf(stderr
, "\tjobpause <printername> <unix jobid>\n");
642 fprintf(stderr
, "\tjobresume <printername> <unix jobid>\n");
643 fprintf(stderr
, "\tjobdelete <printername> <unix jobid>\n");
644 fprintf(stderr
, "\tprinter <printername> <comment|port|"
645 "driver> <value>\n");
652 if (strcmp(cmd
, "queuepause") == 0) {
655 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
656 " queuepause <printername>\n");
660 notify_printer_status_byname(ev_ctx
, msg_ctx
, argv
[2],
661 PRINTER_STATUS_PAUSED
);
665 } else if (strcmp(cmd
, "queueresume") == 0) {
668 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
669 " queuereume <printername>\n");
673 notify_printer_status_byname(ev_ctx
, msg_ctx
, argv
[2],
678 } else if (strcmp(cmd
, "jobpause") == 0) {
682 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
683 " jobpause <printername> <unix-jobid>\n");
687 jobid
= atoi(argv
[3]);
689 notify_job_status_byname(
691 argv
[2], jobid
, JOB_STATUS_PAUSED
,
692 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
696 } else if (strcmp(cmd
, "jobresume") == 0) {
700 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
701 " jobpause <printername> <unix-jobid>\n");
705 jobid
= atoi(argv
[3]);
707 notify_job_status_byname(
709 argv
[2], jobid
, JOB_STATUS_QUEUED
,
710 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
714 } else if (strcmp(cmd
, "jobdelete") == 0) {
718 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify"
719 " jobpause <printername> <unix-jobid>\n");
723 jobid
= atoi(argv
[3]);
725 notify_job_status_byname(
727 argv
[2], jobid
, JOB_STATUS_DELETING
,
728 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
730 notify_job_status_byname(
732 argv
[2], jobid
, JOB_STATUS_DELETING
|
734 SPOOLSS_NOTIFY_MSG_UNIX_JOBID
);
738 } else if (strcmp(cmd
, "printer") == 0) {
742 fprintf(stderr
, "Usage: smbcontrol <dest> printnotify "
743 "printer <printername> <comment|port|driver> "
748 if (strcmp(argv
[3], "comment") == 0) {
749 attribute
= PRINTER_NOTIFY_FIELD_COMMENT
;
750 } else if (strcmp(argv
[3], "port") == 0) {
751 attribute
= PRINTER_NOTIFY_FIELD_PORT_NAME
;
752 } else if (strcmp(argv
[3], "driver") == 0) {
753 attribute
= PRINTER_NOTIFY_FIELD_DRIVER_NAME
;
755 fprintf(stderr
, "Invalid printer command '%s'\n",
760 notify_printer_byname(ev_ctx
, msg_ctx
, argv
[2], attribute
,
761 discard_const_p(char, argv
[4]));
766 fprintf(stderr
, "Invalid subcommand '%s'\n", cmd
);
770 print_notify_send_messages(msg_ctx
, 0);
776 static bool do_closeshare(struct tevent_context
*ev_ctx
,
777 struct messaging_context
*msg_ctx
,
778 const struct server_id pid
,
779 const int argc
, const char **argv
)
782 fprintf(stderr
, "Usage: smbcontrol <dest> close-share "
787 return send_message(msg_ctx
, pid
, MSG_SMB_FORCE_TDIS
, argv
[1],
788 strlen(argv
[1]) + 1);
791 /* Tell winbindd an IP got dropped */
793 static bool do_ip_dropped(struct tevent_context
*ev_ctx
,
794 struct messaging_context
*msg_ctx
,
795 const struct server_id pid
,
796 const int argc
, const char **argv
)
799 fprintf(stderr
, "Usage: smbcontrol <dest> ip-dropped "
804 return send_message(msg_ctx
, pid
, MSG_WINBIND_IP_DROPPED
, argv
[1],
805 strlen(argv
[1]) + 1);
808 /* force a blocking lock retry */
810 static bool do_lockretry(struct tevent_context
*ev_ctx
,
811 struct messaging_context
*msg_ctx
,
812 const struct server_id pid
,
813 const int argc
, const char **argv
)
816 fprintf(stderr
, "Usage: smbcontrol <dest> lockretry\n");
820 return send_message(msg_ctx
, pid
, MSG_SMB_UNLOCK
, NULL
, 0);
823 /* force a validation of all brl entries, including re-sends. */
825 static bool do_brl_revalidate(struct tevent_context
*ev_ctx
,
826 struct messaging_context
*msg_ctx
,
827 const struct server_id pid
,
828 const int argc
, const char **argv
)
831 fprintf(stderr
, "Usage: smbcontrol <dest> brl-revalidate\n");
835 return send_message(msg_ctx
, pid
, MSG_SMB_BRL_VALIDATE
, NULL
, 0);
838 /* Display talloc pool usage */
840 static bool do_poolusage(struct tevent_context
*ev_ctx
,
841 struct messaging_context
*msg_ctx
,
842 const struct server_id pid
,
843 const int argc
, const char **argv
)
846 fprintf(stderr
, "Usage: smbcontrol <dest> pool-usage\n");
850 messaging_register(msg_ctx
, NULL
, MSG_POOL_USAGE
, print_string_cb
);
852 /* Send a message and register our interest in a reply */
854 if (!send_message(msg_ctx
, pid
, MSG_REQ_POOL_USAGE
, NULL
, 0))
857 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
859 /* No replies were received within the timeout period */
861 if (num_replies
== 0)
862 printf("No replies received\n");
864 messaging_deregister(msg_ctx
, MSG_POOL_USAGE
, NULL
);
869 /* Perform a dmalloc mark */
871 static bool do_dmalloc_mark(struct tevent_context
*ev_ctx
,
872 struct messaging_context
*msg_ctx
,
873 const struct server_id pid
,
874 const int argc
, const char **argv
)
877 fprintf(stderr
, "Usage: smbcontrol <dest> dmalloc-mark\n");
881 return send_message(msg_ctx
, pid
, MSG_REQ_DMALLOC_MARK
, NULL
, 0);
884 /* Perform a dmalloc changed */
886 static bool do_dmalloc_changed(struct tevent_context
*ev_ctx
,
887 struct messaging_context
*msg_ctx
,
888 const struct server_id pid
,
889 const int argc
, const char **argv
)
892 fprintf(stderr
, "Usage: smbcontrol <dest> "
893 "dmalloc-log-changed\n");
897 return send_message(msg_ctx
, pid
, MSG_REQ_DMALLOC_LOG_CHANGED
,
901 /* Shutdown a server process */
903 static bool do_shutdown(struct tevent_context
*ev_ctx
,
904 struct messaging_context
*msg_ctx
,
905 const struct server_id pid
,
906 const int argc
, const char **argv
)
909 fprintf(stderr
, "Usage: smbcontrol <dest> shutdown\n");
913 return send_message(msg_ctx
, pid
, MSG_SHUTDOWN
, NULL
, 0);
916 /* Notify a driver upgrade */
918 static bool do_drvupgrade(struct tevent_context
*ev_ctx
,
919 struct messaging_context
*msg_ctx
,
920 const struct server_id pid
,
921 const int argc
, const char **argv
)
924 fprintf(stderr
, "Usage: smbcontrol <dest> drvupgrade "
929 return send_message(msg_ctx
, pid
, MSG_PRINTER_DRVUPGRADE
, argv
[1],
930 strlen(argv
[1]) + 1);
933 static bool do_winbind_online(struct tevent_context
*ev_ctx
,
934 struct messaging_context
*msg_ctx
,
935 const struct server_id pid
,
936 const int argc
, const char **argv
)
941 fprintf(stderr
, "Usage: smbcontrol winbindd online\n");
945 /* Remove the entry in the winbindd_cache tdb to tell a later
946 starting winbindd that we're online. */
948 tdb
= tdb_open_log(state_path("winbindd_cache.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600);
950 fprintf(stderr
, "Cannot open the tdb %s for writing.\n",
951 state_path("winbindd_cache.tdb"));
955 tdb_delete_bystring(tdb
, "WINBINDD_OFFLINE");
958 return send_message(msg_ctx
, pid
, MSG_WINBIND_ONLINE
, NULL
, 0);
961 static bool do_winbind_offline(struct tevent_context
*ev_ctx
,
962 struct messaging_context
*msg_ctx
,
963 const struct server_id pid
,
964 const int argc
, const char **argv
)
971 fprintf(stderr
, "Usage: smbcontrol winbindd offline\n");
975 /* Create an entry in the winbindd_cache tdb to tell a later
976 starting winbindd that we're offline. We may actually create
979 tdb
= tdb_open_log(state_path("winbindd_cache.tdb"),
980 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE
,
981 TDB_DEFAULT
|TDB_INCOMPATIBLE_HASH
/* TDB_CLEAR_IF_FIRST */,
982 O_RDWR
|O_CREAT
, 0600);
985 fprintf(stderr
, "Cannot open the tdb %s for writing.\n",
986 state_path("winbindd_cache.tdb"));
990 /* There's a potential race condition that if a child
991 winbindd detects a domain is online at the same time
992 we're trying to tell it to go offline that it might
993 delete the record we add between us adding it and
994 sending the message. Minimize this by retrying up to
997 for (retry
= 0; retry
< 5; retry
++) {
1003 SIVAL(buf
, 0, time(NULL
));
1007 tdb_store_bystring(tdb
, "WINBINDD_OFFLINE", d
, TDB_INSERT
);
1009 ret
= send_message(msg_ctx
, pid
, MSG_WINBIND_OFFLINE
,
1012 /* Check that the entry "WINBINDD_OFFLINE" still exists. */
1013 d
= tdb_fetch_bystring( tdb
, "WINBINDD_OFFLINE" );
1015 if (!d
.dptr
|| d
.dsize
!= 4) {
1017 DEBUG(10,("do_winbind_offline: offline state not set - retrying.\n"));
1028 static bool do_winbind_onlinestatus(struct tevent_context
*ev_ctx
,
1029 struct messaging_context
*msg_ctx
,
1030 const struct server_id pid
,
1031 const int argc
, const char **argv
)
1033 struct server_id myid
;
1035 myid
= messaging_server_id(msg_ctx
);
1038 fprintf(stderr
, "Usage: smbcontrol winbindd onlinestatus\n");
1042 messaging_register(msg_ctx
, NULL
, MSG_WINBIND_ONLINESTATUS
,
1043 print_pid_string_cb
);
1045 if (!send_message(msg_ctx
, pid
, MSG_WINBIND_ONLINESTATUS
, &myid
,
1049 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
1051 /* No replies were received within the timeout period */
1053 if (num_replies
== 0)
1054 printf("No replies received\n");
1056 messaging_deregister(msg_ctx
, MSG_WINBIND_ONLINESTATUS
, NULL
);
1061 static bool do_dump_event_list(struct tevent_context
*ev_ctx
,
1062 struct messaging_context
*msg_ctx
,
1063 const struct server_id pid
,
1064 const int argc
, const char **argv
)
1067 fprintf(stderr
, "Usage: smbcontrol <dest> dump-event-list\n");
1071 return send_message(msg_ctx
, pid
, MSG_DUMP_EVENT_LIST
, NULL
, 0);
1074 static bool do_winbind_dump_domain_list(struct tevent_context
*ev_ctx
,
1075 struct messaging_context
*msg_ctx
,
1076 const struct server_id pid
,
1077 const int argc
, const char **argv
)
1079 const char *domain
= NULL
;
1081 struct server_id myid
;
1082 uint8_t *buf
= NULL
;
1085 myid
= messaging_server_id(msg_ctx
);
1087 if (argc
< 1 || argc
> 2) {
1088 fprintf(stderr
, "Usage: smbcontrol <dest> dump-domain-list "
1095 domain_len
= strlen(argv
[1]) + 1;
1098 messaging_register(msg_ctx
, NULL
, MSG_WINBIND_DUMP_DOMAIN_LIST
,
1099 print_pid_string_cb
);
1101 buf_len
= sizeof(myid
)+domain_len
;
1102 buf
= SMB_MALLOC_ARRAY(uint8_t, buf_len
);
1107 memcpy(buf
, &myid
, sizeof(myid
));
1108 memcpy(&buf
[sizeof(myid
)], domain
, domain_len
);
1110 if (!send_message(msg_ctx
, pid
, MSG_WINBIND_DUMP_DOMAIN_LIST
,
1117 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
1119 /* No replies were received within the timeout period */
1122 if (num_replies
== 0) {
1123 printf("No replies received\n");
1126 messaging_deregister(msg_ctx
, MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1131 static void winbind_validate_cache_cb(struct messaging_context
*msg
,
1134 struct server_id pid
,
1137 char *src_string
= server_id_str(NULL
, &pid
);
1138 printf("Winbindd cache is %svalid. (answer from pid %s)\n",
1139 (*(data
->data
) == 0 ? "" : "NOT "), src_string
);
1140 TALLOC_FREE(src_string
);
1144 static bool do_winbind_validate_cache(struct tevent_context
*ev_ctx
,
1145 struct messaging_context
*msg_ctx
,
1146 const struct server_id pid
,
1147 const int argc
, const char **argv
)
1149 struct server_id myid
;
1151 myid
= messaging_server_id(msg_ctx
);
1154 fprintf(stderr
, "Usage: smbcontrol winbindd validate-cache\n");
1158 messaging_register(msg_ctx
, NULL
, MSG_WINBIND_VALIDATE_CACHE
,
1159 winbind_validate_cache_cb
);
1161 if (!send_message(msg_ctx
, pid
, MSG_WINBIND_VALIDATE_CACHE
, &myid
,
1166 wait_replies(ev_ctx
, msg_ctx
, procid_to_pid(&pid
) == 0);
1168 if (num_replies
== 0) {
1169 printf("No replies received\n");
1172 messaging_deregister(msg_ctx
, MSG_WINBIND_VALIDATE_CACHE
, NULL
);
1177 static bool do_reload_config(struct tevent_context
*ev_ctx
,
1178 struct messaging_context
*msg_ctx
,
1179 const struct server_id pid
,
1180 const int argc
, const char **argv
)
1183 fprintf(stderr
, "Usage: smbcontrol <dest> reload-config\n");
1187 return send_message(msg_ctx
, pid
, MSG_SMB_CONF_UPDATED
, NULL
, 0);
1190 static bool do_reload_printers(struct tevent_context
*ev_ctx
,
1191 struct messaging_context
*msg_ctx
,
1192 const struct server_id pid
,
1193 const int argc
, const char **argv
)
1196 fprintf(stderr
, "Usage: smbcontrol <dest> reload-printers\n");
1200 return send_message(msg_ctx
, pid
, MSG_PRINTER_PCAP
, NULL
, 0);
1203 static void my_make_nmb_name( struct nmb_name
*n
, const char *name
, int type
)
1206 memset( (char *)n
, '\0', sizeof(struct nmb_name
) );
1207 fstrcpy(unix_name
, name
);
1208 (void)strupper_m(unix_name
);
1209 push_ascii(n
->name
, unix_name
, sizeof(n
->name
), STR_TERMINATE
);
1210 n
->name_type
= (unsigned int)type
& 0xFF;
1211 push_ascii(n
->scope
, lp_netbios_scope(), 64, STR_TERMINATE
);
1214 static bool do_nodestatus(struct tevent_context
*ev_ctx
,
1215 struct messaging_context
*msg_ctx
,
1216 const struct server_id pid
,
1217 const int argc
, const char **argv
)
1219 struct packet_struct p
;
1222 fprintf(stderr
, "Usage: smbcontrol nmbd nodestatus <ip>\n");
1228 p
.ip
= interpret_addr2(argv
[1]);
1230 p
.packet_type
= NMB_PACKET
;
1232 p
.packet
.nmb
.header
.name_trn_id
= 10;
1233 p
.packet
.nmb
.header
.opcode
= 0;
1234 p
.packet
.nmb
.header
.response
= False
;
1235 p
.packet
.nmb
.header
.nm_flags
.bcast
= False
;
1236 p
.packet
.nmb
.header
.nm_flags
.recursion_available
= False
;
1237 p
.packet
.nmb
.header
.nm_flags
.recursion_desired
= False
;
1238 p
.packet
.nmb
.header
.nm_flags
.trunc
= False
;
1239 p
.packet
.nmb
.header
.nm_flags
.authoritative
= False
;
1240 p
.packet
.nmb
.header
.rcode
= 0;
1241 p
.packet
.nmb
.header
.qdcount
= 1;
1242 p
.packet
.nmb
.header
.ancount
= 0;
1243 p
.packet
.nmb
.header
.nscount
= 0;
1244 p
.packet
.nmb
.header
.arcount
= 0;
1245 my_make_nmb_name(&p
.packet
.nmb
.question
.question_name
, "*", 0x00);
1246 p
.packet
.nmb
.question
.question_type
= 0x21;
1247 p
.packet
.nmb
.question
.question_class
= 0x1;
1249 return send_message(msg_ctx
, pid
, MSG_SEND_PACKET
, &p
, sizeof(p
));
1252 static bool do_notify_cleanup(struct tevent_context
*ev_ctx
,
1253 struct messaging_context
*msg_ctx
,
1254 const struct server_id pid
,
1255 const int argc
, const char **argv
)
1258 fprintf(stderr
, "Usage: smbcontrol smbd notify-cleanup\n");
1261 return send_message(msg_ctx
, pid
, MSG_SMB_NOTIFY_CLEANUP
, NULL
, 0);
1264 /* A list of message type supported */
1266 static const struct {
1267 const char *name
; /* Option name */
1268 bool (*fn
)(struct tevent_context
*ev_ctx
,
1269 struct messaging_context
*msg_ctx
,
1270 const struct server_id pid
,
1271 const int argc
, const char **argv
);
1272 const char *help
; /* Short help text */
1274 { "debug", do_debug
, "Set debuglevel" },
1275 { "idmap", do_idmap
, "Manipulate idmap cache" },
1276 { "force-election", do_election
,
1277 "Force a browse election" },
1278 { "ping", do_ping
, "Elicit a response" },
1279 { "profile", do_profile
, "" },
1280 { "inject", do_inject_fault
,
1281 "Inject a fatal signal into a running smbd"},
1282 { "stacktrace", do_daemon_stack_trace
,
1283 "Display a stack trace of a daemon" },
1284 { "profilelevel", do_profilelevel
, "" },
1285 { "debuglevel", do_debuglevel
, "Display current debuglevels" },
1286 { "printnotify", do_printnotify
, "Send a print notify message" },
1287 { "close-share", do_closeshare
, "Forcibly disconnect a share" },
1288 { "ip-dropped", do_ip_dropped
, "Tell winbind that an IP got dropped" },
1289 { "lockretry", do_lockretry
, "Force a blocking lock retry" },
1290 { "brl-revalidate", do_brl_revalidate
, "Revalidate all brl entries" },
1291 { "pool-usage", do_poolusage
, "Display talloc memory usage" },
1292 { "dmalloc-mark", do_dmalloc_mark
, "" },
1293 { "dmalloc-log-changed", do_dmalloc_changed
, "" },
1294 { "shutdown", do_shutdown
, "Shut down daemon" },
1295 { "drvupgrade", do_drvupgrade
, "Notify a printer driver has changed" },
1296 { "reload-config", do_reload_config
, "Force smbd or winbindd to reload config file"},
1297 { "reload-printers", do_reload_printers
, "Force smbd to reload printers"},
1298 { "nodestatus", do_nodestatus
, "Ask nmbd to do a node status request"},
1299 { "online", do_winbind_online
, "Ask winbind to go into online state"},
1300 { "offline", do_winbind_offline
, "Ask winbind to go into offline state"},
1301 { "onlinestatus", do_winbind_onlinestatus
, "Request winbind online status"},
1302 { "dump-event-list", do_dump_event_list
, "Dump event list"},
1303 { "validate-cache" , do_winbind_validate_cache
,
1304 "Validate winbind's credential cache" },
1305 { "dump-domain-list", do_winbind_dump_domain_list
, "Dump winbind domain list"},
1306 { "notify-cleanup", do_notify_cleanup
},
1307 { "noop", do_noop
, "Do nothing" },
1311 /* Display usage information */
1313 static void usage(poptContext pc
)
1317 poptPrintHelp(pc
, stderr
, 0);
1319 fprintf(stderr
, "\n");
1320 fprintf(stderr
, "<destination> is one of \"nmbd\", \"smbd\", \"winbindd\" or a "
1323 fprintf(stderr
, "\n");
1324 fprintf(stderr
, "<message-type> is one of:\n");
1326 for (i
= 0; msg_types
[i
].name
; i
++)
1327 fprintf(stderr
, "\t%-30s%s\n", msg_types
[i
].name
,
1330 fprintf(stderr
, "\n");
1335 /* Return the pid number for a string destination */
1337 static struct server_id
parse_dest(struct messaging_context
*msg
,
1340 struct server_id result
= {-1};
1343 /* Zero is a special return value for broadcast to all processes */
1345 if (strequal(dest
, "all")) {
1346 return interpret_pid(MSG_BROADCAST_PID_STR
);
1349 /* Try self - useful for testing */
1351 if (strequal(dest
, "self")) {
1352 return messaging_server_id(msg
);
1355 /* Fix winbind typo. */
1356 if (strequal(dest
, "winbind")) {
1360 /* Check for numeric pid number */
1361 result
= interpret_pid(dest
);
1363 /* Zero isn't valid if not "all". */
1364 if (result
.pid
&& procid_valid(&result
)) {
1368 /* Look up other destinations in pidfile directory */
1370 if ((pid
= pidfile_pid(lp_piddir(), dest
)) != 0) {
1371 return pid_to_procid(pid
);
1374 fprintf(stderr
,"Can't find pid for destination '%s'\n", dest
);
1379 /* Execute smbcontrol command */
1381 static bool do_command(struct tevent_context
*ev_ctx
,
1382 struct messaging_context
*msg_ctx
,
1383 int argc
, const char **argv
)
1385 const char *dest
= argv
[0], *command
= argv
[1];
1386 struct server_id pid
;
1389 /* Check destination */
1391 pid
= parse_dest(msg_ctx
, dest
);
1392 if (!procid_valid(&pid
)) {
1398 for (i
= 0; msg_types
[i
].name
; i
++) {
1399 if (strequal(command
, msg_types
[i
].name
))
1400 return msg_types
[i
].fn(ev_ctx
, msg_ctx
, pid
,
1401 argc
- 1, argv
+ 1);
1404 fprintf(stderr
, "smbcontrol: unknown command '%s'\n", command
);
1409 static void smbcontrol_help(poptContext pc
,
1410 enum poptCallbackReason preason
,
1411 struct poptOption
* poption
,
1415 if (poption
->shortName
!= '?') {
1416 poptPrintUsage(pc
, stdout
, 0);
1424 struct poptOption help_options
[] = {
1425 { NULL
, '\0', POPT_ARG_CALLBACK
, (void *)&smbcontrol_help
, '\0',
1427 { "help", '?', 0, NULL
, '?', "Show this help message", NULL
},
1428 { "usage", '\0', 0, NULL
, 'u', "Display brief usage message", NULL
},
1434 int main(int argc
, const char **argv
)
1438 struct tevent_context
*evt_ctx
;
1439 struct messaging_context
*msg_ctx
;
1441 static struct poptOption long_options
[] = {
1443 { NULL
, '\0', POPT_ARG_INCLUDE_TABLE
, help_options
,
1444 0, "Help options:", NULL
},
1445 { "timeout", 't', POPT_ARG_INT
, &timeout
, 't',
1446 "Set timeout value in seconds", "TIMEOUT" },
1451 TALLOC_CTX
*frame
= talloc_stackframe();
1456 setup_logging(argv
[0], DEBUG_STDOUT
);
1458 /* Parse command line arguments using popt */
1460 pc
= poptGetContext(
1461 "smbcontrol", argc
, (const char **)argv
, long_options
, 0);
1463 poptSetOtherOptionHelp(pc
, "[OPTION...] <destination> <message-type> "
1469 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1471 case 't': /* --timeout */
1474 fprintf(stderr
, "Invalid option\n");
1475 poptPrintHelp(pc
, stderr
, 0);
1480 /* We should now have the remaining command line arguments in
1481 argv. The argc parameter should have been decremented to the
1482 correct value in the above switch statement. */
1484 argv
= (const char **)poptGetArgs(pc
);
1487 while (argv
[argc
] != NULL
) {
1495 lp_load_global(get_dyn_CONFIGFILE());
1497 /* Need to invert sense of return code -- samba
1498 * routines mostly return True==1 for success, but
1501 if (!(evt_ctx
= tevent_context_init(NULL
)) ||
1502 !(msg_ctx
= messaging_init(NULL
, evt_ctx
))) {
1503 fprintf(stderr
, "could not init messaging context\n");
1508 ret
= !do_command(evt_ctx
, msg_ctx
, argc
, argv
);