r16472: final pass for 3.0.23rc3 I think. Current with SAMBA_3_0 r16471
[Samba.git] / source / web / statuspage.c
blobcb6fa9117118afaff721e7c873a6b557178340ce
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, time_to_asc(&t));
105 all_string_sub(buf," ","&nbsp;",sizeof(buf));
106 return buf;
109 static void print_share_mode(const struct share_mode_entry *e, const char *sharepath, const char *fname)
111 char *utf8_fname;
112 int deny_mode;
114 if (!is_valid_share_mode_entry(e)) {
115 return;
118 deny_mode = map_share_mode_to_deny_mode(e->share_access,
119 e->private_options);
121 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
122 printf("<td>%u</td>",(unsigned int)e->uid);
123 printf("<td>");
124 switch ((deny_mode>>4)&0xF) {
125 case DENY_NONE: printf("DENY_NONE"); break;
126 case DENY_ALL: printf("DENY_ALL "); break;
127 case DENY_DOS: printf("DENY_DOS "); break;
128 case DENY_FCB: printf("DENY_FCB "); break;
129 case DENY_READ: printf("DENY_READ "); break;
130 case DENY_WRITE:printf("DENY_WRITE "); break;
132 printf("</td>");
134 printf("<td>");
135 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
136 printf("%s", _("RDWR "));
137 } else if (e->access_mask & FILE_WRITE_DATA) {
138 printf("%s", _("WRONLY "));
139 } else {
140 printf("%s", _("RDONLY "));
142 printf("</td>");
144 printf("<td>");
145 if((e->op_type &
146 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
147 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
148 printf("EXCLUSIVE+BATCH ");
149 else if (e->op_type & EXCLUSIVE_OPLOCK)
150 printf("EXCLUSIVE ");
151 else if (e->op_type & BATCH_OPLOCK)
152 printf("BATCH ");
153 else if (e->op_type & LEVEL_II_OPLOCK)
154 printf("LEVEL_II ");
155 else
156 printf("NONE ");
157 printf("</td>");
159 push_utf8_allocate(&utf8_fname, fname);
160 printf("<td>%s</td><td>%s</td></tr>\n",
161 utf8_fname,tstring(e->time.tv_sec));
162 SAFE_FREE(utf8_fname);
166 /* kill off any connections chosen by the user */
167 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
169 struct connections_data crec;
171 if (dbuf.dsize != sizeof(crec))
172 return 0;
174 memcpy(&crec, dbuf.dptr, sizeof(crec));
176 if (crec.cnum == -1 && process_exists(crec.pid)) {
177 char buf[30];
178 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec.pid));
179 if (cgi_variable(buf)) {
180 kill_pid(crec.pid);
181 sleep(SLEEP_TIME);
184 return 0;
187 /* traversal fn for showing machine connections */
188 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
190 struct connections_data crec;
192 if (dbuf.dsize != sizeof(crec))
193 return 0;
195 memcpy(&crec, dbuf.dptr, sizeof(crec));
197 if (crec.cnum == -1 || !process_exists(crec.pid) ||
198 procid_equal(&crec.pid, &smbd_pid))
199 return 0;
201 addPid2Machine (crec.pid, crec.machine);
203 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
204 procid_str_static(&crec.pid),
205 crec.machine,crec.addr,
206 tstring(crec.start));
207 if (geteuid() == 0) {
208 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
209 procid_str_static(&crec.pid));
211 printf("</tr>\n");
213 return 0;
216 /* traversal fn for showing share connections */
217 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
219 struct connections_data crec;
221 if (dbuf.dsize != sizeof(crec))
222 return 0;
224 memcpy(&crec, dbuf.dptr, sizeof(crec));
226 if (crec.cnum == -1 || !process_exists(crec.pid))
227 return 0;
229 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
230 crec.name,uidtoname(crec.uid),
231 gidtoname(crec.gid),procid_str_static(&crec.pid),
232 crec.machine,
233 tstring(crec.start));
234 return 0;
238 /* show the current server status */
239 void status_page(void)
241 const char *v;
242 int autorefresh=0;
243 int refresh_interval=30;
244 TDB_CONTEXT *tdb;
245 int nr_running=0;
246 BOOL waitup = False;
248 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
250 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
251 stop_smbd();
252 start_smbd();
253 waitup=True;
256 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
257 start_smbd();
258 waitup=True;
261 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
262 stop_smbd();
263 waitup=True;
266 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
267 stop_nmbd();
268 start_nmbd();
269 waitup=True;
271 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
272 start_nmbd();
273 waitup=True;
276 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
277 stop_nmbd();
278 waitup=True;
281 #ifdef WITH_WINBIND
282 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
283 stop_winbindd();
284 start_winbindd();
285 waitup=True;
288 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
289 start_winbindd();
290 waitup=True;
293 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
294 stop_winbindd();
295 waitup=True;
297 #endif
298 /* wait for daemons to start/stop */
299 if (waitup)
300 sleep(SLEEP_TIME);
302 if (cgi_variable("autorefresh")) {
303 autorefresh = 1;
304 } else if (cgi_variable("norefresh")) {
305 autorefresh = 0;
306 } else if (cgi_variable("refresh")) {
307 autorefresh = 1;
310 if ((v=cgi_variable("refresh_interval"))) {
311 refresh_interval = atoi(v);
314 if (cgi_variable("show_client_in_col_1")) {
315 PID_or_Machine = 1;
318 if (cgi_variable("show_pid_in_col_1")) {
319 PID_or_Machine = 0;
322 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
323 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
325 initPid2Machine ();
327 printf("<H2>%s</H2>\n", _("Server Status"));
329 printf("<FORM method=post>\n");
331 if (!autorefresh) {
332 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
333 printf("<br>%s", _("Refresh Interval: "));
334 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
335 refresh_interval);
336 } else {
337 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
338 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
339 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
342 printf("<p>\n");
344 if (!tdb) {
345 /* open failure either means no connections have been
346 made */
350 printf("<table>\n");
352 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
354 fflush(stdout);
355 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
356 if (geteuid() == 0) {
357 if (smbd_running()) {
358 nr_running++;
359 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
360 } else {
361 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
363 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
365 printf("</tr>\n");
367 fflush(stdout);
368 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
369 if (geteuid() == 0) {
370 if (nmbd_running()) {
371 nr_running++;
372 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
373 } else {
374 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
376 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
378 printf("</tr>\n");
380 #ifdef WITH_WINBIND
381 fflush(stdout);
382 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
383 if (geteuid() == 0) {
384 if (winbindd_running()) {
385 nr_running++;
386 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
387 } else {
388 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
390 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
392 printf("</tr>\n");
393 #endif
395 if (geteuid() == 0) {
396 printf("<tr><td></td><td></td>\n");
397 if (nr_running >= 1) {
398 /* stop, restart all */
399 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
400 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
402 else if (nr_running == 0) {
403 /* start all */
404 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
406 printf("</tr>\n");
408 printf("</table>\n");
409 fflush(stdout);
411 printf("<p><h3>%s</h3>\n", _("Active Connections"));
412 printf("<table border=1>\n");
413 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
414 if (geteuid() == 0) {
415 printf("<th>%s</th>\n", _("Kill"));
417 printf("</tr>\n");
419 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
421 printf("</table><p>\n");
423 printf("<p><h3>%s</h3>\n", _("Active Shares"));
424 printf("<table border=1>\n");
425 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
426 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
428 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
430 printf("</table><p>\n");
432 printf("<h3>%s</h3>\n", _("Open Files"));
433 printf("<table border=1>\n");
434 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"));
436 locking_init(1);
437 share_mode_forall(print_share_mode);
438 locking_end();
439 printf("</table>\n");
441 if (tdb) tdb_close(tdb);
443 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
444 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
446 printf("</FORM>\n");
448 if (autorefresh) {
449 /* this little JavaScript allows for automatic refresh
450 of the page. There are other methods but this seems
451 to be the best alternative */
452 printf("<script language=\"JavaScript\">\n");
453 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
454 cgi_baseurl(),
455 refresh_interval,
456 refresh_interval*1000);
457 printf("//-->\n</script>\n");