don't set the Expires header on any page generated from a POST. This
[tprouty/samba.git] / source / web / swat.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba Web Administration Tool
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 GLOBALS_SNUM -1
30
31 static pstring servicesf = CONFIGFILE;
32
33
34 /* we need these because we link to locking*.o */
35  void become_root(BOOL save_dir) {}
36  void unbecome_root(BOOL restore_dir) {}
37 connection_struct Connections[MAX_CONNECTIONS];
38 files_struct Files[MAX_OPEN_FILES];
39 struct current_user current_user;
40
41
42 /* start the page with standard stuff */
43 static void print_header(void)
44 {
45         if (!cgi_waspost()) {
46                 printf("Expires: 0\r\n");
47         }
48         printf("Content-type: text/html\r\n\r\n");
49         printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
50         printf("<HTML>\n<HEAD>\n<TITLE>Samba Web Administration Tool</TITLE>\n</HEAD>\n<BODY background=\"%simages/background.jpg\">\n\n", cgi_rooturl());
51 }
52
53
54 /* finish off the page */
55 static void print_footer(void)
56 {
57         printf("\n</BODY>\n</HTML>\n");
58 }
59
60 /* include a lump of html in a page */
61 static void include_html(char *fname)
62 {
63         FILE *f = fopen(fname,"r");
64         char buf[1024];
65         int ret;
66
67         if (!f) {
68                 printf("ERROR: Can't open %s\n", fname);
69                 return;
70         }
71
72         while (!feof(f)) {
73                 ret = fread(buf, 1, sizeof(buf), f);
74                 if (ret <= 0) break;
75                 fwrite(buf, 1, ret, stdout);
76         }
77
78         fclose(f);
79 }
80
81
82 /* display one editable parameter in a form */
83 static void show_parameter(int snum, struct parm_struct *parm)
84 {
85         int i;
86         void *ptr = parm->ptr;
87
88         if (parm->class == P_LOCAL && snum >= 0) {
89                 ptr = lp_local_ptr(snum, ptr);
90         }
91
92         printf("<tr><td><A HREF=\"%shelp/parameters.html#%s\">?</A> %s</td><td>", 
93                cgi_rooturl(), parm->label, parm->label);
94
95         switch (parm->type) {
96         case P_CHAR:
97                 printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
98                        parm->label, *(char *)ptr);
99                 break;
100
101         case P_STRING:
102         case P_USTRING:
103                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
104                        parm->label, *(char **)ptr);
105                 break;
106
107         case P_GSTRING:
108         case P_UGSTRING:
109                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
110                        parm->label, (char *)ptr);
111                 break;
112
113         case P_BOOL:
114                 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes&nbsp;&nbsp;", parm->label, (*(BOOL *)ptr)?"CHECKED":"");
115                 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm->label, (*(BOOL *)ptr)?"":"CHECKED");
116                 break;
117
118         case P_BOOLREV:
119                 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes&nbsp;&nbsp;", parm->label, (*(BOOL *)ptr)?"":"CHECKED");
120                 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm->label, (*(BOOL *)ptr)?"CHECKED":"");
121                 break;
122
123         case P_INTEGER:
124                 printf("<input type=text size=8 name=\"parm_%s\" value=%d>", parm->label, *(int *)ptr);
125                 break;
126
127         case P_OCTAL:
128                 printf("<input type=text size=8 name=\"parm_%s\" value=0%o>", parm->label, *(int *)ptr);
129                 break;
130
131         case P_ENUM:
132                 for (i=0;parm->enum_list[i].name;i++)
133                         printf("<input type=radio name=\"parm_%s\" value=%s %s>%s&nbsp;&nbsp;", 
134                                parm->label, parm->enum_list[i].name, 
135                                (*(int *)ptr)==parm->enum_list[i].value?"CHECKED":"", 
136                                parm->enum_list[i].name);
137                 break;
138         case P_SEP:
139                 break;
140         }
141         printf("</td></tr>\n");
142 }
143
144 /* display a set of parameters for a service */
145 static void show_parameters(int snum, int allparameters, int advanced, int printers)
146 {
147         int i = 0;
148         struct parm_struct *parm;
149         char *heading = NULL;
150         char *last_heading = NULL;
151
152         while ((parm = lp_next_parameter(snum, &i, allparameters))) {
153                 if (snum < 0 && parm->class == P_LOCAL && !(parm->flags & FLAG_GLOBAL))
154                         continue;
155                 if (parm->class == P_SEPARATOR) {
156                         heading = parm->label;
157                         continue;
158                 }
159                 if (parm->flags & FLAG_HIDE) continue;
160                 if (!advanced) {
161                         if (!printers && !(parm->flags & FLAG_BASIC)) continue;
162                         if (printers && !(parm->flags & FLAG_PRINT)) continue;
163                 }
164                 if (heading && heading != last_heading) {
165                         printf("<tr><td></td></tr><tr><td><b><u>%s</u></b></td></tr>\n", heading);
166                         last_heading = heading;
167                 }
168                 show_parameter(snum, parm);
169         }
170 }
171
172
173 /* write a config file */
174 static void write_config(FILE *f, BOOL show_defaults)
175 {
176         fprintf(f, "# Samba config file created using SWAT\n");
177         fprintf(f, "# from %s (%s)\n", cgi_remote_host(), cgi_remote_addr());
178         fprintf(f, "# Date: %s\n\n", timestring());
179         
180         lp_dump(f, show_defaults);      
181 }
182
183
184 /* save and reoad the smb.conf config file */
185 static int save_reload(void)
186 {
187         FILE *f;
188
189         f = fopen(servicesf,"w");
190         if (!f) {
191                 printf("failed to open %s for writing\n", servicesf);
192                 return 0;
193         }
194
195         write_config(f, False);
196         fclose(f);
197
198         lp_killunused(NULL);
199
200         if (!lp_load(servicesf,False,False,False)) {
201                 printf("Can't reload %s\n", servicesf);
202                 return 0;
203         }
204
205         return 1;
206 }
207
208
209
210 /* commit one parameter */
211 static void commit_parameter(int snum, struct parm_struct *parm, char *v)
212 {
213         int i;
214         char *s;
215
216         if (snum < 0 && parm->class == P_LOCAL) {
217                 /* this handles the case where we are changing a local
218                    variable globally. We need to change the parameter in 
219                    all shares where it is currently set to the default */
220                 for (i=0;i<lp_numservices();i++) {
221                         s = lp_servicename(i);
222                         if (s && (*s) && lp_is_default(i, parm)) {
223                                 lp_do_parameter(i, parm->label, v);
224                         }
225                 }
226         }
227
228         lp_do_parameter(snum, parm->label, v);
229 }
230
231 /* commit a set of parameters for a service */
232 static void commit_parameters(int snum)
233 {
234         int i = 0;
235         struct parm_struct *parm;
236         pstring label;
237         char *v;
238
239         while ((parm = lp_next_parameter(snum, &i, 1))) {
240                 sprintf(label, "parm_%s", parm->label);
241                 if ((v = cgi_variable(label))) {
242                         if (parm->flags & FLAG_HIDE) continue;
243                         commit_parameter(snum, parm, v); 
244                 }
245         }
246 }
247
248
249 /* load the smb.conf file into loadparm. */
250 static void load_config(void)
251 {
252         if (!lp_load(servicesf,False,True,False)) {
253                 printf("<b>Can't load %s - using defaults</b><p>\n", 
254                        servicesf);
255         }
256 }
257
258 /* spit out the html for a link with an image */
259 static void image_link(char *name,char *hlink, char *src, int width, int height)
260 {
261         printf("<A HREF=\"%s/%s\"><img width=%d height=%d src=\"%s%s\" alt=\"%s\"></A>\n", 
262                cgi_baseurl(),
263                hlink, width, height, 
264                cgi_rooturl(),
265                src, name);
266 }
267
268 /* display the main navigation controls at the top of each page along
269    with a title */
270 static void show_main_buttons(void)
271 {
272         printf("<H2 align=center>Samba Web Administration Tool</H2>\n");
273
274         image_link("Home", "", "images/home.gif", 50, 50);
275         image_link("Globals", "globals", "images/globals.gif", 50, 50);
276         image_link("Shares", "shares", "images/shares.gif", 50, 50);
277         image_link("Printers", "printers", "images/printers.gif", 50, 50);
278         image_link("Status", "status", "images/status.gif", 50, 50);
279         image_link("View Config", "viewconfig", "images/viewconfig.gif", 50, 50);
280
281         printf("<HR>\n");
282 }
283
284 /* display a welcome page  */
285 static void welcome_page(void)
286 {
287         include_html("help/welcome.html");
288 }
289
290
291 /* display the current smb.conf  */
292 static void viewconfig_page(void)
293 {
294         int full_view=0;
295
296         if (cgi_variable("full_view")) {
297                 full_view = 1;
298         }
299
300         printf("<H2>Current Config</H2>\n");
301         printf("<form method=post>\n");
302
303         if (full_view) {
304                 printf("<input type=submit name=\"normal_view\" value=\"Normal View\">\n");
305         } else {
306                 printf("<input type=submit name=\"full_view\" value=\"Full View\">\n");
307         }
308
309         printf("<p><pre>");
310         write_config(stdout, full_view);
311         printf("</pre>");
312         printf("</form>\n");
313 }
314
315
316 /* display a globals editing page  */
317 static void globals_page(void)
318 {
319         int advanced = 0;
320
321         printf("<H2>Global Variables</H2>\n");
322
323         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
324                 advanced = 1;
325
326         if (cgi_variable("Commit")) {
327                 commit_parameters(GLOBALS_SNUM);
328                 save_reload();
329         }
330
331         printf("<FORM method=post>\n");
332
333         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
334         if (advanced == 0) {
335                 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
336         } else {
337                 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
338         }
339         printf("<p>\n");
340         
341         printf("<table>\n");
342         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
343         printf("</table>\n");
344
345         if (advanced) {
346                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
347         }
348
349         printf("</form>\n");
350 }
351
352 /* display a shares editing page  */
353 static void shares_page(void)
354 {
355         char *share = cgi_variable("share");
356         char *s;
357         int snum=-1;
358         int i;
359         int advanced = 0;
360
361         if (share)
362                 snum = lp_servicenumber(share);
363
364         printf("<H2>Share Parameters</H2>\n");
365
366         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
367                 advanced = 1;
368
369         if (cgi_variable("Commit") && snum >= 0) {
370                 commit_parameters(snum);
371                 save_reload();
372         }
373
374         if (cgi_variable("Delete") && snum >= 0) {
375                 lp_remove_service(snum);
376                 save_reload();
377                 share = NULL;
378                 snum = -1;
379         }
380
381         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
382                 lp_copy_service(GLOBALS_SNUM, share);
383                 save_reload();
384                 snum = lp_servicenumber(share);
385         }
386
387         printf("<FORM method=post>\n");
388
389         printf("<table>\n");
390         printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
391         printf("<td><select name=share>\n");
392         if (snum < 0)
393                 printf("<option value=\" \"> \n");
394         for (i=0;i<lp_numservices();i++) {
395                 s = lp_servicename(i);
396                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
397                         printf("<option %s value=\"%s\">%s\n", 
398                                (share && strcmp(share,s)==0)?"SELECTED":"",
399                                s, s);
400                 }
401         }
402         printf("</select></td></tr><p>");
403
404         printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
405         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
406         printf("</table>");
407
408
409         if (snum >= 0) {
410                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
411                 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
412                 if (advanced == 0) {
413                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
414                 } else {
415                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
416                 }
417                 printf("<p>\n");
418         }
419
420         if (snum >= 0) {
421                 printf("<table>\n");
422                 show_parameters(snum, 1, advanced, 0);
423                 printf("</table>\n");
424         }
425
426         if (advanced) {
427                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
428         }
429
430         printf("</FORM>\n");
431 }
432
433
434 /* display a printers editing page  */
435 static void printers_page(void)
436 {
437         char *share = cgi_variable("share");
438         char *s;
439         int snum=-1;
440         int i;
441         int advanced = 0;
442
443         if (share)
444                 snum = lp_servicenumber(share);
445
446         printf("<H2>Printer Parameters</H2>\n");
447
448         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
449                 advanced = 1;
450
451         if (cgi_variable("Commit") && snum >= 0) {
452                 commit_parameters(snum);
453                 save_reload();
454         }
455
456         if (cgi_variable("Delete") && snum >= 0) {
457                 lp_remove_service(snum);
458                 save_reload();
459                 share = NULL;
460                 snum = -1;
461         }
462
463         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
464                 lp_copy_service(GLOBALS_SNUM, share);
465                 snum = lp_servicenumber(share);
466                 lp_do_parameter(snum, "print ok", "Yes");
467                 save_reload();
468                 snum = lp_servicenumber(share);
469         }
470
471         printf("<FORM method=post>\n");
472
473         printf("<table>\n");
474         printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
475         printf("<td><select name=share>\n");
476         if (snum < 0 || !lp_print_ok(snum))
477                 printf("<option value=\" \"> \n");
478         for (i=0;i<lp_numservices();i++) {
479                 s = lp_servicename(i);
480                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
481                         printf("<option %s value=\"%s\">%s\n", 
482                                (share && strcmp(share,s)==0)?"SELECTED":"",
483                                s, s);
484                 }
485         }
486         printf("</select></td></tr><p>");
487
488         printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
489         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
490         printf("</table>");
491
492
493         if (snum >= 0) {
494                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
495                 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
496                 if (advanced == 0) {
497                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
498                 } else {
499                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
500                 }
501                 printf("<p>\n");
502         }
503
504         if (snum >= 0) {
505                 printf("<table>\n");
506                 show_parameters(snum, 1, advanced, 1);
507                 printf("</table>\n");
508         }
509
510         if (advanced) {
511                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
512         }
513
514         printf("</FORM>\n");
515 }
516
517
518
519 int main(int argc, char *argv[])
520 {
521         extern char *optarg;
522         extern int optind;
523         extern FILE *dbf;
524         int opt;
525         char *page;
526         int auth_required = 1;
527
528         /* just in case it goes wild ... */
529         alarm(300);
530
531         dbf = fopen("/dev/null", "w");
532
533         if (!dbf) dbf = stderr;
534
535         while ((opt = getopt(argc, argv,"s:a")) != EOF) {
536                 switch (opt) {
537                 case 's':
538                         pstrcpy(servicesf,optarg);
539                         break;    
540                 case 'a':
541                         auth_required = 0;
542                         break;    
543                 }
544         }
545
546         cgi_setup(SWATDIR, auth_required);
547
548         print_header();
549         
550         charset_initialise();
551
552         /* if this binary is setuid then run completely as root */
553         setuid(0);
554
555         load_config();
556
557         cgi_load_variables(NULL);
558
559         show_main_buttons();
560
561         page = cgi_pathinfo();
562
563         if (strcmp(page, "globals")==0) {
564                 globals_page();
565         } else if (strcmp(page,"shares")==0) {
566                 shares_page();
567         } else if (strcmp(page,"printers")==0) {
568                 printers_page();
569         } else if (strcmp(page,"status")==0) {
570                 status_page();
571         } else if (strcmp(page,"viewconfig")==0) {
572                 viewconfig_page();
573         } else {
574                 welcome_page();
575         }
576         
577         print_footer();
578         return 0;
579 }
580
581