r8219: Merge the new open code from HEAD to 3.0. Haven't yet run the torture
[nivanova/samba-autobuild/.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 /* 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 = SMB_MALLOC_P(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 = SMB_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         char           *utf8_fname;
111         int deny_mode = map_share_mode_to_deny_mode(e->share_access,
112                                                     e->private_options);
113
114         printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
115         printf("<td>");
116         switch ((deny_mode>>4)&0xF) {
117         case DENY_NONE: printf("DENY_NONE"); break;
118         case DENY_ALL:  printf("DENY_ALL   "); break;
119         case DENY_DOS:  printf("DENY_DOS   "); break;
120         case DENY_FCB:  printf("DENY_FCB   "); break;
121         case DENY_READ: printf("DENY_READ  "); break;
122         case DENY_WRITE:printf("DENY_WRITE "); break;
123         }
124         printf("</td>");
125
126         printf("<td>");
127         if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
128                 printf("%s", _("RDWR       "));
129         } else if (e->access_mask & FILE_WRITE_DATA) {
130                 printf("%s", _("WRONLY     "));
131         } else {
132                 printf("%s", _("RDONLY     "));
133         }
134         printf("</td>");
135
136         printf("<td>");
137         if((e->op_type & 
138             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
139            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
140                 printf("EXCLUSIVE+BATCH ");
141         else if (e->op_type & EXCLUSIVE_OPLOCK)
142                 printf("EXCLUSIVE       ");
143         else if (e->op_type & BATCH_OPLOCK)
144                 printf("BATCH           ");
145         else if (e->op_type & LEVEL_II_OPLOCK)
146                 printf("LEVEL_II        ");
147         else
148                 printf("NONE            ");
149         printf("</td>");
150
151         push_utf8_allocate(&utf8_fname, fname);
152         printf("<td>%s</td><td>%s</td></tr>\n",
153                utf8_fname,tstring(e->time.tv_sec));
154         SAFE_FREE(utf8_fname);
155 }
156
157
158 /* kill off any connections chosen by the user */
159 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
160 {
161         struct connections_data crec;
162
163         if (dbuf.dsize != sizeof(crec))
164                 return 0;
165
166         memcpy(&crec, dbuf.dptr, sizeof(crec));
167
168         if (crec.cnum == -1 && process_exists(crec.pid)) {
169                 char buf[30];
170                 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
171                 if (cgi_variable(buf)) {
172                         kill_pid(crec.pid);
173                         sleep(SLEEP_TIME);
174                 }
175         }
176         return 0;
177 }
178
179 /* traversal fn for showing machine connections */
180 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
181 {
182         struct connections_data crec;
183
184         if (dbuf.dsize != sizeof(crec))
185                 return 0;
186
187         memcpy(&crec, dbuf.dptr, sizeof(crec));
188         
189         if (crec.cnum == -1 || !process_exists(crec.pid) || (crec.pid == smbd_pid))
190                 return 0;
191
192         addPid2Machine (crec.pid, crec.machine);
193
194         printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
195                (int)crec.pid,
196                crec.machine,crec.addr,
197                tstring(crec.start));
198         if (geteuid() == 0) {
199                 printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
200                        (int)crec.pid);
201         }
202         printf("</tr>\n");
203
204         return 0;
205 }
206
207 /* traversal fn for showing share connections */
208 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
209 {
210         struct connections_data crec;
211
212         if (dbuf.dsize != sizeof(crec))
213                 return 0;
214
215         memcpy(&crec, dbuf.dptr, sizeof(crec));
216
217         if (crec.cnum == -1 || !process_exists(crec.pid))
218                 return 0;
219
220         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
221                crec.name,uidtoname(crec.uid),
222                gidtoname(crec.gid),(int)crec.pid,
223                crec.machine,
224                tstring(crec.start));
225         return 0;
226 }
227
228
229 /* show the current server status */
230 void status_page(void)
231 {
232         const char *v;
233         int autorefresh=0;
234         int refresh_interval=30;
235         TDB_CONTEXT *tdb;
236         int nr_running=0;
237         BOOL waitup = False;
238
239         smbd_pid = pidfile_pid("smbd");
240
241         if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
242                 stop_smbd();
243                 start_smbd();
244                 waitup=True;
245         }
246
247         if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
248                 start_smbd();
249                 waitup=True;
250         }
251
252         if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
253                 stop_smbd();
254                 waitup=True;
255         }
256
257         if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
258                 stop_nmbd();
259                 start_nmbd();
260                 waitup=True;
261         }
262         if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
263                 start_nmbd();
264                 waitup=True;
265         }
266
267         if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
268                 stop_nmbd();
269                 waitup=True;
270         }
271
272 #ifdef WITH_WINBIND
273         if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
274                 stop_winbindd();
275                 start_winbindd();
276                 waitup=True;
277         }
278
279         if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
280                 start_winbindd();
281                 waitup=True;
282         }
283
284         if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
285                 stop_winbindd();
286                 waitup=True;
287         }
288 #endif
289         /* wait for daemons to start/stop */
290         if (waitup)
291                 sleep(SLEEP_TIME);
292         
293         if (cgi_variable("autorefresh")) {
294                 autorefresh = 1;
295         } else if (cgi_variable("norefresh")) {
296                 autorefresh = 0;
297         } else if (cgi_variable("refresh")) {
298                 autorefresh = 1;
299         }
300
301         if ((v=cgi_variable("refresh_interval"))) {
302                 refresh_interval = atoi(v);
303         }
304
305         if (cgi_variable("show_client_in_col_1")) {
306                 PID_or_Machine = 1;
307         }
308
309         if (cgi_variable("show_pid_in_col_1")) {
310                 PID_or_Machine = 0;
311         }
312
313         tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
314         if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
315  
316         initPid2Machine ();
317
318         printf("<H2>%s</H2>\n", _("Server Status"));
319
320         printf("<FORM method=post>\n");
321
322         if (!autorefresh) {
323                 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
324                 printf("<br>%s", _("Refresh Interval: "));
325                 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n", 
326                        refresh_interval);
327         } else {
328                 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
329                 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
330                 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
331         }
332
333         printf("<p>\n");
334
335         if (!tdb) {
336                 /* open failure either means no connections have been
337                    made */
338         }
339
340
341         printf("<table>\n");
342
343         printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
344
345         fflush(stdout);
346         printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
347         if (geteuid() == 0) {
348             if (smbd_running()) {
349                 nr_running++;
350                 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
351             } else {
352                 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
353             }
354             printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
355         }
356         printf("</tr>\n");
357
358         fflush(stdout);
359         printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
360         if (geteuid() == 0) {
361             if (nmbd_running()) {
362                 nr_running++;
363                 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
364             } else {
365                 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
366             }
367             printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));    
368         }
369         printf("</tr>\n");
370
371 #ifdef WITH_WINBIND
372         fflush(stdout);
373         printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
374         if (geteuid() == 0) {
375             if (winbindd_running()) {
376                 nr_running++;
377                 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
378             } else {
379                 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
380             }
381             printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
382         }
383         printf("</tr>\n");
384 #endif
385
386         if (geteuid() == 0) {
387             printf("<tr><td></td><td></td>\n");
388             if (nr_running >= 1) {
389                 /* stop, restart all */
390                 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
391                 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
392             }
393             else if (nr_running == 0) {
394                 /* start all */
395                 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
396             }
397             printf("</tr>\n");
398         }
399         printf("</table>\n");
400         fflush(stdout);
401
402         printf("<p><h3>%s</h3>\n", _("Active Connections"));
403         printf("<table border=1>\n");
404         printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
405         if (geteuid() == 0) {
406                 printf("<th>%s</th>\n", _("Kill"));
407         }
408         printf("</tr>\n");
409
410         if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
411
412         printf("</table><p>\n");
413
414         printf("<p><h3>%s</h3>\n", _("Active Shares"));
415         printf("<table border=1>\n");
416         printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
417                 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
418
419         if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
420
421         printf("</table><p>\n");
422
423         printf("<h3>%s</h3>\n", _("Open Files"));
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", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
426
427         locking_init(1);
428         share_mode_forall(print_share_mode);
429         locking_end();
430         printf("</table>\n");
431
432         if (tdb) tdb_close(tdb);
433
434         printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
435         printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
436
437         printf("</FORM>\n");
438
439         if (autorefresh) {
440                 /* this little JavaScript allows for automatic refresh
441                    of the page. There are other methods but this seems
442                    to be the best alternative */
443                 printf("<script language=\"JavaScript\">\n");
444                 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", 
445                        cgi_baseurl(),
446                        refresh_interval,
447                        refresh_interval*1000);
448                 printf("//-->\n</script>\n");
449         }
450 }