21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

et[ctr] = ch;<br />

if (ctr && !(ctr & BUF_STEP)) {<br />

if (!(tmp = (unsigned char *)realloc(ret, ctr + BUF_STEP + 1))) {<br />

free(ret);<br />

return 0;<br />

}<br />

ret = tmp;<br />

}<br />

ctr++;<br />

}<br />

}<br />

unsigned char *spc_read_password(unsigned char *prompt, FILE *term) {<br />

int close = 0, termfd;<br />

sigset_t saved_signals, set_signals;<br />

unsigned char *retval;<br />

struct termios saved_term, set_term;<br />

if (!term) {<br />

if (!(term = fopen(_PATH_TTY, "r+"))) return 0;<br />

close = 1;<br />

}<br />

termfd = fileno(term);<br />

fprintf(term, "%s", prompt);<br />

fflush(term);<br />

/* Defer interruption when echo is turned off */<br />

sigemptyset(&set_signals);<br />

sigaddset(&set_signals, SIGINT);<br />

sigaddset(&set_signals, SIGTSTP);<br />

sigprocmask(SIG_BLOCK, &set_signals, &saved_signals);<br />

/*Save the current state and set the terminal to not echo */<br />

tcgetattr(termfd, &saved_term);<br />

set_term = saved_term;<br />

set_term.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);<br />

tcsetattr(termfd, TCSAFLUSH, &set_term);<br />

retval = read_password(termfd);<br />

fprintf(term, "\n");<br />

tcsetattr(termfd, TCSAFLUSH, &saved_term);<br />

sigprocmask(SIG_SETMASK, &saved_signals, 0);<br />

if (close) fclose(term);<br />

return retval;<br />

}<br />

Prompting for a password on Windows<br />

On Windows, prompting for a password is as simple as setting the ES_PASSWORD style<br />

flag for an EDIT control. When this flag is set, Windows will not display the charac-<br />

396 | Chapter 8: Authentication and Key Exchange<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!