pam_get_authtok.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <security/pam_ext.h>
  36. #define PROMPT _("Password: ")
  37. /* For Translators: "%s" is replaced with "<service>". */
  38. #define PROMPT_CURRENT_ARG _("Current %s password: ")
  39. #define PROMPT_CURRENT_NOARG _("Current password: ")
  40. /* For Translators: "%s" is replaced with "<service>". */
  41. #define PROMPT_NEW_ARG _("New %s password: ")
  42. #define PROMPT_NEW_NOARG _("New password: ")
  43. /* For Translators: "%s" is replaced with "<service>". */
  44. #define PROMPT_RETYPE_ARG _("Retype new %s password: ")
  45. #define PROMPT_RETYPE_NOARG _("Retype new password: ")
  46. #define MISTYPED_PASS _("Sorry, passwords do not match.")
  47. #define PAM_GETAUTHTOK_NOVERIFY 1
  48. static const char *
  49. get_option (pam_handle_t *pamh, const char *option)
  50. {
  51. int i;
  52. size_t len;
  53. if (option == NULL || pamh == NULL ||
  54. pamh->mod_argc == 0 || pamh->mod_argv == NULL)
  55. return NULL;
  56. len = strlen (option);
  57. for (i = 0; i < pamh->mod_argc; i++)
  58. {
  59. if (strncmp (option, pamh->mod_argv[i], len) == 0)
  60. {
  61. if (pamh->mod_argv[i][len] == '=')
  62. return &(pamh->mod_argv[i][len+1]);
  63. else if (pamh->mod_argv[i][len] == '\0')
  64. return "";
  65. }
  66. }
  67. return NULL;
  68. }
  69. static int
  70. pam_get_authtok_internal (pam_handle_t *pamh, int item,
  71. const char **authtok, const char *prompt,
  72. unsigned int flags)
  73. {
  74. char *resp[2] = {NULL, NULL};
  75. const void *prevauthtok;
  76. const char *authtok_type = "";
  77. int chpass = 0; /* Password change, ask twice for it */
  78. int retval;
  79. if (authtok == NULL)
  80. return PAM_SYSTEM_ERR;
  81. /* PAM_AUTHTOK in password stack returns new password,
  82. which needs to be verified. */
  83. if (pamh->choice == PAM_CHAUTHTOK)
  84. {
  85. if (item == PAM_AUTHTOK)
  86. {
  87. chpass = 1;
  88. if (!(flags & PAM_GETAUTHTOK_NOVERIFY))
  89. ++chpass;
  90. }
  91. authtok_type = get_option (pamh, "authtok_type");
  92. if (authtok_type == NULL)
  93. {
  94. retval = pam_get_item (pamh, PAM_AUTHTOK_TYPE, (const void **)&authtok_type);
  95. if (retval != PAM_SUCCESS || authtok_type == NULL)
  96. authtok_type = "";
  97. }
  98. else
  99. pam_set_item(pamh, PAM_AUTHTOK_TYPE, authtok_type);
  100. }
  101. retval = pam_get_item (pamh, item, &prevauthtok);
  102. if (retval == PAM_SUCCESS && prevauthtok != NULL)
  103. {
  104. *authtok = prevauthtok;
  105. return PAM_SUCCESS;
  106. }
  107. else if (get_option (pamh, "use_first_pass") ||
  108. (chpass && get_option (pamh, "use_authtok")))
  109. {
  110. if (prevauthtok == NULL)
  111. {
  112. if (chpass)
  113. return PAM_AUTHTOK_ERR;
  114. else
  115. return PAM_AUTH_ERR;
  116. }
  117. else
  118. return retval;
  119. }
  120. if (prompt != NULL)
  121. {
  122. retval = pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[0],
  123. "%s", prompt);
  124. if (retval == PAM_SUCCESS && chpass > 1 && resp[0] != NULL)
  125. retval = pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[1],
  126. _("Retype %s"), prompt);
  127. }
  128. else if (chpass)
  129. {
  130. pamh->authtok_verified = 0;
  131. retval = *authtok_type ?
  132. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[0],
  133. PROMPT_NEW_ARG, authtok_type) :
  134. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[0],
  135. "%s", PROMPT_NEW_NOARG);
  136. if (retval == PAM_SUCCESS && chpass > 1 && resp[0] != NULL)
  137. {
  138. retval = *authtok_type ?
  139. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[1],
  140. PROMPT_RETYPE_ARG, authtok_type) :
  141. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[1],
  142. "%s", PROMPT_RETYPE_NOARG);
  143. }
  144. }
  145. else if (item == PAM_OLDAUTHTOK)
  146. {
  147. retval = *authtok_type ?
  148. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[0],
  149. PROMPT_CURRENT_ARG, authtok_type) :
  150. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[0],
  151. "%s", PROMPT_CURRENT_NOARG);
  152. }
  153. else
  154. retval = pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp[0], "%s", PROMPT);
  155. if (retval != PAM_SUCCESS || resp[0] == NULL ||
  156. (chpass > 1 && resp[1] == NULL))
  157. {
  158. /* We want to abort */
  159. if (chpass)
  160. pam_error (pamh, _("Password change has been aborted."));
  161. return PAM_AUTHTOK_ERR;
  162. }
  163. if (chpass > 1 && strcmp (resp[0], resp[1]) != 0)
  164. {
  165. pam_error (pamh, MISTYPED_PASS);
  166. _pam_overwrite (resp[0]);
  167. _pam_drop (resp[0]);
  168. _pam_overwrite (resp[1]);
  169. _pam_drop (resp[1]);
  170. return PAM_TRY_AGAIN;
  171. }
  172. _pam_overwrite (resp[1]);
  173. _pam_drop (resp[1]);
  174. retval = pam_set_item (pamh, item, resp[0]);
  175. _pam_overwrite (resp[0]);
  176. _pam_drop (resp[0]);
  177. if (retval != PAM_SUCCESS)
  178. return retval;
  179. if (chpass > 1)
  180. pamh->authtok_verified = 1;
  181. return pam_get_item(pamh, item, (const void **)authtok);
  182. }
  183. int
  184. pam_get_authtok (pam_handle_t *pamh, int item, const char **authtok,
  185. const char *prompt)
  186. {
  187. return pam_get_authtok_internal (pamh, item, authtok, prompt, 0);
  188. }
  189. int
  190. pam_get_authtok_noverify (pam_handle_t *pamh, const char **authtok,
  191. const char *prompt)
  192. {
  193. return pam_get_authtok_internal (pamh, PAM_AUTHTOK, authtok, prompt,
  194. PAM_GETAUTHTOK_NOVERIFY);
  195. }
  196. int
  197. pam_get_authtok_verify (pam_handle_t *pamh, const char **authtok,
  198. const char *prompt)
  199. {
  200. char *resp = NULL;
  201. const char *authtok_type = "";
  202. int retval;
  203. if (authtok == NULL || pamh->choice != PAM_CHAUTHTOK)
  204. return PAM_SYSTEM_ERR;
  205. if (pamh->authtok_verified)
  206. return pam_get_item (pamh, PAM_AUTHTOK, (const void **)authtok);
  207. if (prompt != NULL)
  208. {
  209. retval = pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp,
  210. _("Retype %s"), prompt);
  211. }
  212. else
  213. {
  214. retval = pam_get_item (pamh, PAM_AUTHTOK_TYPE, (const void **)&authtok_type);
  215. if (retval != PAM_SUCCESS || authtok_type == NULL)
  216. authtok_type = "";
  217. retval = *authtok_type ?
  218. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp,
  219. PROMPT_RETYPE_ARG, authtok_type) :
  220. pam_prompt (pamh, PAM_PROMPT_ECHO_OFF, &resp,
  221. "%s", PROMPT_RETYPE_NOARG);
  222. }
  223. if (retval != PAM_SUCCESS || resp == NULL)
  224. {
  225. /* We want to abort the password change */
  226. pam_set_item (pamh, PAM_AUTHTOK, NULL);
  227. pam_error (pamh, _("Password change has been aborted."));
  228. return PAM_AUTHTOK_ERR;
  229. }
  230. if (strcmp (*authtok, resp) != 0)
  231. {
  232. pam_set_item (pamh, PAM_AUTHTOK, NULL);
  233. pam_error (pamh, MISTYPED_PASS);
  234. _pam_overwrite (resp);
  235. _pam_drop (resp);
  236. return PAM_TRY_AGAIN;
  237. }
  238. retval = pam_set_item (pamh, PAM_AUTHTOK, resp);
  239. _pam_overwrite (resp);
  240. _pam_drop (resp);
  241. if (retval != PAM_SUCCESS)
  242. return retval;
  243. pamh->authtok_verified = 1;
  244. return pam_get_item(pamh, PAM_AUTHTOK, (const void **)authtok);
  245. }