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