r24781: Fix build warning.
[kai/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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "web/swat_proto.h"
22
23 #define _(x) lang_msg_rotate(x)
24
25 #define PIDMAP          struct PidMap
26
27 /* how long to wait for start/stops to take effect */
28 #define SLEEP_TIME 3
29
30 PIDMAP {
31         PIDMAP  *next, *prev;
32         struct server_id pid;
33         char    *machine;
34 };
35
36 static PIDMAP   *pidmap;
37 static int      PID_or_Machine;         /* 0 = show PID, else show Machine name */
38
39 static struct server_id smbd_pid;
40
41 /* from 2nd call on, remove old list */
42 static void initPid2Machine (void)
43 {
44         /* show machine name rather PID on table "Open Files"? */
45         if (PID_or_Machine) {
46                 PIDMAP *p;
47
48                 for (p = pidmap; p != NULL; ) {
49                         DLIST_REMOVE(pidmap, p);
50                         SAFE_FREE(p->machine);
51                         SAFE_FREE(p);
52                 }
53
54                 pidmap = NULL;
55         }
56 }
57
58 /* add new PID <-> Machine name mapping */
59 static void addPid2Machine (struct server_id pid, const char *machine)
60 {
61         /* show machine name rather PID on table "Open Files"? */
62         if (PID_or_Machine) {
63                 PIDMAP *newmap;
64
65                 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
66                         /* XXX need error message for this?
67                            if malloc fails, PID is always shown */
68                         return;
69                 }
70
71                 newmap->pid = pid;
72                 newmap->machine = SMB_STRDUP(machine);
73
74                 DLIST_ADD(pidmap, newmap);
75         }
76 }
77
78 /* lookup PID <-> Machine name mapping */
79 static char *mapPid2Machine (struct server_id pid)
80 {
81         static char pidbuf [64];
82         PIDMAP *map;
83
84         /* show machine name rather PID on table "Open Files"? */
85         if (PID_or_Machine) {
86                 for (map = pidmap; map != NULL; map = map->next) {
87                         if (procid_equal(&pid, &map->pid)) {
88                                 if (map->machine == NULL)       /* no machine name */
89                                         break;                  /* show PID */
90
91                                 return map->machine;
92                         }
93                 }
94         }
95
96         /* PID not in list or machine name NULL? return pid as string */
97         snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
98                   procid_str_static(&pid));
99         return pidbuf;
100 }
101
102 static char *tstring(time_t t)
103 {
104         static pstring buf;
105         pstrcpy(buf, time_to_asc(t));
106         all_string_sub(buf," ","&nbsp;",sizeof(buf));
107         return buf;
108 }
109
110 static void print_share_mode(const struct share_mode_entry *e,
111                              const char *sharepath,
112                              const char *fname,
113                              void *dummy)
114 {
115         char           *utf8_fname;
116         int deny_mode;
117
118         if (!is_valid_share_mode_entry(e)) {
119                 return;
120         }
121
122         deny_mode = map_share_mode_to_deny_mode(e->share_access,
123                                                     e->private_options);
124
125         printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
126         printf("<td>%u</td>",(unsigned int)e->uid);
127         printf("<td>");
128         switch ((deny_mode>>4)&0xF) {
129         case DENY_NONE: printf("DENY_NONE"); break;
130         case DENY_ALL:  printf("DENY_ALL   "); break;
131         case DENY_DOS:  printf("DENY_DOS   "); break;
132         case DENY_FCB:  printf("DENY_FCB   "); break;
133         case DENY_READ: printf("DENY_READ  "); break;
134         case DENY_WRITE:printf("DENY_WRITE "); break;
135         }
136         printf("</td>");
137
138         printf("<td>");
139         if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
140                 printf("%s", _("RDWR       "));
141         } else if (e->access_mask & FILE_WRITE_DATA) {
142                 printf("%s", _("WRONLY     "));
143         } else {
144                 printf("%s", _("RDONLY     "));
145         }
146         printf("</td>");
147
148         printf("<td>");
149         if((e->op_type & 
150             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
151            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
152                 printf("EXCLUSIVE+BATCH ");
153         else if (e->op_type & EXCLUSIVE_OPLOCK)
154                 printf("EXCLUSIVE       ");
155         else if (e->op_type & BATCH_OPLOCK)
156                 printf("BATCH           ");
157         else if (e->op_type & LEVEL_II_OPLOCK)
158                 printf("LEVEL_II        ");
159         else
160                 printf("NONE            ");
161         printf("</td>");
162
163         push_utf8_allocate(&utf8_fname, fname);
164         printf("<td>%s</td><td>%s</td></tr>\n",
165                utf8_fname,tstring(e->time.tv_sec));
166         SAFE_FREE(utf8_fname);
167 }
168
169
170 /* kill off any connections chosen by the user */
171 static int traverse_fn1(struct db_record *rec,
172                         const struct connections_key *key,
173                         const struct connections_data *crec,
174                         void *private_data)
175 {
176         if (crec->cnum == -1 && process_exists(crec->pid)) {
177                 char buf[30];
178                 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
179                 if (cgi_variable(buf)) {
180                         kill_pid(crec->pid);
181                         sleep(SLEEP_TIME);
182                 }
183         }
184         return 0;
185 }
186
187 /* traversal fn for showing machine connections */
188 static int traverse_fn2(struct db_record *rec,
189                         const struct connections_key *key,
190                         const struct connections_data *crec,
191                         void *private_data)
192 {
193         if (crec->cnum == -1 || !process_exists(crec->pid) ||
194             procid_equal(&crec->pid, &smbd_pid))
195                 return 0;
196
197         addPid2Machine (crec->pid, crec->machine);
198
199         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
200                procid_str_static(&crec->pid),
201                crec->machine, crec->addr,
202                tstring(crec->start));
203         if (geteuid() == 0) {
204                 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
205                        procid_str_static(&crec->pid));
206         }
207         printf("</tr>\n");
208
209         return 0;
210 }
211
212 /* traversal fn for showing share connections */
213 static int traverse_fn3(struct db_record *rec,
214                         const struct connections_key *key,
215                         const struct connections_data *crec,
216                         void *private_data)
217 {
218         if (crec->cnum == -1 || !process_exists(crec->pid))
219                 return 0;
220
221         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
222                crec->servicename, uidtoname(crec->uid),
223                gidtoname(crec->gid),procid_str_static(&crec->pid),
224                crec->machine,
225                tstring(crec->start));
226         return 0;
227 }
228
229
230 /* show the current server status */
231 void status_page(void)
232 {
233         const char *v;
234         int autorefresh=0;
235         int refresh_interval=30;
236         int nr_running=0;
237         BOOL waitup = False;
238
239         smbd_pid = pid_to_procid(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         connections_forall(traverse_fn1, NULL);
314  
315         initPid2Machine ();
316
317         printf("<H2>%s</H2>\n", _("Server Status"));
318
319         printf("<FORM method=post>\n");
320
321         if (!autorefresh) {
322                 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
323                 printf("<br>%s", _("Refresh Interval: "));
324                 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n", 
325                        refresh_interval);
326         } else {
327                 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
328                 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
329                 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
330         }
331
332         printf("<p>\n");
333
334         printf("<table>\n");
335
336         printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
337
338         fflush(stdout);
339         printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
340         if (geteuid() == 0) {
341             if (smbd_running()) {
342                 nr_running++;
343                 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
344             } else {
345                 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
346             }
347             printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
348         }
349         printf("</tr>\n");
350
351         fflush(stdout);
352         printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
353         if (geteuid() == 0) {
354             if (nmbd_running()) {
355                 nr_running++;
356                 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
357             } else {
358                 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
359             }
360             printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));    
361         }
362         printf("</tr>\n");
363
364 #ifdef WITH_WINBIND
365         fflush(stdout);
366         printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
367         if (geteuid() == 0) {
368             if (winbindd_running()) {
369                 nr_running++;
370                 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
371             } else {
372                 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
373             }
374             printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
375         }
376         printf("</tr>\n");
377 #endif
378
379         if (geteuid() == 0) {
380             printf("<tr><td></td><td></td>\n");
381             if (nr_running >= 1) {
382                 /* stop, restart all */
383                 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
384                 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
385             }
386             else if (nr_running == 0) {
387                 /* start all */
388                 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
389             }
390             printf("</tr>\n");
391         }
392         printf("</table>\n");
393         fflush(stdout);
394
395         printf("<p><h3>%s</h3>\n", _("Active Connections"));
396         printf("<table border=1>\n");
397         printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
398         if (geteuid() == 0) {
399                 printf("<th>%s</th>\n", _("Kill"));
400         }
401         printf("</tr>\n");
402
403         connections_forall(traverse_fn2, NULL);
404
405         printf("</table><p>\n");
406
407         printf("<p><h3>%s</h3>\n", _("Active Shares"));
408         printf("<table border=1>\n");
409         printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
410                 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
411
412         connections_forall(traverse_fn3, NULL);
413
414         printf("</table><p>\n");
415
416         printf("<h3>%s</h3>\n", _("Open Files"));
417         printf("<table border=1>\n");
418         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"));
419
420         locking_init(1);
421         share_mode_forall(print_share_mode, NULL);
422         locking_end();
423         printf("</table>\n");
424
425         printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
426         printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
427
428         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                 printf("<script language=\"JavaScript\">\n");
435                 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                 printf("//-->\n</script>\n");
440         }
441 }