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