handle P_LIST parameters
[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 static BOOL demo_mode = False;
33 static BOOL have_write_access = False;
34 static BOOL have_read_access = False;
35 static int iNumNonAutoPrintServices = 0;
36
37 /*
38  * Password Management Globals
39  */
40 #define SWAT_USER "username"
41 #define OLD_PSWD "old_passwd"
42 #define NEW_PSWD "new_passwd"
43 #define NEW2_PSWD "new2_passwd"
44 #define CHG_S_PASSWD_FLAG "chg_s_passwd_flag"
45 #define CHG_R_PASSWD_FLAG "chg_r_passwd_flag"
46 #define ADD_USER_FLAG "add_user_flag"
47 #define DELETE_USER_FLAG "delete_user_flag"
48 #define DISABLE_USER_FLAG "disable_user_flag"
49 #define ENABLE_USER_FLAG "enable_user_flag"
50 #define RHOST "remote_host"
51
52 /* we need these because we link to locking*.o */
53  void become_root(void) {}
54  void unbecome_root(void) {}
55
56 /****************************************************************************
57 ****************************************************************************/
58 static int enum_index(int value, struct enum_list *enumlist)
59 {
60         int i;
61         for (i=0;enumlist[i].name;i++)
62                 if (value == enumlist[i].value) break;
63         return(i);
64 }
65
66 static char *fix_backslash(char *str)
67 {
68         static char newstring[1024];
69         char *p = newstring;
70
71         while (*str) {
72                 if (*str == '\\') {*p++ = '\\';*p++ = '\\';}
73                 else *p++ = *str;
74                 ++str;
75         }
76         *p = '\0';
77         return newstring;
78 }
79
80 static char *stripspace(char *str)
81 {
82 static char newstring[1024];
83 char *p = newstring;
84
85         while (*str) {
86                 if (*str != ' ') *p++ = *str;
87                 ++str;
88         }
89         *p = '\0';
90         return newstring;
91 }
92
93 static char *make_parm_name(char *label)
94 {
95         static char parmname[1024];
96         char *p = parmname;
97
98         while (*label) {
99                 if (*label == ' ') *p++ = '_';
100                 else *p++ = *label;
101                 ++label;
102         }
103         *p = '\0';
104         return parmname;
105 }
106
107 /****************************************************************************
108   include a lump of html in a page 
109 ****************************************************************************/
110 static int include_html(char *fname)
111 {
112         FILE *f = sys_fopen(fname,"r");
113         char buf[1024];
114         int ret;
115
116         if (!f) {
117                 printf("ERROR: Can't open %s\n", fname);
118                 return 0;
119         }
120
121         while (!feof(f)) {
122                 ret = fread(buf, 1, sizeof(buf), f);
123                 if (ret <= 0) break;
124                 fwrite(buf, 1, ret, stdout);
125         }
126
127         fclose(f);
128         return 1;
129 }
130
131 /****************************************************************************
132   start the page with standard stuff 
133 ****************************************************************************/
134 static void print_header(void)
135 {
136         if (!cgi_waspost()) {
137                 printf("Expires: 0\r\n");
138         }
139         printf("Content-type: text/html\r\n\r\n");
140
141         if (!include_html("include/header.html")) {
142                 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
143                 printf("<HTML>\n<HEAD>\n<TITLE>Samba Web Administration Tool</TITLE>\n</HEAD>\n<BODY background=\"/swat/images/background.jpg\">\n\n");
144         }
145 }
146
147 /****************************************************************************
148  finish off the page 
149 ****************************************************************************/
150 static void print_footer(void)
151 {
152         if (!include_html("include/footer.html")) {
153                 printf("\n</BODY>\n</HTML>\n");
154         }
155 }
156
157 /****************************************************************************
158   display one editable parameter in a form 
159 ****************************************************************************/
160 static void show_parameter(int snum, struct parm_struct *parm)
161 {
162         int i;
163         void *ptr = parm->ptr;
164         char* str;
165
166         if (parm->class == P_LOCAL && snum >= 0) {
167                 ptr = lp_local_ptr(snum, ptr);
168         }
169
170         str = stripspace(parm->label);
171         strupper (str);
172         printf("<tr><td><A HREF=\"/swat/help/smb.conf.5.html#%s\" target=\"docs\">Help</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s</td><td>", 
173                str, parm->label);
174
175         switch (parm->type) {
176         case P_CHAR:
177                 printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
178                        make_parm_name(parm->label), *(char *)ptr);
179                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'%c\'\">",
180                         make_parm_name(parm->label),(char)(parm->def.cvalue));
181                 break;
182
183         case P_LIST:
184                 printf("<input type=text size=40 name=\"parm_%s\" value=\"",
185                         make_parm_name(parm->label));
186                 if (ptr) {
187                         char** list = ptr;
188                         for (;*list;list++) {
189                                 printf("%s%s", *list, ((*(list+1))?" ":""));
190                         }
191                 }
192                 printf("\">");
193                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'",
194                         make_parm_name(parm->label));
195                 if (parm->def.lvalue) {
196                         char **list = parm->def.lvalue;
197                         for (; *list; list++) {
198                                 printf("%s%s", *list, ((*(list+1))?" ":""));
199                         }
200                 }
201                 printf("\'\">");
202                 break;
203
204         case P_STRING:
205         case P_USTRING:
206                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
207                        make_parm_name(parm->label), *(char **)ptr);
208                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
209                         make_parm_name(parm->label),fix_backslash((char *)(parm->def.svalue)));
210                 break;
211
212         case P_GSTRING:
213         case P_UGSTRING:
214                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
215                        make_parm_name(parm->label), (char *)ptr);
216                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
217                         make_parm_name(parm->label),fix_backslash((char *)(parm->def.svalue)));
218                 break;
219
220         case P_BOOL:
221                 printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
222                 printf("<option %s>Yes", (*(BOOL *)ptr)?"selected":"");
223                 printf("<option %s>No", (*(BOOL *)ptr)?"":"selected");
224                 printf("</select>");
225                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
226                         make_parm_name(parm->label),(BOOL)(parm->def.bvalue)?0:1);
227                 break;
228
229         case P_BOOLREV:
230                 printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
231                 printf("<option %s>Yes", (*(BOOL *)ptr)?"":"selected");
232                 printf("<option %s>No", (*(BOOL *)ptr)?"selected":"");
233                 printf("</select>");
234                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
235                         make_parm_name(parm->label),(BOOL)(parm->def.bvalue)?1:0);
236                 break;
237
238         case P_INTEGER:
239                 printf("<input type=text size=8 name=\"parm_%s\" value=%d>", make_parm_name(parm->label), *(int *)ptr);
240                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'%d\'\">",
241                         make_parm_name(parm->label),(int)(parm->def.ivalue));
242                 break;
243
244         case P_OCTAL:
245                 printf("<input type=text size=8 name=\"parm_%s\" value=%s>", make_parm_name(parm->label), octal_string(*(int *)ptr));
246                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
247                        make_parm_name(parm->label),
248                        octal_string((int)(parm->def.ivalue)));
249                 break;
250
251         case P_ENUM:
252                 printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
253                 for (i=0;parm->enum_list[i].name;i++) {
254                         if (i == 0 || parm->enum_list[i].value != parm->enum_list[i-1].value) {
255                                 printf("<option %s>%s",(*(int *)ptr)==parm->enum_list[i].value?"selected":"",parm->enum_list[i].name);
256                         }
257                 }
258                 printf("</select>");
259                 printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
260                         make_parm_name(parm->label),enum_index((int)(parm->def.ivalue),parm->enum_list));
261                 break;
262         case P_SEP:
263                 break;
264         }
265         printf("</td></tr>\n");
266 }
267
268 /****************************************************************************
269   display a set of parameters for a service 
270 ****************************************************************************/
271 static void show_parameters(int snum, int allparameters, int advanced, int printers)
272 {
273         int i = 0;
274         struct parm_struct *parm;
275         char *heading = NULL;
276         char *last_heading = NULL;
277
278         while ((parm = lp_next_parameter(snum, &i, allparameters))) {
279                 if (snum < 0 && parm->class == P_LOCAL && !(parm->flags & FLAG_GLOBAL))
280                         continue;
281                 if (parm->class == P_SEPARATOR) {
282                         heading = parm->label;
283                         continue;
284                 }
285                 if (parm->flags & FLAG_HIDE) continue;
286                 if (snum >= 0) {
287                         if (printers & !(parm->flags & FLAG_PRINT)) continue;
288                         if (!printers & !(parm->flags & FLAG_SHARE)) continue;
289                 }
290                 if (!advanced) {
291                         if (!(parm->flags & FLAG_BASIC)) {
292                                 void *ptr = parm->ptr;
293
294                                 if (parm->class == P_LOCAL && snum >= 0) {
295                                         ptr = lp_local_ptr(snum, ptr);
296                                 }
297
298                                 switch (parm->type) {
299                                 case P_CHAR:
300                                         if (*(char *)ptr == (char)(parm->def.cvalue)) continue;
301                                         break;
302
303                                 case P_LIST:
304                                         if (!lp_list_compare(*(char ***)ptr,(char **)(parm->def.lvalue))) continue;
305                                         break;
306
307                                 case P_STRING:
308                                 case P_USTRING:
309                                         if (!strcmp(*(char **)ptr,(char *)(parm->def.svalue))) continue;
310                                         break;
311
312                                 case P_GSTRING:
313                                 case P_UGSTRING:
314                                         if (!strcmp((char *)ptr,(char *)(parm->def.svalue))) continue;
315                                         break;
316
317                                 case P_BOOL:
318                                 case P_BOOLREV:
319                                         if (*(BOOL *)ptr == (BOOL)(parm->def.bvalue)) continue;
320                                         break;
321
322                                 case P_INTEGER:
323                                 case P_OCTAL:
324                                         if (*(int *)ptr == (int)(parm->def.ivalue)) continue;
325                                         break;
326
327
328                                 case P_ENUM:
329                                         if (*(int *)ptr == (int)(parm->def.ivalue)) continue;
330                                         break;
331                                 case P_SEP:
332                                         continue;
333                                 }
334                         }
335                         if (printers && !(parm->flags & FLAG_PRINT)) continue;
336                 }
337                 if (heading && heading != last_heading) {
338                         printf("<tr><td></td></tr><tr><td><b><u>%s</u></b></td></tr>\n", heading);
339                         last_heading = heading;
340                 }
341                 show_parameter(snum, parm);
342         }
343 }
344
345 /****************************************************************************
346   load the smb.conf file into loadparm.
347 ****************************************************************************/
348 static BOOL load_config(BOOL save_def)
349 {
350         lp_resetnumservices();
351         return lp_load(servicesf,False,save_def,False);
352 }
353
354 /****************************************************************************
355   write a config file 
356 ****************************************************************************/
357 static void write_config(FILE *f, BOOL show_defaults, char *(*dos_to_ext)(char *, BOOL))
358 {
359         fprintf(f, "# Samba config file created using SWAT\n");
360         fprintf(f, "# from %s (%s)\n", cgi_remote_host(), cgi_remote_addr());
361         fprintf(f, "# Date: %s\n\n", timestring(False));
362         
363         lp_dump(f, show_defaults, iNumNonAutoPrintServices, dos_to_ext);        
364 }
365
366 /****************************************************************************
367   save and reoad the smb.conf config file 
368 ****************************************************************************/
369 static int save_reload(int snum)
370 {
371         FILE *f;
372         struct stat st;
373
374         f = sys_fopen(servicesf,"w");
375         if (!f) {
376                 printf("failed to open %s for writing\n", servicesf);
377                 return 0;
378         }
379
380         /* just in case they have used the buggy xinetd to create the file */
381         if (fstat(fileno(f), &st) == 0 &&
382             (st.st_mode & S_IWOTH)) {
383                 fchmod(fileno(f), S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
384         }
385
386         write_config(f, False, _dos_to_unix);
387         if (snum)
388                 lp_dump_one(f, False, snum, _dos_to_unix);
389         fclose(f);
390
391         lp_killunused(NULL);
392
393         if (!load_config(False)) {
394                 printf("Can't reload %s\n", servicesf);
395                 return 0;
396         }
397         iNumNonAutoPrintServices = lp_numservices();
398         load_printers();
399
400         return 1;
401 }
402
403 /****************************************************************************
404   commit one parameter 
405 ****************************************************************************/
406 static void commit_parameter(int snum, struct parm_struct *parm, char *v)
407 {
408         int i;
409         char *s;
410
411         /* lp_do_parameter() will do unix_to_dos(v). */
412         if(parm->flags & FLAG_DOS_STRING)
413                 dos_to_unix(v, True);
414
415         if (snum < 0 && parm->class == P_LOCAL) {
416                 /* this handles the case where we are changing a local
417                    variable globally. We need to change the parameter in 
418                    all shares where it is currently set to the default */
419                 for (i=0;i<lp_numservices();i++) {
420                         s = lp_servicename(i);
421                         if (s && (*s) && lp_is_default(i, parm)) {
422                                 lp_do_parameter(i, parm->label, v);
423                         }
424                 }
425         }
426
427         lp_do_parameter(snum, parm->label, v);
428 }
429
430 /****************************************************************************
431   commit a set of parameters for a service 
432 ****************************************************************************/
433 static void commit_parameters(int snum)
434 {
435         int i = 0;
436         struct parm_struct *parm;
437         pstring label;
438         char *v;
439
440         while ((parm = lp_next_parameter(snum, &i, 1))) {
441                 slprintf(label, sizeof(label)-1, "parm_%s", make_parm_name(parm->label));
442                 if ((v = cgi_variable(label))) {
443                         if (parm->flags & FLAG_HIDE) continue;
444                         commit_parameter(snum, parm, v); 
445                 }
446         }
447 }
448
449 /****************************************************************************
450   spit out the html for a link with an image 
451 ****************************************************************************/
452 static void image_link(char *name,char *hlink, char *src)
453 {
454         printf("<A HREF=\"%s/%s\"><img border=\"0\" src=\"/swat/%s\" alt=\"%s\"></A>\n", 
455                cgi_baseurl(), hlink, src, name);
456 }
457
458 /****************************************************************************
459   display the main navigation controls at the top of each page along
460   with a title 
461 ****************************************************************************/
462 static void show_main_buttons(void)
463 {
464         char *p;
465         
466         if ((p = cgi_user_name()) && strcmp(p, "root")) {
467                 printf("Logged in as <b>%s</b><p>\n", p);
468         }
469
470         image_link("Home", "", "images/home.gif");
471         if (have_write_access) {
472                 image_link("Globals", "globals", "images/globals.gif");
473                 image_link("Shares", "shares", "images/shares.gif");
474                 image_link("Printers", "printers", "images/printers.gif");
475         }
476         if (have_read_access) {
477                 image_link("Status", "status", "images/status.gif");
478                 image_link("View Config", "viewconfig","images/viewconfig.gif");
479         }
480         image_link("Password Management", "passwd", "images/passwd.gif");
481
482         printf("<HR>\n");
483 }
484
485 /****************************************************************************
486   display a welcome page  
487 ****************************************************************************/
488 static void welcome_page(void)
489 {
490         include_html("help/welcome.html");
491 }
492
493 /****************************************************************************
494   display the current smb.conf  
495 ****************************************************************************/
496 static void viewconfig_page(void)
497 {
498         int full_view=0;
499
500         if (cgi_variable("full_view")) {
501                 full_view = 1;
502         }
503
504         printf("<H2>Current Config</H2>\n");
505         printf("<form method=post>\n");
506
507         if (full_view) {
508                 printf("<input type=submit name=\"normal_view\" value=\"Normal View\">\n");
509         } else {
510                 printf("<input type=submit name=\"full_view\" value=\"Full View\">\n");
511         }
512
513         printf("<p><pre>");
514         write_config(stdout, full_view, _dos_to_dos);
515         printf("</pre>");
516         printf("</form>\n");
517 }
518
519 /****************************************************************************
520   display a globals editing page  
521 ****************************************************************************/
522 static void globals_page(void)
523 {
524         int advanced = 0;
525
526         printf("<H2>Global Variables</H2>\n");
527
528         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
529                 advanced = 1;
530
531         if (cgi_variable("Commit")) {
532                 commit_parameters(GLOBALS_SNUM);
533                 save_reload(0);
534         }
535
536         printf("<FORM name=\"swatform\" method=post>\n");
537
538         if (have_write_access) {
539                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
540         }
541
542         printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
543         if (advanced == 0) {
544                 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
545         } else {
546                 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
547         }
548         printf("<p>\n");
549         
550         printf("<table>\n");
551         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
552         printf("</table>\n");
553
554         if (advanced) {
555                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
556         }
557
558         printf("</FORM>\n");
559 }
560
561 /****************************************************************************
562   display a shares editing page. share is in unix codepage, and must be in
563   dos codepage. FIXME !!! JRA.
564 ****************************************************************************/
565 static void shares_page(void)
566 {
567         char *share = cgi_variable("share");
568         char *s;
569         int snum=-1;
570         int i;
571         int advanced = 0;
572
573         if (share)
574                 snum = lp_servicenumber(share);
575
576         printf("<H2>Share Parameters</H2>\n");
577
578         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
579                 advanced = 1;
580
581         if (cgi_variable("Commit") && snum >= 0) {
582                 commit_parameters(snum);
583                 save_reload(0);
584         }
585
586         if (cgi_variable("Delete") && snum >= 0) {
587                 lp_remove_service(snum);
588                 save_reload(0);
589                 share = NULL;
590                 snum = -1;
591         }
592
593         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
594                 /* add_a_service() which is called by lp_copy_service()
595                         will do unix_to_dos() conversion, so we need dos_to_unix() before the lp_copy_service(). */
596                 pstring unix_share;
597                 pstrcpy(unix_share, dos_to_unix(share, False));
598                 load_config(False);
599                 lp_copy_service(GLOBALS_SNUM, unix_share);
600                 iNumNonAutoPrintServices = lp_numservices();
601                 save_reload(0);
602                 snum = lp_servicenumber(share);
603         }
604
605         printf("<FORM name=\"swatform\" method=post>\n");
606
607         printf("<table>\n");
608         printf("<tr>\n");
609         printf("<td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
610         printf("<td><select name=share>\n");
611         if (snum < 0)
612                 printf("<option value=\" \"> \n");
613         for (i=0;i<lp_numservices();i++) {
614                 s = lp_servicename(i);
615                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
616                         printf("<option %s value=\"%s\">%s\n", 
617                                (share && strcmp(share,s)==0)?"SELECTED":"",
618                                s, s);
619                 }
620         }
621         printf("</select></td>\n");
622         if (have_write_access) {
623                 printf("<td><input type=submit name=\"Delete\" value=\"Delete Share\"></td>\n");
624         }
625         printf("</tr>\n");
626         printf("</table>");
627         printf("<table>");
628         if (have_write_access) {
629                 printf("<tr>\n");
630                 printf("<td><input type=submit name=createshare value=\"Create Share\"></td>\n");
631                 printf("<td><input type=text size=30 name=newshare></td></tr>\n");
632         }
633         printf("</table>");
634
635
636         if (snum >= 0) {
637                 if (have_write_access) {
638                         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
639                 }
640
641                 printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
642                 if (advanced == 0) {
643                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
644                 } else {
645                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
646                 }
647                 printf("<p>\n");
648         }
649
650         if (snum >= 0) {
651                 printf("<table>\n");
652                 show_parameters(snum, 1, advanced, 0);
653                 printf("</table>\n");
654         }
655
656         if (advanced) {
657                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
658         }
659
660         printf("</FORM>\n");
661 }
662
663 /*************************************************************
664 change a password either locally or remotely
665 *************************************************************/
666 static BOOL change_password(const char *remote_machine, char *user_name, 
667                             char *old_passwd, char *new_passwd, 
668                                 int local_flags)
669 {
670         BOOL ret = False;
671         pstring err_str;
672         pstring msg_str;
673
674         if (demo_mode) {
675                 printf("password change in demo mode rejected\n<p>");
676                 return False;
677         }
678         
679         if (remote_machine != NULL) {
680                 ret = remote_password_change(remote_machine, user_name, old_passwd, 
681                                                                          new_passwd, err_str, sizeof(err_str));
682                 if(*err_str)
683                         printf("%s\n<p>", err_str);
684                 return ret;
685         }
686
687         if(!initialize_password_db(True)) {
688                 printf("Can't setup password database vectors.\n<p>");
689                 return False;
690         }
691         
692         ret = local_password_change(user_name, local_flags, new_passwd, err_str, sizeof(err_str),
693                                          msg_str, sizeof(msg_str));
694
695         if(*msg_str)
696                 printf("%s\n<p>", msg_str);
697         if(*err_str)
698                 printf("%s\n<p>", err_str);
699
700         return ret;
701 }
702
703 /****************************************************************************
704   do the stuff required to add or change a password 
705 ****************************************************************************/
706 static void chg_passwd(void)
707 {
708         char *host;
709         BOOL rslt;
710         int local_flags = 0;
711
712         /* Make sure users name has been specified */
713         if (strlen(cgi_variable(SWAT_USER)) == 0) {
714                 printf("<p> Must specify \"User Name\" \n");
715                 return;
716         }
717
718         /*
719          * smbpasswd doesn't require anything but the users name to delete, disable or enable the user,
720          * so if that's what we're doing, skip the rest of the checks
721          */
722         if (!cgi_variable(DISABLE_USER_FLAG) && !cgi_variable(ENABLE_USER_FLAG) && !cgi_variable(DELETE_USER_FLAG)) {
723
724                 /*
725                  * If current user is not root, make sure old password has been specified 
726                  * If REMOTE change, even root must provide old password 
727                  */
728                 if (((!am_root()) && (strlen( cgi_variable(OLD_PSWD)) <= 0)) ||
729                     ((cgi_variable(CHG_R_PASSWD_FLAG)) &&  (strlen( cgi_variable(OLD_PSWD)) <= 0))) {
730                         printf("<p> Must specify \"Old Password\" \n");
731                         return;
732                 }
733
734                 /* If changing a users password on a remote hosts we have to know what host */
735                 if ((cgi_variable(CHG_R_PASSWD_FLAG)) && (strlen( cgi_variable(RHOST)) <= 0)) {
736                         printf("<p> Must specify \"Remote Machine\" \n");
737                         return;
738                 }
739
740                 /* Make sure new passwords have been specified */
741                 if ((strlen( cgi_variable(NEW_PSWD)) <= 0) ||
742                     (strlen( cgi_variable(NEW2_PSWD)) <= 0)) {
743                         printf("<p> Must specify \"New, and Re-typed Passwords\" \n");
744                         return;
745                 }
746
747                 /* Make sure new passwords was typed correctly twice */
748                 if (strcmp(cgi_variable(NEW_PSWD), cgi_variable(NEW2_PSWD)) != 0) {
749                         printf("<p> Re-typed password didn't match new password\n");
750                         return;
751                 }
752         }
753
754         if (cgi_variable(CHG_R_PASSWD_FLAG)) {
755                 host = cgi_variable(RHOST);
756         } else if (am_root()) {
757                 host = NULL;
758         } else {
759                 host = "127.0.0.1";
760         }
761
762         /*
763          * Set up the local flags.
764          */
765
766         local_flags |= (cgi_variable(ADD_USER_FLAG) ? LOCAL_ADD_USER : 0);
767         local_flags |= (cgi_variable(DELETE_USER_FLAG) ? LOCAL_DELETE_USER : 0);
768         local_flags |= (cgi_variable(ENABLE_USER_FLAG) ? LOCAL_ENABLE_USER : 0);
769         local_flags |= (cgi_variable(DISABLE_USER_FLAG) ? LOCAL_DISABLE_USER : 0);
770
771         rslt = change_password(host,
772                                cgi_variable(SWAT_USER),
773                                cgi_variable(OLD_PSWD), cgi_variable(NEW_PSWD),
774                                    local_flags);
775
776         if(local_flags == 0) {
777                 if (rslt == True) {
778                         printf("<p> The passwd for '%s' has been changed. \n", cgi_variable(SWAT_USER));
779                 } else {
780                         printf("<p> The passwd for '%s' has NOT been changed. \n",cgi_variable(SWAT_USER));
781                 }
782         }
783         
784         return;
785 }
786
787 /****************************************************************************
788   display a password editing page  
789 ****************************************************************************/
790 static void passwd_page(void)
791 {
792         char *new_name = cgi_user_name();
793
794         /* 
795          * After the first time through here be nice. If the user
796          * changed the User box text to another users name, remember it.
797          */
798         if (cgi_variable(SWAT_USER)) {
799                 new_name = cgi_variable(SWAT_USER);
800         } 
801
802         if (!new_name) new_name = "";
803
804         printf("<H2>Server Password Management</H2>\n");
805
806         printf("<FORM name=\"swatform\" method=post>\n");
807
808         printf("<table>\n");
809
810         /* 
811          * Create all the dialog boxes for data collection
812          */
813         printf("<tr><td> User Name : </td>\n");
814         printf("<td><input type=text size=30 name=%s value=%s></td></tr> \n", SWAT_USER, new_name);
815         if (!am_root()) {
816                 printf("<tr><td> Old Password : </td>\n");
817                 printf("<td><input type=password size=30 name=%s></td></tr> \n",OLD_PSWD);
818         }
819         printf("<tr><td> New Password : </td>\n");
820         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
821         printf("<tr><td> Re-type New Password : </td>\n");
822         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
823         printf("</table>\n");
824
825         /*
826          * Create all the control buttons for requesting action
827          */
828         printf("<input type=submit name=%s value=\"Change Password\">\n", 
829                CHG_S_PASSWD_FLAG);
830         if (demo_mode || am_root()) {
831                 printf("<input type=submit name=%s value=\"Add New User\">\n",
832                        ADD_USER_FLAG);
833                 printf("<input type=submit name=%s value=\"Delete User\">\n",
834                        DELETE_USER_FLAG);
835                 printf("<input type=submit name=%s value=\"Disable User\">\n", 
836                        DISABLE_USER_FLAG);
837                 printf("<input type=submit name=%s value=\"Enable User\">\n", 
838                        ENABLE_USER_FLAG);
839         }
840         printf("<p></FORM>\n");
841
842         /*
843          * Do some work if change, add, disable or enable was
844          * requested. It could be this is the first time through this
845          * code, so there isn't anything to do.  */
846         if ((cgi_variable(CHG_S_PASSWD_FLAG)) || (cgi_variable(ADD_USER_FLAG)) || (cgi_variable(DELETE_USER_FLAG)) ||
847             (cgi_variable(DISABLE_USER_FLAG)) || (cgi_variable(ENABLE_USER_FLAG))) {
848                 chg_passwd();           
849         }
850
851         printf("<H2>Client/Server Password Management</H2>\n");
852
853         printf("<FORM name=\"swatform\" method=post>\n");
854
855         printf("<table>\n");
856
857         /* 
858          * Create all the dialog boxes for data collection
859          */
860         printf("<tr><td> User Name : </td>\n");
861         printf("<td><input type=text size=30 name=%s value=%s></td></tr>\n",SWAT_USER, new_name);
862         printf("<tr><td> Old Password : </td>\n");
863         printf("<td><input type=password size=30 name=%s></td></tr>\n",OLD_PSWD);
864         printf("<tr><td> New Password : </td>\n");
865         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
866         printf("<tr><td> Re-type New Password : </td>\n");
867         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
868         printf("<tr><td> Remote Machine : </td>\n");
869         printf("<td><input type=text size=30 name=%s></td></tr>\n",RHOST);
870
871         printf("</table>");
872
873         /*
874          * Create all the control buttons for requesting action
875          */
876         printf("<input type=submit name=%s value=\"Change Password\">", 
877                CHG_R_PASSWD_FLAG);
878
879         printf("<p></FORM>\n");
880
881         /*
882          * Do some work if a request has been made to change the
883          * password somewhere other than the server. It could be this
884          * is the first time through this code, so there isn't
885          * anything to do.  */
886         if (cgi_variable(CHG_R_PASSWD_FLAG)) {
887                 chg_passwd();           
888         }
889
890 }
891
892 /****************************************************************************
893   display a printers editing page  
894 ****************************************************************************/
895 static void printers_page(void)
896 {
897         char *share = cgi_variable("share");
898         char *s;
899         int snum=-1;
900         int i;
901         int advanced = 0;
902
903         if (share)
904                 snum = lp_servicenumber(share);
905
906         printf("<H2>Printer Parameters</H2>\n");
907
908         printf("<H3>Important Note:</H3>\n");
909         printf("Printer names marked with [*] in the Choose Printer drop-down box ");
910         printf("are autoloaded printers from ");
911         printf("<A HREF=\"/swat/help/smb.conf.5.html#PRINTCAPNAME\" target=\"docs\">Printcap Name</A>.\n");
912         printf("Attempting to delete these printers from SWAT will have no effect.\n");
913
914         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
915                 advanced = 1;
916
917         if (cgi_variable("Commit") && snum >= 0) {
918                 commit_parameters(snum);
919                 if (snum >= iNumNonAutoPrintServices)
920                     save_reload(snum);
921                 else
922                     save_reload(0);
923         }
924
925         if (cgi_variable("Delete") && snum >= 0) {
926                 lp_remove_service(snum);
927                 save_reload(0);
928                 share = NULL;
929                 snum = -1;
930         }
931
932         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
933                 /* add_a_service() which is called by lp_copy_service()
934                         will do unix_to_dos() conversion, so we need dos_to_unix() before the lp_copy_service(). */
935                 pstring unix_share;
936                 pstrcpy(unix_share, dos_to_unix(share, False));
937                 load_config(False);
938                 lp_copy_service(GLOBALS_SNUM, unix_share);
939                 iNumNonAutoPrintServices = lp_numservices();
940                 snum = lp_servicenumber(share);
941                 lp_do_parameter(snum, "print ok", "Yes");
942                 save_reload(0);
943                 snum = lp_servicenumber(share);
944         }
945
946         printf("<FORM name=\"swatform\" method=post>\n");
947
948         printf("<table>\n");
949         printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
950         printf("<td><select name=share>\n");
951         if (snum < 0 || !lp_print_ok(snum))
952                 printf("<option value=\" \"> \n");
953         for (i=0;i<lp_numservices();i++) {
954                 s = lp_servicename(i);
955                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
956                     if (i >= iNumNonAutoPrintServices)
957                         printf("<option %s value=\"%s\">[*]%s\n",
958                                (share && strcmp(share,s)==0)?"SELECTED":"",
959                                s, s);
960                     else
961                         printf("<option %s value=\"%s\">%s\n", 
962                                (share && strcmp(share,s)==0)?"SELECTED":"",
963                                s, s);
964                 }
965         }
966         printf("</select></td>");
967         if (have_write_access) {
968                 printf("<td><input type=submit name=\"Delete\" value=\"Delete Printer\"></td>\n");
969         }
970         printf("</tr>");
971         printf("</table>\n");
972
973         if (have_write_access) {
974                 printf("<table>\n");
975                 printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
976                 printf("<td><input type=text size=30 name=newshare></td></tr>\n");
977                 printf("</table>");
978         }
979
980
981         if (snum >= 0) {
982                 if (have_write_access) {
983                         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
984                 }
985                 printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
986                 if (advanced == 0) {
987                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
988                 } else {
989                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
990                 }
991                 printf("<p>\n");
992         }
993
994         if (snum >= 0) {
995                 printf("<table>\n");
996                 show_parameters(snum, 1, advanced, 1);
997                 printf("</table>\n");
998         }
999
1000         if (advanced) {
1001                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
1002         }
1003
1004         printf("</FORM>\n");
1005 }
1006
1007 /****************************************************************************
1008   MAIN()
1009 ****************************************************************************/
1010  int main(int argc, char *argv[])
1011 {
1012         extern char *optarg;
1013         extern int optind;
1014         extern FILE *dbf;
1015         int opt;
1016         char *page;
1017
1018         fault_setup(NULL);
1019         umask(S_IWGRP | S_IWOTH);
1020
1021 #if defined(HAVE_SET_AUTH_PARAMETERS)
1022         set_auth_parameters(argc, argv);
1023 #endif /* HAVE_SET_AUTH_PARAMETERS */
1024
1025         /* just in case it goes wild ... */
1026         alarm(300);
1027
1028         /* we don't want any SIGPIPE messages */
1029         BlockSignals(True,SIGPIPE);
1030
1031         dbf = sys_fopen("/dev/null", "w");
1032         if (!dbf) dbf = stderr;
1033
1034         /* we don't want stderr screwing us up */
1035         close(2);
1036         open("/dev/null", O_WRONLY);
1037
1038         while ((opt = getopt(argc, argv,"s:a")) != EOF) {
1039                 switch (opt) {
1040                 case 's':
1041                         pstrcpy(servicesf,optarg);
1042                         break;    
1043                 case 'a':
1044                         demo_mode = True;
1045                         break;    
1046                 }
1047         }
1048
1049         setup_logging(argv[0],False);
1050         charset_initialise();
1051         load_config(True);
1052         iNumNonAutoPrintServices = lp_numservices();
1053         load_printers();
1054
1055         cgi_setup(SWATDIR, !demo_mode);
1056
1057         print_header();
1058         
1059         cgi_load_variables(NULL);
1060
1061         if (!file_exist(servicesf, NULL)) {
1062                 have_read_access = True;
1063                 have_write_access = True;
1064         } else {
1065                 /* check if the authenticated user has write access - if not then
1066                    don't show write options */
1067                 have_write_access = (access(servicesf,W_OK) == 0);
1068
1069                 /* if the user doesn't have read access to smb.conf then
1070                    don't let them view it */
1071                 have_read_access = (access(servicesf,R_OK) == 0);
1072         }
1073
1074
1075         show_main_buttons();
1076
1077         page = cgi_pathinfo();
1078
1079         /* Root gets full functionality */
1080         if (have_read_access && strcmp(page, "globals")==0) {
1081                 globals_page();
1082         } else if (have_read_access && strcmp(page,"shares")==0) {
1083                 shares_page();
1084         } else if (have_read_access && strcmp(page,"printers")==0) {
1085                 printers_page();
1086         } else if (have_read_access && strcmp(page,"status")==0) {
1087                 status_page();
1088         } else if (have_read_access && strcmp(page,"viewconfig")==0) {
1089                 viewconfig_page();
1090         } else if (strcmp(page,"passwd")==0) {
1091                 passwd_page();
1092         } else {
1093                 welcome_page();
1094         }
1095
1096         print_footer();
1097         return 0;
1098 }