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