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