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