XrdSecsssID.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                        X r d S e c s s s I D . c c                         */
00004 /*                                                                            */
00005 /* (c) 2008 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <grp.h>
00015 #include <pwd.h>
00016 #include <sys/types.h>
00017 
00018 #include "XrdSecsss/XrdSecsssID.hh"
00019 #include "XrdSecsss/XrdSecsssRR.hh"
00020 
00021 #include "XrdOuc/XrdOucPup.hh"
00022 #include "XrdSys/XrdSysHeaders.hh"
00023 
00024 /******************************************************************************/
00025 /*                               D e f i n e s                                */
00026 /******************************************************************************/
00027   
00028 #define XRDSECSSSID "XrdSecsssID"
00029 
00030 XrdSysMutex         XrdSecsssID::InitMutex;
00031 
00032 /******************************************************************************/
00033 /*                           C o n s t r u c t o r                            */
00034 /******************************************************************************/
00035   
00036 XrdSecsssID::XrdSecsssID(authType aType, XrdSecEntity *idP) : defaultID(0)
00037 {
00038    static char buff[64];
00039    union {unsigned long val; XrdSecsssID *myP;} p2i;
00040 
00041 // Check if we have initialized already. If so, indicate warning
00042 //
00043    InitMutex.Lock();
00044    if (getenv(XRDSECSSSID))
00045       {InitMutex.UnLock();
00046        cerr <<"SecsssID: Already instantiated; new instance ineffective!" <<endl;
00047        return;
00048       }
00049 
00050 // Verify the authType
00051 //
00052    switch(aType)
00053          {case idDynamic: break;
00054           case idStatic:  break;
00055           case idStaticM: break;
00056           default:        idP = 0; aType = idStatic; break;
00057          }
00058    myAuth = aType;
00059 
00060 // Generate a default identity
00061 //
00062    if (!idP || !(defaultID = genID(idP)))
00063       defaultID = genID(aType != idDynamic);
00064 
00065 // Establish a pointer to this object so that the shared library can use it
00066 // We only do this once!
00067 //
00068    p2i.myP = this;
00069    sprintf(buff, XRDSECSSSID"=%lx", p2i.val);
00070    putenv(buff);
00071 
00072 // All done with initialization
00073 //
00074    InitMutex.UnLock();
00075 }
00076 
00077 /******************************************************************************/
00078 /*                                  F i n d                                   */
00079 /******************************************************************************/
00080   
00081 int XrdSecsssID::Find(const char *lid, char *Buff, int Blen)
00082 {
00083    sssID *fP;
00084    int rc;
00085 
00086 // Lock the hash table and find the entry
00087 //
00088    myMutex.Lock();
00089    if (!(fP = Registry.Find(lid))) fP = defaultID;
00090    if (!fP || fP->iLen > Blen) {myMutex.UnLock(); return 0;}
00091 
00092 // Return the data
00093 //
00094    memcpy(Buff, fP->iData, fP->iLen);
00095    rc = fP->iLen;
00096    myMutex.UnLock();
00097    return rc;
00098 }
00099   
00100 /******************************************************************************/
00101 /*                                g e t O b j                                 */
00102 /******************************************************************************/
00103   
00104 XrdSecsssID *XrdSecsssID::getObj(authType &aType, char **dID, int &dIDsz)
00105 {
00106    int freeIDP = 0;
00107    sssID *idP;
00108    char *eP, *xP;
00109    union {long long llval; long lval; XrdSecsssID *idP;} i2p;
00110 
00111 // Prevent changes
00112 //
00113    InitMutex.Lock();
00114 
00115 // Convert to pointer
00116 //
00117    aType = idStatic;
00118    if ((eP = getenv(XRDSECSSSID)) && *eP)
00119       {if (sizeof(XrdSecsssID *) > 4) i2p.llval = strtoll(eP, &xP, 16);
00120           else                        i2p.lval  = strtol (eP, &xP, 16);
00121        if (*xP)                       i2p.idP   = 0;
00122           else aType = i2p.idP->myAuth;
00123       } else i2p.idP = 0;
00124 
00125 // Establish the default ID
00126 //
00127    if (!i2p.idP || !(idP = i2p.idP->defaultID))
00128       {idP = genID(aType == idDynamic); freeIDP = 1;}
00129 
00130 // Copy out the default id to the caller
00131 //
00132    dIDsz = idP->iLen;
00133   *dID = (char *)malloc(dIDsz);
00134    memcpy(*dID, idP->iData, dIDsz);
00135 
00136 // Return result
00137 //
00138    InitMutex.UnLock();
00139    if (freeIDP) free(idP);
00140    return i2p.idP;
00141 }
00142 
00143 /******************************************************************************/
00144 /*                              R e g i s t e r                               */
00145 /******************************************************************************/
00146 
00147 int XrdSecsssID::Register(const char *lid, XrdSecEntity *eP, int doRep)
00148 {
00149    sssID *idP;
00150    int    rc;
00151 
00152 // Check if we are simply deleting an entry
00153 //
00154    if (!eP)
00155       {myMutex.Lock(); Registry.Del(lid); myMutex.UnLock(); return 1;}
00156 
00157 // Generate an ID and add it to registry
00158 //
00159    if (!(idP = genID(eP))) return 0;
00160    myMutex.Lock(); 
00161    rc = (Registry.Add(lid, idP, (doRep ? Hash_replace : Hash_default)) ? 0:1);
00162    myMutex.UnLock();
00163    return rc;
00164 }
00165 
00166 /******************************************************************************/
00167 /*                       P r i v a t e   M e t h o d s                        */
00168 /******************************************************************************/
00169 /******************************************************************************/
00170 /*                                 g e n I D                                  */
00171 /******************************************************************************/
00172   
00173 XrdSecsssID::sssID *XrdSecsssID::genID(int Secure)
00174 {
00175    XrdSecEntity   myID("sss");
00176    struct passwd *pEnt;
00177    struct group  *pGrp;
00178 
00179 
00180 // Use either our own uid/gid or a generic
00181 //
00182    myID.name = (Secure || !(pEnt=getpwuid(geteuid())))
00183              ? (char *)"nobody" : pEnt->pw_name;
00184    myID.grps = (Secure || !(pGrp=getgrgid(getegid())))
00185              ? (char *)"nogroup" : pGrp->gr_name;
00186 
00187 // Just return the sssID
00188 //
00189    return genID(&myID);
00190 }
00191 
00192 /******************************************************************************/
00193 
00194 XrdSecsssID::sssID *XrdSecsssID::genID(XrdSecEntity *eP)
00195 {
00196    sssID *idP;
00197    char *bP;
00198    int tLen;
00199 
00200 // Calculate the length needed for the entity (4 bytes overhead for each item)
00201 //
00202    tLen = (eP->name         ? strlen(eP->name)         + 4 : 0)
00203         + (eP->vorg         ? strlen(eP->vorg)         + 4 : 0)
00204         + (eP->role         ? strlen(eP->role)         + 4 : 0)
00205         + (eP->grps         ? strlen(eP->grps)         + 4 : 0)
00206         + (eP->endorsements ? strlen(eP->endorsements) + 4 : 0);
00207 
00208 // If no identity information, return failure otherwise allocate a struct
00209 //
00210    if (!tLen || !(idP = (sssID *)malloc(tLen + sizeof(sssID)))) return 0;
00211 
00212 // Now stick each entry into the iData field
00213 //
00214    bP = idP->iData;
00215    if (eP->name)
00216       {*bP++ = XrdSecsssRR_Data::theName; XrdOucPup::Pack(&bP,eP->name);}
00217    if (eP->vorg)
00218       {*bP++ = XrdSecsssRR_Data::theVorg; XrdOucPup::Pack(&bP,eP->vorg);}
00219    if (eP->role)
00220       {*bP++ = XrdSecsssRR_Data::theRole; XrdOucPup::Pack(&bP,eP->role);}
00221    if (eP->grps)
00222       {*bP++ = XrdSecsssRR_Data::theGrps; XrdOucPup::Pack(&bP,eP->grps);}
00223    if (eP->endorsements)
00224       {*bP++ = XrdSecsssRR_Data::theEndo; XrdOucPup::Pack(&bP,eP->endorsements);}
00225    idP->iLen = bP - (idP->iData);
00226 
00227 // All done
00228 //
00229    return idP;
00230 }

Generated on Tue Jul 5 14:47:02 2011 for ROOT_528-00b_version by  doxygen 1.5.1