r10656: BIG merge from trunk. Features not copied over
[Samba/nascimento.git] / source3 / web / statuspage.c
blobedc03183730d14c97a7eabf7bd772c1d65cfae0a
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "web/swat_proto.h"
24 #define PIDMAP struct PidMap
26 /* how long to wait for start/stops to take effect */
27 #define SLEEP_TIME 3
29 PIDMAP {
30 PIDMAP *next, *prev;
31 struct process_id pid;
32 char *machine;
35 static PIDMAP *pidmap;
36 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
38 static struct process_id smbd_pid;
40 /* from 2nd call on, remove old list */
41 static void initPid2Machine (void)
43 /* show machine name rather PID on table "Open Files"? */
44 if (PID_or_Machine) {
45 PIDMAP *p;
47 for (p = pidmap; p != NULL; ) {
48 DLIST_REMOVE(pidmap, p);
49 SAFE_FREE(p->machine);
50 SAFE_FREE(p);
53 pidmap = NULL;
57 /* add new PID <-> Machine name mapping */
58 static void addPid2Machine (struct process_id pid, char *machine)
60 /* show machine name rather PID on table "Open Files"? */
61 if (PID_or_Machine) {
62 PIDMAP *newmap;
64 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
65 /* XXX need error message for this?
66 if malloc fails, PID is always shown */
67 return;
70 newmap->pid = pid;
71 newmap->machine = SMB_STRDUP(machine);
73 DLIST_ADD(pidmap, newmap);
77 /* lookup PID <-> Machine name mapping */
78 static char *mapPid2Machine (struct process_id pid)
80 static char pidbuf [64];
81 PIDMAP *map;
83 /* show machine name rather PID on table "Open Files"? */
84 if (PID_or_Machine) {
85 for (map = pidmap; map != NULL; map = map->next) {
86 if (procid_equal(&pid, &map->pid)) {
87 if (map->machine == NULL) /* no machine name */
88 break; /* show PID */
90 return map->machine;
95 /* PID not in list or machine name NULL? return pid as string */
96 snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
97 procid_str_static(&pid));
98 return pidbuf;
101 static char *tstring(time_t t)
103 static pstring buf;
104 pstrcpy(buf, asctime(LocalTime(&t)));
105 all_string_sub(buf," ","&nbsp;",sizeof(buf));
106 return buf;
109 static void print_share_mode(const struct share_mode_entry *e, char *fname)
111 char *utf8_fname;
112 int deny_mode = map_share_mode_to_deny_mode(e->share_access,
113 e->private_options);
115 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
116 printf("<td>");
117 switch ((deny_mode>>4)&0xF) {
118 case DENY_NONE: printf("DENY_NONE"); break;
119 case DENY_ALL: printf("DENY_ALL "); break;
120 case DENY_DOS: printf("DENY_DOS "); break;
121 case DENY_FCB: printf("DENY_FCB "); break;
122 case DENY_READ: printf("DENY_READ "); break;
123 case DENY_WRITE:printf("DENY_WRITE "); break;
125 printf("</td>");
127 printf("<td>");
128 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
129 printf("%s", _("RDWR "));
130 } else if (e->access_mask & FILE_WRITE_DATA) {
131 printf("%s", _("WRONLY "));
132 } else {
133 printf("%s", _("RDONLY "));
135 printf("</td>");
137 printf("<td>");
138 if((e->op_type &
139 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
140 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
141 printf("EXCLUSIVE+BATCH ");
142 else if (e->op_type & EXCLUSIVE_OPLOCK)
143 printf("EXCLUSIVE ");
144 else if (e->op_type & BATCH_OPLOCK)
145 printf("BATCH ");
146 else if (e->op_type & LEVEL_II_OPLOCK)
147 printf("LEVEL_II ");
148 else
149 printf("NONE ");
150 printf("</td>");
152 push_utf8_allocate(&utf8_fname, fname);
153 printf("<td>%s</td><td>%s</td></tr>\n",
154 utf8_fname,tstring(e->time.tv_sec));
155 SAFE_FREE(utf8_fname);
159 /* kill off any connections chosen by the user */
160 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
162 struct connections_data crec;
164 if (dbuf.dsize != sizeof(crec))
165 return 0;
167 memcpy(&crec, dbuf.dptr, sizeof(crec));
169 if (crec.cnum == -1 && process_exists(crec.pid)) {
170 char buf[30];
171 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec.pid));
172 if (cgi_variable(buf)) {
173 kill_pid(crec.pid);
174 sleep(SLEEP_TIME);
177 return 0;
180 /* traversal fn for showing machine connections */
181 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
183 struct connections_data crec;
185 if (dbuf.dsize != sizeof(crec))
186 return 0;
188 memcpy(&crec, dbuf.dptr, sizeof(crec));
190 if (crec.cnum == -1 || !process_exists(crec.pid) ||
191 procid_equal(&crec.pid, &smbd_pid))
192 return 0;
194 addPid2Machine (crec.pid, crec.machine);
196 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
197 procid_str_static(&crec.pid),
198 crec.machine,crec.addr,
199 tstring(crec.start));
200 if (geteuid() == 0) {
201 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
202 procid_str_static(&crec.pid));
204 printf("</tr>\n");
206 return 0;
209 /* traversal fn for showing share connections */
210 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
212 struct connections_data crec;
214 if (dbuf.dsize != sizeof(crec))
215 return 0;
217 memcpy(&crec, dbuf.dptr, sizeof(crec));
219 if (crec.cnum == -1 || !process_exists(crec.pid))
220 return 0;
222 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
223 crec.name,uidtoname(crec.uid),
224 gidtoname(crec.gid),procid_str_static(&crec.pid),
225 crec.machine,
226 tstring(crec.start));
227 return 0;
231 /* show the current server status */
232 void status_page(void)
234 const char *v;
235 int autorefresh=0;
236 int refresh_interval=30;
237 TDB_CONTEXT *tdb;
238 int nr_running=0;
239 BOOL waitup = False;
241 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
243 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
244 stop_smbd();
245 start_smbd();
246 waitup=True;
249 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
250 start_smbd();
251 waitup=True;
254 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
255 stop_smbd();
256 waitup=True;
259 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
260 stop_nmbd();
261 start_nmbd();
262 waitup=True;
264 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
265 start_nmbd();
266 waitup=True;
269 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
270 stop_nmbd();
271 waitup=True;
274 #ifdef WITH_WINBIND
275 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
276 stop_winbindd();
277 start_winbindd();
278 waitup=True;
281 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
282 start_winbindd();
283 waitup=True;
286 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
287 stop_winbindd();
288 waitup=True;
290 #endif
291 /* wait for daemons to start/stop */
292 if (waitup)
293 sleep(SLEEP_TIME);
295 if (cgi_variable("autorefresh")) {
296 autorefresh = 1;
297 } else if (cgi_variable("norefresh")) {
298 autorefresh = 0;
299 } else if (cgi_variable("refresh")) {
300 autorefresh = 1;
303 if ((v=cgi_variable("refresh_interval"))) {
304 refresh_interval = atoi(v);
307 if (cgi_variable("show_client_in_col_1")) {
308 PID_or_Machine = 1;
311 if (cgi_variable("show_pid_in_col_1")) {
312 PID_or_Machine = 0;
315 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
316 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
318 initPid2Machine ();
320 printf("<H2>%s</H2>\n", _("Server Status"));
322 printf("<FORM method=post>\n");
324 if (!autorefresh) {
325 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
326 printf("<br>%s", _("Refresh Interval: "));
327 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
328 refresh_interval);
329 } else {
330 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
331 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
332 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
335 printf("<p>\n");
337 if (!tdb) {
338 /* open failure either means no connections have been
339 made */
343 printf("<table>\n");
345 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
347 fflush(stdout);
348 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
349 if (geteuid() == 0) {
350 if (smbd_running()) {
351 nr_running++;
352 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
353 } else {
354 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
356 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
358 printf("</tr>\n");
360 fflush(stdout);
361 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
362 if (geteuid() == 0) {
363 if (nmbd_running()) {
364 nr_running++;
365 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
366 } else {
367 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
369 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
371 printf("</tr>\n");
373 #ifdef WITH_WINBIND
374 fflush(stdout);
375 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
376 if (geteuid() == 0) {
377 if (winbindd_running()) {
378 nr_running++;
379 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
380 } else {
381 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
383 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
385 printf("</tr>\n");
386 #endif
388 if (geteuid() == 0) {
389 printf("<tr><td></td><td></td>\n");
390 if (nr_running >= 1) {
391 /* stop, restart all */
392 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
393 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
395 else if (nr_running == 0) {
396 /* start all */
397 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
399 printf("</tr>\n");
401 printf("</table>\n");
402 fflush(stdout);
404 printf("<p><h3>%s</h3>\n", _("Active Connections"));
405 printf("<table border=1>\n");
406 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
407 if (geteuid() == 0) {
408 printf("<th>%s</th>\n", _("Kill"));
410 printf("</tr>\n");
412 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
414 printf("</table><p>\n");
416 printf("<p><h3>%s</h3>\n", _("Active Shares"));
417 printf("<table border=1>\n");
418 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
419 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
421 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
423 printf("</table><p>\n");
425 printf("<h3>%s</h3>\n", _("Open Files"));
426 printf("<table border=1>\n");
427 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
429 locking_init(1);
430 share_mode_forall(print_share_mode);
431 locking_end();
432 printf("</table>\n");
434 if (tdb) tdb_close(tdb);
436 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
437 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
439 printf("</FORM>\n");
441 if (autorefresh) {
442 /* this little JavaScript allows for automatic refresh
443 of the page. There are other methods but this seems
444 to be the best alternative */
445 printf("<script language=\"JavaScript\">\n");
446 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
447 cgi_baseurl(),
448 refresh_interval,
449 refresh_interval*1000);
450 printf("//-->\n</script>\n");