00001
00002
00003
00004
00005
00006
00007
00008
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
00026
00027
00028 #define XRDSECSSSID "XrdSecsssID"
00029
00030 XrdSysMutex XrdSecsssID::InitMutex;
00031
00032
00033
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
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
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
00061
00062 if (!idP || !(defaultID = genID(idP)))
00063 defaultID = genID(aType != idDynamic);
00064
00065
00066
00067
00068 p2i.myP = this;
00069 sprintf(buff, XRDSECSSSID"=%lx", p2i.val);
00070 putenv(buff);
00071
00072
00073
00074 InitMutex.UnLock();
00075 }
00076
00077
00078
00079
00080
00081 int XrdSecsssID::Find(const char *lid, char *Buff, int Blen)
00082 {
00083 sssID *fP;
00084 int rc;
00085
00086
00087
00088 myMutex.Lock();
00089 if (!(fP = Registry.Find(lid))) fP = defaultID;
00090 if (!fP || fP->iLen > Blen) {myMutex.UnLock(); return 0;}
00091
00092
00093
00094 memcpy(Buff, fP->iData, fP->iLen);
00095 rc = fP->iLen;
00096 myMutex.UnLock();
00097 return rc;
00098 }
00099
00100
00101
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
00112
00113 InitMutex.Lock();
00114
00115
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
00126
00127 if (!i2p.idP || !(idP = i2p.idP->defaultID))
00128 {idP = genID(aType == idDynamic); freeIDP = 1;}
00129
00130
00131
00132 dIDsz = idP->iLen;
00133 *dID = (char *)malloc(dIDsz);
00134 memcpy(*dID, idP->iData, dIDsz);
00135
00136
00137
00138 InitMutex.UnLock();
00139 if (freeIDP) free(idP);
00140 return i2p.idP;
00141 }
00142
00143
00144
00145
00146
00147 int XrdSecsssID::Register(const char *lid, XrdSecEntity *eP, int doRep)
00148 {
00149 sssID *idP;
00150 int rc;
00151
00152
00153
00154 if (!eP)
00155 {myMutex.Lock(); Registry.Del(lid); myMutex.UnLock(); return 1;}
00156
00157
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
00168
00169
00170
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
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
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
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
00209
00210 if (!tLen || !(idP = (sssID *)malloc(tLen + sizeof(sssID)))) return 0;
00211
00212
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
00228
00229 return idP;
00230 }