server/utils.c
/* Read a password from configuration value. */
Read a password from configuration value.
Read a password from configuration value .
int
nbdkit_read_password (const char *value, char **password)
{
  int tty, err;
  struct termios orig, temp;
  ssize_t r;
  size_t n;
  FILE *fp;
  if (strcmp (value, "-") == 0) {
    printf ("password: ");
    tty = isatty (0);
    if (tty) {
      tcgetattr (0, &orig);
      temp = orig;
      temp.c_lflag &= ~ECHO;
      tcsetattr (0, TCSAFLUSH, &temp);
    }
    r = getline (password, &n, stdin);
    err = errno;
    if (tty)
      tcsetattr (0, TCSAFLUSH, &orig);
    printf ("\n");
    if (r == -1) {
      errno = err;
      nbdkit_error ("could not read password from stdin: %m");
      return -1;
    }
    if (*password && r > 0 && (*password)[r-1] == '\n')
      (*password)[r-1] = '\0';
  }
  else if (value[0] == '+') {
    fp = fopen (&value[1], "r");
    if (fp == NULL) {
      nbdkit_error ("open %s: %m", &value[1]);
      return -1;
    }
    r = getline (password, &n, fp);
    err = errno;
    fclose (fp);
    if (r == -1) {
      errno = err;
      nbdkit_error ("could not read password from file %s: %m", &value[1]);
      return -1;
    }
    if (*password && r > 0 && (*password)[r-1] == '\n')
      (*password)[r-1] = '\0';
  }
  else {
    *password = strdup (value);
    if (*password == NULL) {
      nbdkit_error ("strdup: %m");
      return -1;
    }
  }
  return 0;
}
int nbdkit_read_password ( const char * value , char * * password ) { int tty , err ; struct termios orig , temp ; ssize_t r ; size_t n ; FILE * fp ; if ( strcmp ( value , " " ) == 0 ) { printf ( " " ) ; tty = isatty ( 0 ) ; if ( tty ) { tcgetattr ( 0 , & orig ) ; temp = orig ; temp . c_lflag &= ~ ECHO ; tcsetattr ( 0 , TCSAFLUSH , & temp ) ; } r = getline ( password , & n , stdin ) ; err = errno ; if ( tty ) tcsetattr ( 0 , TCSAFLUSH , & orig ) ; printf ( " \n " ) ; if ( r == -1 ) { errno = err ; nbdkit_error ( " " ) ; return -1 ; } if ( * password && r > 0 && ( * password ) [ r - 1 ] == ' \n ' ) ( * password ) [ r - 1 ] = ' \0 ' ; } else if ( value [ 0 ] == ' ' ) { fp = fopen ( & value [ 1 ] , " " ) ; if ( fp == NULL ) { nbdkit_error ( " " , & value [ 1 ] ) ; return -1 ; } r = getline ( password , & n , fp ) ; err = errno ; fclose ( fp ) ; if ( r == -1 ) { errno = err ; nbdkit_error ( " " , & value [ 1 ] ) ; return -1 ; } if ( * password && r > 0 && ( * password ) [ r - 1 ] == ' \n ' ) ( * password ) [ r - 1 ] = ' \0 ' ; } else { * password = strdup ( value ) ; if ( * password == NULL ) { nbdkit_error ( " " ) ; return -1 ; } } return 0 ; }
Read a password from configuration value.
Read a password from configuration value .
/* Read from stdin. */ /* Set no echo. */ /* Restore echo. */ /* Complete the printf above. */ /* Read password from a file. */ /* Parameter is the password. */