SvcPam.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * Header Files
  25. */
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <security/pam_appl.h>
  29. /*
  30. * Local function declarations
  31. */
  32. static int login_conv(int num_msg, const struct pam_message **msg,
  33. struct pam_response **response, void *appdata_ptr);
  34. /*
  35. * Local structures and variables
  36. */
  37. static struct pam_conv pam_conv = {login_conv, NULL};
  38. static char *saved_user_passwd;
  39. static pam_handle_t *pamh;
  40. static int PamStart(const char *service_name, const char *user,
  41. const char *display_name)
  42. {
  43. int status;
  44. char *colon, *hostname;
  45. if (pamh) {
  46. if (service_name)
  47. status = pam_set_item(pamh, PAM_SERVICE, service_name);
  48. if (status != PAM_SUCCESS && user) pam_set_item(pamh, PAM_USER, user);
  49. }
  50. else {
  51. status = pam_start(service_name, user, &pam_conv, &pamh);
  52. }
  53. if (status != PAM_SUCCESS) goto done;
  54. if (!display_name) goto done;
  55. if (display_name[0] == ':') {
  56. status = pam_set_item(pamh, PAM_TTY, display_name);
  57. goto done;
  58. }
  59. if (!(hostname = strdup(display_name))) {
  60. status = PAM_BUF_ERR;
  61. goto done;
  62. }
  63. if (colon = strrchr(hostname, ':')) *colon = '\0';
  64. status = pam_set_item(pamh, PAM_RHOST, hostname);
  65. free(hostname);
  66. done:
  67. if (status != PAM_SUCCESS && pamh && pam_end(pamh, status) == PAM_SUCCESS)
  68. pamh = NULL;
  69. return status;
  70. }
  71. /**
  72. * @brief Authenticate that user / password combination is legal for this
  73. * system.
  74. *
  75. * @param service_name
  76. * @param user
  77. * @param display_name
  78. * @param user_passwd
  79. *
  80. * @return See pam_authenticate.
  81. */
  82. int _DtSvcPamAuthenticate(const char *service_name, const char *user,
  83. const char *display_name, const char *user_passwd)
  84. {
  85. int status;
  86. if (!user_passwd) return PAM_AUTH_ERR;
  87. if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
  88. return status;
  89. saved_user_passwd = (char *) user_passwd;
  90. return pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK);
  91. }
  92. /**
  93. * @brief Start PAM session management.
  94. *
  95. * @param service_name
  96. * @param user
  97. * @param display_name
  98. *
  99. * @return See pam_open_session.
  100. */
  101. int _DtSvcPamOpenSession(const char *service_name, const char *user,
  102. const char *display_name)
  103. {
  104. int status;
  105. if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
  106. return status;
  107. return pam_open_session(pamh, 0);
  108. }
  109. /**
  110. * @brief Terminate PAM session management.
  111. *
  112. * @param service_name
  113. * @param user
  114. * @param display_name
  115. *
  116. * @return See pam_close_session.
  117. */
  118. int _DtSvcPamCloseSession(const char *service_name, const char *user,
  119. const char *display_name)
  120. {
  121. int status;
  122. if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
  123. return status;
  124. return pam_close_session(pamh, 0);
  125. }
  126. /**
  127. * @brief Set Users login credentials.
  128. *
  129. * @param service_name
  130. * @param user
  131. * @param display_name
  132. *
  133. * @return See pam_setcred.
  134. */
  135. int _DtSvcPamSetcred(const char *service_name, const char *user,
  136. const char *display_name)
  137. {
  138. int status;
  139. if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
  140. return status;
  141. return pam_setcred(pamh, PAM_ESTABLISH_CRED);
  142. }
  143. /*****************************************************************************
  144. * login_conv():
  145. *
  146. * This is a conv (conversation) function called from the PAM
  147. * authentication scheme. It returns the user's password when requested by
  148. * internal PAM authentication modules and also logs any internal PAM error
  149. * messages.
  150. *****************************************************************************/
  151. static int login_conv(int num_msg, const struct pam_message **msg,
  152. struct pam_response **response, void *appdata_ptr)
  153. {
  154. const struct pam_message *m;
  155. struct pam_response *r;
  156. char *temp;
  157. int k;
  158. #ifdef lint
  159. conv_id = conv_id;
  160. #endif
  161. if (num_msg <= 0)
  162. return (PAM_CONV_ERR);
  163. *response = (struct pam_response*)
  164. calloc(num_msg, sizeof (struct pam_response));
  165. if (*response == NULL)
  166. return (PAM_BUF_ERR);
  167. k = num_msg;
  168. m = *msg;
  169. r = *response;
  170. while (k--) {
  171. switch (m->msg_style) {
  172. case PAM_PROMPT_ECHO_OFF:
  173. if (saved_user_passwd != NULL) {
  174. r->resp = (char *) malloc(strlen(saved_user_passwd)+1);
  175. if (r->resp == NULL) {
  176. /* __pam_free_resp(num_msg, *response); */
  177. *response = NULL;
  178. return (PAM_BUF_ERR);
  179. }
  180. (void) strcpy(r->resp, saved_user_passwd);
  181. r->resp_retcode=0;
  182. }
  183. m++;
  184. r++;
  185. break;
  186. case PAM_ERROR_MSG:
  187. m++;
  188. r++;
  189. break;
  190. case PAM_TEXT_INFO:
  191. m++;
  192. r++;
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. return (PAM_SUCCESS);
  199. }