pam_unix_acct.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * pam_unix account management
  3. *
  4. * Copyright Elliot Lee, 1996. All rights reserved.
  5. * Copyright Jan Rękorajski, 1999. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, and the entire permission notice in its entirety,
  12. * including the disclaimer of warranties.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * ALTERNATIVELY, this product may be distributed under the terms of
  21. * the GNU Public License, in which case the provisions of the GPL are
  22. * required INSTEAD OF the above restrictions. (This clause is
  23. * necessary due to a potential bad interaction between the GPL and
  24. * the restrictions contained in a BSD-style copyright.)
  25. *
  26. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  28. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  36. * OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include "config.h"
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/resource.h>
  45. #include <syslog.h>
  46. #include <pwd.h>
  47. #include <shadow.h>
  48. #include <time.h> /* for time() */
  49. #include <errno.h>
  50. #include <sys/wait.h>
  51. #include <security/_pam_macros.h>
  52. #include <security/pam_modules.h>
  53. #include <security/pam_ext.h>
  54. #include <security/pam_modutil.h>
  55. #include "pam_cc_compat.h"
  56. #include "support.h"
  57. #include "passverify.h"
  58. int _unix_run_verify_binary(pam_handle_t *pamh, unsigned long long ctrl,
  59. const char *user, int *daysleft)
  60. {
  61. int retval=0, child, fds[2];
  62. struct sigaction newsa, oldsa;
  63. D(("running verify_binary"));
  64. /* create a pipe for the messages */
  65. if (pipe(fds) != 0) {
  66. D(("could not make pipe"));
  67. pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
  68. return PAM_AUTH_ERR;
  69. }
  70. D(("called."));
  71. if (off(UNIX_NOREAP, ctrl)) {
  72. /*
  73. * This code arranges that the demise of the child does not cause
  74. * the application to receive a signal it is not expecting - which
  75. * may kill the application or worse.
  76. *
  77. * The "noreap" module argument is provided so that the admin can
  78. * override this behavior.
  79. */
  80. memset(&newsa, '\0', sizeof(newsa));
  81. newsa.sa_handler = SIG_DFL;
  82. sigaction(SIGCHLD, &newsa, &oldsa);
  83. }
  84. /* fork */
  85. child = fork();
  86. if (child == 0) {
  87. static char *envp[] = { NULL };
  88. const char *args[] = { NULL, NULL, NULL, NULL };
  89. /* XXX - should really tidy up PAM here too */
  90. /* reopen stdout as pipe */
  91. if (dup2(fds[1], STDOUT_FILENO) != STDOUT_FILENO) {
  92. pam_syslog(pamh, LOG_ERR, "dup2 of %s failed: %m", "stdout");
  93. _exit(PAM_AUTHINFO_UNAVAIL);
  94. }
  95. if (pam_modutil_sanitize_helper_fds(pamh, PAM_MODUTIL_PIPE_FD,
  96. PAM_MODUTIL_IGNORE_FD,
  97. PAM_MODUTIL_PIPE_FD) < 0) {
  98. _exit(PAM_AUTHINFO_UNAVAIL);
  99. }
  100. if (geteuid() == 0) {
  101. /* must set the real uid to 0 so the helper will not error
  102. out if pam is called from setuid binary (su, sudo...) */
  103. if (setuid(0) == -1) {
  104. pam_syslog(pamh, LOG_ERR, "setuid failed: %m");
  105. printf("-1\n");
  106. fflush(stdout);
  107. _exit(PAM_AUTHINFO_UNAVAIL);
  108. }
  109. }
  110. /* exec binary helper */
  111. args[0] = CHKPWD_HELPER;
  112. args[1] = user;
  113. args[2] = "chkexpiry";
  114. DIAG_PUSH_IGNORE_CAST_QUAL;
  115. execve(CHKPWD_HELPER, (char *const *) args, envp);
  116. DIAG_POP_IGNORE_CAST_QUAL;
  117. pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
  118. /* should not get here: exit with error */
  119. D(("helper binary is not available"));
  120. printf("-1\n");
  121. fflush(stdout);
  122. _exit(PAM_AUTHINFO_UNAVAIL);
  123. } else {
  124. close(fds[1]);
  125. if (child > 0) {
  126. char buf[32];
  127. int rc=0;
  128. /* wait for helper to complete: */
  129. while ((rc=waitpid(child, &retval, 0)) < 0 && errno == EINTR);
  130. if (rc<0) {
  131. pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
  132. retval = PAM_AUTH_ERR;
  133. } else if (!WIFEXITED(retval)) {
  134. pam_syslog(pamh, LOG_ERR, "unix_chkpwd abnormal exit: %d", retval);
  135. retval = PAM_AUTH_ERR;
  136. } else {
  137. retval = WEXITSTATUS(retval);
  138. rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
  139. if(rc > 0) {
  140. buf[rc] = '\0';
  141. if (sscanf(buf,"%d", daysleft) != 1 )
  142. retval = PAM_AUTH_ERR;
  143. }
  144. else {
  145. pam_syslog(pamh, LOG_ERR, "read unix_chkpwd output error %d: %m", rc);
  146. retval = PAM_AUTH_ERR;
  147. }
  148. }
  149. } else {
  150. pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
  151. D(("fork failed"));
  152. retval = PAM_AUTH_ERR;
  153. }
  154. close(fds[0]);
  155. }
  156. if (off(UNIX_NOREAP, ctrl)) {
  157. sigaction(SIGCHLD, &oldsa, NULL); /* restore old signal handler */
  158. }
  159. D(("Returning %d",retval));
  160. return retval;
  161. }
  162. /*
  163. * PAM framework looks for this entry-point to pass control to the
  164. * account management module.
  165. */
  166. int
  167. pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
  168. {
  169. unsigned long long ctrl;
  170. const void *void_uname;
  171. const char *uname;
  172. int retval, daysleft = -1;
  173. char buf[256];
  174. D(("called."));
  175. ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
  176. retval = pam_get_item(pamh, PAM_USER, &void_uname);
  177. uname = void_uname;
  178. D(("user = `%s'", uname));
  179. if (retval != PAM_SUCCESS || uname == NULL) {
  180. pam_syslog(pamh, LOG_ERR,
  181. "could not identify user (from uid=%lu)",
  182. (unsigned long int)getuid());
  183. return PAM_USER_UNKNOWN;
  184. }
  185. retval = _unix_verify_user(pamh, ctrl, uname, &daysleft);
  186. if (on(UNIX_NO_PASS_EXPIRY, ctrl)) {
  187. const void *pretval = NULL;
  188. int authrv = PAM_AUTHINFO_UNAVAIL; /* authentication not called */
  189. if (pam_get_data(pamh, "unix_setcred_return", &pretval) == PAM_SUCCESS
  190. && pretval)
  191. authrv = *(const int *)pretval;
  192. if (authrv != PAM_SUCCESS
  193. && (retval == PAM_NEW_AUTHTOK_REQD || retval == PAM_AUTHTOK_EXPIRED))
  194. retval = PAM_SUCCESS;
  195. }
  196. switch (retval) {
  197. case PAM_ACCT_EXPIRED:
  198. pam_syslog(pamh, LOG_NOTICE,
  199. "account %s has expired (account expired)",
  200. uname);
  201. _make_remark(pamh, ctrl, PAM_ERROR_MSG,
  202. _("Your account has expired; please contact your system administrator."));
  203. break;
  204. case PAM_NEW_AUTHTOK_REQD:
  205. if (daysleft == 0) {
  206. pam_syslog(pamh, LOG_NOTICE,
  207. "expired password for user %s (root enforced)",
  208. uname);
  209. _make_remark(pamh, ctrl, PAM_ERROR_MSG,
  210. _("You are required to change your password immediately (administrator enforced)."));
  211. } else {
  212. pam_syslog(pamh, LOG_DEBUG,
  213. "expired password for user %s (password aged)",
  214. uname);
  215. _make_remark(pamh, ctrl, PAM_ERROR_MSG,
  216. _("You are required to change your password immediately (password expired)."));
  217. }
  218. break;
  219. case PAM_AUTHTOK_EXPIRED:
  220. pam_syslog(pamh, LOG_NOTICE,
  221. "account %s has expired (failed to change password)",
  222. uname);
  223. _make_remark(pamh, ctrl, PAM_ERROR_MSG,
  224. _("Your account has expired; please contact your system administrator."));
  225. break;
  226. case PAM_AUTHTOK_ERR:
  227. /*
  228. * We ignore "password changed too early" error
  229. * as it is relevant only for password change.
  230. */
  231. retval = PAM_SUCCESS;
  232. /* fallthrough */
  233. case PAM_SUCCESS:
  234. if (daysleft >= 0) {
  235. pam_syslog(pamh, LOG_DEBUG,
  236. "password for user %s will expire in %d days",
  237. uname, daysleft);
  238. #if defined HAVE_DNGETTEXT && defined ENABLE_NLS
  239. snprintf (buf, sizeof (buf),
  240. dngettext(PACKAGE,
  241. "Warning: your password will expire in %d day.",
  242. "Warning: your password will expire in %d days.",
  243. daysleft),
  244. daysleft);
  245. #else
  246. if (daysleft == 1)
  247. snprintf(buf, sizeof (buf),
  248. _("Warning: your password will expire in %d day."),
  249. daysleft);
  250. else
  251. snprintf(buf, sizeof (buf),
  252. /* TRANSLATORS: only used if dngettext is not supported */
  253. _("Warning: your password will expire in %d days."),
  254. daysleft);
  255. #endif
  256. _make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
  257. }
  258. }
  259. D(("all done"));
  260. return retval;
  261. }