r13212: r12414@cabra: derrell | 2006-01-28 17:52:17 -0500
[Samba/gebeck_regimport.git] / source / utils / smbcontrol.c
bloba4d2766b13281ea93c80750cfd812d18dabc6e0d
1 /*
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
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 /* Default timeout value when waiting for replies (in seconds) */
30 #define DEFAULT_TIMEOUT 10
32 static int timeout = DEFAULT_TIMEOUT;
33 static int num_replies; /* Used by message callback fns */
35 /* Send a message to a destination pid. Zero means broadcast smbd. */
37 static BOOL send_message(struct process_id pid, int msg_type,
38 const void *buf, int len,
39 BOOL duplicates)
41 TDB_CONTEXT *tdb;
42 BOOL ret;
43 int n_sent = 0;
45 if (!message_init())
46 return False;
48 if (procid_to_pid(&pid) != 0)
49 return message_send_pid(pid, msg_type, buf, len, duplicates);
51 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
52 TDB_DEFAULT, O_RDWR, 0);
53 if (!tdb) {
54 fprintf(stderr,"Failed to open connections database"
55 ": %s\n", strerror(errno));
56 return False;
59 ret = message_send_all(tdb,msg_type, buf, len, duplicates,
60 &n_sent);
61 DEBUG(10,("smbcontrol/send_message: broadcast message to "
62 "%d processes\n", n_sent));
64 tdb_close(tdb);
66 return ret;
69 /* Wait for one or more reply messages */
71 static void wait_replies(BOOL multiple_replies)
73 time_t start_time = time(NULL);
75 /* Wait around a bit. This is pretty disgusting - we have to
76 busy-wait here as there is no nicer way to do it. */
78 do {
79 message_dispatch();
80 if (num_replies > 0 && !multiple_replies)
81 break;
82 sleep(1);
83 } while (timeout - (time(NULL) - start_time) > 0);
86 /* Message handler callback that displays the PID and a string on stdout */
88 static void print_pid_string_cb(int msg_type, struct process_id pid, void *buf, size_t len)
90 printf("PID %u: %.*s", (unsigned int)procid_to_pid(&pid),
91 (int)len, (const char *)buf);
92 num_replies++;
95 /* Message handler callback that displays a string on stdout */
97 static void print_string_cb(int msg_type, struct process_id pid,
98 void *buf, size_t len)
100 printf("%.*s", (int)len, (const char *)buf);
101 num_replies++;
104 /* Send no message. Useful for testing. */
106 static BOOL do_noop(const struct process_id pid,
107 const int argc, const char **argv)
109 if (argc != 1) {
110 fprintf(stderr, "Usage: smbcontrol <dest> noop\n");
111 return False;
114 /* Move along, nothing to see here */
116 return True;
119 /* Send a debug string */
121 static BOOL do_debug(const struct process_id pid,
122 const int argc, const char **argv)
124 if (argc != 2) {
125 fprintf(stderr, "Usage: smbcontrol <dest> debug "
126 "<debug-string>\n");
127 return False;
130 return send_message(
131 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
134 /* Force a browser election */
136 static BOOL do_election(const struct process_id pid,
137 const int argc, const char **argv)
139 if (argc != 1) {
140 fprintf(stderr, "Usage: smbcontrol <dest> force-election\n");
141 return False;
144 return send_message(
145 pid, MSG_FORCE_ELECTION, NULL, 0, False);
148 /* Ping a samba daemon process */
150 static void pong_cb(int msg_type, struct process_id pid, void *buf, size_t len)
152 char *src_string = procid_str(NULL, &pid);
153 printf("PONG from pid %s\n", src_string);
154 talloc_free(src_string);
155 num_replies++;
158 static BOOL do_ping(const struct process_id pid, const int argc, const char **argv)
160 if (argc != 1) {
161 fprintf(stderr, "Usage: smbcontrol <dest> ping\n");
162 return False;
165 /* Send a message and register our interest in a reply */
167 if (!send_message(pid, MSG_PING, NULL, 0, False))
168 return False;
170 message_register(MSG_PONG, pong_cb);
172 wait_replies(procid_to_pid(&pid) == 0);
174 /* No replies were received within the timeout period */
176 if (num_replies == 0)
177 printf("No replies received\n");
179 message_deregister(MSG_PONG);
181 return num_replies;
184 /* Set profiling options */
186 static BOOL do_profile(const struct process_id pid,
187 const int argc, const char **argv)
189 int v;
191 if (argc != 2) {
192 fprintf(stderr, "Usage: smbcontrol <dest> profile "
193 "<off|count|on|flush>\n");
194 return False;
197 if (strcmp(argv[1], "off") == 0) {
198 v = 0;
199 } else if (strcmp(argv[1], "count") == 0) {
200 v = 1;
201 } else if (strcmp(argv[1], "on") == 0) {
202 v = 2;
203 } else if (strcmp(argv[1], "flush") == 0) {
204 v = 3;
205 } else {
206 fprintf(stderr, "Unknown profile command '%s'\n", argv[1]);
207 return False;
210 return send_message(pid, MSG_PROFILE, &v, sizeof(int), False);
213 /* Return the profiling level */
215 static void profilelevel_cb(int msg_type, struct process_id pid, void *buf, size_t len)
217 int level;
218 const char *s;
220 num_replies++;
222 if (len != sizeof(int)) {
223 fprintf(stderr, "invalid message length %ld returned\n",
224 (unsigned long)len);
225 return;
228 memcpy(&level, buf, sizeof(int));
230 switch (level) {
231 case 0:
232 s = "not enabled";
233 break;
234 case 1:
235 s = "off";
236 break;
237 case 3:
238 s = "count only";
239 break;
240 case 7:
241 s = "count and time";
242 break;
243 default:
244 s = "BOGUS";
245 break;
248 printf("Profiling %s on pid %u\n",s,(unsigned int)procid_to_pid(&pid));
251 static void profilelevel_rqst(int msg_type, struct process_id pid,
252 void *buf, size_t len)
254 int v = 0;
256 /* Send back a dummy reply */
258 send_message(pid, MSG_PROFILELEVEL, &v, sizeof(int), False);
261 static BOOL do_profilelevel(const struct process_id pid,
262 const int argc, const char **argv)
264 if (argc != 1) {
265 fprintf(stderr, "Usage: smbcontrol <dest> profilelevel\n");
266 return False;
269 /* Send a message and register our interest in a reply */
271 if (!send_message(pid, MSG_REQ_PROFILELEVEL, NULL, 0, False))
272 return False;
274 message_register(MSG_PROFILELEVEL, profilelevel_cb);
275 message_register(MSG_REQ_PROFILELEVEL, profilelevel_rqst);
277 wait_replies(procid_to_pid(&pid) == 0);
279 /* No replies were received within the timeout period */
281 if (num_replies == 0)
282 printf("No replies received\n");
284 message_deregister(MSG_PROFILE);
286 return num_replies;
289 /* Display debug level settings */
291 static BOOL do_debuglevel(const struct process_id pid,
292 const int argc, const char **argv)
294 if (argc != 1) {
295 fprintf(stderr, "Usage: smbcontrol <dest> debuglevel\n");
296 return False;
299 /* Send a message and register our interest in a reply */
301 if (!send_message(pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False))
302 return False;
304 message_register(MSG_DEBUGLEVEL, print_pid_string_cb);
306 wait_replies(procid_to_pid(&pid) == 0);
308 /* No replies were received within the timeout period */
310 if (num_replies == 0)
311 printf("No replies received\n");
313 message_deregister(MSG_DEBUGLEVEL);
315 return num_replies;
318 /* Send a print notify message */
320 static BOOL do_printnotify(const struct process_id pid,
321 const int argc, const char **argv)
323 const char *cmd;
325 /* Check for subcommand */
327 if (argc == 1) {
328 fprintf(stderr, "Must specify subcommand:\n");
329 fprintf(stderr, "\tqueuepause <printername>\n");
330 fprintf(stderr, "\tqueueresume <printername>\n");
331 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
332 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
333 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
334 fprintf(stderr, "\tprinter <printername> <comment|port|"
335 "driver> <value>\n");
337 return False;
340 cmd = argv[1];
342 if (strcmp(cmd, "queuepause") == 0) {
344 if (argc != 3) {
345 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
346 " queuepause <printername>\n");
347 return False;
350 notify_printer_status_byname(argv[2], PRINTER_STATUS_PAUSED);
352 goto send;
354 } else if (strcmp(cmd, "queueresume") == 0) {
356 if (argc != 3) {
357 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
358 " queuereume <printername>\n");
359 return False;
362 notify_printer_status_byname(argv[2], PRINTER_STATUS_OK);
364 goto send;
366 } else if (strcmp(cmd, "jobpause") == 0) {
367 int jobid;
369 if (argc != 4) {
370 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
371 " jobpause <printername> <unix-jobid>\n");
372 return False;
375 jobid = atoi(argv[3]);
377 notify_job_status_byname(
378 argv[2], jobid, JOB_STATUS_PAUSED,
379 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
381 goto send;
383 } else if (strcmp(cmd, "jobresume") == 0) {
384 int jobid;
386 if (argc != 4) {
387 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
388 " jobpause <printername> <unix-jobid>\n");
389 return False;
392 jobid = atoi(argv[3]);
394 notify_job_status_byname(
395 argv[2], jobid, JOB_STATUS_QUEUED,
396 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
398 goto send;
400 } else if (strcmp(cmd, "jobdelete") == 0) {
401 int jobid;
403 if (argc != 4) {
404 fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
405 " jobpause <printername> <unix-jobid>\n");
406 return False;
409 jobid = atoi(argv[3]);
411 notify_job_status_byname(
412 argv[2], jobid, JOB_STATUS_DELETING,
413 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
415 notify_job_status_byname(
416 argv[2], jobid, JOB_STATUS_DELETING|
417 JOB_STATUS_DELETED,
418 SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
420 goto send;
422 } else if (strcmp(cmd, "printer") == 0) {
423 uint32 attribute;
425 if (argc != 5) {
426 fprintf(stderr, "Usage: smbcontrol <dest> printnotify "
427 "printer <printername> <comment|port|driver> "
428 "<value>\n");
429 return False;
432 if (strcmp(argv[3], "comment") == 0) {
433 attribute = PRINTER_NOTIFY_COMMENT;
434 } else if (strcmp(argv[3], "port") == 0) {
435 attribute = PRINTER_NOTIFY_PORT_NAME;
436 } else if (strcmp(argv[3], "driver") == 0) {
437 attribute = PRINTER_NOTIFY_DRIVER_NAME;
438 } else {
439 fprintf(stderr, "Invalid printer command '%s'\n",
440 argv[3]);
441 return False;
444 notify_printer_byname(argv[2], attribute,
445 CONST_DISCARD(char *, argv[4]));
447 goto send;
450 fprintf(stderr, "Invalid subcommand '%s'\n", cmd);
451 return False;
453 send:
454 print_notify_send_messages(0);
455 return True;
458 /* Close a share */
460 static BOOL do_closeshare(const struct process_id pid,
461 const int argc, const char **argv)
463 if (argc != 2) {
464 fprintf(stderr, "Usage: smbcontrol <dest> close-share "
465 "<sharename>\n");
466 return False;
469 return send_message(
470 pid, MSG_SMB_FORCE_TDIS, argv[1], strlen(argv[1]) + 1, False);
473 /* Force a SAM synchronisation */
475 static BOOL do_samsync(const struct process_id pid,
476 const int argc, const char **argv)
478 if (argc != 1) {
479 fprintf(stderr, "Usage: smbcontrol <dest> samsync\n");
480 return False;
483 return send_message(
484 pid, MSG_SMB_SAM_SYNC, NULL, 0, False);
487 /* Force a SAM replication */
489 static BOOL do_samrepl(const struct process_id pid,
490 const int argc, const char **argv)
492 if (argc != 1) {
493 fprintf(stderr, "Usage: smbcontrol <dest> samrepl\n");
494 return False;
497 return send_message(
498 pid, MSG_SMB_SAM_REPL, NULL, 0, False);
501 /* Display talloc pool usage */
503 static BOOL do_poolusage(const struct process_id pid,
504 const int argc, const char **argv)
506 if (argc != 1) {
507 fprintf(stderr, "Usage: smbcontrol <dest> pool-usage\n");
508 return False;
511 message_register(MSG_POOL_USAGE, print_string_cb);
513 /* Send a message and register our interest in a reply */
515 if (!send_message(pid, MSG_REQ_POOL_USAGE, NULL, 0, False))
516 return False;
518 wait_replies(procid_to_pid(&pid) == 0);
520 /* No replies were received within the timeout period */
522 if (num_replies == 0)
523 printf("No replies received\n");
525 message_deregister(MSG_POOL_USAGE);
527 return num_replies;
530 /* Perform a dmalloc mark */
532 static BOOL do_dmalloc_mark(const struct process_id pid,
533 const int argc, const char **argv)
535 if (argc != 1) {
536 fprintf(stderr, "Usage: smbcontrol <dest> dmalloc-mark\n");
537 return False;
540 return send_message(
541 pid, MSG_REQ_DMALLOC_MARK, NULL, 0, False);
544 /* Perform a dmalloc changed */
546 static BOOL do_dmalloc_changed(const struct process_id pid,
547 const int argc, const char **argv)
549 if (argc != 1) {
550 fprintf(stderr, "Usage: smbcontrol <dest> "
551 "dmalloc-log-changed\n");
552 return False;
555 return send_message(
556 pid, MSG_REQ_DMALLOC_LOG_CHANGED, NULL, 0, False);
559 /* Shutdown a server process */
561 static BOOL do_shutdown(const struct process_id pid,
562 const int argc, const char **argv)
564 if (argc != 1) {
565 fprintf(stderr, "Usage: smbcontrol <dest> shutdown\n");
566 return False;
569 return send_message(pid, MSG_SHUTDOWN, NULL, 0, False);
572 /* Notify a driver upgrade */
574 static BOOL do_drvupgrade(const struct process_id pid,
575 const int argc, const char **argv)
577 if (argc != 2) {
578 fprintf(stderr, "Usage: smbcontrol <dest> drvupgrade "
579 "<driver-name>\n");
580 return False;
583 return send_message(
584 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
587 static BOOL do_reload_config(const struct process_id pid,
588 const int argc, const char **argv)
590 if (argc != 1) {
591 fprintf(stderr, "Usage: smbcontrol <dest> reload-config\n");
592 return False;
595 return send_message(pid, MSG_SMB_CONF_UPDATED, NULL, 0, False);
598 static void my_make_nmb_name( struct nmb_name *n, const char *name, int type)
600 fstring unix_name;
601 memset( (char *)n, '\0', sizeof(struct nmb_name) );
602 fstrcpy(unix_name, name);
603 strupper_m(unix_name);
604 push_ascii(n->name, unix_name, sizeof(n->name), STR_TERMINATE);
605 n->name_type = (unsigned int)type & 0xFF;
606 push_ascii(n->scope, global_scope(), 64, STR_TERMINATE);
609 static BOOL do_nodestatus(const struct process_id pid,
610 const int argc, const char **argv)
612 struct packet_struct p;
614 if (argc != 2) {
615 fprintf(stderr, "Usage: smbcontrol nmbd nodestatus <ip>\n");
616 return False;
619 ZERO_STRUCT(p);
621 p.ip = *interpret_addr2(argv[1]);
622 p.port = 137;
623 p.packet_type = NMB_PACKET;
625 p.packet.nmb.header.name_trn_id = 10;
626 p.packet.nmb.header.opcode = 0;
627 p.packet.nmb.header.response = False;
628 p.packet.nmb.header.nm_flags.bcast = False;
629 p.packet.nmb.header.nm_flags.recursion_available = False;
630 p.packet.nmb.header.nm_flags.recursion_desired = False;
631 p.packet.nmb.header.nm_flags.trunc = False;
632 p.packet.nmb.header.nm_flags.authoritative = False;
633 p.packet.nmb.header.rcode = 0;
634 p.packet.nmb.header.qdcount = 1;
635 p.packet.nmb.header.ancount = 0;
636 p.packet.nmb.header.nscount = 0;
637 p.packet.nmb.header.arcount = 0;
638 my_make_nmb_name(&p.packet.nmb.question.question_name, "*", 0x00);
639 p.packet.nmb.question.question_type = 0x21;
640 p.packet.nmb.question.question_class = 0x1;
642 return send_message(pid, MSG_SEND_PACKET, &p, sizeof(p), False);
645 /* A list of message type supported */
647 static const struct {
648 const char *name; /* Option name */
649 BOOL (*fn)(const struct process_id pid,
650 const int argc, const char **argv);
651 const char *help; /* Short help text */
652 } msg_types[] = {
653 { "debug", do_debug, "Set debuglevel" },
654 { "force-election", do_election,
655 "Force a browse election" },
656 { "ping", do_ping, "Elicit a response" },
657 { "profile", do_profile, "" },
658 { "profilelevel", do_profilelevel, "" },
659 { "debuglevel", do_debuglevel, "Display current debuglevels" },
660 { "printnotify", do_printnotify, "Send a print notify message" },
661 { "close-share", do_closeshare, "Forcibly disconnect a share" },
662 { "samsync", do_samsync, "Initiate SAM synchronisation" },
663 { "samrepl", do_samrepl, "Initiate SAM replication" },
664 { "pool-usage", do_poolusage, "Display talloc memory usage" },
665 { "dmalloc-mark", do_dmalloc_mark, "" },
666 { "dmalloc-log-changed", do_dmalloc_changed, "" },
667 { "shutdown", do_shutdown, "Shut down daemon" },
668 { "drvupgrade", do_drvupgrade, "Notify a printer driver has changed" },
669 { "reload-config", do_reload_config, "Force smbd or winbindd to reload config file"},
670 { "nodestatus", do_nodestatus, "Ask nmbd to do a node status request"},
671 { "noop", do_noop, "Do nothing" },
672 { NULL }
675 /* Display usage information */
677 static void usage(poptContext *pc)
679 int i;
681 poptPrintHelp(*pc, stderr, 0);
683 fprintf(stderr, "\n");
684 fprintf(stderr, "<destination> is one of \"nmbd\", \"smbd\" or a "
685 "process ID\n");
687 fprintf(stderr, "\n");
688 fprintf(stderr, "<message-type> is one of:\n");
690 for (i = 0; msg_types[i].name; i++)
691 fprintf(stderr, "\t%-30s%s\n", msg_types[i].name,
692 msg_types[i].help);
694 fprintf(stderr, "\n");
696 exit(1);
699 /* Return the pid number for a string destination */
701 static struct process_id parse_dest(const char *dest)
703 struct process_id result;
704 pid_t pid;
706 /* Zero is a special return value for broadcast smbd */
708 if (strequal(dest, "smbd")) {
709 return interpret_pid("0");
712 /* Try self - useful for testing */
714 if (strequal(dest, "self")) {
715 return pid_to_procid(sys_getpid());
718 /* Check for numeric pid number */
720 result = interpret_pid(dest);
721 if (procid_valid(&result)) {
722 return result;
725 /* Look up other destinations in pidfile directory */
727 if ((pid = pidfile_pid(dest)) != 0) {
728 return pid_to_procid(pid);
731 fprintf(stderr,"Can't find pid for destination '%s'\n", dest);
733 return result;
736 /* Execute smbcontrol command */
738 static BOOL do_command(int argc, const char **argv)
740 const char *dest = argv[0], *command = argv[1];
741 struct process_id pid;
742 int i;
744 /* Check destination */
746 pid = parse_dest(dest);
747 if (!procid_valid(&pid)) {
748 return False;
751 /* Check command */
753 for (i = 0; msg_types[i].name; i++) {
754 if (strequal(command, msg_types[i].name))
755 return msg_types[i].fn(pid, argc - 1, argv + 1);
758 fprintf(stderr, "smbcontrol: unknown command '%s'\n", command);
760 return False;
763 /* Main program */
765 int main(int argc, const char **argv)
767 poptContext pc;
768 int opt;
770 static struct poptOption wbinfo_options[] = {
771 { "timeout", 't', POPT_ARG_INT, &timeout, 't',
772 "Set timeout value in seconds", "TIMEOUT" },
774 { "configfile", 's', POPT_ARG_STRING, NULL, 's',
775 "Use alternative configuration file", "CONFIGFILE" },
777 POPT_TABLEEND
780 struct poptOption options[] = {
781 { NULL, 0, POPT_ARG_INCLUDE_TABLE, wbinfo_options, 0,
782 "Options" },
784 POPT_AUTOHELP
785 POPT_COMMON_VERSION
786 POPT_TABLEEND
789 load_case_tables();
791 setup_logging(argv[0],True);
793 /* Parse command line arguments using popt */
795 pc = poptGetContext(
796 "smbcontrol", argc, (const char **)argv, options, 0);
798 poptSetOtherOptionHelp(pc, "[OPTION...] <destination> <message-type> "
799 "<parameters>");
801 if (argc == 1)
802 usage(&pc);
804 while ((opt = poptGetNextOpt(pc)) != -1) {
805 switch(opt) {
806 case 't': /* --timeout */
807 argc -= 2;
808 break;
809 case 's': /* --configfile */
810 pstrcpy(dyn_CONFIGFILE, poptGetOptArg(pc));
811 argc -= 2;
812 break;
813 default:
814 fprintf(stderr, "Invalid option\n");
815 poptPrintHelp(pc, stderr, 0);
816 break;
820 /* We should now have the remaining command line arguments in
821 argv. The argc parameter should have been decremented to the
822 correct value in the above switch statement. */
824 argv = (const char **)poptGetArgs(pc);
825 argc--; /* Don't forget about argv[0] */
827 if (argc == 1)
828 usage(&pc);
830 lp_load(dyn_CONFIGFILE,False,False,False,True);
832 /* Need to invert sense of return code -- samba
833 * routines mostly return True==1 for success, but
834 * shell needs 0. */
836 return !do_command(argc, argv);