pam_data.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that the following conditions
  4. * are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, and the entire permission notice in its entirety,
  7. * including the disclaimer of warranties.
  8. * 2. Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * 3. The name of the author may not be used to endorse or promote
  12. * products derived from this software without specific prior
  13. * written permission.
  14. *
  15. * ALTERNATIVELY, this product may be distributed under the terms of
  16. * the GNU Public License, in which case the provisions of the GPL are
  17. * required INSTEAD OF the above restrictions. (This clause is
  18. * necessary due to a potential bad interaction between the GPL and
  19. * the restrictions contained in a BSD-style copyright.)
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  29. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  31. * OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "config.h"
  34. #include "pam_private.h"
  35. #include <stdlib.h>
  36. #include <string.h>
  37. static struct pam_data *_pam_locate_data(const pam_handle_t *pamh,
  38. const char *name)
  39. {
  40. struct pam_data *data;
  41. D(("called"));
  42. IF_NO_PAMH("_pam_locate_data", pamh, NULL);
  43. data = pamh->data;
  44. while (data) {
  45. if (!strcmp(data->name, name)) {
  46. return data;
  47. }
  48. data = data->next;
  49. }
  50. return NULL;
  51. }
  52. int pam_set_data(
  53. pam_handle_t *pamh,
  54. const char *module_data_name,
  55. void *data,
  56. void (*cleanup)(pam_handle_t *pamh, void *data, int error_status))
  57. {
  58. struct pam_data *data_entry;
  59. D(("called"));
  60. IF_NO_PAMH("pam_set_data", pamh, PAM_SYSTEM_ERR);
  61. if (__PAM_FROM_APP(pamh)) {
  62. D(("called from application!?"));
  63. return PAM_SYSTEM_ERR;
  64. }
  65. /* module_data_name should not be NULL */
  66. if (module_data_name == NULL) {
  67. D(("called with NULL as module_data_name"));
  68. return PAM_SYSTEM_ERR;
  69. }
  70. /* first check if there is some data already. If so clean it up */
  71. if ((data_entry = _pam_locate_data(pamh, module_data_name))) {
  72. if (data_entry->cleanup) {
  73. data_entry->cleanup(pamh, data_entry->data,
  74. PAM_DATA_REPLACE | PAM_SUCCESS );
  75. }
  76. } else if ((data_entry = malloc(sizeof(*data_entry)))) {
  77. char *tname;
  78. if ((tname = _pam_strdup(module_data_name)) == NULL) {
  79. pam_syslog(pamh, LOG_CRIT,
  80. "pam_set_data: no memory for data name");
  81. _pam_drop(data_entry);
  82. return PAM_BUF_ERR;
  83. }
  84. data_entry->next = pamh->data;
  85. pamh->data = data_entry;
  86. data_entry->name = tname;
  87. } else {
  88. pam_syslog(pamh, LOG_CRIT,
  89. "pam_set_data: cannot allocate data entry");
  90. return PAM_BUF_ERR;
  91. }
  92. data_entry->data = data; /* note this could be NULL */
  93. data_entry->cleanup = cleanup;
  94. return PAM_SUCCESS;
  95. }
  96. int pam_get_data(
  97. const pam_handle_t *pamh,
  98. const char *module_data_name,
  99. const void **datap)
  100. {
  101. struct pam_data *data;
  102. D(("called"));
  103. IF_NO_PAMH("pam_get_data", pamh, PAM_SYSTEM_ERR);
  104. if (__PAM_FROM_APP(pamh)) {
  105. D(("called from application!?"));
  106. return PAM_SYSTEM_ERR;
  107. }
  108. /* module_data_name should not be NULL */
  109. if (module_data_name == NULL) {
  110. D(("called with NULL as module_data_name"));
  111. return PAM_SYSTEM_ERR;
  112. }
  113. data = _pam_locate_data(pamh, module_data_name);
  114. if (data) {
  115. *datap = data->data;
  116. return PAM_SUCCESS;
  117. }
  118. return PAM_NO_MODULE_DATA;
  119. }
  120. void _pam_free_data(pam_handle_t *pamh, int status)
  121. {
  122. struct pam_data *last;
  123. struct pam_data *data;
  124. D(("called"));
  125. IF_NO_PAMH("_pam_free_data", pamh, /* no return value for void fn */);
  126. data = pamh->data;
  127. while (data) {
  128. last = data;
  129. data = data->next;
  130. if (last->cleanup) {
  131. last->cleanup(pamh, last->data, status);
  132. }
  133. _pam_drop(last->name);
  134. _pam_drop(last);
  135. }
  136. }