packaging/RHEL-CTDB: try harder to set $RPMVER
[Samba.git] / source3 / web / statuspage.c
blobd25797064332429712c9b3d4a0f1230ae3c763a9
1 /*
2 Unix SMB/CIFS implementation.
3 web status page
4 Copyright (C) Andrew Tridgell 1997-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "web/swat_proto.h"
22 #include "libcli/security/security.h"
23 #include "locking/proto.h"
24 #include "librpc/gen_ndr/open_files.h"
25 #include "lib/conn_tdb.h"
26 #include "../lib/util/pidfile.h"
28 #define _(x) lang_msg_rotate(talloc_tos(),x)
30 #define PIDMAP struct PidMap
32 /* how long to wait for start/stops to take effect */
33 #define SLEEP_TIME 3
35 PIDMAP {
36 PIDMAP *next, *prev;
37 struct server_id pid;
38 char *machine;
41 static PIDMAP *pidmap;
42 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
44 static struct server_id smbd_pid;
46 /* from 2nd call on, remove old list */
47 static void initPid2Machine (void)
49 /* show machine name rather PID on table "Open Files"? */
50 if (PID_or_Machine) {
51 PIDMAP *p, *next;
53 for (p = pidmap; p != NULL; p = next) {
54 next = p->next;
55 DLIST_REMOVE(pidmap, p);
56 SAFE_FREE(p->machine);
57 SAFE_FREE(p);
60 pidmap = NULL;
64 /* add new PID <-> Machine name mapping */
65 static void addPid2Machine (struct server_id pid, const char *machine)
67 /* show machine name rather PID on table "Open Files"? */
68 if (PID_or_Machine) {
69 PIDMAP *newmap;
71 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
72 /* XXX need error message for this?
73 if malloc fails, PID is always shown */
74 return;
77 newmap->pid = pid;
78 newmap->machine = SMB_STRDUP(machine);
80 DLIST_ADD(pidmap, newmap);
84 /* lookup PID <-> Machine name mapping */
85 static char *mapPid2Machine (struct server_id pid)
87 static char pidbuf [64];
88 PIDMAP *map;
90 /* show machine name rather PID on table "Open Files"? */
91 if (PID_or_Machine) {
92 for (map = pidmap; map != NULL; map = map->next) {
93 if (serverid_equal(&pid, &map->pid)) {
94 if (map->machine == NULL) /* no machine name */
95 break; /* show PID */
97 return map->machine;
102 /* PID not in list or machine name NULL? return pid as string */
103 snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
104 procid_str_static(&pid));
105 return pidbuf;
108 static const char *tstring(TALLOC_CTX *ctx, time_t t)
110 char *buf;
111 buf = talloc_strdup(ctx, time_to_asc(t));
112 if (!buf) {
113 return "";
115 buf = talloc_all_string_sub(ctx,
116 buf,
117 " ",
118 "&nbsp;");
119 if (!buf) {
120 return "";
122 return buf;
125 static void print_share_mode(const struct share_mode_entry *e,
126 const char *sharepath,
127 const char *fname,
128 void *dummy)
130 char *utf8_fname;
131 char *utf8_sharepath;
132 int deny_mode;
133 size_t converted_size;
135 if (!is_valid_share_mode_entry(e)) {
136 return;
139 deny_mode = map_share_mode_to_deny_mode(e->share_access,
140 e->private_options);
142 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
143 printf("<td>%u</td>",(unsigned int)e->uid);
144 printf("<td>");
145 switch ((deny_mode>>4)&0xF) {
146 case DENY_NONE: printf("DENY_NONE"); break;
147 case DENY_ALL: printf("DENY_ALL "); break;
148 case DENY_DOS: printf("DENY_DOS "); break;
149 case DENY_FCB: printf("DENY_FCB "); break;
150 case DENY_READ: printf("DENY_READ "); break;
151 case DENY_WRITE:printf("DENY_WRITE "); break;
153 printf("</td>");
155 printf("<td>");
156 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
157 printf("%s", _("RDWR "));
158 } else if (e->access_mask & FILE_WRITE_DATA) {
159 printf("%s", _("WRONLY "));
160 } else {
161 printf("%s", _("RDONLY "));
163 printf("</td>");
165 printf("<td>");
166 if((e->op_type &
167 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
168 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
169 printf("EXCLUSIVE+BATCH ");
170 else if (e->op_type & EXCLUSIVE_OPLOCK)
171 printf("EXCLUSIVE ");
172 else if (e->op_type & BATCH_OPLOCK)
173 printf("BATCH ");
174 else if (e->op_type & LEVEL_II_OPLOCK)
175 printf("LEVEL_II ");
176 else
177 printf("NONE ");
178 printf("</td>");
180 push_utf8_talloc(talloc_tos(), &utf8_fname, fname, &converted_size);
181 push_utf8_talloc(talloc_tos(), &utf8_sharepath, sharepath,
182 &converted_size);
183 printf("<td>%s</td><td>%s</td><td>%s</td></tr>\n",
184 utf8_sharepath,utf8_fname,tstring(talloc_tos(),e->time.tv_sec));
185 TALLOC_FREE(utf8_fname);
189 /* kill off any connections chosen by the user */
190 static int traverse_fn1(const struct connections_key *key,
191 const struct connections_data *crec,
192 void *private_data)
194 if (crec->cnum == TID_FIELD_INVALID && process_exists(crec->pid)) {
195 char buf[30];
196 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
197 if (cgi_variable(buf)) {
198 kill_pid(crec->pid);
199 sleep(SLEEP_TIME);
202 return 0;
205 /* traversal fn for showing machine connections */
206 static int traverse_fn2(const struct connections_key *key,
207 const struct connections_data *crec,
208 void *private_data)
210 if (crec->cnum == TID_FIELD_INVALID || !process_exists(crec->pid) ||
211 serverid_equal(&crec->pid, &smbd_pid))
212 return 0;
214 addPid2Machine (crec->pid, crec->machine);
216 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
217 procid_str_static(&crec->pid),
218 crec->machine, crec->addr,
219 tstring(talloc_tos(),crec->start));
220 if (geteuid() == 0) {
221 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
222 procid_str_static(&crec->pid));
224 printf("</tr>\n");
226 return 0;
229 /* traversal fn for showing share connections */
230 static int traverse_fn3(const struct connections_key *key,
231 const struct connections_data *crec,
232 void *private_data)
234 if (crec->cnum == TID_FIELD_INVALID || !process_exists(crec->pid))
235 return 0;
237 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
238 crec->servicename, uidtoname(crec->uid),
239 gidtoname(crec->gid),procid_str_static(&crec->pid),
240 crec->machine,
241 tstring(talloc_tos(),crec->start));
242 return 0;
246 /* show the current server status */
247 void status_page(void)
249 const char *v;
250 int autorefresh=0;
251 int refresh_interval=30;
252 int nr_running=0;
253 bool waitup = False;
254 TALLOC_CTX *ctx = talloc_stackframe();
255 const char form_name[] = "status";
257 smbd_pid = pid_to_procid(pidfile_pid(lp_piddir(), "smbd"));
259 if (!verify_xsrf_token(form_name)) {
260 goto output_page;
263 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
264 stop_smbd();
265 start_smbd();
266 waitup=True;
269 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
270 start_smbd();
271 waitup=True;
274 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
275 stop_smbd();
276 waitup=True;
279 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
280 stop_nmbd();
281 start_nmbd();
282 waitup=True;
284 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
285 start_nmbd();
286 waitup=True;
289 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
290 stop_nmbd();
291 waitup=True;
294 #ifdef WITH_WINBIND
295 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
296 stop_winbindd();
297 start_winbindd();
298 waitup=True;
301 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
302 start_winbindd();
303 waitup=True;
306 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
307 stop_winbindd();
308 waitup=True;
310 #endif
311 /* wait for daemons to start/stop */
312 if (waitup)
313 sleep(SLEEP_TIME);
315 if (cgi_variable("autorefresh")) {
316 autorefresh = 1;
317 } else if (cgi_variable("norefresh")) {
318 autorefresh = 0;
319 } else if (cgi_variable("refresh")) {
320 autorefresh = 1;
323 if ((v=cgi_variable("refresh_interval"))) {
324 refresh_interval = atoi(v);
327 if (cgi_variable("show_client_in_col_1")) {
328 PID_or_Machine = 1;
331 if (cgi_variable("show_pid_in_col_1")) {
332 PID_or_Machine = 0;
335 connections_forall_read(traverse_fn1, NULL);
337 initPid2Machine ();
339 output_page:
340 printf("<H2>%s</H2>\n", _("Server Status"));
342 printf("<FORM method=post>\n");
343 print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name);
345 if (!autorefresh) {
346 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
347 printf("<br>%s", _("Refresh Interval: "));
348 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
349 refresh_interval);
350 } else {
351 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
352 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
353 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
356 printf("<p>\n");
358 printf("<table>\n");
360 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), samba_version_string());
362 fflush(stdout);
363 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
364 if (geteuid() == 0) {
365 if (smbd_running()) {
366 nr_running++;
367 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
368 } else {
369 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
371 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
373 printf("</tr>\n");
375 fflush(stdout);
376 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
377 if (geteuid() == 0) {
378 if (nmbd_running()) {
379 nr_running++;
380 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
381 } else {
382 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
384 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
386 printf("</tr>\n");
388 #ifdef WITH_WINBIND
389 fflush(stdout);
390 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
391 if (geteuid() == 0) {
392 if (winbindd_running()) {
393 nr_running++;
394 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
395 } else {
396 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
398 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
400 printf("</tr>\n");
401 #endif
403 if (geteuid() == 0) {
404 printf("<tr><td></td><td></td>\n");
405 if (nr_running >= 1) {
406 /* stop, restart all */
407 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
408 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
410 else if (nr_running == 0) {
411 /* start all */
412 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
414 printf("</tr>\n");
416 printf("</table>\n");
417 fflush(stdout);
419 printf("<p><h3>%s</h3>\n", _("Active Connections"));
420 printf("<table border=1>\n");
421 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
422 if (geteuid() == 0) {
423 printf("<th>%s</th>\n", _("Kill"));
425 printf("</tr>\n");
427 connections_forall_read(traverse_fn2, NULL);
429 printf("</table><p>\n");
431 printf("<p><h3>%s</h3>\n", _("Active Shares"));
432 printf("<table border=1>\n");
433 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
434 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
436 connections_forall_read(traverse_fn3, NULL);
438 printf("</table><p>\n");
440 printf("<h3>%s</h3>\n", _("Open Files"));
441 printf("<table border=1>\n");
442 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",
443 _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("Share"), _("File"), _("Date"));
445 locking_init_readonly();
446 share_mode_forall(print_share_mode, NULL);
447 locking_end();
448 printf("</table>\n");
450 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
451 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
453 printf("</FORM>\n");
455 if (autorefresh) {
456 /* this little JavaScript allows for automatic refresh
457 of the page. There are other methods but this seems
458 to be the best alternative */
459 printf("<script language=\"JavaScript\">\n");
460 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
461 cgi_baseurl(),
462 refresh_interval,
463 refresh_interval*1000);
464 printf("//-->\n</script>\n");
466 TALLOC_FREE(ctx);