forked from aad-for-linux/libnss-aad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnss_aad.c
495 lines (390 loc) · 13 KB
/
libnss_aad.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include <crypt.h>
#include <curl/curl.h>
#include <fcntl.h>
#include <grp.h>
#include <inttypes.h>
#include <jansson.h>
#include <nss.h>
#include <pwd.h>
#include <sds/sds.h>
#include <shadow.h>
#include <sodium.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define CONF_FILE "/etc/libnss-aad.conf"
#define MAX_PASSWD_LENGTH 32
#define MIN_GID 1000
#define MIN_UID 1000
#define PASSWD_FILE "/etc/passwd"
#define RESOURCE_ID "00000002-0000-0000-c000-000000000000"
#define SHADOW_FILE "/etc/shadow"
#define SHELL "/bin/sh"
#define USER_AGENT "libnss_aad/1.0"
#define USER_FIELD "mailNickname"
struct charset {
char const *const c;
uint32_t const l;
};
struct response {
char *data;
size_t size;
};
static size_t response_callback(void *contents, size_t size, size_t nmemb,
void *userp)
{
size_t realsize = size * nmemb;
struct response *resp = (struct response *) userp;
char *ptr = realloc(resp->data, resp->size + realsize + 1);
if (ptr == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
resp->data = ptr;
memcpy(&(resp->data[resp->size]), contents, realsize);
resp->size += realsize;
resp->data[resp->size] = 0;
return realsize;
}
static char *get_static(char **buffer, size_t *buflen, int len)
{
char *result;
if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
return NULL;
}
result = *buffer;
*buffer += len;
*buflen -= len;
return result;
}
static char *generate_passwd(void)
{
if (sodium_init() < 0) {
fprintf(stderr, "libsodium could not be initialized\n");
return NULL;
}
uintmax_t const length = MAX_PASSWD_LENGTH;
struct charset lower = {
"abcdefghijklmnopqrstuvwxyz",
(uint32_t) strlen(lower.c)
};
struct charset numeric = {
"0123456789",
(uint32_t) strlen(numeric.c)
};
struct charset special = {
"!@#$%^&*()-_=+`~[]{}\\|;:'\",.<>/?",
(uint32_t) strlen(special.c)
};
struct charset upper = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
(uint32_t) strlen(upper.c)
};
uint32_t const chars_l = lower.l + numeric.l + special.l + upper.l;
char *const chars = malloc(chars_l + 1);
if (chars == NULL) {
fprintf(stderr, "failed to allocate memory for string\n");
return NULL;
}
chars[0] = '\0';
char *endptr = chars;
char *passwd = (char *) malloc((length + 1) * sizeof(char));
if (passwd == NULL) {
fprintf(stderr, "failed to allocate memory for string\n");
return NULL;
}
memcpy(endptr, lower.c, lower.l);
endptr += lower.l;
memcpy(endptr, numeric.c, numeric.l);
endptr += numeric.l;
memcpy(endptr, special.c, special.l);
endptr += special.l;
memcpy(endptr, upper.c, upper.l);
for (uintmax_t i = 0; i < length; ++i) {
passwd[i] = chars[randombytes_uniform(chars_l)];
}
passwd[length + 1] = '\0';
free(chars);
char entropy[16];
int fd;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
printf("Can't open /dev/urandom\n");
return NULL;
}
if (read(fd, entropy, sizeof(entropy)) != sizeof(entropy)) {
printf("Not enough entropy\n");
return NULL;
}
close(fd);
return crypt(passwd,
crypt_gensalt("$2a$", 12, entropy, sizeof(entropy)));
}
static json_t *get_oauth2_token(const char *client_id,
const char *client_secret,
const char *domain, bool debug)
{
CURL *curl_handle;
CURLcode res;
json_t *token_data, *token;
json_error_t error;
struct response resp;
resp.data = malloc(1);
resp.size = 0;
/* https://login.microsoftonline.com/<domain>/oauth2/token */
sds endpoint = sdsnew("https://login.microsoftonline.com/");
endpoint = sdscat(endpoint, domain);
endpoint = sdscat(endpoint, "/oauth2/token");
sds post_body = sdsnew("grant_type=client_credentials&client_secret=");
post_body = sdscat(post_body, client_secret);
post_body = sdscat(post_body, "&client_id=");
post_body = sdscat(post_body, client_id);
post_body = sdscat(post_body, "&resource=");
post_body = sdscat(post_body, RESOURCE_ID);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, endpoint);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, post_body);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
response_callback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &resp);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT);
if (debug)
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl_handle);
/* check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
} else {
token_data = json_loads(resp.data, 0, &error);
if (!token_data) {
fprintf(stderr, "json_loads() failed: %s\n", error.text);
return NULL;
}
}
curl_easy_cleanup(curl_handle);
sdsfree(endpoint);
sdsfree(post_body);
free(resp.data);
token = json_object_get(token_data, "access_token");
return (token) ? token : NULL;
}
static int verify_user(json_t * auth_token, const char *domain,
const char *name, bool debug)
{
CURL *curl_handle;
CURLcode res;
json_t *user_data;
json_error_t error;
sds auth_header = sdsnew("Authorization: Bearer ");
sds endpoint = sdsnew("https://graph.windows.net/");
struct response resp;
struct curl_slist *headers = NULL;
const char *user_field;
resp.data = malloc(1);
resp.size = 0;
auth_header = sdscat(auth_header, json_string_value(auth_token));
headers = curl_slist_append(headers, auth_header);
/* https://graph.windows.net/<domain>/users/<username>@<domain>?api-version=1.6 */
endpoint = sdscat(endpoint, domain);
endpoint = sdscat(endpoint, "/users/");
endpoint = sdscat(endpoint, name);
endpoint = sdscat(endpoint, "@");
endpoint = sdscat(endpoint, domain);
endpoint = sdscat(endpoint, "?api-version=1.6");
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, endpoint);
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
response_callback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &resp);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 1L);
if (debug)
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl_handle);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
} else {
user_data = json_loads(resp.data, 0, &error);
if (!user_data) {
fprintf(stderr, "json_loads() failed: %s\n", error.text);
return EXIT_FAILURE;
}
if (json_object_get(user_data, "odata.error")) {
fprintf(stderr, "returned odata.error\n");
return EXIT_FAILURE;
}
}
curl_easy_cleanup(curl_handle);
curl_slist_free_all(headers);
sdsfree(auth_header);
sdsfree(endpoint);
free(resp.data);
user_field = json_string_value(json_object_get(user_data, USER_FIELD));
return (user_field
&& strcmp(user_field,
name) == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
static int write_entry(const char *fp, void *userp)
{
int ret = EXIT_FAILURE;
FILE *fd = fopen(fp, "a");
if (fd) {
fseek(fd, 0, SEEK_END);
if (strcmp(fp, PASSWD_FILE) == 0) {
struct passwd *p = (struct passwd *) userp;
ret = putpwent(p, fd);
}
if (strcmp(fp, SHADOW_FILE) == 0) {
struct spwd *s = (struct spwd *) userp;
ret = putspent(s, fd);
}
fclose(fd);
}
return ret;
}
enum nss_status _nss_aad_getpwnam_r(const char *name, struct passwd *p,
char *buffer, size_t buflen,
int *errnop)
{
bool debug = false;
const char *client_id, *client_secret, *domain, *shell;
json_t *config, *client, *shell_cfg, *token, *user_cfg;
json_error_t error;
int ret = 0, user_id = MIN_UID;
sds home_dir = sdsnew("/home/");
struct group *group;
struct passwd *user;
(void) (errnop); /* unused-parameter */
config = json_load_file(CONF_FILE, 0, &error);
if (!config) {
fprintf(stderr, "error in config on line %d: %s\n", error.line,
error.text);
return NSS_STATUS_NOTFOUND;
}
if (json_object_get(config, "debug"))
if (strcmp
(json_string_value(json_object_get(config, "debug")),
"true") == 0)
debug = true;
if (json_object_get(config, "client")) {
client = json_object_get(config, "client");
} else {
fprintf(stderr, "error with Client in JSON\n");
return ret;
}
if (json_object_get(client, "id")) {
client_id = json_string_value(json_object_get(client, "id"));
} else {
fprintf(stderr, "error with Client ID in JSON\n");
return ret;
}
if (json_object_get(client, "secret")) {
client_secret =
json_string_value(json_object_get(client, "secret"));
} else {
fprintf(stderr, "error with Client Secret in JSON\n");
return ret;
}
if (json_object_get(config, "domain")) {
domain = json_string_value(json_object_get(config, "domain"));
} else {
fprintf(stderr, "error with Domain in JSON\n");
return ret;
}
if (json_object_get(config, "user")) {
user_cfg = json_object_get(config, "user");
} else {
fprintf(stderr, "error with User in JSON\n");
return ret;
}
user = getpwuid(user_id);
if (json_object_get(user_cfg, "group")) {
group =
getgrnam(json_string_value
(json_object_get(user_cfg, "group")));
} else {
fprintf(stderr, "error with Group in JSON\n");
return ret;
}
if (json_object_get(user_cfg, "shell")) {
shell_cfg = json_object_get(user_cfg, "shell");
}
shell = (shell_cfg) ? json_string_value(shell_cfg) : sdsnew(SHELL);
if (!shell) {
fprintf(stderr, "error with Shell in JSON\n");
return ret;
}
home_dir = sdscat(home_dir, name);
if (!home_dir) {
fprintf(stderr, "error with HOME directory\n");
return ret;
}
curl_global_init(CURL_GLOBAL_ALL);
token = get_oauth2_token(client_id, client_secret, domain, debug);
ret = verify_user(token, domain, name, debug);
curl_global_cleanup();
if (!ret) {
if ((p->pw_name =
get_static(&buffer, &buflen, strlen(name) + 1)) == NULL)
return NSS_STATUS_TRYAGAIN;
strcpy(p->pw_name, name);
if ((p->pw_passwd =
get_static(&buffer, &buflen, strlen("x") + 1)) == NULL)
return NSS_STATUS_TRYAGAIN;
strcpy(p->pw_passwd, "x");
while (user != NULL) {
user = getpwuid(++user_id);
}
p->pw_uid = user_id;
p->pw_gid = (group) ? group->gr_gid : MIN_GID;
if ((p->pw_gecos =
get_static(&buffer, &buflen, strlen("\0") + 1)) == NULL)
return NSS_STATUS_TRYAGAIN;
strcpy(p->pw_gecos, "\0");
if ((p->pw_dir =
get_static(&buffer, &buflen, strlen(home_dir) + 1)) == NULL)
return NSS_STATUS_TRYAGAIN;
strcpy(p->pw_dir, home_dir);
if ((p->pw_shell =
get_static(&buffer, &buflen, strlen(shell) + 1)) == NULL)
return NSS_STATUS_TRYAGAIN;
strcpy(p->pw_shell, shell);
write_entry(PASSWD_FILE, p);
return NSS_STATUS_SUCCESS;
}
return NSS_STATUS_TRYAGAIN;
}
enum nss_status _nss_aad_getspnam_r(const char *name, struct spwd *s,
char *buffer, size_t buflen,
int *errnop)
{
(void) (errnop); /* unused-parameter */
/* If out of memory */
if ((s->sp_namp =
get_static(&buffer, &buflen, strlen(name) + 1)) == NULL) {
return NSS_STATUS_TRYAGAIN;
}
strcpy(s->sp_namp, name);
if ((s->sp_pwdp =
get_static(&buffer, &buflen, MAX_PASSWD_LENGTH + 1)) == NULL) {
return NSS_STATUS_TRYAGAIN;
}
char *passwd = generate_passwd();
if (passwd == NULL)
return NSS_STATUS_TRYAGAIN;
strcpy(s->sp_pwdp, passwd);
write_entry(SHADOW_FILE, s);
s->sp_lstchg = 13571;
s->sp_min = 0;
s->sp_max = 99999;
s->sp_warn = 7;
return NSS_STATUS_SUCCESS;
}