auth/credentials: add cli_credentials_set_utf16_password()
[obnox/samba/samba-obnox.git] / auth / credentials / credentials_ntlm.c
index 8c6be39522690e7f6e2c6781b47a165c8c443fe6..5e9aeeda9544d0df263002ae9dadd07873e50426 100644 (file)
@@ -214,7 +214,60 @@ _PUBLIC_ NTSTATUS cli_credentials_get_ntlm_response(struct cli_credentials *cred
        }
        return NT_STATUS_OK;
 }
-       
+
+/*
+ * Set a utf16 password on the credentials context, including an indication
+ * of 'how' the password was obtained
+ *
+ * This is required because the nt_hash is calculated over the raw utf16 blob,
+ * which might not be completely valid utf16, which means the conversion
+ * from CH_UTF16MUNGED to CH_UTF8 might loose information.
+ */
+_PUBLIC_ bool cli_credentials_set_utf16_password(struct cli_credentials *cred,
+                                                const DATA_BLOB *password_utf16,
+                                                enum credentials_obtained obtained)
+{
+       if (password_utf16 == NULL) {
+               return cli_credentials_set_password(cred, NULL, obtained);
+       }
+
+       if (obtained >= cred->password_obtained) {
+               struct samr_Password *nt_hash = NULL;
+               char *password_talloc = NULL;
+               size_t password_len = 0;
+               bool ok;
+
+               nt_hash = talloc(cred, struct samr_Password);
+               if (nt_hash == NULL) {
+                       return false;
+               }
+
+               ok = convert_string_talloc(cred,
+                                          CH_UTF16MUNGED, CH_UTF8,
+                                          password_utf16->data,
+                                          password_utf16->length,
+                                          (void *)&password_talloc,
+                                          &password_len);
+               if (!ok) {
+                       TALLOC_FREE(nt_hash);
+                       return false;
+               }
+
+               ok = cli_credentials_set_password(cred, password_talloc, obtained);
+               TALLOC_FREE(password_talloc);
+               if (!ok) {
+                       TALLOC_FREE(nt_hash);
+                       return false;
+               }
+
+               mdfour(nt_hash->hash, password_utf16->data, password_utf16->length);
+               cred->nt_hash = nt_hash;
+               return true;
+       }
+
+       return false;
+}
+
 _PUBLIC_ bool cli_credentials_set_nt_hash(struct cli_credentials *cred,
                                 const struct samr_Password *nt_hash, 
                                 enum credentials_obtained obtained)