static bool do_daemon_stack_trace(struct tevent_context *ev_ctx, struct messaging_context *msg_ctx, const struct server_id pid, const int argc, const char **argv) { pid_t dest; int count = 0; if (argc != 1) { fprintf(stderr, "Usage: smbcontrol <dest> stacktrace\n"); return False; } dest = procid_to_pid(&pid); if (dest != 0) { /* It would be nice to be able to make sure that this PID is * the PID of a smbd/winbind/nmbd process, not some random PID * the user liked the look of. It doesn't seem like it's worth * the effort at the moment, however. */ print_stack_trace(dest, &count); } else { connections_forall_read(stack_trace_connection, &count); } return True; }
int main(int argc, char *argv[]) { int c; int profile_only = 0; bool show_processes, show_locks, show_shares; poptContext pc; struct poptOption long_options[] = { POPT_AUTOHELP {"processes", 'p', POPT_ARG_NONE, NULL, 'p', "Show processes only" }, {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" }, {"locks", 'L', POPT_ARG_NONE, NULL, 'L', "Show locks only" }, {"shares", 'S', POPT_ARG_NONE, NULL, 'S', "Show shares only" }, {"user", 'u', POPT_ARG_STRING, &username, 'u', "Switch to user" }, {"brief", 'b', POPT_ARG_NONE, NULL, 'b', "Be brief" }, {"profile", 'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" }, {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" }, {"byterange", 'B', POPT_ARG_NONE, NULL, 'B', "Include byte range locks"}, {"numeric", 'n', POPT_ARG_NONE, NULL, 'n', "Numeric uid/gid"}, POPT_COMMON_SAMBA POPT_TABLEEND }; TALLOC_CTX *frame = talloc_stackframe(); int ret = 0; struct messaging_context *msg_ctx; sec_init(); load_case_tables(); setup_logging(argv[0], DEBUG_STDERR); if (getuid() != geteuid()) { d_printf("smbstatus should not be run setuid\n"); ret = 1; goto done; } pc = poptGetContext(NULL, argc, (const char **) argv, long_options, POPT_CONTEXT_KEEP_FIRST); while ((c = poptGetNextOpt(pc)) != -1) { switch (c) { case 'p': processes_only = true; break; case 'v': verbose = true; break; case 'L': locks_only = true; break; case 'S': shares_only = true; break; case 'b': brief = true; break; case 'u': Ucrit_addUid(nametouid(poptGetOptArg(pc))); break; case 'P': case 'R': profile_only = c; break; case 'B': show_brl = true; break; case 'n': numeric_only = true; break; } } /* setup the flags based on the possible combincations */ show_processes = !(shares_only || locks_only || profile_only) || processes_only; show_locks = !(shares_only || processes_only || profile_only) || locks_only; show_shares = !(processes_only || locks_only || profile_only) || shares_only; if ( username ) Ucrit_addUid( nametouid(username) ); if (verbose) { d_printf("using configfile = %s\n", get_dyn_CONFIGFILE()); } if (!lp_load_initial_only(get_dyn_CONFIGFILE())) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE()); ret = -1; goto done; } if (lp_clustering()) { /* * This implicitly initializes the global ctdbd * connection, usable by the db_open() calls further * down. */ msg_ctx = messaging_init(NULL, event_context_init(NULL)); if (msg_ctx == NULL) { fprintf(stderr, "messaging_init failed\n"); ret = -1; goto done; } } if (!lp_load_global(get_dyn_CONFIGFILE())) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE()); ret = -1; goto done; } switch (profile_only) { case 'P': /* Dump profile data */ return status_profile_dump(verbose); case 'R': /* Continuously display rate-converted data */ return status_profile_rates(verbose); default: break; } if ( show_processes ) { d_printf("\nSamba version %s\n",samba_version_string()); d_printf("PID Username Group Machine \n"); d_printf("-------------------------------------------------------------------\n"); if (lp_security() == SEC_SHARE) { d_printf(" <processes do not show up in " "anonymous mode>\n"); } sessionid_traverse_read(traverse_sessionid, NULL); if (processes_only) { goto done; } } if ( show_shares ) { if (verbose) { d_printf("Opened %s\n", lock_path("connections.tdb")); } if (brief) { goto done; } d_printf("\nService pid machine Connected at\n"); d_printf("-------------------------------------------------------\n"); connections_forall_read(traverse_fn1, NULL); d_printf("\n"); if ( shares_only ) { goto done; } } if ( show_locks ) { int result; struct db_context *db; db = db_open(NULL, lock_path("locking.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0, DBWRAP_LOCK_ORDER_1); if (!db) { d_printf("%s not initialised\n", lock_path("locking.tdb")); d_printf("This is normal if an SMB client has never " "connected to your server.\n"); exit(0); } else { TALLOC_FREE(db); } if (!locking_init_readonly()) { d_printf("Can't initialise locking module - exiting\n"); ret = 1; goto done; } result = share_mode_forall(print_share_mode, NULL); if (result == 0) { d_printf("No locked files\n"); } else if (result < 0) { d_printf("locked file list truncated\n"); } d_printf("\n"); if (show_brl) { brl_forall(print_brl, NULL); } locking_end(); } done: TALLOC_FREE(frame); return ret; }
/* show the current server status */ void status_page(void) { const char *v; int autorefresh=0; int refresh_interval=30; int nr_running=0; bool waitup = False; TALLOC_CTX *ctx = talloc_stackframe(); const char form_name[] = "status"; smbd_pid = pid_to_procid(pidfile_pid("smbd")); if (!verify_xsrf_token(form_name)) { goto output_page; } if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) { stop_smbd(); start_smbd(); waitup=True; } if (cgi_variable("smbd_start") || cgi_variable("all_start")) { start_smbd(); waitup=True; } if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) { stop_smbd(); waitup=True; } if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) { stop_nmbd(); start_nmbd(); waitup=True; } if (cgi_variable("nmbd_start") || cgi_variable("all_start")) { start_nmbd(); waitup=True; } if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) { stop_nmbd(); waitup=True; } #ifdef WITH_WINBIND if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) { stop_winbindd(); start_winbindd(); waitup=True; } if (cgi_variable("winbindd_start") || cgi_variable("all_start")) { start_winbindd(); waitup=True; } if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) { stop_winbindd(); waitup=True; } #endif /* wait for daemons to start/stop */ if (waitup) sleep(SLEEP_TIME); if (cgi_variable("autorefresh")) { autorefresh = 1; } else if (cgi_variable("norefresh")) { autorefresh = 0; } else if (cgi_variable("refresh")) { autorefresh = 1; } if ((v=cgi_variable("refresh_interval"))) { refresh_interval = atoi(v); } if (cgi_variable("show_client_in_col_1")) { PID_or_Machine = 1; } if (cgi_variable("show_pid_in_col_1")) { PID_or_Machine = 0; } connections_forall_read(traverse_fn1, NULL); initPid2Machine (); output_page: printf("<H2>%s</H2>\n", _("Server Status")); printf("<FORM method=post>\n"); print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name); if (!autorefresh) { printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh")); printf("<br>%s", _("Refresh Interval: ")); printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n", refresh_interval); } else { printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing")); printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval); printf("<input type=hidden name=\"refresh\" value=\"1\">\n"); } printf("<p>\n"); printf("<table>\n"); printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), samba_version_string()); fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (smbd_running()) { nr_running++; printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd")); } else { printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd")); } printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd")); } printf("</tr>\n"); fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (nmbd_running()) { nr_running++; printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd")); } else { printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd")); } printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd")); } printf("</tr>\n"); #ifdef WITH_WINBIND fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (winbindd_running()) { nr_running++; printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd")); } else { printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd")); } printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd")); } printf("</tr>\n"); #endif if (geteuid() == 0) { printf("<tr><td></td><td></td>\n"); if (nr_running >= 1) { /* stop, restart all */ printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All")); printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All")); } else if (nr_running == 0) { /* start all */ printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All")); } printf("</tr>\n"); } printf("</table>\n"); fflush(stdout); printf("<p><h3>%s</h3>\n", _("Active Connections")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date")); if (geteuid() == 0) { printf("<th>%s</th>\n", _("Kill")); } printf("</tr>\n"); connections_forall_read(traverse_fn2, NULL); printf("</table><p>\n"); printf("<p><h3>%s</h3>\n", _("Active Shares")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n", _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date")); connections_forall_read(traverse_fn3, NULL); printf("</table><p>\n"); printf("<h3>%s</h3>\n", _("Open Files")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("Share"), _("File"), _("Date")); locking_init_readonly(); share_mode_forall(print_share_mode, NULL); locking_end(); printf("</table>\n"); printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1")); printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1")); printf("</FORM>\n"); if (autorefresh) { /* this little JavaScript allows for automatic refresh of the page. There are other methods but this seems to be the best alternative */ printf("<script language=\"JavaScript\">\n"); printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", cgi_baseurl(), refresh_interval, refresh_interval*1000); printf("//-->\n</script>\n"); } TALLOC_FREE(ctx); }
int main(int argc, const char *argv[]) { int c; int profile_only = 0; bool show_processes, show_locks, show_shares; bool show_notify = false; poptContext pc; struct poptOption long_options[] = { POPT_AUTOHELP {"processes", 'p', POPT_ARG_NONE, NULL, 'p', "Show processes only" }, {"verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Be verbose" }, {"locks", 'L', POPT_ARG_NONE, NULL, 'L', "Show locks only" }, {"shares", 'S', POPT_ARG_NONE, NULL, 'S', "Show shares only" }, {"notify", 'N', POPT_ARG_NONE, NULL, 'N', "Show notifies" }, {"user", 'u', POPT_ARG_STRING, &username, 'u', "Switch to user" }, {"brief", 'b', POPT_ARG_NONE, NULL, 'b', "Be brief" }, {"profile", 'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" }, {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" }, {"byterange", 'B', POPT_ARG_NONE, NULL, 'B', "Include byte range locks"}, {"numeric", 'n', POPT_ARG_NONE, NULL, 'n', "Numeric uid/gid"}, {"fast", 'f', POPT_ARG_NONE, NULL, 'f', "Skip checks if processes still exist"}, POPT_COMMON_SAMBA POPT_TABLEEND }; TALLOC_CTX *frame = talloc_stackframe(); int ret = 0; struct messaging_context *msg_ctx = NULL; char *db_path; bool ok; sec_init(); smb_init_locale(); setup_logging(argv[0], DEBUG_STDERR); lp_set_cmdline("log level", "0"); if (getuid() != geteuid()) { d_printf("smbstatus should not be run setuid\n"); ret = 1; goto done; } if (getuid() != 0) { d_printf("smbstatus only works as root!\n"); ret = 1; goto done; } pc = poptGetContext(NULL, argc, argv, long_options, POPT_CONTEXT_KEEP_FIRST); while ((c = poptGetNextOpt(pc)) != -1) { switch (c) { case 'p': processes_only = true; break; case 'v': verbose = true; break; case 'L': locks_only = true; break; case 'S': shares_only = true; break; case 'N': show_notify = true; break; case 'b': brief = true; break; case 'u': Ucrit_addUid(nametouid(poptGetOptArg(pc))); break; case 'P': case 'R': profile_only = c; break; case 'B': show_brl = true; break; case 'n': numeric_only = true; break; case 'f': do_checks = false; break; } } /* setup the flags based on the possible combincations */ show_processes = !(shares_only || locks_only || profile_only) || processes_only; show_locks = !(shares_only || processes_only || profile_only) || locks_only; show_shares = !(processes_only || locks_only || profile_only) || shares_only; if ( username ) Ucrit_addUid( nametouid(username) ); if (verbose) { d_printf("using configfile = %s\n", get_dyn_CONFIGFILE()); } if (!lp_load_initial_only(get_dyn_CONFIGFILE())) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE()); ret = -1; goto done; } /* * This implicitly initializes the global ctdbd connection, * usable by the db_open() calls further down. */ msg_ctx = messaging_init(NULL, samba_tevent_context_init(NULL)); if (msg_ctx == NULL) { fprintf(stderr, "messaging_init failed\n"); ret = -1; goto done; } if (!lp_load_global(get_dyn_CONFIGFILE())) { fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE()); ret = -1; goto done; } switch (profile_only) { case 'P': /* Dump profile data */ ok = status_profile_dump(verbose); return ok ? 0 : 1; case 'R': /* Continuously display rate-converted data */ ok = status_profile_rates(verbose); return ok ? 0 : 1; default: break; } if ( show_processes ) { d_printf("\nSamba version %s\n",samba_version_string()); d_printf("%-7s %-12s %-12s %-41s %-17s %-20s %-21s\n", "PID", "Username", "Group", "Machine", "Protocol Version", "Encryption", "Signing"); d_printf("----------------------------------------------------------------------------------------------------------------------------------------\n"); sessionid_traverse_read(traverse_sessionid, frame); if (processes_only) { goto done; } } if ( show_shares ) { if (brief) { goto done; } d_printf("\n%-12s %-7s %-13s %-32s %-12s %-12s\n", "Service", "pid", "Machine", "Connected at", "Encryption", "Signing"); d_printf("---------------------------------------------------------------------------------------------\n"); connections_forall_read(traverse_connections, frame); d_printf("\n"); if ( shares_only ) { goto done; } } if ( show_locks ) { int result; struct db_context *db; db_path = lock_path("locking.tdb"); if (db_path == NULL) { d_printf("Out of memory - exiting\n"); ret = -1; goto done; } db = db_open(NULL, db_path, 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDONLY, 0, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE); if (!db) { d_printf("%s not initialised\n", db_path); d_printf("This is normal if an SMB client has never " "connected to your server.\n"); TALLOC_FREE(db_path); exit(0); } else { TALLOC_FREE(db); TALLOC_FREE(db_path); } if (!locking_init_readonly()) { d_printf("Can't initialise locking module - exiting\n"); ret = 1; goto done; } result = share_entry_forall(print_share_mode, NULL); if (result == 0) { d_printf("No locked files\n"); } else if (result < 0) { d_printf("locked file list truncated\n"); } d_printf("\n"); if (show_brl) { brl_forall(print_brl, NULL); } locking_end(); } if (show_notify) { struct notify_context *n; n = notify_init(talloc_tos(), msg_ctx, messaging_tevent_context(msg_ctx)); if (n == NULL) { goto done; } notify_walk(n, print_notify_rec, NULL); TALLOC_FREE(n); } done: TALLOC_FREE(frame); return ret; }