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