XrdAccAuthFile.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                     X r d A c c A u t h F i l e . c c                      */
00004 /*                                                                            */
00005 /* (c) 2003 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-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 //         $Id: XrdAccAuthFile.cc 22437 2008-03-04 14:35:16Z rdm $
00012 
00013 const char *XrdAccAuthFileCVSID = "$Id: XrdAccAuthFile.cc 22437 2008-03-04 14:35:16Z rdm $";
00014 
00015 #include <errno.h>
00016 #include <fcntl.h>
00017 #include <string.h>
00018 #include <strings.h>
00019 #include <sys/stat.h>
00020 
00021 #include "XrdAcc/XrdAccAuthFile.hh"
00022   
00023 /******************************************************************************/
00024 /*                   X r d A c c A u t h D B _ O b j e c t                    */
00025 /******************************************************************************/
00026   
00027 XrdAccAuthDB *XrdAccAuthDBObject(XrdSysError *erp)
00028 {
00029       static XrdAccAuthFile mydatabase(erp);
00030 
00031       return (XrdAccAuthDB *)&mydatabase;
00032 }
00033   
00034 /******************************************************************************/
00035 /*                           C o n s t r u c t o r                            */
00036 /******************************************************************************/
00037   
00038 XrdAccAuthFile::XrdAccAuthFile(XrdSysError *erp)
00039 {
00040 
00041 // Set starting values
00042 //
00043    authfn = 0;
00044    flags = Noflags;
00045    modtime = 0;
00046    Eroute = erp;
00047 
00048 // Setup for an error in the first record
00049 //
00050    strcpy(path_buff, "start of file");
00051 }
00052 
00053 /******************************************************************************/
00054 /*                            D e s t r u c t o r                             */
00055 /******************************************************************************/
00056 
00057 XrdAccAuthFile::~XrdAccAuthFile()
00058 {
00059 
00060 // If the file is open, close it
00061 //
00062    if (flags &isOpen) Close();
00063 
00064 // Free the authfn string
00065 //
00066    if (authfn) free(authfn);
00067 }
00068 
00069 /******************************************************************************/
00070 /*                               C h a n g e d                                */
00071 /******************************************************************************/
00072 
00073 int XrdAccAuthFile::Changed(const char *dbfn)
00074 {
00075     struct stat statbuff;
00076 
00077 // If no file here, indicate nothing changed
00078 //
00079    if (!authfn || !*authfn) return 0;
00080 
00081 // If file paths differ, indicate that something has changed
00082 //
00083    if (dbfn && strcmp(dbfn, authfn)) return 1;
00084 
00085 // Get the modification timestamp for this file
00086 //
00087    if (stat(authfn, &statbuff))
00088       {Eroute->Emsg("AuthFile", errno, "find", authfn);
00089        return 0;
00090       }
00091 
00092 // Indicate whether or not the file has changed
00093 //
00094    return (modtime < statbuff.st_mtime);
00095 }
00096   
00097 /******************************************************************************/
00098 /*                                 C l o s e                                  */
00099 /******************************************************************************/
00100   
00101 int XrdAccAuthFile::Close()
00102 {
00103 // Return is the file is not open
00104 //
00105    if (!(flags & isOpen)) return 1;
00106 
00107 // Close the stream
00108 //
00109    DBfile.Close();
00110 
00111 // Unlock the protecting mutex
00112 //
00113    DBcontext.UnLock();
00114 
00115 // Indicate file is no longer open
00116 //
00117    flags = (DBflags)(flags & ~isOpen);
00118 
00119 // Return indicator of whether we had any errors
00120 //
00121    if (flags & dbError) return 0;
00122    return 1;
00123 }
00124 
00125 /******************************************************************************/
00126 /*                                 g e t P P                                  */
00127 /******************************************************************************/
00128   
00129 int XrdAccAuthFile::getPP(char **path, char **priv)
00130 {
00131    char *pp, *bp;
00132 
00133 // If a record has not been read, return end of record (i.e., 0)
00134 //
00135    if (!(flags & inRec)) return 0;
00136 
00137 // read the next word from the record (if none, simulate end of record)
00138 //
00139    if (!(pp = DBfile.GetWord()))
00140       {flags = (DBflags)(flags & ~inRec);
00141        return 0;
00142       }
00143 
00144 // Copy the value since the stream buffer might get overlaid.
00145 //
00146    bp = Copy(path_buff, pp, sizeof(path_buff)-1);
00147    *path = path_buff;
00148 
00149 // Check if this is really a path or a template
00150 //
00151    if (*path_buff != '/') {*priv = (char *)0; return 1;}
00152 
00153 // Verify that the path ends correctly (normally we would force a slash to
00154 // appear at the end but that prevents caps on files. So, we commented the
00155 // code out until we decide that maybe we really need to do this, sigh.
00156 //
00157 // bp--;
00158 // if (*bp != '/') {bp++; *bp = '/'; bp++; *bp = '\0';}
00159 
00160 // Get the next word which should be the privilege string
00161 //
00162    if (!(pp = DBfile.GetWord()))
00163       {flags = (DBflags)(flags & ~inRec);
00164        Eroute->Emsg("AuthFile", "Privileges missing after", path_buff);
00165        flags = (DBflags)(flags | dbError);
00166        *priv = (char *)0;
00167        return 0;
00168       }
00169 
00170 // All done here
00171 //
00172    *priv = pp;
00173    return 1;
00174 }
00175 
00176 /******************************************************************************/
00177 /*                                g e t R e c                                 */
00178 /******************************************************************************/
00179   
00180 char XrdAccAuthFile::getRec(char **recname)
00181 {
00182    char *pp;
00183    int idok;
00184 
00185 // Do this until we get a vlaid record
00186 //
00187    while(1)
00188         {
00189          // If we arer still in the middle of a record, flush it
00190          //
00191          if (flags & inRec) while(DBfile.GetWord()) {}
00192             else flags = (DBflags)(flags | inRec);
00193 
00194         // Get the next word, the record type
00195         //
00196         if (!(pp = DBfile.GetWord()))
00197            {*recname = (char *)0; return '\0';}
00198 
00199         // Verify the id-type
00200         //
00201         idok = 0;
00202         if (strlen(pp) == 1)
00203            switch(*pp)
00204                  {case 'g':
00205                   case 'h':
00206                   case 's':
00207                   case 'n':
00208                   case 't':
00209                   case 'u': idok = 1;
00210                             break;
00211                    default: break;
00212                  }
00213 
00214         // Check if the record type was valid
00215         //
00216         if (!idok) {Eroute->Emsg("AuthFile", "Invalid id type -", pp);
00217                     flags = (DBflags)(flags | dbError);
00218                     continue;
00219                    }
00220         rectype = *pp;
00221 
00222         // Get the record name. It must exist
00223         //
00224         if (!(pp = DBfile.GetWord()))
00225            {Eroute->Emsg("AuthFile","Record name is missing after",path_buff);
00226             flags = (DBflags)(flags | dbError);
00227             continue;
00228            }
00229 
00230         // Copy the record name
00231         //
00232         Copy(recname_buff, pp, sizeof(recname_buff));
00233         *recname = recname_buff;
00234         return rectype;
00235        }
00236    return '\0'; // Keep the compiler happy :-)
00237 }
00238 
00239 /******************************************************************************/
00240 /*                                  O p e n                                   */
00241 /******************************************************************************/
00242 
00243 int XrdAccAuthFile::Open(XrdSysError &eroute, const char *path)
00244 {
00245    struct stat statbuff;
00246    int authFD;
00247 
00248 // Enter the DB context (serialize use of this database)
00249 //
00250    DBcontext.Lock();
00251    Eroute = &eroute;
00252 
00253 // Use whichever path is the more recent
00254 //
00255    if (path)
00256       {if (authfn) free(authfn); authfn = strdup(path);}
00257    if( !authfn || !*authfn) return Bail(0, "Authorization file not specified.");
00258 
00259 // Get the modification timestamp for this file
00260 //
00261    if (stat(authfn, &statbuff)) return Bail(errno, "find", authfn);
00262 
00263 // Try to open the authorization file.
00264 //
00265    if ( (authFD = open(authfn, O_RDONLY, 0)) < 0)
00266       return Bail(errno,"open authorization file",authfn);
00267 
00268 // Copy in all the relevant information
00269 //
00270    modtime = statbuff.st_mtime;
00271    flags = isOpen;
00272    DBfile.SetEroute(Eroute);
00273    DBfile.Tabs(0);
00274 
00275 // Attach the file to the stream
00276 //
00277    if (DBfile.Attach(authFD))
00278       return Bail(DBfile.LastError(), "initialize stream for", authfn);
00279    return 1;
00280 }
00281   
00282 /******************************************************************************/
00283 /*                       P r i v a t e   M e t h o d s                        */
00284 /******************************************************************************/
00285 /******************************************************************************/
00286 /*                                  B a i l                                   */
00287 /******************************************************************************/
00288   
00289 int XrdAccAuthFile::Bail(int retc, const char *txt1, const char *txt2)
00290 {
00291 // This routine is typically used by open and the DBcontext lock must be held
00292 //
00293    flags = (DBflags)(flags & ~isOpen);
00294    DBcontext.UnLock();
00295    if (retc) Eroute->Emsg("AuthFile", retc, txt1, txt2);
00296       else   Eroute->Emsg("AuthFile", txt1, txt2);
00297    return 0;
00298 }
00299 
00300 /******************************************************************************/
00301 /*                                  C o p y                                   */
00302 /******************************************************************************/
00303 
00304 // This routine is used instead of strncpy because, frankly, it's a lot smarter
00305   
00306 char *XrdAccAuthFile::Copy(char *dp, char *sp, int dplen)
00307 {
00308    // Copy one less that the size of the buffer so that we have room for null
00309    //
00310    while(--dplen && *sp) {*dp = *sp; dp++; sp++;}
00311 
00312 // Insert a null character and return a pointer to it.
00313 //
00314    *dp = '\0';
00315    return dp;
00316 }

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