- claim the null connection after the session request to mak sure we
[nivanova/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 void print_share_mode(share_mode_entry *e, char *fname)
26 {
27         printf("<tr><td>%d</td>",e->pid);
28         printf("<td>");
29         switch ((e->share_mode>>4)&0xF) {
30         case DENY_NONE: printf("DENY_NONE"); break;
31         case DENY_ALL:  printf("DENY_ALL   "); break;
32         case DENY_DOS:  printf("DENY_DOS   "); break;
33         case DENY_READ: printf("DENY_READ  "); break;
34         case DENY_WRITE:printf("DENY_WRITE "); break;
35         }
36         printf("</td>");
37
38         printf("<td>");
39         switch (e->share_mode&0xF) {
40         case 0: printf("RDONLY     "); break;
41         case 1: printf("WRONLY     "); break;
42         case 2: printf("RDWR       "); break;
43         }
44         printf("</td>");
45
46         printf("<td>");
47         if((e->op_type & 
48             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
49            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
50                 printf("EXCLUSIVE+BATCH ");
51         else if (e->op_type & EXCLUSIVE_OPLOCK)
52                 printf("EXCLUSIVE       ");
53         else if (e->op_type & BATCH_OPLOCK)
54                 printf("BATCH           ");
55         else
56                 printf("NONE            ");
57         printf("</td>");
58
59         printf("<td>%s</td><td>%s</td></tr>\n",
60                fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
61 }
62
63
64 /* show the current server status */
65 void status_page(void)
66 {
67         struct connect_record crec;
68         pstring fname;
69         FILE *f;
70
71         if (cgi_variable("smbd_start")) {
72                 start_smbd();
73         }
74
75         if (cgi_variable("smbd_stop")) {
76                 stop_smbd();
77         }
78
79         if (cgi_variable("nmbd_start")) {
80                 start_nmbd();
81         }
82
83         if (cgi_variable("nmbd_stop")) {
84                 stop_nmbd();
85         }
86
87         pstrcpy(fname,lp_lockdir());
88         standard_sub_basic(fname);
89         trim_string(fname,"","/");
90         strcat(fname,"/STATUS..LCK");
91
92
93         f = fopen(fname,"r");
94         if (f) {
95                 while (!feof(f)) {
96                         if (fread(&crec,sizeof(crec),1,f) != 1) break;
97                         if (crec.magic == 0x280267 && crec.cnum == -1 &&
98                             process_exists(crec.pid)) {
99                                 char buf[30];
100                                 sprintf(buf,"kill_%d", crec.pid);
101                                 if (cgi_variable(buf)) {
102                                         kill_pid(crec.pid);
103                                 }
104                         }
105                 }
106                 fclose(f);
107         }
108
109         printf("<H2>Server Status</H2>\n");
110
111         printf("<FORM method=post>\n");
112
113         f = fopen(fname,"r");
114         if (!f) {
115                 printf("Couldn't open status file %s\n",fname);
116                 if (!lp_status(-1))
117                         printf("You need to have status=yes in your smb config file\n");
118                 return;
119         }
120
121
122         printf("<table>\n");
123
124         printf("<tr><td>version:</td><td>%s</td></tr>",VERSION);
125
126         fflush(stdout);
127         if (smbd_running()) {
128                 printf("<tr><td>smbd:</td><td>running</td><td><input type=submit name=\"smbd_stop\" value=\"Stop smbd\"></td></tr>\n");
129         } else {
130                 printf("<tr><td>smbd:</td><td>not running</td><td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td></tr>\n");
131         }
132
133         fflush(stdout);
134         if (nmbd_running()) {
135                 printf("<tr><td>nmbd:</td><td>running</td><td><input type=submit name=\"nmbd_stop\" value=\"Stop nmbd\"></td></tr>\n");
136         } else {
137                 printf("<tr><td>nmbd:</td><td>not running</td><td><input type=submit name=\"nmbd_start\" value=\"Start nmbd\"></td></tr>\n");
138         }
139
140         printf("</table>\n");
141         fflush(stdout);
142
143
144         if (geteuid() != 0)
145                 printf("<b>NOTE: You are not logged in as root and won't be able to start/stop the server</b><p>\n");
146
147         printf("<p><h3>Active Connections</h3>\n");
148         printf("<table border=1>\n");
149         printf("<tr><th>PID</th><th>Client</th><th>IP address</th><th>Date</th><th>Kill</th></tr>\n");
150
151         while (!feof(f)) {
152                 if (fread(&crec,sizeof(crec),1,f) != 1)
153                         break;
154                 if (crec.magic == 0x280267 && 
155                     crec.cnum == -1 &&
156                     process_exists(crec.pid)) {
157                         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",
158                                crec.pid,
159                                crec.machine,crec.addr,
160                                asctime(LocalTime(&crec.start)),
161                                crec.pid);
162                 }
163         }
164
165         printf("</table><p>\n");
166
167         fseek(f, 0, SEEK_SET);
168         
169         printf("<p><h3>Active Shares</h3>\n");
170         printf("<table border=1>\n");
171         printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
172
173         while (!feof(f)) {
174                 if (fread(&crec,sizeof(crec),1,f) != 1)
175                         break;
176                 if (crec.cnum == -1) continue;
177                 if (crec.magic == 0x280267 && process_exists(crec.pid)) {
178                         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n",
179                                crec.name,uidtoname(crec.uid),
180                                gidtoname(crec.gid),crec.pid,
181                                crec.machine,
182                                asctime(LocalTime(&crec.start)));
183                 }
184         }
185
186         printf("</table><p>\n");
187
188         printf("<h3>Open Files</h3>\n");
189         printf("<table border=1>\n");
190         printf("<tr><th>PID</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n");
191
192         locking_init(1);
193         share_mode_forall(print_share_mode);
194         locking_end();
195         printf("</table>\n");
196
197         fclose(f);
198
199         printf("</FORM>\n");
200 }
201