lib/misc/passwdfn.cc
changeset 0 6f7a81934006
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include "passwdfn.h"
       
    19 #include <pwd.h>
       
    20 #include <stdlib.h>
       
    21 #include <sys/types.h>
       
    22 #include <sys/stat.h>
       
    23 #include <unistd.h>
       
    24 #include <ctype.h>
       
    25 #include "fdbuf/fdbuf.h"
       
    26 #include "mystring/mystring.h"
       
    27 #include "stat_fns.h"
       
    28 
       
    29 #ifdef HAVE_CRYPT_H
       
    30 #include <crypt.h>
       
    31 #endif
       
    32 
       
    33 static mystring getpasswd_interactive(const char* err)
       
    34 {
       
    35   mystring pass1 = getpass("Enter the user's new password: ");
       
    36   mystring pass2 = getpass("Please type it again for verification: ");
       
    37   if(!(pass1 == pass2)) {
       
    38     ferr << err << ": error: passwords don't match\n";
       
    39     return "";
       
    40   }
       
    41   if(pass1.length() == 0)
       
    42     ferr << err << ": error: password is empty.\n";
       
    43   return pass1;
       
    44 }
       
    45 
       
    46 static mystring getpasswd_stdin(const char* err)
       
    47 {
       
    48   mystring tmp;
       
    49   fin.getline(tmp);
       
    50   tmp = tmp.rstrip();
       
    51   if(!tmp)
       
    52     ferr << err << ": error: password is empty.\n";
       
    53   return tmp;
       
    54 }
       
    55 
       
    56 mystring getpasswd(const char* err)
       
    57 {
       
    58   return isatty(0) ?
       
    59     getpasswd_interactive(err) :
       
    60     getpasswd_stdin(err);
       
    61 }