2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1997-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define PIDMAP struct PidMap
33 static PIDMAP *pidmap;
34 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
36 static pid_t smbd_pid;
38 /* from 2nd call on, remove old list */
39 static void initPid2Machine (void)
41 /* show machine name rather PID on table "Open Files"? */
45 for (p = pidmap; p != NULL; ) {
46 DLIST_REMOVE(pidmap, p);
47 SAFE_FREE(p->machine);
55 /* add new PID <-> Machine name mapping */
56 static void addPid2Machine (pid_t pid, char *machine)
58 /* show machine name rather PID on table "Open Files"? */
62 if ((newmap = (PIDMAP *) malloc (sizeof (PIDMAP))) == NULL) {
63 /* XXX need error message for this?
64 if malloc fails, PID is always shown */
69 newmap->machine = strdup (machine);
71 DLIST_ADD(pidmap, newmap);
75 /* lookup PID <-> Machine name mapping */
76 static char *mapPid2Machine (pid_t pid)
78 static char pidbuf [64];
81 /* show machine name rather PID on table "Open Files"? */
83 for (map = pidmap; map != NULL; map = map->next) {
84 if (pid == map->pid) {
85 if (map->machine == NULL) /* no machine name */
93 /* PID not in list or machine name NULL? return pid as string */
94 snprintf (pidbuf, sizeof (pidbuf) - 1, "%d", pid);
98 static char *tstring(time_t t)
101 pstrcpy(buf, asctime(LocalTime(&t)));
102 all_string_sub(buf," "," ",sizeof(buf));
106 static void print_share_mode(share_mode_entry *e, char *fname)
108 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
110 switch ((e->share_mode>>4)&0xF) {
111 case DENY_NONE: printf(_("DENY_NONE")); break;
112 case DENY_ALL: printf(_("DENY_ALL ")); break;
113 case DENY_DOS: printf(_("DENY_DOS ")); break;
114 case DENY_READ: printf(_("DENY_READ ")); break;
115 case DENY_WRITE:printf(_("DENY_WRITE ")); break;
120 switch (e->share_mode&0xF) {
121 case 0: printf(_("RDONLY ")); break;
122 case 1: printf(_("WRONLY ")); break;
123 case 2: printf(_("RDWR ")); break;
129 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
130 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
131 printf(_("EXCLUSIVE+BATCH "));
132 else if (e->op_type & EXCLUSIVE_OPLOCK)
133 printf(_("EXCLUSIVE "));
134 else if (e->op_type & BATCH_OPLOCK)
136 else if (e->op_type & LEVEL_II_OPLOCK)
137 printf(_("LEVEL_II "));
142 printf("<td>%s</td><td>%s</td></tr>\n",
143 fname,tstring(e->time.tv_sec));
147 /* kill off any connections chosen by the user */
148 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
150 struct connections_data crec;
152 if (dbuf.dsize != sizeof(crec))
155 memcpy(&crec, dbuf.dptr, sizeof(crec));
157 if (crec.cnum == -1 && process_exists(crec.pid)) {
159 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
160 if (cgi_variable(buf)) {
167 /* traversal fn for showing machine connections */
168 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
170 struct connections_data crec;
172 if (dbuf.dsize != sizeof(crec))
175 memcpy(&crec, dbuf.dptr, sizeof(crec));
177 if (crec.cnum != -1 || !process_exists(crec.pid) || (crec.pid == smbd_pid))
180 addPid2Machine (crec.pid, crec.machine);
182 printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
184 crec.machine,crec.addr,
185 tstring(crec.start));
186 if (geteuid() == 0) {
187 printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
195 /* traversal fn for showing share connections */
196 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
198 struct connections_data crec;
200 if (dbuf.dsize != sizeof(crec))
203 memcpy(&crec, dbuf.dptr, sizeof(crec));
205 if (crec.cnum == -1 || !process_exists(crec.pid))
208 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
209 crec.name,uidtoname(crec.uid),
210 gidtoname(crec.gid),(int)crec.pid,
212 tstring(crec.start));
217 /* show the current server status */
218 void status_page(void)
222 int refresh_interval=30;
225 smbd_pid = pidfile_pid("smbd");
227 if (cgi_variable("smbd_restart")) {
232 if (cgi_variable("smbd_start")) {
236 if (cgi_variable("smbd_stop")) {
240 if (cgi_variable("nmbd_restart")) {
244 if (cgi_variable("nmbd_start")) {
248 if (cgi_variable("nmbd_stop")) {
252 if (cgi_variable("autorefresh")) {
254 } else if (cgi_variable("norefresh")) {
256 } else if (cgi_variable("refresh")) {
260 if ((v=cgi_variable("refresh_interval"))) {
261 refresh_interval = atoi(v);
264 if (cgi_variable("show_client_in_col_1")) {
268 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
269 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
273 printf("<H2>%s</H2>\n", _("Server Status"));
275 printf("<FORM method=post>\n");
278 printf("<input type=submit value=\"%s\" name=autorefresh>\n", _("Auto Refresh"));
279 printf("<br>%s", _("Refresh Interval: "));
280 printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n",
283 printf("<input type=submit value=\"%s\" name=norefresh>\n", _("Stop Refreshing"));
284 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
285 printf("<input type=hidden name=refresh value=1>\n");
291 /* open failure either means no connections have been
298 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), VERSION);
301 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
302 if (geteuid() == 0) {
303 if (smbd_running()) {
304 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
306 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
308 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
313 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
314 if (geteuid() == 0) {
315 if (nmbd_running()) {
316 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
318 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
320 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
324 printf("</table>\n");
327 printf("<p><h3>%s</h3>\n", _("Active Connections"));
328 printf("<table border=1>\n");
329 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
330 if (geteuid() == 0) {
331 printf("<th>%s</th>\n", _("Kill"));
335 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
337 printf("</table><p>\n");
339 printf("<p><h3>%s</h3>\n", _("Active Shares"));
340 printf("<table border=1>\n");
341 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
342 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
344 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
346 printf("</table><p>\n");
348 printf("<h3>%s</h3>\n", _("Open Files"));
349 printf("<table border=1>\n");
350 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
353 share_mode_forall(print_share_mode);
355 printf("</table>\n");
357 if (tdb) tdb_close(tdb);
359 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"Show Client in col 1\">\n");
360 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"Show PID in col 1\">\n");
365 /* this little JavaScript allows for automatic refresh
366 of the page. There are other methods but this seems
367 to be the best alternative */
368 printf("<script language=\"JavaScript\">\n");
369 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
372 refresh_interval*1000);
373 printf("//-->\n</script>\n");