source: branches/samba-3.0/source/web/statuspage.c@ 374

Last change on this file since 374 was 165, checked in by Paul Smedley, 16 years ago

Add 'missing' 3.0.34 diffs

File size: 12.4 KB
Line 
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include "includes.h"
22#include "web/swat_proto.h"
23
24#define PIDMAP struct PidMap
25
26/* how long to wait for start/stops to take effect */
27#define SLEEP_TIME 3
28
29PIDMAP {
30 PIDMAP *next, *prev;
31 struct process_id pid;
32 char *machine;
33};
34
35static PIDMAP *pidmap;
36static int PID_or_Machine; /* 0 = show PID, else show Machine name */
37
38static struct process_id smbd_pid;
39
40/* from 2nd call on, remove old list */
41static void initPid2Machine (void)
42{
43 /* show machine name rather PID on table "Open Files"? */
44 if (PID_or_Machine) {
45 PIDMAP *p, *next;
46
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);
52 }
53
54 pidmap = NULL;
55 }
56}
57
58/* add new PID <-> Machine name mapping */
59static void addPid2Machine (struct process_id pid, char *machine)
60{
61 /* show machine name rather PID on table "Open Files"? */
62 if (PID_or_Machine) {
63 PIDMAP *newmap;
64
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;
69 }
70
71 newmap->pid = pid;
72 newmap->machine = SMB_STRDUP(machine);
73
74 DLIST_ADD(pidmap, newmap);
75 }
76}
77
78/* lookup PID <-> Machine name mapping */
79static char *mapPid2Machine (struct process_id pid)
80{
81 static char pidbuf [64];
82 PIDMAP *map;
83
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 */
90
91 return map->machine;
92 }
93 }
94 }
95
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;
100}
101
102static char *tstring(time_t t)
103{
104 static pstring buf;
105 pstrcpy(buf, time_to_asc(t));
106 all_string_sub(buf," ","&nbsp;",sizeof(buf));
107 return buf;
108}
109
110static void print_share_mode(const struct share_mode_entry *e,
111 const char *sharepath,
112 const char *fname,
113 void *dummy)
114{
115 char *utf8_fname;
116 int deny_mode;
117
118 if (!is_valid_share_mode_entry(e)) {
119 return;
120 }
121
122 deny_mode = map_share_mode_to_deny_mode(e->share_access,
123 e->private_options);
124
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;
135 }
136 printf("</td>");
137
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 "));
145 }
146 printf("</td>");
147
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>");
162
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);
167}
168
169
170/* kill off any connections chosen by the user */
171static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
172{
173 struct connections_data crec;
174
175 if (dbuf.dsize != sizeof(crec))
176 return 0;
177
178 memcpy(&crec, dbuf.dptr, sizeof(crec));
179
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);
186 }
187 }
188 return 0;
189}
190
191/* traversal fn for showing machine connections */
192static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
193{
194 struct connections_data crec;
195
196 if (dbuf.dsize != sizeof(crec))
197 return 0;
198
199 memcpy(&crec, dbuf.dptr, sizeof(crec));
200
201 if (crec.cnum == -1 || !process_exists(crec.pid) ||
202 procid_equal(&crec.pid, &smbd_pid))
203 return 0;
204
205 addPid2Machine (crec.pid, crec.machine);
206
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));
214 }
215 printf("</tr>\n");
216
217 return 0;
218}
219
220/* traversal fn for showing share connections */
221static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
222{
223 struct connections_data crec;
224
225 if (dbuf.dsize != sizeof(crec))
226 return 0;
227
228 memcpy(&crec, dbuf.dptr, sizeof(crec));
229
230 if (crec.cnum == -1 || !process_exists(crec.pid))
231 return 0;
232
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;
239}
240
241
242/* show the current server status */
243void status_page(void)
244{
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
252 smbd_pid = pid_to_procid(pidfile_pid("smbd"));
253
254 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
255 stop_smbd();
256 start_smbd();
257 waitup=True;
258 }
259
260 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
261 start_smbd();
262 waitup=True;
263 }
264
265 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
266 stop_smbd();
267 waitup=True;
268 }
269
270 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
271 stop_nmbd();
272 start_nmbd();
273 waitup=True;
274 }
275 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
276 start_nmbd();
277 waitup=True;
278 }
279
280 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
281 stop_nmbd();
282 waitup=True;
283 }
284
285#ifdef WITH_WINBIND
286 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
287 stop_winbindd();
288 start_winbindd();
289 waitup=True;
290 }
291
292 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
293 start_winbindd();
294 waitup=True;
295 }
296
297 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
298 stop_winbindd();
299 waitup=True;
300 }
301#endif
302 /* wait for daemons to start/stop */
303 if (waitup)
304 sleep(SLEEP_TIME);
305
306 if (cgi_variable("autorefresh")) {
307 autorefresh = 1;
308 } else if (cgi_variable("norefresh")) {
309 autorefresh = 0;
310 } else if (cgi_variable("refresh")) {
311 autorefresh = 1;
312 }
313
314 if ((v=cgi_variable("refresh_interval"))) {
315 refresh_interval = atoi(v);
316 }
317
318 if (cgi_variable("show_client_in_col_1")) {
319 PID_or_Machine = 1;
320 }
321
322 if (cgi_variable("show_pid_in_col_1")) {
323 PID_or_Machine = 0;
324 }
325
326 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
327 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
328
329 initPid2Machine ();
330
331 printf("<H2>%s</H2>\n", _("Server Status"));
332
333 printf("<FORM method=post>\n");
334
335 if (!autorefresh) {
336 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
337 printf("<br>%s", _("Refresh Interval: "));
338 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
339 refresh_interval);
340 } else {
341 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
342 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
343 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
344 }
345
346 printf("<p>\n");
347
348 if (!tdb) {
349 /* open failure either means no connections have been
350 made */
351 }
352
353
354 printf("<table>\n");
355
356 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
357
358 fflush(stdout);
359 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
360 if (geteuid() == 0) {
361 if (smbd_running()) {
362 nr_running++;
363 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
364 } else {
365 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
366 }
367 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
368 }
369 printf("</tr>\n");
370
371 fflush(stdout);
372 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
373 if (geteuid() == 0) {
374 if (nmbd_running()) {
375 nr_running++;
376 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
377 } else {
378 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
379 }
380 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
381 }
382 printf("</tr>\n");
383
384#ifdef WITH_WINBIND
385 fflush(stdout);
386 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
387 if (geteuid() == 0) {
388 if (winbindd_running()) {
389 nr_running++;
390 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
391 } else {
392 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
393 }
394 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
395 }
396 printf("</tr>\n");
397#endif
398
399 if (geteuid() == 0) {
400 printf("<tr><td></td><td></td>\n");
401 if (nr_running >= 1) {
402 /* stop, restart all */
403 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
404 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
405 }
406 else if (nr_running == 0) {
407 /* start all */
408 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
409 }
410 printf("</tr>\n");
411 }
412 printf("</table>\n");
413 fflush(stdout);
414
415 printf("<p><h3>%s</h3>\n", _("Active Connections"));
416 printf("<table border=1>\n");
417 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
418 if (geteuid() == 0) {
419 printf("<th>%s</th>\n", _("Kill"));
420 }
421 printf("</tr>\n");
422
423 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
424
425 printf("</table><p>\n");
426
427 printf("<p><h3>%s</h3>\n", _("Active Shares"));
428 printf("<table border=1>\n");
429 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
430 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
431
432 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
433
434 printf("</table><p>\n");
435
436 printf("<h3>%s</h3>\n", _("Open Files"));
437 printf("<table border=1>\n");
438 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"));
439
440 locking_init(1);
441 share_mode_forall(print_share_mode, NULL);
442 locking_end();
443 printf("</table>\n");
444
445 if (tdb) tdb_close(tdb);
446
447 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
448 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
449
450 printf("</FORM>\n");
451
452 if (autorefresh) {
453 /* this little JavaScript allows for automatic refresh
454 of the page. There are other methods but this seems
455 to be the best alternative */
456 printf("<script language=\"JavaScript\">\n");
457 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
458 cgi_baseurl(),
459 refresh_interval,
460 refresh_interval*1000);
461 printf("//-->\n</script>\n");
462 }
463}
Note: See TracBrowser for help on using the repository browser.