// @(#)root/auth:$Id$
// Author: Fons Rademakers   26/11/2000

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TAuthenticate
#define ROOT_TAuthenticate


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TAuthenticate                                                        //
//                                                                      //
// An authentication module for ROOT based network services, like rootd //
// and proofd.                                                          //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TObject
#include "TObject.h"
#endif
#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef ROOT_TList
#include "TList.h"
#endif
#ifndef ROOT_TDatime
#include "TDatime.h"
#endif
#ifndef ROOT_rsafun
#include "rsafun.h"
#endif
#ifndef ROOT_AuthConst
#include "AuthConst.h"
#endif
#ifdef R__SSL
// SSL specific headers for blowfish encryption
#include <openssl/blowfish.h>
#endif

class TAuthenticate;
class THostAuth;
class TPluginHandler;
class TSocket;
class TRootSecContext;
class TVirtualMutex;

typedef Int_t (*CheckSecCtx_t)(const char *subj, TRootSecContext *ctx);
typedef Int_t (*GlobusAuth_t)(TAuthenticate *auth, TString &user, TString &det);
typedef Int_t (*Krb5Auth_t)(TAuthenticate *auth, TString &user, TString &det, Int_t version);
typedef Int_t (*SecureAuth_t)(TAuthenticate *auth, const char *user, const char *passwd,
                              const char *remote, TString &det, Int_t version);

R__EXTERN TVirtualMutex *gAuthenticateMutex;

class TAuthenticate : public TObject {

friend class TRootAuth;
friend class TRootSecContext;
friend class TSocket;

public:
   enum ESecurity { kClear, kSRP, kKrb5, kGlobus, kSSH, kRfio }; // type of authentication

private:
   TString      fDetails;     // logon details (method dependent ...)
   THostAuth   *fHostAuth;    // pointer to relevant authentication info
   TString      fPasswd;      // user's password
   TString      fProtocol;    // remote service (rootd, proofd)
   Bool_t       fPwHash;      // kTRUE if fPasswd is a passwd hash
   TString      fRemote;      // remote host to which we want to connect
   Int_t        fRSAKey;      // Type of RSA key used
   TRootSecContext *fSecContext;  // pointer to relevant sec context
   ESecurity    fSecurity;    // actual logon security level
   TSocket     *fSocket;      // connection to remote daemon
   Bool_t       fSRPPwd;      // kTRUE if fPasswd is a SRP passwd
   Int_t        fVersion;     // 0,1,2, ... accordingly to remote daemon version
   TString      fUser;        // user to be authenticated
   Int_t        fTimeOut;     // timeout flag

   Int_t        GenRSAKeys();
   Bool_t       GetPwHash() const { return fPwHash; }
   Int_t        GetRSAKey() const { return fRSAKey; }
   ESecurity    GetSecurity() const { return fSecurity; }
   Bool_t       GetSRPPwd() const { return fSRPPwd; }
   const char  *GetSshUser(TString user) const;
   Int_t        GetVersion() const { return fVersion; }
   Int_t        ClearAuth(TString &user, TString &passwd, Bool_t &pwhash);
   Bool_t       GetUserPasswd(TString &user, TString &passwd,
                              Bool_t &pwhash, Bool_t srppwd);
   char        *GetRandString(Int_t Opt,Int_t Len);
   Int_t        ProofAuthSetup();
   Int_t        RfioAuth(TString &user);
   void         SetEnvironment();
   Int_t        SshAuth(TString &user);
   Int_t        SshError(const char *errfile);

   static TList          *fgAuthInfo;
   static TString         fgAuthMeth[kMAXSEC];
   static Bool_t          fgAuthReUse;      // kTRUE is ReUse required
   static TString         fgDefaultUser;    // Default user information
   static TDatime         fgExpDate;        // Expiring date for new security contexts
   static GlobusAuth_t    fgGlobusAuthHook;
   static Krb5Auth_t      fgKrb5AuthHook;
   static TString         fgKrb5Principal;  // Principal for Krb5 ticket
   static TDatime         fgLastAuthrc;     // Time of last reading of fgRootAuthrc
   static TString         fgPasswd;
   static TPluginHandler *fgPasswdDialog;   // Passwd dialog GUI plugin
   static Bool_t          fgPromptUser;     // kTRUE if user prompt required
   static TList          *fgProofAuthInfo;  // Specific lists of THostAuth fro proof
   static Bool_t          fgPwHash;         // kTRUE if fgPasswd is a passwd hash
   static Bool_t          fgReadHomeAuthrc; // kTRUE to look for $HOME/.rootauthrc
   static TString         fgRootAuthrc;     // Path to last rootauthrc-like file read
   static Int_t           fgRSAKey;         // Default type of RSA key to be tried
   static Int_t           fgRSAInit;
   static rsa_KEY         fgRSAPriKey;
   static rsa_KEY         fgRSAPubKey;
   static rsa_KEY_export  fgRSAPubExport[2];
#ifdef R__SSL
   static BF_KEY          fgBFKey;          // Blowfish symmetric key
#endif
   static SecureAuth_t    fgSecAuthHook;
   static Bool_t          fgSRPPwd;         // kTRUE if fgPasswd is a SRP passwd
   static TString         fgUser;
   static Bool_t          fgUsrPwdCrypt;    // kTRUE if encryption for UsrPwd is required
   static Int_t           fgLastError;      // Last error code processed by AuthError()
   static Int_t           fgAuthTO;         // if > 0, timeout in sec
   static Int_t           fgProcessID;      // ID of the main thread as unique identifier

   static Bool_t          CheckHost(const char *Host, const char *host);

   static void            FileExpand(const char *fin, FILE *ftmp);
   static Int_t           ProofAuthSetup(TSocket *sock, Bool_t client);
   static void            RemoveSecContext(TRootSecContext *ctx);

public:
   TAuthenticate(TSocket *sock, const char *remote, const char *proto,
                 const char *user = "");
   virtual ~TAuthenticate() { }

   Bool_t             Authenticate();
   Int_t              AuthExists(TString User, Int_t method, const char *Options,
                          Int_t *Message, Int_t *Rflag, CheckSecCtx_t funcheck);
   void               CatchTimeOut();
   Bool_t             CheckNetrc(TString &user, TString &passwd);
   Bool_t             CheckNetrc(TString &user, TString &passwd,
                                 Bool_t &pwhash, Bool_t srppwd);
   THostAuth         *GetHostAuth() const { return fHostAuth; }
   const char        *GetProtocol() const { return fProtocol; }
   const char        *GetRemoteHost() const { return fRemote; }
   Int_t              GetRSAKeyType() const { return fRSAKey; }
   TRootSecContext       *GetSecContext() const { return fSecContext; }
   TSocket           *GetSocket() const { return fSocket; }
   const char        *GetUser() const { return fUser; }
   Int_t              HasTimedOut() const { return fTimeOut; }
   void               SetRSAKeyType(Int_t key) { fRSAKey = key; }
   void               SetSecContext(TRootSecContext *ctx) { fSecContext = ctx; }

   static void        AuthError(const char *where, Int_t error);
   static Bool_t      CheckProofAuth(Int_t cSec, TString &det);

   static Int_t       DecodeRSAPublic(const char *rsapubexport, rsa_NUMBER &n,
                                      rsa_NUMBER &d, char **rsassl = 0);

   static TList      *GetAuthInfo();
   static const char *GetAuthMethod(Int_t idx);
   static Int_t       GetAuthMethodIdx(const char *meth);
   static Bool_t      GetAuthReUse();
   static Int_t       GetClientProtocol();
   static char       *GetDefaultDetails(Int_t method, Int_t opt, const char *user);
   static const char *GetDefaultUser();
   static TDatime     GetGlobalExpDate();
   static Bool_t      GetGlobalPwHash();
   static Bool_t      GetGlobalSRPPwd();
   static const char *GetGlobalUser();
   static GlobusAuth_t GetGlobusAuthHook();
   static THostAuth  *GetHostAuth(const char *host, const char *user="",
                                  Option_t *opt = "R", Int_t *Exact = 0);
   static const char *GetKrb5Principal();
   static Bool_t      GetPromptUser();
   static TList      *GetProofAuthInfo();
   static Int_t       GetRSAInit();
   static const char *GetRSAPubExport(Int_t key = 0);
   static THostAuth  *HasHostAuth(const char *host, const char *user,
                                  Option_t *opt = "R");
   static void        InitRandom();
   static void        MergeHostAuthList(TList *Std, TList *New, Option_t *Opt = "");
   static char       *PromptPasswd(const char *prompt = "Password: ");
   static char       *PromptUser(const char *remote);
   static Int_t       ReadRootAuthrc();
   static void        RemoveHostAuth(THostAuth *ha, Option_t *opt = "");
   static Int_t       SecureRecv(TSocket *Socket, Int_t dec,
                                 Int_t KeyType, char **Out);
   static Int_t       SecureSend(TSocket *Socket, Int_t enc,
                                 Int_t KeyType, const char *In);
   static Int_t       SendRSAPublicKey(TSocket *Socket, Int_t key = 0);
   static void        SetAuthReUse(Bool_t authreuse);
   static void        SetDefaultUser(const char *defaultuser);
   static void        SetGlobalExpDate(TDatime expdate);
   static void        SetGlobalPasswd(const char *passwd);
   static void        SetGlobalPwHash(Bool_t pwhash);
   static void        SetGlobalSRPPwd(Bool_t srppwd);
   static void        SetGlobalUser(const char *user);
   static void        SetGlobusAuthHook(GlobusAuth_t func);
   static void        SetKrb5AuthHook(Krb5Auth_t func);
   static void        SetPromptUser(Bool_t promptuser);
   static void        SetDefaultRSAKeyType(Int_t key);
   static void        SetReadHomeAuthrc(Bool_t readhomeauthrc); // for PROOF
   static void        SetRSAInit(Int_t init = 1);
   static Int_t       SetRSAPublic(const char *rsapubexport, Int_t klen);
   static void        SetSecureAuthHook(SecureAuth_t func);
   static void        SetTimeOut(Int_t to);
   static void        Show(Option_t *opt="S");

   ClassDef(TAuthenticate,0)  // Class providing remote authentication service
};

#endif
 TAuthenticate.h:1
 TAuthenticate.h:2
 TAuthenticate.h:3
 TAuthenticate.h:4
 TAuthenticate.h:5
 TAuthenticate.h:6
 TAuthenticate.h:7
 TAuthenticate.h:8
 TAuthenticate.h:9
 TAuthenticate.h:10
 TAuthenticate.h:11
 TAuthenticate.h:12
 TAuthenticate.h:13
 TAuthenticate.h:14
 TAuthenticate.h:15
 TAuthenticate.h:16
 TAuthenticate.h:17
 TAuthenticate.h:18
 TAuthenticate.h:19
 TAuthenticate.h:20
 TAuthenticate.h:21
 TAuthenticate.h:22
 TAuthenticate.h:23
 TAuthenticate.h:24
 TAuthenticate.h:25
 TAuthenticate.h:26
 TAuthenticate.h:27
 TAuthenticate.h:28
 TAuthenticate.h:29
 TAuthenticate.h:30
 TAuthenticate.h:31
 TAuthenticate.h:32
 TAuthenticate.h:33
 TAuthenticate.h:34
 TAuthenticate.h:35
 TAuthenticate.h:36
 TAuthenticate.h:37
 TAuthenticate.h:38
 TAuthenticate.h:39
 TAuthenticate.h:40
 TAuthenticate.h:41
 TAuthenticate.h:42
 TAuthenticate.h:43
 TAuthenticate.h:44
 TAuthenticate.h:45
 TAuthenticate.h:46
 TAuthenticate.h:47
 TAuthenticate.h:48
 TAuthenticate.h:49
 TAuthenticate.h:50
 TAuthenticate.h:51
 TAuthenticate.h:52
 TAuthenticate.h:53
 TAuthenticate.h:54
 TAuthenticate.h:55
 TAuthenticate.h:56
 TAuthenticate.h:57
 TAuthenticate.h:58
 TAuthenticate.h:59
 TAuthenticate.h:60
 TAuthenticate.h:61
 TAuthenticate.h:62
 TAuthenticate.h:63
 TAuthenticate.h:64
 TAuthenticate.h:65
 TAuthenticate.h:66
 TAuthenticate.h:67
 TAuthenticate.h:68
 TAuthenticate.h:69
 TAuthenticate.h:70
 TAuthenticate.h:71
 TAuthenticate.h:72
 TAuthenticate.h:73
 TAuthenticate.h:74
 TAuthenticate.h:75
 TAuthenticate.h:76
 TAuthenticate.h:77
 TAuthenticate.h:78
 TAuthenticate.h:79
 TAuthenticate.h:80
 TAuthenticate.h:81
 TAuthenticate.h:82
 TAuthenticate.h:83
 TAuthenticate.h:84
 TAuthenticate.h:85
 TAuthenticate.h:86
 TAuthenticate.h:87
 TAuthenticate.h:88
 TAuthenticate.h:89
 TAuthenticate.h:90
 TAuthenticate.h:91
 TAuthenticate.h:92
 TAuthenticate.h:93
 TAuthenticate.h:94
 TAuthenticate.h:95
 TAuthenticate.h:96
 TAuthenticate.h:97
 TAuthenticate.h:98
 TAuthenticate.h:99
 TAuthenticate.h:100
 TAuthenticate.h:101
 TAuthenticate.h:102
 TAuthenticate.h:103
 TAuthenticate.h:104
 TAuthenticate.h:105
 TAuthenticate.h:106
 TAuthenticate.h:107
 TAuthenticate.h:108
 TAuthenticate.h:109
 TAuthenticate.h:110
 TAuthenticate.h:111
 TAuthenticate.h:112
 TAuthenticate.h:113
 TAuthenticate.h:114
 TAuthenticate.h:115
 TAuthenticate.h:116
 TAuthenticate.h:117
 TAuthenticate.h:118
 TAuthenticate.h:119
 TAuthenticate.h:120
 TAuthenticate.h:121
 TAuthenticate.h:122
 TAuthenticate.h:123
 TAuthenticate.h:124
 TAuthenticate.h:125
 TAuthenticate.h:126
 TAuthenticate.h:127
 TAuthenticate.h:128
 TAuthenticate.h:129
 TAuthenticate.h:130
 TAuthenticate.h:131
 TAuthenticate.h:132
 TAuthenticate.h:133
 TAuthenticate.h:134
 TAuthenticate.h:135
 TAuthenticate.h:136
 TAuthenticate.h:137
 TAuthenticate.h:138
 TAuthenticate.h:139
 TAuthenticate.h:140
 TAuthenticate.h:141
 TAuthenticate.h:142
 TAuthenticate.h:143
 TAuthenticate.h:144
 TAuthenticate.h:145
 TAuthenticate.h:146
 TAuthenticate.h:147
 TAuthenticate.h:148
 TAuthenticate.h:149
 TAuthenticate.h:150
 TAuthenticate.h:151
 TAuthenticate.h:152
 TAuthenticate.h:153
 TAuthenticate.h:154
 TAuthenticate.h:155
 TAuthenticate.h:156
 TAuthenticate.h:157
 TAuthenticate.h:158
 TAuthenticate.h:159
 TAuthenticate.h:160
 TAuthenticate.h:161
 TAuthenticate.h:162
 TAuthenticate.h:163
 TAuthenticate.h:164
 TAuthenticate.h:165
 TAuthenticate.h:166
 TAuthenticate.h:167
 TAuthenticate.h:168
 TAuthenticate.h:169
 TAuthenticate.h:170
 TAuthenticate.h:171
 TAuthenticate.h:172
 TAuthenticate.h:173
 TAuthenticate.h:174
 TAuthenticate.h:175
 TAuthenticate.h:176
 TAuthenticate.h:177
 TAuthenticate.h:178
 TAuthenticate.h:179
 TAuthenticate.h:180
 TAuthenticate.h:181
 TAuthenticate.h:182
 TAuthenticate.h:183
 TAuthenticate.h:184
 TAuthenticate.h:185
 TAuthenticate.h:186
 TAuthenticate.h:187
 TAuthenticate.h:188
 TAuthenticate.h:189
 TAuthenticate.h:190
 TAuthenticate.h:191
 TAuthenticate.h:192
 TAuthenticate.h:193
 TAuthenticate.h:194
 TAuthenticate.h:195
 TAuthenticate.h:196
 TAuthenticate.h:197
 TAuthenticate.h:198
 TAuthenticate.h:199
 TAuthenticate.h:200
 TAuthenticate.h:201
 TAuthenticate.h:202
 TAuthenticate.h:203
 TAuthenticate.h:204
 TAuthenticate.h:205
 TAuthenticate.h:206
 TAuthenticate.h:207
 TAuthenticate.h:208
 TAuthenticate.h:209
 TAuthenticate.h:210
 TAuthenticate.h:211
 TAuthenticate.h:212
 TAuthenticate.h:213
 TAuthenticate.h:214
 TAuthenticate.h:215
 TAuthenticate.h:216
 TAuthenticate.h:217
 TAuthenticate.h:218
 TAuthenticate.h:219
 TAuthenticate.h:220
 TAuthenticate.h:221
 TAuthenticate.h:222
 TAuthenticate.h:223
 TAuthenticate.h:224
 TAuthenticate.h:225