Commit 7ef6b05e authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

My reimplementation and cleanup of the getpass source code. We officially no

longer use Angus Mackay's getpass code due to the weirdo license his code was
donated to us under.
parent 695f95aa
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -1263,13 +1263,12 @@ AC_CHECK_FUNCS( strtoll \
                inet_addr \
                inet_ntoa \
                inet_pton \
                tcsetattr \
                tcgetattr \
                perror \
                closesocket \
                siginterrupt \
                sigaction \
                signal \
                getpass \
                getpass_r \
                strlcat \
                getpwuid \
+154 −168
Original line number Diff line number Diff line
/* ============================================================================
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Redistribution and use are freely permitted provided that:
 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 *   1) This header remain in tact.
 *   2) The prototypes for getpass and getpass_r are not changed from:
 *         char *getpass(const char *prompt)
 *         char *getpass_r(const char *prompt, char* buffer, int buflen)
 *   3) This source code is not used outside of this(getpass.c) file.
 *   4) Any changes to this(getpass.c) source code are made publicly available.
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * ============================================================================
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * $Id$
 *
 * The spirit of this license is to allow use of this source code in any
 * project be it open or closed but still encourage the use of the open,
 * library based equivilents.
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * Author(s):
 *   Angus Mackay <amackay@gus.ml.org>
 *   Daniel Stenberg <daniel@haxx.se>
 */
 * $Id$
 ***************************************************************************/

#include "setup.h" /* setup.h is required for read() prototype */
/* This file is a reimplementation of the previous one, due to license
   problems. */

#include "setup.h"

#ifndef HAVE_GETPASS_R
/* this file is only for systems without getpass_r() */

#include <stdio.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "getpass.h"

#ifndef WIN32
#ifdef HAVE_GETPASS
char *getpass_r(const char *prompt, char *password, size_t passlen)
{
  char *ptr = getpass(prompt);
  strncpy(password, ptr, passlen);
  password[passlen-1]=0;
  return password;
}
#define DONE
#else
/* the rest of this file is only for systems without getpass() */

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#else
#ifdef HAVE_TERMIO_H
#include <termio.h>
#endif
#endif

/* The last #include file should be: */
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
#include "memdebug.h"
#endif

#ifdef VMS
#include <stdio.h>
#include <string.h>
/* VMS implementation */
#include descrip
#include starlet
#include iodef
@@ -78,167 +104,127 @@ char *getpass_r(const char *prompt, char *buffer, size_t buflen)
  }
  return buffer; /* we always return success */
}
#else /* VMS */
#ifdef HAVE_TERMIOS_H
#  if !defined(HAVE_TCGETATTR) && !defined(HAVE_TCSETATTR)
#    undef HAVE_TERMIOS_H
#  endif
#endif

#ifndef RETSIGTYPE
#  define RETSIGTYPE void
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <signal.h>
#ifdef HAVE_TERMIOS_H
#  include <termios.h>
#else
#  ifdef HAVE_TERMIO_H
#  include <termio.h>
#  else
#  endif
#endif

/* The last #include file should be: */
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
#include "memdebug.h"
#endif
#define DONE
#endif /* VMS */

#ifdef WIN32
/* Windows implementation */
#include <conio.h>
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
{
  FILE *infp;
  char infp_fclose = 0;
  FILE *outfp;
  RETSIGTYPE (*sigint)(int);
#ifdef SIGTSTP
  RETSIGTYPE (*sigtstp)(int);
#endif
  size_t bytes_read;
  int infd;
  int outfd;
#ifdef HAVE_TERMIOS_H
  struct termios orig;
  struct termios noecho;
#else
#  ifdef HAVE_TERMIO_H
  struct termio orig;
  struct termio noecho;
#  else
#  endif
#endif

  sigint = signal(SIGINT, SIG_IGN);
#ifdef SIGTSTP
  sigtstp = signal(SIGTSTP, SIG_IGN);
#endif
  size_t i;
  fputs(prompt, stderr);

  infp=fopen("/dev/tty", "r");
  if( NULL == infp )
    infp = stdin;
  for(i=0; i<buflen; i++) {
    buffer[i] = getch();
    if ( buffer[i] == '\r' ) {
      buffer[i] = 0;
      break;
    }
    else
    infp_fclose = 1;
      if ( buffer[i] == '\b')
        /* remove this letter and if this is not the first key, remove the
           previous one as well */
        i = i - (i>=1?2:1);
  }
  /* if user didn't hit ENTER, terminate buffer */
  if (i==buflen)
    buffer[buflen-1]=0;

  outfp = stderr;
  return buffer; /* we always return success */
}
#define DONE
#endif /* WIN32 */

  infd = fileno(infp);
  outfd = fileno(outfp);
#ifndef DONE /* not previously provided */

  /* dissable echo */
#ifdef HAVE_TERMIOS_H
  tcgetattr(outfd, &orig);

  noecho = orig;
  noecho.c_lflag &= ~ECHO;
  tcsetattr(outfd, TCSANOW, &noecho);
#define struct_term struct termios
#else
#ifdef HAVE_TERMIO_H
  ioctl(outfd, TCGETA, &orig);
  noecho = orig;
  noecho.c_lflag &= ~ECHO;
  ioctl(outfd, TCSETA, &noecho);
#define struct_term  struct termio
#else
#undef struct_term
#endif
#endif

  fputs(prompt, outfp);
  fflush(outfp);

  bytes_read=read(infd, buffer, buflen);
  buffer[bytes_read > 0 ? (bytes_read -1) : 0] = '\0';

  /* print a new line if needed */
static bool ttyecho(bool enable, int fd)
{
#ifdef struct_term
  static struct_term withecho;
  static struct_term noecho;
#endif
  if(!enable) {
  /* dissable echo by extracting the current 'withecho' mode and remove the
     ECHO bit and set back the struct */
#ifdef HAVE_TERMIOS_H
  fputs("\n", outfp);
#else
    tcgetattr(fd, &withecho);
    noecho = withecho;
    noecho.c_lflag &= ~ECHO;
    tcsetattr(fd, TCSANOW, &noecho);
#else /* HAVE_TERMIOS_H */
#ifdef HAVE_TERMIO_H
  fputs("\n", outfp);
#  else
    ioctl(fd, TCGETA, &withecho);
    noecho = withecho;
    noecho.c_lflag &= ~ECHO;
    ioctl(fd, TCSETA, &noecho);
#else /* HAVE_TERMIO_H */
/* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H, we can't disable echo! */
    (void)fd; /* prevent compiler warning on unused variable */
    return FALSE; /* not disabled */
#endif
#endif

  /*
   * reset term charectaristics, use TCSAFLUSH incase the
   * user types more than buflen
   */
    return TRUE; /* disabled */
  }
  else {
    /* re-enable echo, assumes we disabled it before (and set the structs we
       now use to reset the terminal status) */
#ifdef HAVE_TERMIOS_H
  tcsetattr(outfd, TCSAFLUSH, &orig);
#else
    tcsetattr(fd, TCSAFLUSH, &withecho);
#else /* HAVE_TERMIOS_H */
#ifdef HAVE_TERMIO_H
  ioctl(outfd, TCSETA, &orig);
    ioctl(fd, TCSETA, &withecho);
#else
/* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H */
    return FALSE; /* not enabled */
#endif
#endif

  signal(SIGINT, sigint);
#ifdef SIGTSTP
  signal(SIGTSTP, sigtstp);
#endif

  if(infp_fclose)
    fclose(infp);

  return buffer; /* we always return success */
    return TRUE; /* enabled */
  }
#endif /* VMS */
#else /* WIN32 */
#include <stdio.h>
#include <conio.h>
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
{
  size_t i;
  printf("%s", prompt);

  for(i=0; i<buflen; i++) {
    buffer[i] = getch();
    if ( buffer[i] == '\r' ) {
      buffer[i] = 0;
      break;
}

char *getpass_r(const char *prompt, /* prompt to display */
                char *password,     /* buffer to store password in */
                size_t buflen)      /* size of buffer to store password in */
{
  ssize_t nread;
  bool disabled;
  int fd=open("/dev/tty", O_RDONLY);
  if(-1 == fd)
    fd = 1; /* use stdin if the tty couldn't be used */

  disabled = ttyecho(FALSE, fd); /* disable terminal echo */

  fputs(prompt, stderr);
  nread=read(fd, password, buflen);
  if(nread > 0)
    password[--nread]=0; /* zero terminate where enter is stored */
  else
      if ( buffer[i] == '\b')
        /* remove this letter and if this is not the first key, remove the
           previous one as well */
        i = i - (i>=1?2:1);
  }
  /* if user didn't hit ENTER, terminate buffer */
  if (i==buflen)
    buffer[buflen-1]=0;
    password[0]=0; /* got nothing */

  return buffer; /* we always return success */
  if(disabled) {
    /* if echo actually was disabled, add a newline */
    fputs("\n", stderr);
    ttyecho(TRUE, fd); /* enable echo */
  }
#endif

#endif /* ifndef HAVE_GETPASS_R */
  if(1 != fd)
    close(fd);

#if 0
/* for consistensy, here's the old-style function: */
char *getpass(const char *prompt)
{
  static char buf[256];
  return getpass_r(prompt, buf, sizeof(buf));
  return password; /* return pointer to buffer */
}
#endif

#endif /* DONE */

#endif /* HAVE_GETPASS */
#endif /* HAVE_GETPASS_R */
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@
#endif
#endif

#ifdef CURLDEBUG
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
/* This is an ugly hack for CURLDEBUG conditions only. We need to include
   the file here, since it might set the _FILE_OFFSET_BITS define, which must
   be set BEFORE all normal system headers. */
+5 −5

File changed.

Contains only whitespace changes.