The big character set handling changeover!
[samba.git] / source / web / swat.c
index 8c5ac782a39bed69160249b36714a810a4fa77f7..5dcfe3d658d7562bfc097c61f734b10c9b93438d 100644 (file)
@@ -31,6 +31,8 @@
 static pstring servicesf = CONFIGFILE;
 static BOOL demo_mode = False;
 static BOOL have_write_access = False;
+static BOOL have_read_access = False;
+static int iNumNonAutoPrintServices = 0;
 
 /*
  * Password Management Globals
@@ -42,19 +44,20 @@ static BOOL have_write_access = False;
 #define CHG_S_PASSWD_FLAG "chg_s_passwd_flag"
 #define CHG_R_PASSWD_FLAG "chg_r_passwd_flag"
 #define ADD_USER_FLAG "add_user_flag"
+#define DELETE_USER_FLAG "delete_user_flag"
 #define DISABLE_USER_FLAG "disable_user_flag"
 #define ENABLE_USER_FLAG "enable_user_flag"
 #define RHOST "remote_host"
 
 /* we need these because we link to locking*.o */
- void become_root(BOOL save_dir) {}
- void unbecome_root(BOOL restore_dir) {}
+ void become_root(void) {}
+ void unbecome_root(void) {}
 
 /****************************************************************************
 ****************************************************************************/
 static int enum_index(int value, struct enum_list *enumlist)
 {
-int i;
+       int i;
        for (i=0;enumlist[i].name;i++)
                if (value == enumlist[i].value) break;
        return(i);
@@ -62,8 +65,8 @@ int i;
 
 static char *fix_backslash(char *str)
 {
-static char newstring[1024];
-char *p = newstring;
+       static char newstring[1024];
+       char *p = newstring;
 
         while (*str) {
                 if (*str == '\\') {*p++ = '\\';*p++ = '\\';}
@@ -158,13 +161,16 @@ static void show_parameter(int snum, struct parm_struct *parm)
 {
        int i;
        void *ptr = parm->ptr;
+       char* str;
 
        if (parm->class == P_LOCAL && snum >= 0) {
                ptr = lp_local_ptr(snum, ptr);
        }
 
-       printf("<tr><td><A HREF=\"/swat/help/smb.conf.5.html#%s\">Help</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s</td><td>", 
-              stripspace(parm->label), parm->label);
+       str = stripspace(parm->label);
+       strupper (str);
+       printf("<tr><td><A HREF=\"/swat/help/smb.conf.5.html#%s\" target=\"docs\">Help</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s</td><td>", 
+              str, parm->label);
 
        switch (parm->type) {
        case P_CHAR:
@@ -174,6 +180,27 @@ static void show_parameter(int snum, struct parm_struct *parm)
                        make_parm_name(parm->label),(char)(parm->def.cvalue));
                break;
 
+       case P_LIST:
+               printf("<input type=text size=40 name=\"parm_%s\" value=\"",
+                       make_parm_name(parm->label));
+               if ((char ***)ptr && *(char ***)ptr && **(char ***)ptr) {
+                       char **list = *(char ***)ptr;
+                       for (;*list;list++) {
+                               printf("%s%s", *list, ((*(list+1))?" ":""));
+                       }
+               }
+               printf("\">");
+               printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'",
+                       make_parm_name(parm->label));
+               if (parm->def.lvalue) {
+                       char **list = (char **)(parm->def.lvalue);
+                       for (; *list; list++) {
+                               printf("%s%s", *list, ((*(list+1))?" ":""));
+                       }
+               }
+               printf("\'\">");
+               break;
+
        case P_STRING:
        case P_USTRING:
                printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
@@ -215,15 +242,19 @@ static void show_parameter(int snum, struct parm_struct *parm)
                break;
 
        case P_OCTAL:
-               printf("<input type=text size=8 name=\"parm_%s\" value=0%o>", make_parm_name(parm->label), *(int *)ptr);
-               printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'0%o\'\">",
-                       make_parm_name(parm->label),(int)(parm->def.ivalue));
+               printf("<input type=text size=8 name=\"parm_%s\" value=%s>", make_parm_name(parm->label), octal_string(*(int *)ptr));
+               printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
+                      make_parm_name(parm->label),
+                      octal_string((int)(parm->def.ivalue)));
                break;
 
        case P_ENUM:
                printf("<select name=\"parm_%s\">",make_parm_name(parm->label)); 
-               for (i=0;parm->enum_list[i].name;i++)
-                       printf("<option %s>%s",(*(int *)ptr)==parm->enum_list[i].value?"selected":"",parm->enum_list[i].name);
+               for (i=0;parm->enum_list[i].name;i++) {
+                       if (i == 0 || parm->enum_list[i].value != parm->enum_list[i-1].value) {
+                               printf("<option %s>%s",(*(int *)ptr)==parm->enum_list[i].value?"selected":"",parm->enum_list[i].name);
+                       }
+               }
                printf("</select>");
                printf("<input type=button value=\"Set Default\" onClick=\"swatform.parm_%s.selectedIndex=\'%d\'\">",
                        make_parm_name(parm->label),enum_index((int)(parm->def.ivalue),parm->enum_list));
@@ -252,15 +283,27 @@ static void show_parameters(int snum, int allparameters, int advanced, int print
                        continue;
                }
                if (parm->flags & FLAG_HIDE) continue;
+               if (snum >= 0) {
+                       if (printers & !(parm->flags & FLAG_PRINT)) continue;
+                       if (!printers & !(parm->flags & FLAG_SHARE)) continue;
+               }
                if (!advanced) {
-                       if (!printers && !(parm->flags & FLAG_BASIC)) {
+                       if (!(parm->flags & FLAG_BASIC)) {
                                void *ptr = parm->ptr;
 
+                               if (parm->class == P_LOCAL && snum >= 0) {
+                                       ptr = lp_local_ptr(snum, ptr);
+                               }
+
                                switch (parm->type) {
                                case P_CHAR:
                                        if (*(char *)ptr == (char)(parm->def.cvalue)) continue;
                                        break;
 
+                               case P_LIST:
+                                       if (!lp_list_compare(*(char ***)ptr, (char **)(parm->def.lvalue))) continue;
+                                       break;
+
                                case P_STRING:
                                case P_USTRING:
                                        if (!strcmp(*(char **)ptr,(char *)(parm->def.svalue))) continue;
@@ -299,6 +342,15 @@ static void show_parameters(int snum, int allparameters, int advanced, int print
        }
 }
 
+/****************************************************************************
+  load the smb.conf file into loadparm.
+****************************************************************************/
+static BOOL load_config(BOOL save_def)
+{
+       lp_resetnumservices();
+       return lp_load(servicesf,False,save_def,False);
+}
+
 /****************************************************************************
   write a config file 
 ****************************************************************************/
@@ -306,17 +358,18 @@ static void write_config(FILE *f, BOOL show_defaults)
 {
        fprintf(f, "# Samba config file created using SWAT\n");
        fprintf(f, "# from %s (%s)\n", cgi_remote_host(), cgi_remote_addr());
-       fprintf(f, "# Date: %s\n\n", timestring());
+       fprintf(f, "# Date: %s\n\n", timestring(False));
        
-       lp_dump(f, show_defaults);      
+       lp_dump(f, show_defaults, iNumNonAutoPrintServices);
 }
 
 /****************************************************************************
   save and reoad the smb.conf config file 
 ****************************************************************************/
-static int save_reload(void)
+static int save_reload(int snum)
 {
        FILE *f;
+       struct stat st;
 
        f = sys_fopen(servicesf,"w");
        if (!f) {
@@ -324,15 +377,25 @@ static int save_reload(void)
                return 0;
        }
 
+       /* just in case they have used the buggy xinetd to create the file */
+       if (fstat(fileno(f), &st) == 0 &&
+           (st.st_mode & S_IWOTH)) {
+               fchmod(fileno(f), S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
+       }
+
        write_config(f, False);
+       if (snum)
+               lp_dump_one(f, False, snum);
        fclose(f);
 
        lp_killunused(NULL);
 
-       if (!lp_load(servicesf,False,False,False)) {
+       if (!load_config(False)) {
                 printf("Can't reload %s\n", servicesf);
                 return 0;
         }
+       iNumNonAutoPrintServices = lp_numservices();
+       load_printers();
 
        return 1;
 }
@@ -379,23 +442,12 @@ static void commit_parameters(int snum)
        }
 }
 
-/****************************************************************************
-  load the smb.conf file into loadparm.
-****************************************************************************/
-static void load_config(void)
-{
-       if (!lp_load(servicesf,False,True,False)) {
-               printf("<b>Can't load %s - using defaults</b><p>\n", 
-                      servicesf);
-       }
-}
-
 /****************************************************************************
   spit out the html for a link with an image 
 ****************************************************************************/
 static void image_link(char *name,char *hlink, char *src)
 {
-       printf("<A HREF=\"%s/%s\"><img src=\"/swat/%s\" alt=\"%s\"></A>\n", 
+       printf("<A HREF=\"%s/%s\"><img border=\"0\" src=\"/swat/%s\" alt=\"%s\"></A>\n", 
               cgi_baseurl(), hlink, src, name);
 }
 
@@ -405,13 +457,22 @@ static void image_link(char *name,char *hlink, char *src)
 ****************************************************************************/
 static void show_main_buttons(void)
 {
-       image_link("Home", "", "images/home.gif");
+       char *p;
+       
+       if ((p = cgi_user_name()) && strcmp(p, "root")) {
+               printf("Logged in as <b>%s</b><p>\n", p);
+       }
 
-       image_link("Globals", "globals", "images/globals.gif");
-       image_link("Shares", "shares", "images/shares.gif");
-       image_link("Printers", "printers", "images/printers.gif");
-       image_link("Status", "status", "images/status.gif");
-       image_link("View Config", "viewconfig","images/viewconfig.gif");
+       image_link("Home", "", "images/home.gif");
+       if (have_write_access) {
+               image_link("Globals", "globals", "images/globals.gif");
+               image_link("Shares", "shares", "images/shares.gif");
+               image_link("Printers", "printers", "images/printers.gif");
+       }
+       if (have_read_access) {
+               image_link("Status", "status", "images/status.gif");
+               image_link("View Config", "viewconfig","images/viewconfig.gif");
+       }
        image_link("Password Management", "passwd", "images/passwd.gif");
 
        printf("<HR>\n");
@@ -465,7 +526,7 @@ static void globals_page(void)
 
        if (cgi_variable("Commit")) {
                commit_parameters(GLOBALS_SNUM);
-               save_reload();
+               save_reload(0);
        }
 
        printf("<FORM name=\"swatform\" method=post>\n");
@@ -494,7 +555,8 @@ static void globals_page(void)
 }
 
 /****************************************************************************
-  display a shares editing page  
+  display a shares editing page. share is in unix codepage, and must be in
+  dos codepage. FIXME !!! JRA.
 ****************************************************************************/
 static void shares_page(void)
 {
@@ -514,26 +576,29 @@ static void shares_page(void)
 
        if (cgi_variable("Commit") && snum >= 0) {
                commit_parameters(snum);
-               save_reload();
+               save_reload(0);
        }
 
        if (cgi_variable("Delete") && snum >= 0) {
                lp_remove_service(snum);
-               save_reload();
+               save_reload(0);
                share = NULL;
                snum = -1;
        }
 
        if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
+               load_config(False);
                lp_copy_service(GLOBALS_SNUM, share);
-               save_reload();
+               iNumNonAutoPrintServices = lp_numservices();
+               save_reload(0);
                snum = lp_servicenumber(share);
        }
 
        printf("<FORM name=\"swatform\" method=post>\n");
 
        printf("<table>\n");
-       printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
+       printf("<tr>\n");
+       printf("<td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
        printf("<td><select name=share>\n");
        if (snum < 0)
                printf("<option value=\" \"> \n");
@@ -545,10 +610,18 @@ static void shares_page(void)
                               s, s);
                }
        }
-       printf("</select></td></tr><p>");
-
-       printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
-       printf("<td><input type=text size=30 name=newshare></td></tr>\n");
+       printf("</select></td>\n");
+       if (have_write_access) {
+               printf("<td><input type=submit name=\"Delete\" value=\"Delete Share\"></td>\n");
+       }
+       printf("</tr>\n");
+       printf("</table>");
+       printf("<table>");
+       if (have_write_access) {
+               printf("<tr>\n");
+               printf("<td><input type=submit name=createshare value=\"Create Share\"></td>\n");
+               printf("<td><input type=text size=30 name=newshare></td></tr>\n");
+       }
        printf("</table>");
 
 
@@ -557,7 +630,7 @@ static void shares_page(void)
                        printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
                }
 
-               printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
+               printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
                if (advanced == 0) {
                        printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
                } else {
@@ -584,7 +657,7 @@ change a password either locally or remotely
 *************************************************************/
 static BOOL change_password(const char *remote_machine, char *user_name, 
                            char *old_passwd, char *new_passwd, 
-                           BOOL add_user, BOOL enable_user, BOOL disable_user)
+                               int local_flags)
 {
        BOOL ret = False;
        pstring err_str;
@@ -603,13 +676,12 @@ static BOOL change_password(const char *remote_machine, char *user_name,
                return ret;
        }
 
-       if(!initialise_password_db()) {
+       if(!initialize_password_db(True)) {
                printf("Can't setup password database vectors.\n<p>");
                return False;
        }
        
-       ret = local_password_change(user_name, False, add_user, enable_user, 
-                                    disable_user, False, new_passwd, err_str, sizeof(err_str),
+       ret = local_password_change(user_name, local_flags, new_passwd, err_str, sizeof(err_str),
                                         msg_str, sizeof(msg_str));
 
        if(*msg_str)
@@ -627,6 +699,7 @@ static void chg_passwd(void)
 {
        char *host;
        BOOL rslt;
+       int local_flags = 0;
 
        /* Make sure users name has been specified */
        if (strlen(cgi_variable(SWAT_USER)) == 0) {
@@ -635,10 +708,10 @@ static void chg_passwd(void)
        }
 
        /*
-        * smbpasswd doesn't require anything but the users name to disable or enable the user,
+        * smbpasswd doesn't require anything but the users name to delete, disable or enable the user,
         * so if that's what we're doing, skip the rest of the checks
         */
-       if (!cgi_variable(DISABLE_USER_FLAG) && !cgi_variable(ENABLE_USER_FLAG)) {
+       if (!cgi_variable(DISABLE_USER_FLAG) && !cgi_variable(ENABLE_USER_FLAG) && !cgi_variable(DELETE_USER_FLAG)) {
 
                /*
                 * If current user is not root, make sure old password has been specified 
@@ -677,18 +750,27 @@ static void chg_passwd(void)
        } else {
                host = "127.0.0.1";
        }
+
+       /*
+        * Set up the local flags.
+        */
+
+       local_flags |= (cgi_variable(ADD_USER_FLAG) ? LOCAL_ADD_USER : 0);
+       local_flags |= (cgi_variable(DELETE_USER_FLAG) ? LOCAL_DELETE_USER : 0);
+       local_flags |= (cgi_variable(ENABLE_USER_FLAG) ? LOCAL_ENABLE_USER : 0);
+       local_flags |= (cgi_variable(DISABLE_USER_FLAG) ? LOCAL_DISABLE_USER : 0);
+
        rslt = change_password(host,
                               cgi_variable(SWAT_USER),
                               cgi_variable(OLD_PSWD), cgi_variable(NEW_PSWD),
-                              cgi_variable(ADD_USER_FLAG)? True : False,
-                              cgi_variable(ENABLE_USER_FLAG)? True : False,
-                              cgi_variable(DISABLE_USER_FLAG)? True : False);
-
+                                  local_flags);
 
-       if (rslt == True) {
-               printf("<p> The passwd for '%s' has been changed. \n", cgi_variable(SWAT_USER));
-       } else {
-               printf("<p> The passwd for '%s' has NOT been changed. \n",cgi_variable(SWAT_USER));
+       if(local_flags == 0) {
+               if (rslt == True) {
+                       printf("<p> The passwd for '%s' has been changed. \n", cgi_variable(SWAT_USER));
+               } else {
+                       printf("<p> The passwd for '%s' has NOT been changed. \n",cgi_variable(SWAT_USER));
+               }
        }
        
        return;
@@ -730,30 +812,34 @@ static void passwd_page(void)
        printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW_PSWD);
        printf("<tr><td> Re-type New Password : </td>\n");
        printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
+       printf("</table>\n");
 
        /*
         * Create all the control buttons for requesting action
         */
-       printf("<tr><td><input type=submit name=%s value=\"Change Password\"></td></tr>\n", CHG_S_PASSWD_FLAG);
+       printf("<input type=submit name=%s value=\"Change Password\">\n", 
+              CHG_S_PASSWD_FLAG);
        if (demo_mode || am_root()) {
-               printf("<tr><td><input type=submit name=%s value=\"Add New User\"></td></tr>\n", ADD_USER_FLAG);
-               printf("<tr><td><input type=submit name=%s value=\"Disable User\"></td></tr>\n", DISABLE_USER_FLAG);
-               printf("<tr><td><input type=submit name=%s value=\"Enable User\"></td></tr>\n", ENABLE_USER_FLAG);
+               printf("<input type=submit name=%s value=\"Add New User\">\n",
+                      ADD_USER_FLAG);
+               printf("<input type=submit name=%s value=\"Delete User\">\n",
+                      DELETE_USER_FLAG);
+               printf("<input type=submit name=%s value=\"Disable User\">\n", 
+                      DISABLE_USER_FLAG);
+               printf("<input type=submit name=%s value=\"Enable User\">\n", 
+                      ENABLE_USER_FLAG);
        }
+       printf("<p></FORM>\n");
 
        /*
-        * Do some work if change, add, disable or enable was requested. It could be
-        * this is the first time through this code, so there isn't anything to do.
-        */
-       if ((cgi_variable(CHG_S_PASSWD_FLAG)) || (cgi_variable(ADD_USER_FLAG)) ||
+        * Do some work if change, add, disable or enable was
+        * requested. It could be this is the first time through this
+        * code, so there isn't anything to do.  */
+       if ((cgi_variable(CHG_S_PASSWD_FLAG)) || (cgi_variable(ADD_USER_FLAG)) || (cgi_variable(DELETE_USER_FLAG)) ||
            (cgi_variable(DISABLE_USER_FLAG)) || (cgi_variable(ENABLE_USER_FLAG))) {
                chg_passwd();           
        }
 
-       printf("</table>\n");
-
-       printf("</FORM>\n");
-
        printf("<H2>Client/Server Password Management</H2>\n");
 
        printf("<FORM name=\"swatform\" method=post>\n");
@@ -772,25 +858,27 @@ static void passwd_page(void)
        printf("<tr><td> Re-type New Password : </td>\n");
        printf("<td><input type=password size=30 name=%s></td></tr>\n",NEW2_PSWD);
        printf("<tr><td> Remote Machine : </td>\n");
-       printf("<td><input type=password size=30 name=%s></td></tr>\n",RHOST);
+       printf("<td><input type=text size=30 name=%s></td></tr>\n",RHOST);
+
+       printf("</table>");
 
        /*
         * Create all the control buttons for requesting action
         */
-       printf("<tr><td><input type=submit name=%s value=\"Change Password\"></td></tr>", CHG_R_PASSWD_FLAG);
+       printf("<input type=submit name=%s value=\"Change Password\">", 
+              CHG_R_PASSWD_FLAG);
+
+       printf("<p></FORM>\n");
 
        /*
-        * Do some work if a request has been made to change the password somewhere other
-        * than the server. It could be this is the first time through this code, so there 
-        * isn't anything to do.
-        */
+        * Do some work if a request has been made to change the
+        * password somewhere other than the server. It could be this
+        * is the first time through this code, so there isn't
+        * anything to do.  */
        if (cgi_variable(CHG_R_PASSWD_FLAG)) {
                chg_passwd();           
        }
 
-       printf("</table>");
-
-       printf("</FORM>\n");
 }
 
 /****************************************************************************
@@ -809,26 +897,37 @@ static void printers_page(void)
 
        printf("<H2>Printer Parameters</H2>\n");
 
+       printf("<H3>Important Note:</H3>\n");
+       printf("Printer names marked with [*] in the Choose Printer drop-down box ");
+       printf("are autoloaded printers from ");
+       printf("<A HREF=\"/swat/help/smb.conf.5.html#PRINTCAPNAME\" target=\"docs\">Printcap Name</A>.\n");
+       printf("Attempting to delete these printers from SWAT will have no effect.\n");
+
        if (cgi_variable("Advanced") && !cgi_variable("Basic"))
                advanced = 1;
 
        if (cgi_variable("Commit") && snum >= 0) {
                commit_parameters(snum);
-               save_reload();
+               if (snum >= iNumNonAutoPrintServices)
+                   save_reload(snum);
+               else
+                   save_reload(0);
        }
 
        if (cgi_variable("Delete") && snum >= 0) {
                lp_remove_service(snum);
-               save_reload();
+               save_reload(0);
                share = NULL;
                snum = -1;
        }
 
        if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
+               load_config(False);
                lp_copy_service(GLOBALS_SNUM, share);
+               iNumNonAutoPrintServices = lp_numservices();
                snum = lp_servicenumber(share);
                lp_do_parameter(snum, "print ok", "Yes");
-               save_reload();
+               save_reload(0);
                snum = lp_servicenumber(share);
        }
 
@@ -842,23 +941,36 @@ static void printers_page(void)
        for (i=0;i<lp_numservices();i++) {
                s = lp_servicename(i);
                if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
+                    if (i >= iNumNonAutoPrintServices)
+                        printf("<option %s value=\"%s\">[*]%s\n",
+                               (share && strcmp(share,s)==0)?"SELECTED":"",
+                               s, s);
+                    else
                        printf("<option %s value=\"%s\">%s\n", 
                               (share && strcmp(share,s)==0)?"SELECTED":"",
                               s, s);
                }
        }
-       printf("</select></td></tr><p>");
+       printf("</select></td>");
+       if (have_write_access) {
+               printf("<td><input type=submit name=\"Delete\" value=\"Delete Printer\"></td>\n");
+       }
+       printf("</tr>");
+       printf("</table>\n");
 
-       printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
-       printf("<td><input type=text size=30 name=newshare></td></tr>\n");
-       printf("</table>");
+       if (have_write_access) {
+               printf("<table>\n");
+               printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
+               printf("<td><input type=text size=30 name=newshare></td></tr>\n");
+               printf("</table>");
+       }
 
 
        if (snum >= 0) {
                if (have_write_access) {
                        printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
                }
-               printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
+               printf("<input type=reset name=\"Reset Values\" value=\"Reset Values\">\n");
                if (advanced == 0) {
                        printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
                } else {
@@ -891,13 +1003,26 @@ static void printers_page(void)
        int opt;
        char *page;
 
+       fault_setup(NULL);
+       umask(S_IWGRP | S_IWOTH);
+
+#if defined(HAVE_SET_AUTH_PARAMETERS)
+       set_auth_parameters(argc, argv);
+#endif /* HAVE_SET_AUTH_PARAMETERS */
+
        /* just in case it goes wild ... */
        alarm(300);
 
-       dbf = sys_fopen("/dev/null", "w");
+       /* we don't want any SIGPIPE messages */
+       BlockSignals(True,SIGPIPE);
 
+       dbf = sys_fopen("/dev/null", "w");
        if (!dbf) dbf = stderr;
 
+       /* we don't want stderr screwing us up */
+       close(2);
+       open("/dev/null", O_WRONLY);
+
        while ((opt = getopt(argc, argv,"s:a")) != EOF) {
                switch (opt) {
                case 's':
@@ -909,37 +1034,45 @@ static void printers_page(void)
                }
        }
 
+       setup_logging(argv[0],False);
+       load_config(True);
+       iNumNonAutoPrintServices = lp_numservices();
+       load_printers();
+
        cgi_setup(SWATDIR, !demo_mode);
 
        print_header();
        
-       charset_initialise();
+       cgi_load_variables(NULL);
 
-       /* if this binary is setuid then run completely as root */
-       setuid(0);
+       if (!file_exist(servicesf, NULL)) {
+               have_read_access = True;
+               have_write_access = True;
+       } else {
+               /* check if the authenticated user has write access - if not then
+                  don't show write options */
+               have_write_access = (access(servicesf,W_OK) == 0);
 
-       load_config();
+               /* if the user doesn't have read access to smb.conf then
+                  don't let them view it */
+               have_read_access = (access(servicesf,R_OK) == 0);
+       }
 
-       cgi_load_variables(NULL);
 
        show_main_buttons();
 
        page = cgi_pathinfo();
 
-       /* check if the authenticated user has write access - if not then
-          don't show write options */
-       have_write_access = (access(servicesf,W_OK) == 0);
-
        /* Root gets full functionality */
-       if (strcmp(page, "globals")==0) {
+       if (have_read_access && strcmp(page, "globals")==0) {
                globals_page();
-       } else if (strcmp(page,"shares")==0) {
+       } else if (have_read_access && strcmp(page,"shares")==0) {
                shares_page();
-       } else if (strcmp(page,"printers")==0) {
+       } else if (have_read_access && strcmp(page,"printers")==0) {
                printers_page();
-       } else if (strcmp(page,"status")==0) {
+       } else if (have_read_access && strcmp(page,"status")==0) {
                status_page();
-       } else if (strcmp(page,"viewconfig")==0) {
+       } else if (have_read_access && strcmp(page,"viewconfig")==0) {
                viewconfig_page();
        } else if (strcmp(page,"passwd")==0) {
                passwd_page();