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