xrootd
XrdSecProtocolpwd.hh
Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                 X r d S e c P r o t o c o l p w d . h h                    */
00004 /*                                                                            */
00005 /* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*   Produced by Gerri Ganis for CERN                                         */
00007 /*                                                                            */
00008 /* This file is part of the XRootD software suite.                            */
00009 /*                                                                            */
00010 /* XRootD is free software: you can redistribute it and/or modify it under    */
00011 /* the terms of the GNU Lesser General Public License as published by the     */
00012 /* Free Software Foundation, either version 3 of the License, or (at your     */
00013 /* option) any later version.                                                 */
00014 /*                                                                            */
00015 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00016 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00017 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00018 /* License for more details.                                                  */
00019 /*                                                                            */
00020 /* You should have received a copy of the GNU Lesser General Public License   */
00021 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00022 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00023 /*                                                                            */
00024 /* The copyright holder's institutional names and contributor's names may not */
00025 /* be used to endorse or promote products derived from this software without  */
00026 /* specific prior written permission of the institution or contributor.       */
00027 /******************************************************************************/
00028 
00029 #include "XrdOuc/XrdOucErrInfo.hh"
00030 #include "XrdSys/XrdSysPthread.hh"
00031 #include "XrdOuc/XrdOucString.hh"
00032 #include "XrdOuc/XrdOucTokenizer.hh"
00033 
00034 #include "XrdSec/XrdSecInterface.hh"
00035 #include "XrdSecpwd/XrdSecpwdTrace.hh"
00036 
00037 #include "XrdSut/XrdSutPFEntry.hh"
00038 #include "XrdSut/XrdSutPFile.hh"
00039 #include "XrdSut/XrdSutBuffer.hh"
00040 #include "XrdSut/XrdSutRndm.hh"
00041 
00042 #include "XrdCrypto/XrdCryptoAux.hh"
00043 #include "XrdCrypto/XrdCryptoCipher.hh"
00044 #include "XrdCrypto/XrdCryptoFactory.hh"
00045 
00046 /******************************************************************************/
00047 /*                               D e f i n e s                                */
00048 /******************************************************************************/
00049 
00050 typedef XrdOucString String;
00051   
00052 #define XrdSecPROTOIDENT    "pwd"
00053 #define XrdSecPROTOIDLEN    sizeof(XrdSecPROTOIDENT)
00054 #define XrdSecpwdVERSION    10100
00055 #define XrdSecNOIPCHK       0x0001
00056 #define XrdSecDEBUG         0x1000
00057 #define XrdCryptoMax        10
00058 
00059 #define kMAXBUFLEN          1024
00060 #define kMAXUSRLEN          9
00061 #define kMAXPWDLEN          64
00062 
00063 //
00064 // Message codes either returned by server or included in buffers
00065 enum kpwdStatus {
00066    kpST_error    = -1,      // error occured
00067    kpST_ok       =  0,      // ok
00068    kpST_more     =  1       // need more info
00069 };
00070 
00071 //
00072 // Auto-reg modes
00073 enum kpwdAutoreg {
00074    kpAR_none       =  0,      // autoreg disabled
00075    kpAR_users      =  1,      // only for tags in password files (local, system's) 
00076    kpAR_all        =  2       // for all tags
00077 };
00078 
00079 //
00080 // Client update autologin modes
00081 enum kpwdUpdate {
00082    kpUP_none       =  0,      // no update
00083    kpUP_remove     =  1,      // remove obsolete entries only 
00084    kpUP_all        =  2       // remove obsolete entries and register new valid info 
00085 };
00086 
00087 //
00088 // Creds input type
00089 enum kpwdCredsInput {
00090    kpCI_undef      = -1,      // undefined
00091    kpCI_prompt     =  0,      // from prompt
00092    kpCI_exact      =  1,      // from FileNetRc, exact tag 
00093    kpCI_wildcard   =  2       // from FileNetRc, wildcard tag
00094 };
00095 
00096 //
00097 // Creds type (for prompt)
00098 enum kpwdCredType {
00099    kpCT_undef      = -1,      // undefined
00100    kpCT_normal     =  0,      // confirmed credentials
00101    kpCT_onetime    =  1,      // one-time credentials
00102    kpCT_old        =  2,      // old credentials to be changed 
00103    kpCT_new        =  3,      // new credentials to be confirmed
00104    kpCT_newagain   =  4,      // new credentials again for confirmation
00105    kpCT_autoreg    =  5,      // autoreg: new creds to be confirmed
00106    kpCT_ar_again   =  6,      // autoreg: new creds again for confirmation
00107    kpCT_crypt      =  7,      // standard crypt hash
00108    kpCT_afs        =  8,      // AFS plain password
00109    kpCT_afsenc     =  9       // AFS encrypted password
00110 };
00111 
00112 //
00113 // Creds actions
00114 enum kpwdCredsActions {
00115    kpCA_undef      = -1,      // undefined
00116    kpCA_check      =  0,      // normal check of credentials
00117    kpCA_checkold   =  1,      // check current creds before asking for new ones 
00118    kpCA_cache      =  2,      // cache received (new) credentials
00119    kpCA_checkcache =  3       // check cached credentials and save them, if ok
00120 };
00121 
00122 // Client steps
00123 enum kpwdClientSteps {
00124    kXPC_none = 0,
00125    kXPC_normal     = 1000, // 1000: standard packet
00126    kXPC_verifysrv,         // 1001: request for server verification 
00127    kXPC_signedrtag,        // 1002: signed rtag (after server request for verification)
00128    kXPC_creds,             // 1003: credentials packet
00129    kXPC_autoreg,           // 1004: query for autoregistration
00130    kXPC_failureack,        // 1005: failure acknowledgement
00131    kXPC_reserved           // 
00132 };
00133 
00134 // Server steps
00135 enum kpwdServerSteps {
00136    kXPS_none = 0,
00137    kXPS_init       = 2000,   // 2000: fake code used the first time 
00138    kXPS_credsreq,            // 2001: request for credentials 
00139    kXPS_rtag,                // 2002: rndm tag to be signed (strong verification)
00140    kXPS_signedrtag,          // 2003: signed rtag (after client request for verification)
00141    kXPS_newpuk,              // 2004: new public part for session ciphers 
00142    kXPS_puk,                 // 2005: public part for session ciphers (after autoreg)
00143    kXPS_failure,             // 2006: signal failure to client to drop invalid cached info
00144    kXPS_reserved             //
00145 };
00146 
00147 // Error codes
00148 enum kpwdErrors {
00149    kPWErrParseBuffer = 10000,       // 10000
00150    kPWErrDecodeBuffer,              // 10001
00151    kPWErrLoadCrypto,                // 10002
00152    kPWErrBadProtocol,               // 10003
00153    kPWErrNoUserHost,                // 10004
00154    kPWErrNoUser,                    // 10005
00155    kPWErrNoHost,                    // 10006
00156    kPWErrBadUser,                   // 10007
00157    kPWErrCreateBucket,              // 10008
00158    kPWErrDuplicateBucket,           // 10009
00159    kPWErrCreateBuffer,              // 10010
00160    kPWErrSerialBuffer,              // 10011
00161    kPWErrGenCipher,                 // 10012
00162    kPWErrExportPuK,                 // 10013
00163    kPWErrEncRndmTag,                // 10014
00164    kPWErrBadRndmTag,                // 10015
00165    kPWErrNoRndmTag,                 // 10016
00166    kPWErrNoCipher,                  // 10017
00167    kPWErrQueryCreds,                // 10018
00168    kPWErrNoCreds,                   // 10019
00169    kPWErrBadPasswd,                 // 10020
00170    kPWErrBadCache,                  // 10021
00171    kPWErrNoCache,                   // 10022
00172    kPWErrNoSessID,                  // 10023
00173    kPWErrBadSessID,                 // 10024
00174    kPWErrBadOpt,                    // 10025
00175    kPWErrMarshal,                   // 10026
00176    kPWErrUnmarshal,                 // 10027
00177    kPWErrSaveCreds,                 // 10028
00178    kPWErrNoSalt,                    // 10029
00179    kPWErrNoBuffer,                  // 10030
00180    kPWErrRefCipher,                 // 10031
00181    kPWErrNoPublic,                  // 10032
00182    kPWErrAddBucket,                 // 10033
00183    kPWErrFinCipher,                 // 10034
00184    kPWErrInit,                      // 10034
00185    kPWErrBadCreds,                  // 10035
00186    kPWErrError                      // 10036  
00187 };
00188 
00189 // Structuring the status word
00190 typedef struct {
00191    char  ctype;
00192    char  action;
00193    short options; 
00194 } pwdStatus_t;
00195 
00196 #define REL1(x)     { if (x) delete x; }
00197 #define REL2(x,y)   { if (x) delete x; if (y) delete y; }
00198 #define REL3(x,y,z) { if (x) delete x; if (y) delete y; if (z) delete z; }
00199 #if 0
00200 #ifndef NODEBUG
00201 #define PRINT(y) {{SecTrace->Beg(epname); cerr <<y; SecTrace->End();}}
00202 #else
00203 #define PRINT(y) { }
00204 #endif
00205 #endif
00206 #define SafeDelete(x) { if (x) delete x ; x = 0; }
00207 #define SafeDelArray(x) { if (x) delete [] x ; x = 0; }
00208 
00209 //
00210 // This a small class to set the relevant options in one go
00211 //
00212 class pwdOptions {
00213 public:
00214    short  debug;        // [cs] debug flag
00215    short  mode;         // [cs] 'c' or 's'
00216    short  areg;         // [cs] auto-registration opt (s); update-autolog-info opt (c)
00217    short  upwd;         // [s] check / do-not-check pwd file in user's $HOME
00218    short  alog;         // [c] check / do-not-check user's autologin info
00219    short  verisrv;      // [c] verify / do-not-verify server ownership of srvpuk
00220    short  vericlnt;     // [s] level of verification client ownership of clntpuk
00221    short  syspwd;       // [s] check / do-not-check system pwd (requires privileges) 
00222    int    lifecreds;    // [s] lifetime in seconds of credentials
00223    int    maxprompts;   // [c] max number of empty prompts
00224    int    maxfailures;  // [s] max passwd failures before blocking
00225    char  *clist;        // [s] list of crypto modules ["ssl"]
00226    char  *dir;          // [s] directory with admin pwd files [$HOME/.xrd]
00227    char  *udir;         // [s] users's sub-directory with pwd files [$HOME/.xrd]
00228    char  *cpass;        // [s] users's crypt hash pwd file [$HOME/.xrootdpass]
00229    char  *alogfile;     // [c] autologin file [$HOME/.xrd/pwdnetrc]
00230    char  *srvpuk;       // [c] file with server puks [$HOME/.xrd/pwdsrvpuk]
00231    short  keepcreds;    // [s] keep / do-not-keep client credentials 
00232    char  *expcreds;     // [s] (template for) file with exported creds
00233    int    expfmt;       // [s] formta for exported credentials
00234 
00235    pwdOptions() { debug = -1; mode = 's'; areg = -1; upwd = -1; alog = -1;
00236                   verisrv = -1; vericlnt = -1;
00237                   syspwd = -1; lifecreds = -1; maxprompts = -1; maxfailures = -1;
00238                   clist = 0; dir = 0; udir = 0; cpass = 0;
00239                   alogfile = 0; srvpuk = 0; keepcreds = 0; expcreds = 0; expfmt = 0;}
00240    virtual ~pwdOptions() { } // Cleanup inside XrdSecProtocolpwdInit
00241    void Print(XrdOucTrace *t); // Print summary of pwd option status
00242 };
00243 
00244 class pwdHSVars {
00245 public:
00246    int               Iter;          // iteration number
00247    int               TimeStamp;     // Time of last call
00248    String            CryptoMod;     // crypto module in use
00249    String            User;          // remote username
00250    String            Tag;           // tag for credentials
00251    int               RemVers;       // Version run by remote counterpart
00252    XrdCryptoFactory *CF;            // crypto factory
00253    XrdCryptoCipher  *Hcip;          // handshake cipher
00254    XrdCryptoCipher  *Rcip;          // reference cipher
00255    String            ID;            // Handshake ID (dummy for clients)
00256    XrdSutPFEntry    *Cref;          // Cache reference
00257    XrdSutPFEntry    *Pent;          // Pointer to relevant file entry 
00258    bool              RtagOK;        // Rndm tag checked / not checked
00259    pwdStatus_t       Status;        // Some state flags
00260    bool              Tty;           // Terminal attached / not attached
00261    int               Step;          // Current step
00262    int               LastStep;      // Step required at previous iteration
00263    String            ErrMsg;        // Last error message
00264    int               SysPwd;        // 0 = no, 1 = Unix sys pwd, 2 = AFS pwd
00265    String            AFScell;       // AFS cell if it makes sense
00266    XrdSutBuffer     *Parms;         // Buffer with server parms on first iteration 
00267 
00268    pwdHSVars() { Iter = 0; TimeStamp = -1; CryptoMod = ""; User = ""; Tag = "";
00269                  RemVers = -1; CF = 0; Hcip = 0; Rcip = 0;
00270                  ID = ""; Cref = 0; Pent = 0; RtagOK = 0; Tty = 0;
00271                  Step = 0; LastStep = 0; ErrMsg = "";
00272                  SysPwd = 0; AFScell = "";
00273                  Status.ctype = 0; Status.action = 0; Status.options = 0; Parms = 0;}
00274 
00275    ~pwdHSVars() { SafeDelete(Cref); SafeDelete(Hcip); SafeDelete(Parms); }
00276 };
00277 
00278 
00279 /******************************************************************************/
00280 /*              X r d S e c P r o t o c o l p w d   C l a s s                 */
00281 /******************************************************************************/
00282 
00283 class XrdSecProtocolpwd : public XrdSecProtocol
00284 {
00285 public:
00286         int                Authenticate  (XrdSecCredentials *cred,
00287                                           XrdSecParameters **parms,
00288                                           XrdOucErrInfo     *einfo=0);
00289 
00290         XrdSecCredentials *getCredentials(XrdSecParameters  *parm=0,
00291                                           XrdOucErrInfo     *einfo=0);
00292 
00293         XrdSecProtocolpwd(int opts, const char *hname,
00294                           const struct sockaddr *ipadd,
00295                           const char *parms = 0);
00296         virtual ~XrdSecProtocolpwd() {} // Delete() does it all
00297 
00298         // Initialization methods
00299         static char      *Init(pwdOptions o, XrdOucErrInfo *erp);
00300 
00301         void              Delete();
00302 
00303         static void       PrintTimeStat();
00304 
00305         // Enable tracing
00306         static XrdOucTrace *EnableTracing();
00307 
00308 private:
00309 
00310    // Static members initialized at startup
00311    static XrdSysMutex      pwdContext;
00312    static String           FileAdmin;
00313    static String           FileExpCreds;     // (Template for) file with exported creds [S]
00314    static String           FileUser;
00315    static String           FileCrypt;
00316    static String           FileSrvPuk;
00317    static String           SrvID;
00318    static String           SrvEmail; 
00319    static String           DefCrypto;
00320    static String           DefError;
00321    static XrdSutPFile      PFAdmin;          // Admin file [S]
00322    static XrdSutPFile      PFAlog;           // Autologin file [CS]
00323    static XrdSutPFile      PFSrvPuk;         // File with server public keys [CS]
00324    //
00325    // Crypto related info
00326    static int              ncrypt;                  // Number of factories
00327    static int              cryptID[XrdCryptoMax];   // their IDs 
00328    static String           cryptName[XrdCryptoMax]; // their names 
00329    static XrdCryptoCipher *loccip[XrdCryptoMax];    // local ciphers
00330    static XrdCryptoCipher *refcip[XrdCryptoMax];    // ref for session ciphers 
00331    //
00332    // Caches for info files
00333    static XrdSutCache      cacheAdmin;  // Admin file
00334    static XrdSutCache      cacheSrvPuk; // SrvPuk file
00335    static XrdSutCache      cacheUser;   // User files
00336    static XrdSutCache      cacheAlog;   // Autologin file
00337    //
00338    // Running options / settings
00339    static int              Debug;          // [CS] Debug level
00340    static bool             Server;         // [CS] If server mode 
00341    static int              UserPwd;        // [S] Check passwd file in user's <xrdsecdir> 
00342    static bool             SysPwd;         // [S] Check system passwd file if allowed 
00343    static int              VeriClnt;       // [S] Client verification level
00344    static int              VeriSrv;        // [C] Server verification level
00345    static int              AutoReg;        // [S] Autoreg mode 
00346    static int              LifeCreds;      // [S] if > 0, credential lifetime in secs
00347    static int              MaxPrompts;     // [C] Repeating prompt
00348    static int              MaxFailures;    // [S] Max passwd failures before blocking
00349    static int              AutoLogin;      // [C] do-not-check/check/update autolog info
00350    static int              TimeSkew;       // [CS] Allowed skew in secs for time stamps 
00351    static bool             KeepCreds;      // [S] Keep / Do-Not-Keep client creds
00352    static int              FmtExpCreds;    // [S] Format for the exported credentials 
00353    //
00354    // for error logging and tracing
00355    static XrdSysLogger     Logger;
00356    static XrdSysError      eDest;
00357    static XrdOucTrace     *PWDTrace;
00358 
00359    // Information local to this instance
00360    int                     options;
00361    struct sockaddr         hostaddr;      // Client-side only
00362    char                    CName[256];    // Client-name
00363    bool                    srvMode;       // TRUE if server mode 
00364 
00365    // Handshake local info
00366    pwdHSVars              *hs;
00367 
00368    // Acquired credentials (server side)
00369    XrdSecCredentials      *clientCreds;
00370 
00371    // Parsing received buffers
00372    int            ParseClientInput(XrdSutBuffer *br, XrdSutBuffer **bm,
00373                                    String &emsg);
00374    int            ParseServerInput(XrdSutBuffer *br, XrdSutBuffer **bm,
00375                                    String &cmsg);
00376    int            ParseCrypto(XrdSutBuffer *buf);
00377 
00378    // Error functions
00379    static void    ErrF(XrdOucErrInfo *einfo, kXR_int32 ecode,
00380                        const char *msg1, const char *msg2 = 0,
00381                        const char *msg3 = 0);
00382    XrdSecCredentials *ErrC(XrdOucErrInfo *einfo, XrdSutBuffer *b1,
00383                            XrdSutBuffer *b2,XrdSutBuffer *b3,
00384                            kXR_int32 ecode, const char *msg1 = 0,
00385                            const char *msg2 = 0, const char *msg3 = 0);
00386    int            ErrS(String ID, XrdOucErrInfo *einfo, XrdSutBuffer *b1,
00387                        XrdSutBuffer *b2, XrdSutBuffer *b3,
00388                        kXR_int32 ecode, const char *msg1 = 0,
00389                        const char *msg2 = 0, const char *msg3 = 0);
00390 
00391    // Query methods
00392    XrdSutBucket  *QueryCreds(XrdSutBuffer *bm, bool netrc, int &status);
00393    int            QueryUser(int &status, String &cmsg);
00394    int            QueryCrypt(String &fn, String &pwhash);
00395    int            QueryNetRc(String host, String &passwd, int &status);
00396 
00397    // Check credentials
00398    bool           CheckCreds(XrdSutBucket *creds, int credtype);
00399    bool           CheckCredsAFS(XrdSutBucket *creds, int ctype);
00400 
00401    // Check Time stamp
00402    bool           CheckTimeStamp(XrdSutBuffer *b, int skew, String &emsg);
00403 
00404    // Check random challenge
00405    bool           CheckRtag(XrdSutBuffer *bm, String &emsg);
00406 
00407    // Saving / Updating
00408    int            ExportCreds(XrdSutBucket *creds);
00409    int            SaveCreds(XrdSutBucket *creds);
00410    int            UpdateAlog();
00411 
00412    // Auxilliary methods
00413    int            GetUserHost(String &usr, String &host);
00414    int            AddSerialized(char opt, kXR_int32 step, String ID, 
00415                                 XrdSutBuffer *bls, XrdSutBuffer *buf,
00416                                 kXR_int32 type, XrdCryptoCipher *cip);
00417    int            DoubleHash(XrdCryptoFactory *cf, XrdSutBucket *bck,
00418                              XrdSutBucket *s1, XrdSutBucket *s2 = 0,
00419                              const char *tag = 0);
00420 };