f3b07425b7880ae1182b8a34e30a7027d121ed10
[samba.git] / source / web / statuspage.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2.
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
80         if (dbuf.dsize != sizeof(crec))
81                 return 0;
82
83         memcpy(&crec, dbuf.dptr, sizeof(crec));
84
85         if (crec.cnum == -1 && process_exists(crec.pid)) {
86                 char buf[30];
87                 slprintf(buf,sizeof(buf)-1,"kill_%d", (int)crec.pid);
88                 if (cgi_variable(buf)) {
89                         kill_pid(crec.pid);
90                 }
91         }
92         return 0;
93 }
94
95 /* traversal fn for showing machine connections */
96 static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
97 {
98         struct connections_data crec;
99
100         if (dbuf.dsize != sizeof(crec))
101                 return 0;
102
103         memcpy(&crec, dbuf.dptr, sizeof(crec));
104         
105         if (crec.cnum != -1 || !process_exists(crec.pid) || (crec.pid == smbd_pid))
106                 return 0;
107
108         printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td>\n",
109                (int)crec.pid,
110                crec.machine,crec.addr,
111                tstring(crec.start));
112         if (geteuid() == 0) {
113                 printf("<td><input type=submit value=\"X\" name=\"kill_%d\"></td>\n",
114                        (int)crec.pid);
115         }
116         printf("</tr>\n");
117
118         return 0;
119 }
120
121 /* traversal fn for showing share connections */
122 static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
123 {
124         struct connections_data crec;
125
126         if (dbuf.dsize != sizeof(crec))
127                 return 0;
128
129         memcpy(&crec, dbuf.dptr, sizeof(crec));
130
131         if (crec.cnum == -1 || !process_exists(crec.pid))
132                 return 0;
133
134         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
135                crec.name,uidtoname(crec.uid),
136                gidtoname(crec.gid),(int)crec.pid,
137                crec.machine,
138                tstring(crec.start));
139         return 0;
140 }
141
142
143 /* show the current server status */
144 void status_page(void)
145 {
146         char *v;
147         int autorefresh=0;
148         int refresh_interval=30;
149         TDB_CONTEXT *tdb;
150
151         smbd_pid = pidfile_pid("smbd");
152
153         if (cgi_variable("smbd_restart")) {
154                 stop_smbd();
155                 start_smbd();
156         }
157
158         if (cgi_variable("smbd_start")) {
159                 start_smbd();
160         }
161
162         if (cgi_variable("smbd_stop")) {
163                 stop_smbd();
164         }
165
166         if (cgi_variable("nmbd_restart")) {
167                 stop_nmbd();
168                 start_nmbd();
169         }
170         if (cgi_variable("nmbd_start")) {
171                 start_nmbd();
172         }
173
174         if (cgi_variable("nmbd_stop")) {
175                 stop_nmbd();
176         }
177
178         if (cgi_variable("autorefresh")) {
179                 autorefresh = 1;
180         } else if (cgi_variable("norefresh")) {
181                 autorefresh = 0;
182         } else if (cgi_variable("refresh")) {
183                 autorefresh = 1;
184         }
185
186         if ((v=cgi_variable("refresh_interval"))) {
187                 refresh_interval = atoi(v);
188         }
189
190         tdb = tdb_open_log(lock_path("connections.tdb"), 0, 0, O_RDONLY, 0);
191         if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
192
193         printf("<H2>Server Status</H2>\n");
194
195         printf("<FORM method=post>\n");
196
197         if (!autorefresh) {
198                 printf("<input type=submit value=\"Auto Refresh\" name=autorefresh>\n");
199                 printf("<br>Refresh Interval: ");
200                 printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n", 
201                        refresh_interval);
202         } else {
203                 printf("<input type=submit value=\"Stop Refreshing\" name=norefresh>\n");
204                 printf("<br>Refresh Interval: %d\n", refresh_interval);
205                 printf("<input type=hidden name=refresh value=1>\n");
206         }
207
208         printf("<p>\n");
209
210         if (!tdb) {
211                 /* open failure either means no connections have been
212                    made or status=no */
213                 if (!lp_status(-1))
214                         printf("You need to have status=yes in your smb config file\n");
215         }
216
217
218         printf("<table>\n");
219
220         printf("<tr><td>version:</td><td>%s</td></tr>",VERSION);
221
222         fflush(stdout);
223         printf("<tr><td>smbd:</td><td>%srunning</td>\n",smbd_running()?"":"not ");
224         if (geteuid() == 0) {
225             if (smbd_running()) {
226                 printf("<td><input type=submit name=\"smbd_stop\" value=\"Stop smbd\"></td>\n");
227             } else {
228                 printf("<td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td>\n");
229             }
230             printf("<td><input type=submit name=\"smbd_restart\" value=\"Restart smbd\"></td>\n");
231         }
232         printf("</tr>\n");
233
234         fflush(stdout);
235         printf("<tr><td>nmbd:</td><td>%srunning</td>\n",nmbd_running()?"":"not ");
236         if (geteuid() == 0) {
237             if (nmbd_running()) {
238                 printf("<td><input type=submit name=\"nmbd_stop\" value=\"Stop nmbd\"></td>\n");
239             } else {
240                 printf("<td><input type=submit name=\"nmbd_start\" value=\"Start nmbd\"></td>\n");
241             }
242             printf("<td><input type=submit name=\"nmbd_restart\" value=\"Restart nmbd\"></td>\n");
243         }
244         printf("</tr>\n");
245
246         printf("</table>\n");
247         fflush(stdout);
248
249         printf("<p><h3>Active Connections</h3>\n");
250         printf("<table border=1>\n");
251         printf("<tr><th>PID</th><th>Client</th><th>IP address</th><th>Date</th>\n");
252         if (geteuid() == 0) {
253                 printf("<th>Kill</th>\n");
254         }
255         printf("</tr>\n");
256
257         if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
258
259         printf("</table><p>\n");
260
261         printf("<p><h3>Active Shares</h3>\n");
262         printf("<table border=1>\n");
263         printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
264
265         if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
266
267         printf("</table><p>\n");
268
269         printf("<h3>Open Files</h3>\n");
270         printf("<table border=1>\n");
271         printf("<tr><th>PID</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n");
272
273         locking_init(1);
274         share_mode_forall(print_share_mode);
275         locking_end();
276         printf("</table>\n");
277
278         if (tdb) tdb_close(tdb);
279
280         printf("</FORM>\n");
281
282         if (autorefresh) {
283                 /* this little JavaScript allows for automatic refresh
284                    of the page. There are other methods but this seems
285                    to be the best alternative */
286                 printf("<script language=\"JavaScript\">\n");
287                 printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", 
288                        cgi_baseurl(),
289                        refresh_interval,
290                        refresh_interval*1000);
291                 printf("//-->\n</script>\n");
292         }
293 }