better layout of password options.
[samba.git] / source / web / swat.c
index 30150815fe69694d0889d3e674d0bc7a9c1a8b04..8538082a1384c3dedbc736dba51b62f4133b2028 100644 (file)
 
 static pstring servicesf = CONFIGFILE;
 static BOOL demo_mode = False;
+static BOOL have_write_access = False;
 
 /*
  * Password Management Globals
  */
-#define USER "username"
+#define SWAT_USER "username"
 #define OLD_PSWD "old_passwd"
 #define NEW_PSWD "new_passwd"
 #define NEW2_PSWD "new2_passwd"
@@ -105,7 +106,7 @@ static char *make_parm_name(char *label)
 ****************************************************************************/
 static int include_html(char *fname)
 {
-       FILE *f = fopen(fname,"r");
+       FILE *f = sys_fopen(fname,"r");
        char buf[1024];
        int ret;
 
@@ -162,7 +163,7 @@ static void show_parameter(int snum, struct parm_struct *parm)
                ptr = lp_local_ptr(snum, ptr);
        }
 
-       printf("<tr><td><A HREF=\"/swat/help/smb.conf.5.html#%s\">?</A> %s</td><td>", 
+       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);
 
        switch (parm->type) {
@@ -317,7 +318,7 @@ static int save_reload(void)
 {
        FILE *f;
 
-       f = fopen(servicesf,"w");
+       f = sys_fopen(servicesf,"w");
        if (!f) {
                printf("failed to open %s for writing\n", servicesf);
                return 0;
@@ -381,12 +382,9 @@ static void commit_parameters(int snum)
 /****************************************************************************
   load the smb.conf file into loadparm.
 ****************************************************************************/
-static void load_config(void)
+static BOOL load_config(void)
 {
-       if (!lp_load(servicesf,False,True,False)) {
-               printf("<b>Can't load %s - using defaults</b><p>\n", 
-                      servicesf);
-       }
+       return lp_load(servicesf,False,True,False);
 }
 
 /****************************************************************************
@@ -406,16 +404,11 @@ static void show_main_buttons(void)
 {
        image_link("Home", "", "images/home.gif");
 
-       /* Root gets full functionality */
-       if (demo_mode || am_root()) {
-               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");
-       }
-
-       /* Everyone gets this functionality */
+       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("Password Management", "passwd", "images/passwd.gif");
 
        printf("<HR>\n");
@@ -474,7 +467,10 @@ static void globals_page(void)
 
        printf("<FORM name=\"swatform\" method=post>\n");
 
-       printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
+       if (have_write_access) {
+               printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\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");
@@ -554,7 +550,10 @@ static void shares_page(void)
 
 
        if (snum >= 0) {
-               printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
+               if (have_write_access) {
+                       printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
+               }
+
                printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
                if (advanced == 0) {
                        printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
@@ -601,7 +600,7 @@ static BOOL change_password(const char *remote_machine, char *user_name,
                return ret;
        }
 
-       if(!initialize_password_db()) {
+       if(!initialise_password_db()) {
                printf("Can't setup password database vectors.\n<p>");
                return False;
        }
@@ -627,7 +626,7 @@ static void chg_passwd(void)
        BOOL rslt;
 
        /* Make sure users name has been specified */
-       if (strlen(cgi_variable(USER)) == 0) {
+       if (strlen(cgi_variable(SWAT_USER)) == 0) {
                printf("<p> Must specify \"User Name\" \n");
                return;
        }
@@ -642,7 +641,7 @@ 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() == False) && (strlen( cgi_variable(OLD_PSWD)) <= 0)) ||
+               if (((!am_root()) && (strlen( cgi_variable(OLD_PSWD)) <= 0)) ||
                    ((cgi_variable(CHG_R_PASSWD_FLAG)) &&  (strlen( cgi_variable(OLD_PSWD)) <= 0))) {
                        printf("<p> Must specify \"Old Password\" \n");
                        return;
@@ -676,7 +675,7 @@ static void chg_passwd(void)
                host = "127.0.0.1";
        }
        rslt = change_password(host,
-                              cgi_variable(USER),
+                              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,
@@ -684,9 +683,9 @@ static void chg_passwd(void)
 
 
        if (rslt == True) {
-               printf("<p> The passwd for '%s' has been changed. \n", cgi_variable(USER));
+               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(USER));
+               printf("<p> The passwd for '%s' has NOT been changed. \n",cgi_variable(SWAT_USER));
        }
        
        return;
@@ -697,14 +696,14 @@ static void chg_passwd(void)
 ****************************************************************************/
 static void passwd_page(void)
 {
-       char *new_name = get_user_name();
+       char *new_name = cgi_user_name();
 
        /* 
         * After the first time through here be nice. If the user
         * changed the User box text to another users name, remember it.
         */
-       if (cgi_variable(USER)) {
-               new_name = cgi_variable(USER);
+       if (cgi_variable(SWAT_USER)) {
+               new_name = cgi_variable(SWAT_USER);
        } 
 
        if (!new_name) new_name = "";
@@ -719,8 +718,8 @@ static void passwd_page(void)
         * Create all the dialog boxes for data collection
         */
        printf("<tr><td> User Name : </td>\n");
-       printf("<td><input type=text size=30 name=%s value=%s></td></tr> \n", USER, new_name);
-       if (am_root() == False) {
+       printf("<td><input type=text size=30 name=%s value=%s></td></tr> \n", SWAT_USER, new_name);
+       if (!am_root()) {
                printf("<tr><td> Old Password : </td>\n");
                printf("<td><input type=password size=30 name=%s></td></tr> \n",OLD_PSWD);
        }
@@ -728,30 +727,32 @@ 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);
-       if (am_root() == True) {
-               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=\"Change Password\">\n", 
+              CHG_S_PASSWD_FLAG);
+       if (demo_mode || am_root()) {
+               printf("<input type=submit name=%s value=\"Add New User\">\n",
+                      ADD_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.
-        */
+        * 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(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");
@@ -762,7 +763,7 @@ static void passwd_page(void)
         * Create all the dialog boxes for data collection
         */
        printf("<tr><td> User Name : </td>\n");
-       printf("<td><input type=text size=30 name=%s value=%s></td></tr>\n",USER, new_name);
+       printf("<td><input type=text size=30 name=%s value=%s></td></tr>\n",SWAT_USER, new_name);
        printf("<tr><td> Old Password : </td>\n");
        printf("<td><input type=password size=30 name=%s></td></tr>\n",OLD_PSWD);
        printf("<tr><td> New Password : </td>\n");
@@ -772,23 +773,25 @@ static void passwd_page(void)
        printf("<tr><td> Remote Machine : </td>\n");
        printf("<td><input type=password 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");
 }
 
 /****************************************************************************
@@ -853,7 +856,9 @@ static void printers_page(void)
 
 
        if (snum >= 0) {
-               printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
+               if (have_write_access) {
+                       printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
+               }
                printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
                if (advanced == 0) {
                        printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
@@ -890,7 +895,7 @@ static void printers_page(void)
        /* just in case it goes wild ... */
        alarm(300);
 
-       dbf = fopen("/dev/null", "w");
+       dbf = sys_fopen("/dev/null", "w");
 
        if (!dbf) dbf = stderr;
 
@@ -905,49 +910,40 @@ static void printers_page(void)
                }
        }
 
+       charset_initialise();
+       load_config();
+
        cgi_setup(SWATDIR, !demo_mode);
 
        print_header();
        
-       charset_initialise();
-
-       /* if this binary is setuid then run completely as root */
-       setuid(0);
-
-       load_config();
-
        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 (demo_mode || am_root()) {
-               if (strcmp(page, "globals")==0) {
-                       globals_page();
-               } else if (strcmp(page,"shares")==0) {
-                       shares_page();
-               } else if (strcmp(page,"printers")==0) {
-                       printers_page();
-               } else if (strcmp(page,"status")==0) {
-                       status_page();
-               } else if (strcmp(page,"viewconfig")==0) {
-                       viewconfig_page();
-               } else if (strcmp(page,"passwd")==0) {
-                       passwd_page();
-               } else {
-                       welcome_page();
-               }
+       if (strcmp(page, "globals")==0) {
+               globals_page();
+       } else if (strcmp(page,"shares")==0) {
+               shares_page();
+       } else if (strcmp(page,"printers")==0) {
+               printers_page();
+       } else if (strcmp(page,"status")==0) {
+               status_page();
+       } else if (strcmp(page,"viewconfig")==0) {
+               viewconfig_page();
+       } else if (strcmp(page,"passwd")==0) {
+               passwd_page();
        } else {
-               /* Everyone gets this functionality */
-               if (strcmp(page,"passwd")==0) {
-                       passwd_page();
-               } else {
-                       welcome_page();
-               }
+               welcome_page();
        }
-       
+
        print_footer();
        return 0;
 }