preparing for release of 2.2.3a
[Samba.git] / source / web / statuspage.c
blob920d0f3367c319eaa9e60724cdb7b70b2d22a451
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.2.
4 web status page
5 Copyright (C) Andrew Tridgell 1997-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 #define PIDMAP struct PidMap
26 PIDMAP {
27 PIDMAP *next, *prev;
28 pid_t pid;
29 char *machine;
32 static PIDMAP *pidmap;
33 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
35 static pid_t smbd_pid;
37 /* from 2nd call on, remove old list */
38 static void initPid2Machine (void)
40 /* show machine name rather PID on table "Open Files"? */
41 if (PID_or_Machine) {
42 PIDMAP *p;
44 for (p = pidmap; p != NULL; ) {
45 DLIST_REMOVE(pidmap, p);
46 SAFE_FREE(p->machine);
47 SAFE_FREE(p);
50 pidmap = NULL;
54 /* add new PID <-> Machine name mapping */
55 static void addPid2Machine (pid_t pid, char *machine)
57 /* show machine name rather PID on table "Open Files"? */
58 if (PID_or_Machine) {
59 PIDMAP *newmap;
61 if ((newmap = (PIDMAP *) malloc (sizeof (PIDMAP))) == NULL) {
62 /* XXX need error message for this?
63 if malloc fails, PID is always shown */
64 return;
67 newmap->pid = pid;
68 newmap->machine = strdup (machine);
70 DLIST_ADD(pidmap, newmap);
74 /* lookup PID <-> Machine name mapping */
75 static char *mapPid2Machine (pid_t pid)
77 static char pidbuf [64];
78 PIDMAP *map;
80 /* show machine name rather PID on table "Open Files"? */
81 if (PID_or_Machine) {
82 for (map = pidmap; map != NULL; map = map->next) {
83 if (pid == map->pid) {
84 if (map->machine == NULL) /* no machine name */
85 break; /* show PID */
87 return map->machine;
92 /* PID not in list or machine name NULL? return pid as string */
93 snprintf (pidbuf, sizeof (pidbuf) - 1, "%d", pid);
94 return pidbuf;
97 static char *tstring(time_t t)
99 static pstring buf;
100 pstrcpy(buf, asctime(LocalTime(&t)));
101 all_string_sub(buf," ","&nbsp;",sizeof(buf));
102 return buf;
105 static void print_share_mode(share_mode_entry *e, char *fname)
107 printf("<tr><td>%s</td>", mapPid2Machine (e->pid));
108 printf("<td>");
109 switch ((e->share_mode>>4)&0xF) {
110 case DENY_NONE: printf("DENY_NONE"); break;
111 case DENY_ALL: printf("DENY_ALL "); break;
112 case DENY_DOS: printf("DENY_DOS "); break;
113 case DENY_READ: printf("DENY_READ "); break;
114 case DENY_WRITE:printf("DENY_WRITE "); break;
116 printf("</td>");
118 printf("<td>");
119 switch (e->share_mode&0xF) {
120 case 0: printf("RDONLY "); break;
121 case 1: printf("WRONLY "); break;
122 case 2: printf("RDWR "); break;
124 printf("</td>");
126 printf("<td>");
127 if((e->op_type &
128 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
129 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
130 printf("EXCLUSIVE+BATCH ");
131 else if (e->op_type & EXCLUSIVE_OPLOCK)
132 printf("EXCLUSIVE ");
133 else if (e->op_type & BATCH_OPLOCK)
134 printf("BATCH ");
135 else if (e->op_type & LEVEL_II_OPLOCK)
136 printf("LEVEL_II ");
137 else
138 printf("NONE ");
139 printf("</td>");
141 printf("<td>%s</td><td>%s</td></tr>\n",
142 dos_to_unix(fname,False),tstring(e->time.tv_sec));
146 /* kill off any connections chosen by the user */
147 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
149 struct connections_data crec;
151 if (dbuf.dsize != sizeof(crec))
152 return 0;
154 memcpy(&crec, dbuf.dptr, sizeof(crec));
156 if (crec.cnum == -1 && process_exists(crec.pid)) {
157 char buf[30];
158 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
159 if (cgi_variable(buf)) {
160 kill_pid(crec.pid);
163 return 0;
166 /* traversal fn for showing machine connections */
167 static int traverse_fn2(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) || (crec.pid == smbd_pid))
177 return 0;
179 addPid2Machine (crec.pid, crec.machine);
181 printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
182 (int)crec.pid,
183 crec.machine,crec.addr,
184 tstring(crec.start));
185 if (geteuid() == 0) {
186 printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
187 (int)crec.pid);
189 printf("</tr>\n");
191 return 0;
194 /* traversal fn for showing share connections */
195 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
197 struct connections_data crec;
199 if (dbuf.dsize != sizeof(crec))
200 return 0;
202 memcpy(&crec, dbuf.dptr, sizeof(crec));
204 if (crec.cnum == -1 || !process_exists(crec.pid))
205 return 0;
207 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
208 crec.name,uidtoname(crec.uid),
209 gidtoname(crec.gid),(int)crec.pid,
210 crec.machine,
211 tstring(crec.start));
212 return 0;
216 /* show the current server status */
217 void status_page(void)
219 char *v;
220 int autorefresh=0;
221 int refresh_interval=30;
222 TDB_CONTEXT *tdb;
224 smbd_pid = pidfile_pid("smbd");
226 if (cgi_variable("smbd_restart")) {
227 stop_smbd();
228 start_smbd();
231 if (cgi_variable("smbd_start")) {
232 start_smbd();
235 if (cgi_variable("smbd_stop")) {
236 stop_smbd();
239 if (cgi_variable("nmbd_restart")) {
240 stop_nmbd();
241 start_nmbd();
243 if (cgi_variable("nmbd_start")) {
244 start_nmbd();
247 if (cgi_variable("nmbd_stop")) {
248 stop_nmbd();
251 if (cgi_variable("autorefresh")) {
252 autorefresh = 1;
253 } else if (cgi_variable("norefresh")) {
254 autorefresh = 0;
255 } else if (cgi_variable("refresh")) {
256 autorefresh = 1;
259 if ((v=cgi_variable("refresh_interval"))) {
260 refresh_interval = atoi(v);
263 if (cgi_variable("show_client_in_col_1")) {
264 PID_or_Machine = 1;
267 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
268 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
270 initPid2Machine ();
272 printf("<H2>Server Status</H2>\n");
274 printf("<FORM method=post>\n");
276 if (!autorefresh) {
277 printf("<input type=submit value=\"Auto Refresh\" name=autorefresh>\n");
278 printf("<br>Refresh Interval: ");
279 printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n",
280 refresh_interval);
281 } else {
282 printf("<input type=submit value=\"Stop Refreshing\" name=norefresh>\n");
283 printf("<br>Refresh Interval: %d\n", refresh_interval);
284 printf("<input type=hidden name=refresh value=1>\n");
287 printf("<p>\n");
289 if (!tdb) {
290 /* open failure either means no connections have been
291 made or status=no */
292 if (!lp_status(-1))
293 printf("You need to have status=yes in your smb config file\n");
297 printf("<table>\n");
299 printf("<tr><td>version:</td><td>%s</td></tr>",VERSION);
301 fflush(stdout);
302 printf("<tr><td>smbd:</td><td>%srunning</td>\n",smbd_running()?"":"not ");
303 if (geteuid() == 0) {
304 if (smbd_running()) {
305 printf("<td><input type=submit name=\"smbd_stop\" value=\"Stop smbd\"></td>\n");
306 } else {
307 printf("<td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td>\n");
309 printf("<td><input type=submit name=\"smbd_restart\" value=\"Restart smbd\"></td>\n");
311 printf("</tr>\n");
313 fflush(stdout);
314 printf("<tr><td>nmbd:</td><td>%srunning</td>\n",nmbd_running()?"":"not ");
315 if (geteuid() == 0) {
316 if (nmbd_running()) {
317 printf("<td><input type=submit name=\"nmbd_stop\" value=\"Stop nmbd\"></td>\n");
318 } else {
319 printf("<td><input type=submit name=\"nmbd_start\" value=\"Start nmbd\"></td>\n");
321 printf("<td><input type=submit name=\"nmbd_restart\" value=\"Restart nmbd\"></td>\n");
323 printf("</tr>\n");
325 printf("</table>\n");
326 fflush(stdout);
328 printf("<p><h3>Active Connections</h3>\n");
329 printf("<table border=1>\n");
330 printf("<tr><th>PID</th><th>Client</th><th>IP address</th><th>Date</th>\n");
331 if (geteuid() == 0) {
332 printf("<th>Kill</th>\n");
334 printf("</tr>\n");
336 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
338 printf("</table><p>\n");
340 printf("<p><h3>Active Shares</h3>\n");
341 printf("<table border=1>\n");
342 printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
344 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
346 printf("</table><p>\n");
348 printf("<h3>Open Files</h3>\n");
349 printf("<table border=1>\n");
350 printf("<tr><th>%s</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n", PID_or_Machine ? "Client" : "PID");
352 locking_init(1);
353 share_mode_forall(print_share_mode);
354 locking_end();
355 printf("</table>\n");
357 if (tdb) tdb_close(tdb);
359 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"Show Client in col 1\">\n");
360 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"Show PID in col 1\">\n");
362 printf("</FORM>\n");
364 if (autorefresh) {
365 /* this little JavaScript allows for automatic refresh
366 of the page. There are other methods but this seems
367 to be the best alternative */
368 printf("<script language=\"JavaScript\">\n");
369 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
370 cgi_baseurl(),
371 refresh_interval,
372 refresh_interval*1000);
373 printf("//-->\n</script>\n");