-
Notifications
You must be signed in to change notification settings - Fork 0
/
nss_identity.c
175 lines (141 loc) · 3.79 KB
/
nss_identity.c
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
166
167
168
169
170
171
172
173
#include <nss.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h> /* sysconf() */
#include "debug.h"
enum nss_status
group(const char *name, gid_t gid, struct group *grp,
char *buf, size_t buflen, struct group **result)
{
struct passwd *pw, *pwp;
char *bufp;
int len = 0;
int maxpwbuflen;
char *pwdata = NULL;
char grname[1024];
int r;
printd("name=%s, gid=%d, buf=%p, buflen=%d\n", name, (int) gid, buf, buflen);
/* Preset the result value so that we don't forget to do this
* when returning early with an error. */
*result = NULL;
/* We'll compute the required buffer length. If enough space is not
* given to us, we can return an error. */
/* Prepare for getpw*_r calls */
maxpwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
pwdata = malloc(maxpwbuflen);
pw = malloc(sizeof(struct passwd));
if (name) {
/* We are performing getgrnam */
r = getpwnam_r(name, pw, pwdata, maxpwbuflen, &pwp);
/* If no match, but group matches group_####, extract gid */
if (r != 0 || pwp == NULL) {
if (!strncmp(name, "group_", 6)) {
char *p, *end;
/* Extract gid from requested name */
p = (char *) &name[6];
gid = (gid_t) strtoul(p, &end, 10);
/* Convert to gid request */
r = getpwuid_r((uid_t) gid, pw, pwdata, maxpwbuflen, &pwp);
}
}
}
else {
/* We are performing getgrgid - search for a user with uid == gid */
r = getpwuid_r((uid_t) gid, pw, pwdata, maxpwbuflen, &pwp);
}
printd("r = %d, pwp = %p, name = %p, gid = %d\n", r, pwp, name, gid);
if (r != 0 || pwp == NULL) {
printd("no\n");
/* Not found, add space for empty member list to requirements */
len += sizeof(char *);
}
else {
printd("yes\n");
/* If found, add space for 1-element member list to requirements */
len += sizeof(char *) * 2;
len += strlen(pwp->pw_name) + 1;
}
if (pwp && name == NULL) {
name = pwp->pw_name;
}
else if (name == NULL) {
snprintf(grname, sizeof(grname), "group_%d", gid);
name = grname;
}
else if (pwp && gid == -1) {
gid = (gid_t) pwp->pw_uid;
}
else if (gid == -1) {
/* Free transient data */
if (pwp && pwp != pw)
free(pwp);
free(pw);
free(pwdata);
return NSS_STATUS_NOTFOUND;
}
/* Init required length with size of group name */
len += strlen(name) + 1;
/* Add empty password string to requirements */
len += 1;
/* If not enough space provided, return */
if (buflen < len) {
/* Free transient data */
if (pwp && pwp != pw)
free(pwp);
free(pw);
free(pwdata);
return NSS_STATUS_TRYAGAIN;
}
/* Point to provided buffer. We will stack on internal strings. */
bufp = buf;
/* Stack on group name */
strcpy(bufp, name);
grp->gr_name = bufp;
bufp += strlen(name);
/* Stack on empty password */
*bufp = '\0';
grp->gr_passwd = bufp;
bufp++;
/* Set gid */
grp->gr_gid = gid;
/* Stack on group member list */
if (pwp) {
char *pwname, **gp;
strcpy(pwname = bufp, pwp->pw_name);
bufp += strlen(pwp->pw_name) + 1;
grp->gr_mem = gp = (char **)bufp;
*gp++ = pwname;
*gp++ = NULL;
}
else {
char **gp;
grp->gr_mem = gp = (char **)bufp;
*gp++ = NULL;
}
/* Free transient data */
if (pwp && pwp != pw)
free(pwp);
free(pw);
free(pwdata);
*result = grp;
return NSS_STATUS_SUCCESS;
}
enum nss_status
_nss_identity_getgrnam_r(const char *name, struct group *grp,
char *buf, size_t buflen, struct group **result)
{
printd("nss_identity_getgrgid_r\n");
/* Hand off to generic group backend */
return group(name, -1, grp, buf, buflen, result);
}
enum nss_status
_nss_identity_getgrgid_r(gid_t gid, struct group *grp,
char *buf, size_t buflen, struct group **result)
{
printd("nss_identity_getgrgid_r\n");
/* Hand off to generic group backend */
return group(NULL, gid, grp, buf, buflen, result);
}