コード例 #1
0
        public Response Post(UsersProperties theModel)
        {
            if (string.IsNullOrEmpty(theModel.Username))
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=1")
                });
            }

            if (string.IsNullOrEmpty(theModel.OldPassword))
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=2")
                });
            }

            if (theModel.NewPassword != theModel.NewPasswordCheck)
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=3")
                });
            }

            if (string.IsNullOrEmpty(theModel.NewPassword))
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=4")
                });
            }

            Task <ProcessResult> UserEntries = ProcessRunner.GetProcessResultAsync("grep", "^" + theModel.Username + " /etc/shadow");

            UserEntries.Wait();

            var CommandResultLines = UserEntries.Result.GetOutput().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                     .Select(line => line.TrimEnd())
                                     .ToArray();

            if (CommandResultLines.Length == 0)
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=5")
                });
            }

            if (CommandResultLines.Length > 1)
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=6")
                });
            }

            // Format of Line is Username$AlgorithmType$Salt$HashedPassword:OtherMeta
            string UserEntry = CommandResultLines[0];

            const string Dollar = "$";
            const string Colon  = ":";

            int StartIndex = UserEntry.IndexOf(Dollar);
            int EndHashAlgorithmTypeIndex = UserEntry.IndexOf(Dollar, StartIndex + Dollar.Length);
            int EndSaltIndex = UserEntry.IndexOf(Dollar, EndHashAlgorithmTypeIndex + Dollar.Length);
            int EndHashIndex = UserEntry.IndexOf(Colon, StartIndex + Dollar.Length);

            string HashAlgorithmType = UserEntry.Substring(StartIndex, EndHashAlgorithmTypeIndex - StartIndex + Dollar.Length);

            // Existing Hash, including the Algorithm Type $6$, the Salt and the Hash
            string ExistingHash = UserEntry.Substring(StartIndex, EndHashIndex - StartIndex);
            string Salt         = UserEntry.Substring(EndHashAlgorithmTypeIndex + Dollar.Length, EndSaltIndex - EndHashAlgorithmTypeIndex - Dollar.Length);

            string NewHash = Linux.Crypt(theModel.OldPassword, HashAlgorithmType + Salt + Dollar);

            if (ExistingHash != NewHash)
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=7")
                });
            }

            Task <ProcessResult> ChangePassword = ProcessRunner.GetProcessResultAsync("bash", "-c \"echo '" + theModel.Username + ":" + theModel.NewPassword + "' | chpasswd\"");

            ChangePassword.Wait();

            if (ChangePassword.Result.Okay())
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=10")
                });
            }
            else
            {
                return(new Response
                {
                    StatusCode = System.Net.HttpStatusCode.Moved,
                    Location = new Uri(RemoveOldQueryString(Context.Request.AbsoluteUri) + "?msg=9")
                });
            }
        }