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