-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswd.c
193 lines (158 loc) · 4.76 KB
/
passwd.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright © 2011 Faidon Liambotis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <nss.h>
#include <pwd.h>
#include <errno.h>
enum nss_status _nss_uidpool_getpwuid_r(uid_t, struct passwd *, char *, size_t, int *);
enum nss_status _nss_uidpool_getpwnam_r(const char *, struct passwd *, char *, size_t, int *);
#define CONFIG_FILE "/etc/libnss_uidpool.conf"
/* Some safe defaults first in case we don't have a config file */
char *USERNAME_PREFIX = "pool";
char *SHELL = "/bin/false";
char *HOME = "/noexistent";
char *GECOS = "uidpool user,,,";
int UID_MIN = 10001;
int UID_MAX = 10101;
int DEFAULT_GID = 65534;
static char *strstrip(char *s, char *t)
{
/*
* This function returns a pointer to a substring of the s, with all
* occurences of t removed.
*/
char *cleaned;
int counter = 0;
/* at most strlen(s) */
cleaned = malloc(strlen(s));
while(*s) {
if (!strchr(t, *s)) {
cleaned[counter] = *s;
counter++;
}
++s;
}
return cleaned;
}
static int parse_config(void)
{
FILE *fp;
size_t len = 0;
ssize_t read;
char *key, *value, *saveptr, *clean, *cleaned;
char *line = NULL;
char *cleaners = "'\"";
fp = fopen(CONFIG_FILE, "r");
if (fp != NULL) {
while ((read = getline(&line, &len, fp)) != -1) {
/* comments get ignored first */
if (line[0] == '#')
continue;
/* Then we clean usual invalid characters */
cleaned = strstrip(line, cleaners);
/* a = b, not a == b or a = b = c */
if (index(cleaned, '=') == rindex(cleaned, '=')) {
/* clean newlines first */
clean = strsep(&cleaned, "\n");
/* Break the key-value pair */
key = strtok_r(clean, " =", &saveptr);
value = strtok_r(NULL, " =", &saveptr);
/* Our little grammar */
if (strcmp(key, "UID_PREFIX") == 0) {
USERNAME_PREFIX = value;
} else if (strcmp(key, "UID_MIN") == 0) {
UID_MIN = atoi(value);
} else if (strcmp(key, "UID_MAX") == 0) {
UID_MAX = atoi(value);
} else if (strcmp(key, "DEFAULT_GID") == 0) {
DEFAULT_GID = atoi(value);
} else if (strcmp(key, "SHELL") == 0) {
SHELL = value;
} else if (strcmp(key, "HOME") == 0) {
HOME = value;
} else if (strcmp(key, "GECOS") == 0) {
GECOS = value;
}
/* None of our keywords, next line please*/
}
}
fclose(fp);
}
return 0;
}
/* append a string to buf, move buf forward and reduce buflen */
static int append_to_buf(char **buf, size_t *buflen, const char *string)
{
size_t len = strlen(string) + 1;
if (len == 0) {
errno = EOVERFLOW;
return 1;
}
if (*buflen < len) {
errno = ERANGE;
return 1;
}
memcpy(*buf, string, len);
*buf += len;
*buflen -= len;
return 0;
}
enum nss_status _nss_uidpool_getpwuid_r(uid_t uid, struct passwd *result, char *buf, size_t buflen, int *errnop)
{
*errnop = 0;
parse_config();
/* the last check is a paranoid safeguard against returning root */
if (uid < UID_MIN+1 || uid >= UID_MAX || uid == 0) {
return NSS_STATUS_NOTFOUND;
}
if (!result || !buf) {
return NSS_STATUS_UNAVAIL;
}
result->pw_uid = uid;
result->pw_gid = DEFAULT_GID;
#define ASSIGN_FROM_BUF(x, str) { \
x = buf; \
if (append_to_buf(&buf, &buflen, str) == 1) { \
*errnop = errno; \
return NSS_STATUS_TRYAGAIN; \
} \
}
ASSIGN_FROM_BUF(result->pw_passwd, "!");
ASSIGN_FROM_BUF(result->pw_gecos, GECOS);
ASSIGN_FROM_BUF(result->pw_dir, HOME);
ASSIGN_FROM_BUF(result->pw_shell, SHELL);
/* assume that uid length is 10 (i.e. uid_t is 32-bits) */
if (buflen < strlen(USERNAME_PREFIX) + 10 + 1) {
*errnop = ERANGE;
return NSS_STATUS_TRYAGAIN;
}
sprintf(buf, "%s%u", USERNAME_PREFIX, (uid_t)(uid - UID_MIN));
result->pw_name = buf;
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_uidpool_getpwnam_r(const char *name, struct passwd *result, char *buf, size_t buflen, int *errnop)
{
*errnop = 0;
parse_config();
if (strncmp(name, USERNAME_PREFIX, strlen(USERNAME_PREFIX)) != 0) {
return NSS_STATUS_NOTFOUND;
}
uid_t uid = atoi(name + strlen(USERNAME_PREFIX));
uid += UID_MIN;
return _nss_uidpool_getpwuid_r(uid, result, buf, buflen, errnop);
}