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