fix comments
[Samba.git] / source / wsmbconf.c
blob203952c204f79166080190be0e3605cb3d28c973
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 html smb.conf editing - prototype only
5 Copyright (C) Andrew Tridgell 1997
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.
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.
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.
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
26 #include "includes.h"
27 #include "smb.h"
29 #define SDEFAULTS "Service defaults"
30 #define SGLOBAL "Global Parameters"
31 #define GLOBALS_SNUM -2
32 #define DEFAULTS_SNUM -1
35 /* start the page with standard stuff */
36 static void print_header(void)
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");
44 /* finish off the page */
45 static void print_footer(void)
47 printf("\n</BODY>\n</HTML>\n");
50 /* setup persisant variables */
51 static void set_persistent(char *name)
53 char *p;
54 p = cgi_variable(name);
55 if (!p) return;
56 printf("<input type=hidden name=%s value=%s>\n", name, p);
59 /* display a servce, ready for editing */
60 static void show_service(int snum, int allparameters)
62 int i = 0;
63 pstring label, value;
64 char *sname;
66 if (snum == GLOBALS_SNUM)
67 sname = SGLOBAL;
68 else if (snum == DEFAULTS_SNUM)
69 sname = SDEFAULTS;
70 else sname = lp_servicename(snum);
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");
83 while (lp_next_parameter(snum, &i, label, value, allparameters)) {
84 printf("<option value=\"%s\">%s = %s\n",
85 label, label, value);
88 printf("</select>\n");
89 printf("</form>\n</td>\n</tr>\n</table>\n");
91 printf("<p>\n");
95 /* loop over all services, displaying them one after the other */
96 static void show_services(void)
98 int i;
99 int n;
100 int allparameters = cgi_boolean("allparameters", 0);
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":"");
107 printf("<INPUT TYPE=submit NAME=reload VALUE=Reload>\n");
109 printf("</FORM>\n");
111 n = lp_numservices();
113 show_service(GLOBALS_SNUM, allparameters);
114 show_service(DEFAULTS_SNUM, allparameters);
116 for (i=0;i<n;i++)
117 if (VALID_SNUM(i))
118 show_service(i, allparameters);
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)
127 static pstring servicesf = CONFIGFILE;
128 char *p;
130 p = strrchr(servicesf,'/');
131 if (!p) return 0;
133 *p = 0;
135 setuid(0);
137 if (chdir(servicesf) || chroot(servicesf)) {
138 printf("wsmbconf is not configured correctly\n");
139 return 0;
142 *p = '/';
144 if (!lp_load(p,False)) {
145 printf("<b>Can't load %s - using defaults</b><p>\n",
146 servicesf);
148 return 1;
152 static int save_reload(void)
154 static pstring servicesf = CONFIGFILE;
155 char *p;
156 FILE *f;
158 p = strrchr(servicesf,'/');
159 if (!p) return 0;
161 f = fopen(p,"w");
162 if (!f) {
163 printf("failed to open %s for writing\n", servicesf);
164 return 0;
167 fprintf(f, "# Samba config file created using wsmbconf\n");
169 lp_dump(f);
171 fclose(f);
173 lp_killunused(NULL);
175 if (!lp_load(p,False)) {
176 printf("Can't reload %s\n", servicesf);
177 return 0;
180 return 1;
183 static void process_requests(void)
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;
191 if (!req) return;
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;
205 if (!newvalue)
206 newvalue = "";
208 if (strcmp(req,"Change") == 0) {
209 /* change the value of a parameter */
210 if (!parameter || !service) return;
212 lp_do_parameter(snum, parameter, newvalue);
213 } else if (strcmp(req,"Rename") == 0) {
214 /* rename a service */
215 if (!newvalue || !service) return;
217 lp_rename_service(snum, newvalue);
218 } else if (strcmp(req,"Remove") == 0) {
219 /* remove a service */
220 if (!service) return;
222 lp_remove_service(snum);
223 } else if (strcmp(req,"Copy") == 0) {
224 /* copy a service */
225 if (!service || !newvalue) return;
227 lp_copy_service(snum, newvalue);
230 save_reload();
234 int main(int argc, char *argv[])
236 extern FILE *dbf;
238 print_header();
240 dbf = stderr;
242 charset_initialise();
244 if (load_config()) {
245 cgi_load_variables();
246 process_requests();
247 show_services();
249 print_footer();
250 return 0;