s4 dns: Add TSIG and TKEY records to idl
[Samba/gebeck_regimport.git] / source3 / web / statuspage.c
blob49fb32e4fd9ec02123205a9d204ec9494d13c9a9
1 /*
2 Unix SMB/CIFS implementation.
3 web status page
4 Copyright (C) Andrew Tridgell 1997-1998
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.
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.
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/>.
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"
26 #define _(x) lang_msg_rotate(talloc_tos(),x)
28 #define PIDMAP struct PidMap
30 /* how long to wait for start/stops to take effect */
31 #define SLEEP_TIME 3
33 PIDMAP {
34 PIDMAP *next, *prev;
35 struct server_id pid;
36 char *machine;
39 static PIDMAP *pidmap;
40 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
42 static struct server_id smbd_pid;
44 /* from 2nd call on, remove old list */
45 static void initPid2Machine (void)
47 /* show machine name rather PID on table "Open Files"? */
48 if (PID_or_Machine) {
49 PIDMAP *p, *next;
51 for (p = pidmap; p != NULL; p = next) {
52 next = p->next;
53 DLIST_REMOVE(pidmap, p);
54 SAFE_FREE(p->machine);
55 SAFE_FREE(p);
58 pidmap = NULL;
62 /* add new PID <-> Machine name mapping */
63 static void addPid2Machine (struct server_id pid, const char *machine)
65 /* show machine name rather PID on table "Open Files"? */
66 if (PID_or_Machine) {
67 PIDMAP *newmap;
69 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
70 /* XXX need error message for this?
71 if malloc fails, PID is always shown */
72 return;
75 newmap->pid = pid;
76 newmap->machine = SMB_STRDUP(machine);
78 DLIST_ADD(pidmap, newmap);
82 /* lookup PID <-> Machine name mapping */
83 static char *mapPid2Machine (struct server_id pid)
85 static char pidbuf [64];
86 PIDMAP *map;
88 /* show machine name rather PID on table "Open Files"? */
89 if (PID_or_Machine) {
90 for (map = pidmap; map != NULL; map = map->next) {
91 if (procid_equal(&pid, &map->pid)) {
92 if (map->machine == NULL) /* no machine name */
93 break; /* show PID */
95 return map->machine;
100 /* PID not in list or machine name NULL? return pid as string */
101 snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
102 procid_str_static(&pid));
103 return pidbuf;
106 static const char *tstring(TALLOC_CTX *ctx, time_t t)
108 char *buf;
109 buf = talloc_strdup(ctx, time_to_asc(t));
110 if (!buf) {
111 return "";
113 buf = talloc_all_string_sub(ctx,
114 buf,
115 " ",
116 "&nbsp;");
117 if (!buf) {
118 return "";
120 return buf;
123 static void print_share_mode(const struct share_mode_entry *e,
124 const char *sharepath,
125 const char *fname,
126 void *dummy)
128 char *utf8_fname;
129 char *utf8_sharepath;
130 int deny_mode;
131 size_t converted_size;
133 if (!is_valid_share_mode_entry(e)) {
134 return;
137 deny_mode = map_share_mode_to_deny_mode(e->share_access,
138 e->private_options);
140 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
141 printf("<td>%u</td>",(unsigned int)e->uid);
142 printf("<td>");
143 switch ((deny_mode>>4)&0xF) {
144 case DENY_NONE: printf("DENY_NONE"); break;
145 case DENY_ALL: printf("DENY_ALL "); break;
146 case DENY_DOS: printf("DENY_DOS "); break;
147 case DENY_FCB: printf("DENY_FCB "); break;
148 case DENY_READ: printf("DENY_READ "); break;
149 case DENY_WRITE:printf("DENY_WRITE "); break;
151 printf("</td>");
153 printf("<td>");
154 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
155 printf("%s", _("RDWR "));
156 } else if (e->access_mask & FILE_WRITE_DATA) {
157 printf("%s", _("WRONLY "));
158 } else {
159 printf("%s", _("RDONLY "));
161 printf("</td>");
163 printf("<td>");
164 if((e->op_type &
165 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
166 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
167 printf("EXCLUSIVE+BATCH ");
168 else if (e->op_type & EXCLUSIVE_OPLOCK)
169 printf("EXCLUSIVE ");
170 else if (e->op_type & BATCH_OPLOCK)
171 printf("BATCH ");
172 else if (e->op_type & LEVEL_II_OPLOCK)
173 printf("LEVEL_II ");
174 else
175 printf("NONE ");
176 printf("</td>");
178 push_utf8_talloc(talloc_tos(), &utf8_fname, fname, &converted_size);
179 push_utf8_talloc(talloc_tos(), &utf8_sharepath, sharepath,
180 &converted_size);
181 printf("<td>%s</td><td>%s</td><td>%s</td></tr>\n",
182 utf8_sharepath,utf8_fname,tstring(talloc_tos(),e->time.tv_sec));
183 TALLOC_FREE(utf8_fname);
187 /* kill off any connections chosen by the user */
188 static int traverse_fn1(const struct connections_key *key,
189 const struct connections_data *crec,
190 void *private_data)
192 if (crec->cnum == -1 && process_exists(crec->pid)) {
193 char buf[30];
194 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
195 if (cgi_variable(buf)) {
196 kill_pid(crec->pid);
197 sleep(SLEEP_TIME);
200 return 0;
203 /* traversal fn for showing machine connections */
204 static int traverse_fn2(const struct connections_key *key,
205 const struct connections_data *crec,
206 void *private_data)
208 if (crec->cnum == -1 || !process_exists(crec->pid) ||
209 procid_equal(&crec->pid, &smbd_pid))
210 return 0;
212 addPid2Machine (crec->pid, crec->machine);
214 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
215 procid_str_static(&crec->pid),
216 crec->machine, crec->addr,
217 tstring(talloc_tos(),crec->start));
218 if (geteuid() == 0) {
219 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
220 procid_str_static(&crec->pid));
222 printf("</tr>\n");
224 return 0;
227 /* traversal fn for showing share connections */
228 static int traverse_fn3(const struct connections_key *key,
229 const struct connections_data *crec,
230 void *private_data)
232 if (crec->cnum == -1 || !process_exists(crec->pid))
233 return 0;
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;
244 /* show the current server status */
245 void status_page(void)
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 const char form_name[] = "status";
255 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
257 if (!verify_xsrf_token(form_name)) {
258 goto output_page;
261 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
262 stop_smbd();
263 start_smbd();
264 waitup=True;
267 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
268 start_smbd();
269 waitup=True;
272 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
273 stop_smbd();
274 waitup=True;
277 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
278 stop_nmbd();
279 start_nmbd();
280 waitup=True;
282 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
283 start_nmbd();
284 waitup=True;
287 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
288 stop_nmbd();
289 waitup=True;
292 #ifdef WITH_WINBIND
293 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
294 stop_winbindd();
295 start_winbindd();
296 waitup=True;
299 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
300 start_winbindd();
301 waitup=True;
304 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
305 stop_winbindd();
306 waitup=True;
308 #endif
309 /* wait for daemons to start/stop */
310 if (waitup)
311 sleep(SLEEP_TIME);
313 if (cgi_variable("autorefresh")) {
314 autorefresh = 1;
315 } else if (cgi_variable("norefresh")) {
316 autorefresh = 0;
317 } else if (cgi_variable("refresh")) {
318 autorefresh = 1;
321 if ((v=cgi_variable("refresh_interval"))) {
322 refresh_interval = atoi(v);
325 if (cgi_variable("show_client_in_col_1")) {
326 PID_or_Machine = 1;
329 if (cgi_variable("show_pid_in_col_1")) {
330 PID_or_Machine = 0;
333 connections_forall_read(traverse_fn1, NULL);
335 initPid2Machine ();
337 output_page:
338 printf("<H2>%s</H2>\n", _("Server Status"));
340 printf("<FORM method=post>\n");
341 print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name);
343 if (!autorefresh) {
344 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
345 printf("<br>%s", _("Refresh Interval: "));
346 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
347 refresh_interval);
348 } else {
349 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
350 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
351 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
354 printf("<p>\n");
356 printf("<table>\n");
358 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), samba_version_string());
360 fflush(stdout);
361 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
362 if (geteuid() == 0) {
363 if (smbd_running()) {
364 nr_running++;
365 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
366 } else {
367 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
369 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
371 printf("</tr>\n");
373 fflush(stdout);
374 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
375 if (geteuid() == 0) {
376 if (nmbd_running()) {
377 nr_running++;
378 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
379 } else {
380 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
382 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
384 printf("</tr>\n");
386 #ifdef WITH_WINBIND
387 fflush(stdout);
388 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
389 if (geteuid() == 0) {
390 if (winbindd_running()) {
391 nr_running++;
392 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
393 } else {
394 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
396 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
398 printf("</tr>\n");
399 #endif
401 if (geteuid() == 0) {
402 printf("<tr><td></td><td></td>\n");
403 if (nr_running >= 1) {
404 /* stop, restart all */
405 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
406 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
408 else if (nr_running == 0) {
409 /* start all */
410 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
412 printf("</tr>\n");
414 printf("</table>\n");
415 fflush(stdout);
417 printf("<p><h3>%s</h3>\n", _("Active Connections"));
418 printf("<table border=1>\n");
419 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
420 if (geteuid() == 0) {
421 printf("<th>%s</th>\n", _("Kill"));
423 printf("</tr>\n");
425 connections_forall_read(traverse_fn2, NULL);
427 printf("</table><p>\n");
429 printf("<p><h3>%s</h3>\n", _("Active Shares"));
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></tr>\n\n",
432 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
434 connections_forall_read(traverse_fn3, NULL);
436 printf("</table><p>\n");
438 printf("<h3>%s</h3>\n", _("Open Files"));
439 printf("<table border=1>\n");
440 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",
441 _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("Share"), _("File"), _("Date"));
443 locking_init_readonly();
444 share_mode_forall(print_share_mode, NULL);
445 locking_end();
446 printf("</table>\n");
448 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
449 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
451 printf("</FORM>\n");
453 if (autorefresh) {
454 /* this little JavaScript allows for automatic refresh
455 of the page. There are other methods but this seems
456 to be the best alternative */
457 printf("<script language=\"JavaScript\">\n");
458 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
459 cgi_baseurl(),
460 refresh_interval,
461 refresh_interval*1000);
462 printf("//-->\n</script>\n");
464 TALLOC_FREE(ctx);