Samba 3: security updates CVE-2011-2522 and CVE-2011-2694 (unused in Tomato)
[tomato.git] / release / src / router / samba3 / source / web / statuspage.c
blobd7742345fa69139225b0bfb77198c9c38630a67a
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "web/swat_proto.h"
24 #define PIDMAP struct PidMap
26 /* how long to wait for start/stops to take effect */
27 #define SLEEP_TIME 3
29 PIDMAP {
30 PIDMAP *next, *prev;
31 struct process_id pid;
32 char *machine;
35 static PIDMAP *pidmap;
36 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
38 static struct process_id smbd_pid;
40 /* from 2nd call on, remove old list */
41 static void initPid2Machine (void)
43 /* show machine name rather PID on table "Open Files"? */
44 if (PID_or_Machine) {
45 PIDMAP *p, *next;
47 for (p = pidmap; p != NULL; p = next) {
48 next = p->next;
49 DLIST_REMOVE(pidmap, p);
50 SAFE_FREE(p->machine);
51 SAFE_FREE(p);
54 pidmap = NULL;
58 /* add new PID <-> Machine name mapping */
59 static void addPid2Machine (struct process_id pid, char *machine)
61 /* show machine name rather PID on table "Open Files"? */
62 if (PID_or_Machine) {
63 PIDMAP *newmap;
65 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
66 /* XXX need error message for this?
67 if malloc fails, PID is always shown */
68 return;
71 newmap->pid = pid;
72 newmap->machine = SMB_STRDUP(machine);
74 DLIST_ADD(pidmap, newmap);
78 /* lookup PID <-> Machine name mapping */
79 static char *mapPid2Machine (struct process_id pid)
81 static char pidbuf [64];
82 PIDMAP *map;
84 /* show machine name rather PID on table "Open Files"? */
85 if (PID_or_Machine) {
86 for (map = pidmap; map != NULL; map = map->next) {
87 if (procid_equal(&pid, &map->pid)) {
88 if (map->machine == NULL) /* no machine name */
89 break; /* show PID */
91 return map->machine;
96 /* PID not in list or machine name NULL? return pid as string */
97 snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
98 procid_str_static(&pid));
99 return pidbuf;
102 static char *tstring(time_t t)
104 static pstring buf;
105 pstrcpy(buf, time_to_asc(t));
106 all_string_sub(buf," ","&nbsp;",sizeof(buf));
107 return buf;
110 static void print_share_mode(const struct share_mode_entry *e,
111 const char *sharepath,
112 const char *fname,
113 void *dummy)
115 char *utf8_fname;
116 int deny_mode;
118 if (!is_valid_share_mode_entry(e)) {
119 return;
122 deny_mode = map_share_mode_to_deny_mode(e->share_access,
123 e->private_options);
125 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
126 printf("<td>%u</td>",(unsigned int)e->uid);
127 printf("<td>");
128 switch ((deny_mode>>4)&0xF) {
129 case DENY_NONE: printf("DENY_NONE"); break;
130 case DENY_ALL: printf("DENY_ALL "); break;
131 case DENY_DOS: printf("DENY_DOS "); break;
132 case DENY_FCB: printf("DENY_FCB "); break;
133 case DENY_READ: printf("DENY_READ "); break;
134 case DENY_WRITE:printf("DENY_WRITE "); break;
136 printf("</td>");
138 printf("<td>");
139 if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
140 printf("%s", _("RDWR "));
141 } else if (e->access_mask & FILE_WRITE_DATA) {
142 printf("%s", _("WRONLY "));
143 } else {
144 printf("%s", _("RDONLY "));
146 printf("</td>");
148 printf("<td>");
149 if((e->op_type &
150 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
151 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
152 printf("EXCLUSIVE+BATCH ");
153 else if (e->op_type & EXCLUSIVE_OPLOCK)
154 printf("EXCLUSIVE ");
155 else if (e->op_type & BATCH_OPLOCK)
156 printf("BATCH ");
157 else if (e->op_type & LEVEL_II_OPLOCK)
158 printf("LEVEL_II ");
159 else
160 printf("NONE ");
161 printf("</td>");
163 push_utf8_allocate(&utf8_fname, fname);
164 printf("<td>%s</td><td>%s</td></tr>\n",
165 utf8_fname,tstring(e->time.tv_sec));
166 SAFE_FREE(utf8_fname);
170 /* kill off any connections chosen by the user */
171 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
173 struct connections_data crec;
175 if (dbuf.dsize != sizeof(crec))
176 return 0;
178 memcpy(&crec, dbuf.dptr, sizeof(crec));
180 if (crec.cnum == -1 && process_exists(crec.pid)) {
181 char buf[30];
182 slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec.pid));
183 if (cgi_variable(buf)) {
184 kill_pid(crec.pid);
185 sleep(SLEEP_TIME);
188 return 0;
191 /* traversal fn for showing machine connections */
192 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
194 struct connections_data crec;
196 if (dbuf.dsize != sizeof(crec))
197 return 0;
199 memcpy(&crec, dbuf.dptr, sizeof(crec));
201 if (crec.cnum == -1 || !process_exists(crec.pid) ||
202 procid_equal(&crec.pid, &smbd_pid))
203 return 0;
205 addPid2Machine (crec.pid, crec.machine);
207 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
208 procid_str_static(&crec.pid),
209 crec.machine,crec.addr,
210 tstring(crec.start));
211 if (geteuid() == 0) {
212 printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
213 procid_str_static(&crec.pid));
215 printf("</tr>\n");
217 return 0;
220 /* traversal fn for showing share connections */
221 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
223 struct connections_data crec;
225 if (dbuf.dsize != sizeof(crec))
226 return 0;
228 memcpy(&crec, dbuf.dptr, sizeof(crec));
230 if (crec.cnum == -1 || !process_exists(crec.pid))
231 return 0;
233 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
234 crec.servicename,uidtoname(crec.uid),
235 gidtoname(crec.gid),procid_str_static(&crec.pid),
236 crec.machine,
237 tstring(crec.start));
238 return 0;
242 /* show the current server status */
243 void status_page(void)
245 const char *v;
246 int autorefresh=0;
247 int refresh_interval=30;
248 TDB_CONTEXT *tdb;
249 int nr_running=0;
250 BOOL waitup = False;
251 const char form_name[] = "status";
253 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
255 if (!verify_xsrf_token(form_name)) {
256 goto output_page;
259 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
260 stop_smbd();
261 start_smbd();
262 waitup=True;
265 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
266 start_smbd();
267 waitup=True;
270 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
271 stop_smbd();
272 waitup=True;
275 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
276 stop_nmbd();
277 start_nmbd();
278 waitup=True;
280 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
281 start_nmbd();
282 waitup=True;
285 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
286 stop_nmbd();
287 waitup=True;
290 #ifdef WITH_WINBIND
291 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
292 stop_winbindd();
293 start_winbindd();
294 waitup=True;
297 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
298 start_winbindd();
299 waitup=True;
302 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
303 stop_winbindd();
304 waitup=True;
306 #endif
307 /* wait for daemons to start/stop */
308 if (waitup)
309 sleep(SLEEP_TIME);
311 if (cgi_variable("autorefresh")) {
312 autorefresh = 1;
313 } else if (cgi_variable("norefresh")) {
314 autorefresh = 0;
315 } else if (cgi_variable("refresh")) {
316 autorefresh = 1;
319 if ((v=cgi_variable("refresh_interval"))) {
320 refresh_interval = atoi(v);
323 if (cgi_variable("show_client_in_col_1")) {
324 PID_or_Machine = 1;
327 if (cgi_variable("show_pid_in_col_1")) {
328 PID_or_Machine = 0;
331 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
332 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
334 initPid2Machine ();
336 output_page:
337 printf("<H2>%s</H2>\n", _("Server Status"));
339 printf("<FORM method=post>\n");
340 print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name);
342 if (!autorefresh) {
343 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
344 printf("<br>%s", _("Refresh Interval: "));
345 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
346 refresh_interval);
347 } else {
348 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
349 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
350 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
353 printf("<p>\n");
355 if (!tdb) {
356 /* open failure either means no connections have been
357 made */
361 printf("<table>\n");
363 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
365 fflush(stdout);
366 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
367 if (geteuid() == 0) {
368 if (smbd_running()) {
369 nr_running++;
370 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
371 } else {
372 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
374 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
376 printf("</tr>\n");
378 fflush(stdout);
379 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
380 if (geteuid() == 0) {
381 if (nmbd_running()) {
382 nr_running++;
383 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
384 } else {
385 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
387 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
389 printf("</tr>\n");
391 #ifdef WITH_WINBIND
392 fflush(stdout);
393 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
394 if (geteuid() == 0) {
395 if (winbindd_running()) {
396 nr_running++;
397 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
398 } else {
399 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
401 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
403 printf("</tr>\n");
404 #endif
406 if (geteuid() == 0) {
407 printf("<tr><td></td><td></td>\n");
408 if (nr_running >= 1) {
409 /* stop, restart all */
410 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
411 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
413 else if (nr_running == 0) {
414 /* start all */
415 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
417 printf("</tr>\n");
419 printf("</table>\n");
420 fflush(stdout);
422 printf("<p><h3>%s</h3>\n", _("Active Connections"));
423 printf("<table border=1>\n");
424 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
425 if (geteuid() == 0) {
426 printf("<th>%s</th>\n", _("Kill"));
428 printf("</tr>\n");
430 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
432 printf("</table><p>\n");
434 printf("<p><h3>%s</h3>\n", _("Active Shares"));
435 printf("<table border=1>\n");
436 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
437 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
439 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
441 printf("</table><p>\n");
443 printf("<h3>%s</h3>\n", _("Open Files"));
444 printf("<table border=1>\n");
445 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"));
447 locking_init(1);
448 share_mode_forall(print_share_mode, NULL);
449 locking_end();
450 printf("</table>\n");
452 if (tdb) tdb_close(tdb);
454 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
455 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
457 printf("</FORM>\n");
459 if (autorefresh) {
460 /* this little JavaScript allows for automatic refresh
461 of the page. There are other methods but this seems
462 to be the best alternative */
463 printf("<script language=\"JavaScript\">\n");
464 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
465 cgi_baseurl(),
466 refresh_interval,
467 refresh_interval*1000);
468 printf("//-->\n</script>\n");