pam_echo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2005, 2006 Thorsten Kukuk <kukuk@suse.de>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, and the entire permission notice in its entirety,
  9. * including the disclaimer of warranties.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote
  14. * products derived from this software without specific prior
  15. * written permission.
  16. *
  17. * ALTERNATIVELY, this product may be distributed under the terms of
  18. * the GNU Public License, in which case the provisions of the GPL are
  19. * required INSTEAD OF the above restrictions. (This clause is
  20. * necessary due to a potential bad interaction between the GPL and
  21. * the restrictions contained in a BSD-style copyright.)
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  27. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  31. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  33. * OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #if defined(HAVE_CONFIG_H)
  36. #include "config.h"
  37. #endif
  38. #include <errno.h>
  39. #include <stdio.h>
  40. #include <fcntl.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <limits.h>
  45. #include <syslog.h>
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #ifndef HOST_NAME_MAX
  49. #define HOST_NAME_MAX 255
  50. #endif
  51. #include <security/pam_modules.h>
  52. #include <security/pam_modutil.h>
  53. #include <security/_pam_macros.h>
  54. #include <security/pam_ext.h>
  55. #include "pam_inline.h"
  56. static int
  57. replace_and_print (pam_handle_t *pamh, const char *mesg)
  58. {
  59. char *output;
  60. size_t length = strlen (mesg) + PAM_MAX_MSG_SIZE;
  61. char myhostname[HOST_NAME_MAX+1];
  62. const void *str = NULL;
  63. const char *p, *q;
  64. int item;
  65. size_t len;
  66. output = malloc (length);
  67. if (output == NULL)
  68. {
  69. pam_syslog (pamh, LOG_CRIT, "running out of memory");
  70. return PAM_BUF_ERR;
  71. }
  72. for (p = mesg, len = 0; *p != '\0' && len < length - 1; ++p)
  73. {
  74. if (*p != '%' || p[1] == '\0')
  75. {
  76. output[len++] = *p;
  77. continue;
  78. }
  79. switch (*++p)
  80. {
  81. case 'H':
  82. item = PAM_RHOST;
  83. break;
  84. case 'h':
  85. item = -2; /* aka PAM_LOCALHOST */
  86. break;
  87. case 's':
  88. item = PAM_SERVICE;
  89. break;
  90. case 't':
  91. item = PAM_TTY;
  92. break;
  93. case 'U':
  94. item = PAM_RUSER;
  95. break;
  96. case 'u':
  97. item = PAM_USER;
  98. break;
  99. default:
  100. output[len++] = *p;
  101. continue;
  102. }
  103. if (item == -2)
  104. {
  105. if (gethostname (myhostname, sizeof (myhostname)) == -1)
  106. str = NULL;
  107. else
  108. str = &myhostname;
  109. }
  110. else
  111. {
  112. if (pam_get_item (pamh, item, &str) != PAM_SUCCESS)
  113. str = NULL;
  114. }
  115. if (str == NULL)
  116. str = "(null)";
  117. for (q = str; *q != '\0' && len < length - 1; ++q)
  118. output[len++] = *q;
  119. }
  120. output[len] = '\0';
  121. pam_info (pamh, "%s", output);
  122. free (output);
  123. return PAM_SUCCESS;
  124. }
  125. static int
  126. pam_echo (pam_handle_t *pamh, int flags, int argc, const char **argv)
  127. {
  128. int fd;
  129. int orig_argc = argc;
  130. const char **orig_argv = argv;
  131. const char *file = NULL;
  132. int retval;
  133. if (flags & PAM_SILENT)
  134. return PAM_IGNORE;
  135. for (; argc-- > 0; ++argv)
  136. {
  137. const char *str = pam_str_skip_prefix(*argv, "file=");
  138. if (str != NULL)
  139. file = str;
  140. }
  141. /* No file= option, use argument for output. */
  142. if (file == NULL || file[0] == '\0')
  143. {
  144. char msg[PAM_MAX_MSG_SIZE];
  145. const char *p;
  146. int i;
  147. size_t len;
  148. for (i = 0, len = 0; i < orig_argc && len < sizeof (msg) - 1; ++i)
  149. {
  150. if (i > 0)
  151. msg[len++] = ' ';
  152. for (p = orig_argv[i]; *p != '\0' && len < sizeof(msg) - 1; ++p)
  153. msg[len++] = *p;
  154. }
  155. msg[len] = '\0';
  156. retval = replace_and_print (pamh, msg);
  157. }
  158. else if ((fd = open (file, O_RDONLY, 0)) >= 0)
  159. {
  160. char *mtmp = NULL;
  161. struct stat st;
  162. /* load file into message buffer. */
  163. if ((fstat (fd, &st) < 0) || !st.st_size)
  164. {
  165. close (fd);
  166. return PAM_IGNORE;
  167. }
  168. mtmp = malloc (st.st_size + 1);
  169. if (!mtmp)
  170. {
  171. close (fd);
  172. return PAM_BUF_ERR;
  173. }
  174. if (pam_modutil_read (fd, mtmp, st.st_size) == -1)
  175. {
  176. pam_syslog (pamh, LOG_ERR, "Error while reading %s: %m", file);
  177. free (mtmp);
  178. close (fd);
  179. return PAM_IGNORE;
  180. }
  181. if (mtmp[st.st_size - 1] == '\n')
  182. mtmp[st.st_size - 1] = '\0';
  183. else
  184. mtmp[st.st_size] = '\0';
  185. close (fd);
  186. retval = replace_and_print (pamh, mtmp);
  187. free (mtmp);
  188. }
  189. else
  190. {
  191. pam_syslog (pamh, LOG_ERR, "Cannot open %s: %m", file);
  192. retval = PAM_IGNORE;
  193. }
  194. return retval;
  195. }
  196. int
  197. pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc,
  198. const char **argv)
  199. {
  200. return pam_echo (pamh, flags, argc, argv);
  201. }
  202. int
  203. pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
  204. int argc UNUSED, const char **argv UNUSED)
  205. {
  206. return PAM_IGNORE;
  207. }
  208. int
  209. pam_sm_acct_mgmt (pam_handle_t *pamh, int flags, int argc,
  210. const char **argv)
  211. {
  212. return pam_echo (pamh, flags, argc, argv);
  213. }
  214. int
  215. pam_sm_open_session (pam_handle_t *pamh, int flags, int argc,
  216. const char **argv)
  217. {
  218. return pam_echo (pamh, flags, argc, argv);
  219. }
  220. int
  221. pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
  222. int argc UNUSED, const char **argv UNUSED)
  223. {
  224. return PAM_IGNORE;
  225. }
  226. int
  227. pam_sm_chauthtok (pam_handle_t *pamh, int flags, int argc,
  228. const char **argv)
  229. {
  230. if (flags & PAM_PRELIM_CHECK)
  231. return pam_echo (pamh, flags, argc, argv);
  232. else
  233. return PAM_IGNORE;
  234. }