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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <nss.h>
#include <pthread.h>
#include "env-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "log.h"
#include "macro.h"
#include "nss-systemd.h"
#include "nss-util.h"
#include "pthread-util.h"
#include "signal-util.h"
#include "strv.h"
#include "user-record-nss.h"
#include "user-util.h"
#include "userdb-glue.h"
#include "userdb.h"
static const struct passwd root_passwd = {
.pw_name = (char*) "root",
.pw_passwd = (char*) PASSWORD_SEE_SHADOW,
.pw_uid = 0,
.pw_gid = 0,
.pw_gecos = (char*) "Super User",
.pw_dir = (char*) "/root",
.pw_shell = (char*) "/bin/sh",
};
static const struct spwd root_spwd = {
.sp_namp = (char*) "root",
.sp_pwdp = (char*) PASSWORD_LOCKED_AND_INVALID,
.sp_lstchg = -1,
.sp_min = -1,
.sp_max = -1,
.sp_warn = -1,
.sp_inact = -1,
.sp_expire = -1,
.sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
};
static const struct passwd nobody_passwd = {
.pw_name = (char*) NOBODY_USER_NAME,
.pw_passwd = (char*) PASSWORD_LOCKED_AND_INVALID,
.pw_uid = UID_NOBODY,
.pw_gid = GID_NOBODY,
.pw_gecos = (char*) "User Nobody",
.pw_dir = (char*) "/",
.pw_shell = (char*) NOLOGIN,
};
static const struct spwd nobody_spwd = {
.sp_namp = (char*) NOBODY_USER_NAME,
.sp_pwdp = (char*) PASSWORD_LOCKED_AND_INVALID,
.sp_lstchg = -1,
.sp_min = -1,
.sp_max = -1,
.sp_warn = -1,
.sp_inact = -1,
.sp_expire = -1,
.sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
};
static const struct group root_group = {
.gr_name = (char*) "root",
.gr_gid = 0,
.gr_passwd = (char*) PASSWORD_SEE_SHADOW,
.gr_mem = (char*[]) { NULL },
};
static const struct sgrp root_sgrp = {
.sg_namp = (char*) "root",
.sg_passwd = (char*) PASSWORD_LOCKED_AND_INVALID,
};
static const struct group nobody_group = {
.gr_name = (char*) NOBODY_GROUP_NAME,
.gr_gid = GID_NOBODY,
.gr_passwd = (char*) PASSWORD_LOCKED_AND_INVALID,
.gr_mem = (char*[]) { NULL },
};
static const struct sgrp nobody_sgrp = {
.sg_namp = (char*) NOBODY_GROUP_NAME,
.sg_passwd = (char*) PASSWORD_LOCKED_AND_INVALID,
};
typedef struct GetentData {
/* As explained in NOTES section of getpwent_r(3) as 'getpwent_r() is not really reentrant since it
* shares the reading position in the stream with all other threads', we need to protect the data in
* UserDBIterator from multithreaded programs which may call setpwent(), getpwent_r(), or endpwent()
* simultaneously. So, each function locks the data by using the mutex below. */
pthread_mutex_t mutex;
UserDBIterator *iterator;
/* Applies to group iterations only: true while we iterate over groups defined through NSS, false
* otherwise. */
bool by_membership;
} GetentData;
static GetentData getpwent_data = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
};
static GetentData getgrent_data = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
};
static GetentData getspent_data = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
};
static GetentData getsgent_data = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
};
static void setup_logging(void) {
/* We need a dummy function because log_parse_environment is a macro. */
log_parse_environment();
}
static void setup_logging_once(void) {
static pthread_once_t once = PTHREAD_ONCE_INIT;
assert_se(pthread_once(&once, setup_logging) == 0);
}
#define NSS_ENTRYPOINT_BEGIN \
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK); \
setup_logging_once()
NSS_GETPW_PROTOTYPES(systemd);
NSS_GETSP_PROTOTYPES(systemd);
NSS_GETGR_PROTOTYPES(systemd);
NSS_GETSG_PROTOTYPES(systemd);
NSS_PWENT_PROTOTYPES(systemd);
NSS_SPENT_PROTOTYPES(systemd);
NSS_GRENT_PROTOTYPES(systemd);
NSS_SGENT_PROTOTYPES(systemd);
NSS_INITGROUPS_PROTOTYPE(systemd);
enum nss_status _nss_systemd_getpwnam_r(
const char *name,
struct passwd *pwd,
char *buffer, size_t buflen,
int *errnop) {
enum nss_status status;
int e;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(name);
assert(pwd);
assert(errnop);
/* If the username is not valid, then we don't know it. Ideally libc would filter these for us
* anyway. We don't generate EINVAL here, because it isn't really out business to complain about
* invalid user names. */
if (!valid_user_group_name(name, VALID_USER_RELAX))
return NSS_STATUS_NOTFOUND;
/* Synthesize entries for the root and nobody users, in case they are missing in /etc/passwd */
if (getenv_bool_secure("SYSTEMD_NSS_BYPASS_SYNTHETIC") <= 0) {
if (streq(name, root_passwd.pw_name)) {
*pwd = root_passwd;
return NSS_STATUS_SUCCESS;
}
if (streq(name, nobody_passwd.pw_name)) {
if (!synthesize_nobody())
return NSS_STATUS_NOTFOUND;
*pwd = nobody_passwd;
return NSS_STATUS_SUCCESS;
}
} else if (STR_IN_SET(name, root_passwd.pw_name, nobody_passwd.pw_name))
return NSS_STATUS_NOTFOUND;
status = userdb_getpwnam(name, pwd, buffer, buflen, &e);
if (IN_SET(status, NSS_STATUS_UNAVAIL, NSS_STATUS_TRYAGAIN)) {
UNPROTECT_ERRNO;
*errnop = e;
return status;
}
return status;
}
enum nss_status _nss_systemd_getpwuid_r(
uid_t uid,
struct passwd *pwd,
char *buffer, size_t buflen,
int *errnop) {
enum nss_status status;
int e;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(pwd);
assert(errnop);
if (!uid_is_valid(uid))
return NSS_STATUS_NOTFOUND;
/* Synthesize data for the root user and for nobody in case they are missing from /etc/passwd */
if (getenv_bool_secure("SYSTEMD_NSS_BYPASS_SYNTHETIC") <= 0) {
if (uid == root_passwd.pw_uid) {
*pwd = root_passwd;
return NSS_STATUS_SUCCESS;
}
if (uid == nobody_passwd.pw_uid) {
if (!synthesize_nobody())
return NSS_STATUS_NOTFOUND;
*pwd = nobody_passwd;
return NSS_STATUS_SUCCESS;
}
} else if (uid == root_passwd.pw_uid || uid == nobody_passwd.pw_uid)
return NSS_STATUS_NOTFOUND;
status = userdb_getpwuid(uid, pwd, buffer, buflen, &e);
if (IN_SET(status, NSS_STATUS_UNAVAIL, NSS_STATUS_TRYAGAIN)) {
UNPROTECT_ERRNO;
*errnop = e;
return status;
}
return status;
}
enum nss_status _nss_systemd_getspnam_r(
const char *name,
struct spwd *spwd,
char *buffer, size_t buflen,
int *errnop) {
enum nss_status status;
int e;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(name);
assert(spwd);
assert(errnop);
if (!valid_user_group_name(name, VALID_USER_RELAX))
return NSS_STATUS_NOTFOUND;
/* Synthesize entries for the root and nobody users, in case they are missing in /etc/passwd */
if (getenv_bool_secure("SYSTEMD_NSS_BYPASS_SYNTHETIC") <= 0) {
if (streq(name, root_spwd.sp_namp)) {
*spwd = root_spwd;
return NSS_STATUS_SUCCESS;
}
if (streq(name, nobody_spwd.sp_namp)) {
if (!synthesize_nobody())
return NSS_STATUS_NOTFOUND;
*spwd = nobody_spwd;
return NSS_STATUS_SUCCESS;
}
} else if (STR_IN_SET(name, root_spwd.sp_namp, nobody_spwd.sp_namp))
return NSS_STATUS_NOTFOUND;
status = userdb_getspnam(name, spwd, buffer, buflen, &e);
if (IN_SET(status, NSS_STATUS_UNAVAIL, NSS_STATUS_TRYAGAIN)) {
UNPROTECT_ERRNO;
*errnop = e;
return status;
}
return status;
}
#pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
enum nss_status _nss_systemd_getgrnam_r(
const char *name,
struct group *gr,
char *buffer, size_t buflen,
int *errnop) {
enum nss_status status;
int e;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(name);
assert(gr);
assert(errnop);
if (!valid_user_group_name(name, VALID_USER_RELAX))
return NSS_STATUS_NOTFOUND;
/* Synthesize records for root and nobody, in case they are missing from /etc/group */
if (getenv_bool_secure("SYSTEMD_NSS_BYPASS_SYNTHETIC") <= 0) {
if (streq(name, root_group.gr_name)) {
*gr = root_group;
return NSS_STATUS_SUCCESS;
}
if (streq(name, nobody_group.gr_name)) {
if (!synthesize_nobody())
return NSS_STATUS_NOTFOUND;
*gr = nobody_group;
return NSS_STATUS_SUCCESS;
}
} else if (STR_IN_SET(name, root_group.gr_name, nobody_group.gr_name))
return NSS_STATUS_NOTFOUND;
status = userdb_getgrnam(name, gr, buffer, buflen, &e);
if (IN_SET(status, NSS_STATUS_UNAVAIL, NSS_STATUS_TRYAGAIN)) {
UNPROTECT_ERRNO;
*errnop = e;
return status;
}
return status;
}
enum nss_status _nss_systemd_getgrgid_r(
gid_t gid,
struct group *gr,
char *buffer, size_t buflen,
int *errnop) {
enum nss_status status;
int e;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(gr);
assert(errnop);
if (!gid_is_valid(gid))
return NSS_STATUS_NOTFOUND;
/* Synthesize records for root and nobody, in case they are missing from /etc/group */
if (getenv_bool_secure("SYSTEMD_NSS_BYPASS_SYNTHETIC") <= 0) {
if (gid == root_group.gr_gid) {
*gr = root_group;
return NSS_STATUS_SUCCESS;
}
if (gid == nobody_group.gr_gid) {
if (!synthesize_nobody())
return NSS_STATUS_NOTFOUND;
*gr = nobody_group;
return NSS_STATUS_SUCCESS;
}
} else if (gid == root_group.gr_gid || gid == nobody_group.gr_gid)
return NSS_STATUS_NOTFOUND;
status = userdb_getgrgid(gid, gr, buffer, buflen, &e);
if (IN_SET(status, NSS_STATUS_UNAVAIL, NSS_STATUS_TRYAGAIN)) {
UNPROTECT_ERRNO;
*errnop = e;
return status;
}
return status;
}
enum nss_status _nss_systemd_getsgnam_r(
const char *name,
struct sgrp *sgrp,
char *buffer, size_t buflen,
int *errnop) {
enum nss_status status;
int e;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(name);
assert(sgrp);
assert(errnop);
if (!valid_user_group_name(name, VALID_USER_RELAX))
return NSS_STATUS_NOTFOUND;
/* Synthesize records for root and nobody, in case they are missing from /etc/group */
if (getenv_bool_secure("SYSTEMD_NSS_BYPASS_SYNTHETIC") <= 0) {
if (streq(name, root_sgrp.sg_namp)) {
*sgrp = root_sgrp;
return NSS_STATUS_SUCCESS;
}
if (streq(name, nobody_sgrp.sg_namp)) {
if (!synthesize_nobody())
return NSS_STATUS_NOTFOUND;
*sgrp = nobody_sgrp;
return NSS_STATUS_SUCCESS;
}
} else if (STR_IN_SET(name, root_sgrp.sg_namp, nobody_sgrp.sg_namp))
return NSS_STATUS_NOTFOUND;
status = userdb_getsgnam(name, sgrp, buffer, buflen, &e);
if (IN_SET(status, NSS_STATUS_UNAVAIL, NSS_STATUS_TRYAGAIN)) {
UNPROTECT_ERRNO;
*errnop = e;
return status;
}
return status;
}
static enum nss_status nss_systemd_endent(GetentData *p) {
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(p);
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&p->mutex);
(void) _l; /* make llvm shut up about _l not being used. */
p->iterator = userdb_iterator_free(p->iterator);
p->by_membership = false;
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_endpwent(void) {
return nss_systemd_endent(&getpwent_data);
}
enum nss_status _nss_systemd_endspent(void) {
return nss_systemd_endent(&getspent_data);
}
enum nss_status _nss_systemd_endgrent(void) {
return nss_systemd_endent(&getgrent_data);
}
enum nss_status _nss_systemd_endsgent(void) {
return nss_systemd_endent(&getsgent_data);
}
enum nss_status _nss_systemd_setpwent(int stayopen) {
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getpwent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
getpwent_data.iterator = userdb_iterator_free(getpwent_data.iterator);
getpwent_data.by_membership = false;
/* Don't synthesize root/nobody when iterating. Let nss-files take care of that. If the two records
* are missing there, then that's fine, after all getpwent() is known to be possibly incomplete
* (think: LDAP/NIS type situations), and our synthesizing of root/nobody is a robustness fallback
* only, which matters for getpwnam()/getpwuid() primarily, which are the main NSS entrypoints to the
* user database. */
r = userdb_all(nss_glue_userdb_flags() | USERDB_DONT_SYNTHESIZE, &getpwent_data.iterator);
return r < 0 ? NSS_STATUS_UNAVAIL : NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_setgrent(int stayopen) {
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getgrent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
getgrent_data.iterator = userdb_iterator_free(getgrent_data.iterator);
getgrent_data.by_membership = false;
/* See _nss_systemd_setpwent() for an explanation why we use USERDB_DONT_SYNTHESIZE here */
r = groupdb_all(nss_glue_userdb_flags() | USERDB_DONT_SYNTHESIZE, &getgrent_data.iterator);
return r < 0 ? NSS_STATUS_UNAVAIL : NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_setspent(int stayopen) {
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getspent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
getspent_data.iterator = userdb_iterator_free(getspent_data.iterator);
getspent_data.by_membership = false;
/* See _nss_systemd_setpwent() for an explanation why we use USERDB_DONT_SYNTHESIZE here */
r = userdb_all(nss_glue_userdb_flags() | USERDB_DONT_SYNTHESIZE, &getspent_data.iterator);
return r < 0 ? NSS_STATUS_UNAVAIL : NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_setsgent(int stayopen) {
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getsgent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
getsgent_data.iterator = userdb_iterator_free(getsgent_data.iterator);
getsgent_data.by_membership = false;
/* See _nss_systemd_setpwent() for an explanation why we use USERDB_DONT_SYNTHESIZE here */
r = groupdb_all(nss_glue_userdb_flags() | USERDB_DONT_SYNTHESIZE, &getsgent_data.iterator);
return r < 0 ? NSS_STATUS_UNAVAIL : NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_getpwent_r(
struct passwd *result,
char *buffer, size_t buflen,
int *errnop) {
_cleanup_(user_record_unrefp) UserRecord *ur = NULL;
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(result);
assert(errnop);
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getpwent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
if (!getpwent_data.iterator) {
UNPROTECT_ERRNO;
*errnop = EHOSTDOWN;
return NSS_STATUS_UNAVAIL;
}
r = userdb_iterator_get(getpwent_data.iterator, &ur);
if (r == -ESRCH)
return NSS_STATUS_NOTFOUND;
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
r = nss_pack_user_record(ur, result, buffer, buflen);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_TRYAGAIN;
}
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_getgrent_r(
struct group *result,
char *buffer, size_t buflen,
int *errnop) {
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
_cleanup_free_ char **members = NULL;
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(result);
assert(errnop);
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getgrent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
if (!getgrent_data.iterator) {
UNPROTECT_ERRNO;
*errnop = EHOSTDOWN;
return NSS_STATUS_UNAVAIL;
}
if (!getgrent_data.by_membership) {
r = groupdb_iterator_get(getgrent_data.iterator, &gr);
if (r == -ESRCH) {
/* So we finished iterating native groups now. Let's now continue with iterating
* native memberships, and generate additional group entries for any groups
* referenced there that are defined in NSS only. This means for those groups there
* will be two or more entries generated during iteration, but this is apparently how
* this is supposed to work, and what other implementations do too. Clients are
* supposed to merge the group records found during iteration automatically. */
getgrent_data.iterator = userdb_iterator_free(getgrent_data.iterator);
r = membershipdb_all(nss_glue_userdb_flags(), &getgrent_data.iterator);
if (r < 0 && r != -ESRCH) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
getgrent_data.by_membership = true;
} else if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
} else if (!STR_IN_SET(gr->group_name, root_group.gr_name, nobody_group.gr_name)) {
r = membershipdb_by_group_strv(gr->group_name, nss_glue_userdb_flags(), &members);
if (r < 0 && r != -ESRCH) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
}
}
if (getgrent_data.by_membership) {
_cleanup_(_nss_systemd_unblockp) bool blocked = false;
if (!getgrent_data.iterator)
return NSS_STATUS_NOTFOUND;
for (;;) {
_cleanup_free_ char *user_name = NULL, *group_name = NULL;
r = membershipdb_iterator_get(getgrent_data.iterator, &user_name, &group_name);
if (r == -ESRCH)
return NSS_STATUS_NOTFOUND;
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
if (STR_IN_SET(user_name, root_passwd.pw_name, nobody_passwd.pw_name))
continue;
if (STR_IN_SET(group_name, root_group.gr_name, nobody_group.gr_name))
continue;
/* We are about to recursively call into NSS, let's make sure we disable recursion into our own code. */
if (!blocked) {
r = _nss_systemd_block(true);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
blocked = true;
}
r = nss_group_record_by_name(group_name, false, &gr);
if (r == -ESRCH)
continue;
if (r < 0) {
log_debug_errno(r, "Failed to do NSS check for group '%s', ignoring: %m", group_name);
continue;
}
members = strv_new(user_name);
if (!members) {
UNPROTECT_ERRNO;
*errnop = ENOMEM;
return NSS_STATUS_TRYAGAIN;
}
/* Note that we currently generate one group entry per user that is part of a
* group. It's a bit ugly, but equivalent to generating a single entry with a set of
* members in them. */
break;
}
}
r = nss_pack_group_record(gr, members, result, buffer, buflen);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_TRYAGAIN;
}
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_getspent_r(
struct spwd *result,
char *buffer, size_t buflen,
int *errnop) {
_cleanup_(user_record_unrefp) UserRecord *ur = NULL;
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(result);
assert(errnop);
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getspent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
if (!getspent_data.iterator) {
UNPROTECT_ERRNO;
*errnop = EHOSTDOWN;
return NSS_STATUS_UNAVAIL;
}
for (;;) {
r = userdb_iterator_get(getspent_data.iterator, &ur);
if (r == -ESRCH)
return NSS_STATUS_NOTFOUND;
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
if (!ur->incomplete) /* don't synthesize shadow records for records where we couldn't read shadow data */
break;
ur = user_record_unref(ur);
}
r = nss_pack_user_record_shadow(ur, result, buffer, buflen);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_TRYAGAIN;
}
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_getsgent_r(
struct sgrp *result,
char *buffer, size_t buflen,
int *errnop) {
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(result);
assert(errnop);
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
_cleanup_(pthread_mutex_unlock_assertp) pthread_mutex_t *_l = pthread_mutex_lock_assert(&getsgent_data.mutex);
(void) _l; /* make llvm shut up about _l not being used. */
if (!getsgent_data.iterator) {
UNPROTECT_ERRNO;
*errnop = EHOSTDOWN;
return NSS_STATUS_UNAVAIL;
}
for (;;) {
r = groupdb_iterator_get(getsgent_data.iterator, &gr);
if (r == -ESRCH)
return NSS_STATUS_NOTFOUND;
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
if (!gr->incomplete) /* don't synthesize shadow records for records where we couldn't read shadow data */
break;
gr = group_record_unref(gr);
}
r = nss_pack_group_record_shadow(gr, result, buffer, buflen);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_TRYAGAIN;
}
return NSS_STATUS_SUCCESS;
}
enum nss_status _nss_systemd_initgroups_dyn(
const char *user_name,
gid_t gid,
long *start,
long *size,
gid_t **groupsp,
long int limit,
int *errnop) {
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
bool any = false;
int r;
PROTECT_ERRNO;
NSS_ENTRYPOINT_BEGIN;
assert(user_name);
assert(start);
assert(size);
assert(groupsp);
assert(errnop);
if (!valid_user_group_name(user_name, VALID_USER_RELAX))
return NSS_STATUS_NOTFOUND;
/* Don't allow extending these two special users, the same as we won't resolve them via getpwnam() */
if (STR_IN_SET(user_name, root_passwd.pw_name, nobody_passwd.pw_name))
return NSS_STATUS_NOTFOUND;
if (_nss_systemd_is_blocked())
return NSS_STATUS_NOTFOUND;
r = membershipdb_by_user(user_name, nss_glue_userdb_flags(), &iterator);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
for (;;) {
_cleanup_(group_record_unrefp) GroupRecord *g = NULL;
_cleanup_free_ char *group_name = NULL;
r = membershipdb_iterator_get(iterator, NULL, &group_name);
if (r == -ESRCH)
break;
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
/* The group might be defined via traditional NSS only, hence let's do a full look-up without
* disabling NSS. This means we are operating recursively here. */
r = groupdb_by_name(group_name, (nss_glue_userdb_flags() & ~USERDB_EXCLUDE_NSS) | USERDB_SUPPRESS_SHADOW, &g);
if (r == -ESRCH)
continue;
if (r < 0) {
log_debug_errno(r, "Failed to resolve group '%s', ignoring: %m", group_name);
continue;
}
if (g->gid == gid)
continue;
if (*start >= *size) {
gid_t *new_groups;
long new_size;
if (limit > 0 && *size >= limit) /* Reached the limit.? */
break;
if (*size > LONG_MAX/2) { /* Check for overflow */
UNPROTECT_ERRNO;
*errnop = ENOMEM;
return NSS_STATUS_TRYAGAIN;
}
new_size = *start * 2;
if (limit > 0 && new_size > limit)
new_size = limit;
/* Enlarge buffer */
new_groups = reallocarray(*groupsp, new_size, sizeof(**groupsp));
if (!new_groups) {
UNPROTECT_ERRNO;
*errnop = ENOMEM;
return NSS_STATUS_TRYAGAIN;
}
*groupsp = new_groups;
*size = new_size;
}
(*groupsp)[(*start)++] = g->gid;
any = true;
}
return any ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND;
}
static thread_local unsigned _blocked = 0;
_public_ int _nss_systemd_block(bool b) {
/* This blocks recursively: it's blocked for as many times this function is called with `true` until
* it is called an equal time with `false`. */
if (b) {
if (_blocked >= UINT_MAX)
return -EOVERFLOW;
_blocked++;
} else {
if (_blocked <= 0)
return -EOVERFLOW;
_blocked--;
}
return b; /* Return what is passed in, i.e. the new state from the PoV of the caller */
}
_public_ bool _nss_systemd_is_blocked(void) {
return _blocked > 0;
}
|