2 Unix SMB/CIFS implementation.
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/>.
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 #include "../lib/util/pidfile.h"
28 #define _(x) lang_msg_rotate(talloc_tos(),x)
30 #define PIDMAP struct PidMap
32 /* how long to wait for start/stops to take effect */
41 static PIDMAP
*pidmap
;
42 static int PID_or_Machine
; /* 0 = show PID, else show Machine name */
44 static struct server_id smbd_pid
;
46 /* from 2nd call on, remove old list */
47 static void initPid2Machine (void)
49 /* show machine name rather PID on table "Open Files"? */
53 for (p
= pidmap
; p
!= NULL
; p
= next
) {
55 DLIST_REMOVE(pidmap
, p
);
56 SAFE_FREE(p
->machine
);
64 /* add new PID <-> Machine name mapping */
65 static void addPid2Machine (struct server_id pid
, const char *machine
)
67 /* show machine name rather PID on table "Open Files"? */
71 if ((newmap
= SMB_MALLOC_P(PIDMAP
)) == NULL
) {
72 /* XXX need error message for this?
73 if malloc fails, PID is always shown */
78 newmap
->machine
= SMB_STRDUP(machine
);
80 DLIST_ADD(pidmap
, newmap
);
84 /* lookup PID <-> Machine name mapping */
85 static char *mapPid2Machine (struct server_id pid
)
87 static char pidbuf
[64];
90 /* show machine name rather PID on table "Open Files"? */
92 for (map
= pidmap
; map
!= NULL
; map
= map
->next
) {
93 if (serverid_equal(&pid
, &map
->pid
)) {
94 if (map
->machine
== NULL
) /* no machine name */
102 /* PID not in list or machine name NULL? return pid as string */
103 snprintf (pidbuf
, sizeof (pidbuf
) - 1, "%s",
104 procid_str_static(&pid
));
108 static const char *tstring(TALLOC_CTX
*ctx
, time_t t
)
111 buf
= talloc_strdup(ctx
, time_to_asc(t
));
115 buf
= talloc_all_string_sub(ctx
,
125 static void print_share_mode(const struct share_mode_entry
*e
,
126 const char *sharepath
,
131 char *utf8_sharepath
;
133 size_t converted_size
;
135 if (!is_valid_share_mode_entry(e
)) {
139 deny_mode
= map_share_mode_to_deny_mode(e
->share_access
,
142 printf("<tr><td>%s</td>",_(mapPid2Machine(e
->pid
)));
143 printf("<td>%u</td>",(unsigned int)e
->uid
);
145 switch ((deny_mode
>>4)&0xF) {
146 case DENY_NONE
: printf("DENY_NONE"); break;
147 case DENY_ALL
: printf("DENY_ALL "); break;
148 case DENY_DOS
: printf("DENY_DOS "); break;
149 case DENY_FCB
: printf("DENY_FCB "); break;
150 case DENY_READ
: printf("DENY_READ "); break;
151 case DENY_WRITE
:printf("DENY_WRITE "); break;
156 if (e
->access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
)) {
157 printf("%s", _("RDWR "));
158 } else if (e
->access_mask
& FILE_WRITE_DATA
) {
159 printf("%s", _("WRONLY "));
161 printf("%s", _("RDONLY "));
167 (EXCLUSIVE_OPLOCK
|BATCH_OPLOCK
)) ==
168 (EXCLUSIVE_OPLOCK
|BATCH_OPLOCK
))
169 printf("EXCLUSIVE+BATCH ");
170 else if (e
->op_type
& EXCLUSIVE_OPLOCK
)
171 printf("EXCLUSIVE ");
172 else if (e
->op_type
& BATCH_OPLOCK
)
174 else if (e
->op_type
& LEVEL_II_OPLOCK
)
180 push_utf8_talloc(talloc_tos(), &utf8_fname
, fname
, &converted_size
);
181 push_utf8_talloc(talloc_tos(), &utf8_sharepath
, sharepath
,
183 printf("<td>%s</td><td>%s</td><td>%s</td></tr>\n",
184 utf8_sharepath
,utf8_fname
,tstring(talloc_tos(),e
->time
.tv_sec
));
185 TALLOC_FREE(utf8_fname
);
189 /* kill off any connections chosen by the user */
190 static int traverse_fn1(const struct connections_key
*key
,
191 const struct connections_data
*crec
,
194 if (crec
->cnum
== TID_FIELD_INVALID
&& process_exists(crec
->pid
)) {
196 slprintf(buf
,sizeof(buf
)-1,"kill_%s", procid_str_static(&crec
->pid
));
197 if (cgi_variable(buf
)) {
205 /* traversal fn for showing machine connections */
206 static int traverse_fn2(const struct connections_key
*key
,
207 const struct connections_data
*crec
,
210 if (crec
->cnum
== TID_FIELD_INVALID
|| !process_exists(crec
->pid
) ||
211 serverid_equal(&crec
->pid
, &smbd_pid
))
214 addPid2Machine (crec
->pid
, crec
->machine
);
216 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
217 procid_str_static(&crec
->pid
),
218 crec
->machine
, crec
->addr
,
219 tstring(talloc_tos(),crec
->start
));
220 if (geteuid() == 0) {
221 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
222 procid_str_static(&crec
->pid
));
229 /* traversal fn for showing share connections */
230 static int traverse_fn3(const struct connections_key
*key
,
231 const struct connections_data
*crec
,
234 if (crec
->cnum
== TID_FIELD_INVALID
|| !process_exists(crec
->pid
))
237 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
238 crec
->servicename
, uidtoname(crec
->uid
),
239 gidtoname(crec
->gid
),procid_str_static(&crec
->pid
),
241 tstring(talloc_tos(),crec
->start
));
246 /* show the current server status */
247 void status_page(void)
251 int refresh_interval
=30;
254 TALLOC_CTX
*ctx
= talloc_stackframe();
255 const char form_name
[] = "status";
257 smbd_pid
= pid_to_procid(pidfile_pid(lp_piddir(), "smbd"));
259 if (!verify_xsrf_token(form_name
)) {
263 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
269 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
274 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
279 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
284 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
289 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
295 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
301 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
306 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
311 /* wait for daemons to start/stop */
315 if (cgi_variable("autorefresh")) {
317 } else if (cgi_variable("norefresh")) {
319 } else if (cgi_variable("refresh")) {
323 if ((v
=cgi_variable("refresh_interval"))) {
324 refresh_interval
= atoi(v
);
327 if (cgi_variable("show_client_in_col_1")) {
331 if (cgi_variable("show_pid_in_col_1")) {
335 connections_forall_read(traverse_fn1
, NULL
);
340 printf("<H2>%s</H2>\n", _("Server Status"));
342 printf("<FORM method=post>\n");
343 print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name
);
346 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
347 printf("<br>%s", _("Refresh Interval: "));
348 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
351 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
352 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval
);
353 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
360 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), samba_version_string());
363 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
364 if (geteuid() == 0) {
365 if (smbd_running()) {
367 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
369 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
371 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
376 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
377 if (geteuid() == 0) {
378 if (nmbd_running()) {
380 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
382 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
384 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
390 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
391 if (geteuid() == 0) {
392 if (winbindd_running()) {
394 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
396 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
398 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
403 if (geteuid() == 0) {
404 printf("<tr><td></td><td></td>\n");
405 if (nr_running
>= 1) {
406 /* stop, restart all */
407 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
408 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
410 else if (nr_running
== 0) {
412 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
416 printf("</table>\n");
419 printf("<p><h3>%s</h3>\n", _("Active Connections"));
420 printf("<table border=1>\n");
421 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
422 if (geteuid() == 0) {
423 printf("<th>%s</th>\n", _("Kill"));
427 connections_forall_read(traverse_fn2
, NULL
);
429 printf("</table><p>\n");
431 printf("<p><h3>%s</h3>\n", _("Active Shares"));
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></tr>\n\n",
434 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
436 connections_forall_read(traverse_fn3
, NULL
);
438 printf("</table><p>\n");
440 printf("<h3>%s</h3>\n", _("Open Files"));
441 printf("<table border=1>\n");
442 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",
443 _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("Share"), _("File"), _("Date"));
445 locking_init_readonly();
446 share_mode_forall(print_share_mode
, NULL
);
448 printf("</table>\n");
450 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
451 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
456 /* this little JavaScript allows for automatic refresh
457 of the page. There are other methods but this seems
458 to be the best alternative */
459 printf("<script language=\"JavaScript\">\n");
460 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
463 refresh_interval
*1000);
464 printf("//-->\n</script>\n");