.cvsignore: Removed old entries.
[tprouty/samba.git] / source / web / swat.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba Web Administration Tool
5    Copyright (C) Andrew Tridgell 1997-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
25
26 #include "includes.h"
27 #include "smb.h"
28
29 #define GLOBALS_SNUM -1
30
31 static pstring servicesf = CONFIGFILE;
32 static BOOL demo_mode = False;
33
34 /*
35  * Password Management Globals
36  */
37 #define 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         /* Root gets full functionality */
410         if (demo_mode || am_root()) {
411                 image_link("Globals", "globals", "images/globals.gif");
412                 image_link("Shares", "shares", "images/shares.gif");
413                 image_link("Printers", "printers", "images/printers.gif");
414                 image_link("Status", "status", "images/status.gif");
415                 image_link("View Config", "viewconfig","images/viewconfig.gif");
416         }
417
418         /* Everyone gets this functionality */
419         image_link("Password Management", "passwd", "images/passwd.gif");
420
421         printf("<HR>\n");
422 }
423
424 /****************************************************************************
425   display a welcome page  
426 ****************************************************************************/
427 static void welcome_page(void)
428 {
429         include_html("help/welcome.html");
430 }
431
432 /****************************************************************************
433   display the current smb.conf  
434 ****************************************************************************/
435 static void viewconfig_page(void)
436 {
437         int full_view=0;
438
439         if (cgi_variable("full_view")) {
440                 full_view = 1;
441         }
442
443         printf("<H2>Current Config</H2>\n");
444         printf("<form method=post>\n");
445
446         if (full_view) {
447                 printf("<input type=submit name=\"normal_view\" value=\"Normal View\">\n");
448         } else {
449                 printf("<input type=submit name=\"full_view\" value=\"Full View\">\n");
450         }
451
452         printf("<p><pre>");
453         write_config(stdout, full_view);
454         printf("</pre>");
455         printf("</form>\n");
456 }
457
458 /****************************************************************************
459   display a globals editing page  
460 ****************************************************************************/
461 static void globals_page(void)
462 {
463         int advanced = 0;
464
465         printf("<H2>Global Variables</H2>\n");
466
467         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
468                 advanced = 1;
469
470         if (cgi_variable("Commit")) {
471                 commit_parameters(GLOBALS_SNUM);
472                 save_reload();
473         }
474
475         printf("<FORM name=\"swatform\" method=post>\n");
476
477         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
478         printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
479         if (advanced == 0) {
480                 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
481         } else {
482                 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
483         }
484         printf("<p>\n");
485         
486         printf("<table>\n");
487         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
488         printf("</table>\n");
489
490         if (advanced) {
491                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
492         }
493
494         printf("</FORM>\n");
495 }
496
497 /****************************************************************************
498   display a shares editing page  
499 ****************************************************************************/
500 static void shares_page(void)
501 {
502         char *share = cgi_variable("share");
503         char *s;
504         int snum=-1;
505         int i;
506         int advanced = 0;
507
508         if (share)
509                 snum = lp_servicenumber(share);
510
511         printf("<H2>Share Parameters</H2>\n");
512
513         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
514                 advanced = 1;
515
516         if (cgi_variable("Commit") && snum >= 0) {
517                 commit_parameters(snum);
518                 save_reload();
519         }
520
521         if (cgi_variable("Delete") && snum >= 0) {
522                 lp_remove_service(snum);
523                 save_reload();
524                 share = NULL;
525                 snum = -1;
526         }
527
528         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
529                 lp_copy_service(GLOBALS_SNUM, share);
530                 save_reload();
531                 snum = lp_servicenumber(share);
532         }
533
534         printf("<FORM name=\"swatform\" method=post>\n");
535
536         printf("<table>\n");
537         printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
538         printf("<td><select name=share>\n");
539         if (snum < 0)
540                 printf("<option value=\" \"> \n");
541         for (i=0;i<lp_numservices();i++) {
542                 s = lp_servicename(i);
543                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
544                         printf("<option %s value=\"%s\">%s\n", 
545                                (share && strcmp(share,s)==0)?"SELECTED":"",
546                                s, s);
547                 }
548         }
549         printf("</select></td></tr><p>");
550
551         printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
552         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
553         printf("</table>");
554
555
556         if (snum >= 0) {
557                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
558                 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
559                 if (advanced == 0) {
560                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
561                 } else {
562                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
563                 }
564                 printf("<p>\n");
565         }
566
567         if (snum >= 0) {
568                 printf("<table>\n");
569                 show_parameters(snum, 1, advanced, 0);
570                 printf("</table>\n");
571         }
572
573         if (advanced) {
574                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
575         }
576
577         printf("</FORM>\n");
578 }
579
580 /*************************************************************
581 change a password either locally or remotely
582 *************************************************************/
583 static BOOL change_password(const char *remote_machine, char *user_name, 
584                             char *old_passwd, char *new_passwd, 
585                             BOOL add_user, BOOL enable_user, BOOL disable_user)
586 {
587         if (demo_mode) {
588                 printf("password change in demo mode rejected\n<p>");
589                 return False;
590         }
591         
592         if (remote_machine != NULL) {
593                 return remote_password_change(remote_machine, user_name, old_passwd, new_passwd);
594         }
595
596         if(!initialize_password_db()) {
597                 printf("Can't setup password database vectors.\n<p>");
598                 return False;
599         }
600         
601         return local_password_change(user_name, False, add_user, enable_user, 
602                                      disable_user, False, new_passwd);
603 }
604
605 /****************************************************************************
606   do the stuff required to add or change a password 
607 ****************************************************************************/
608 static void chg_passwd(void)
609 {
610         char *host;
611         BOOL rslt;
612
613         /* Make sure users name has been specified */
614         if (strlen(cgi_variable(USER)) == 0) {
615                 printf("<p> Must specify \"User Name\" \n");
616                 return;
617         }
618
619         /*
620          * smbpasswd doesn't require anything but the users name to disable or enable the user,
621          * so if that's what we're doing, skip the rest of the checks
622          */
623         if (!cgi_variable(DISABLE_USER_FLAG) && !cgi_variable(ENABLE_USER_FLAG)) {
624
625                 /*
626                  * If current user is not root, make sure old password has been specified 
627                  * If REMOTE change, even root must provide old password 
628                  */
629                 if (((am_root() == False) && (strlen( cgi_variable(OLD_PSWD)) <= 0)) ||
630                     ((cgi_variable(CHG_R_PASSWD_FLAG)) &&  (strlen( cgi_variable(OLD_PSWD)) <= 0))) {
631                         printf("<p> Must specify \"Old Password\" \n");
632                         return;
633                 }
634
635                 /* If changing a users password on a remote hosts we have to know what host */
636                 if ((cgi_variable(CHG_R_PASSWD_FLAG)) && (strlen( cgi_variable(RHOST)) <= 0)) {
637                         printf("<p> Must specify \"Remote Machine\" \n");
638                         return;
639                 }
640
641                 /* Make sure new passwords have been specified */
642                 if ((strlen( cgi_variable(NEW_PSWD)) <= 0) ||
643                     (strlen( cgi_variable(NEW2_PSWD)) <= 0)) {
644                         printf("<p> Must specify \"New, and Re-typed Passwords\" \n");
645                         return;
646                 }
647
648                 /* Make sure new passwords was typed correctly twice */
649                 if (strcmp(cgi_variable(NEW_PSWD), cgi_variable(NEW2_PSWD)) != 0) {
650                         printf("<p> Re-typed password didn't match new password\n");
651                         return;
652                 }
653         }
654
655         if (cgi_variable(CHG_R_PASSWD_FLAG)) {
656                 host = cgi_variable(RHOST);
657         } else if (am_root()) {
658                 host = NULL;
659         } else {
660                 host = "127.0.0.1";
661         }
662         rslt = change_password(host,
663                                cgi_variable(USER),
664                                cgi_variable(OLD_PSWD), cgi_variable(NEW_PSWD),
665                                cgi_variable(ADD_USER_FLAG)? True : False,
666                                cgi_variable(ENABLE_USER_FLAG)? True : False,
667                                cgi_variable(DISABLE_USER_FLAG)? True : False);
668
669
670         if (rslt == True) {
671                 printf("<p> The passwd for '%s' has been changed. \n", cgi_variable(USER));
672         } else {
673                 printf("<p> The passwd for '%s' has NOT been changed. \n",cgi_variable(USER));
674         }
675         
676         return;
677 }
678
679 /****************************************************************************
680   display a password editing page  
681 ****************************************************************************/
682 static void passwd_page(void)
683 {
684         char *new_name = get_user_name();
685
686         /* 
687          * After the first time through here be nice. If the user
688          * changed the User box text to another users name, remember it.
689          */
690         if (cgi_variable(USER)) {
691                 new_name = cgi_variable(USER);
692         } 
693
694         if (!new_name) new_name = "";
695
696         printf("<H2>Server Password Management</H2>\n");
697
698         printf("<FORM name=\"swatform\" method=post>\n");
699
700         printf("<table>\n");
701
702         /* 
703          * Create all the dialog boxes for data collection
704          */
705         printf("<tr><td> User Name : </td>\n");
706         printf("<td><input type=text size=30 name=%s value=%s></td></tr> \n", USER, new_name);
707         if (am_root() == False) {
708                 printf("<tr><td> Old Password : </td>\n");
709                 printf("<td><input type=password size=30 name=%s></td></tr> \n",OLD_PSWD);
710         }
711         printf("<tr><td> New Password : </td>\n");
712         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
713         printf("<tr><td> Re-type New Password : </td>\n");
714         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
715
716         /*
717          * Create all the control buttons for requesting action
718          */
719         printf("<tr><td><input type=submit name=%s value=\"Change Password\"></td></tr>\n", CHG_S_PASSWD_FLAG);
720         if (am_root() == True) {
721                 printf("<tr><td><input type=submit name=%s value=\"Add New User\"></td></tr>\n", ADD_USER_FLAG);
722                 printf("<tr><td><input type=submit name=%s value=\"Disable User\"></td></tr>\n", DISABLE_USER_FLAG);
723                 printf("<tr><td><input type=submit name=%s value=\"Enable User\"></td></tr>\n", ENABLE_USER_FLAG);
724         }
725
726         /*
727          * Do some work if change, add, disable or enable was requested. It could be
728          * this is the first time through this code, so there isn't anything to do.
729          */
730         if ((cgi_variable(CHG_S_PASSWD_FLAG)) || (cgi_variable(ADD_USER_FLAG)) ||
731             (cgi_variable(DISABLE_USER_FLAG)) || (cgi_variable(ENABLE_USER_FLAG))) {
732                 chg_passwd();           
733         }
734
735         printf("</table>\n");
736
737         printf("</FORM>\n");
738
739         printf("<H2>Client/Server Password Management</H2>\n");
740
741         printf("<FORM name=\"swatform\" method=post>\n");
742
743         printf("<table>\n");
744
745         /* 
746          * Create all the dialog boxes for data collection
747          */
748         printf("<tr><td> User Name : </td>\n");
749         printf("<td><input type=text size=30 name=%s value=%s></td></tr>\n",USER, new_name);
750         printf("<tr><td> Old Password : </td>\n");
751         printf("<td><input type=password size=30 name=%s></td></tr>\n",OLD_PSWD);
752         printf("<tr><td> New Password : </td>\n");
753         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
754         printf("<tr><td> Re-type New Password : </td>\n");
755         printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
756         printf("<tr><td> Remote Machine : </td>\n");
757         printf("<td><input type=password size=30 name=%s></td></tr>\n",RHOST);
758
759         /*
760          * Create all the control buttons for requesting action
761          */
762         printf("<tr><td><input type=submit name=%s value=\"Change Password\"></td></tr>", CHG_R_PASSWD_FLAG);
763
764         /*
765          * Do some work if a request has been made to change the password somewhere other
766          * than the server. It could be this is the first time through this code, so there 
767          * isn't anything to do.
768          */
769         if (cgi_variable(CHG_R_PASSWD_FLAG)) {
770                 chg_passwd();           
771         }
772
773         printf("</table>");
774
775         printf("</FORM>\n");
776 }
777
778 /****************************************************************************
779   display a printers editing page  
780 ****************************************************************************/
781 static void printers_page(void)
782 {
783         char *share = cgi_variable("share");
784         char *s;
785         int snum=-1;
786         int i;
787         int advanced = 0;
788
789         if (share)
790                 snum = lp_servicenumber(share);
791
792         printf("<H2>Printer Parameters</H2>\n");
793
794         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
795                 advanced = 1;
796
797         if (cgi_variable("Commit") && snum >= 0) {
798                 commit_parameters(snum);
799                 save_reload();
800         }
801
802         if (cgi_variable("Delete") && snum >= 0) {
803                 lp_remove_service(snum);
804                 save_reload();
805                 share = NULL;
806                 snum = -1;
807         }
808
809         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
810                 lp_copy_service(GLOBALS_SNUM, share);
811                 snum = lp_servicenumber(share);
812                 lp_do_parameter(snum, "print ok", "Yes");
813                 save_reload();
814                 snum = lp_servicenumber(share);
815         }
816
817         printf("<FORM name=\"swatform\" method=post>\n");
818
819         printf("<table>\n");
820         printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
821         printf("<td><select name=share>\n");
822         if (snum < 0 || !lp_print_ok(snum))
823                 printf("<option value=\" \"> \n");
824         for (i=0;i<lp_numservices();i++) {
825                 s = lp_servicename(i);
826                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
827                         printf("<option %s value=\"%s\">%s\n", 
828                                (share && strcmp(share,s)==0)?"SELECTED":"",
829                                s, s);
830                 }
831         }
832         printf("</select></td></tr><p>");
833
834         printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
835         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
836         printf("</table>");
837
838
839         if (snum >= 0) {
840                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
841                 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
842                 if (advanced == 0) {
843                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
844                 } else {
845                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
846                 }
847                 printf("<p>\n");
848         }
849
850         if (snum >= 0) {
851                 printf("<table>\n");
852                 show_parameters(snum, 1, advanced, 1);
853                 printf("</table>\n");
854         }
855
856         if (advanced) {
857                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
858         }
859
860         printf("</FORM>\n");
861 }
862
863 /****************************************************************************
864   MAIN()
865 ****************************************************************************/
866  int main(int argc, char *argv[])
867 {
868         extern char *optarg;
869         extern int optind;
870         extern FILE *dbf;
871         int opt;
872         char *page;
873
874         /* just in case it goes wild ... */
875         alarm(300);
876
877         dbf = fopen("/dev/null", "w");
878
879         if (!dbf) dbf = stderr;
880
881         while ((opt = getopt(argc, argv,"s:a")) != EOF) {
882                 switch (opt) {
883                 case 's':
884                         pstrcpy(servicesf,optarg);
885                         break;    
886                 case 'a':
887                         demo_mode = True;
888                         break;    
889                 }
890         }
891
892         cgi_setup(SWATDIR, !demo_mode);
893
894         print_header();
895         
896         charset_initialise();
897
898         /* if this binary is setuid then run completely as root */
899         setuid(0);
900
901         load_config();
902
903         cgi_load_variables(NULL);
904
905         show_main_buttons();
906
907         page = cgi_pathinfo();
908
909         /* Root gets full functionality */
910         if (demo_mode || am_root()) {
911                 if (strcmp(page, "globals")==0) {
912                         globals_page();
913                 } else if (strcmp(page,"shares")==0) {
914                         shares_page();
915                 } else if (strcmp(page,"printers")==0) {
916                         printers_page();
917                 } else if (strcmp(page,"status")==0) {
918                         status_page();
919                 } else if (strcmp(page,"viewconfig")==0) {
920                         viewconfig_page();
921                 } else if (strcmp(page,"passwd")==0) {
922                         passwd_page();
923                 } else {
924                         welcome_page();
925                 }
926         } else {
927                 /* Everyone gets this functionality */
928                 if (strcmp(page,"passwd")==0) {
929                         passwd_page();
930                 } else {
931                         welcome_page();
932                 }
933         }
934         
935         print_footer();
936         return 0;
937 }