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