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