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