123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /*
- * Header Files
- */
- #include <stdlib.h>
- #include <string.h>
- #include <security/pam_appl.h>
- /*
- * Local function declarations
- */
- static int login_conv(int num_msg, const struct pam_message **msg,
- struct pam_response **response, void *appdata_ptr);
- /*
- * Local structures and variables
- */
- static struct pam_conv pam_conv = {login_conv, NULL};
- static char *saved_user_passwd;
- static pam_handle_t *pamh;
- static int PamStart(const char *service_name, const char *user,
- const char *display_name)
- {
- int status;
- char *colon, *hostname;
- if (pamh) {
- if (service_name)
- status = pam_set_item(pamh, PAM_SERVICE, service_name);
- if (status != PAM_SUCCESS && user) pam_set_item(pamh, PAM_USER, user);
- }
- else {
- status = pam_start(service_name, user, &pam_conv, &pamh);
- }
- if (status != PAM_SUCCESS) goto done;
- if (!display_name) goto done;
- if (display_name[0] == ':') {
- status = pam_set_item(pamh, PAM_TTY, display_name);
- goto done;
- }
- if (!(hostname = strdup(display_name))) {
- status = PAM_BUF_ERR;
- goto done;
- }
- if (colon = strrchr(hostname, ':')) *colon = '\0';
- status = pam_set_item(pamh, PAM_RHOST, hostname);
- free(hostname);
- done:
- if (status != PAM_SUCCESS && pamh && pam_end(pamh, status) == PAM_SUCCESS)
- pamh = NULL;
- return status;
- }
- /**
- * @brief Authenticate that user / password combination is legal for this
- * system.
- *
- * @param service_name
- * @param user
- * @param display_name
- * @param user_passwd
- *
- * @return See pam_authenticate.
- */
- int _DtSvcPamAuthenticate(const char *service_name, const char *user,
- const char *display_name, const char *user_passwd)
- {
- int status;
- if (!user_passwd) return PAM_AUTH_ERR;
- if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
- return status;
- saved_user_passwd = (char *) user_passwd;
- return pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK);
- }
- /**
- * @brief Start PAM session management.
- *
- * @param service_name
- * @param user
- * @param display_name
- *
- * @return See pam_open_session.
- */
- int _DtSvcPamOpenSession(const char *service_name, const char *user,
- const char *display_name)
- {
- int status;
- if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
- return status;
- return pam_open_session(pamh, 0);
- }
- /**
- * @brief Terminate PAM session management.
- *
- * @param service_name
- * @param user
- * @param display_name
- *
- * @return See pam_close_session.
- */
- int _DtSvcPamCloseSession(const char *service_name, const char *user,
- const char *display_name)
- {
- int status;
- if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
- return status;
- return pam_close_session(pamh, 0);
- }
- /**
- * @brief Set Users login credentials.
- *
- * @param service_name
- * @param user
- * @param display_name
- *
- * @return See pam_setcred.
- */
- int _DtSvcPamSetcred(const char *service_name, const char *user,
- const char *display_name)
- {
- int status;
- if ((status = PamStart(service_name, user, display_name)) != PAM_SUCCESS)
- return status;
- return pam_setcred(pamh, PAM_ESTABLISH_CRED);
- }
- /*****************************************************************************
- * login_conv():
- *
- * This is a conv (conversation) function called from the PAM
- * authentication scheme. It returns the user's password when requested by
- * internal PAM authentication modules and also logs any internal PAM error
- * messages.
- *****************************************************************************/
- static int login_conv(int num_msg, const struct pam_message **msg,
- struct pam_response **response, void *appdata_ptr)
- {
- const struct pam_message *m;
- struct pam_response *r;
- char *temp;
- int k;
- #ifdef lint
- conv_id = conv_id;
- #endif
- if (num_msg <= 0)
- return (PAM_CONV_ERR);
- *response = (struct pam_response*)
- calloc(num_msg, sizeof (struct pam_response));
- if (*response == NULL)
- return (PAM_BUF_ERR);
- k = num_msg;
- m = *msg;
- r = *response;
- while (k--) {
- switch (m->msg_style) {
- case PAM_PROMPT_ECHO_OFF:
- if (saved_user_passwd != NULL) {
- r->resp = (char *) malloc(strlen(saved_user_passwd)+1);
- if (r->resp == NULL) {
- /* __pam_free_resp(num_msg, *response); */
- *response = NULL;
- return (PAM_BUF_ERR);
- }
- (void) strcpy(r->resp, saved_user_passwd);
- r->resp_retcode=0;
- }
- m++;
- r++;
- break;
- case PAM_ERROR_MSG:
- m++;
- r++;
- break;
- case PAM_TEXT_INFO:
- m++;
- r++;
- break;
- default:
- break;
- }
- }
- return (PAM_SUCCESS);
- }
|