spruced up SWAT a bit - it now uses the new Samba logo at the top and
[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=\"%simages/background.jpg\">\n\n", cgi_rooturl());
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=\"%shelp/parameters.html#%s\">?</A> %s</td><td>", 
136                cgi_rooturl(), 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=\"%s%s\" alt=\"%s\"></A>\n", 
359                cgi_baseurl(),
360                hlink, width, height, 
361                cgi_rooturl(),
362                src, name);
363 }
364
365 /* display the main navigation controls at the top of each page along
366    with a title */
367 static void show_main_buttons(void)
368 {
369         image_link("Home", "", "images/home.gif", 50, 50);
370         image_link("Globals", "globals", "images/globals.gif", 50, 50);
371         image_link("Shares", "shares", "images/shares.gif", 50, 50);
372         image_link("Printers", "printers", "images/printers.gif", 50, 50);
373         image_link("Status", "status", "images/status.gif", 50, 50);
374         image_link("View Config", "viewconfig", "images/viewconfig.gif", 50, 50);
375
376         printf("<HR>\n");
377 }
378
379 /* display a welcome page  */
380 static void welcome_page(void)
381 {
382         include_html("help/welcome.html");
383 }
384
385
386 /* display the current smb.conf  */
387 static void viewconfig_page(void)
388 {
389         int full_view=0;
390
391         if (cgi_variable("full_view")) {
392                 full_view = 1;
393         }
394
395         printf("<H2>Current Config</H2>\n");
396         printf("<form method=post>\n");
397
398         if (full_view) {
399                 printf("<input type=submit name=\"normal_view\" value=\"Normal View\">\n");
400         } else {
401                 printf("<input type=submit name=\"full_view\" value=\"Full View\">\n");
402         }
403
404         printf("<p><pre>");
405         write_config(stdout, full_view);
406         printf("</pre>");
407         printf("</form>\n");
408 }
409
410
411 /* display a globals editing page  */
412 static void globals_page(void)
413 {
414         int advanced = 0;
415
416         printf("<H2>Global Variables</H2>\n");
417
418         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
419                 advanced = 1;
420
421         if (cgi_variable("Commit")) {
422                 commit_parameters(GLOBALS_SNUM);
423                 save_reload();
424         }
425
426         printf("<FORM name=\"swatform\" method=post>\n");
427
428         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
429         printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
430         if (advanced == 0) {
431                 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
432         } else {
433                 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
434         }
435         printf("<p>\n");
436         
437         printf("<table>\n");
438         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
439         printf("</table>\n");
440
441         if (advanced) {
442                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
443         }
444
445         printf("</FORM>\n");
446 }
447
448 /* display a shares editing page  */
449 static void shares_page(void)
450 {
451         char *share = cgi_variable("share");
452         char *s;
453         int snum=-1;
454         int i;
455         int advanced = 0;
456
457         if (share)
458                 snum = lp_servicenumber(share);
459
460         printf("<H2>Share Parameters</H2>\n");
461
462         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
463                 advanced = 1;
464
465         if (cgi_variable("Commit") && snum >= 0) {
466                 commit_parameters(snum);
467                 save_reload();
468         }
469
470         if (cgi_variable("Delete") && snum >= 0) {
471                 lp_remove_service(snum);
472                 save_reload();
473                 share = NULL;
474                 snum = -1;
475         }
476
477         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
478                 lp_copy_service(GLOBALS_SNUM, share);
479                 save_reload();
480                 snum = lp_servicenumber(share);
481         }
482
483         printf("<FORM name=\"swatform\" method=post>\n");
484
485         printf("<table>\n");
486         printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
487         printf("<td><select name=share>\n");
488         if (snum < 0)
489                 printf("<option value=\" \"> \n");
490         for (i=0;i<lp_numservices();i++) {
491                 s = lp_servicename(i);
492                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
493                         printf("<option %s value=\"%s\">%s\n", 
494                                (share && strcmp(share,s)==0)?"SELECTED":"",
495                                s, s);
496                 }
497         }
498         printf("</select></td></tr><p>");
499
500         printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
501         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
502         printf("</table>");
503
504
505         if (snum >= 0) {
506                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
507                 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
508                 if (advanced == 0) {
509                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
510                 } else {
511                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
512                 }
513                 printf("<p>\n");
514         }
515
516         if (snum >= 0) {
517                 printf("<table>\n");
518                 show_parameters(snum, 1, advanced, 0);
519                 printf("</table>\n");
520         }
521
522         if (advanced) {
523                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
524         }
525
526         printf("</FORM>\n");
527 }
528
529
530 /* display a printers editing page  */
531 static void printers_page(void)
532 {
533         char *share = cgi_variable("share");
534         char *s;
535         int snum=-1;
536         int i;
537         int advanced = 0;
538
539         if (share)
540                 snum = lp_servicenumber(share);
541
542         printf("<H2>Printer Parameters</H2>\n");
543
544         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
545                 advanced = 1;
546
547         if (cgi_variable("Commit") && snum >= 0) {
548                 commit_parameters(snum);
549                 save_reload();
550         }
551
552         if (cgi_variable("Delete") && snum >= 0) {
553                 lp_remove_service(snum);
554                 save_reload();
555                 share = NULL;
556                 snum = -1;
557         }
558
559         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
560                 lp_copy_service(GLOBALS_SNUM, share);
561                 snum = lp_servicenumber(share);
562                 lp_do_parameter(snum, "print ok", "Yes");
563                 save_reload();
564                 snum = lp_servicenumber(share);
565         }
566
567         printf("<FORM name=\"swatform\" method=post>\n");
568
569         printf("<table>\n");
570         printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
571         printf("<td><select name=share>\n");
572         if (snum < 0 || !lp_print_ok(snum))
573                 printf("<option value=\" \"> \n");
574         for (i=0;i<lp_numservices();i++) {
575                 s = lp_servicename(i);
576                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
577                         printf("<option %s value=\"%s\">%s\n", 
578                                (share && strcmp(share,s)==0)?"SELECTED":"",
579                                s, s);
580                 }
581         }
582         printf("</select></td></tr><p>");
583
584         printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
585         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
586         printf("</table>");
587
588
589         if (snum >= 0) {
590                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
591                 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
592                 if (advanced == 0) {
593                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
594                 } else {
595                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
596                 }
597                 printf("<p>\n");
598         }
599
600         if (snum >= 0) {
601                 printf("<table>\n");
602                 show_parameters(snum, 1, advanced, 1);
603                 printf("</table>\n");
604         }
605
606         if (advanced) {
607                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
608         }
609
610         printf("</FORM>\n");
611 }
612
613
614
615 int main(int argc, char *argv[])
616 {
617         extern char *optarg;
618         extern int optind;
619         extern FILE *dbf;
620         int opt;
621         char *page;
622         int auth_required = 1;
623
624         /* just in case it goes wild ... */
625         alarm(300);
626
627         dbf = fopen("/dev/null", "w");
628
629         if (!dbf) dbf = stderr;
630
631         while ((opt = getopt(argc, argv,"s:a")) != EOF) {
632                 switch (opt) {
633                 case 's':
634                         pstrcpy(servicesf,optarg);
635                         break;    
636                 case 'a':
637                         auth_required = 0;
638                         break;    
639                 }
640         }
641
642         cgi_setup(SWATDIR, auth_required);
643
644         print_header();
645         
646         charset_initialise();
647
648         /* if this binary is setuid then run completely as root */
649         setuid(0);
650
651         load_config();
652
653         cgi_load_variables(NULL);
654
655         show_main_buttons();
656
657         page = cgi_pathinfo();
658
659         if (strcmp(page, "globals")==0) {
660                 globals_page();
661         } else if (strcmp(page,"shares")==0) {
662                 shares_page();
663         } else if (strcmp(page,"printers")==0) {
664                 printers_page();
665         } else if (strcmp(page,"status")==0) {
666                 status_page();
667         } else if (strcmp(page,"viewconfig")==0) {
668                 viewconfig_page();
669         } else {
670                 welcome_page();
671         }
672         
673         print_footer();
674         return 0;
675 }
676
677