Added in oplock info.
[kai/samba.git] / source3 / wsmbconf.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    html smb.conf editing - prototype only
5    Copyright (C) Andrew Tridgell 1997
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 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
25
26 #include "includes.h"
27 #include "smb.h"
28
29 #define SDEFAULTS "Service defaults"
30 #define SGLOBAL "Global Parameters"
31 #define GLOBALS_SNUM -2
32 #define DEFAULTS_SNUM -1
33
34
35 /* start the page with standard stuff */
36 static void print_header(void)
37 {
38         printf("Content-type: text/html\n\n");
39         printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
40         printf("<HTML>\n<HEAD>\n<TITLE>smb.conf</TITLE>\n</HEAD>\n<BODY>\n\n");
41 }
42
43
44 /* finish off the page */
45 static void print_footer(void)
46 {
47         printf("\n</BODY>\n</HTML>\n");
48 }
49
50 /* setup persisant variables */
51 static void set_persistent(char *name)
52 {
53         char *p;
54         p = cgi_variable(name);
55         if (!p) return;
56         printf("<input type=hidden name=%s value=%s>\n", name, p);
57 }
58
59 /* display a servce, ready for editing */
60 static void show_service(int snum, int allparameters)
61 {
62         int i = 0;
63         pstring label, value;
64         char *sname;
65
66         if (snum == GLOBALS_SNUM) 
67                 sname = SGLOBAL;
68         else if (snum == DEFAULTS_SNUM)
69                 sname = SDEFAULTS;
70         else sname = lp_servicename(snum);
71
72         printf("\n<p><table border=0>\n<tr>\n<td></td><td>\n\n");
73         printf("<form method=POST>\n");
74         printf("<H3>%s</H3>\n", sname);
75         printf("<input type=hidden name=service value=\"%s\">\n", sname);
76         printf("<input type=submit name=request value=Change>\n");
77         printf("<input type=submit name=request value=Rename>\n");
78         printf("<input type=submit name=request value=Copy>\n");
79         printf("<input type=submit name=request value=Remove>\n");
80         printf("<br><input name=newvalue><br>\n");
81         printf("<select name=parameter size=5>\n");
82         
83         while (lp_next_parameter(snum, &i, label, value, allparameters)) {
84                 printf("<option value=\"%s\">%s = %s\n", 
85                        label, label, value);
86         }
87
88         printf("</select>\n");
89         printf("</form>\n</td>\n</tr>\n</table>\n");
90
91         printf("<p>\n");
92 }
93
94
95 /* loop over all services, displaying them one after the other */
96 static void show_services(void)
97 {
98         int i;
99         int n;
100         int allparameters = cgi_boolean("allparameters", 0);
101
102         printf("<FORM METHOD=POST>\n");
103         printf("<p>Show all parameters?\n");
104         printf("<INPUT NAME=allparameters TYPE=checkbox VALUE=1 %s>\n",
105                allparameters?"CHECKED":"");
106
107         printf("<INPUT TYPE=submit NAME=reload VALUE=Reload>\n");
108         
109         printf("</FORM>\n");
110         
111         n = lp_numservices();
112
113         show_service(GLOBALS_SNUM, allparameters);
114         show_service(DEFAULTS_SNUM, allparameters);
115         
116         for (i=0;i<n;i++)
117                 if (VALID_SNUM(i))
118                         show_service(i, allparameters);
119 }
120
121
122 /* load the smb.conf file into loadparm. this also does the chroot
123    to the config directory. This must be called _BEFORE_ any client
124    supplied data is parsed */
125 static int load_config(void)
126 {
127         static pstring servicesf = CONFIGFILE;
128         char *p;
129
130         p = strrchr(servicesf,'/');
131         if (!p) return 0;
132
133         *p = 0;
134
135         setuid(0);
136
137         if (chdir(servicesf) || chroot(servicesf)) {
138                 printf("wsmbconf is not configured correctly\n");
139                 return 0;
140         }
141
142         *p = '/';
143
144         if (!lp_load(p,False)) {
145                 printf("<b>Can't load %s - using defaults</b><p>\n", 
146                        servicesf);
147         }
148         return 1;
149 }
150
151
152 static int save_reload(void)
153 {
154         static pstring servicesf = CONFIGFILE;
155         char *p;
156         FILE *f;
157
158         p = strrchr(servicesf,'/');
159         if (!p) return 0;
160
161         f = fopen(p,"w");
162         if (!f) {
163                 printf("failed to open %s for writing\n", servicesf);
164                 return 0;
165         }
166
167         fprintf(f, "# Samba config file created using wsmbconf\n");
168
169         lp_dump(f);
170
171         fclose(f);
172
173         lp_killunused(NULL);
174
175         if (!lp_load(p,False)) {
176                 printf("Can't reload %s\n", servicesf);
177                 return 0;
178         }
179
180         return 1;
181 }
182
183 static void process_requests(void)
184 {
185         char *req = cgi_variable("request");
186         char *newvalue = cgi_variable("newvalue");
187         char *parameter = cgi_variable("parameter");
188         char *service = cgi_variable("service");
189         int snum=0;
190
191         if (!req) return;
192
193         if (service) {
194                 /* work out what service it is */
195                 if (strcmp(service,SGLOBAL) == 0) {
196                         snum = GLOBALS_SNUM;
197                 } else if (strcmp(service,SDEFAULTS) == 0) {
198                         snum = DEFAULTS_SNUM;
199                 } else {
200                         snum = lp_servicenumber(service);
201                         if (snum < 0) return;
202                 }
203         }
204
205         if (!newvalue)
206                 newvalue = "";
207
208         if (strcmp(req,"Change") == 0) {
209                 /* change the value of a parameter */
210                 if (!parameter || !service) return;
211
212                 lp_do_parameter(snum, parameter, newvalue); 
213         } else if (strcmp(req,"Rename") == 0) {
214                 /* rename a service */
215                 if (!newvalue || !service) return;
216
217                 lp_rename_service(snum, newvalue);
218         } else if (strcmp(req,"Remove") == 0) {
219                 /* remove a service */
220                 if (!service) return;
221
222                 lp_remove_service(snum);
223         } else if (strcmp(req,"Copy") == 0) {
224                 /* copy a service */
225                 if (!service || !newvalue) return;
226
227                 lp_copy_service(snum, newvalue);
228         }
229
230         save_reload();
231 }
232
233
234 int main(int argc, char *argv[])
235 {
236         extern FILE *dbf;
237
238         print_header();
239
240         dbf = stderr;
241
242         charset_initialise();
243
244         if (load_config()) {
245                 cgi_load_variables();
246                 process_requests();
247                 show_services();
248         }
249         print_footer();
250         return 0;
251 }