Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

rpmio/ugid.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 #include "ugid.h"
00007 #include "debug.h"
00008 
00009 /* unameToUid(), uidTouname() and the group variants are really poorly
00010    implemented. They really ought to use hash tables. I just made the
00011    guess that most files would be owned by root or the same person/group
00012    who owned the last file. Those two values are cached, everything else
00013    is looked up via getpw() and getgr() functions.  If this performs
00014    too poorly I'll have to implement it properly :-( */
00015 
00016 int unameToUid(const char * thisUname, uid_t * uid)
00017 {
00018 /*@only@*/ static char * lastUname = NULL;
00019     static size_t lastUnameLen = 0;
00020     static size_t lastUnameAlloced;
00021     static uid_t lastUid;
00022     struct passwd * pwent;
00023     size_t thisUnameLen;
00024 
00025     if (!thisUname) {
00026         lastUnameLen = 0;
00027         return -1;
00028     } else if (strcmp(thisUname, "root") == 0) {
00029         *uid = 0;
00030         return 0;
00031     }
00032 
00033     thisUnameLen = strlen(thisUname);
00034     if (lastUname == NULL || thisUnameLen != lastUnameLen ||
00035         strcmp(thisUname, lastUname) != 0) {
00036         if (lastUnameAlloced < thisUnameLen + 1) {
00037             lastUnameAlloced = thisUnameLen + 10;
00038             lastUname = xrealloc(lastUname, lastUnameAlloced);  /* XXX memory leak */
00039         }
00040         strcpy(lastUname, thisUname);
00041 
00042         pwent = getpwnam(thisUname);
00043         if (pwent == NULL) {
00044             endpwent();
00045             pwent = getpwnam(thisUname);
00046             if (pwent == NULL) return -1;
00047         }
00048 
00049         lastUid = pwent->pw_uid;
00050     }
00051 
00052     *uid = lastUid;
00053 
00054     return 0;
00055 }
00056 
00057 int gnameToGid(const char * thisGname, gid_t * gid)
00058 {
00059 /*@only@*/ static char * lastGname = NULL;
00060     static size_t lastGnameLen = 0;
00061     static size_t lastGnameAlloced;
00062     static gid_t lastGid;
00063     size_t thisGnameLen;
00064     struct group * grent;
00065 
00066     if (thisGname == NULL) {
00067         lastGnameLen = 0;
00068         return -1;
00069     } else if (strcmp(thisGname, "root") == 0) {
00070         *gid = 0;
00071         return 0;
00072     }
00073 
00074     thisGnameLen = strlen(thisGname);
00075     if (lastGname == NULL || thisGnameLen != lastGnameLen ||
00076         strcmp(thisGname, lastGname) != 0)
00077     {
00078         if (lastGnameAlloced < thisGnameLen + 1) {
00079             lastGnameAlloced = thisGnameLen + 10;
00080             lastGname = xrealloc(lastGname, lastGnameAlloced);  /* XXX memory leak */
00081         }
00082         strcpy(lastGname, thisGname);
00083 
00084         grent = getgrnam(thisGname);
00085         if (grent == NULL) {
00086             endgrent();
00087             grent = getgrnam(thisGname);
00088             if (grent == NULL) return -1;
00089         }
00090         lastGid = grent->gr_gid;
00091     }
00092 
00093     *gid = lastGid;
00094 
00095     return 0;
00096 }
00097 
00098 char * uidToUname(uid_t uid)
00099 {
00100     static uid_t lastUid = (uid_t) -1;
00101 /*@only@*/ static char * lastUname = NULL;
00102     static size_t lastUnameLen = 0;
00103 
00104     if (uid == (uid_t) -1) {
00105         lastUid = (uid_t) -1;
00106         return NULL;
00107     } else if (uid == (uid_t) 0) {
00108         return "root";
00109     } else if (uid == lastUid) {
00110         return lastUname;
00111     } else {
00112         struct passwd * pwent = getpwuid(uid);
00113         size_t len;
00114 
00115         if (pwent == NULL) return NULL;
00116 
00117         lastUid = uid;
00118         len = strlen(pwent->pw_name);
00119         if (lastUnameLen < len + 1) {
00120             lastUnameLen = len + 20;
00121             lastUname = xrealloc(lastUname, lastUnameLen);
00122         }
00123         strcpy(lastUname, pwent->pw_name);
00124 
00125         return lastUname;
00126     }
00127 }
00128 
00129 char * gidToGname(gid_t gid)
00130 {
00131     static gid_t lastGid = (gid_t) -1;
00132 /*@only@*/ static char * lastGname = NULL;
00133     static size_t lastGnameLen = 0;
00134 
00135     if (gid == (gid_t) -1) {
00136         lastGid = (gid_t) -1;
00137         return NULL;
00138     } else if (gid == (gid_t) 0) {
00139         return "root";
00140     } else if (gid == lastGid) {
00141         return lastGname;
00142     } else {
00143         struct group * grent = getgrgid(gid);
00144         size_t len;
00145 
00146         if (grent == NULL) return NULL;
00147 
00148         lastGid = gid;
00149         len = strlen(grent->gr_name);
00150         if (lastGnameLen < len + 1) {
00151             lastGnameLen = len + 20;
00152             lastGname = xrealloc(lastGname, lastGnameLen);
00153         }
00154         strcpy(lastGname, grent->gr_name);
00155 
00156         return lastGname;
00157     }
00158 }

Generated at Thu Sep 6 11:25:44 2001 for rpm by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001