1 | /** \ingroup rpmio |
---|
2 | * \file rpmio/ugid.c |
---|
3 | */ |
---|
4 | |
---|
5 | #include "system.h" |
---|
6 | #include "ugid.h" |
---|
7 | #include "debug.h" |
---|
8 | |
---|
9 | /* unameToUid(), uidTouname() and the group variants are really poorly |
---|
10 | implemented. They really ought to use hash tables. I just made the |
---|
11 | guess that most files would be owned by root or the same person/group |
---|
12 | who owned the last file. Those two values are cached, everything else |
---|
13 | is looked up via getpw() and getgr() functions. If this performs |
---|
14 | too poorly I'll have to implement it properly :-( */ |
---|
15 | |
---|
16 | int unameToUid(const char * thisUname, uid_t * uid) |
---|
17 | { |
---|
18 | /*@only@*/ static char * lastUname = NULL; |
---|
19 | static size_t lastUnameLen = 0; |
---|
20 | static size_t lastUnameAlloced; |
---|
21 | static uid_t lastUid; |
---|
22 | struct passwd * pwent; |
---|
23 | size_t thisUnameLen; |
---|
24 | |
---|
25 | if (!thisUname) { |
---|
26 | lastUnameLen = 0; |
---|
27 | return -1; |
---|
28 | } else if (strcmp(thisUname, "root") == 0) { |
---|
29 | /*@-boundswrite@*/ |
---|
30 | *uid = 0; |
---|
31 | /*@=boundswrite@*/ |
---|
32 | return 0; |
---|
33 | } |
---|
34 | |
---|
35 | thisUnameLen = strlen(thisUname); |
---|
36 | if (lastUname == NULL || thisUnameLen != lastUnameLen || |
---|
37 | strcmp(thisUname, lastUname) != 0) |
---|
38 | { |
---|
39 | if (lastUnameAlloced < thisUnameLen + 1) { |
---|
40 | lastUnameAlloced = thisUnameLen + 10; |
---|
41 | lastUname = xrealloc(lastUname, lastUnameAlloced); /* XXX memory leak */ |
---|
42 | } |
---|
43 | /*@-boundswrite@*/ |
---|
44 | strcpy(lastUname, thisUname); |
---|
45 | /*@=boundswrite@*/ |
---|
46 | |
---|
47 | pwent = getpwnam(thisUname); |
---|
48 | if (pwent == NULL) { |
---|
49 | /*@-internalglobs@*/ /* FIX: shrug */ |
---|
50 | endpwent(); |
---|
51 | /*@=internalglobs@*/ |
---|
52 | pwent = getpwnam(thisUname); |
---|
53 | if (pwent == NULL) return -1; |
---|
54 | } |
---|
55 | |
---|
56 | lastUid = pwent->pw_uid; |
---|
57 | } |
---|
58 | |
---|
59 | /*@-boundswrite@*/ |
---|
60 | *uid = lastUid; |
---|
61 | /*@=boundswrite@*/ |
---|
62 | |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | int gnameToGid(const char * thisGname, gid_t * gid) |
---|
67 | { |
---|
68 | /*@only@*/ static char * lastGname = NULL; |
---|
69 | static size_t lastGnameLen = 0; |
---|
70 | static size_t lastGnameAlloced; |
---|
71 | static gid_t lastGid; |
---|
72 | size_t thisGnameLen; |
---|
73 | struct group * grent; |
---|
74 | |
---|
75 | if (thisGname == NULL) { |
---|
76 | lastGnameLen = 0; |
---|
77 | return -1; |
---|
78 | } else if (strcmp(thisGname, "root") == 0) { |
---|
79 | /*@-boundswrite@*/ |
---|
80 | *gid = 0; |
---|
81 | /*@=boundswrite@*/ |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | thisGnameLen = strlen(thisGname); |
---|
86 | if (lastGname == NULL || thisGnameLen != lastGnameLen || |
---|
87 | strcmp(thisGname, lastGname) != 0) |
---|
88 | { |
---|
89 | if (lastGnameAlloced < thisGnameLen + 1) { |
---|
90 | lastGnameAlloced = thisGnameLen + 10; |
---|
91 | lastGname = xrealloc(lastGname, lastGnameAlloced); /* XXX memory leak */ |
---|
92 | } |
---|
93 | /*@-boundswrite@*/ |
---|
94 | strcpy(lastGname, thisGname); |
---|
95 | /*@=boundswrite@*/ |
---|
96 | |
---|
97 | grent = getgrnam(thisGname); |
---|
98 | if (grent == NULL) { |
---|
99 | /*@-internalglobs@*/ /* FIX: shrug */ |
---|
100 | endgrent(); |
---|
101 | /*@=internalglobs@*/ |
---|
102 | grent = getgrnam(thisGname); |
---|
103 | if (grent == NULL) return -1; |
---|
104 | } |
---|
105 | lastGid = grent->gr_gid; |
---|
106 | } |
---|
107 | |
---|
108 | /*@-boundswrite@*/ |
---|
109 | *gid = lastGid; |
---|
110 | /*@=boundswrite@*/ |
---|
111 | |
---|
112 | return 0; |
---|
113 | } |
---|
114 | |
---|
115 | char * uidToUname(uid_t uid) |
---|
116 | { |
---|
117 | static uid_t lastUid = (uid_t) -1; |
---|
118 | /*@only@*/ static char * lastUname = NULL; |
---|
119 | static size_t lastUnameLen = 0; |
---|
120 | |
---|
121 | if (uid == (uid_t) -1) { |
---|
122 | lastUid = (uid_t) -1; |
---|
123 | return NULL; |
---|
124 | } else if (uid == (uid_t) 0) { |
---|
125 | return "root"; |
---|
126 | } else if (uid == lastUid) { |
---|
127 | return lastUname; |
---|
128 | } else { |
---|
129 | struct passwd * pwent = getpwuid(uid); |
---|
130 | size_t len; |
---|
131 | |
---|
132 | if (pwent == NULL) return NULL; |
---|
133 | |
---|
134 | lastUid = uid; |
---|
135 | len = strlen(pwent->pw_name); |
---|
136 | if (lastUnameLen < len + 1) { |
---|
137 | lastUnameLen = len + 20; |
---|
138 | lastUname = xrealloc(lastUname, lastUnameLen); |
---|
139 | } |
---|
140 | /*@-boundswrite@*/ |
---|
141 | strcpy(lastUname, pwent->pw_name); |
---|
142 | /*@=boundswrite@*/ |
---|
143 | |
---|
144 | return lastUname; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | char * gidToGname(gid_t gid) |
---|
149 | { |
---|
150 | static gid_t lastGid = (gid_t) -1; |
---|
151 | /*@only@*/ static char * lastGname = NULL; |
---|
152 | static size_t lastGnameLen = 0; |
---|
153 | |
---|
154 | if (gid == (gid_t) -1) { |
---|
155 | lastGid = (gid_t) -1; |
---|
156 | return NULL; |
---|
157 | } else if (gid == (gid_t) 0) { |
---|
158 | return "root"; |
---|
159 | } else if (gid == lastGid) { |
---|
160 | return lastGname; |
---|
161 | } else { |
---|
162 | struct group * grent = getgrgid(gid); |
---|
163 | size_t len; |
---|
164 | |
---|
165 | if (grent == NULL) return NULL; |
---|
166 | |
---|
167 | lastGid = gid; |
---|
168 | len = strlen(grent->gr_name); |
---|
169 | if (lastGnameLen < len + 1) { |
---|
170 | lastGnameLen = len + 20; |
---|
171 | lastGname = xrealloc(lastGname, lastGnameLen); |
---|
172 | } |
---|
173 | /*@-boundswrite@*/ |
---|
174 | strcpy(lastGname, grent->gr_name); |
---|
175 | /*@=boundswrite@*/ |
---|
176 | |
---|
177 | return lastGname; |
---|
178 | } |
---|
179 | } |
---|