~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to rpmio/ugid.c

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        *uid = 0;
 
30
        return 0;
 
31
    }
 
32
 
 
33
    thisUnameLen = strlen(thisUname);
 
34
    if (lastUname == NULL || thisUnameLen != lastUnameLen ||
 
35
        strcmp(thisUname, lastUname) != 0) {
 
36
        if (lastUnameAlloced < thisUnameLen + 1) {
 
37
            lastUnameAlloced = thisUnameLen + 10;
 
38
            lastUname = xrealloc(lastUname, lastUnameAlloced);  /* XXX memory leak */
 
39
        }
 
40
        strcpy(lastUname, thisUname);
 
41
 
 
42
        pwent = getpwnam(thisUname);
 
43
        if (pwent == NULL) {
 
44
            endpwent();
 
45
            pwent = getpwnam(thisUname);
 
46
            if (pwent == NULL) return -1;
 
47
        }
 
48
 
 
49
        lastUid = pwent->pw_uid;
 
50
    }
 
51
 
 
52
    *uid = lastUid;
 
53
 
 
54
    return 0;
 
55
}
 
56
 
 
57
int gnameToGid(const char * thisGname, gid_t * gid)
 
58
{
 
59
/*@only@*/ static char * lastGname = NULL;
 
60
    static size_t lastGnameLen = 0;
 
61
    static size_t lastGnameAlloced;
 
62
    static gid_t lastGid;
 
63
    size_t thisGnameLen;
 
64
    struct group * grent;
 
65
 
 
66
    if (thisGname == NULL) {
 
67
        lastGnameLen = 0;
 
68
        return -1;
 
69
    } else if (strcmp(thisGname, "root") == 0) {
 
70
        *gid = 0;
 
71
        return 0;
 
72
    }
 
73
 
 
74
    thisGnameLen = strlen(thisGname);
 
75
    if (lastGname == NULL || thisGnameLen != lastGnameLen ||
 
76
        strcmp(thisGname, lastGname) != 0)
 
77
    {
 
78
        if (lastGnameAlloced < thisGnameLen + 1) {
 
79
            lastGnameAlloced = thisGnameLen + 10;
 
80
            lastGname = xrealloc(lastGname, lastGnameAlloced);  /* XXX memory leak */
 
81
        }
 
82
        strcpy(lastGname, thisGname);
 
83
 
 
84
        grent = getgrnam(thisGname);
 
85
        if (grent == NULL) {
 
86
            endgrent();
 
87
            grent = getgrnam(thisGname);
 
88
            if (grent == NULL) return -1;
 
89
        }
 
90
        lastGid = grent->gr_gid;
 
91
    }
 
92
 
 
93
    *gid = lastGid;
 
94
 
 
95
    return 0;
 
96
}
 
97
 
 
98
char * uidToUname(uid_t uid)
 
99
{
 
100
    static uid_t lastUid = (uid_t) -1;
 
101
/*@only@*/ static char * lastUname = NULL;
 
102
    static size_t lastUnameLen = 0;
 
103
 
 
104
    if (uid == (uid_t) -1) {
 
105
        lastUid = (uid_t) -1;
 
106
        return NULL;
 
107
    } else if (uid == (uid_t) 0) {
 
108
        return "root";
 
109
    } else if (uid == lastUid) {
 
110
        return lastUname;
 
111
    } else {
 
112
        struct passwd * pwent = getpwuid(uid);
 
113
        size_t len;
 
114
 
 
115
        if (pwent == NULL) return NULL;
 
116
 
 
117
        lastUid = uid;
 
118
        len = strlen(pwent->pw_name);
 
119
        if (lastUnameLen < len + 1) {
 
120
            lastUnameLen = len + 20;
 
121
            lastUname = xrealloc(lastUname, lastUnameLen);
 
122
        }
 
123
        strcpy(lastUname, pwent->pw_name);
 
124
 
 
125
        return lastUname;
 
126
    }
 
127
}
 
128
 
 
129
char * gidToGname(gid_t gid)
 
130
{
 
131
    static gid_t lastGid = (gid_t) -1;
 
132
/*@only@*/ static char * lastGname = NULL;
 
133
    static size_t lastGnameLen = 0;
 
134
 
 
135
    if (gid == (gid_t) -1) {
 
136
        lastGid = (gid_t) -1;
 
137
        return NULL;
 
138
    } else if (gid == (gid_t) 0) {
 
139
        return "root";
 
140
    } else if (gid == lastGid) {
 
141
        return lastGname;
 
142
    } else {
 
143
        struct group * grent = getgrgid(gid);
 
144
        size_t len;
 
145
 
 
146
        if (grent == NULL) return NULL;
 
147
 
 
148
        lastGid = gid;
 
149
        len = strlen(grent->gr_name);
 
150
        if (lastGnameLen < len + 1) {
 
151
            lastGnameLen = len + 20;
 
152
            lastGname = xrealloc(lastGname, lastGnameLen);
 
153
        }
 
154
        strcpy(lastGname, grent->gr_name);
 
155
 
 
156
        return lastGname;
 
157
    }
 
158
}