2009-04-29 01:36:46 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 2009 - 2010, Nicolas François
|
2009-04-29 01:36:46 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2009-04-29 01:36:46 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ident "$Id:$"
|
|
|
|
|
2009-05-09 18:45:25 +05:30
|
|
|
#ifdef USE_PAM
|
2009-04-29 01:36:46 +05:30
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include "prototypes.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2009-04-29 01:36:46 +05:30
|
|
|
|
* libmisc/console.c, libmisc/motd.c, libmisc/setupenv.c,
libmisc/sulog.c, libmisc/hushed.c, libmisc/failure.c,
libmisc/loginprompt.c, libmisc/ttytype.c,
libmisc/pam_pass_non_interractive.c, src/userdel.c, src/login.c,
lib/commonio.c, lib/commonio.h: Fix some const issues.
* libmisc/motd.c: Avoid multi-statements lines.
* libmisc/motd.c: Support long MOTD_FILE.
* libmisc/list.c, lib/prototypes.h: Revert previous change.
dup_list and is_on_list are used with members as defined for the
group structure, and thus even if the list is not modified, the
list elements cannot be constant strings.
* libmisc/system.c: Avoid C++ comments.
* src/vipw.c: WITH_TCB cannot be tested inside a gettextized
string. Split the Usage string.
* lib/commonio.h: Re-indent.
2010-08-21 21:02:53 +05:30
|
|
|
/*@null@*/ /*@only@*/static const char *non_interactive_password = NULL;
|
2009-05-09 18:45:25 +05:30
|
|
|
static int ni_conv (int num_msg,
|
|
|
|
const struct pam_message **msg,
|
|
|
|
struct pam_response **resp,
|
|
|
|
unused void *appdata_ptr);
|
|
|
|
static struct pam_conv non_interactive_pam_conv = {
|
|
|
|
ni_conv,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-04-29 01:36:46 +05:30
|
|
|
|
|
|
|
|
|
|
|
static int ni_conv (int num_msg,
|
|
|
|
const struct pam_message **msg,
|
|
|
|
struct pam_response **resp,
|
2009-05-09 18:45:25 +05:30
|
|
|
unused void *appdata_ptr)
|
|
|
|
{
|
2009-04-29 01:36:46 +05:30
|
|
|
struct pam_response *responses;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
assert (NULL != non_interactive_password);
|
|
|
|
|
|
|
|
if (num_msg <= 0) {
|
|
|
|
return PAM_CONV_ERR;
|
|
|
|
}
|
|
|
|
|
2009-04-29 03:31:20 +05:30
|
|
|
responses = (struct pam_response *) calloc ((size_t) num_msg,
|
2009-04-29 01:36:46 +05:30
|
|
|
sizeof (*responses));
|
|
|
|
if (NULL == responses) {
|
|
|
|
return PAM_CONV_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (count=0; count < num_msg; count++) {
|
|
|
|
responses[count].resp_retcode = 0;
|
|
|
|
|
|
|
|
switch (msg[count]->msg_style) {
|
|
|
|
case PAM_PROMPT_ECHO_ON:
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2009-04-29 01:36:46 +05:30
|
|
|
_("%s: PAM modules requesting echoing are not supported.\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname());
|
2009-04-29 01:36:46 +05:30
|
|
|
goto failed_conversation;
|
|
|
|
case PAM_PROMPT_ECHO_OFF:
|
|
|
|
responses[count].resp = strdup (non_interactive_password);
|
|
|
|
if (NULL == responses[count].resp) {
|
|
|
|
goto failed_conversation;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PAM_ERROR_MSG:
|
|
|
|
if ( (NULL == msg[count]->msg)
|
2021-11-29 05:07:53 +05:30
|
|
|
|| (fprintf (log_get_logfd(), "%s\n", msg[count]->msg) <0)) {
|
2009-04-29 01:36:46 +05:30
|
|
|
goto failed_conversation;
|
|
|
|
}
|
|
|
|
responses[count].resp = NULL;
|
|
|
|
break;
|
|
|
|
case PAM_TEXT_INFO:
|
|
|
|
if ( (NULL == msg[count]->msg)
|
|
|
|
|| (fprintf (stdout, "%s\n", msg[count]->msg) <0)) {
|
|
|
|
goto failed_conversation;
|
|
|
|
}
|
|
|
|
responses[count].resp = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
2021-11-29 05:07:53 +05:30
|
|
|
(void) fprintf (log_get_logfd(),
|
2009-04-29 01:36:46 +05:30
|
|
|
_("%s: conversation type %d not supported.\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), msg[count]->msg_style);
|
2009-04-29 01:36:46 +05:30
|
|
|
goto failed_conversation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*resp = responses;
|
|
|
|
|
|
|
|
return PAM_SUCCESS;
|
|
|
|
|
|
|
|
failed_conversation:
|
|
|
|
for (count=0; count < num_msg; count++) {
|
|
|
|
if (NULL != responses[count].resp) {
|
|
|
|
memset (responses[count].resp, 0,
|
|
|
|
strlen (responses[count].resp));
|
|
|
|
free (responses[count].resp);
|
|
|
|
responses[count].resp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (responses);
|
|
|
|
*resp = NULL;
|
|
|
|
|
|
|
|
return PAM_CONV_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-09 18:45:25 +05:30
|
|
|
/*
|
|
|
|
* Change non interactively the user's password using PAM.
|
|
|
|
*
|
|
|
|
* Return 0 on success, 1 on failure.
|
|
|
|
*/
|
2017-10-23 01:54:32 +05:30
|
|
|
int do_pam_passwd_non_interactive (const char *pam_service,
|
2009-05-09 18:45:25 +05:30
|
|
|
const char *username,
|
|
|
|
const char* password)
|
|
|
|
{
|
|
|
|
pam_handle_t *pamh = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pam_start (pam_service, username, &non_interactive_pam_conv, &pamh);
|
|
|
|
if (ret != PAM_SUCCESS) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2009-05-09 18:45:25 +05:30
|
|
|
_("%s: (user %s) pam_start failure %d\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), username, ret);
|
2009-05-09 18:45:25 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
non_interactive_password = password;
|
|
|
|
ret = pam_chauthtok (pamh, 0);
|
|
|
|
if (ret != PAM_SUCCESS) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2009-05-09 18:45:25 +05:30
|
|
|
_("%s: (user %s) pam_chauthtok() failed, error:\n"
|
|
|
|
"%s\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), username, pam_strerror (pamh, ret));
|
2009-05-09 18:45:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
(void) pam_end (pamh, PAM_SUCCESS);
|
2009-05-09 18:45:38 +05:30
|
|
|
|
|
|
|
return ((PAM_SUCCESS == ret) ? 0 : 1);
|
2009-05-09 18:45:25 +05:30
|
|
|
}
|
|
|
|
#else /* !USE_PAM */
|
|
|
|
extern int errno; /* warning: ANSI C forbids an empty source file */
|
|
|
|
#endif /* !USE_PAM */
|