Fix same old bug in sample conversation function.

This commit is contained in:
Dag-Erling Smørgrav 2003-12-10 16:40:10 +00:00
parent ec88e446c2
commit 037ce3fa7f
Notes: svn2git 2020-12-08 03:00:23 +00:00
svn path=/head/; revision=19124
2 changed files with 32 additions and 20 deletions

View file

@ -45,29 +45,30 @@ int
converse(int n, const struct pam_message **msg, converse(int n, const struct pam_message **msg,
struct pam_response **resp, void *data) struct pam_response **resp, void *data)
{ {
struct pam_response *aresp;
char buf[PAM_MAX_RESP_SIZE]; char buf[PAM_MAX_RESP_SIZE];
int i; int i;
data = data; data = data;
if (n <= 0 || n > PAM_MAX_NUM_MSG) if (n <= 0 || n > PAM_MAX_NUM_MSG)
return (PAM_CONV_ERR); return (PAM_CONV_ERR);
if ((*resp = calloc(n, sizeof **resp)) == NULL) if ((aresp = calloc(n, sizeof *aresp)) == NULL)
return (PAM_BUF_ERR); return (PAM_BUF_ERR);
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
resp[i]->resp_retcode = 0; aresp[i].resp_retcode = 0;
resp[i]->resp = NULL; aresp[i].resp = NULL;
switch (msg[i]->msg_style) { switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_OFF: case PAM_PROMPT_ECHO_OFF:
resp[i]->resp = strdup(getpass(msg[i]->msg)); aresp[i].resp = strdup(getpass(msg[i]->msg));
if (resp[i]->resp == NULL) if (aresp[i].resp == NULL)
goto fail; goto fail;
break; break;
case PAM_PROMPT_ECHO_ON: case PAM_PROMPT_ECHO_ON:
fputs(msg[i]->msg, stderr); fputs(msg[i]->msg, stderr);
if (fgets(buf, sizeof buf, stdin) == NULL) if (fgets(buf, sizeof buf, stdin) == NULL)
goto fail; goto fail;
resp[i]->resp = strdup(buf); aresp[i].resp = strdup(buf);
if (resp[i]->resp == NULL) if (aresp[i].resp == NULL)
goto fail; goto fail;
break; break;
case PAM_ERROR_MSG: case PAM_ERROR_MSG:
@ -86,11 +87,16 @@ converse(int n, const struct pam_message **msg,
goto fail; goto fail;
} }
} }
*resp = aresp;
return (PAM_SUCCESS); return (PAM_SUCCESS);
fail: fail:
while (i) for (i = 0; i < n; ++i) {
free(resp[--i]); if (aresp[i].resp != NULL) {
free(*resp); memset(aresp[i].resp, 0, strlen(aresp[i].resp));
free(aresp[i].resp);
}
}
memset(aresp, 0, n * sizeof *aresp);
*resp = NULL; *resp = NULL;
return (PAM_CONV_ERR); return (PAM_CONV_ERR);
} }

View file

@ -45,29 +45,30 @@ int
converse(int n, const struct pam_message **msg, converse(int n, const struct pam_message **msg,
struct pam_response **resp, void *data) struct pam_response **resp, void *data)
{ {
struct pam_response *aresp;
char buf[PAM_MAX_RESP_SIZE]; char buf[PAM_MAX_RESP_SIZE];
int i; int i;
data = data; data = data;
if (n <= 0 || n > PAM_MAX_NUM_MSG) if (n <= 0 || n > PAM_MAX_NUM_MSG)
return (PAM_CONV_ERR); return (PAM_CONV_ERR);
if ((*resp = calloc(n, sizeof **resp)) == NULL) if ((aresp = calloc(n, sizeof *aresp)) == NULL)
return (PAM_BUF_ERR); return (PAM_BUF_ERR);
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
resp[i]->resp_retcode = 0; aresp[i].resp_retcode = 0;
resp[i]->resp = NULL; aresp[i].resp = NULL;
switch (msg[i]->msg_style) { switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_OFF: case PAM_PROMPT_ECHO_OFF:
resp[i]->resp = strdup(getpass(msg[i]->msg)); aresp[i].resp = strdup(getpass(msg[i]->msg));
if (resp[i]->resp == NULL) if (aresp[i].resp == NULL)
goto fail; goto fail;
break; break;
case PAM_PROMPT_ECHO_ON: case PAM_PROMPT_ECHO_ON:
fputs(msg[i]->msg, stderr); fputs(msg[i]->msg, stderr);
if (fgets(buf, sizeof buf, stdin) == NULL) if (fgets(buf, sizeof buf, stdin) == NULL)
goto fail; goto fail;
resp[i]->resp = strdup(buf); aresp[i].resp = strdup(buf);
if (resp[i]->resp == NULL) if (aresp[i].resp == NULL)
goto fail; goto fail;
break; break;
case PAM_ERROR_MSG: case PAM_ERROR_MSG:
@ -86,11 +87,16 @@ converse(int n, const struct pam_message **msg,
goto fail; goto fail;
} }
} }
*resp = aresp;
return (PAM_SUCCESS); return (PAM_SUCCESS);
fail: fail:
while (i) for (i = 0; i < n; ++i) {
free(resp[--i]); if (aresp[i].resp != NULL) {
free(*resp); memset(aresp[i].resp, 0, strlen(aresp[i].resp));
free(aresp[i].resp);
}
}
memset(aresp, 0, n * sizeof *aresp);
*resp = NULL; *resp = NULL;
return (PAM_CONV_ERR); return (PAM_CONV_ERR);
} }