From 2506d32da81e4f1f1269ab11daff50012f4da282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez?= Date: Mon, 26 Nov 2012 09:08:10 +0000 Subject: [PATCH] * Changed separator of action/parameters to '\r' * Added "ChangeUserPassword" to Operations * Added so GetInfo can return a message not only for machine renaming, but also for an user password changing (for WinRandomPassOsManager) --- udsService/rpc/Operation.cs | 28 ++++++++++++++++++ udsService/rpc/rpc.cs | 2 +- udsService/udsService/Service.cs | 49 +++++++++++++++++++++++++++----- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/udsService/rpc/Operation.cs b/udsService/rpc/Operation.cs index 96cec0c04..8367a563c 100644 --- a/udsService/rpc/Operation.cs +++ b/udsService/rpc/Operation.cs @@ -35,6 +35,16 @@ namespace uds [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] internal static extern bool ExitWindowsEx(int flg, int rea); + [DllImport("netapi32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, + SetLastError = true)] + static extern uint NetUserChangePassword( + [MarshalAs(UnmanagedType.LPWStr)] string domainname, + [MarshalAs(UnmanagedType.LPWStr)] string username, + [MarshalAs(UnmanagedType.LPWStr)] string oldpassword, + [MarshalAs(UnmanagedType.LPWStr)] string newpassword + ); + + [Flags] public enum JoinOptions { @@ -141,5 +151,23 @@ namespace uds } + public static bool ChangeUserPassword(string user, string oldPass, string newPass) + { + try { + logger.Debug("Setting new password for user " + user + " to " + newPass); + uint res = NetUserChangePassword(null, user, oldPass, newPass); + logger.Debug("Result of changeUserPassword: " + res); + + if( res != 0 ) + logger.Error("Could not change password for user \"" + user + "\" (using password \"" + newPass + "\"), result: " + res); + + return res == 0; + } + catch (Exception e) + { + logger.Error("Exception at change user password", e); + return false; + } + } } } diff --git a/udsService/rpc/rpc.cs b/udsService/rpc/rpc.cs index 5847fabf6..add8511ec 100644 --- a/udsService/rpc/rpc.cs +++ b/udsService/rpc/rpc.cs @@ -18,7 +18,7 @@ namespace uds private const string LOGON_MSG = "logon"; private const string LOGOFF_MSG = "logoff"; - private const string INFO_MSG = "info"; + private const string INFO_MSG = "information"; private const string READY_MSG = "ready"; private const string IP_MSG = "ip"; diff --git a/udsService/udsService/Service.cs b/udsService/udsService/Service.cs index bcd1f5421..27fbca558 100644 --- a/udsService/udsService/Service.cs +++ b/udsService/udsService/Service.cs @@ -127,10 +127,18 @@ namespace uds.Services rpc.ResetManager(); return; } - // Message is in the form "action:params", where we can identify: - // rename:computername - // domain:computername\tdomain\tou\tuserToAuth\tpassToAuth - string[] data = action.Split(':'); + + // Important note: + // Remove ":" as separator, due to the posibility that ":" can be used as part of a password + // Old ":" is now '\r' + // In order to keep compatibility, getInfo will invoke rcp "information", so old actors "versions" + // will keep invoking "info" and return the old ":" separator way + + // Message is in the form "action\rparams", where we can identify: + // rename\rcomputername --- > Just rename + // rename\rcomputername\tuser\toldPass\tnewPass --> Rename with user password changing + // domain:computername\tdomain\tou\tuserToAuth\tpassToAuth --> Rename and add machine to domain + string[] data = action.Split('\r'); if (data.Length != 2) { logger.Error("Unrecognized instruction: \"" + action + "\""); @@ -138,14 +146,27 @@ namespace uds.Services return; } + string[] parms = data[1].Split('\t'); + switch (data[0]) { case "rename": - Rename(data[1]); + if (parms.Length == 1 ) + // Do not have to change user password + Rename(parms[0], null, null, null); + else if (parms.Length == 4) + // Rename, and also change user password + Rename(parms[0], parms[1], parms[2], parms[2]); + else + { + logger.Error("Unrecognized parameters: " + data[1]); + rpc.ResetManager(); + return; + } + break; case "domain": { - string[] parms = data[1].Split('\t'); if (parms.Length != 5) { logger.Error("Unrecognized parameters: " + data[1]); @@ -213,23 +234,37 @@ namespace uds.Services } } - private void Rename(string name) + private void Rename(string name, string user, string oldPass, string newPass) { logger.Info("Requested renaming of computer to \"" + name + "\""); // name and newName can be different case, but still same Info.DomainInfo info = Info.Computer.GetDomainInfo(); + if ( string.Equals(info.ComputerName, name, StringComparison.CurrentCultureIgnoreCase)) { logger.Debug("Computer do not needs to be renamed"); rpc.SetReady(); return; } + + // Set user password if provided + if (user != null) + { + if (Operation.ChangeUserPassword(user, oldPass, newPass) == false) + { + logger.Error("Could not change password to " + newPass + " for user " + user); + rpc.ResetManager(); + return; + } + } + if (Operation.RenameComputer(name) == false) { logger.Error("Could not rename machine to \"" + name + "\""); rpc.ResetManager(); return; } + Reboot(); }