transaction.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "_cgo_export.h"
  2. #include <security/pam_appl.h>
  3. #include <string.h>
  4. int cb_pam_conv(
  5. int num_msg,
  6. const struct pam_message **msg,
  7. struct pam_response **resp,
  8. void *appdata_ptr)
  9. {
  10. *resp = calloc(num_msg, sizeof **resp);
  11. if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG) {
  12. return PAM_CONV_ERR;
  13. }
  14. if (!*resp) {
  15. return PAM_BUF_ERR;
  16. }
  17. for (size_t i = 0; i < num_msg; ++i) {
  18. struct cbPAMConv_return result = cbPAMConv(
  19. msg[i]->msg_style,
  20. (char *)msg[i]->msg,
  21. appdata_ptr);
  22. if (result.r1 != PAM_SUCCESS) {
  23. goto error;
  24. }
  25. (*resp)[i].resp = result.r0;
  26. }
  27. return PAM_SUCCESS;
  28. error:
  29. for (size_t i = 0; i < num_msg; ++i) {
  30. if ((*resp)[i].resp) {
  31. memset((*resp)[i].resp, 0, strlen((*resp)[i].resp));
  32. free((*resp)[i].resp);
  33. }
  34. }
  35. memset(*resp, 0, num_msg * sizeof *resp);
  36. free(*resp);
  37. *resp = NULL;
  38. return PAM_CONV_ERR;
  39. }
  40. struct pam_conv *make_pam_conv(void *appdata_ptr)
  41. {
  42. struct pam_conv* conv = malloc(sizeof *conv);
  43. if (!conv) {
  44. return NULL;
  45. }
  46. conv->conv = cb_pam_conv;
  47. conv->appdata_ptr = appdata_ptr;
  48. return conv;
  49. }