Fix bug #6040 - Missing <th> header in Status page.
[Samba.git] / source / web / statuspage.c
blob80f079eed0d744bf2c04d84eb92abf86244e4791
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"
23 #define _(x) lang_msg_rotate(talloc_tos(),x)
25 #define PIDMAP struct PidMap
27 /* how long to wait for start/stops to take effect */
28 #define SLEEP_TIME 3
30 PIDMAP {
31 PIDMAP *next, *prev;
32 struct server_id pid;
33 char *machine;
36 static PIDMAP *pidmap;
37 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
39 static struct server_id smbd_pid;
41 /* from 2nd call on, remove old list */
42 static void initPid2Machine (void)
44 /* show machine name rather PID on table "Open Files"? */
45 if (PID_or_Machine) {
46 PIDMAP *p, *next;
48 for (p = pidmap; p != NULL; p = next) {
49 next = p->next;
50 DLIST_REMOVE(pidmap, p);
51 SAFE_FREE(p->machine);
52 SAFE_FREE(p);
55 pidmap = NULL;
59 /* add new PID <-> Machine name mapping */
60 static void addPid2Machine (struct server_id pid, const char *machine)
62 /* show machine name rather PID on table "Open Files"? */
63 if (PID_or_Machine) {
64 PIDMAP *newmap;
66 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
67 /* XXX need error message for this?
68 if malloc fails, PID is always shown */
69 return;
72 newmap->pid = pid;
73 newmap->machine = SMB_STRDUP(machine);
75 DLIST_ADD(pidmap, newmap);
79 /* lookup PID <-> Machine name mapping */
80 static char *mapPid2Machine (struct server_id pid)
82 static char pidbuf [64];
83 PIDMAP *map;
85 /* show machine name rather PID on table "Open Files"? */
86 if (PID_or_Machine) {
87 for (map = pidmap; map != NULL; map = map->next) {
88 if (procid_equal(&pid, &map->pid)) {
89 if (map->machine == NULL) /* no machine name */
90 break; /* show PID */
92 return map->machine;
97 /* PID not in list or machine name NULL? return pid as string */
98 snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
99 procid_str_static(&pid));
100 return pidbuf;
103 static const char *tstring(TALLOC_CTX *ctx, time_t t)
105 char *buf;
106 buf = talloc_strdup(ctx, time_to_asc(t));
107 if (!buf) {
108 return "";
110 buf = talloc_all_string_sub(ctx,
111 buf,
112 " ",
113 "&nbsp;");
114 if (!buf) {
115 return "";
117 return buf;
120 static void print_share_mode(const struct share_mode_entry *e,
121 const char *sharepath,
122 const char *fname,
123 void *dummy)
125 char *utf8_fname;
126 int deny_mode;
128 if (!is_valid_share_mode_entry(e)) {
129 return;
132 deny_mode = map_share_mode_to_deny_mode(e->share_access,
133 e->private_options);
135 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
136 printf("<td>%u</td>",(unsigned int)e->uid);
137 printf("<td>");
138 switch ((deny_mode>>4)&0xF) {
139 case DENY_NONE: printf("DENY_NONE"); break;
140 case DENY_ALL: printf("DENY_ALL "); break;
141 case DENY_DOS: printf("DENY_DOS "); break;
142 case DENY_FCB: printf("DENY_FCB "); break;
143 case DENY_READ: printf("DENY_READ "); break;
144 case DENY_WRITE:printf("DENY_WRITE "); break;
146 printf("</td>");
148 printf("<td>");
149 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
150 printf("%s", _("RDWR "));
151 } else if (e->access_mask & FILE_WRITE_DATA) {
152 printf("%s", _("WRONLY "));
153 } else {
154 printf("%s", _("RDONLY "));
156 printf("</td>");
158 printf("<td>");
159 if((e->op_type &
160 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
161 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
162 printf("EXCLUSIVE+BATCH ");
163 else if (e->op_type & EXCLUSIVE_OPLOCK)
164 printf("EXCLUSIVE ");
165 else if (e->op_type & BATCH_OPLOCK)
166 printf("BATCH ");
167 else if (e->op_type & LEVEL_II_OPLOCK)
168 printf("LEVEL_II ");
169 else
170 printf("NONE ");
171 printf("</td>");
173 push_utf8_allocate(&utf8_fname, fname);
174 printf("<td>%s</td><td>%s</td></tr>\n",
175 utf8_fname,tstring(talloc_tos(),e->time.tv_sec));
176 SAFE_FREE(utf8_fname);
180 /* kill off any connections chosen by the user */
181 static int traverse_fn1(struct db_record *rec,
182 const struct connections_key *key,
183 const struct connections_data *crec,
184 void *private_data)
186 if (crec->cnum == -1 && process_exists(crec->pid)) {
187 char buf[30];
188 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
189 if (cgi_variable(buf)) {
190 kill_pid(crec->pid);
191 sleep(SLEEP_TIME);
194 return 0;
197 /* traversal fn for showing machine connections */
198 static int traverse_fn2(struct db_record *rec,
199 const struct connections_key *key,
200 const struct connections_data *crec,
201 void *private_data)
203 if (crec->cnum == -1 || !process_exists(crec->pid) ||
204 procid_equal(&crec->pid, &smbd_pid))
205 return 0;
207 addPid2Machine (crec->pid, crec->machine);
209 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
210 procid_str_static(&crec->pid),
211 crec->machine, crec->addr,
212 tstring(talloc_tos(),crec->start));
213 if (geteuid() == 0) {
214 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
215 procid_str_static(&crec->pid));
217 printf("</tr>\n");
219 return 0;
222 /* traversal fn for showing share connections */
223 static int traverse_fn3(struct db_record *rec,
224 const struct connections_key *key,
225 const struct connections_data *crec,
226 void *private_data)
228 if (crec->cnum == -1 || !process_exists(crec->pid))
229 return 0;
231 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
232 crec->servicename, uidtoname(crec->uid),
233 gidtoname(crec->gid),procid_str_static(&crec->pid),
234 crec->machine,
235 tstring(talloc_tos(),crec->start));
236 return 0;
240 /* show the current server status */
241 void status_page(void)
243 const char *v;
244 int autorefresh=0;
245 int refresh_interval=30;
246 int nr_running=0;
247 bool waitup = False;
248 TALLOC_CTX *ctx = talloc_stackframe();
250 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
252 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
253 stop_smbd();
254 start_smbd();
255 waitup=True;
258 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
259 start_smbd();
260 waitup=True;
263 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
264 stop_smbd();
265 waitup=True;
268 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
269 stop_nmbd();
270 start_nmbd();
271 waitup=True;
273 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
274 start_nmbd();
275 waitup=True;
278 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
279 stop_nmbd();
280 waitup=True;
283 #ifdef WITH_WINBIND
284 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
285 stop_winbindd();
286 start_winbindd();
287 waitup=True;
290 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
291 start_winbindd();
292 waitup=True;
295 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
296 stop_winbindd();
297 waitup=True;
299 #endif
300 /* wait for daemons to start/stop */
301 if (waitup)
302 sleep(SLEEP_TIME);
304 if (cgi_variable("autorefresh")) {
305 autorefresh = 1;
306 } else if (cgi_variable("norefresh")) {
307 autorefresh = 0;
308 } else if (cgi_variable("refresh")) {
309 autorefresh = 1;
312 if ((v=cgi_variable("refresh_interval"))) {
313 refresh_interval = atoi(v);
316 if (cgi_variable("show_client_in_col_1")) {
317 PID_or_Machine = 1;
320 if (cgi_variable("show_pid_in_col_1")) {
321 PID_or_Machine = 0;
324 connections_forall(traverse_fn1, NULL);
326 initPid2Machine ();
328 printf("<H2>%s</H2>\n", _("Server Status"));
330 printf("<FORM method=post>\n");
332 if (!autorefresh) {
333 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
334 printf("<br>%s", _("Refresh Interval: "));
335 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
336 refresh_interval);
337 } else {
338 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
339 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
340 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
343 printf("<p>\n");
345 printf("<table>\n");
347 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
349 fflush(stdout);
350 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
351 if (geteuid() == 0) {
352 if (smbd_running()) {
353 nr_running++;
354 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
355 } else {
356 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
358 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
360 printf("</tr>\n");
362 fflush(stdout);
363 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
364 if (geteuid() == 0) {
365 if (nmbd_running()) {
366 nr_running++;
367 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
368 } else {
369 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
371 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
373 printf("</tr>\n");
375 #ifdef WITH_WINBIND
376 fflush(stdout);
377 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
378 if (geteuid() == 0) {
379 if (winbindd_running()) {
380 nr_running++;
381 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
382 } else {
383 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
385 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
387 printf("</tr>\n");
388 #endif
390 if (geteuid() == 0) {
391 printf("<tr><td></td><td></td>\n");
392 if (nr_running >= 1) {
393 /* stop, restart all */
394 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
395 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
397 else if (nr_running == 0) {
398 /* start all */
399 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
401 printf("</tr>\n");
403 printf("</table>\n");
404 fflush(stdout);
406 printf("<p><h3>%s</h3>\n", _("Active Connections"));
407 printf("<table border=1>\n");
408 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
409 if (geteuid() == 0) {
410 printf("<th>%s</th>\n", _("Kill"));
412 printf("</tr>\n");
414 connections_forall(traverse_fn2, NULL);
416 printf("</table><p>\n");
418 printf("<p><h3>%s</h3>\n", _("Active Shares"));
419 printf("<table border=1>\n");
420 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
421 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
423 connections_forall(traverse_fn3, NULL);
425 printf("</table><p>\n");
427 printf("<h3>%s</h3>\n", _("Open Files"));
428 printf("<table border=1>\n");
429 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n",
430 _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
432 locking_init_readonly();
433 share_mode_forall(print_share_mode, NULL);
434 locking_end();
435 printf("</table>\n");
437 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
438 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
440 printf("</FORM>\n");
442 if (autorefresh) {
443 /* this little JavaScript allows for automatic refresh
444 of the page. There are other methods but this seems
445 to be the best alternative */
446 printf("<script language=\"JavaScript\">\n");
447 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
448 cgi_baseurl(),
449 refresh_interval,
450 refresh_interval*1000);
451 printf("//-->\n</script>\n");
453 TALLOC_FREE(ctx);