File: userinfo.c

package info (click to toggle)
bindfs 1.8-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,504 kB
  • ctags: 148
  • sloc: sh: 8,955; ansic: 1,388; ruby: 120; makefile: 56
file content (165 lines) | stat: -rw-r--r-- 3,533 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.

    Author: Martin P�rtel <martin.partel@gmail.com>
*/

#include "userinfo.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>


int user_uid(const char *username, uid_t *ret)
{
    struct passwd pwbuf, *pwbufp = NULL;
    char *buf;
    size_t buflen;
    int res;

    uid_t uid;
    char *endptr;

    /* Check whether the string is a numerical UID */
    uid = strtol(username, &endptr, 10);
    if (*endptr == '\0') {
        buflen = 1024;
        buf = malloc(buflen);
        res = getpwuid_r(uid, &pwbuf, buf, buflen, &pwbufp);
        if (res != 0) {
            if (res != ERANGE) { /* don't care if buffer was too small */
                free(buf);
                return 0;
            }
        }
        free(buf);
        *ret = uid;
        return 1;
    }

    /* Process user name */
    buflen = 1024;
    buf = malloc(buflen);

    res = getpwnam_r(username, &pwbuf, buf, buflen, &pwbufp);
    while(res == ERANGE) {
        buflen *= 2;
        buf = realloc(buf, buflen);
        res = getpwnam_r(username, &pwbuf, buf, buflen, &pwbufp);
    }

    if (pwbufp == NULL) {
        free(buf);
        return 0;
    }

    *ret = pwbuf.pw_uid;
    free(buf);
    return 1;
}


int group_gid(const char *groupname, gid_t *ret)
{
    struct group gbuf, *gbufp = NULL;
    char *buf;
    size_t buflen;
    int res;

    gid_t gid;
    char *endptr;

    /* Check whether the string is a numerical GID */
    gid = strtol(groupname, &endptr, 10);
    if (*endptr == '\0') {
        buflen = 1024;
        buf = malloc(buflen);
        res = getgrgid_r(gid, &gbuf, buf, buflen, &gbufp);
        if (res != 0) {
            if (res != ERANGE) { /* don't care if buffer was too small */
                free(buf);
                return 0;
            }
        }
        free(buf);
        *ret = gid;
        return 1;
    }

    /* Process group name */
    buflen = 1024;
    buf = malloc(buflen);

    res = getgrnam_r(groupname, &gbuf, buf, buflen, &gbufp);
    while(res == ERANGE) {
        buflen *= 2;
        buf = realloc(buf, buflen);
        res = getgrnam_r(groupname, &gbuf, buf, buflen, &gbufp);
    }

    if (gbufp == NULL) {
        free(buf);
        return 0;
    }

    *ret = gbuf.gr_gid;
    free(buf);
    return 1;
}


int user_belongs_to_group(uid_t uid, gid_t gid)
{
    struct passwd pwbuf, *pwbufp = NULL;
    struct group grbuf, *grbufp = NULL;
    char *buf;
    size_t buflen;

    int member;
    uid_t member_uid;

    int res;

    buflen = 1024;
    buf = malloc(buflen);

    res = getpwuid_r(uid, &pwbuf, buf, buflen, &pwbufp);
    while(res == ERANGE) {
        buflen *= 2;
        buf = realloc(buf, buflen);
        res = getpwuid_r(uid, &pwbuf, buf, buflen, &pwbufp);
    }

    if (pwbufp == NULL)
        goto no;

    if (gid == pwbuf.pw_gid)
        goto yes;

    /* we reuse the same buf because we don't need the passwd info any more */
    res = getgrgid_r(gid, &grbuf, buf, buflen, &grbufp);
    while(res == ERANGE) {
        buflen *= 2;
        buf = realloc(buf, buflen);
        res = getgrgid_r(gid, &grbuf, buf, buflen, &grbufp);
    }

    if (grbufp == NULL)
        goto no;

    for (member = 0; grbuf.gr_mem[member] != NULL; ++member) {
        if (user_uid(grbuf.gr_mem[member], &member_uid))
            if (member_uid == uid)
                goto yes;
    }

    goto no;

yes:
    free(buf);
    return 1;
no:
    free(buf);
    return 0;
}