Main Page   Modules   Compound List   File List   Compound Members   File Members   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 int lastUnameLen = 0;
00020     static int lastUnameAlloced;
00021     static uid_t lastUid;
00022     struct passwd * pwent;
00023     int thisUnameLen;
00024 
00025     if (!thisUname) {
00026         lastUnameLen = 0;
00027         return -1;
00028     } else if (!strcmp(thisUname, "root")) {
00029         *uid = 0;
00030         return 0;
00031     }
00032 
00033     thisUnameLen = strlen(thisUname);
00034     if (!lastUname || thisUnameLen != lastUnameLen ||
00035         strcmp(thisUname, lastUname)) {
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) {
00044             endpwent();
00045             pwent = getpwnam(thisUname);
00046             if (!pwent) 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 int lastGnameLen = 0;
00061     static int lastGnameAlloced;
00062     static uid_t lastGid;
00063     int thisGnameLen;
00064     struct group * grent;
00065 
00066     if (!thisGname) {
00067         lastGnameLen = 0;
00068         return -1;
00069     } else if (!strcmp(thisGname, "root")) {
00070         *gid = 0;
00071         return 0;
00072     }
00073 
00074     thisGnameLen = strlen(thisGname);
00075     if (!lastGname || thisGnameLen != lastGnameLen ||
00076         strcmp(thisGname, lastGname)) {
00077         if (lastGnameAlloced < thisGnameLen + 1) {
00078             lastGnameAlloced = thisGnameLen + 10;
00079             lastGname = xrealloc(lastGname, lastGnameAlloced);  /* XXX memory leak */
00080         }
00081         strcpy(lastGname, thisGname);
00082 
00083         grent = getgrnam(thisGname);
00084         if (!grent) {
00085             endgrent();
00086             grent = getgrnam(thisGname);
00087             if (!grent) return -1;
00088         }
00089         lastGid = grent->gr_gid;
00090     }
00091 
00092     *gid = lastGid;
00093 
00094     return 0;
00095 }
00096 
00097 char * uidToUname(uid_t uid)
00098 {
00099     static int lastUid = -1;
00100     /*@only@*/ static char * lastUname = NULL;
00101     static int lastUnameLen = 0;
00102     struct passwd * pwent;
00103     int len;
00104 
00105     if (uid == (uid_t) -1) {
00106         lastUid = -1;
00107         return NULL;
00108     } else if (!uid) {
00109         return "root";
00110     } else if (uid == lastUid) {
00111         return lastUname;
00112     } else {
00113         pwent = getpwuid(uid);
00114         if (!pwent) return NULL;
00115 
00116         lastUid = uid;
00117         len = strlen(pwent->pw_name);
00118         if (lastUnameLen < len + 1) {
00119             lastUnameLen = len + 20;
00120             lastUname = xrealloc(lastUname, lastUnameLen);
00121         }
00122         strcpy(lastUname, pwent->pw_name);
00123 
00124         return lastUname;
00125     }
00126 }
00127 
00128 char * gidToGname(gid_t gid)
00129 {
00130     static int lastGid = -1;
00131     /*@only@*/ static char * lastGname = NULL;
00132     static int lastGnameLen = 0;
00133     struct group * grent;
00134     int len;
00135 
00136     if (gid == (gid_t) -1) {
00137         lastGid = -1;
00138         return NULL;
00139     } else if (!gid) {
00140         return "root";
00141     } else if (gid == lastGid) {
00142         return lastGname;
00143     } else {
00144         grent = getgrgid(gid);
00145         if (!grent) return NULL;
00146 
00147         lastGid = gid;
00148         len = strlen(grent->gr_name);
00149         if (lastGnameLen < len + 1) {
00150             lastGnameLen = len + 20;
00151             lastGname = xrealloc(lastGname, lastGnameLen);
00152         }
00153         strcpy(lastGname, grent->gr_name);
00154 
00155         return lastGname;
00156     }
00157 }

Generated at Sun Apr 8 18:43:02 2001 for rpm by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000