r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[ira/wip.git] / source3 / web / swat.c
index 4082574e442fc7345d92386e8b255a305dd62ed8..25dc9306ea63ce8ff54b2b716a2970753b12e3a0 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -16,8 +16,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /**
@@ -103,7 +102,7 @@ static char *stripspaceupper(const char *str)
        char *p = newstring;
 
        while (*str) {
-               if (*str != ' ') *p++ = toupper(*str);
+               if (*str != ' ') *p++ = toupper_ascii(*str);
                ++str;
        }
        *p = '\0';
@@ -188,12 +187,12 @@ static const char* get_parm_translated(
        if(strcmp(pLabel, pTranslated) != 0)
        {
                pstr_sprintf(output,
-                 "<A HREF=\"/swat/help/smb.conf.5.html#%s\" target=\"docs\"> %s</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s <br><span class=\"i18n_translated_parm\">%s</span>",
+                 "<A HREF=\"/swat/help/manpages/smb.conf.5.html#%s\" target=\"docs\"> %s</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s <br><span class=\"i18n_translated_parm\">%s</span>",
                   pAnchor, pHelp, pLabel, pTranslated);
                return output;
        }
        pstr_sprintf(output, 
-         "<a href=\"/swat/help/smb.conf.5.html#%s\" target=\"docs\" class=\"help_link\"> %s</a> %s",
+         "<A HREF=\"/swat/help/manpages/smb.conf.5.html#%s\" target=\"docs\"> %s</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %s",
          pAnchor, pHelp, pLabel);
        return output;
 }
@@ -220,7 +219,7 @@ static void show_parameter(int snum, struct parm_struct *parm)
                ptr = lp_local_ptr(snum, ptr);
        }
 
-       printf("<tr><td width=\"230\">%s</td><td>", get_parm_translated(stripspaceupper(parm->label), _("Help"), parm->label));
+       printf("<tr><td>%s</td><td>", get_parm_translated(stripspaceupper(parm->label), _("Help"), parm->label));
        switch (parm->type) {
        case P_CHAR:
                printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
@@ -230,7 +229,7 @@ static void show_parameter(int snum, struct parm_struct *parm)
                break;
 
        case P_LIST:
-               printf("<input type=text size=30 name=\"parm_%s\" value=\"",
+               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;
@@ -268,7 +267,7 @@ static void show_parameter(int snum, struct parm_struct *parm)
        case P_STRING:
        case P_USTRING:
                push_utf8_allocate(&utf8_s1, *(char **)ptr);
-               printf("<input type=text size=30 name=\"parm_%s\" value=\"%s\">",
+               printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
                       make_parm_name(parm->label), fix_quotes(utf8_s1));
                SAFE_FREE(utf8_s1);
                printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
@@ -278,7 +277,7 @@ static void show_parameter(int snum, struct parm_struct *parm)
        case P_GSTRING:
        case P_UGSTRING:
                push_utf8_allocate(&utf8_s1, (char *)ptr);
-               printf("<input type=text size=30 name=\"parm_%s\" value=\"%s\">",
+               printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
                       make_parm_name(parm->label), fix_quotes(utf8_s1));
                SAFE_FREE(utf8_s1);
                printf("<input type=button value=\"%s\" onClick=\"swatform.parm_%s.value=\'%s\'\">",
@@ -422,7 +421,7 @@ static void show_parameters(int snum, int allparameters, unsigned int parm_filte
 static BOOL load_config(BOOL save_def)
 {
        lp_resetnumservices();
-       return lp_load(dyn_CONFIGFILE,False,save_def,False);
+       return lp_load(dyn_CONFIGFILE,False,save_def,False,True);
 }
 
 /****************************************************************************
@@ -432,7 +431,7 @@ 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(False));
+       fprintf(f, "# Date: %s\n\n", current_timestring(False));
        
        lp_dump(f, show_defaults, iNumNonAutoPrintServices);
 }
@@ -515,7 +514,7 @@ static void commit_parameters(int snum)
 
        while ((parm = lp_next_parameter(snum, &i, 1))) {
                slprintf(label, sizeof(label)-1, "parm_%s", make_parm_name(parm->label));
-               if ((v = cgi_variable(label))) {
+               if ((v = cgi_variable(label)) != NULL) {
                        if (parm->flags & FLAG_HIDE) continue;
                        commit_parameter(snum, parm, v); 
                }
@@ -523,50 +522,42 @@ static void commit_parameters(int snum)
 }
 
 /****************************************************************************
-  generate html for rollovers
+  spit out the html for a link with an image 
 ****************************************************************************/
-static void rollover_link(const char *name, const char *id, const char *page)
+static void image_link(const char *name, const char *hlink, const char *src)
 {
-       if ( strcmp(page, id)==0 ) {
-               printf("    <img src=\"/swat/images/%s_flat.png\" alt=\"%s\" />\n", 
-                  id, name);
-       } else {
-               printf("    <a href=\"%s/%s\" onmouseover=\"swapImg('%s','%sOver')\" onmouseout=\"swapImg('%s','%sLink')\"><img src=\"/swat/images/%s_link.png\" name=\"%s\" alt=\"%s\" /></a>\n",
-              cgi_baseurl(), id, id, id, id, id, id, id, name);
-       }
+       printf("<A HREF=\"%s/%s\"><img border=\"0\" src=\"/swat/%s\" alt=\"%s\"></A>\n", 
+              cgi_baseurl(), hlink, src, name);
 }
 
 /****************************************************************************
   display the main navigation controls at the top of each page along
   with a title 
 ****************************************************************************/
-static void show_main_buttons(const char *page)
+static void show_main_buttons(void)
 {
        char *p;
        
-       printf("  <div id=\"nav\">\n");
+       if ((p = cgi_user_name()) && strcmp(p, "root")) {
+               printf(_("Logged in as <b>%s</b>"), p);
+               printf("<p>\n");
+       }
 
+       image_link(_("Home"), "", "images/home.gif");
        if (have_write_access) {
-               rollover_link(_("Configure"), "conf", page);
-               rollover_link(_("Services"), "services", page);
+               image_link(_("Globals"), "globals", "images/globals.gif");
+               image_link(_("Shares"), "shares", "images/shares.gif");
+               image_link(_("Printers"), "printers", "images/printers.gif");
+               image_link(_("Wizard"), "wizard", "images/wizard.gif");
        }
-   
-       /* root always gets all buttons, otherwise look for -P */
+   /* root always gets all buttons, otherwise look for -P */
        if ( have_write_access || (!passwd_only && have_read_access) ) {
-               rollover_link(_("Status"), "status", page);
-       }
-       rollover_link(_("Password Management"), "passwd", page);
-       
-       printf("  </div>\n\n");
-       
-       /* Wrap the rest in a control div */
-       printf("  <div id=\"controls\">\n\n");
-
-       if ((p = cgi_user_name()) && strcmp(p, "root")) {
-               printf(_("Logged in as <b>%s</b>"), p);
-               printf("<p>\n");
+               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");
 }
 
 /****************************************************************************
@@ -584,49 +575,17 @@ static void ViewModeBoxes(int mode)
 }
 
 /****************************************************************************
-  display a welcome page (Read-only users under passwd only get a unique welcome)
+  display a welcome page  
 ****************************************************************************/
 static void welcome_page(void)
 {
-       if (passwd_only && !have_write_access) {
-               include_html("help/welcome_passwd_only.html");
-       } else {
+       if (file_exist("help/welcome.html", NULL)) {
                include_html("help/welcome.html");
+       } else {
+               include_html("help/welcome-no-samba-doc.html");
        }
 }
 
-/****************************************************************************
-  display help page  
-****************************************************************************/
-static void help_page(void)
-{
-       include_html("help/docs.html");
-}
-
-/****************************************************************************
-  display shares and printers links from an overall services page
-****************************************************************************/
-static void services_page(void)
-{
-       printf("  <div class=\"whereto\">\n");
-       printf("    <h2>File and Printer Shares</h2>\n\n");
-       printf("    <p>Follow the links below to edit service-level parameters for file and printer shares.</p>\n");
-       printf("  </div>\n\n");
-
-       printf("  <div class=\"view_conf\"><a href=\"viewconfig\" onclick=\"openHelp(this.href); return false\">View smb.conf file</a></div>\n\n");
-
-       printf("  <div class=\"services_opts\">\n");
-       printf("    <ul>\n");
-       printf("      <li><a href=\"shares\">File Shares</a></li>\n");
-       printf("      <li><a href=\"printers\">Printer Shares</a></li>\n");
-       printf("    </ul>\n");
-       printf("  </div>\n\n");
-
-       printf("  <div>\n");
-       printf("    <p>Shares may also be added via the links above.</p>\n");
-       printf("  </div>\n\n");
-}
-
 /****************************************************************************
   display the current smb.conf  
 ****************************************************************************/
@@ -692,9 +651,7 @@ static void rewritecfg_file(void)
 {
        commit_parameters(GLOBAL_SECTION_SNUM);
        save_reload(0);
-       printf("<h2>Samba Configuration Saved</h2>");
-       printf("<p>%s</p>\n", _("Note: smb.conf file has been read and rewritten"));
-       printf("<p>Return to the <a href=\"javascript:history.go(-1)\">previous page</a>.\n");
+       printf("<H2>%s</H2>\n", _("Note: smb.conf file has been read and rewritten"));
 }
 
 /****************************************************************************
@@ -720,10 +677,10 @@ static void wizard_page(void)
        }
 
        if (cgi_variable("Commit")){
-               SerType = atoi(cgi_variable("ServerType"));
-               winstype = atoi(cgi_variable("WINSType"));
+               SerType = atoi(cgi_variable_nonull("ServerType"));
+               winstype = atoi(cgi_variable_nonull("WINSType"));
                have_home = lp_servicenumber(HOMES_NAME);
-               HomeExpo = atoi(cgi_variable("HomeExpo"));
+               HomeExpo = atoi(cgi_variable_nonull("HomeExpo"));
 
                /* Plain text passwords are too badly broken - use encrypted passwords only */
                lp_do_parameter( GLOBAL_SECTION_SNUM, "encrypt passwords", "Yes");
@@ -756,7 +713,7 @@ static void wizard_page(void)
                                break;
                        case 2:
                                lp_do_parameter( GLOBAL_SECTION_SNUM, "wins support", "No" );
-                               lp_do_parameter( GLOBAL_SECTION_SNUM, "wins server", cgi_variable("WINSAddr"));
+                               lp_do_parameter( GLOBAL_SECTION_SNUM, "wins server", cgi_variable_nonull("WINSAddr"));
                                break;
                }
 
@@ -806,7 +763,7 @@ static void wizard_page(void)
        printf("<form method=post action=wizard>\n");
 
        if (have_write_access) {
-               printf("%s\n", _("The &quot;Rewrite smb.conf file&quot; button will clear the smb.conf file of all default values and of comments."));
+               printf("%s\n", _("The \"Rewrite smb.conf file\" button will clear the smb.conf file of all default values and of comments."));
                printf("%s", _("The same will happen if you press the commit button."));
                printf("<br><br>\n");
                printf("<center>");
@@ -866,19 +823,14 @@ static void wizard_page(void)
 
 
 /****************************************************************************
-  display a conf page for editing global parameters 
+  display a globals editing page  
 ****************************************************************************/
-static void conf_page(void)
+static void globals_page(void)
 {
        unsigned int parm_filter = FLAG_BASIC;
        int mode = 0;
 
-       printf("  <div class=\"whereto\">\n");
-       printf("    <h2>Configuring Samba</h2>\n\n");
-       printf("    <p>The following menu allows for editing of global parameters affecting your Samba configuration.</p>\n");
-       printf("  </div>\n\n");
-
-       printf("  <div class=\"view_conf\"><a href=\"viewconfig\" onclick=\"openHelp(this.href); return false\">View smb.conf file</a></div>\n\n");
+       printf("<H2>%s</H2>\n", _("Global Parameters"));
 
        if (cgi_variable("Commit")) {
                commit_parameters(GLOBAL_SECTION_SNUM);
@@ -886,13 +838,13 @@ static void conf_page(void)
        }
 
        if ( cgi_variable("ViewMode") )
-               mode = atoi(cgi_variable("ViewMode"));
+               mode = atoi(cgi_variable_nonull("ViewMode"));
        if ( cgi_variable("BasicMode"))
                mode = 0;
        if ( cgi_variable("AdvMode"))
                mode = 1;
 
-       printf("<form name=\"swatform\" method=post action=conf>\n");
+       printf("<form name=\"swatform\" method=post action=globals>\n");
 
        ViewModeBoxes( mode );
        switch ( mode ) {
@@ -936,8 +888,6 @@ static void shares_page(void)
                snum = lp_servicenumber(share);
 
        printf("<H2>%s</H2>\n", _("Share Parameters"));
-       
-       printf("  <div class=\"view_conf\"><a href=\"services\">Return to Services Page</a><a href=\"viewconfig\" onclick=\"openHelp(this.href); return false\">View smb.conf file</a></div>\n\n");
 
        if (cgi_variable("Commit") && snum >= 0) {
                commit_parameters(snum);
@@ -964,7 +914,7 @@ static void shares_page(void)
        printf("<table>\n");
 
        if ( cgi_variable("ViewMode") )
-               mode = atoi(cgi_variable("ViewMode"));
+               mode = atoi(cgi_variable_nonull("ViewMode"));
        if ( cgi_variable("BasicMode"))
                mode = 0;
        if ( cgi_variable("AdvMode"))
@@ -1035,7 +985,7 @@ static BOOL change_password(const char *remote_machine, const char *user_name,
                            const char *old_passwd, const char *new_passwd, 
                                int local_flags)
 {
-       BOOL ret = False;
+       NTSTATUS ret;
        pstring err_str;
        pstring msg_str;
 
@@ -1049,10 +999,10 @@ static BOOL change_password(const char *remote_machine, const char *user_name,
                                                                         new_passwd, err_str, sizeof(err_str));
                if(*err_str)
                        printf("%s\n<p>", err_str);
-               return ret;
+               return NT_STATUS_IS_OK(ret);
        }
 
-       if(!initialize_password_db(True)) {
+       if(!initialize_password_db(True, NULL)) {
                printf("%s\n<p>", _("Can't setup password database vectors."));
                return False;
        }
@@ -1065,7 +1015,7 @@ static BOOL change_password(const char *remote_machine, const char *user_name,
        if(*err_str)
                printf("%s\n<p>", err_str);
 
-       return ret;
+       return NT_STATUS_IS_OK(ret);
 }
 
 /****************************************************************************
@@ -1078,7 +1028,7 @@ static void chg_passwd(void)
        int local_flags = 0;
 
        /* Make sure users name has been specified */
-       if (strlen(cgi_variable(SWAT_USER)) == 0) {
+       if (strlen(cgi_variable_nonull(SWAT_USER)) == 0) {
                printf("<p>%s\n", _(" Must specify \"User Name\" "));
                return;
        }
@@ -1093,27 +1043,27 @@ static void chg_passwd(void)
                 * If current user is not root, make sure old password has been specified 
                 * If REMOTE change, even root must provide old password 
                 */
-               if (((!am_root()) && (strlen( cgi_variable(OLD_PSWD)) <= 0)) ||
-                   ((cgi_variable(CHG_R_PASSWD_FLAG)) &&  (strlen( cgi_variable(OLD_PSWD)) <= 0))) {
+               if (((!am_root()) && (strlen( cgi_variable_nonull(OLD_PSWD)) <= 0)) ||
+                   ((cgi_variable(CHG_R_PASSWD_FLAG)) &&  (strlen( cgi_variable_nonull(OLD_PSWD)) <= 0))) {
                        printf("<p>%s\n", _(" Must specify \"Old Password\" "));
                        return;
                }
 
                /* If changing a users password on a remote hosts we have to know what host */
-               if ((cgi_variable(CHG_R_PASSWD_FLAG)) && (strlen( cgi_variable(RHOST)) <= 0)) {
+               if ((cgi_variable(CHG_R_PASSWD_FLAG)) && (strlen( cgi_variable_nonull(RHOST)) <= 0)) {
                        printf("<p>%s\n", _(" Must specify \"Remote Machine\" "));
                        return;
                }
 
                /* Make sure new passwords have been specified */
-               if ((strlen( cgi_variable(NEW_PSWD)) <= 0) ||
-                   (strlen( cgi_variable(NEW2_PSWD)) <= 0)) {
+               if ((strlen( cgi_variable_nonull(NEW_PSWD)) <= 0) ||
+                   (strlen( cgi_variable_nonull(NEW2_PSWD)) <= 0)) {
                        printf("<p>%s\n", _(" Must specify \"New, and Re-typed Passwords\" "));
                        return;
                }
 
                /* Make sure new passwords was typed correctly twice */
-               if (strcmp(cgi_variable(NEW_PSWD), cgi_variable(NEW2_PSWD)) != 0) {
+               if (strcmp(cgi_variable_nonull(NEW_PSWD), cgi_variable_nonull(NEW2_PSWD)) != 0) {
                        printf("<p>%s\n", _(" Re-typed password didn't match new password "));
                        return;
                }
@@ -1140,17 +1090,17 @@ static void chg_passwd(void)
        
 
        rslt = change_password(host,
-                              cgi_variable(SWAT_USER),
-                              cgi_variable(OLD_PSWD), cgi_variable(NEW_PSWD),
+                              cgi_variable_nonull(SWAT_USER),
+                              cgi_variable_nonull(OLD_PSWD), cgi_variable_nonull(NEW_PSWD),
                                   local_flags);
 
        if(cgi_variable(CHG_S_PASSWD_FLAG)) {
                printf("<p>");
                if (rslt == True) {
-                       printf(_(" The passwd for '%s' has been changed."), cgi_variable(SWAT_USER));
+                       printf(_(" The passwd for '%s' has been changed."), cgi_variable_nonull(SWAT_USER));
                        printf("\n");
                } else {
-                       printf(_(" The passwd for '%s' has NOT been changed."), cgi_variable(SWAT_USER));
+                       printf(_(" The passwd for '%s' has NOT been changed."), cgi_variable_nonull(SWAT_USER));
                        printf("\n");
                }
        }
@@ -1170,7 +1120,7 @@ static void passwd_page(void)
         * changed the User box text to another users name, remember it.
         */
        if (cgi_variable(SWAT_USER)) {
-               new_name = cgi_variable(SWAT_USER);
+               new_name = cgi_variable_nonull(SWAT_USER);
        } 
 
        if (!new_name) new_name = "";
@@ -1279,8 +1229,6 @@ static void printers_page(void)
                snum = lp_servicenumber(share);
 
         printf("<H2>%s</H2>\n", _("Printer Parameters"));
-
-       printf("  <div class=\"view_conf\"><a href=\"services\">Return to Services Page</a><a href=\"viewconfig\" onclick=\"openHelp(this.href); return false\">View smb.conf file</a></div>\n\n");
  
         printf("<H3>%s</H3>\n", _("Important Note:"));
         printf(_("Printer names marked with [*] in the Choose Printer drop-down box "));
@@ -1316,7 +1264,7 @@ static void printers_page(void)
        printf("<FORM name=\"swatform\" method=post>\n");
 
        if ( cgi_variable("ViewMode") )
-               mode = atoi(cgi_variable("ViewMode"));
+               mode = atoi(cgi_variable_nonull("ViewMode"));
         if ( cgi_variable("BasicMode"))
                 mode = 0;
         if ( cgi_variable("AdvMode"))
@@ -1426,8 +1374,11 @@ static void printers_page(void)
 
        poptFreeContext(pc);
 
+       load_case_tables();
+
        setup_logging(argv[0],False);
        load_config(True);
+       load_interfaces();
        iNumNonAutoPrintServices = lp_numservices();
        load_printers();
 
@@ -1450,32 +1401,29 @@ static void printers_page(void)
                have_read_access = (access(dyn_CONFIGFILE,R_OK) == 0);
        }
 
-       page = cgi_pathinfo();
+       show_main_buttons();
 
-       show_main_buttons(page);
+       page = cgi_pathinfo();
 
-       if (have_read_access && strcmp(page,"conf")==0) { 
-               conf_page();
-       } else if (have_read_access && strcmp(page,"viewconfig")==0) {
-               viewconfig_page();
-       } else if (have_read_access && strcmp(page,"rewritecfg")==0) {
-               rewritecfg_file();
-       } else if (have_read_access && strcmp(page,"services")==0) {
-               services_page();
+       /* Root gets full functionality */
+       if (have_read_access && strcmp(page, "globals")==0) {
+               globals_page();
        } else if (have_read_access && strcmp(page,"shares")==0) {
                shares_page();
        } else if (have_read_access && strcmp(page,"printers")==0) {
                printers_page();
        } else if (have_read_access && strcmp(page,"status")==0) {
                status_page();
+       } else if (have_read_access && strcmp(page,"viewconfig")==0) {
+               viewconfig_page();
        } else if (strcmp(page,"passwd")==0) {
                passwd_page();
        } else if (have_read_access && strcmp(page,"wizard")==0) {
                wizard_page();
        } else if (have_read_access && strcmp(page,"wizard_params")==0) {
                wizard_params_page();
-       } else if (have_read_access && strcmp(page,"help")==0) {
-               help_page();
+       } else if (have_read_access && strcmp(page,"rewritecfg")==0) {
+               rewritecfg_file();
        } else {
                welcome_page();
        }