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