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