converted all our existing shared memory code to use a tdb database
[kamenim/samba.git] / source3 / web / statuspage.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    web status page
5    Copyright (C) Andrew Tridgell 1997-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 static char *tstring(time_t t)
26 {
27         static pstring buf;
28         pstrcpy(buf, asctime(LocalTime(&t)));
29         all_string_sub(buf," "," ",sizeof(buf));
30         return buf;
31 }
32
33 static void print_share_mode(share_mode_entry *e, char *fname)
34 {
35         printf("<tr><td>%d</td>",(int)e->pid);
36         printf("<td>");
37         switch ((e->share_mode>>4)&0xF) {
38         case DENY_NONE: printf("DENY_NONE"); break;
39         case DENY_ALL:  printf("DENY_ALL   "); break;
40         case DENY_DOS:  printf("DENY_DOS   "); break;
41         case DENY_READ: printf("DENY_READ  "); break;
42         case DENY_WRITE:printf("DENY_WRITE "); break;
43         }
44         printf("</td>");
45
46         printf("<td>");
47         switch (e->share_mode&0xF) {
48         case 0: printf("RDONLY     "); break;
49         case 1: printf("WRONLY     "); break;
50         case 2: printf("RDWR       "); break;
51         }
52         printf("</td>");
53
54         printf("<td>");
55         if((e->op_type & 
56             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
57            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
58                 printf("EXCLUSIVE+BATCH ");
59         else if (e->op_type & EXCLUSIVE_OPLOCK)
60                 printf("EXCLUSIVE       ");
61         else if (e->op_type & BATCH_OPLOCK)
62                 printf("BATCH           ");
63         else if (e->op_type & LEVEL_II_OPLOCK)
64                 printf("LEVEL_II        ");
65         else
66                 printf("NONE            ");
67         printf("</td>");
68
69         printf("<td>%s</td><td>%s</td></tr>\n",
70                dos_to_unix(fname,False),tstring(e->time.tv_sec));
71 }
72
73
74 /* kill off any connections chosen by the user */
75 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
76 {
77         struct connections_data crec;
78         memcpy(&crec, dbuf.dptr, sizeof(crec));
79
80         if (crec.cnum == -1 && process_exists(crec.pid)) {
81                 char buf[30];
82                 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
83                 if (cgi_variable(buf)) {
84                         kill_pid(crec.pid);
85                 }
86         }
87         return 0;
88 }
89
90 /* traversal fn for showing machine connections */
91 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
92 {
93         struct connections_data crec;
94         memcpy(&crec, dbuf.dptr, sizeof(crec));
95         
96         if (crec.cnum != -1 || !process_exists(crec.pid)) return 0;
97
98         printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
99                (int)crec.pid,
100                crec.machine,crec.addr,
101                tstring(crec.start));
102         if (geteuid() == 0) {
103                 printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
104                        (int)crec.pid);
105         }
106         printf("</tr>\n");
107
108         return 0;
109 }
110
111 /* traversal fn for showing share connections */
112 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
113 {
114         struct connections_data crec;
115         memcpy(&crec, dbuf.dptr, sizeof(crec));
116
117         if (crec.cnum != -1 || !process_exists(crec.pid)) return 0;
118
119         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
120                crec.name,uidtoname(crec.uid),
121                gidtoname(crec.gid),(int)crec.pid,
122                crec.machine,
123                tstring(crec.start));
124         return 0;
125 }
126
127
128 /* show the current server status */
129 void status_page(void)
130 {
131         char *v;
132         int autorefresh=0;
133         int refresh_interval=30;
134         TDB_CONTEXT *tdb;
135
136         if (cgi_variable("smbd_restart")) {
137                 if (smbd_running())
138                         stop_smbd();
139                 start_smbd();
140         }
141
142         if (cgi_variable("smbd_start")) {
143                 start_smbd();
144         }
145
146         if (cgi_variable("smbd_stop")) {
147                 stop_smbd();
148         }
149
150         if (cgi_variable("nmbd_restart")) {
151                 if (nmbd_running())
152                         stop_nmbd();
153                 start_nmbd();
154         }
155         if (cgi_variable("nmbd_start")) {
156                 start_nmbd();
157         }
158
159         if (cgi_variable("nmbd_stop")) {
160                 stop_nmbd();
161         }
162
163         if (cgi_variable("autorefresh")) {
164                 autorefresh = 1;
165         } else if (cgi_variable("norefresh")) {
166                 autorefresh = 0;
167         } else if (cgi_variable("refresh")) {
168                 autorefresh = 1;
169         }
170
171         if ((v=cgi_variable("refresh_interval"))) {
172                 refresh_interval = atoi(v);
173         }
174
175         tdb = tdb_open(lock_path("connections.tdb"), 0, O_RDONLY, 0);
176         if (tdb) tdb_traverse(tdb, traverse_fn1);
177
178         printf("<H2>Server Status</H2>\n");
179
180         printf("<FORM method=post>\n");
181
182         if (!autorefresh) {
183                 printf("<input type=submit value=\"Auto Refresh\" name=autorefresh>\n");
184                 printf("<br>Refresh Interval: ");
185                 printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n", 
186                        refresh_interval);
187         } else {
188                 printf("<input type=submit value=\"Stop Refreshing\" name=norefresh>\n");
189                 printf("<br>Refresh Interval: %d\n", refresh_interval);
190                 printf("<input type=hidden name=refresh value=1>\n");
191         }
192
193         printf("<p>\n");
194
195         if (!tdb) {
196                 /* open failure either means no connections have been
197                    made or status=no */
198                 if (!lp_status(-1))
199                         printf("You need to have status=yes in your smb config file\n");
200         }
201
202
203         printf("<table>\n");
204
205         printf("<tr><td>version:</td><td>%s</td></tr>",VERSION);
206
207         fflush(stdout);
208         printf("<tr><td>smbd:</td><td>%srunning</td>\n",smbd_running()?"":"not ");
209         if (geteuid() == 0) {
210             if (smbd_running()) {
211                 printf("<td><input type=submit name=\"smbd_stop\" value=\"Stop smbd\"></td>\n");
212             } else {
213                 printf("<td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td>\n");
214             }
215             printf("<td><input type=submit name=\"smbd_restart\" value=\"Restart smbd\"></td>\n");
216         }
217         printf("</tr>\n");
218
219         fflush(stdout);
220         printf("<tr><td>nmbd:</td><td>%srunning</td>\n",nmbd_running()?"":"not ");
221         if (geteuid() == 0) {
222             if (nmbd_running()) {
223                 printf("<td><input type=submit name=\"nmbd_stop\" value=\"Stop nmbd\"></td>\n");
224             } else {
225                 printf("<td><input type=submit name=\"nmbd_start\" value=\"Start nmbd\"></td>\n");
226             }
227             printf("<td><input type=submit name=\"nmbd_restart\" value=\"Restart nmbd\"></td>\n");
228         }
229         printf("</tr>\n");
230
231         printf("</table>\n");
232         fflush(stdout);
233
234         printf("<p><h3>Active Connections</h3>\n");
235         printf("<table border=1>\n");
236         printf("<tr><th>PID</th><th>Client</th><th>IP address</th><th>Date</th>\n");
237         if (geteuid() == 0) {
238                 printf("<th>Kill</th>\n");
239         }
240         printf("</tr>\n");
241
242         if (tdb) tdb_traverse(tdb, traverse_fn2);
243
244         printf("</table><p>\n");
245
246         printf("<p><h3>Active Shares</h3>\n");
247         printf("<table border=1>\n");
248         printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
249
250         if (tdb) tdb_traverse(tdb, traverse_fn3);
251
252         printf("</table><p>\n");
253
254         printf("<h3>Open Files</h3>\n");
255         printf("<table border=1>\n");
256         printf("<tr><th>PID</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n");
257
258         locking_init(1);
259         share_mode_forall(print_share_mode);
260         locking_end();
261         printf("</table>\n");
262
263         tdb_close(tdb);
264
265         printf("</FORM>\n");
266
267         if (autorefresh) {
268                 /* this little JavaScript allows for automatic refresh
269                    of the page. There are other methods but this seems
270                    to be the best alternative */
271                 printf("<script language=\"JavaScript\">\n");
272                 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", 
273                        cgi_baseurl(),
274                        refresh_interval,
275                        refresh_interval*1000);
276                 printf("//-->\n</script>\n");
277         }
278 }
279