r5318: Fix a small problem in where we ignore the response from a SamrGetGroupsForUser
[Samba/gebeck_regimport.git] / source3 / web / statuspage.c
blob57b5d0f7b73ffd1d414a3b54a7071ab69c7cbf0a
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 pid_t pid;
32 char *machine;
35 static PIDMAP *pidmap;
36 static int PID_or_Machine; /* 0 = show PID, else show Machine name */
38 static pid_t 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;
47 for (p = pidmap; p != NULL; ) {
48 DLIST_REMOVE(pidmap, p);
49 SAFE_FREE(p->machine);
50 SAFE_FREE(p);
53 pidmap = NULL;
57 /* add new PID <-> Machine name mapping */
58 static void addPid2Machine (pid_t pid, char *machine)
60 /* show machine name rather PID on table "Open Files"? */
61 if (PID_or_Machine) {
62 PIDMAP *newmap;
64 if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
65 /* XXX need error message for this?
66 if malloc fails, PID is always shown */
67 return;
70 newmap->pid = pid;
71 newmap->machine = SMB_STRDUP(machine);
73 DLIST_ADD(pidmap, newmap);
77 /* lookup PID <-> Machine name mapping */
78 static char *mapPid2Machine (pid_t pid)
80 static char pidbuf [64];
81 PIDMAP *map;
83 /* show machine name rather PID on table "Open Files"? */
84 if (PID_or_Machine) {
85 for (map = pidmap; map != NULL; map = map->next) {
86 if (pid == map->pid) {
87 if (map->machine == NULL) /* no machine name */
88 break; /* show PID */
90 return map->machine;
95 /* PID not in list or machine name NULL? return pid as string */
96 snprintf (pidbuf, sizeof (pidbuf) - 1, "%lu", (unsigned long)pid);
97 return pidbuf;
100 static char *tstring(time_t t)
102 static pstring buf;
103 pstrcpy(buf, asctime(LocalTime(&t)));
104 all_string_sub(buf," ","&nbsp;",sizeof(buf));
105 return buf;
108 static void print_share_mode(share_mode_entry *e, char *fname)
110 char *utf8_fname;
112 printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
113 printf("<td>");
114 switch ((e->share_mode>>4)&0xF) {
115 case DENY_NONE: printf("DENY_NONE"); break;
116 case DENY_ALL: printf("DENY_ALL "); break;
117 case DENY_DOS: printf("DENY_DOS "); break;
118 case DENY_READ: printf("DENY_READ "); break;
119 case DENY_WRITE:printf("DENY_WRITE "); break;
121 printf("</td>");
123 printf("<td>");
124 switch (e->share_mode&0xF) {
125 case 0: printf("%s", _("RDONLY ")); break;
126 case 1: printf("%s", _("WRONLY ")); break;
127 case 2: printf("%s", _("RDWR ")); break;
129 printf("</td>");
131 printf("<td>");
132 if((e->op_type &
133 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
134 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
135 printf("EXCLUSIVE+BATCH ");
136 else if (e->op_type & EXCLUSIVE_OPLOCK)
137 printf("EXCLUSIVE ");
138 else if (e->op_type & BATCH_OPLOCK)
139 printf("BATCH ");
140 else if (e->op_type & LEVEL_II_OPLOCK)
141 printf("LEVEL_II ");
142 else
143 printf("NONE ");
144 printf("</td>");
146 push_utf8_allocate(&utf8_fname, fname);
147 printf("<td>%s</td><td>%s</td></tr>\n",
148 utf8_fname,tstring(e->time.tv_sec));
149 SAFE_FREE(utf8_fname);
153 /* kill off any connections chosen by the user */
154 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
156 struct connections_data crec;
158 if (dbuf.dsize != sizeof(crec))
159 return 0;
161 memcpy(&crec, dbuf.dptr, sizeof(crec));
163 if (crec.cnum == -1 && process_exists(crec.pid)) {
164 char buf[30];
165 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
166 if (cgi_variable(buf)) {
167 kill_pid(crec.pid);
168 sleep(SLEEP_TIME);
171 return 0;
174 /* traversal fn for showing machine connections */
175 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
177 struct connections_data crec;
179 if (dbuf.dsize != sizeof(crec))
180 return 0;
182 memcpy(&crec, dbuf.dptr, sizeof(crec));
184 if (crec.cnum == -1 || !process_exists(crec.pid) || (crec.pid == smbd_pid))
185 return 0;
187 addPid2Machine (crec.pid, crec.machine);
189 printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
190 (int)crec.pid,
191 crec.machine,crec.addr,
192 tstring(crec.start));
193 if (geteuid() == 0) {
194 printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
195 (int)crec.pid);
197 printf("</tr>\n");
199 return 0;
202 /* traversal fn for showing share connections */
203 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
205 struct connections_data crec;
207 if (dbuf.dsize != sizeof(crec))
208 return 0;
210 memcpy(&crec, dbuf.dptr, sizeof(crec));
212 if (crec.cnum == -1 || !process_exists(crec.pid))
213 return 0;
215 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
216 crec.name,uidtoname(crec.uid),
217 gidtoname(crec.gid),(int)crec.pid,
218 crec.machine,
219 tstring(crec.start));
220 return 0;
224 /* show the current server status */
225 void status_page(void)
227 const char *v;
228 int autorefresh=0;
229 int refresh_interval=30;
230 TDB_CONTEXT *tdb;
231 int nr_running=0;
232 BOOL waitup = False;
234 smbd_pid = pidfile_pid("smbd");
236 if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
237 stop_smbd();
238 start_smbd();
239 waitup=True;
242 if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
243 start_smbd();
244 waitup=True;
247 if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
248 stop_smbd();
249 waitup=True;
252 if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
253 stop_nmbd();
254 start_nmbd();
255 waitup=True;
257 if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
258 start_nmbd();
259 waitup=True;
262 if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
263 stop_nmbd();
264 waitup=True;
267 #ifdef WITH_WINBIND
268 if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
269 stop_winbindd();
270 start_winbindd();
271 waitup=True;
274 if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
275 start_winbindd();
276 waitup=True;
279 if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
280 stop_winbindd();
281 waitup=True;
283 #endif
284 /* wait for daemons to start/stop */
285 if (waitup)
286 sleep(SLEEP_TIME);
288 if (cgi_variable("autorefresh")) {
289 autorefresh = 1;
290 } else if (cgi_variable("norefresh")) {
291 autorefresh = 0;
292 } else if (cgi_variable("refresh")) {
293 autorefresh = 1;
296 if ((v=cgi_variable("refresh_interval"))) {
297 refresh_interval = atoi(v);
300 if (cgi_variable("show_client_in_col_1")) {
301 PID_or_Machine = 1;
304 if (cgi_variable("show_pid_in_col_1")) {
305 PID_or_Machine = 0;
308 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
309 if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
311 initPid2Machine ();
313 printf("<H2>%s</H2>\n", _("Server Status"));
315 printf("<FORM method=post>\n");
317 if (!autorefresh) {
318 printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
319 printf("<br>%s", _("Refresh Interval: "));
320 printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
321 refresh_interval);
322 } else {
323 printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
324 printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
325 printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
328 printf("<p>\n");
330 if (!tdb) {
331 /* open failure either means no connections have been
332 made */
336 printf("<table>\n");
338 printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
340 fflush(stdout);
341 printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
342 if (geteuid() == 0) {
343 if (smbd_running()) {
344 nr_running++;
345 printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
346 } else {
347 printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
349 printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
351 printf("</tr>\n");
353 fflush(stdout);
354 printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
355 if (geteuid() == 0) {
356 if (nmbd_running()) {
357 nr_running++;
358 printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
359 } else {
360 printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
362 printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
364 printf("</tr>\n");
366 #ifdef WITH_WINBIND
367 fflush(stdout);
368 printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
369 if (geteuid() == 0) {
370 if (winbindd_running()) {
371 nr_running++;
372 printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
373 } else {
374 printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
376 printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
378 printf("</tr>\n");
379 #endif
381 if (geteuid() == 0) {
382 printf("<tr><td></td><td></td>\n");
383 if (nr_running >= 1) {
384 /* stop, restart all */
385 printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
386 printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
388 else if (nr_running == 0) {
389 /* start all */
390 printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
392 printf("</tr>\n");
394 printf("</table>\n");
395 fflush(stdout);
397 printf("<p><h3>%s</h3>\n", _("Active Connections"));
398 printf("<table border=1>\n");
399 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
400 if (geteuid() == 0) {
401 printf("<th>%s</th>\n", _("Kill"));
403 printf("</tr>\n");
405 if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
407 printf("</table><p>\n");
409 printf("<p><h3>%s</h3>\n", _("Active Shares"));
410 printf("<table border=1>\n");
411 printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
412 _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
414 if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
416 printf("</table><p>\n");
418 printf("<h3>%s</h3>\n", _("Open Files"));
419 printf("<table border=1>\n");
420 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"));
422 locking_init(1);
423 share_mode_forall(print_share_mode);
424 locking_end();
425 printf("</table>\n");
427 if (tdb) tdb_close(tdb);
429 printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
430 printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
432 printf("</FORM>\n");
434 if (autorefresh) {
435 /* this little JavaScript allows for automatic refresh
436 of the page. There are other methods but this seems
437 to be the best alternative */
438 printf("<script language=\"JavaScript\">\n");
439 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
440 cgi_baseurl(),
441 refresh_interval,
442 refresh_interval*1000);
443 printf("//-->\n</script>\n");