PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/source3/libads/kerberos_util.c

https://bitbucket.org/mikedep333/rdssamba4
C | 107 lines | 57 code | 14 blank | 36 comment | 10 complexity | 30bb8841763e8d336d4ed6e5bcc30d72 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0
  1. /*
  2. Unix SMB/CIFS implementation.
  3. krb5 set password implementation
  4. Copyright (C) Andrew Tridgell 2001
  5. Copyright (C) Remus Koos 2001 (remuskoos@yahoo.com)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "includes.h"
  18. #include "smb_krb5.h"
  19. #include "ads.h"
  20. #ifdef HAVE_KRB5
  21. /**
  22. * Set the machine account password
  23. * @param ads connection to ads server
  24. * @param hostname machine whose password is being set
  25. * @param password new password
  26. * @return status of password change
  27. **/
  28. ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
  29. const char *machine_account,
  30. const char *password)
  31. {
  32. ADS_STATUS status;
  33. char *principal = NULL;
  34. /*
  35. we need to use the '$' form of the name here (the machine account name),
  36. as otherwise the server might end up setting the password for a user
  37. instead
  38. */
  39. if (asprintf(&principal, "%s@%s", machine_account, ads->config.realm) < 0) {
  40. return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
  41. }
  42. status = ads_krb5_set_password(ads->auth.kdc_server, principal,
  43. password, ads->auth.time_offset);
  44. SAFE_FREE(principal);
  45. return status;
  46. }
  47. /* run kinit to setup our ccache */
  48. int ads_kinit_password(ADS_STRUCT *ads)
  49. {
  50. char *s;
  51. int ret;
  52. const char *account_name;
  53. fstring acct_name;
  54. if (ads->auth.flags & ADS_AUTH_USER_CREDS) {
  55. account_name = ads->auth.user_name;
  56. goto got_accountname;
  57. }
  58. if ( IS_DC ) {
  59. /* this will end up getting a ticket for DOMAIN@RUSTED.REA.LM */
  60. account_name = lp_workgroup();
  61. } else {
  62. /* always use the sAMAccountName for security = domain */
  63. /* lp_netbios_name()$@REA.LM */
  64. if ( lp_security() == SEC_DOMAIN ) {
  65. fstr_sprintf( acct_name, "%s$", lp_netbios_name() );
  66. account_name = acct_name;
  67. }
  68. else
  69. /* This looks like host/lp_netbios_name()@REA.LM */
  70. account_name = ads->auth.user_name;
  71. }
  72. got_accountname:
  73. if (asprintf(&s, "%s@%s", account_name, ads->auth.realm) == -1) {
  74. return KRB5_CC_NOMEM;
  75. }
  76. if (!ads->auth.password) {
  77. SAFE_FREE(s);
  78. return KRB5_LIBOS_CANTREADPWD;
  79. }
  80. ret = kerberos_kinit_password_ext(s, ads->auth.password, ads->auth.time_offset,
  81. &ads->auth.tgt_expire, NULL, NULL, False, False, ads->auth.renewable,
  82. NULL);
  83. if (ret) {
  84. DEBUG(0,("kerberos_kinit_password %s failed: %s\n",
  85. s, error_message(ret)));
  86. }
  87. SAFE_FREE(s);
  88. return ret;
  89. }
  90. #endif