Added Stephen Roylance's patch - Adds to Status page, a Start All, Restart All
[ira/wip.git] / source3 / web / statuspage.c
1 /* 
2    Unix SMB/CIFS implementation.
3    web status page
4    Copyright (C) Andrew Tridgell 1997-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "../web/swat_proto.h"
23
24 #define PIDMAP          struct PidMap
25
26 PIDMAP {
27         PIDMAP  *next, *prev;
28         pid_t   pid;
29         char    *machine;
30 };
31
32 static PIDMAP   *pidmap;
33 static int      PID_or_Machine;         /* 0 = show PID, else show Machine name */
34
35 static pid_t smbd_pid;
36
37 /* from 2nd call on, remove old list */
38 static void initPid2Machine (void)
39 {
40         /* show machine name rather PID on table "Open Files"? */
41         if (PID_or_Machine) {
42                 PIDMAP *p;
43
44                 for (p = pidmap; p != NULL; ) {
45                         DLIST_REMOVE(pidmap, p);
46                         SAFE_FREE(p->machine);
47                         SAFE_FREE(p);
48                 }
49
50                 pidmap = NULL;
51         }
52 }
53
54 /* add new PID <-> Machine name mapping */
55 static void addPid2Machine (pid_t pid, char *machine)
56 {
57         /* show machine name rather PID on table "Open Files"? */
58         if (PID_or_Machine) {
59                 PIDMAP *newmap;
60
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;
65                 }
66
67                 newmap->pid = pid;
68                 newmap->machine = strdup (machine);
69
70                 DLIST_ADD(pidmap, newmap);
71         }
72 }
73
74 /* lookup PID <-> Machine name mapping */
75 static char *mapPid2Machine (pid_t pid)
76 {
77         static char pidbuf [64];
78         PIDMAP *map;
79
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 */
86
87                                 return map->machine;
88                         }
89                 }
90         }
91
92         /* PID not in list or machine name NULL? return pid as string */
93         snprintf (pidbuf, sizeof (pidbuf) - 1, "%d", pid);
94         return pidbuf;
95 }
96
97 static char *tstring(time_t t)
98 {
99         static pstring buf;
100         pstrcpy(buf, asctime(LocalTime(&t)));
101         all_string_sub(buf," ","&nbsp;",sizeof(buf));
102         return buf;
103 }
104
105 static void print_share_mode(share_mode_entry *e, char *fname)
106 {
107         d_printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
108         d_printf("<td>");
109         switch ((e->share_mode>>4)&0xF) {
110         case DENY_NONE: d_printf("DENY_NONE"); break;
111         case DENY_ALL:  d_printf("DENY_ALL   "); break;
112         case DENY_DOS:  d_printf("DENY_DOS   "); break;
113         case DENY_READ: d_printf("DENY_READ  "); break;
114         case DENY_WRITE:d_printf("DENY_WRITE "); break;
115         }
116         d_printf("</td>");
117
118         d_printf("<td>");
119         switch (e->share_mode&0xF) {
120         case 0: d_printf("RDONLY     "); break;
121         case 1: d_printf("WRONLY     "); break;
122         case 2: d_printf("RDWR       "); break;
123         }
124         d_printf("</td>");
125
126         d_printf("<td>");
127         if((e->op_type & 
128             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
129            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
130                 d_printf("EXCLUSIVE+BATCH ");
131         else if (e->op_type & EXCLUSIVE_OPLOCK)
132                 d_printf("EXCLUSIVE       ");
133         else if (e->op_type & BATCH_OPLOCK)
134                 d_printf("BATCH           ");
135         else if (e->op_type & LEVEL_II_OPLOCK)
136                 d_printf("LEVEL_II        ");
137         else
138                 d_printf("NONE            ");
139         d_printf("</td>");
140
141         d_printf("<td>%s</td><td>%s</td></tr>\n",
142                fname,tstring(e->time.tv_sec));
143 }
144
145
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)
148 {
149         struct connections_data crec;
150
151         if (dbuf.dsize != sizeof(crec))
152                 return 0;
153
154         memcpy(&crec, dbuf.dptr, sizeof(crec));
155
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);
161                 }
162         }
163         return 0;
164 }
165
166 /* traversal fn for showing machine connections */
167 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
168 {
169         struct connections_data crec;
170
171         if (dbuf.dsize != sizeof(crec))
172                 return 0;
173
174         memcpy(&crec, dbuf.dptr, sizeof(crec));
175         
176         if (crec.cnum != -1 || !process_exists(crec.pid) || (crec.pid == smbd_pid))
177                 return 0;
178
179         addPid2Machine (crec.pid, crec.machine);
180
181         d_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                 d_printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
187                        (int)crec.pid);
188         }
189         d_printf("</tr>\n");
190
191         return 0;
192 }
193
194 /* traversal fn for showing share connections */
195 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
196 {
197         struct connections_data crec;
198
199         if (dbuf.dsize != sizeof(crec))
200                 return 0;
201
202         memcpy(&crec, dbuf.dptr, sizeof(crec));
203
204         if (crec.cnum == -1 || !process_exists(crec.pid))
205                 return 0;
206
207         d_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;
213 }
214
215
216 /* show the current server status */
217 void status_page(void)
218 {
219         const char *v;
220         int autorefresh=0;
221         int refresh_interval=30;
222         TDB_CONTEXT *tdb;
223         int nr_running=0;
224
225         smbd_pid = pidfile_pid("smbd");
226
227         if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
228                 stop_smbd();
229                 start_smbd();
230         }
231
232         if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
233                 start_smbd();
234         }
235
236         if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
237                 stop_smbd();
238         }
239
240         if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
241                 stop_nmbd();
242                 start_nmbd();
243         }
244         if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
245                 start_nmbd();
246         }
247
248         if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
249                 stop_nmbd();
250         }
251
252 #ifdef WITH_WINBIND
253         if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
254                 stop_winbindd();
255                 start_winbindd();
256         }
257
258         if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
259                 start_winbindd();
260         }
261
262         if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
263                 stop_winbindd();
264         }
265 #endif
266         if (cgi_variable("autorefresh")) {
267                 autorefresh = 1;
268         } else if (cgi_variable("norefresh")) {
269                 autorefresh = 0;
270         } else if (cgi_variable("refresh")) {
271                 autorefresh = 1;
272         }
273
274         if ((v=cgi_variable("refresh_interval"))) {
275                 refresh_interval = atoi(v);
276         }
277
278         if (cgi_variable("show_client_in_col_1")) {
279                 PID_or_Machine = 1;
280         }
281
282         tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
283         if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
284  
285         initPid2Machine ();
286
287         d_printf("<H2>%s</H2>\n", _("Server Status"));
288
289         d_printf("<FORM method=post>\n");
290
291         if (!autorefresh) {
292                 d_printf("<input type=submit value=\"%s\" name=autorefresh>\n", _("Auto Refresh"));
293                 d_printf("<br>%s", _("Refresh Interval: "));
294                 d_printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n", 
295                        refresh_interval);
296         } else {
297                 d_printf("<input type=submit value=\"%s\" name=norefresh>\n", _("Stop Refreshing"));
298                 d_printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
299                 d_printf("<input type=hidden name=refresh value=1>\n");
300         }
301
302         d_printf("<p>\n");
303
304         if (!tdb) {
305                 /* open failure either means no connections have been
306                    made */
307         }
308
309
310         d_printf("<table>\n");
311
312         d_printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), VERSION);
313
314         fflush(stdout);
315         d_printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
316         if (geteuid() == 0) {
317             if (smbd_running()) {
318                 nr_running++;
319                 d_printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
320             } else {
321                 d_printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
322             }
323             d_printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
324         }
325         d_printf("</tr>\n");
326
327         fflush(stdout);
328         d_printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
329         if (geteuid() == 0) {
330             if (nmbd_running()) {
331                 nr_running++;
332                 d_printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
333             } else {
334                 d_printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
335             }
336             d_printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
337 #ifndef WITH_WINBIND
338             if (nr_running >= 1) {
339                 /* stop, restart all */
340                 d_printf("</tr><tr><td></td><td></td>\n");
341                 d_printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
342                 d_printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
343             }
344             else if (nr_running == 0) {
345                 /* start all */
346                 d_printf("</tr><tr><td></td><td></td>\n");
347                 d_printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
348             }    
349 #endif      
350         }
351         d_printf("</tr>\n");
352
353 #ifdef WITH_WINBIND
354         fflush(stdout);
355         d_printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
356         if (geteuid() == 0) {
357             if (winbindd_running()) {
358                 nr_running++;
359                 d_printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
360             } else {
361                 d_printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
362             }
363             d_printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
364             if (nr_running >= 1) {
365                 /* stop, restart all */
366                 d_printf("</tr><tr><td></td><td></td>\n");
367                 d_printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
368                 d_printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
369             }
370             else if (nr_running == 0) {
371                 /* start all */
372                 d_printf("</tr><tr><td></td><td></td>\n");
373                 d_printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
374             }
375                 
376         }
377         d_printf("</tr>\n");
378 #endif
379
380         d_printf("</table>\n");
381         fflush(stdout);
382
383         d_printf("<p><h3>%s</h3>\n", _("Active Connections"));
384         d_printf("<table border=1>\n");
385         d_printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
386         if (geteuid() == 0) {
387                 d_printf("<th>%s</th>\n", _("Kill"));
388         }
389         d_printf("</tr>\n");
390
391         if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
392
393         d_printf("</table><p>\n");
394
395         d_printf("<p><h3>%s</h3>\n", _("Active Shares"));
396         d_printf("<table border=1>\n");
397         d_printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
398                 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
399
400         if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
401
402         d_printf("</table><p>\n");
403
404         d_printf("<h3>%s</h3>\n", _("Open Files"));
405         d_printf("<table border=1>\n");
406         d_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"));
407
408         locking_init(1);
409         share_mode_forall(print_share_mode);
410         locking_end();
411         d_printf("</table>\n");
412
413         if (tdb) tdb_close(tdb);
414
415         d_printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"Show Client in col 1\">\n");
416         d_printf("<input type=submit name=\"show_pid_in_col_1\" value=\"Show PID in col 1\">\n");
417
418         d_printf("</FORM>\n");
419
420         if (autorefresh) {
421                 /* this little JavaScript allows for automatic refresh
422                    of the page. There are other methods but this seems
423                    to be the best alternative */
424                 d_printf("<script language=\"JavaScript\">\n");
425                 d_printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", 
426                        cgi_baseurl(),
427                        refresh_interval,
428                        refresh_interval*1000);
429                 d_printf("//-->\n</script>\n");
430         }
431 }