pam_audit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* pam_audit.c -- Instrumentation code for Linux Auditing System */
  2. /* (C) 2005-2006 Red Hat, Inc. -- Licensing details are in the COPYING
  3. file accompanying the Linux-PAM source distribution.
  4. Authors:
  5. Steve Grubb <sgrubb@redhat.com> */
  6. #include "pam_private.h"
  7. #include "pam_modutil_private.h"
  8. #ifdef HAVE_LIBAUDIT
  9. #include <stdio.h>
  10. #include <syslog.h>
  11. #include <libaudit.h>
  12. #include <pwd.h>
  13. #include <netdb.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <arpa/inet.h>
  18. #include <errno.h>
  19. #define PAMAUDIT_LOGGED 1
  20. static int
  21. _pam_audit_writelog(pam_handle_t *pamh, int audit_fd, int type,
  22. const char *message, const char *grantors, int retval)
  23. {
  24. static int old_errno = -1;
  25. int rc = -ENOMEM;
  26. char *buf;
  27. const char *grantors_field = " grantors=";
  28. if (grantors == NULL) {
  29. grantors = "";
  30. grantors_field = "";
  31. }
  32. if (asprintf(&buf, "PAM:%s%s%s", message, grantors_field, grantors) >= 0) {
  33. rc = audit_log_acct_message(audit_fd, type, NULL, buf,
  34. (retval != PAM_USER_UNKNOWN && pamh->user) ? pamh->user : "?",
  35. -1, pamh->rhost, NULL, pamh->tty, retval == PAM_SUCCESS);
  36. free(buf);
  37. }
  38. /* libaudit sets errno to his own negative error code. This can be
  39. an official errno number, but must not. It can also be a audit
  40. internal error code. Which makes errno useless :-((. Try the
  41. best to fix it. */
  42. errno = -rc;
  43. pamh->audit_state |= PAMAUDIT_LOGGED;
  44. if (rc < 0) {
  45. if (rc == -EPERM)
  46. return 0;
  47. if (errno != old_errno) {
  48. old_errno = errno;
  49. pam_syslog (pamh, LOG_CRIT, "audit_log_acct_message() failed: %m");
  50. }
  51. }
  52. return rc;
  53. }
  54. static int
  55. _pam_audit_open(pam_handle_t *pamh)
  56. {
  57. int audit_fd;
  58. audit_fd = audit_open();
  59. if (audit_fd < 0) {
  60. /* You get these error codes only when the kernel doesn't have
  61. * audit compiled in. */
  62. if (errno == EINVAL || errno == EPROTONOSUPPORT ||
  63. errno == EAFNOSUPPORT)
  64. return -2;
  65. /* this should only fail in case of extreme resource shortage,
  66. * need to prevent login in that case for CAPP compliance.
  67. */
  68. pam_syslog(pamh, LOG_CRIT, "audit_open() failed: %m");
  69. return -1;
  70. }
  71. return audit_fd;
  72. }
  73. static int
  74. _pam_list_grantors(struct handler *hlist, int retval, char **list)
  75. {
  76. *list = NULL;
  77. if (retval == PAM_SUCCESS) {
  78. struct handler *h;
  79. char *p = NULL;
  80. size_t len = 0;
  81. for (h = hlist; h != NULL; h = h->next) {
  82. if (h->grantor) {
  83. len += strlen(h->mod_name) + 1;
  84. }
  85. }
  86. if (len == 0) {
  87. return 0;
  88. }
  89. *list = malloc(len);
  90. if (*list == NULL) {
  91. return -1;
  92. }
  93. for (h = hlist; h != NULL; h = h->next) {
  94. if (h->grantor) {
  95. if (p == NULL) {
  96. p = *list;
  97. } else {
  98. p = stpcpy(p, ",");
  99. }
  100. p = stpcpy(p, h->mod_name);
  101. }
  102. }
  103. }
  104. return 0;
  105. }
  106. int
  107. _pam_auditlog(pam_handle_t *pamh, int action, int retval, int flags, struct handler *h)
  108. {
  109. const char *message;
  110. int type;
  111. int audit_fd;
  112. char *grantors;
  113. if ((audit_fd=_pam_audit_open(pamh)) == -1) {
  114. return PAM_SYSTEM_ERR;
  115. } else if (audit_fd == -2) {
  116. return retval;
  117. }
  118. switch (action) {
  119. case PAM_AUTHENTICATE:
  120. message = "authentication";
  121. type = AUDIT_USER_AUTH;
  122. break;
  123. case PAM_OPEN_SESSION:
  124. message = "session_open";
  125. type = AUDIT_USER_START;
  126. break;
  127. case PAM_CLOSE_SESSION:
  128. message = "session_close";
  129. type = AUDIT_USER_END;
  130. break;
  131. case PAM_ACCOUNT:
  132. message = "accounting";
  133. type = AUDIT_USER_ACCT;
  134. break;
  135. case PAM_CHAUTHTOK:
  136. message = "chauthtok";
  137. type = AUDIT_USER_CHAUTHTOK;
  138. break;
  139. case PAM_SETCRED:
  140. message = "setcred";
  141. if (flags & PAM_ESTABLISH_CRED)
  142. type = AUDIT_CRED_ACQ;
  143. else if ((flags & PAM_REINITIALIZE_CRED) || (flags & PAM_REFRESH_CRED))
  144. type = AUDIT_CRED_REFR;
  145. else if (flags & PAM_DELETE_CRED)
  146. type = AUDIT_CRED_DISP;
  147. else
  148. type = AUDIT_USER_ERR;
  149. break;
  150. case _PAM_ACTION_DONE:
  151. message = "bad_ident";
  152. type = AUDIT_USER_ERR;
  153. break;
  154. default:
  155. message = "UNKNOWN";
  156. type = AUDIT_USER_ERR;
  157. pam_syslog(pamh, LOG_CRIT, "_pam_auditlog() should never get here");
  158. retval = PAM_SYSTEM_ERR;
  159. }
  160. if (_pam_list_grantors(h, retval, &grantors) < 0) {
  161. /* allocation failure */
  162. pam_syslog(pamh, LOG_CRIT, "_pam_list_grantors() failed: %m");
  163. retval = PAM_SYSTEM_ERR;
  164. }
  165. if (_pam_audit_writelog(pamh, audit_fd, type, message,
  166. grantors ? grantors : "?", retval) < 0)
  167. retval = PAM_SYSTEM_ERR;
  168. free(grantors);
  169. audit_close(audit_fd);
  170. return retval;
  171. }
  172. int
  173. _pam_audit_end(pam_handle_t *pamh, int status UNUSED)
  174. {
  175. if (! (pamh->audit_state & PAMAUDIT_LOGGED)) {
  176. /* PAM library is being shut down without any of the auditted
  177. * stacks having been run. Assume that this is sshd faking
  178. * things for an unknown user.
  179. */
  180. _pam_auditlog(pamh, _PAM_ACTION_DONE, PAM_USER_UNKNOWN, 0, NULL);
  181. }
  182. return 0;
  183. }
  184. int
  185. pam_modutil_audit_write(pam_handle_t *pamh, int type,
  186. const char *message, int retval)
  187. {
  188. int audit_fd;
  189. int rc;
  190. if ((audit_fd=_pam_audit_open(pamh)) == -1) {
  191. return PAM_SYSTEM_ERR;
  192. } else if (audit_fd == -2) {
  193. return retval;
  194. }
  195. rc = _pam_audit_writelog(pamh, audit_fd, type, message, NULL, retval);
  196. audit_close(audit_fd);
  197. return rc < 0 ? PAM_SYSTEM_ERR : PAM_SUCCESS;
  198. }
  199. #else
  200. int pam_modutil_audit_write(pam_handle_t *pamh UNUSED, int type UNUSED,
  201. const char *message UNUSED, int retval UNUSED)
  202. {
  203. return PAM_SUCCESS;
  204. }
  205. #endif /* HAVE_LIBAUDIT */