Patch from metze and me that adds dummy smb_register_*() functions
[samba.git] / source3 / utils / smbcontrol.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Send messages to other Samba daemons
5
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    
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.
15    
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.
20    
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.
24 */
25
26 #include "includes.h"
27
28 #include "module_dummy.h"
29
30 /* Default timeout value when waiting for replies (in seconds) */
31
32 #define DEFAULT_TIMEOUT 10
33
34 static int timeout = DEFAULT_TIMEOUT;
35 static int num_replies;         /* Used by message callback fns */
36
37 /* Send a message to a destination pid.  Zero means broadcast smbd. */
38
39 static BOOL send_message(pid_t pid, int msg_type, void *buf, int len,
40                          BOOL duplicates)
41 {
42         TDB_CONTEXT *tdb;
43         BOOL ret;
44         int n_sent = 0;
45
46         if (!message_init())
47                 return False;
48
49         if (pid != 0)
50                 return message_send_pid(pid, msg_type, buf, len, duplicates);
51
52         tdb = tdb_open_log(lock_path("connections.tdb"), 0, 
53                            TDB_DEFAULT, O_RDWR, 0);
54         if (!tdb) {
55                 fprintf(stderr,"Failed to open connections database"
56                         ": %s\n", strerror(errno));
57                 return False;
58         }
59         
60         ret = message_send_all(tdb,msg_type, buf, len, duplicates,
61                                &n_sent);
62         DEBUG(10,("smbcontrol/send_message: broadcast message to "
63                   "%d processes\n", n_sent));
64         
65         tdb_close(tdb);
66         
67         return ret;
68 }
69
70 /* Wait for one or more reply messages */
71
72 static void wait_replies(BOOL multiple_replies)
73 {
74         time_t start_time = time(NULL);
75
76         /* Wait around a bit.  This is pretty disgusting - we have to
77            busy-wait here as there is no nicer way to do it. */
78
79         do {
80                 message_dispatch();
81                 if (num_replies > 0 && !multiple_replies)
82                         break;
83                 sleep(1);
84         } while (timeout - (time(NULL) - start_time) > 0);
85 }
86
87 /* Message handler callback that displays a string on stdout */
88
89 static void print_string_cb(int msg_type, pid_t pid, void *buf, size_t len)
90 {
91         printf("%.*s", (int)len, (const char *)buf);
92         num_replies++;
93 }
94
95 /* Send no message.  Useful for testing. */
96
97 static BOOL do_noop(const pid_t pid, const int argc, char **argv)
98 {
99         if (argc != 1) {
100                 fprintf(stderr, "Usage: smbcontrol <dest> noop\n");
101                 return False;
102         }
103
104         /* Move along, nothing to see here */
105
106         return True;
107 }
108
109 /* Send a debug string */
110
111 static BOOL do_debug(const pid_t pid, const int argc, char **argv)
112 {
113         if (argc != 2) {
114                 fprintf(stderr, "Usage: smbcontrol <dest> debug "
115                         "<debug-string>\n");
116                 return False;
117         }
118
119         return send_message(
120                 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
121 }
122
123 /* Force a browser election */
124
125 static BOOL do_election(const pid_t pid, const int argc, char **argv)
126 {
127         if (argc != 1) {
128                 fprintf(stderr, "Usage: smbcontrol <dest> force-election\n");
129                 return False;
130         }
131
132         return send_message(
133                 pid, MSG_FORCE_ELECTION, NULL, 0, False);
134 }
135
136 /* Ping a samba daemon process */
137
138 static void pong_cb(int msg_type, pid_t pid, void *buf, size_t len)
139 {
140         printf("PONG from pid %u\n", (unsigned int)pid);
141         num_replies++;
142 }
143
144 static BOOL do_ping(const pid_t pid, const int argc, char **argv)
145 {
146         if (argc != 1) {
147                 fprintf(stderr, "Usage: smbcontrol <dest> ping\n");
148                 return False;
149         }
150
151         /* Send a message and register our interest in a reply */
152
153         if (!send_message(pid, MSG_PING, NULL, 0, False))
154                 return False;
155
156         message_register(MSG_PONG, pong_cb);
157
158         wait_replies(pid == 0);
159
160         /* No replies were received within the timeout period */
161
162         if (num_replies == 0)
163                 printf("No replies received\n");
164
165         message_deregister(MSG_PONG);
166
167         return num_replies;
168 }
169
170 /* Set profiling options */
171
172 static BOOL do_profile(const pid_t pid, const int argc, char **argv)
173 {
174         int v;
175
176         if (argc != 2) {
177                 fprintf(stderr, "Usage: smbcontrol <dest> profile "
178                         "<off|count|on|flush>\n");
179                 return False;
180         }
181
182         if (strcmp(argv[1], "off") == 0) {
183                 v = 0;
184         } else if (strcmp(argv[1], "count") == 0) {
185                 v = 1;
186         } else if (strcmp(argv[1], "on") == 0) {
187                 v = 2;
188         } else if (strcmp(argv[1], "flush") == 0) {
189                 v = 3;
190         } else {
191                 fprintf(stderr, "Unknown profile command '%s'\n", argv[1]);
192                 return False;
193         }
194
195         return send_message(pid, MSG_PROFILE, &v, sizeof(int), False);
196 }
197
198 /* Return the profiling level */
199
200 static void profilelevel_cb(int msg_type, pid_t pid, void *buf, size_t len)
201 {
202         int level;
203         const char *s;
204
205         num_replies++;
206
207         if (len != sizeof(int)) {
208                 fprintf(stderr, "invalid message length %d returned\n", len);
209                 return;
210         }
211
212         memcpy(&level, buf, sizeof(int));
213
214         switch (level) {
215         case 0:
216                 s = "not enabled";
217                 break;
218         case 1:
219                 s = "off";
220                 break;
221         case 3:
222                 s = "count only";
223                 break;
224         case 7:
225                 s = "count and time";
226                 break;
227         default:
228                 s = "BOGUS";
229                 break;
230         }
231         
232         printf("Profiling %s on pid %u\n",s,(unsigned int)pid);
233 }
234
235 static void profilelevel_rqst(int msg_type, pid_t pid, void *buf, size_t len)
236 {
237         int v = 0;
238
239         /* Send back a dummy reply */
240
241         send_message(pid, MSG_PROFILELEVEL, &v, sizeof(int), False);
242 }
243
244 static BOOL do_profilelevel(const pid_t pid, const int argc, char **argv)
245 {
246         if (argc != 1) {
247                 fprintf(stderr, "Usage: smbcontrol <dest> profilelevel\n");
248                 return False;
249         }
250
251         /* Send a message and register our interest in a reply */
252
253         if (!send_message(pid, MSG_REQ_PROFILELEVEL, NULL, 0, False))
254                 return False;
255
256         message_register(MSG_PROFILELEVEL, profilelevel_cb);
257         message_register(MSG_REQ_PROFILELEVEL, profilelevel_rqst);
258
259         wait_replies(pid == 0);
260
261         /* No replies were received within the timeout period */
262
263         if (num_replies == 0)
264                 printf("No replies received\n");
265
266         message_deregister(MSG_PROFILE);
267
268         return num_replies;
269 }
270
271 /* Display debug level settings */
272
273 static BOOL do_debuglevel(const pid_t pid, const int argc, char **argv)
274 {
275         if (argc != 1) {
276                 fprintf(stderr, "Usage: smbcontrol <dest> debuglevel\n");
277                 return False;
278         }
279
280         /* Send a message and register our interest in a reply */
281
282         if (!send_message(pid, MSG_REQ_DEBUGLEVEL, NULL, 0, False))
283                 return False;
284
285         message_register(MSG_DEBUGLEVEL, print_string_cb);
286
287         wait_replies(pid == 0);
288
289         /* No replies were received within the timeout period */
290
291         if (num_replies == 0)
292                 printf("No replies received\n");
293
294         message_deregister(MSG_DEBUGLEVEL);
295
296         return num_replies;
297 }
298
299 /* Send a print notify message */
300
301 static BOOL do_printnotify(const pid_t pid, const int argc, char **argv)
302 {
303         char *cmd;
304
305         /* Check for subcommand */
306
307         if (argc == 1) {
308                 fprintf(stderr, "Must specify subcommand:\n");
309                 fprintf(stderr, "\tqueuepause <printername>\n");
310                 fprintf(stderr, "\tqueueresume <printername>\n");
311                 fprintf(stderr, "\tjobpause <printername> <unix jobid>\n");
312                 fprintf(stderr, "\tjobresume <printername> <unix jobid>\n");
313                 fprintf(stderr, "\tjobdelete <printername> <unix jobid>\n");
314                 fprintf(stderr, "\tprinter <printername> <comment|port|"
315                         "driver> <value>\n");
316                 
317                 return False;
318         }
319
320         cmd = argv[1];
321
322         if (strcmp(cmd, "queuepause") == 0) {
323
324                 if (argc != 3) {
325                         fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
326                                 " queuepause <printername>\n");
327                         return False;
328                 }
329                 
330                 notify_printer_status_byname(argv[2], PRINTER_STATUS_PAUSED);
331
332                 goto send;
333
334         } else if (strcmp(cmd, "queueresume") == 0) {
335
336                 if (argc != 3) {
337                         fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
338                                 " queuereume <printername>\n");
339                         return False;
340                 }
341                 
342                 notify_printer_status_byname(argv[2], PRINTER_STATUS_OK);
343
344                 goto send;
345
346         } else if (strcmp(cmd, "jobpause") == 0) {
347                 int jobid;
348
349                 if (argc != 4) {
350                         fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
351                                 " jobpause <printername> <unix-jobid>\n");
352                         return False;
353                 }
354
355                 jobid = atoi(argv[3]);
356
357                 notify_job_status_byname(
358                         argv[2], jobid, JOB_STATUS_PAUSED, 
359                         SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
360
361                 goto send;
362
363         } else if (strcmp(cmd, "jobresume") == 0) {
364                 int jobid;
365
366                 if (argc != 4) {
367                         fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
368                                 " jobpause <printername> <unix-jobid>\n");
369                         return False;
370                 }
371
372                 jobid = atoi(argv[3]);
373
374                 notify_job_status_byname(
375                         argv[2], jobid, JOB_STATUS_QUEUED, 
376                         SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
377
378                 goto send;
379
380         } else if (strcmp(cmd, "jobdelete") == 0) {
381                 int jobid;
382
383                 if (argc != 4) {
384                         fprintf(stderr, "Usage: smbcontrol <dest> printnotify"
385                                 " jobpause <printername> <unix-jobid>\n");
386                         return False;
387                 }
388
389                 jobid = atoi(argv[3]);
390
391                 notify_job_status_byname(
392                         argv[2], jobid, JOB_STATUS_DELETING,
393                         SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
394                 
395                 notify_job_status_byname(
396                         argv[2], jobid, JOB_STATUS_DELETING|
397                         JOB_STATUS_DELETED,
398                         SPOOLSS_NOTIFY_MSG_UNIX_JOBID);
399
400                 goto send;
401
402         } else if (strcmp(cmd, "printer") == 0) {
403                 uint32 attribute;
404                 
405                 if (argc != 5) {
406                         fprintf(stderr, "Usage: smbcontrol <dest> printnotify "
407                                 "printer <printername> <comment|port|driver> "
408                                 "<value>\n");
409                         return False;
410                 }
411
412                 if (strcmp(argv[3], "comment") == 0) {
413                         attribute = PRINTER_NOTIFY_COMMENT;
414                 } else if (strcmp(argv[3], "port") == 0) {
415                         attribute = PRINTER_NOTIFY_PORT_NAME;
416                 } else if (strcmp(argv[3], "driver") == 0) {
417                         attribute = PRINTER_NOTIFY_DRIVER_NAME;
418                 } else {
419                         fprintf(stderr, "Invalid printer command '%s'\n",
420                                 argv[3]);
421                         return False;
422                 }
423
424                 notify_printer_byname(argv[2], attribute, argv[4]);
425
426                 goto send;
427         }
428
429         fprintf(stderr, "Invalid subcommand '%s'\n", cmd);
430         return False;
431
432 send:
433         print_notify_send_messages(0);
434         return True;
435 }
436
437 /* Close a share */
438
439 static BOOL do_closeshare(const pid_t pid, const int argc, char **argv)
440 {
441         if (argc != 2) {
442                 fprintf(stderr, "Usage: smbcontrol <dest> close-share "
443                         "<sharename>\n");
444                 return False;
445         }
446
447         return send_message(
448                 pid, MSG_SMB_FORCE_TDIS, argv[1], strlen(argv[1]) + 1, False);
449 }
450
451 /* Force a SAM synchronisation */
452
453 static BOOL do_samsync(const pid_t pid, const int argc, char **argv)
454 {
455         if (argc != 1) {
456                 fprintf(stderr, "Usage: smbcontrol <dest> samsync\n");
457                 return False;
458         }
459
460         return send_message(
461                 pid, MSG_SMB_SAM_SYNC, NULL, 0, False);
462 }
463
464 /* Force a SAM replication */
465
466 static BOOL do_samrepl(const pid_t pid, const int argc, char **argv)
467 {
468         if (argc != 1) {
469                 fprintf(stderr, "Usage: smbcontrol <dest> samrepl\n");
470                 return False;
471         }
472
473         return send_message(
474                 pid, MSG_SMB_SAM_REPL, NULL, 0, False);
475 }
476
477 /* Display talloc pool usage */
478
479 static BOOL do_poolusage(const pid_t pid, const int argc, char **argv)
480 {
481         if (argc != 1) {
482                 fprintf(stderr, "Usage: smbcontrol <dest> pool-usage\n");
483                 return False;
484         }
485
486         /* Send a message and register our interest in a reply */
487
488         if (!send_message(pid, MSG_REQ_POOL_USAGE, NULL, 0, False))
489                 return False;
490
491         message_register(MSG_POOL_USAGE, print_string_cb);
492
493         wait_replies(pid == 0);
494
495         /* No replies were received within the timeout period */
496
497         if (num_replies == 0)
498                 printf("No replies received\n");
499
500         message_deregister(MSG_POOL_USAGE);
501
502         return num_replies;
503 }
504
505 /* Perform a dmalloc mark */
506
507 static BOOL do_dmalloc_mark(const pid_t pid, const int argc, char **argv)
508 {
509         if (argc != 1) {
510                 fprintf(stderr, "Usage: smbcontrol <dest> dmalloc-mark\n");
511                 return False;
512         }
513
514         return send_message(
515                 pid, MSG_REQ_DMALLOC_MARK, NULL, 0, False);
516 }
517
518 /* Perform a dmalloc changed */
519
520 static BOOL do_dmalloc_changed(const pid_t pid, const int argc, 
521                                char **argv)
522 {
523         if (argc != 1) {
524                 fprintf(stderr, "Usage: smbcontrol <dest> "
525                         "dmalloc-log-changed\n");
526                 return False;
527         }
528
529         return send_message(
530                 pid, MSG_REQ_DMALLOC_LOG_CHANGED, NULL, 0, False);
531 }
532
533 /* Shutdown a server process */
534
535 static BOOL do_shutdown(const pid_t pid, const int argc, char **argv)
536 {
537         if (argc != 1) {
538                 fprintf(stderr, "Usage: smbcontrol <dest> shutdown\n");
539                 return False;
540         }
541
542         return send_message(pid, MSG_SHUTDOWN, NULL, 0, False);
543 }
544
545 /* Notify a driver upgrade */
546
547 static BOOL do_drvupgrade(const pid_t pid, const int argc, char **argv)
548 {
549         if (argc != 2) {
550                 fprintf(stderr, "Usage: smbcontrol <dest> drvupgrade "
551                         "<driver-name>\n");
552                 return False;
553         }
554
555         return send_message(
556                 pid, MSG_DEBUG, argv[1], strlen(argv[1]) + 1, False);
557 }
558
559 /* A list of message type supported */
560
561 static const struct {
562         const char *name;       /* Option name */
563         BOOL (*fn)(const pid_t pid, const int argc, char **argv);
564         const char *help;       /* Short help text */
565 } msg_types[] = {
566         { "debug", do_debug, "Set debuglevel"  },
567         { "force-election", do_election,
568           "Force a browse election" },
569         { "ping", do_ping, "Elicit a response" },
570         { "profile", do_profile, "" },
571         { "profilelevel", do_profilelevel, "" },
572         { "debuglevel", do_debuglevel, "Display current debuglevels" },
573         { "printnotify", do_printnotify, "Send a print notify message" },
574         { "close-share", do_closeshare, "Forcibly disconnect a share" },
575         { "samsync", do_samsync, "Initiate SAM synchronisation" },
576         { "samrepl", do_samrepl, "Initiate SAM replication" },
577         { "pool-usage", do_poolusage, "Display talloc memory usage" },
578         { "dmalloc-mark", do_dmalloc_mark, "" },
579         { "dmalloc-log-changed", do_dmalloc_changed, "" },
580         { "shutdown", do_shutdown, "Shut down daemon" },
581         { "drvupgrade", do_drvupgrade, "Notify a printer driver has changed" },
582         { "noop", do_noop, "Do nothing" },
583         { NULL }
584 };
585
586 /* Yuck - we need these because we link to printing*.o even though
587    they aren't used. */
588
589 void become_root(void) {}
590 void unbecome_root(void) {}
591
592 /* Display usage information */
593
594 static void usage(poptContext *pc)
595 {
596         int i;
597
598         poptPrintHelp(*pc, stderr, 0);
599
600         fprintf(stderr, "\n");
601         fprintf(stderr, "<destination> is one of \"nmbd\", \"smbd\" or a "
602                 "process ID\n");
603
604         fprintf(stderr, "\n");
605         fprintf(stderr, "<message-type> is one of:\n");
606
607         for (i = 0; msg_types[i].name; i++) 
608             fprintf(stderr, "\t%-30s%s\n", msg_types[i].name, 
609                     msg_types[i].help);
610
611         fprintf(stderr, "\n");
612
613         exit(1);
614 }
615
616 /* Return the pid number for a string destination */
617
618 static pid_t parse_dest(char *dest)
619 {
620         pid_t pid;
621
622         /* Zero is a special return value for broadcast smbd */
623
624         if (strequal(dest, "smbd"))
625                 return 0;
626
627         /* Try self - useful for testing */
628
629         if (strequal(dest, "self"))
630                 return sys_getpid();
631
632         /* Check for numeric pid number */
633
634         if ((pid = atoi(dest)) != 0)
635                 return pid;
636
637         /* Look up other destinations in pidfile directory */
638
639         if ((pid = pidfile_pid(dest)) != 0)
640                 return pid;
641
642         fprintf(stderr,"Can't find pid for destination '%s'\n", dest);
643
644         return -1;
645 }       
646
647 /* Execute smbcontrol command */
648
649 static BOOL do_command(int argc, char **argv)
650 {
651         char *dest = argv[0], *command = argv[1];
652         pid_t pid;
653         int i;
654
655         /* Check destination */
656
657         if ((pid = parse_dest(dest)) == -1)
658                 return False;
659
660         /* Check command */
661
662         for (i = 0; msg_types[i].name; i++) {
663                 if (strequal(command, msg_types[i].name))
664                         return msg_types[i].fn(pid, argc - 1, argv + 1);
665         }
666
667         fprintf(stderr, "smbcontrol: unknown command '%s'\n", command);
668
669         return False;
670 }
671
672 /* Main program */
673
674 int main(int argc, char **argv)
675 {
676         poptContext pc;
677         int opt;
678
679         static struct poptOption wbinfo_options[] = {
680                 { "timeout", 't', POPT_ARG_INT, &timeout, 't', 
681                   "Set timeout value in seconds", "TIMEOUT" },
682
683                 { "configfile", 's', POPT_ARG_STRING, NULL, 's', 
684                   "Use alternative configuration file", "CONFIGFILE" },
685
686                 POPT_TABLEEND
687         };
688
689         struct poptOption options[] = {
690                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, wbinfo_options, 0, 
691                   "Options" },
692
693                 POPT_AUTOHELP
694                 POPT_COMMON_VERSION
695                 POPT_TABLEEND
696         };
697
698         setup_logging(argv[0],True);
699         
700         /* Parse command line arguments using popt */
701
702         pc = poptGetContext(
703                 "smbcontrol", argc, (const char **)argv, options, 0);
704
705         poptSetOtherOptionHelp(pc, "[OPTION...] <destination> <message-type> "
706                                "<parameters>");
707
708         if (argc == 1)
709                 usage(&pc);
710
711         while ((opt = poptGetNextOpt(pc)) != -1) {
712                 switch(opt) {
713                 case 't':       /* --timeout */
714                         argc -= 2;
715                         break;
716                 case 's':       /* --configfile */
717                         pstrcpy(dyn_CONFIGFILE, optarg);
718                         argc -= 2;
719                         break;
720                 default:
721                         fprintf(stderr, "Invalid option\n");
722                         poptPrintHelp(pc, stderr, 0);
723                         break;
724                 }
725         }
726
727         /* We should now have the remaining command line arguments in
728            argv.  The argc parameter should have been decremented to the
729            correct value in the above switch statement. */
730
731         argv = (char **)poptGetArgs(pc);
732         argc--;                 /* Don't forget about argv[0] */
733
734         if (argc == 1)
735                 usage(&pc);
736
737         lp_load(dyn_CONFIGFILE,False,False,False);
738
739         /* Need to invert sense of return code -- samba
740          * routines mostly return True==1 for success, but
741          * shell needs 0. */ 
742         
743         return !do_command(argc, argv);
744 }