context: // 
  // lat - EditUserViewDialog.cs
  // Author: Loren Bandiera
  // Copyright 2005 MMG Security, Inc.
  //
  // 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; Version 2 
  //
  // This program is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  //
  //
  
  using Gtk;
  using System;
  using System.Collections.Generic;
  using System.Security.Cryptography;
  using System.Text;
  using Novell.Directory.Ldap;
  
  namespace lat
  {
  	public class EditUserViewDialog : ViewDialog
  	{
  		Glade.XML ui;
  
  		[Glade.Widget] Gtk.Dialog editUserDialog;
  
  		// General 
  		[Glade.Widget] Gtk.Label usernameLabel;
  		[Glade.Widget] Gtk.Label fullnameLabel;
  
  		[Glade.Widget] Gtk.Entry firstNameEntry;
  		[Glade.Widget] Gtk.Entry initialsEntry;
  		[Glade.Widget] Gtk.Entry lastNameEntry;
  		[Glade.Widget] Gtk.Entry descriptionEntry;
  		[Glade.Widget] Gtk.Entry officeEntry;
  
  		[Glade.Widget] Gtk.Entry mailEntry;
  		[Glade.Widget] Gtk.Entry phoneEntry;
  
  		// Account
  		[Glade.Widget] Gtk.Entry usernameEntry;
  		[Glade.Widget] Gtk.SpinButton uidSpinButton;
  		[Glade.Widget] Gtk.Entry homeDirEntry;
  		[Glade.Widget] Gtk.Entry shellEntry;
  
  		[Glade.Widget] Gtk.CheckButton smbEnableSambaButton;
  		[Glade.Widget] Gtk.Entry smbLoginScriptEntry;
  		[Glade.Widget] Gtk.Entry smbProfilePathEntry;
  		[Glade.Widget] Gtk.Entry smbHomePathEntry;
  		[Glade.Widget] Gtk.Entry smbHomeDriveEntry;
  		[Glade.Widget] Gtk.Entry smbExpireEntry;
  		[Glade.Widget] Gtk.Entry smbCanChangePwdEntry;
  		[Glade.Widget] Gtk.Entry smbMustChangePwdEntry;
  		[Glade.Widget] Gtk.Button smbSetExpireButton;
  		[Glade.Widget] Gtk.Button smbSetCanButton;
  		[Glade.Widget] Gtk.Button smbSetMustButton;
  
  		// Groups
  		[Glade.Widget] Gtk.Label primaryGroupLabel;
  		[Glade.Widget] Gtk.TreeView memberOfTreeview;
  
  		// Address
  		[Glade.Widget] Gtk.TextView adStreetTextView;
  		[Glade.Widget] Gtk.Entry adPOBoxEntry;
  		[Glade.Widget] Gtk.Entry adCityEntry;
  		[Glade.Widget] Gtk.Entry adStateEntry;
  		[Glade.Widget] Gtk.Entry adZipEntry;
  
  		// Telephones
  		[Glade.Widget] Gtk.Entry tnHomeEntry;
  		[Glade.Widget] Gtk.Entry tnPagerEntry;
  		[Glade.Widget] Gtk.Entry tnMobileEntry;
  		[Glade.Widget] Gtk.Entry tnFaxEntry;
  		[Glade.Widget] Gtk.Entry tnIPPhoneEntry;
  
  		// Organization
  		[Glade.Widget] Gtk.Entry ozTitleEntry;
  		[Glade.Widget] Gtk.Entry ozDeptEntry;
  		[Glade.Widget] Gtk.Entry ozCompanyEntry;
  
  		bool isSamba = false;
  		bool firstTimeSamba = false;
  		string pass = "";
  		string smbLM = "";
  		string smbNT = "";
  		string smbSID = "";
  		bool passChanged = false;
  		
  		LdapEntry currentEntry;
  
  		Dictionary<string,LdapEntry> _allGroups;
  		Dictionary<string,string> _allGroupGids;
  		Dictionary<string,LdapModification> _modsGroup;
  		Dictionary<string,string> _memberOfGroups;
  
  		ListStore _memberOfStore;
  
  		public EditUserViewDialog (Connection conn, LdapEntry le) : base (conn, null)
  		{
  			currentEntry = le;
  
  			Init ();
  
  			isSamba = Util.CheckSamba (currentEntry);
  			if (!isSamba)
  				firstTimeSamba = true;
  
  			try { getGroups (currentEntry); }
  			catch (Exception e) { Log.Debug (e); }
  
  			string userName = conn.Data.GetAttributeValueFromEntry (currentEntry, "cn");
  			editUserDialog.Title = userName + " Properties";
  
  			// General
  			usernameLabel.UseMarkup = true;
  			usernameLabel.Markup = 
  				String.Format ("<span size=\"larger\"><b>{0}</b></span>", conn.Data.GetAttributeValueFromEntry (currentEntry, "uid"));
  
  			fullnameLabel.Text = String.Format ("{0} {1}", 
  				conn.Data.GetAttributeValueFromEntry (currentEntry, "givenName"),
  				conn.Data.GetAttributeValueFromEntry (currentEntry, "sn"));
  
  			firstNameEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "givenName");
  			initialsEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "initials");
  			lastNameEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "sn");
  			descriptionEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "description");
  			officeEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "physicalDeliveryOfficeName");
  			mailEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "mail");
  			phoneEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "telephoneNumber");
  
  			// Account
  			usernameEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "uid");
  			uidSpinButton.Value = int.Parse (conn.Data.GetAttributeValueFromEntry (currentEntry, "uidNumber"));
  			shellEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "loginShell");;
  			homeDirEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "homeDirectory");
  
  			if (isSamba) {
  				toggleSambaWidgets (true);
  				smbEnableSambaButton.Hide ();
  
  				smbLoginScriptEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaLogonScript");
  				smbProfilePathEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaProfilePath");
  				smbHomePathEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaHomePath");
  				smbHomeDriveEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaHomeDrive");
  								
  				smbExpireEntry.Text = GetStringDate (conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaKickoffTime"));
  				smbCanChangePwdEntry.Text = GetStringDate (conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaPwdCanChange"));
  				smbMustChangePwdEntry.Text = GetStringDate (conn.Data.GetAttributeValueFromEntry (currentEntry, "sambaPwdMustChange"));
  
  			} else {
  
  				smbEnableSambaButton.Toggled += new EventHandler (OnSambaChanged);
  				toggleSambaWidgets (false);
  			}
  
  			// Groups
  			string pgid = conn.Data.GetAttributeValueFromEntry (currentEntry, "gidNumber");
  			if (_allGroupGids.ContainsKey (pgid))
  				primaryGroupLabel.Text = _allGroupGids [pgid];
  
  			// Address
  			adStreetTextView.Buffer.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "street");
  			adPOBoxEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "postOfficeBox");
  			adCityEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "l");
  			adStateEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "st");
  			adZipEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "postalCode");
  
  			// Telephones
  			tnHomeEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "homePhone");
  			tnPagerEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "pager");
  			tnMobileEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "mobile");
  			tnFaxEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "facsimileTelephoneNumber");
  
  			// Organization
  			ozTitleEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "title");
  			ozDeptEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "departmentNumber");
  			ozCompanyEntry.Text = conn.Data.GetAttributeValueFromEntry (currentEntry, "o");
  
  			editUserDialog.Icon = Global.latIcon;
  			editUserDialog.Run ();
  
  			while (missingValues || errorOccured) {
  
  				if (missingValues)
  					missingValues = false;
  				else if (errorOccured)
  					errorOccured = false;
  
  				editUserDialog.Run ();				
  			}
  
  			editUserDialog.Destroy ();
  		}
  	
  		string GetStringDate (string unixTime)
  		{
  			try {
  			
  				double d = double.Parse (unixTime);
  				DateTime dt = Util.GetDateTime (d);
  				
  				return dt.ToString ();
  			
  			} catch {
  				return "";
  			}
  		}
  	
  		void OnSambaChanged (object o, EventArgs args)
  		{
  			if (smbEnableSambaButton.Active) {
  
  				smbSID = conn.Data.GetLocalSID ();
  
  				if (smbSID == null) {
  					Util.DisplaySambaSIDWarning (editUserDialog);
  					smbEnableSambaButton.Active = false;
  					return;
  				}
  
  				toggleSambaWidgets (true);
  			} else {
  
  				toggleSambaWidgets (false);
  			}
  		}
  
  		bool checkMemberOf (string user, string[] members)
  		{
  			foreach (string s in members)
  				if (s.Equals (user))
  					return true;
  	
  			return false;			
  		}
  
  		void getGroups (LdapEntry le)
  		{
  			LdapEntry[] grps = conn.Data.SearchByClass ("posixGroup");
  
  			foreach (LdapEntry e in grps) {
  
  				LdapAttribute nameAttr, gidAttr;
  				nameAttr = e.getAttribute ("cn");
  				gidAttr = e.getAttribute ("gidNumber");
  
  				if (le != null && nameAttr != null) {
  
  					LdapAttribute a;
  					a  = e.getAttribute ("memberUid");
  					
  					if (a != null) {
  
  						if (checkMemberOf (conn.Data.GetAttributeValueFromEntry (currentEntry, "uid"), a.StringValueArray)
  						   && !_memberOfGroups.ContainsKey (nameAttr.StringValue)) {
  
  							_memberOfGroups.Add (nameAttr.StringValue,"memeberUid");
  							_memberOfStore.AppendValues (nameAttr.StringValue);
  						}
  					}
  				}
  
  				if (!_allGroups.ContainsKey (nameAttr.StringValue))
  					_allGroups.Add (nameAttr.StringValue, e);
  
  				if (gidAttr != null)
  					if (!_allGroupGids.ContainsKey (gidAttr.StringValue))
  						_allGroupGids.Add (gidAttr.StringValue, nameAttr.StringValue);
  			}
  				
  		}
  
  		void Init ()
  		{
  			_memberOfGroups = new Dictionary<string,string> ();
  			_allGroups = new Dictionary<string,LdapEntry> ();
  			_allGroupGids = new Dictionary<string,string> ();
  			_modsGroup = new Dictionary<string,LdapModification> ();
  
  			ui = new Glade.XML (null, "dialogs.glade", "editUserDialog", null);
  			ui.Autoconnect (this);
  
  			viewDialog = editUserDialog;
  
  			TreeViewColumn col;
  
  
gt: 			_memberOfStore = new ListStore (typeof (string));