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