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