- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

# 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             /*@-internalglobs@*/ /* FIX: shrug */
    00045             endpwent();
    00046             /*@=internalglobs@*/
    00047             pwent = getpwnam(thisUname);
    00048             if (pwent == NULL) return -1;
    00049         }
    00050
    00051         lastUid = pwent->pw_uid;
    00052     }
    00053
    00054     *uid = lastUid;
    00055
    00056     return 0;
    00057 }
    00058
    00059 int gnameToGid(const char * thisGname, gid_t * gid)
    00060 {
    00061 /*@only@*/ static char * lastGname = NULL;
    00062     static size_t lastGnameLen = 0;
    00063     static size_t lastGnameAlloced;
    00064     static gid_t lastGid;
    00065     size_t thisGnameLen;
    00066     struct group * grent;
    00067
    00068     if (thisGname == NULL) {
    00069         lastGnameLen = 0;
    00070         return -1;
    00071     } else if (strcmp(thisGname, "root") == 0) {
    00072         *gid = 0;
    00073         return 0;
    00074     }
    00075
    00076     thisGnameLen = strlen(thisGname);
    00077     if (lastGname == NULL || thisGnameLen != lastGnameLen ||
    00078         strcmp(thisGname, lastGname) != 0)
    00079     {
    00080         if (lastGnameAlloced < thisGnameLen + 1) {
    00081             lastGnameAlloced = thisGnameLen + 10;
    00082             lastGname = xrealloc(lastGname, lastGnameAlloced);  /* XXX memory leak */
    00083         }
    00084         strcpy(lastGname, thisGname);
    00085
    00086         grent = getgrnam(thisGname);
    00087         if (grent == NULL) {
    00088             /*@-internalglobs@*/ /* FIX: shrug */
    00089             endgrent();
    00090             /*@=internalglobs@*/
    00091             grent = getgrnam(thisGname);
    00092             if (grent == NULL) return -1;
    00093         }
    00094         lastGid = grent->gr_gid;
    00095     }
    00096
    00097     *gid = lastGid;
    00098
    00099     return 0;
    00100 }
    00101
    00102 char * uidToUname(uid_t uid)
    00103 {
    00104     static uid_t lastUid = (uid_t) -1;
    00105 /*@only@*/ static char * lastUname = NULL;
    00106     static size_t lastUnameLen = 0;
    00107
    00108     if (uid == (uid_t) -1) {
    00109         lastUid = (uid_t) -1;
    00110         return NULL;
    00111     } else if (uid == (uid_t) 0) {
    00112         return "root";
    00113     } else if (uid == lastUid) {
    00114         return lastUname;
    00115     } else {
    00116         struct passwd * pwent = getpwuid(uid);
    00117         size_t len;
    00118
    00119         if (pwent == NULL) return NULL;
    00120
    00121         lastUid = uid;
    00122         len = strlen(pwent->pw_name);
    00123         if (lastUnameLen < len + 1) {
    00124             lastUnameLen = len + 20;
    00125             lastUname = xrealloc(lastUname, lastUnameLen);
    00126         }
    00127         strcpy(lastUname, pwent->pw_name);
    00128
    00129         return lastUname;
    00130     }
    00131 }
    00132
    00133 char * gidToGname(gid_t gid)
    00134 {
    00135     static gid_t lastGid = (gid_t) -1;
    00136 /*@only@*/ static char * lastGname = NULL;
    00137     static size_t lastGnameLen = 0;
    00138
    00139     if (gid == (gid_t) -1) {
    00140         lastGid = (gid_t) -1;
    00141         return NULL;
    00142     } else if (gid == (gid_t) 0) {
    00143         return "root";
    00144     } else if (gid == lastGid) {
    00145         return lastGname;
    00146     } else {
    00147         struct group * grent = getgrgid(gid);
    00148         size_t len;
    00149
    00150         if (grent == NULL) return NULL;
    00151
    00152         lastGid = gid;
    00153         len = strlen(grent->gr_name);
    00154         if (lastGnameLen < len + 1) {
    00155             lastGnameLen = len + 20;
    00156             lastGname = xrealloc(lastGname, lastGnameLen);
    00157         }
    00158         strcpy(lastGname, grent->gr_name);
    00159
    00160         return lastGname;
    00161     }
    00162 }