10Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate * promptusr.c --- prompt user for input/output
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate #include <k5-int.h>
7*781Sgtb #if !defined(_WIN32)
80Sstevel@tonic-gate
90Sstevel@tonic-gate #include <stdio.h>
100Sstevel@tonic-gate #include <stdlib.h>
110Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
120Sstevel@tonic-gate #include <unistd.h>
130Sstevel@tonic-gate #endif
140Sstevel@tonic-gate #include <termios.h>
150Sstevel@tonic-gate #include <signal.h>
160Sstevel@tonic-gate #include <setjmp.h>
170Sstevel@tonic-gate
180Sstevel@tonic-gate typedef struct _krb5_uio {
190Sstevel@tonic-gate krb5_magic magic;
200Sstevel@tonic-gate int flags;
210Sstevel@tonic-gate char * prompt;
220Sstevel@tonic-gate char * response;
230Sstevel@tonic-gate struct _krb5_uio *next;
240Sstevel@tonic-gate } *krb5_uio;
250Sstevel@tonic-gate
260Sstevel@tonic-gate #define KRB5_UIO_GETRESPONSE 0x0001
270Sstevel@tonic-gate #define KRB5_UIO_ECHORESPONSE 0x0002
280Sstevel@tonic-gate #define KRB5_UIO_FREE_PROMPT 0x0004
290Sstevel@tonic-gate
300Sstevel@tonic-gate static jmp_buf pwd_jump;
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*ARGSUSED*/
330Sstevel@tonic-gate static krb5_sigtype
intr_routine(int signo)34*781Sgtb intr_routine(int signo)
350Sstevel@tonic-gate {
360Sstevel@tonic-gate longjmp(pwd_jump, 1);
370Sstevel@tonic-gate /*NOTREACHED*/
380Sstevel@tonic-gate }
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*ARGSUSED*/
410Sstevel@tonic-gate krb5_error_code
krb5_os_get_tty_uio(krb5_context context,krb5_uio uio)42*781Sgtb krb5_os_get_tty_uio(krb5_context context, krb5_uio uio)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate volatile krb5_error_code retval;
450Sstevel@tonic-gate krb5_sigtype (*volatile ointrfunc)();
460Sstevel@tonic-gate krb5_uio p;
470Sstevel@tonic-gate struct termios echo_control, save_control;
480Sstevel@tonic-gate int fd;
490Sstevel@tonic-gate char read_string[BUFSIZ];
500Sstevel@tonic-gate char *cp;
510Sstevel@tonic-gate int ch;
520Sstevel@tonic-gate
530Sstevel@tonic-gate /* get the file descriptor associated with stdin */
540Sstevel@tonic-gate fd=fileno(stdin);
550Sstevel@tonic-gate
560Sstevel@tonic-gate if (tcgetattr(fd, &echo_control) == -1)
570Sstevel@tonic-gate return errno;
580Sstevel@tonic-gate
590Sstevel@tonic-gate save_control = echo_control;
600Sstevel@tonic-gate echo_control.c_lflag &= ~(ECHO|ECHONL);
610Sstevel@tonic-gate
620Sstevel@tonic-gate if (setjmp(pwd_jump)) {
630Sstevel@tonic-gate retval = KRB5_LIBOS_PWDINTR; /* we were interrupted... */
640Sstevel@tonic-gate goto cleanup;
650Sstevel@tonic-gate }
660Sstevel@tonic-gate /* save intrfunc */
670Sstevel@tonic-gate ointrfunc = signal(SIGINT, intr_routine);
680Sstevel@tonic-gate
690Sstevel@tonic-gate for (p = uio; p; p = p->next) {
700Sstevel@tonic-gate if (p->prompt) {
710Sstevel@tonic-gate fputs(p->prompt, stdout);
720Sstevel@tonic-gate fflush(stdout);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate if ((p->flags & KRB5_UIO_GETRESPONSE) == 0)
750Sstevel@tonic-gate continue;
760Sstevel@tonic-gate
770Sstevel@tonic-gate if ((p->flags & KRB5_UIO_ECHORESPONSE) == 0)
780Sstevel@tonic-gate if (tcsetattr(fd, TCSANOW, &echo_control) == -1)
790Sstevel@tonic-gate return errno;
800Sstevel@tonic-gate
810Sstevel@tonic-gate if (fgets(read_string, sizeof(read_string), stdin) == NULL) {
820Sstevel@tonic-gate (void) putchar('\n');
830Sstevel@tonic-gate retval = KRB5_LIBOS_CANTREADPWD;
840Sstevel@tonic-gate goto cleanup;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* replace newline with null */
880Sstevel@tonic-gate if ((cp = strchr(read_string, '\n')))
890Sstevel@tonic-gate *cp = '\0';
900Sstevel@tonic-gate else /* flush rest of input line */
910Sstevel@tonic-gate do {
920Sstevel@tonic-gate ch = getchar();
930Sstevel@tonic-gate } while (ch != EOF && ch != '\n');
940Sstevel@tonic-gate read_string[sizeof(read_string)-1] = 0;
950Sstevel@tonic-gate
960Sstevel@tonic-gate if ((p->response = malloc(strlen(read_string)+1)) == NULL) {
970Sstevel@tonic-gate errno = ENOMEM;
980Sstevel@tonic-gate goto cleanup;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate strcpy(p->response, read_string);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if ((p->flags & KRB5_UIO_ECHORESPONSE) == 0) {
1030Sstevel@tonic-gate (void) putchar('\n');
1040Sstevel@tonic-gate if (tcsetattr(fd, TCSANOW, &save_control) == -1) {
1050Sstevel@tonic-gate retval = errno;
1060Sstevel@tonic-gate goto cleanup;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate retval = 0;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate cleanup:
1130Sstevel@tonic-gate (void) signal(SIGINT, ointrfunc);
1140Sstevel@tonic-gate if (retval) {
1150Sstevel@tonic-gate for (p = uio; p; p = p->next) {
1160Sstevel@tonic-gate if (p->response) {
1170Sstevel@tonic-gate memset(p->response, 0, strlen(p->response));
1180Sstevel@tonic-gate free(p->response);
1190Sstevel@tonic-gate p->response = 0;
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate memset(read_string, 0, sizeof(read_string));
1240Sstevel@tonic-gate tcsetattr(fd, TCSANOW, &save_control);
1250Sstevel@tonic-gate return retval;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*ARGSUSED*/
1290Sstevel@tonic-gate void
krb5_free_uio(krb5_context context,krb5_uio uio)130*781Sgtb krb5_free_uio(krb5_context context, krb5_uio uio)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate krb5_uio p, next;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate for (p = uio; p; p = next) {
1350Sstevel@tonic-gate next = p->next;
1360Sstevel@tonic-gate if (p->prompt && (p->flags & KRB5_UIO_FREE_PROMPT))
1370Sstevel@tonic-gate free(p->prompt);
1380Sstevel@tonic-gate if (p->response)
1390Sstevel@tonic-gate free(p->response);
1400Sstevel@tonic-gate free(p);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate #ifdef TEST_DRIVER
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate struct _krb5_uio uio_a = { 0, KRB5_UIO_GETRESPONSE, "Password 1: " };
1470Sstevel@tonic-gate struct _krb5_uio uio_b = { 0, KRB5_UIO_GETRESPONSE |
1480Sstevel@tonic-gate KRB5_UIO_ECHORESPONSE, "Password 2: " };
1490Sstevel@tonic-gate struct _krb5_uio uio_c = { 0, KRB5_UIO_GETRESPONSE, "Password 3: " };
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate void
main(int argc,char ** argv)1530Sstevel@tonic-gate main(int argc, char **argv)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate uio_a.next = &uio_b;
1560Sstevel@tonic-gate uio_b.next = &uio_c;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate krb5_os_get_tty_uio(0, &uio_a);
1590Sstevel@tonic-gate exit(0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate
164*781Sgtb #endif /* !_MSODS */
165