kerberospw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright (c) 2008 Guido Guenther <agx@sigxcpu.org>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. #include <Python.h>
  17. #include "kerberospw.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #undef PRINTFS
  22. extern PyObject *PwdChangeException_class;
  23. static void set_pwchange_error(krb5_context context, krb5_error_code code)
  24. {
  25. PyErr_SetObject(PwdChangeException_class, Py_BuildValue("(s:i)",
  26. krb5_get_err_text(context, code), code));
  27. }
  28. /* Inspired by krb5_verify_user from Heimdal */
  29. static krb5_error_code verify_krb5_user(krb5_context context,
  30. krb5_principal principal,
  31. const char *password,
  32. const char *service,
  33. krb5_creds* creds)
  34. {
  35. krb5_get_init_creds_opt gic_options;
  36. krb5_error_code code;
  37. int ret = 0;
  38. #ifdef PRINTFS
  39. {
  40. char *name = NULL;
  41. code = krb5_unparse_name(context, principal, &name);
  42. if (!code)
  43. printf("Trying to get TGT for user %s\n", name);
  44. free(name);
  45. }
  46. #endif
  47. krb5_get_init_creds_opt_init(&gic_options);
  48. krb5_get_init_creds_opt_set_forwardable(&gic_options, 0);
  49. krb5_get_init_creds_opt_set_proxiable(&gic_options, 0);
  50. krb5_get_init_creds_opt_set_renew_life(&gic_options, 0);
  51. memset(creds, 0, sizeof(krb5_creds));
  52. code = krb5_get_init_creds_password(context, creds, principal,
  53. (char *)password, NULL, NULL, 0,
  54. (char *)service, &gic_options);
  55. if (code) {
  56. set_pwchange_error(context, code);
  57. goto end;
  58. }
  59. ret = 1; /* success */
  60. end:
  61. return ret;
  62. }
  63. int change_user_krb5pwd(const char *user, const char* oldpswd, const char *newpswd)
  64. {
  65. krb5_context kcontext = NULL;
  66. krb5_error_code code;
  67. krb5_principal client = NULL;
  68. krb5_creds creds;
  69. int ret = 0;
  70. char *name = NULL;
  71. const char* service = "kadmin/changepw";
  72. int result_code;
  73. krb5_data result_code_string, result_string;
  74. code = krb5_init_context(&kcontext);
  75. if (code) {
  76. PyErr_SetObject(PwdChangeException_class, Py_BuildValue("((s:i))",
  77. "Cannot initialize Kerberos5 context",
  78. code));
  79. return 0;
  80. }
  81. name = (char *)malloc(256);
  82. snprintf(name, 256, "%s", user);
  83. code = krb5_parse_name(kcontext, name, &client);
  84. if (code) {
  85. set_pwchange_error(kcontext, code);
  86. goto end;
  87. }
  88. code = verify_krb5_user(kcontext, client, oldpswd, service, &creds);
  89. if (!code) /* exception set by verify_krb5_user */
  90. goto end;
  91. code = krb5_change_password(kcontext, &creds, (char*)newpswd,
  92. &result_code, &result_code_string, &result_string);
  93. if (code) {
  94. set_pwchange_error(kcontext, code);
  95. goto end;
  96. }
  97. if (result_code) {
  98. char *message = NULL;
  99. asprintf(&message, "%.*s: %.*s",
  100. (int) result_code_string.length,
  101. (char *) result_code_string.data,
  102. (int) result_string.length,
  103. (char *) result_string.data);
  104. PyErr_SetObject(PwdChangeException_class, Py_BuildValue("((s:i))",
  105. message, result_code));
  106. free(message);
  107. goto end;
  108. }
  109. ret = 1; /* success */
  110. end:
  111. #ifdef PRINTFS
  112. printf("%s: ret=%d user=%s\n", __FUNCTION__, ret, name);
  113. #endif
  114. if (name)
  115. free(name);
  116. if (client)
  117. krb5_free_principal(kcontext, client);
  118. krb5_free_context(kcontext);
  119. return ret;
  120. }