Make remote_password_change return malloced error strings
[samba.git] / source3 / web / swat.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba Web Administration Tool
4    Version 3.0.0
5    Copyright (C) Andrew Tridgell 1997-2002
6    Copyright (C) John H Terpstra 2002
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /**
23  * @defgroup swat SWAT - Samba Web Administration Tool
24  * @{ 
25  * @file swat.c
26  *
27  * @brief Samba Web Administration Tool.
28  **/
29
30 #include "includes.h"
31 #include "web/swat_proto.h"
32
33 static int demo_mode = False;
34 static int passwd_only = False;
35 static bool have_write_access = False;
36 static bool have_read_access = False;
37 static int iNumNonAutoPrintServices = 0;
38
39 /*
40  * Password Management Globals
41  */
42 #define SWAT_USER "username"
43 #define OLD_PSWD "old_passwd"
44 #define NEW_PSWD "new_passwd"
45 #define NEW2_PSWD "new2_passwd"
46 #define CHG_S_PASSWD_FLAG "chg_s_passwd_flag"
47 #define CHG_R_PASSWD_FLAG "chg_r_passwd_flag"
48 #define ADD_USER_FLAG "add_user_flag"
49 #define DELETE_USER_FLAG "delete_user_flag"
50 #define DISABLE_USER_FLAG "disable_user_flag"
51 #define ENABLE_USER_FLAG "enable_user_flag"
52 #define RHOST "remote_host"
53
54 #define _(x) lang_msg_rotate(x)
55
56 /****************************************************************************
57 ****************************************************************************/
58 static int enum_index(int value, const 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(const 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 *fix_quotes(const char *str)
81 {
82         static pstring newstring;
83         char *p = newstring;
84         size_t newstring_len = sizeof(newstring);
85         int quote_len = strlen("&quot;");
86
87         while (*str) {
88                 if ( *str == '\"' && (newstring_len - PTR_DIFF(p, newstring) - 1) > quote_len ) {
89                         strncpy( p, "&quot;", quote_len); 
90                         p += quote_len;
91                 } else {
92                         *p++ = *str;
93                 }
94                 ++str;
95         }
96         *p = '\0';
97         return newstring;
98 }
99
100 static char *stripspaceupper(const char *str)
101 {
102         static char newstring[1024];
103         char *p = newstring;
104
105         while (*str) {
106                 if (*str != ' ') *p++ = toupper_ascii(*str);
107                 ++str;
108         }
109         *p = '\0';
110         return newstring;
111 }
112
113 static char *make_parm_name(const char *label)
114 {
115         static char parmname[1024];
116         char *p = parmname;
117
118         while (*label) {
119                 if (*label == ' ') *p++ = '_';
120                 else *p++ = *label;
121                 ++label;
122         }
123         *p = '\0';
124         return parmname;
125 }
126
127 /****************************************************************************
128   include a lump of html in a page 
129 ****************************************************************************/
130 static int include_html(const char *fname)
131 {
132         int fd;
133         char buf[1024];
134         int ret;
135
136         fd = web_open(fname, O_RDONLY, 0);
137
138         if (fd == -1) {
139                 printf(_("ERROR: Can't open %s"), fname);
140                 printf("\n");
141                 return 0;
142         }
143
144         while ((ret = read(fd, buf, sizeof(buf))) > 0) {
145                 write(1, buf, ret);
146         }
147
148         close(fd);
149         return 1;
150 }
151
152 /****************************************************************************
153   start the page with standard stuff 
154 ****************************************************************************/
155 static void print_header(void)
156 {
157         if (!cgi_waspost()) {
158                 printf("Expires: 0\r\n");
159         }
160         printf("Content-type: text/html\r\n\r\n");
161
162         if (!include_html("include/header.html")) {
163                 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
164                 printf("<HTML>\n<HEAD>\n<TITLE>Samba Web Administration Tool</TITLE>\n</HEAD>\n<BODY background=\"/swat/images/background.jpg\">\n\n");
165         }
166 }
167
168 /* *******************************************************************
169    show parameter label with translated name in the following form
170    because showing original and translated label in one line looks
171    too long, and showing translated label only is unusable for
172    heavy users.
173    -------------------------------
174    HELP       security   [combo box][button]
175    SECURITY
176    -------------------------------
177    (capital words are translated by gettext.)
178    if no translation is available, then same form as original is
179    used.
180    "i18n_translated_parm" class is used to change the color of the
181    translated parameter with CSS.
182    **************************************************************** */
183 static const char* get_parm_translated(
184         const char* pAnchor, const char* pHelp, const char* pLabel)
185 {
186         const char* pTranslated = _(pLabel);
187         static pstring output;
188         if(strcmp(pLabel, pTranslated) != 0)
189         {
190                 pstr_sprintf(output,
191                   "<A HREF=\"/swat/help/manpages/smb.conf.5.html#%s\" target=\"docs\"> %s</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s <br><span class=\"i18n_translated_parm\">%s</span>",
192                    pAnchor, pHelp, pLabel, pTranslated);
193                 return output;
194         }
195         pstr_sprintf(output, 
196           "<A HREF=\"/swat/help/manpages/smb.conf.5.html#%s\" target=\"docs\"> %s</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s",
197           pAnchor, pHelp, pLabel);
198         return output;
199 }
200 /****************************************************************************
201  finish off the page 
202 ****************************************************************************/
203 static void print_footer(void)
204 {
205         if (!include_html("include/footer.html")) {
206                 printf("\n</BODY>\n</HTML>\n");
207         }
208 }
209
210 /****************************************************************************
211   display one editable parameter in a form 
212 ****************************************************************************/
213 static void show_parameter(int snum, struct parm_struct *parm)
214 {
215         int i;
216         void *ptr = parm->ptr;
217         char *utf8_s1, *utf8_s2;
218
219         if (parm->p_class == P_LOCAL && snum >= 0) {
220                 ptr = lp_local_ptr(snum, ptr);
221         }
222
223         printf("<tr><td>%s</td><td>", get_parm_translated(stripspaceupper(parm->label), _("Help"), parm->label));
224         switch (parm->type) {
225         case P_CHAR:
226                 printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
227                        make_parm_name(parm->label), *(char *)ptr);
228                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%c\'\">",
229                         _("Set Default"), make_parm_name(parm->label),(char)(parm->def.cvalue));
230                 break;
231
232         case P_LIST:
233                 printf("<input type=text size=40 name=\"parm_%s\" value=\"",
234                         make_parm_name(parm->label));
235                 if ((char ***)ptr && *(char ***)ptr && **(char ***)ptr) {
236                         char **list = *(char ***)ptr;
237                         for (;*list;list++) {
238                                 /* enclose in HTML encoded quotes if the string contains a space */
239                                 if ( strchr_m(*list, ' ') ) {
240                                         push_utf8_allocate(&utf8_s1, *list);
241                                         push_utf8_allocate(&utf8_s2, ((*(list+1))?", ":""));
242                                         printf("&quot;%s&quot;%s", utf8_s1, utf8_s2);
243                                 } else {
244                                         push_utf8_allocate(&utf8_s1, *list);
245                                         push_utf8_allocate(&utf8_s2, ((*(list+1))?", ":""));
246                                         printf("%s%s", utf8_s1, utf8_s2);
247                                 }
248                                 SAFE_FREE(utf8_s1);
249                                 SAFE_FREE(utf8_s2);
250                         }
251                 }
252                 printf("\">");
253                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'",
254                         _("Set Default"), make_parm_name(parm->label));
255                 if (parm->def.lvalue) {
256                         char **list = (char **)(parm->def.lvalue);
257                         for (; *list; list++) {
258                                 /* enclose in HTML encoded quotes if the string contains a space */
259                                 if ( strchr_m(*list, ' ') ) 
260                                         printf("&quot;%s&quot;%s", *list, ((*(list+1))?", ":""));
261                                 else
262                                         printf("%s%s", *list, ((*(list+1))?", ":""));
263                         }
264                 }
265                 printf("\'\">");
266                 break;
267
268         case P_STRING:
269         case P_USTRING:
270                 push_utf8_allocate(&utf8_s1, *(char **)ptr);
271                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
272                        make_parm_name(parm->label), fix_quotes(utf8_s1));
273                 SAFE_FREE(utf8_s1);
274                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
275                         _("Set Default"), make_parm_name(parm->label),fix_backslash((char *)(parm->def.svalue)));
276                 break;
277
278         case P_GSTRING:
279         case P_UGSTRING:
280                 push_utf8_allocate(&utf8_s1, (char *)ptr);
281                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
282                        make_parm_name(parm->label), fix_quotes(utf8_s1));
283                 SAFE_FREE(utf8_s1);
284                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
285                         _("Set Default"), make_parm_name(parm->label),fix_backslash((char *)(parm->def.svalue)));
286                 break;
287
288         case P_BOOL:
289                 printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
290                 printf("<option %s>Yes", (*(bool *)ptr)?"selected":"");
291                 printf("<option %s>No", (*(bool *)ptr)?"":"selected");
292                 printf("</select>");
293                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
294                         _("Set Default"), make_parm_name(parm->label),(bool)(parm->def.bvalue)?0:1);
295                 break;
296
297         case P_BOOLREV:
298                 printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
299                 printf("<option %s>Yes", (*(bool *)ptr)?"":"selected");
300                 printf("<option %s>No", (*(bool *)ptr)?"selected":"");
301                 printf("</select>");
302                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
303                         _("Set Default"), make_parm_name(parm->label),(bool)(parm->def.bvalue)?1:0);
304                 break;
305
306         case P_INTEGER:
307                 printf("<input type=text size=8 name=\"parm_%s\" value=\"%d\">", make_parm_name(parm->label), *(int *)ptr);
308                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%d\'\">",
309                         _("Set Default"), make_parm_name(parm->label),(int)(parm->def.ivalue));
310                 break;
311
312         case P_OCTAL:
313                 printf("<input type=text size=8 name=\"parm_%s\" value=%s>", make_parm_name(parm->label), octal_string(*(int *)ptr));
314                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
315                        _("Set Default"), make_parm_name(parm->label),
316                        octal_string((int)(parm->def.ivalue)));
317                 break;
318
319         case P_ENUM:
320                 printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
321                 for (i=0;parm->enum_list[i].name;i++) {
322                         if (i == 0 || parm->enum_list[i].value != parm->enum_list[i-1].value) {
323                                 printf("<option %s>%s",(*(int *)ptr)==parm->enum_list[i].value?"selected":"",parm->enum_list[i].name);
324                         }
325                 }
326                 printf("</select>");
327                 printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
328                         _("Set Default"), make_parm_name(parm->label),enum_index((int)(parm->def.ivalue),parm->enum_list));
329                 break;
330         case P_SEP:
331                 break;
332         }
333         printf("</td></tr>\n");
334 }
335
336 /****************************************************************************
337   display a set of parameters for a service 
338 ****************************************************************************/
339 static void show_parameters(int snum, int allparameters, unsigned int parm_filter, int printers)
340 {
341         int i = 0;
342         struct parm_struct *parm;
343         const char *heading = NULL;
344         const char *last_heading = NULL;
345
346         while ((parm = lp_next_parameter(snum, &i, allparameters))) {
347                 if (snum < 0 && parm->p_class == P_LOCAL && !(parm->flags & FLAG_GLOBAL))
348                         continue;
349                 if (parm->p_class == P_SEPARATOR) {
350                         heading = parm->label;
351                         continue;
352                 }
353                 if (parm->flags & FLAG_HIDE) continue;
354                 if (snum >= 0) {
355                         if (printers & !(parm->flags & FLAG_PRINT)) continue;
356                         if (!printers & !(parm->flags & FLAG_SHARE)) continue;
357                 }
358
359                 if (!( parm_filter & FLAG_ADVANCED )) {
360                         if (!(parm->flags & FLAG_BASIC)) {
361                                         void *ptr = parm->ptr;
362
363                                 if (parm->p_class == P_LOCAL && snum >= 0) {
364                                         ptr = lp_local_ptr(snum, ptr);
365                                 }
366
367                                 switch (parm->type) {
368                                 case P_CHAR:
369                                         if (*(char *)ptr == (char)(parm->def.cvalue)) continue;
370                                         break;
371
372                                 case P_LIST:
373                                         if (!str_list_compare(*(char ***)ptr, (char **)(parm->def.lvalue))) continue;
374                                         break;
375
376                                 case P_STRING:
377                                 case P_USTRING:
378                                         if (!strcmp(*(char **)ptr,(char *)(parm->def.svalue))) continue;
379                                         break;
380
381                                 case P_GSTRING:
382                                 case P_UGSTRING:
383                                         if (!strcmp((char *)ptr,(char *)(parm->def.svalue))) continue;
384                                         break;
385
386                                 case P_BOOL:
387                                 case P_BOOLREV:
388                                         if (*(bool *)ptr == (bool)(parm->def.bvalue)) continue;
389                                         break;
390
391                                 case P_INTEGER:
392                                 case P_OCTAL:
393                                         if (*(int *)ptr == (int)(parm->def.ivalue)) continue;
394                                         break;
395
396
397                                 case P_ENUM:
398                                         if (*(int *)ptr == (int)(parm->def.ivalue)) continue;
399                                         break;
400                                 case P_SEP:
401                                         continue;
402                                         }
403                         }
404                         if (printers && !(parm->flags & FLAG_PRINT)) continue;
405                 }
406
407                 if ((parm_filter & FLAG_WIZARD) && !(parm->flags & FLAG_WIZARD)) continue;
408                 
409                 if ((parm_filter & FLAG_ADVANCED) && !(parm->flags & FLAG_ADVANCED)) continue;
410                 
411                 if (heading && heading != last_heading) {
412                         printf("<tr><td></td></tr><tr><td><b><u>%s</u></b></td></tr>\n", _(heading));
413                         last_heading = heading;
414                 }
415                 show_parameter(snum, parm);
416         }
417 }
418
419 /****************************************************************************
420   load the smb.conf file into loadparm.
421 ****************************************************************************/
422 static bool load_config(bool save_def)
423 {
424         lp_resetnumservices();
425         return lp_load(dyn_CONFIGFILE,False,save_def,False,True);
426 }
427
428 /****************************************************************************
429   write a config file 
430 ****************************************************************************/
431 static void write_config(FILE *f, bool show_defaults)
432 {
433         fprintf(f, "# Samba config file created using SWAT\n");
434         fprintf(f, "# from %s (%s)\n", cgi_remote_host(), cgi_remote_addr());
435         fprintf(f, "# Date: %s\n\n", current_timestring(False));
436         
437         lp_dump(f, show_defaults, iNumNonAutoPrintServices);
438 }
439
440 /****************************************************************************
441   save and reload the smb.conf config file 
442 ****************************************************************************/
443 static int save_reload(int snum)
444 {
445         FILE *f;
446         struct stat st;
447
448         f = sys_fopen(dyn_CONFIGFILE,"w");
449         if (!f) {
450                 printf(_("failed to open %s for writing"), dyn_CONFIGFILE);
451                 printf("\n");
452                 return 0;
453         }
454
455         /* just in case they have used the buggy xinetd to create the file */
456         if (fstat(fileno(f), &st) == 0 &&
457             (st.st_mode & S_IWOTH)) {
458 #if defined HAVE_FCHMOD
459                 fchmod(fileno(f), S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
460 #else
461                 chmod(dyn_CONFIGFILE, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
462 #endif
463         }
464
465         write_config(f, False);
466         if (snum)
467                 lp_dump_one(f, False, snum);
468         fclose(f);
469
470         lp_killunused(NULL);
471
472         if (!load_config(False)) {
473                 printf(_("Can't reload %s"), dyn_CONFIGFILE);
474                 printf("\n");
475                 return 0;
476         }
477         iNumNonAutoPrintServices = lp_numservices();
478         load_printers();
479
480         return 1;
481 }
482
483 /****************************************************************************
484   commit one parameter 
485 ****************************************************************************/
486 static void commit_parameter(int snum, struct parm_struct *parm, const char *v)
487 {
488         int i;
489         char *s;
490
491         if (snum < 0 && parm->p_class == P_LOCAL) {
492                 /* this handles the case where we are changing a local
493                    variable globally. We need to change the parameter in 
494                    all shares where it is currently set to the default */
495                 for (i=0;i<lp_numservices();i++) {
496                         s = lp_servicename(i);
497                         if (s && (*s) && lp_is_default(i, parm)) {
498                                 lp_do_parameter(i, parm->label, v);
499                         }
500                 }
501         }
502
503         lp_do_parameter(snum, parm->label, v);
504 }
505
506 /****************************************************************************
507   commit a set of parameters for a service 
508 ****************************************************************************/
509 static void commit_parameters(int snum)
510 {
511         int i = 0;
512         struct parm_struct *parm;
513         pstring label;
514         const char *v;
515
516         while ((parm = lp_next_parameter(snum, &i, 1))) {
517                 slprintf(label, sizeof(label)-1, "parm_%s", make_parm_name(parm->label));
518                 if ((v = cgi_variable(label)) != NULL) {
519                         if (parm->flags & FLAG_HIDE) continue;
520                         commit_parameter(snum, parm, v); 
521                 }
522         }
523 }
524
525 /****************************************************************************
526   spit out the html for a link with an image 
527 ****************************************************************************/
528 static void image_link(const char *name, const char *hlink, const char *src)
529 {
530         printf("<A HREF=\"%s/%s\"><img border=\"0\" src=\"/swat/%s\" alt=\"%s\"></A>\n", 
531                cgi_baseurl(), hlink, src, name);
532 }
533
534 /****************************************************************************
535   display the main navigation controls at the top of each page along
536   with a title 
537 ****************************************************************************/
538 static void show_main_buttons(void)
539 {
540         char *p;
541         
542         if ((p = cgi_user_name()) && strcmp(p, "root")) {
543                 printf(_("Logged in as <b>%s</b>"), p);
544                 printf("<p>\n");
545         }
546
547         image_link(_("Home"), "", "images/home.gif");
548         if (have_write_access) {
549                 image_link(_("Globals"), "globals", "images/globals.gif");
550                 image_link(_("Shares"), "shares", "images/shares.gif");
551                 image_link(_("Printers"), "printers", "images/printers.gif");
552                 image_link(_("Wizard"), "wizard", "images/wizard.gif");
553         }
554    /* root always gets all buttons, otherwise look for -P */
555         if ( have_write_access || (!passwd_only && have_read_access) ) {
556                 image_link(_("Status"), "status", "images/status.gif");
557                 image_link(_("View Config"), "viewconfig", "images/viewconfig.gif");
558         }
559         image_link(_("Password Management"), "passwd", "images/passwd.gif");
560
561         printf("<HR>\n");
562 }
563
564 /****************************************************************************
565  * Handle Display/Edit Mode CGI
566  ****************************************************************************/
567 static void ViewModeBoxes(int mode)
568 {
569         printf("<p>%s:&nbsp;\n", _("Current View Is"));
570         printf("<input type=radio name=\"ViewMode\" value=0 %s>%s\n", ((mode == 0) ? "checked" : ""), _("Basic"));
571         printf("<input type=radio name=\"ViewMode\" value=1 %s>%s\n", ((mode == 1) ? "checked" : ""), _("Advanced"));
572         printf("<br>%s:&nbsp;\n", _("Change View To"));
573         printf("<input type=submit name=\"BasicMode\" value=\"%s\">\n", _("Basic"));
574         printf("<input type=submit name=\"AdvMode\" value=\"%s\">\n", _("Advanced"));
575         printf("</p><br>\n");
576 }
577
578 /****************************************************************************
579   display a welcome page  
580 ****************************************************************************/
581 static void welcome_page(void)
582 {
583         if (file_exist("help/welcome.html", NULL)) {
584                 include_html("help/welcome.html");
585         } else {
586                 include_html("help/welcome-no-samba-doc.html");
587         }
588 }
589
590 /****************************************************************************
591   display the current smb.conf  
592 ****************************************************************************/
593 static void viewconfig_page(void)
594 {
595         int full_view=0;
596
597         if (cgi_variable("full_view")) {
598                 full_view = 1;
599         }
600
601         printf("<H2>%s</H2>\n", _("Current Config"));
602         printf("<form method=post>\n");
603
604         if (full_view) {
605                 printf("<input type=submit name=\"normal_view\" value=\"%s\">\n", _("Normal View"));
606         } else {
607                 printf("<input type=submit name=\"full_view\" value=\"%s\">\n", _("Full View"));
608         }
609
610         printf("<p><pre>");
611         write_config(stdout, full_view);
612         printf("</pre>");
613         printf("</form>\n");
614 }
615
616 /****************************************************************************
617   second screen of the wizard ... Fetch Configuration Parameters
618 ****************************************************************************/
619 static void wizard_params_page(void)
620 {
621         unsigned int parm_filter = FLAG_WIZARD;
622
623         /* Here we first set and commit all the parameters that were selected
624            in the previous screen. */
625
626         printf("<H2>%s</H2>\n", _("Wizard Parameter Edit Page"));
627
628         if (cgi_variable("Commit")) {
629                 commit_parameters(GLOBAL_SECTION_SNUM);
630                 save_reload(0);
631         }
632
633         printf("<form name=\"swatform\" method=post action=wizard_params>\n");
634
635         if (have_write_access) {
636                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
637         }
638
639         printf("<input type=reset name=\"Reset Values\" value=\"Reset\">\n");
640         printf("<p>\n");
641         
642         printf("<table>\n");
643         show_parameters(GLOBAL_SECTION_SNUM, 1, parm_filter, 0);
644         printf("</table>\n");
645         printf("</form>\n");
646 }
647
648 /****************************************************************************
649   Utility to just rewrite the smb.conf file - effectively just cleans it up
650 ****************************************************************************/
651 static void rewritecfg_file(void)
652 {
653         commit_parameters(GLOBAL_SECTION_SNUM);
654         save_reload(0);
655         printf("<H2>%s</H2>\n", _("Note: smb.conf file has been read and rewritten"));
656 }
657
658 /****************************************************************************
659   wizard to create/modify the smb.conf file
660 ****************************************************************************/
661 static void wizard_page(void)
662 {
663         /* Set some variables to collect data from smb.conf */
664         int role = 0;
665         int winstype = 0;
666         int have_home = -1;
667         int HomeExpo = 0;
668         int SerType = 0;
669
670         if (cgi_variable("Rewrite")) {
671                 (void) rewritecfg_file();
672                 return;
673         }
674
675         if (cgi_variable("GetWizardParams")){
676                 (void) wizard_params_page();
677                 return;
678         }
679
680         if (cgi_variable("Commit")){
681                 SerType = atoi(cgi_variable_nonull("ServerType"));
682                 winstype = atoi(cgi_variable_nonull("WINSType"));
683                 have_home = lp_servicenumber(HOMES_NAME);
684                 HomeExpo = atoi(cgi_variable_nonull("HomeExpo"));
685
686                 /* Plain text passwords are too badly broken - use encrypted passwords only */
687                 lp_do_parameter( GLOBAL_SECTION_SNUM, "encrypt passwords", "Yes");
688                 
689                 switch ( SerType ){
690                         case 0:
691                                 /* Stand-alone Server */
692                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "security", "USER" );
693                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "domain logons", "No" );
694                                 break;
695                         case 1:
696                                 /* Domain Member */
697                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "security", "DOMAIN" );
698                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "domain logons", "No" );
699                                 break;
700                         case 2:
701                                 /* Domain Controller */
702                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "security", "USER" );
703                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "domain logons", "Yes" );
704                                 break;
705                 }
706                 switch ( winstype ) {
707                         case 0:
708                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "wins support", "No" );
709                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "wins server", "" );
710                                 break;
711                         case 1:
712                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "wins support", "Yes" );
713                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "wins server", "" );
714                                 break;
715                         case 2:
716                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "wins support", "No" );
717                                 lp_do_parameter( GLOBAL_SECTION_SNUM, "wins server", cgi_variable_nonull("WINSAddr"));
718                                 break;
719                 }
720
721                 /* Have to create Homes share? */
722                 if ((HomeExpo == 1) && (have_home == -1)) {
723                         pstring unix_share;
724                         
725                         pstrcpy(unix_share,HOMES_NAME);
726                         load_config(False);
727                         lp_copy_service(GLOBAL_SECTION_SNUM, unix_share);
728                         iNumNonAutoPrintServices = lp_numservices();
729                         have_home = lp_servicenumber(HOMES_NAME);
730                         lp_do_parameter( have_home, "read only", "No");
731                         lp_do_parameter( have_home, "valid users", "%S");
732                         lp_do_parameter( have_home, "browseable", "No");
733                         commit_parameters(have_home);
734                 }
735
736                 /* Need to Delete Homes share? */
737                 if ((HomeExpo == 0) && (have_home != -1)) {
738                         lp_remove_service(have_home);
739                         have_home = -1;
740                 }
741
742                 commit_parameters(GLOBAL_SECTION_SNUM);
743                 save_reload(0);
744         }
745         else
746         {
747                 /* Now determine smb.conf WINS settings */
748                 if (lp_wins_support())
749                         winstype = 1;
750                 if (lp_wins_server_list() && strlen(*lp_wins_server_list()))
751                         winstype = 2;
752                 
753
754                 /* Do we have a homes share? */
755                 have_home = lp_servicenumber(HOMES_NAME);
756         }
757         if ((winstype == 2) && lp_wins_support())
758                 winstype = 3;
759
760         role = lp_server_role();
761         
762         /* Here we go ... */
763         printf("<H2>%s</H2>\n", _("Samba Configuration Wizard"));
764         printf("<form method=post action=wizard>\n");
765
766         if (have_write_access) {
767                 printf("%s\n", _("The \"Rewrite smb.conf file\" button will clear the smb.conf file of all default values and of comments."));
768                 printf("%s", _("The same will happen if you press the commit button."));
769                 printf("<br><br>\n");
770                 printf("<center>");
771                 printf("<input type=submit name=\"Rewrite\" value=\"%s\"> &nbsp;&nbsp;",_("Rewrite smb.conf file"));
772                 printf("<input type=submit name=\"Commit\" value=\"%s\"> &nbsp;&nbsp;",_("Commit"));
773                 printf("<input type=submit name=\"GetWizardParams\" value=\"%s\">", _("Edit Parameter Values"));
774                 printf("</center>\n");
775         }
776
777         printf("<hr>");
778         printf("<center><table border=0>");
779         printf("<tr><td><b>%s:&nbsp;</b></td>\n", _("Server Type"));
780         printf("<td><input type=radio name=\"ServerType\" value=\"0\" %s> %s&nbsp;</td>", ((role == ROLE_STANDALONE) ? "checked" : ""), _("Stand Alone"));
781         printf("<td><input type=radio name=\"ServerType\" value=\"1\" %s> %s&nbsp;</td>", ((role == ROLE_DOMAIN_MEMBER) ? "checked" : ""), _("Domain Member")); 
782         printf("<td><input type=radio name=\"ServerType\" value=\"2\" %s> %s&nbsp;</td>", ((role == ROLE_DOMAIN_PDC) ? "checked" : ""), _("Domain Controller"));
783         printf("</tr>\n");
784         if (role == ROLE_DOMAIN_BDC) {
785                 printf("<tr><td></td><td colspan=3><font color=\"#ff0000\">%s</font></td></tr>\n", _("Unusual Type in smb.conf - Please Select New Mode"));
786         }
787         printf("<tr><td><b>%s:&nbsp;</b></td>\n", _("Configure WINS As"));
788         printf("<td><input type=radio name=\"WINSType\" value=\"0\" %s> %s&nbsp;</td>", ((winstype == 0) ? "checked" : ""), _("Not Used"));
789         printf("<td><input type=radio name=\"WINSType\" value=\"1\" %s> %s&nbsp;</td>", ((winstype == 1) ? "checked" : ""), _("Server for client use"));
790         printf("<td><input type=radio name=\"WINSType\" value=\"2\" %s> %s&nbsp;</td>", ((winstype == 2) ? "checked" : ""), _("Client of another WINS server"));
791         printf("</tr>\n");
792         printf("<tr><td></td><td></td><td></td><td>%s&nbsp;<input type=text size=\"16\" name=\"WINSAddr\" value=\"", _("Remote WINS Server"));
793
794         /* Print out the list of wins servers */
795         if(lp_wins_server_list()) {
796                 int i;
797                 const char **wins_servers = lp_wins_server_list();
798                 for(i = 0; wins_servers[i]; i++) printf("%s ", wins_servers[i]);
799         }
800         
801         printf("\"></td></tr>\n");
802         if (winstype == 3) {
803                 printf("<tr><td></td><td colspan=3><font color=\"#ff0000\">%s</font></td></tr>\n", _("Error: WINS Server Mode and WINS Support both set in smb.conf"));
804                 printf("<tr><td></td><td colspan=3><font color=\"#ff0000\">%s</font></td></tr>\n", _("Please Select desired WINS mode above."));
805         }
806         printf("<tr><td><b>%s:&nbsp;</b></td>\n", _("Expose Home Directories"));
807         printf("<td><input type=radio name=\"HomeExpo\" value=\"1\" %s> Yes</td>", (have_home == -1) ? "" : "checked ");
808         printf("<td><input type=radio name=\"HomeExpo\" value=\"0\" %s> No</td>", (have_home == -1 ) ? "checked" : "");
809         printf("<td></td></tr>\n");
810         
811         /* Enable this when we are ready ....
812          * printf("<tr><td><b>%s:&nbsp;</b></td>\n", _("Is Print Server"));
813          * printf("<td><input type=radio name=\"PtrSvr\" value=\"1\" %s> Yes</td>");
814          * printf("<td><input type=radio name=\"PtrSvr\" value=\"0\" %s> No</td>");
815          * printf("<td></td></tr>\n");
816          */
817         
818         printf("</table></center>");
819         printf("<hr>");
820
821         printf("%s\n", _("The above configuration options will set multiple parameters and will generally assist with rapid Samba deployment."));
822         printf("</form>\n");
823 }
824
825
826 /****************************************************************************
827   display a globals editing page  
828 ****************************************************************************/
829 static void globals_page(void)
830 {
831         unsigned int parm_filter = FLAG_BASIC;
832         int mode = 0;
833
834         printf("<H2>%s</H2>\n", _("Global Parameters"));
835
836         if (cgi_variable("Commit")) {
837                 commit_parameters(GLOBAL_SECTION_SNUM);
838                 save_reload(0);
839         }
840
841         if ( cgi_variable("ViewMode") )
842                 mode = atoi(cgi_variable_nonull("ViewMode"));
843         if ( cgi_variable("BasicMode"))
844                 mode = 0;
845         if ( cgi_variable("AdvMode"))
846                 mode = 1;
847
848         printf("<form name=\"swatform\" method=post action=globals>\n");
849
850         ViewModeBoxes( mode );
851         switch ( mode ) {
852                 case 0:
853                         parm_filter = FLAG_BASIC;
854                         break;
855                 case 1:
856                         parm_filter = FLAG_ADVANCED;
857                         break;
858         }
859         printf("<br>\n");
860         if (have_write_access) {
861                 printf("<input type=submit name=\"Commit\" value=\"%s\">\n",
862                         _("Commit Changes"));
863         }
864
865         printf("<input type=reset name=\"Reset Values\" value=\"%s\">\n", 
866                  _("Reset Values"));
867
868         printf("<p>\n");
869         printf("<table>\n");
870         show_parameters(GLOBAL_SECTION_SNUM, 1, parm_filter, 0);
871         printf("</table>\n");
872         printf("</form>\n");
873 }
874
875 /****************************************************************************
876   display a shares editing page. share is in unix codepage, 
877 ****************************************************************************/
878 static void shares_page(void)
879 {
880         const char *share = cgi_variable("share");
881         char *s;
882         char *utf8_s;
883         int snum = -1;
884         int i;
885         int mode = 0;
886         unsigned int parm_filter = FLAG_BASIC;
887
888         if (share)
889                 snum = lp_servicenumber(share);
890
891         printf("<H2>%s</H2>\n", _("Share Parameters"));
892
893         if (cgi_variable("Commit") && snum >= 0) {
894                 commit_parameters(snum);
895                 save_reload(0);
896         }
897
898         if (cgi_variable("Delete") && snum >= 0) {
899                 lp_remove_service(snum);
900                 save_reload(0);
901                 share = NULL;
902                 snum = -1;
903         }
904
905         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
906                 load_config(False);
907                 lp_copy_service(GLOBAL_SECTION_SNUM, share);
908                 iNumNonAutoPrintServices = lp_numservices();
909                 save_reload(0);
910                 snum = lp_servicenumber(share);
911         }
912
913         printf("<FORM name=\"swatform\" method=post>\n");
914
915         printf("<table>\n");
916
917         if ( cgi_variable("ViewMode") )
918                 mode = atoi(cgi_variable_nonull("ViewMode"));
919         if ( cgi_variable("BasicMode"))
920                 mode = 0;
921         if ( cgi_variable("AdvMode"))
922                 mode = 1;
923
924         ViewModeBoxes( mode );
925         switch ( mode ) {
926                 case 0:
927                         parm_filter = FLAG_BASIC;
928                         break;
929                 case 1:
930                         parm_filter = FLAG_ADVANCED;
931                         break;
932         }
933         printf("<br><tr>\n");
934         printf("<td><input type=submit name=selectshare value=\"%s\"></td>\n", _("Choose Share"));
935         printf("<td><select name=share>\n");
936         if (snum < 0)
937                 printf("<option value=\" \"> \n");
938         for (i=0;i<lp_numservices();i++) {
939                 s = lp_servicename(i);
940                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
941                         push_utf8_allocate(&utf8_s, s);
942                         printf("<option %s value=\"%s\">%s\n", 
943                                (share && strcmp(share,s)==0)?"SELECTED":"",
944                                utf8_s, utf8_s);
945                         SAFE_FREE(utf8_s);
946                         
947                 }
948         }
949         printf("</select></td>\n");
950         if (have_write_access) {
951                 printf("<td><input type=submit name=\"Delete\" value=\"%s\"></td>\n", _("Delete Share"));
952         }
953         printf("</tr>\n");
954         printf("</table>");
955         printf("<table>");
956         if (have_write_access) {
957                 printf("<tr>\n");
958                 printf("<td><input type=submit name=createshare value=\"%s\"></td>\n", _("Create Share"));
959                 printf("<td><input type=text size=30 name=newshare></td></tr>\n");
960         }
961         printf("</table>");
962
963
964         if (snum >= 0) {
965                 if (have_write_access) {
966                         printf("<input type=submit name=\"Commit\" value=\"%s\">\n", _("Commit Changes"));
967                 }
968
969                 printf("<input type=reset name=\"Reset Values\" value=\"%s\">\n", _("Reset Values"));
970                 printf("<p>\n");
971         }
972
973         if (snum >= 0) {
974                 printf("<table>\n");
975                 show_parameters(snum, 1, parm_filter, 0);
976                 printf("</table>\n");
977         }
978
979         printf("</FORM>\n");
980 }
981
982 /*************************************************************
983 change a password either locally or remotely
984 *************************************************************/
985 static bool change_password(const char *remote_machine, const char *user_name, 
986                             const char *old_passwd, const char *new_passwd, 
987                                 int local_flags)
988 {
989         NTSTATUS ret;
990         char *err_str = NULL;
991         char *msg_str = NULL;
992
993         if (demo_mode) {
994                 printf("%s\n<p>", _("password change in demo mode rejected"));
995                 return False;
996         }
997         
998         if (remote_machine != NULL) {
999                 ret = remote_password_change(remote_machine, user_name,
1000                                              old_passwd, new_passwd, &err_str);
1001                 if (err_str != NULL)
1002                         printf("%s\n<p>", err_str);
1003                 SAFE_FREE(err_str);
1004                 return NT_STATUS_IS_OK(ret);
1005         }
1006
1007         if(!initialize_password_db(True, NULL)) {
1008                 printf("%s\n<p>", _("Can't setup password database vectors."));
1009                 return False;
1010         }
1011         
1012         ret = local_password_change(user_name, local_flags, new_passwd,
1013                                         &err_str, &msg_str);
1014
1015         if(msg_str)
1016                 printf("%s\n<p>", msg_str);
1017         if(err_str)
1018                 printf("%s\n<p>", err_str);
1019
1020         SAFE_FREE(msg_str);
1021         SAFE_FREE(err_str);
1022         return NT_STATUS_IS_OK(ret);
1023 }
1024
1025 /****************************************************************************
1026   do the stuff required to add or change a password 
1027 ****************************************************************************/
1028 static void chg_passwd(void)
1029 {
1030         const char *host;
1031         bool rslt;
1032         int local_flags = 0;
1033
1034         /* Make sure users name has been specified */
1035         if (strlen(cgi_variable_nonull(SWAT_USER)) == 0) {
1036                 printf("<p>%s\n", _(" Must specify \"User Name\" "));
1037                 return;
1038         }
1039
1040         /*
1041          * smbpasswd doesn't require anything but the users name to delete, disable or enable the user,
1042          * so if that's what we're doing, skip the rest of the checks
1043          */
1044         if (!cgi_variable(DISABLE_USER_FLAG) && !cgi_variable(ENABLE_USER_FLAG) && !cgi_variable(DELETE_USER_FLAG)) {
1045
1046                 /*
1047                  * If current user is not root, make sure old password has been specified 
1048                  * If REMOTE change, even root must provide old password 
1049                  */
1050                 if (((!am_root()) && (strlen( cgi_variable_nonull(OLD_PSWD)) <= 0)) ||
1051                     ((cgi_variable(CHG_R_PASSWD_FLAG)) &&  (strlen( cgi_variable_nonull(OLD_PSWD)) <= 0))) {
1052                         printf("<p>%s\n", _(" Must specify \"Old Password\" "));
1053                         return;
1054                 }
1055
1056                 /* If changing a users password on a remote hosts we have to know what host */
1057                 if ((cgi_variable(CHG_R_PASSWD_FLAG)) && (strlen( cgi_variable_nonull(RHOST)) <= 0)) {
1058                         printf("<p>%s\n", _(" Must specify \"Remote Machine\" "));
1059                         return;
1060                 }
1061
1062                 /* Make sure new passwords have been specified */
1063                 if ((strlen( cgi_variable_nonull(NEW_PSWD)) <= 0) ||
1064                     (strlen( cgi_variable_nonull(NEW2_PSWD)) <= 0)) {
1065                         printf("<p>%s\n", _(" Must specify \"New, and Re-typed Passwords\" "));
1066                         return;
1067                 }
1068
1069                 /* Make sure new passwords was typed correctly twice */
1070                 if (strcmp(cgi_variable_nonull(NEW_PSWD), cgi_variable_nonull(NEW2_PSWD)) != 0) {
1071                         printf("<p>%s\n", _(" Re-typed password didn't match new password "));
1072                         return;
1073                 }
1074         }
1075
1076         if (cgi_variable(CHG_R_PASSWD_FLAG)) {
1077                 host = cgi_variable(RHOST);
1078         } else if (am_root()) {
1079                 host = NULL;
1080         } else {
1081                 host = "127.0.0.1";
1082         }
1083
1084         /*
1085          * Set up the local flags.
1086          */
1087
1088         local_flags |= (cgi_variable(ADD_USER_FLAG) ? LOCAL_ADD_USER : 0);
1089         local_flags |= (cgi_variable(ADD_USER_FLAG) ?  LOCAL_SET_PASSWORD : 0);
1090         local_flags |= (cgi_variable(CHG_S_PASSWD_FLAG) ? LOCAL_SET_PASSWORD : 0);
1091         local_flags |= (cgi_variable(DELETE_USER_FLAG) ? LOCAL_DELETE_USER : 0);
1092         local_flags |= (cgi_variable(ENABLE_USER_FLAG) ? LOCAL_ENABLE_USER : 0);
1093         local_flags |= (cgi_variable(DISABLE_USER_FLAG) ? LOCAL_DISABLE_USER : 0);
1094         
1095
1096         rslt = change_password(host,
1097                                cgi_variable_nonull(SWAT_USER),
1098                                cgi_variable_nonull(OLD_PSWD), cgi_variable_nonull(NEW_PSWD),
1099                                    local_flags);
1100
1101         if(cgi_variable(CHG_S_PASSWD_FLAG)) {
1102                 printf("<p>");
1103                 if (rslt == True) {
1104                         printf(_(" The passwd for '%s' has been changed."), cgi_variable_nonull(SWAT_USER));
1105                         printf("\n");
1106                 } else {
1107                         printf(_(" The passwd for '%s' has NOT been changed."), cgi_variable_nonull(SWAT_USER));
1108                         printf("\n");
1109                 }
1110         }
1111         
1112         return;
1113 }
1114
1115 /****************************************************************************
1116   display a password editing page  
1117 ****************************************************************************/
1118 static void passwd_page(void)
1119 {
1120         const char *new_name = cgi_user_name();
1121
1122         /* 
1123          * After the first time through here be nice. If the user
1124          * changed the User box text to another users name, remember it.
1125          */
1126         if (cgi_variable(SWAT_USER)) {
1127                 new_name = cgi_variable_nonull(SWAT_USER);
1128         } 
1129
1130         if (!new_name) new_name = "";
1131
1132         printf("<H2>%s</H2>\n", _("Server Password Management"));
1133
1134         printf("<FORM name=\"swatform\" method=post>\n");
1135
1136         printf("<table>\n");
1137
1138         /* 
1139          * Create all the dialog boxes for data collection
1140          */
1141         printf("<tr><td> %s : </td>\n", _("User Name"));
1142         printf("<td><input type=text size=30 name=%s value=%s></td></tr> \n", SWAT_USER, new_name);
1143         if (!am_root()) {
1144                 printf("<tr><td> %s : </td>\n", _("Old Password"));
1145                 printf("<td><input type=password size=30 name=%s></td></tr> \n",OLD_PSWD);
1146         }
1147         printf("<tr><td> %s : </td>\n", _("New Password"));
1148         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
1149         printf("<tr><td> %s : </td>\n", _("Re-type New Password"));
1150         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
1151         printf("</table>\n");
1152
1153         /*
1154          * Create all the control buttons for requesting action
1155          */
1156         printf("<input type=submit name=%s value=\"%s\">\n", 
1157                CHG_S_PASSWD_FLAG, _("Change Password"));
1158         if (demo_mode || am_root()) {
1159                 printf("<input type=submit name=%s value=\"%s\">\n",
1160                        ADD_USER_FLAG, _("Add New User"));
1161                 printf("<input type=submit name=%s value=\"%s\">\n",
1162                        DELETE_USER_FLAG, _("Delete User"));
1163                 printf("<input type=submit name=%s value=\"%s\">\n", 
1164                        DISABLE_USER_FLAG, _("Disable User"));
1165                 printf("<input type=submit name=%s value=\"%s\">\n", 
1166                        ENABLE_USER_FLAG, _("Enable User"));
1167         }
1168         printf("<p></FORM>\n");
1169
1170         /*
1171          * Do some work if change, add, disable or enable was
1172          * requested. It could be this is the first time through this
1173          * code, so there isn't anything to do.  */
1174         if ((cgi_variable(CHG_S_PASSWD_FLAG)) || (cgi_variable(ADD_USER_FLAG)) || (cgi_variable(DELETE_USER_FLAG)) ||
1175             (cgi_variable(DISABLE_USER_FLAG)) || (cgi_variable(ENABLE_USER_FLAG))) {
1176                 chg_passwd();           
1177         }
1178
1179         printf("<H2>%s</H2>\n", _("Client/Server Password Management"));
1180
1181         printf("<FORM name=\"swatform\" method=post>\n");
1182
1183         printf("<table>\n");
1184
1185         /* 
1186          * Create all the dialog boxes for data collection
1187          */
1188         printf("<tr><td> %s : </td>\n", _("User Name"));
1189         printf("<td><input type=text size=30 name=%s value=%s></td></tr>\n",SWAT_USER, new_name);
1190         printf("<tr><td> %s : </td>\n", _("Old Password"));
1191         printf("<td><input type=password size=30 name=%s></td></tr>\n",OLD_PSWD);
1192         printf("<tr><td> %s : </td>\n", _("New Password"));
1193         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
1194         printf("<tr><td> %s : </td>\n", _("Re-type New Password"));
1195         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
1196         printf("<tr><td> %s : </td>\n", _("Remote Machine"));
1197         printf("<td><input type=text size=30 name=%s></td></tr>\n",RHOST);
1198
1199         printf("</table>");
1200
1201         /*
1202          * Create all the control buttons for requesting action
1203          */
1204         printf("<input type=submit name=%s value=\"%s\">", 
1205                CHG_R_PASSWD_FLAG, _("Change Password"));
1206
1207         printf("<p></FORM>\n");
1208
1209         /*
1210          * Do some work if a request has been made to change the
1211          * password somewhere other than the server. It could be this
1212          * is the first time through this code, so there isn't
1213          * anything to do.  */
1214         if (cgi_variable(CHG_R_PASSWD_FLAG)) {
1215                 chg_passwd();           
1216         }
1217
1218 }
1219
1220 /****************************************************************************
1221   display a printers editing page  
1222 ****************************************************************************/
1223 static void printers_page(void)
1224 {
1225         const char *share = cgi_variable("share");
1226         char *s;
1227         int snum=-1;
1228         int i;
1229         int mode = 0;
1230         unsigned int parm_filter = FLAG_BASIC;
1231
1232         if (share)
1233                 snum = lp_servicenumber(share);
1234
1235         printf("<H2>%s</H2>\n", _("Printer Parameters"));
1236  
1237         printf("<H3>%s</H3>\n", _("Important Note:"));
1238         printf(_("Printer names marked with [*] in the Choose Printer drop-down box "));
1239         printf(_("are autoloaded printers from "));
1240         printf("<A HREF=\"/swat/help/smb.conf.5.html#printcapname\" target=\"docs\">%s</A>\n", _("Printcap Name"));
1241         printf("%s\n", _("Attempting to delete these printers from SWAT will have no effect."));
1242
1243         if (cgi_variable("Commit") && snum >= 0) {
1244                 commit_parameters(snum);
1245                 if (snum >= iNumNonAutoPrintServices)
1246                     save_reload(snum);
1247                 else
1248                     save_reload(0);
1249         }
1250
1251         if (cgi_variable("Delete") && snum >= 0) {
1252                 lp_remove_service(snum);
1253                 save_reload(0);
1254                 share = NULL;
1255                 snum = -1;
1256         }
1257
1258         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
1259                 load_config(False);
1260                 lp_copy_service(GLOBAL_SECTION_SNUM, share);
1261                 iNumNonAutoPrintServices = lp_numservices();
1262                 snum = lp_servicenumber(share);
1263                 lp_do_parameter(snum, "print ok", "Yes");
1264                 save_reload(0);
1265                 snum = lp_servicenumber(share);
1266         }
1267
1268         printf("<FORM name=\"swatform\" method=post>\n");
1269
1270         if ( cgi_variable("ViewMode") )
1271                 mode = atoi(cgi_variable_nonull("ViewMode"));
1272         if ( cgi_variable("BasicMode"))
1273                 mode = 0;
1274         if ( cgi_variable("AdvMode"))
1275                 mode = 1;
1276
1277         ViewModeBoxes( mode );
1278         switch ( mode ) {
1279                 case 0:
1280                         parm_filter = FLAG_BASIC;
1281                         break;
1282                 case 1:
1283                         parm_filter = FLAG_ADVANCED;
1284                         break;
1285         }
1286         printf("<table>\n");
1287         printf("<tr><td><input type=submit name=\"selectshare\" value=\"%s\"></td>\n", _("Choose Printer"));
1288         printf("<td><select name=\"share\">\n");
1289         if (snum < 0 || !lp_print_ok(snum))
1290                 printf("<option value=\" \"> \n");
1291         for (i=0;i<lp_numservices();i++) {
1292                 s = lp_servicename(i);
1293                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
1294                     if (i >= iNumNonAutoPrintServices)
1295                         printf("<option %s value=\"%s\">[*]%s\n",
1296                                (share && strcmp(share,s)==0)?"SELECTED":"",
1297                                s, s);
1298                     else
1299                         printf("<option %s value=\"%s\">%s\n", 
1300                                (share && strcmp(share,s)==0)?"SELECTED":"",
1301                                s, s);
1302                 }
1303         }
1304         printf("</select></td>");
1305         if (have_write_access) {
1306                 printf("<td><input type=submit name=\"Delete\" value=\"%s\"></td>\n", _("Delete Printer"));
1307         }
1308         printf("</tr>");
1309         printf("</table>\n");
1310
1311         if (have_write_access) {
1312                 printf("<table>\n");
1313                 printf("<tr><td><input type=submit name=\"createshare\" value=\"%s\"></td>\n", _("Create Printer"));
1314                 printf("<td><input type=text size=30 name=\"newshare\"></td></tr>\n");
1315                 printf("</table>");
1316         }
1317
1318
1319         if (snum >= 0) {
1320                 if (have_write_access) {
1321                         printf("<input type=submit name=\"Commit\" value=\"%s\">\n", _("Commit Changes"));
1322                 }
1323                 printf("<input type=reset name=\"Reset Values\" value=\"%s\">\n", _("Reset Values"));
1324                 printf("<p>\n");
1325         }
1326
1327         if (snum >= 0) {
1328                 printf("<table>\n");
1329                 show_parameters(snum, 1, parm_filter, 1);
1330                 printf("</table>\n");
1331         }
1332         printf("</FORM>\n");
1333 }
1334
1335 /*
1336   when the _() translation macro is used there is no obvious place to free
1337   the resulting string and there is no easy way to give a static pointer.
1338   All we can do is rotate between some static buffers and hope a single d_printf()
1339   doesn't have more calls to _() than the number of buffers
1340 */
1341
1342 const char *lang_msg_rotate(const char *msgid)
1343 {
1344 #define NUM_LANG_BUFS 16
1345         char *msgstr;
1346         static pstring bufs[NUM_LANG_BUFS];
1347         static int next;
1348
1349         msgstr = (char *)lang_msg(msgid);
1350         if (!msgstr) return msgid;
1351
1352         pstrcpy(bufs[next], msgstr);
1353         msgstr = bufs[next];
1354
1355         next = (next+1) % NUM_LANG_BUFS;
1356
1357         return msgstr;
1358 }
1359
1360 /**
1361  * main function for SWAT.
1362  **/
1363  int main(int argc, char *argv[])
1364 {
1365         const char *page;
1366         poptContext pc;
1367         struct poptOption long_options[] = {
1368                 POPT_AUTOHELP
1369                 { "disable-authentication", 'a', POPT_ARG_VAL, &demo_mode, True, "Disable authentication (demo mode)" },
1370                 { "password-menu-only", 'P', POPT_ARG_VAL, &passwd_only, True, "Show only change password menu" }, 
1371                 POPT_COMMON_SAMBA
1372                 POPT_TABLEEND
1373         };
1374         TALLOC_CTX *frame = talloc_stackframe();
1375
1376         fault_setup(NULL);
1377         umask(S_IWGRP | S_IWOTH);
1378
1379 #if defined(HAVE_SET_AUTH_PARAMETERS)
1380         set_auth_parameters(argc, argv);
1381 #endif /* HAVE_SET_AUTH_PARAMETERS */
1382
1383         /* just in case it goes wild ... */
1384         alarm(300);
1385
1386         setlinebuf(stdout);
1387
1388         /* we don't want any SIGPIPE messages */
1389         BlockSignals(True,SIGPIPE);
1390
1391         dbf = x_fopen("/dev/null", O_WRONLY, 0);
1392         if (!dbf) dbf = x_stderr;
1393
1394         /* we don't want stderr screwing us up */
1395         close(2);
1396         open("/dev/null", O_WRONLY);
1397
1398         pc = poptGetContext("swat", argc, (const char **) argv, long_options, 0);
1399
1400         /* Parse command line options */
1401
1402         while(poptGetNextOpt(pc) != -1) { }
1403
1404         poptFreeContext(pc);
1405
1406         load_case_tables();
1407
1408         setup_logging(argv[0],False);
1409         load_config(True);
1410         load_interfaces();
1411         iNumNonAutoPrintServices = lp_numservices();
1412         load_printers();
1413
1414         cgi_setup(dyn_SWATDIR, !demo_mode);
1415
1416         print_header();
1417
1418         cgi_load_variables();
1419
1420         if (!file_exist(dyn_CONFIGFILE, NULL)) {
1421                 have_read_access = True;
1422                 have_write_access = True;
1423         } else {
1424                 /* check if the authenticated user has write access - if not then
1425                    don't show write options */
1426                 have_write_access = (access(dyn_CONFIGFILE,W_OK) == 0);
1427
1428                 /* if the user doesn't have read access to smb.conf then
1429                    don't let them view it */
1430                 have_read_access = (access(dyn_CONFIGFILE,R_OK) == 0);
1431         }
1432
1433         show_main_buttons();
1434
1435         page = cgi_pathinfo();
1436
1437         /* Root gets full functionality */
1438         if (have_read_access && strcmp(page, "globals")==0) {
1439                 globals_page();
1440         } else if (have_read_access && strcmp(page,"shares")==0) {
1441                 shares_page();
1442         } else if (have_read_access && strcmp(page,"printers")==0) {
1443                 printers_page();
1444         } else if (have_read_access && strcmp(page,"status")==0) {
1445                 status_page();
1446         } else if (have_read_access && strcmp(page,"viewconfig")==0) {
1447                 viewconfig_page();
1448         } else if (strcmp(page,"passwd")==0) {
1449                 passwd_page();
1450         } else if (have_read_access && strcmp(page,"wizard")==0) {
1451                 wizard_page();
1452         } else if (have_read_access && strcmp(page,"wizard_params")==0) {
1453                 wizard_params_page();
1454         } else if (have_read_access && strcmp(page,"rewritecfg")==0) {
1455                 rewritecfg_file();
1456         } else {
1457                 welcome_page();
1458         }
1459
1460         print_footer();
1461
1462         TALLOC_FREE(frame);
1463         return 0;
1464 }
1465
1466 /** @} **/