rpm 5.3.7
|
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 #ifdef SUSE_REFERENCE 00026 news 00027 uucp 00028 man 00029 nobody 00030 wwwrun 00031 mail 00032 lp 00033 #endif 00034 if (!thisUname) { 00035 lastUnameLen = 0; 00036 return -1; 00037 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */ 00038 } else if (strcmp(thisUname, "root") == 0) { 00039 *uid = 0; 00040 return 0; 00041 #endif 00042 } 00043 00044 thisUnameLen = strlen(thisUname); 00045 if (lastUname == NULL || thisUnameLen != lastUnameLen || 00046 strcmp(thisUname, lastUname) != 0) 00047 { 00048 if (lastUnameAlloced < thisUnameLen + 1) { 00049 lastUnameAlloced = thisUnameLen + 10; 00050 lastUname = (char *) xrealloc(lastUname, lastUnameAlloced); 00051 } 00052 strcpy(lastUname, thisUname); 00053 00054 pwent = getpwnam(thisUname); 00055 if (pwent == NULL) { 00056 /*@-internalglobs@*/ /* FIX: shrug */ 00057 endpwent(); 00058 /*@=internalglobs@*/ 00059 pwent = getpwnam(thisUname); 00060 if (pwent == NULL) return -1; 00061 } 00062 00063 lastUid = pwent->pw_uid; 00064 } 00065 00066 *uid = lastUid; 00067 00068 return 0; 00069 } 00070 00071 int gnameToGid(const char * thisGname, gid_t * gid) 00072 { 00073 /*@only@*/ static char * lastGname = NULL; 00074 static size_t lastGnameLen = 0; 00075 static size_t lastGnameAlloced; 00076 static gid_t lastGid; 00077 size_t thisGnameLen; 00078 struct group * grent; 00079 00080 #ifdef SUSE_REFERENCE 00081 news 00082 dialout 00083 uucp 00084 lp 00085 #endif 00086 if (thisGname == NULL) { 00087 lastGnameLen = 0; 00088 return -1; 00089 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */ 00090 } else if (strcmp(thisGname, "root") == 0) { 00091 *gid = 0; 00092 return 0; 00093 #endif 00094 } 00095 00096 thisGnameLen = strlen(thisGname); 00097 if (lastGname == NULL || thisGnameLen != lastGnameLen || 00098 strcmp(thisGname, lastGname) != 0) 00099 { 00100 if (lastGnameAlloced < thisGnameLen + 1) { 00101 lastGnameAlloced = thisGnameLen + 10; 00102 lastGname = (char *) xrealloc(lastGname, lastGnameAlloced); 00103 } 00104 strcpy(lastGname, thisGname); 00105 00106 grent = getgrnam(thisGname); 00107 if (grent == NULL) { 00108 /*@-internalglobs@*/ /* FIX: shrug */ 00109 endgrent(); 00110 /*@=internalglobs@*/ 00111 grent = getgrnam(thisGname); 00112 if (grent == NULL) { 00113 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */ 00114 /* XXX The filesystem package needs group/lock w/o getgrnam. */ 00115 if (strcmp(thisGname, "lock") == 0) { 00116 *gid = lastGid = 54; 00117 return 0; 00118 } else 00119 if (strcmp(thisGname, "mail") == 0) { 00120 *gid = lastGid = 12; 00121 return 0; 00122 } else 00123 #endif 00124 return -1; 00125 } 00126 } 00127 lastGid = grent->gr_gid; 00128 } 00129 00130 *gid = lastGid; 00131 00132 return 0; 00133 } 00134 00135 char * uidToUname(uid_t uid) 00136 { 00137 static uid_t lastUid = (uid_t) -1; 00138 /*@only@*/ static char * lastUname = NULL; 00139 static size_t lastUnameLen = 0; 00140 00141 if (uid == (uid_t) -1) { 00142 lastUid = (uid_t) -1; 00143 return NULL; 00144 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */ 00145 } else if (uid == (uid_t) 0) { 00146 return (char *) "root"; 00147 #endif 00148 } else if (uid == lastUid) { 00149 return lastUname; 00150 } else { 00151 struct passwd * pwent = getpwuid(uid); 00152 size_t len; 00153 00154 if (pwent == NULL) return NULL; 00155 00156 lastUid = uid; 00157 len = strlen(pwent->pw_name); 00158 if (lastUnameLen < len + 1) { 00159 lastUnameLen = len + 20; 00160 lastUname = (char *) xrealloc(lastUname, lastUnameLen); 00161 } 00162 strcpy(lastUname, pwent->pw_name); 00163 00164 return lastUname; 00165 } 00166 } 00167 00168 char * gidToGname(gid_t gid) 00169 { 00170 static gid_t lastGid = (gid_t) -1; 00171 /*@only@*/ static char * lastGname = NULL; 00172 static size_t lastGnameLen = 0; 00173 00174 if (gid == (gid_t) -1) { 00175 lastGid = (gid_t) -1; 00176 return NULL; 00177 #if !defined(RPM_VENDOR_OPENPKG) /* no-hard-coded-ugid */ 00178 } else if (gid == (gid_t) 0) { 00179 return (char *) "root"; 00180 #endif 00181 } else if (gid == lastGid) { 00182 return lastGname; 00183 } else { 00184 struct group * grent = getgrgid(gid); 00185 size_t len; 00186 00187 if (grent == NULL) return NULL; 00188 00189 lastGid = gid; 00190 len = strlen(grent->gr_name); 00191 if (lastGnameLen < len + 1) { 00192 lastGnameLen = len + 20; 00193 lastGname = (char *) xrealloc(lastGname, lastGnameLen); 00194 } 00195 strcpy(lastGname, grent->gr_name); 00196 00197 return lastGname; 00198 } 00199 }