00001
00005 #include "system.h"
00006 #include "ugid.h"
00007 #include "debug.h"
00008
00009
00010
00011
00012
00013
00014
00015
00016 int unameToUid(const char * thisUname, uid_t * uid)
00017 {
00018 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
00030 *uid = 0;
00031
00032 return 0;
00033 }
00034
00035 thisUnameLen = strlen(thisUname);
00036 if (lastUname == NULL || thisUnameLen != lastUnameLen ||
00037 strcmp(thisUname, lastUname) != 0)
00038 {
00039 if (lastUnameAlloced < thisUnameLen + 1) {
00040 lastUnameAlloced = thisUnameLen + 10;
00041 lastUname = xrealloc(lastUname, lastUnameAlloced);
00042 }
00043
00044 strcpy(lastUname, thisUname);
00045
00046
00047 pwent = getpwnam(thisUname);
00048 if (pwent == NULL) {
00049
00050 endpwent();
00051
00052 pwent = getpwnam(thisUname);
00053 if (pwent == NULL) return -1;
00054 }
00055
00056 lastUid = pwent->pw_uid;
00057 }
00058
00059
00060 *uid = lastUid;
00061
00062
00063 return 0;
00064 }
00065
00066 int gnameToGid(const char * thisGname, gid_t * gid)
00067 {
00068 static char * lastGname = NULL;
00069 static size_t lastGnameLen = 0;
00070 static size_t lastGnameAlloced;
00071 static gid_t lastGid;
00072 size_t thisGnameLen;
00073 struct group * grent;
00074
00075 if (thisGname == NULL) {
00076 lastGnameLen = 0;
00077 return -1;
00078 } else if (strcmp(thisGname, "root") == 0) {
00079
00080 *gid = 0;
00081
00082 return 0;
00083 }
00084
00085 thisGnameLen = strlen(thisGname);
00086 if (lastGname == NULL || thisGnameLen != lastGnameLen ||
00087 strcmp(thisGname, lastGname) != 0)
00088 {
00089 if (lastGnameAlloced < thisGnameLen + 1) {
00090 lastGnameAlloced = thisGnameLen + 10;
00091 lastGname = xrealloc(lastGname, lastGnameAlloced);
00092 }
00093
00094 strcpy(lastGname, thisGname);
00095
00096
00097 grent = getgrnam(thisGname);
00098 if (grent == NULL) {
00099
00100 endgrent();
00101
00102 grent = getgrnam(thisGname);
00103 if (grent == NULL) {
00104
00105 if (strcmp(thisGname, "lock") == 0) {
00106 *gid = lastGid = 54;
00107 return 0;
00108 } else
00109 if (strcmp(thisGname, "mail") == 0) {
00110 *gid = lastGid = 12;
00111 return 0;
00112 } else
00113 return -1;
00114 }
00115 }
00116 lastGid = grent->gr_gid;
00117 }
00118
00119
00120 *gid = lastGid;
00121
00122
00123 return 0;
00124 }
00125
00126 char * uidToUname(uid_t uid)
00127 {
00128 static uid_t lastUid = (uid_t) -1;
00129 static char * lastUname = NULL;
00130 static size_t lastUnameLen = 0;
00131
00132 if (uid == (uid_t) -1) {
00133 lastUid = (uid_t) -1;
00134 return NULL;
00135 } else if (uid == (uid_t) 0) {
00136 return "root";
00137 } else if (uid == lastUid) {
00138 return lastUname;
00139 } else {
00140 struct passwd * pwent = getpwuid(uid);
00141 size_t len;
00142
00143 if (pwent == NULL) return NULL;
00144
00145 lastUid = uid;
00146 len = strlen(pwent->pw_name);
00147 if (lastUnameLen < len + 1) {
00148 lastUnameLen = len + 20;
00149 lastUname = xrealloc(lastUname, lastUnameLen);
00150 }
00151
00152 strcpy(lastUname, pwent->pw_name);
00153
00154
00155 return lastUname;
00156 }
00157 }
00158
00159 char * gidToGname(gid_t gid)
00160 {
00161 static gid_t lastGid = (gid_t) -1;
00162 static char * lastGname = NULL;
00163 static size_t lastGnameLen = 0;
00164
00165 if (gid == (gid_t) -1) {
00166 lastGid = (gid_t) -1;
00167 return NULL;
00168 } else if (gid == (gid_t) 0) {
00169 return "root";
00170 } else if (gid == lastGid) {
00171 return lastGname;
00172 } else {
00173 struct group * grent = getgrgid(gid);
00174 size_t len;
00175
00176 if (grent == NULL) return NULL;
00177
00178 lastGid = gid;
00179 len = strlen(grent->gr_name);
00180 if (lastGnameLen < len + 1) {
00181 lastGnameLen = len + 20;
00182 lastGname = xrealloc(lastGname, lastGnameLen);
00183 }
00184
00185 strcpy(lastGname, grent->gr_name);
00186
00187
00188 return lastGname;
00189 }
00190 }