*** empty log message ***
authorJohn Kohl <jtkohl@mit.edu>
Thu, 18 Jan 1990 12:19:34 +0000 (12:19 +0000)
committerJohn Kohl <jtkohl@mit.edu>
Thu, 18 Jan 1990 12:19:34 +0000 (12:19 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@117 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/os/read_pwd.c [new file with mode: 0644]

diff --git a/src/lib/krb5/os/read_pwd.c b/src/lib/krb5/os/read_pwd.c
new file mode 100644 (file)
index 0000000..4437cfc
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * $Source$
+ * $Author$
+ *
+ * Copyright 1990 by the Massachusetts Institute of Technology.
+ *
+ * For copying and distribution information, please see the file
+ * <krb5/mit-copyright.h>.
+ *
+ * libos: krb5_read_password for BSD 4.3
+ */
+
+#ifndef        lint
+static char rcsid_read_pwd_c[] =
+"$Id$";
+#endif lint
+
+#include <krb5/copyright.h>
+
+#include <krb5/krb5.h>
+#include <krb5/krb5_err.h>
+
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <errno.h>
+
+#ifdef __STDC__
+#include <stdlib.h>
+#else
+char *malloc(), *index();
+#endif
+
+extern int errno;
+
+#define cleanup(errcode) ioctl(0, TIOCSETP, (char *)&tty_savestate); return errcode;
+
+krb5_error_code
+krb5_read_password(prompt, prompt2, return_pwd, size_return)
+char *prompt;
+char *prompt2;
+char *return_pwd;
+int size_return;
+{
+    /* adapted from Kerberos v4 des/read_password.c */
+
+    struct sgttyb tty_state, tty_savestate;
+    char *readin_string;
+    register char *ptr;
+    int scratchchar;
+
+    /* save terminal state */
+    if (ioctl(0,TIOCGETP,(char *)&tty_savestate) == -1) 
+       return errno;
+
+    tty_state = tty_savestate;
+
+    tty_state.sg_flags &= ~ECHO;
+    if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
+       return errno;
+
+    /* put out the prompt */
+    (void) fputs(prompt,stdout);
+    (void) fflush(stdout);
+    (void) bzero(return_pwd, size_return);
+
+    if (fgets(return_pwd, size_return, stdin) == NULL) {
+       /* error */
+       (void) bzero(return_pwd, size_return);
+       cleanup(KRB5_LIBOS_CANTREADPWD);
+    }
+    /* fgets always null-terminates the returned string */
+
+    /* replace newline with null */
+    if (ptr = index(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) putchar('\n');
+       (void) fputs(prompt2,stdout);
+       (void) fflush(stdout);
+       readin_string = malloc(size_return);
+       if (!readin_string) {
+           (void) bzero(return_pwd, size_return);
+           cleanup(ENOMEM);
+       }
+       (void) bzero(readin_string, size_return);
+       if (fgets(readin_string, size_return, stdin) == NULL) {
+           /* error */
+           (void) bzero(readin_string, size_return);
+           (void) bzero(return_pwd, size_return);
+           free(readin_string);
+           cleanup(KRB5_LIBOS_CANTREADPWD);
+       }
+       if (ptr = index(readin_string, '\n'))
+           *ptr = '\0';
+       else /* need to flush */
+           do {
+               scratchchar = getchar();
+           } while (scratchchar != EOF && scratchchar != '\n');
+       /* compare */
+       if (strncmp(return_pwd, readin_string, size_return)) {
+           (void) bzero(readin_string, size_return);
+           (void) bzero(return_pwd, size_return);
+           free(readin_string);
+           cleanup(KRB5_LIBOS_BADPWDMATCH);
+       }
+       (void) bzero(readin_string, size_return);
+       free(readin_string);
+    }
+    
+    if (ioctl(0, TIOCSETP, (char *)&tty_savestate) == -1)
+       return errno;
+
+    return 0;
+}