From: Sam Hartman Date: Thu, 10 Oct 2002 22:06:02 +0000 (+0000) Subject: Implement krb5_read_password an des_read_pw_string X-Git-Tag: krb5-1.3-alpha1~330 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=08436c6c6017708eaa0a2b986f8cc8193e31da46;p=krb5.git Implement krb5_read_password an des_read_pw_string in terms of krb5_prompter_posix. Change motivated by the desire for echo foo |kinit -4 bar to work in test scripts, but having one implementation of password read functions on unix is good anyway git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@14921 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/des425/ChangeLog b/src/lib/des425/ChangeLog index 70ee74139..6b36d425f 100644 --- a/src/lib/des425/ChangeLog +++ b/src/lib/des425/ChangeLog @@ -1,3 +1,7 @@ +2002-10-10 Sam Hartman + + * read_passwd.c : Implement in terms of krb5_prompter_posix + 2002-09-26 Tom Yu * cksum.c (des_cbc_cksum): Update API for KfM merge. diff --git a/src/lib/des425/read_passwd.c b/src/lib/des425/read_passwd.c index 0a56f4822..03efe698e 100644 --- a/src/lib/des425/read_passwd.c +++ b/src/lib/des425/read_passwd.c @@ -36,28 +36,12 @@ #include "des.h" #include #include -#include -#include -#ifndef ECHO_PASSWORD -#include -#endif /* ECHO_PASSWORD */ - -static jmp_buf pwd_jump; - -static krb5_sigtype intr_routine (int); - -static krb5_sigtype -intr_routine(signo) - int signo; -{ - longjmp(pwd_jump, 1); - /*NOTREACHED*/ -} - +#include /* This is re-declared here because des.h might not declare it. */ int KRB5_CALLCONV des_read_pw_string(char *, int, char *, int); static int des_rd_pwstr_2prompt(char *, int, char *, char *); + /*** Routines ****************************************************** */ static int des_rd_pwstr_2prompt(return_pwd, bufsize_in, prompt, prompt2) @@ -66,106 +50,41 @@ des_rd_pwstr_2prompt(return_pwd, bufsize_in, prompt, prompt2) char *prompt; char *prompt2; { - char *volatile readin_string = 0; - register char *ptr; - int scratchchar; - krb5_sigtype (*volatile ointrfunc)(); - int errcode; - size_t bufsize = bufsize_in; -#ifndef ECHO_PASSWORD - struct termios echo_control, save_control; - int fd; - - /* get the file descriptor associated with stdin */ - fd=fileno(stdin); - - if (tcgetattr(fd, &echo_control) == -1) - return errno; - - save_control = echo_control; - echo_control.c_lflag &= ~(ECHO|ECHONL); - - if (tcsetattr(fd, TCSANOW, &echo_control) == -1) - return errno; -#endif /* ECHO_PASSWORD */ - - if (setjmp(pwd_jump)) { - errcode = -1; /* we were interrupted... */ - goto cleanup; - } - /* save intrfunc */ - ointrfunc = signal(SIGINT, intr_routine); - - /* put out the prompt */ - (void) fputs(prompt,stdout); - (void) fflush(stdout); - (void) memset(return_pwd, 0, bufsize); - - if (fgets(return_pwd, bufsize_in, stdin) == NULL) { - (void) putchar('\n'); - errcode = -1; - goto cleanup; - } - (void) putchar('\n'); - /* fgets always null-terminates the returned string */ - - /* replace newline with null */ - if ((ptr = strchr(return_pwd, '\n'))) - *ptr = '\0'; - else /* flush rest of input line */ - do { - scratchchar = getchar(); - } while (scratchchar != EOF && scratchchar != '\n'); - - if (prompt2) { - /* put out the prompt */ - (void) fputs(prompt2,stdout); - (void) fflush(stdout); - readin_string = malloc(bufsize); - if (!readin_string) { - errcode = ENOMEM; - goto cleanup; - } - (void) memset((char *)readin_string, 0, bufsize); - if (fgets((char *)readin_string, bufsize_in, stdin) == NULL) { - (void) putchar('\n'); - errcode = -1; - goto cleanup; - } - (void) putchar('\n'); - - if ((ptr = strchr((char *)readin_string, '\n'))) - *ptr = '\0'; - else /* need to flush */ - do { - scratchchar = getchar(); - } while (scratchchar != EOF && scratchchar != '\n'); - - /* compare */ - if (strncmp(return_pwd, (char *)readin_string, bufsize)) { - errcode = -1; - goto cleanup; + krb5_data reply_data; + krb5_prompt k5prompt; + krb5_error_code retval; + reply_data.length = bufsize_in; + reply_data.data = return_pwd; + k5prompt.prompt = prompt; + k5prompt.hidden = 1; + k5prompt.reply = &reply_data; + retval = krb5_prompter_posix(NULL, + NULL, NULL, NULL, 1, &k5prompt); + + if ((retval==0) && prompt2) { + krb5_data verify_data; + verify_data.data = malloc(bufsize_in); + verify_data.length = bufsize_in; + k5prompt.prompt = prompt2; + k5prompt.reply = &verify_data; + if (!verify_data.data) + return ENOMEM; + retval = krb5_prompter_posix(NULL, + NULL,NULL, NULL, 1, &k5prompt); + if (retval) { + free(verify_data.data); + } else { + /* compare */ + if (strncmp(return_pwd, (char *)verify_data.data, bufsize_in)) { + retval = KRB5_LIBOS_BADPWDMATCH; + free(verify_data.data); + } } } - - errcode = 0; - -cleanup: - (void) signal(SIGINT, ointrfunc); -#ifndef ECHO_PASSWORD - if ((tcsetattr(fd, TCSANOW, &save_control) == -1) && - errcode == 0) - return errno; -#endif - if (readin_string) { - memset((char *)readin_string, 0, bufsize); - krb5_xfree(readin_string); - } - if (errcode) - memset(return_pwd, 0, bufsize); - return errcode; + return retval; } + int KRB5_CALLCONV des_read_password(k,prompt,verify) mit_des_cblock *k; diff --git a/src/lib/krb5/os/ChangeLog b/src/lib/krb5/os/ChangeLog index 99b90cda3..5f5bf002c 100644 --- a/src/lib/krb5/os/ChangeLog +++ b/src/lib/krb5/os/ChangeLog @@ -1,7 +1,13 @@ +2002-10-10 Sam Hartman + + * read_pwd.c (krb5_read_password): Reimplement in terms of krb5_prompter_posix for unix + + 2002-09-26 Ken Raeburn * sendto_kdc.c (krb5int_cm_call_select): Fix last change. + 2002-09-19 Ken Raeburn * sendto_kdc.c (krb5int_cm_call_select): If timeout value has diff --git a/src/lib/krb5/os/read_pwd.c b/src/lib/krb5/os/read_pwd.c index 4b60bafbf..e113df469 100644 --- a/src/lib/krb5/os/read_pwd.c +++ b/src/lib/krb5/os/read_pwd.c @@ -40,119 +40,41 @@ #include #endif /* ECHO_PASSWORD */ -static jmp_buf pwd_jump; - -static krb5_sigtype -intr_routine(int signo) -{ - longjmp(pwd_jump, 1); - /*NOTREACHED*/ -} - krb5_error_code -krb5_read_password(krb5_context context, const char *prompt, const char *prompt2, char *return_pwd, unsigned int *size_return) +krb5_read_password(krb5_context context, const char *prompt, const char *prompt2, char *return_pwd, unsigned int *bufsize_in) { - /* adapted from Kerberos v4 des/read_password.c */ - /* readin_string is used after a longjmp, so must be volatile */ - char *volatile readin_string = 0; - register char *ptr; - int scratchchar; - krb5_sigtype (*volatile ointrfunc)(); - krb5_error_code errcode; -#ifndef ECHO_PASSWORD - struct termios echo_control, save_control; - int fd; - - /* get the file descriptor associated with stdin */ - fd=fileno(stdin); - - if (tcgetattr(fd, &echo_control) == -1) - return errno; - - save_control = echo_control; - echo_control.c_lflag &= ~(ECHO|ECHONL); - - if (tcsetattr(fd, TCSANOW, &echo_control) == -1) - return errno; -#endif /* ECHO_PASSWORD */ - - if (setjmp(pwd_jump)) { - errcode = KRB5_LIBOS_PWDINTR; /* we were interrupted... */ - goto cleanup; - } - /* save intrfunc */ - ointrfunc = signal(SIGINT, intr_routine); - - /* put out the prompt */ - (void) fputs(prompt,stdout); - (void) fflush(stdout); - (void) memset(return_pwd, 0, *size_return); - - if (fgets(return_pwd, (int) *size_return, stdin) == NULL) { - (void) putchar('\n'); - errcode = KRB5_LIBOS_CANTREADPWD; - goto cleanup; - } - (void) putchar('\n'); - /* fgets always null-terminates the returned string */ - - /* replace newline with null */ - if ((ptr = strchr(return_pwd, '\n'))) - *ptr = '\0'; - else /* flush rest of input line */ - do { - scratchchar = getchar(); - } while (scratchchar != EOF && scratchchar != '\n'); - - if (prompt2) { - /* put out the prompt */ - (void) fputs(prompt2,stdout); - (void) fflush(stdout); - readin_string = malloc(*size_return); - if (!readin_string) { - errcode = ENOMEM; - goto cleanup; - } - (void) memset((char *)readin_string, 0, *size_return); - if (fgets((char *)readin_string, (int) *size_return, stdin) == NULL) { - (void) putchar('\n'); - errcode = KRB5_LIBOS_CANTREADPWD; - goto cleanup; - } - (void) putchar('\n'); - - if ((ptr = strchr((char *)readin_string, '\n'))) - *ptr = '\0'; - else /* need to flush */ - do { - scratchchar = getchar(); - } while (scratchchar != EOF && scratchchar != '\n'); - - /* compare */ - if (strncmp(return_pwd, (char *)readin_string, *size_return)) { - errcode = KRB5_LIBOS_BADPWDMATCH; - goto cleanup; + krb5_data reply_data; + krb5_prompt k5prompt; + krb5_error_code retval; + reply_data.length = *bufsize_in; + reply_data.data = return_pwd; + k5prompt.prompt = (const char *) prompt; + k5prompt.hidden = 1; + k5prompt.reply = &reply_data; + retval = krb5_prompter_posix(NULL, + NULL, NULL, NULL, 1, &k5prompt); + + if ((retval==0) && prompt2) { + krb5_data verify_data; + verify_data.data = malloc(*bufsize_in); + verify_data.length = *bufsize_in; + k5prompt.prompt = (const char *) prompt2; + k5prompt.reply = &verify_data; + if (!verify_data.data) + return ENOMEM; + retval = krb5_prompter_posix(NULL, + NULL,NULL, NULL, 1, &k5prompt); + if (retval) { + free(verify_data.data); + } else { + /* compare */ + if (strncmp(return_pwd, (char *)verify_data.data, *bufsize_in)) { + retval = KRB5_LIBOS_BADPWDMATCH; + free(verify_data.data); + } } } - - errcode = 0; - -cleanup: - (void) signal(SIGINT, ointrfunc); -#ifndef ECHO_PASSWORD - if ((tcsetattr(fd, TCSANOW, &save_control) == -1) && - errcode == 0) - return errno; -#endif - if (readin_string) { - memset((char *)readin_string, 0, *size_return); - krb5_xfree(readin_string); - } - if (errcode) - memset(return_pwd, 0, *size_return); - else - *size_return = strlen(return_pwd); - return errcode; + return retval; } #endif