def __init__(self, user, passwd, verbose):
     super(PasswdStack, self).__init__()
     
     self.user=user
     self.passwd=passwd
     self._verbose=verbose
     
     #Load Configuration from file
     self._genConf = PasswdStackClientConf()
     self._genConf.load_passwdstackConfig()        
     self.serveraddr = self._genConf.getServeraddr()
     self.port = self._genConf.getPort()
     
     self._ca_certs = self._genConf.getCaCerts()
     self._certfile = self._genConf.getCertFile()
     self._keyfile = self._genConf.getKeyFile()
     
     self._log = fgLog.fgLog(self._genConf.getLogFile(), self._genConf.getLogLevel(), "PasswdStackClient", False)
class PasswdStack(object):
    def __init__(self, user, passwd, verbose):
        super(PasswdStack, self).__init__()
        
        self.user=user
        self.passwd=passwd
        self._verbose=verbose
        
        #Load Configuration from file
        self._genConf = PasswdStackClientConf()
        self._genConf.load_passwdstackConfig()        
        self.serveraddr = self._genConf.getServeraddr()
        self.port = self._genConf.getPort()
        
        self._ca_certs = self._genConf.getCaCerts()
        self._certfile = self._genConf.getCertFile()
        self._keyfile = self._genConf.getKeyFile()
        
        self._log = fgLog.fgLog(self._genConf.getLogFile(), self._genConf.getLogLevel(), "PasswdStackClient", False)


    def check_auth(self, socket_conn, checkauthstat):
        endloop = False
        passed = False
        while not endloop:
            ret = socket_conn.read(1024)
            if (ret == "OK"):
                if self._verbose:
                    print "Authentication OK. Your image request is being processed"
                self._log.debug("Authentication OK")
                endloop = True
                passed = True
            elif (ret == "TryAuthAgain"):
                msg = "ERROR: Permission denied, please try again. User is " + self.user                    
                self._log.error(msg)
                if self._verbose:
                    print msg                            
                m = hashlib.md5()
                m.update(getpass())
                passwd = m.hexdigest()
                socket_conn.write(passwd)
                self.passwd = passwd
            elif ret == "NoActive":
                msg="ERROR: The status of the user "+ self.user + " is not active"
                checkauthstat.append(str(msg))
                self._log.error(msg)
                #if self._verbose:
                #    print msg            
                endloop = True
                passed = False          
            elif ret == "NoUser":
                msg="ERROR: User "+ self.user + " does not exist"
                checkauthstat.append(str(msg))
                self._log.error(msg)
                #if self._verbose:
                #    print msg + " WE"  
                endloop = True
                passed = False
            else:                
                self._log.error(str(ret))
                #if self._verbose:
                #    print ret
                checkauthstat.append(str(ret))
                endloop = True
                passed = False
        return passed

    def passwdstackReset(self, dashboardpasswd):
        start_all = time.time()
        #generate string with options separated by | character
        output = None
        checkauthstat = []
        #params[0] is user
        #params[1] is the user password
        #params[2] is the type of password
        #params[3] is the new password for the dashboard
        
               
        
        options = str(self.user) + "|" + str(self.passwd) + "|ldappassmd5|" + str(dashboardpasswd)
        
        #self._log.debug("string to send: "+options)
        
        #Notify xCAT deployment to finish the job
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            genServer = ssl.wrap_socket(s,
                                        ca_certs=self._ca_certs,
                                        certfile=self._certfile,
                                        keyfile=self._keyfile,
                                        cert_reqs=ssl.CERT_REQUIRED,
                                        ssl_version=ssl.PROTOCOL_TLSv1)
            self._log.debug("Connecting server: " + self.serveraddr + ":" + str(self.port))
            if self._verbose:
                print "Connecting server: " + self.serveraddr + ":" + str(self.port)
            genServer.connect((self.serveraddr, self.port))            
        except ssl.SSLError:
            self._log.error("CANNOT establish SSL connection. EXIT")
            if self._verbose:
                print "ERROR: CANNOT establish SSL connection. EXIT"

        genServer.write(options)
        #check if the server received all parameters
        if self._verbose:
            print "Your request is in the queue to be processed after authentication"
                
        if self.check_auth(genServer, checkauthstat):
                
            ret = genServer.read(2048)
            
            if (re.search('^ERROR', ret)):
                output='The password could not be modified. Exit error:' + ret
                self._log.error(output)
                
            else:
                output="The password was reset: " + str(ret)
                self._log.debug(output)
                
        else:       
            self._log.error(str(checkauthstat[0]))
            if self._verbose:
                print checkauthstat[0]
            return
        
        end_all = time.time()
        self._log.info('TIME walltime reset client passwd:' + str(end_all - start_all))
        
        return output