<%!

/**********************************************************************
**
**     SAP AG Walldorf
**     (C) Copyright SAP AG 1999-2010
**
***********************************************************************/

#include "publicincl/tools.inc"

struct icm_change_user_type
{
  string auth_file;
  string user;
  string group;
  string cert;
  string passwd;
  string passwd_new;
};

struct icm_adm_status_type
{
  string auth_file;
  string host;
  string hostaddr;
  string rhost;
  string rhostaddr;
  int port;
};

struct icm_auth_status_type
{  
  boolean active;
  string perm_file;
  string auth_file;
  string filt_profile;
  string filt_pattern;
};

static int checkinput (string user, string group, string pw1, string user_orig, string group_orig, string pw2)
{
  if (user == "")
    return (-257);
  else if (pw1 != pw2)
    return (-256);
  else if ((pw1 != "") && (pw1 != "x") && (strlen (pw1) < 5))
    return (-258);
  else if (user != user_orig)
    return (-260);
  else if (group != group_orig)
    return (-261);
  else if (strstr(pw1,"<")!="" || strstr(pw1,">")!="" || strstr(pw1,"&")!="" || strstr(pw1,"\"")!="")
    return (-262);
  else
    return (0);
}
%>

<%
  int user_count;
  int nr, rc, row;
  string what, hdlsel, browser, fld, is_checked, opstr,op;
  string user, passwd, afile;
  boolean is_admin;
  string token;
  string msg;
  string icon;


  struct icm_user_type icm_user_info[];
  struct icm_change_user_type icm_user;

  struct icm_adm_status_type icm_adm_status;
  struct icm_auth_status_type icm_auth_status;

  is_admin = __icm_get_system_property ("is_admin");
  browser = __icm_get_browser_type();
  what = anumenc(__icm_get_form_field ("what"));
  hdlsel = anumenc(__icm_get_form_field ("hdlsel"));
  afile = anumenc(__icm_get_form_field ("afile"));
  if (afile == "admin") {
    __icm_adm_hdl (1, icm_adm_status);
    icm_user.auth_file = icm_adm_status.auth_file;
  }
  else {
    __icm_auth_hdl (1, hdlsel, icm_auth_status);
    icm_user.auth_file = icm_auth_status.auth_file;
  }

  fld = anumenc(__icm_get_form_field ("row_selected"));
  if (fld == "")
    row = 0;
  else
    row = fld;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <link rel=stylesheet type="text/css" href="../public/css/controls_<%=browser%>.css">
    <link rel=stylesheet type="text/css" href="../public/css/sapbsp.css">
    <title>User administration</title>
    <script language="JavaScript" type="text/javascript">
      <!--
      function callSubmitAction(form,value,token)
      {
	var Form = document[form];
      	Form.what.value = value;
      	Form.token.value = token;
      	Form.submit();
      }
      -->
    </script>
  </head>

  <body class="sapBdy">
<%
   if (what == "add_user") {
     icm_user.user = anumenc(__icm_get_form_field ("user"));
     icm_user.group = anumenc(__icm_get_form_field ("group"));
     icm_user.cert = __icm_get_form_field ("cert");
     icm_user.passwd = __icm_get_form_field ("passwd_new1",1);
     icm_user.passwd_new = "";

     token = __icm_get_form_field ("token");

     if (icm_user.passwd == "")
	rc = -258;
     else
	rc = checkinput (icm_user.user, icm_user.group, icm_user.passwd,
			__icm_get_form_field ("user"), 
			__icm_get_form_field ("group"), 
			__icm_get_form_field ("passwd_new2",1));
     if (rc == 0)
       rc = __icm_execute (11, icm_user, token);

    if (rc == 0)
    {
	msg = "User successfully created. Changes will become active in the next 30 seconds.";
	icon = "s_m_info.gif";
    }
    else if (rc == -256)
    {
	msg = "Specified passwords are different";
	icon = "s_m_warn.gif";
    }
    else if (rc == -257)
    {
	msg = "No user name specified";
	icon = "s_m_warn.gif";
    }
    else if (rc == -258)
    {
	msg = "Password is too short (should be at least 5 chars)";
	icon = "s_m_warn.gif";
    }
    else if (rc == -259)
    {
	msg = "Old and new passwords are identical";
	icon = "s_m_warn.gif";
    }
    else if (rc == -260)
    {
	msg = "User name contains invalid characters. Use characters from the set [a-zA-Z0-9_-]";
	icon = "s_m_warn.gif";
    }
    else if (rc == -261)
    {
	msg = "Group name contains invalid characters. Use characters from the set [a-zA-Z0-9_-]";
	icon = "s_m_warn.gif";
    }
    else if (rc == -262)
    {
	msg = "Password contains invalid characters. The following characters are not allowed: <>&\"";
	icon = "s_m_warn.gif";
    }
    else if (rc == -11) 
    {
	msg = "Session timeout. Please retry operation.";
	icon = "s_m_warn.gif";
    }
    else
    {
	msg = "User could not be created: " +  __icm_get_error_text(rc) + " (rc=" + rc + ")";
	icon = "s_m_erro.gif";
    }
%>

      <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="50%">
	<tr>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>">
	  </td>
	  <td class="sapTbvCellAlt" Colspan="2" align="center">
	    <b><i>Information</i></b>
	  </td>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>">
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <b><i><%=msg%></i></b>
	  </td>
	</tr>
	<tr><td class="sapTbvCellStd" Colspan="4"></td></tr>
	<tr><td class="sapTbvCellStd" Colspan="4"></td></tr>

	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <a href="./user_admin.icp?afile=<%=afile%>&hdlsel=<%=hdlsel%>" class="sapBtnStd"><nobr>Back</nobr></a>
	  </td>
	</tr>
      </table><p>
<%
   }
   else if ((what == "do_change_user") || (what == "do_del_user") || (what == "do_lock_user") || (what == "do_unlock_user")) {
     user = anumenc(__icm_get_form_field ("user"));
     icm_user.user = user;

     token = __icm_get_form_field ("token");

     if (what == "do_change_user") {
       icm_user.passwd = __icm_get_form_field ("passwd",1);
       icm_user.passwd_new = __icm_get_form_field ("passwd_new1",1);
       icm_user.group = anumenc(__icm_get_form_field ("group"));
       icm_user.cert = __icm_get_form_field ("cert");

       opstr = "changed";
       rc = checkinput (icm_user.user, icm_user.group, icm_user.passwd_new,
			icm_user.user,
			__icm_get_form_field ("group"),
			__icm_get_form_field ("passwd_new2",1));
       if (rc == 0)
	 if ((icm_user.passwd != "") && (icm_user.passwd == icm_user.passwd_new))
	   rc = -259;
	 else
           rc = __icm_execute (13, icm_user, token);
     } else if (what == "do_lock_user") {
       opstr = "locked";
       rc = __icm_execute (22, icm_user, token);
     } else if (what == "do_unlock_user") {
       opstr = "unlocked";
       rc = __icm_execute (23, icm_user, token);
     } else {
       opstr = "deleted";
       rc = __icm_execute (12, icm_user, token);
     }

    if (rc == 0)
    {
	msg = "User successfully " + opstr + ". Changes will become active in the next 30 seconds.";
	icon = "s_m_info.gif";
    }
    else if (rc == -256)
    {
	msg = "Specified passwords are different";
	icon = "s_m_warn.gif";
    }
    else if (rc == -257)
    {
	msg = "No user name specified";
	icon = "s_m_warn.gif";
    }
    else if (rc == -258)
    {
	msg = "New password is too short (should be at least 5 chars)";
	icon = "s_m_warn.gif";
    }
    else if (rc == -259)
    {
	msg = "Old and new passwords are identical";
	icon = "s_m_warn.gif";
    }
    else if (rc == -260)
    {
	msg = "User name contains invalid characters. Use characters from the set [a-zA-Z0-9_-]";
	icon = "s_m_warn.gif";
    }
    else if (rc == -261)
    {
	msg = "Group name contains invalid characters. Use characters from the set [a-zA-Z0-9_-]";
	icon = "s_m_warn.gif";
    }
    else if (rc == -262)
    {
	msg = "Password contains invalid characters. The following characters are not allowed: <>&\"";
	icon = "s_m_warn.gif";
    }
    else if (rc == -11) 
    {
	msg = "Session timeout. Please retry operation.";
	icon = "s_m_warn.gif";
    }
    else if (rc == -13) 
    {
	msg = "Permission denied.";
	icon = "s_m_warn.gif";
    }
    else
    {
	msg = "User could not be " + opstr + ": " +  __icm_get_error_text(rc) + "(" + rc + ")";
	icon = "s_m_erro.gif";
    }
%>

      <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="50%">
	<tr>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>">
	  </td>
	  <td class="sapTbvCellAlt" Colspan="2" align="center">
	    <b><i>Information</i></b>
	  </td>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/<%=icon%>">
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <b><i><%=msg%></i></b>
	  </td>
	</tr>
	<tr><td class="sapTbvCellStd" Colspan="4"></td></tr>
	<tr><td class="sapTbvCellStd" Colspan="4"></td></tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <a href="./user_admin.icp?afile=<%=afile%>&hdlsel=<%=hdlsel%>" class="sapBtnStd"><nobr>Back</nobr></a>
	  </td>
	</tr>
      </table><p>
<%
   }
   else if ((what == "del_user") || (what == "lock_user") || (what == "unlock_user")) {
     user_count = __icm_get_user_info(icm_user.auth_file, icm_user_info);
     if ((row < 0) || (row >= user_count))
	row = 0;
     user = icm_user_info[row].user;
     if (what == "del_user") { 
	opstr = "delete";op="do_del_user"; 
	token = __icm_get_token(12);
     }
     else if (what == "lock_user") { 
	opstr = "lock";op="do_lock_user"; 
	token = __icm_get_token(22);
     }
     else { 
	opstr = "unlock";op="do_unlock_user"; 
	token = __icm_get_token(23);
     }

%>
   <form action="" name="exeForm" method="POST">
    <input type="hidden" name="what" value="">
    <input type="hidden" name="user" value="<%=user%>">
    <input type="hidden" name="afile" value="<%=afile%>">
    <input type="hidden" name="token" value="">
      <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="50%">
	<tr>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/s_m_crit.gif">
	  </td>
	  <td class="sapTbvCellAlt" Colspan="2" align="center">
	    <b><i>Confirm operation</i></b>
	  </td>
	  <td class="sapTbvCellAlt" Colspan="1" align="center">
	    <img src="../public/icons/s_m_crit.gif">
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4" align="center">
	    <b><i>Do you really want to <%=opstr%> the user <%=htmlenc(user)%> ?</i></b>
	  </td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>
	<tr>
	  <td class="sapTbvCellStd" Colspan="4"></td>
	</tr>

	<tr>
	  <td class="sapTbvCellStd" Colspan="1"></td>
	  <td class="sapTbvCellStd" Colspan="1" align="center">
	    <a href="javascript:callSubmitAction('exeForm', '<%=op%>', '<%=token%>')" class="sapBtnStd">
	      <nobr>Yes</nobr></a>
	  </td>
	  <td class="sapTbvCellStd" Colspan="1" align="center">
	    <a href="javascript:callSubmitAction('exeForm','','')" class="sapBtnEmph">
		<nobr>No</nobr></a>
	  </td>
	  <td class="sapTbvCellStd" Colspan="1"></td>
	</tr>
      </table><p>
   </form>
<%
   }
   else
   {
     user_count = __icm_get_user_info(icm_user.auth_file, icm_user_info);
%>

   <form action="" name="userForm" method="POST">
    <input type="hidden" name="what" value="">
    <input type="hidden" name="afile" value="<%=afile%>">
    <input type="hidden" name="token" value="">
    <table border=0 cellspacing=1 cellpadding=0 class="sapTbvAlt" width="100%">
      <tr>
        <td class="sapTbvCellAlt" Colspan=6>
          <b><i>Users in file "<%=icm_user.auth_file%>"</i></b>
        </td>
      </tr>
      <tr>
        <td class="sapTbvCellAlt">
          <i>&nbsp;</i>
        </td>
        <td class="sapTbvCellAlt">
          <i>Name</i>
        </td>
        <td class="sapTbvCellAlt">
          <i>Group</i>
        </td>
        <td class="sapTbvCellAlt" width="500">
          <i>Client Certificate Data</i>
        </td>
        <td class="sapTbvCellAlt" width="80">
          <i>Password</i>
        </td>
        <td class="sapTbvCellAlt" width="50">
          <i>Attributes</i>
        </td>
      </tr>
<%
	for (nr = 0; nr < user_count; nr++) {
	  if (icm_user_info[nr].passwd != "x") passwd = "set";
	  else passwd = "none";
	  if ((what == "change_user") && (nr == row)) {
%>

      <tr>
	<td class="sapTbvCellStd" nowrap align=left>
          <input type="radio" name="row_selected" value="9999" checked >
	</td>
        <td class="sapTbvCellStd" width=200>
	  <input type="text" class="sapEdfTxtDsbl" name="user"
            value="<%=icm_user_info[nr].user%>" size="25" maxlength="50">
        </td>
        <td class="sapTbvCellStd">
	  <input type="text" class="sapEdfTxtEnbl" name="group"
            value="<%=icm_user_info[nr].group%>" size="25" maxlength="50">
        </td>
        <td class="sapTbvCellStd">
	  <input type="text" class="sapEdfTxtEnbl" name="cert"
            value="<%=icm_user_info[nr].cert%>" size="25" maxlength="128">
        </td>
        <td class="sapTbvCellStd">
	 <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="100%">
	  <tr>
	    <td class="sapTbvCellStd">Old Password:</td>
	    <td class="sapTbvCellStd">
		<input type="password" class="sapEdfTxtEnbl" name="passwd"
		value="" size="25" maxlength="128">
	    </td>
	  </tr>
	  <tr>
	    <td class="sapTbvCellStd">New Password:</td>
	    <td class="sapTbvCellStd">
		<input type="password" class="sapEdfTxtEnbl" name="passwd_new1"
		value="" size="25" maxlength="128">
	    </td>
	  </tr>
	  <tr>
	    <td class="sapTbvCellStd">Confirm Password:</td>
	    <td class="sapTbvCellStd">
		<input type="password" class="sapEdfTxtEnbl" name="passwd_new2"
		value="" size="25" maxlength="128">
	    </td>
	  </tr>
	 </table>
        <td class="sapTbvCellStd">
<%
          if (icm_user_info[nr].locked)
	    __output("<img src=\"../public/icons/s_s_locl.gif\" alt=\"user is locked\">");
	  else
	    __output("&nbsp;");
%>
	</td>
        </td>
      </tr>
<%
	  } else {
	    if (row == nr)
		is_checked = "checked";
	    else is_checked = "";
%>
      <tr>
	<td class="sapTbvCellStd" nowrap align=left width=2>
        <input type="radio" name="row_selected" value="<%= nr %>" <%=is_checked%>>
	</td>
        <td class="sapTbvCellStd"><%=icm_user_info[nr].user%></td>
        <td class="sapTbvCellStd"><%=icm_user_info[nr].group%></td>
        <td class="sapTbvCellStd"><%=icm_user_info[nr].cert%></td>
        <td class="sapTbvCellStd"><%=passwd%></td>
        <td class="sapTbvCellStd">
<%
          if (icm_user_info[nr].locked)
	    __output("<img src=\"../public/icons/s_s_locl.gif\" alt=\"user is locked\">");
	  else
	    __output("&nbsp;");
%>
	</td>
      </tr>
<%
	  }
	}
%>

<%
   if (what == "show_add_user") {
%>

      <tr>
	<td class="sapTbvCellStd" nowrap align=left>
          <input type="radio" name="row_selected" value="9999" checked >
	</td>
        <td class="sapTbvCellStd">
	  <input type="text" class="sapEdfTxtEnbl" name="user"
            value="" size="25" maxlength="50">
        </td>
        <td class="sapTbvCellStd">
	  <input type="text" class="sapEdfTxtEnbl" name="group"
            value="" size="25" maxlength="50">
        </td>
        <td class="sapTbvCellStd">
	  <input type="text" class="sapEdfTxtEnbl" name="cert"
            value="" size="25" maxlength="50">
        </td>
        <td class="sapTbvCellStd">
	 <table border="0" cellspacing="0" cellpadding="0" class="sapTbvAlt" width="100%">
	  <tr>
	    <td class="sapTbvCellStd">New Password:</td>
	    <td class="sapTbvCellStd">
		<input type="password" class="sapEdfTxtEnbl" name="passwd_new1"
		value="" size="25" maxlength="128">
	    </td>
	  </tr>
	  <tr>
	    <td class="sapTbvCellStd">Confirm Password:</td>
	    <td class="sapTbvCellStd">
		<input type="password" class="sapEdfTxtEnbl" name="passwd_new2"
		value="" size="25" maxlength="128">
	    </td>
	  </tr>
	 </table>
        </td>
        <td class="sapTbvCellStd">&nbsp;</td>
      </tr>
<%
   }
%>
    </table>
<%
   if (what == "show_add_user") {
     token = __icm_get_token(11);
%>
    <p>
    <a href="javascript:callSubmitAction('userForm','add_user', '<%=token%>')" class="sapBtnStd">
	<nobr>Save</nobr></a>
    <a href="javascript:callSubmitAction('userForm','','')" class="sapBtnEmph">
	<nobr>Cancel</nobr></a>
<%
   } else if (what == "change_user") {
     token = __icm_get_token(13);
%>

    <p>
    <a href="javascript:callSubmitAction('userForm','do_change_user', '<%=token%>')" class="sapBtnStd">
	<nobr>Save</nobr></a>
    <a href="javascript:callSubmitAction('userForm','','')" class="sapBtnEmph">
	<nobr>Cancel</nobr></a>
<%
   } else if (is_admin) {
%>
    <p>
    <a href="javascript:callSubmitAction('userForm', 'change_user', '')" class="sapBtnStd">
	<nobr>Edit user</nobr></a>
    <a href="javascript:callSubmitAction('userForm', 'show_add_user', '')" class="sapBtnStd">
	<nobr>Add User</nobr></a>
    <a href="javascript:callSubmitAction('userForm', 'del_user', '')" class="sapBtnStd">
	<nobr>Delete User</nobr></a>
    <a href="javascript:callSubmitAction('userForm', 'lock_user', '')" class="sapBtnStd">
	<nobr>Lock User</nobr></a>
    <a href="javascript:callSubmitAction('userForm', 'unlock_user', '')" class="sapBtnStd">
	<nobr>Unlock User</nobr></a>
<%
   }
%>
   </form>
<%
   }
%>

  </body>
</html>

